]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/trace/v2/testdata/testprog/cgo-callback.go
runtime: add execution tracer v2 behind GOEXPERIMENT=exectracer2
[gostls13.git] / src / internal / trace / v2 / testdata / testprog / cgo-callback.go
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Tests CPU profiling.
6
7 //go:build ignore
8
9 package main
10
11 /*
12 #include <pthread.h>
13
14 void go_callback();
15 void go_callback2();
16
17 static void *thr(void *arg) {
18     go_callback();
19     return 0;
20 }
21
22 static void foo() {
23     pthread_t th;
24     pthread_attr_t attr;
25     pthread_attr_init(&attr);
26     pthread_attr_setstacksize(&attr, 256 << 10);
27     pthread_create(&th, &attr, thr, 0);
28     pthread_join(th, 0);
29 }
30
31 static void bar() {
32     go_callback2();
33 }
34 */
35 import "C"
36
37 import (
38         "log"
39         "os"
40         "runtime"
41         "runtime/trace"
42 )
43
44 //export go_callback
45 func go_callback() {
46         // Do another call into C, just to test that path too.
47         C.bar()
48 }
49
50 //export go_callback2
51 func go_callback2() {
52         runtime.GC()
53 }
54
55 func main() {
56         // Start tracing.
57         if err := trace.Start(os.Stdout); err != nil {
58                 log.Fatalf("failed to start tracing: %v", err)
59         }
60
61         // Do a whole bunch of cgocallbacks.
62         const n = 10
63         done := make(chan bool)
64         for i := 0; i < n; i++ {
65                 go func() {
66                         C.foo()
67                         done <- true
68                 }()
69         }
70         for i := 0; i < n; i++ {
71                 <-done
72         }
73
74         // Do something to steal back any Ps from the Ms, just
75         // for coverage.
76         runtime.GC()
77
78         // End of traced execution.
79         trace.Stop()
80 }