pprof_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2018 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. package pprof
  5. import (
  6. "bytes"
  7. "fmt"
  8. "internal/profile"
  9. "internal/testenv"
  10. "io"
  11. "net/http"
  12. "net/http/httptest"
  13. "runtime"
  14. "runtime/pprof"
  15. "strings"
  16. "sync"
  17. "sync/atomic"
  18. "testing"
  19. "time"
  20. )
  21. // TestDescriptions checks that the profile names under runtime/pprof package
  22. // have a key in the description map.
  23. func TestDescriptions(t *testing.T) {
  24. for _, p := range pprof.Profiles() {
  25. _, ok := profileDescriptions[p.Name()]
  26. if ok != true {
  27. t.Errorf("%s does not exist in profileDescriptions map\n", p.Name())
  28. }
  29. }
  30. }
  31. func TestHandlers(t *testing.T) {
  32. testCases := []struct {
  33. path string
  34. handler http.HandlerFunc
  35. statusCode int
  36. contentType string
  37. contentDisposition string
  38. resp []byte
  39. }{
  40. {"/debug/pprof/<script>scripty<script>", Index, http.StatusNotFound, "text/plain; charset=utf-8", "", []byte("Unknown profile\n")},
  41. {"/debug/pprof/heap", Index, http.StatusOK, "application/octet-stream", `attachment; filename="heap"`, nil},
  42. {"/debug/pprof/heap?debug=1", Index, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  43. {"/debug/pprof/cmdline", Cmdline, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  44. {"/debug/pprof/profile?seconds=1", Profile, http.StatusOK, "application/octet-stream", `attachment; filename="profile"`, nil},
  45. {"/debug/pprof/symbol", Symbol, http.StatusOK, "text/plain; charset=utf-8", "", nil},
  46. {"/debug/pprof/trace", Trace, http.StatusOK, "application/octet-stream", `attachment; filename="trace"`, nil},
  47. {"/debug/pprof/mutex", Index, http.StatusOK, "application/octet-stream", `attachment; filename="mutex"`, nil},
  48. {"/debug/pprof/block?seconds=1", Index, http.StatusOK, "application/octet-stream", `attachment; filename="block-delta"`, nil},
  49. {"/debug/pprof/goroutine?seconds=1", Index, http.StatusOK, "application/octet-stream", `attachment; filename="goroutine-delta"`, nil},
  50. {"/debug/pprof/", Index, http.StatusOK, "text/html; charset=utf-8", "", []byte("Types of profiles available:")},
  51. }
  52. for _, tc := range testCases {
  53. t.Run(tc.path, func(t *testing.T) {
  54. req := httptest.NewRequest("GET", "http://example.com"+tc.path, nil)
  55. w := httptest.NewRecorder()
  56. tc.handler(w, req)
  57. resp := w.Result()
  58. if got, want := resp.StatusCode, tc.statusCode; got != want {
  59. t.Errorf("status code: got %d; want %d", got, want)
  60. }
  61. body, err := io.ReadAll(resp.Body)
  62. if err != nil {
  63. t.Errorf("when reading response body, expected non-nil err; got %v", err)
  64. }
  65. if got, want := resp.Header.Get("X-Content-Type-Options"), "nosniff"; got != want {
  66. t.Errorf("X-Content-Type-Options: got %q; want %q", got, want)
  67. }
  68. if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want {
  69. t.Errorf("Content-Type: got %q; want %q", got, want)
  70. }
  71. if got, want := resp.Header.Get("Content-Disposition"), tc.contentDisposition; got != want {
  72. t.Errorf("Content-Disposition: got %q; want %q", got, want)
  73. }
  74. if resp.StatusCode == http.StatusOK {
  75. return
  76. }
  77. if got, want := resp.Header.Get("X-Go-Pprof"), "1"; got != want {
  78. t.Errorf("X-Go-Pprof: got %q; want %q", got, want)
  79. }
  80. if !bytes.Equal(body, tc.resp) {
  81. t.Errorf("response: got %q; want %q", body, tc.resp)
  82. }
  83. })
  84. }
  85. }
  86. var Sink uint32
  87. func mutexHog1(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration) {
  88. atomic.AddUint32(&Sink, 1)
  89. for time.Since(start) < dt {
  90. // When using gccgo the loop of mutex operations is
  91. // not preemptible. This can cause the loop to block a GC,
  92. // causing the time limits in TestDeltaContentionz to fail.
  93. // Since this loop is not very realistic, when using
  94. // gccgo add preemption points 100 times a second.
  95. t1 := time.Now()
  96. for time.Since(start) < dt && time.Since(t1) < 10*time.Millisecond {
  97. mu1.Lock()
  98. mu2.Lock()
  99. mu1.Unlock()
  100. mu2.Unlock()
  101. }
  102. if runtime.Compiler == "gccgo" {
  103. runtime.Gosched()
  104. }
  105. }
  106. }
  107. // mutexHog2 is almost identical to mutexHog but we keep them separate
  108. // in order to distinguish them with function names in the stack trace.
  109. // We make them slightly different, using Sink, because otherwise
  110. // gccgo -c opt will merge them.
  111. func mutexHog2(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration) {
  112. atomic.AddUint32(&Sink, 2)
  113. for time.Since(start) < dt {
  114. // See comment in mutexHog.
  115. t1 := time.Now()
  116. for time.Since(start) < dt && time.Since(t1) < 10*time.Millisecond {
  117. mu1.Lock()
  118. mu2.Lock()
  119. mu1.Unlock()
  120. mu2.Unlock()
  121. }
  122. if runtime.Compiler == "gccgo" {
  123. runtime.Gosched()
  124. }
  125. }
  126. }
  127. // mutexHog starts multiple goroutines that runs the given hogger function for the specified duration.
  128. // The hogger function will be given two mutexes to lock & unlock.
  129. func mutexHog(duration time.Duration, hogger func(mu1, mu2 *sync.Mutex, start time.Time, dt time.Duration)) {
  130. start := time.Now()
  131. mu1 := new(sync.Mutex)
  132. mu2 := new(sync.Mutex)
  133. var wg sync.WaitGroup
  134. wg.Add(10)
  135. for i := 0; i < 10; i++ {
  136. go func() {
  137. defer wg.Done()
  138. hogger(mu1, mu2, start, duration)
  139. }()
  140. }
  141. wg.Wait()
  142. }
  143. func TestDeltaProfile(t *testing.T) {
  144. if runtime.GOOS == "openbsd" && runtime.GOARCH == "arm" {
  145. testenv.SkipFlaky(t, 50218)
  146. }
  147. rate := runtime.SetMutexProfileFraction(1)
  148. defer func() {
  149. runtime.SetMutexProfileFraction(rate)
  150. }()
  151. // mutexHog1 will appear in non-delta mutex profile
  152. // if the mutex profile works.
  153. mutexHog(20*time.Millisecond, mutexHog1)
  154. // If mutexHog1 does not appear in the mutex profile,
  155. // skip this test. Mutex profile is likely not working,
  156. // so is the delta profile.
  157. p, err := query("/debug/pprof/mutex")
  158. if err != nil {
  159. t.Skipf("mutex profile is unsupported: %v", err)
  160. }
  161. if !seen(p, "mutexHog1") {
  162. t.Skipf("mutex profile is not working: %v", p)
  163. }
  164. // causes mutexHog2 call stacks to appear in the mutex profile.
  165. done := make(chan bool)
  166. go func() {
  167. for {
  168. mutexHog(20*time.Millisecond, mutexHog2)
  169. select {
  170. case <-done:
  171. done <- true
  172. return
  173. default:
  174. time.Sleep(10 * time.Millisecond)
  175. }
  176. }
  177. }()
  178. defer func() { // cleanup the above goroutine.
  179. done <- true
  180. <-done // wait for the goroutine to exit.
  181. }()
  182. for _, d := range []int{1, 4, 16, 32} {
  183. endpoint := fmt.Sprintf("/debug/pprof/mutex?seconds=%d", d)
  184. p, err := query(endpoint)
  185. if err != nil {
  186. t.Fatalf("failed to query %q: %v", endpoint, err)
  187. }
  188. if !seen(p, "mutexHog1") && seen(p, "mutexHog2") && p.DurationNanos > 0 {
  189. break // pass
  190. }
  191. if d == 32 {
  192. t.Errorf("want mutexHog2 but no mutexHog1 in the profile, and non-zero p.DurationNanos, got %v", p)
  193. }
  194. }
  195. p, err = query("/debug/pprof/mutex")
  196. if err != nil {
  197. t.Fatalf("failed to query mutex profile: %v", err)
  198. }
  199. if !seen(p, "mutexHog1") || !seen(p, "mutexHog2") {
  200. t.Errorf("want both mutexHog1 and mutexHog2 in the profile, got %v", p)
  201. }
  202. }
  203. var srv = httptest.NewServer(nil)
  204. func query(endpoint string) (*profile.Profile, error) {
  205. url := srv.URL + endpoint
  206. r, err := http.Get(url)
  207. if err != nil {
  208. return nil, fmt.Errorf("failed to fetch %q: %v", url, err)
  209. }
  210. if r.StatusCode != http.StatusOK {
  211. return nil, fmt.Errorf("failed to fetch %q: %v", url, r.Status)
  212. }
  213. b, err := io.ReadAll(r.Body)
  214. r.Body.Close()
  215. if err != nil {
  216. return nil, fmt.Errorf("failed to read and parse the result from %q: %v", url, err)
  217. }
  218. return profile.Parse(bytes.NewBuffer(b))
  219. }
  220. // seen returns true if the profile includes samples whose stacks include
  221. // the specified function name (fname).
  222. func seen(p *profile.Profile, fname string) bool {
  223. locIDs := map[*profile.Location]bool{}
  224. for _, loc := range p.Location {
  225. for _, l := range loc.Line {
  226. if strings.Contains(l.Function.Name, fname) {
  227. locIDs[loc] = true
  228. break
  229. }
  230. }
  231. }
  232. for _, sample := range p.Sample {
  233. for _, loc := range sample.Location {
  234. if locIDs[loc] {
  235. return true
  236. }
  237. }
  238. }
  239. return false
  240. }