Debian Codegraph @

libgo/go/internal/poll/splice_linux_test.go

raw
1 // Copyright 2021 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 package poll_test
6
7 import (
8 "internal/poll"
9 "runtime"
10 "sync"
11 "sync/atomic"
12 "testing"
13 "time"
14 )
15
16 var closeHook atomic.Value // func(fd int)
17
18 func init() {
19 closeFunc := poll.CloseFunc
20 poll.CloseFunc = func(fd int) (err error) {
21 if v := closeHook.Load(); v != nil {
22 if hook := v.(func(int)); hook != nil {
23 hook(fd)
24 }
25 }
26 return closeFunc(fd)
27 }
28 }
29
30 func TestSplicePipePool(t *testing.T) {
31 if runtime.Compiler == "gccgo" {
32 t.Skip("gofrontend conservative stack collection causes this test to fail")
33 }
34
35 const N = 64
36 var (
37 p *poll.SplicePipe
38 ps []*poll.SplicePipe
39 allFDs []int
40 pendingFDs sync.Map // fd → struct{}{}
41 err error
42 )
43
44 closeHook.Store(func(fd int) { pendingFDs.Delete(fd) })
45 t.Cleanup(func() { closeHook.Store((func(int))(nil)) })
46
47 for i := 0; i < N; i++ {
48 p, _, err = poll.GetPipe()
49 if err != nil {
50 t.Skipf("failed to create pipe due to error(%v), skip this test", err)
51 }
52 _, pwfd := poll.GetPipeFds(p)
53 allFDs = append(allFDs, pwfd)
54 pendingFDs.Store(pwfd, struct{}{})
55 ps = append(ps, p)
56 }
57 for _, p = range ps {
58 poll.PutPipe(p)
59 }
60 ps = nil
61 p = nil
62
63 // Exploit the timeout of "go test" as a timer for the subsequent verification.
64 timeout := 5 * time.Minute
65 if deadline, ok := t.Deadline(); ok {
66 timeout = deadline.Sub(time.Now())
67 timeout -= timeout / 10 // Leave 10% headroom for cleanup.
68 }
69 expiredTime := time.NewTimer(timeout)
70 defer expiredTime.Stop()
71
72 // Trigger garbage collection repeatedly, waiting for all pipes in sync.Pool
73 // to either be deallocated and closed, or to time out.
74 for {
75 runtime.GC()
76 time.Sleep(10 * time.Millisecond)
77
78 // Detect whether all pipes are closed properly.
79 var leakedFDs []int
80 pendingFDs.Range(func(k, v any) bool {
81 leakedFDs = append(leakedFDs, k.(int))
82 return true
83 })
84 if len(leakedFDs) == 0 {
85 break
86 }
87
88 select {
89 case <-expiredTime.C:
90 t.Logf("all descriptors: %v", allFDs)
91 t.Fatalf("leaked descriptors: %v", leakedFDs)
92 default:
93 }
94 }
95 }
96
97 func BenchmarkSplicePipe(b *testing.B) {
98 b.Run("SplicePipeWithPool", func(b *testing.B) {
99 for i := 0; i < b.N; i++ {
100 p, _, err := poll.GetPipe()
101 if err != nil {
102 continue
103 }
104 poll.PutPipe(p)
105 }
106 })
107 b.Run("SplicePipeWithoutPool", func(b *testing.B) {
108 for i := 0; i < b.N; i++ {
109 p := poll.NewPipe()
110 if p == nil {
111 b.Skip("newPipe returned nil")
112 }
113 poll.DestroyPipe(p)
114 }
115 })
116 }
117
118 func BenchmarkSplicePipePoolParallel(b *testing.B) {
119 b.RunParallel(func(pb *testing.PB) {
120 for pb.Next() {
121 p, _, err := poll.GetPipe()
122 if err != nil {
123 continue
124 }
125 poll.PutPipe(p)
126 }
127 })
128 }
129
130 func BenchmarkSplicePipeNativeParallel(b *testing.B) {
131 b.RunParallel(func(pb *testing.PB) {
132 for pb.Next() {
133 p := poll.NewPipe()
134 if p == nil {
135 b.Skip("newPipe returned nil")
136 }
137 poll.DestroyPipe(p)
138 }
139 })
140 }