Skip to content

Commit bc1fc56

Browse files
committed
runtime trace
1 parent 86766ab commit bc1fc56

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pprof/03-trace.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"os"
7+
"runtime"
8+
"runtime/trace"
9+
"sync"
10+
)
11+
12+
func counter(wg *sync.WaitGroup) {
13+
wg.Done()
14+
slice := []int{0}
15+
c := 1
16+
for i := 0; i < 100000; i++ {
17+
mutex.Lock()
18+
c = i + 1 + 2 + 3 + 4 + 5
19+
slice = append(slice, c)
20+
mutex.Unlock()
21+
}
22+
}
23+
24+
func main() {
25+
runtime.GOMAXPROCS(5)
26+
var traceProfile = flag.String("traceprofile", "", "write trace profile to file")
27+
flag.Parse()
28+
if *traceProfile != "" {
29+
f, err := os.Create(*traceProfile)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
trace.Start(f)
34+
defer f.Close()
35+
defer trace.Stop()
36+
}
37+
38+
var wg sync.WaitGroup
39+
wg.Add(3)
40+
for i := 0; i < 3; i++ {
41+
go counter(&wg)
42+
}
43+
wg.Wait()
44+
}

0 commit comments

Comments
 (0)