1
2
3
4
5 package main
6
7 import (
8 "io"
9 "os"
10 "runtime"
11 "runtime/pprof"
12 "sync"
13 "time"
14 )
15
16 const spawnGCMaxDepth = 5
17
18 func init() {
19 register("SpawnGC", SpawnGC)
20 register("DaisyChain", DaisyChain)
21 }
22
23 func spawnGC(i int) {
24 prof := pprof.Lookup("goroutineleak")
25 if i == 0 {
26 return
27 }
28 wg := &sync.WaitGroup{}
29 wg.Add(i + 1)
30 go func() {
31 wg.Done()
32 <-make(chan int)
33 }()
34 for j := 0; j < i; j++ {
35 go func() {
36 wg.Done()
37 spawnGC(i - 1)
38 }()
39 }
40 wg.Wait()
41 runtime.Gosched()
42 if i == spawnGCMaxDepth {
43 prof.WriteTo(os.Stdout, 2)
44 } else {
45
46
47
48
49
50 prof.WriteTo(io.Discard, 2)
51 }
52 }
53
54
55
56
57 func SpawnGC() {
58 spawnGC(spawnGCMaxDepth)
59 }
60
61
62
63
64
65
66
67 func DaisyChain() {
68 prof := pprof.Lookup("goroutineleak")
69 defer func() {
70 time.Sleep(time.Second)
71 prof.WriteTo(os.Stdout, 2)
72 }()
73 var chain func(i int, ch chan struct{})
74 chain = func(i int, ch chan struct{}) {
75 if i <= 0 {
76 go func() {
77 time.Sleep(time.Hour)
78 ch <- struct{}{}
79 }()
80 return
81 }
82 ch2 := make(chan struct{})
83 go chain(i-1, ch2)
84 <-ch2
85 ch <- struct{}{}
86 }
87
88 go chain(1000, make(chan struct{}, 1))
89 }
90
View as plain text