LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package pprof serves via its HTTP server runtime profiling data | 5 // Package pprof serves via its HTTP server runtime profiling data |
6 // in the format expected by the pprof visualization tool. | 6 // in the format expected by the pprof visualization tool. |
7 // For more information about pprof, see | 7 // For more information about pprof, see |
8 // http://code.google.com/p/google-perftools/. | 8 // http://code.google.com/p/google-perftools/. |
9 // | 9 // |
10 // The package is typically only imported for the side effect of | 10 // The package is typically only imported for the side effect of |
11 // registering its HTTP handlers. | 11 // registering its HTTP handlers. |
12 // The handled paths all begin with /debug/pprof/. | 12 // The handled paths all begin with /debug/pprof/. |
13 // | 13 // |
14 // To use pprof, link this package into your program: | 14 // To use pprof, link this package into your program: |
15 // import _ "http/pprof" | 15 // import _ "http/pprof" |
16 // | 16 // |
17 // Then use the pprof tool to look at the heap profile: | 17 // Then use the pprof tool to look at the heap profile: |
18 // | 18 // |
19 //» pprof http://localhost:6060/debug/pprof/heap | 19 //» go tool pprof http://localhost:6060/debug/pprof/heap |
20 // | 20 // |
21 // Or to look at a 30-second CPU profile: | 21 // Or to look at a 30-second CPU profile: |
22 // | 22 // |
23 //» pprof http://localhost:6060/debug/pprof/profile | 23 //» go tool pprof http://localhost:6060/debug/pprof/profile |
| 24 // |
| 25 // Or to look at the thread creation profile: |
| 26 // |
| 27 //» go tool pprof http://localhost:6060/debug/pprof/thread |
24 // | 28 // |
25 package pprof | 29 package pprof |
26 | 30 |
27 import ( | 31 import ( |
28 "bufio" | 32 "bufio" |
29 "bytes" | 33 "bytes" |
30 "fmt" | 34 "fmt" |
31 "io" | 35 "io" |
32 "net/http" | 36 "net/http" |
33 "os" | 37 "os" |
34 "runtime" | 38 "runtime" |
35 "runtime/pprof" | 39 "runtime/pprof" |
36 "strconv" | 40 "strconv" |
37 "strings" | 41 "strings" |
38 "time" | 42 "time" |
39 ) | 43 ) |
40 | 44 |
41 func init() { | 45 func init() { |
42 http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline)) | 46 http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline)) |
43 http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile)) | 47 http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile)) |
44 http.Handle("/debug/pprof/heap", http.HandlerFunc(Heap)) | 48 http.Handle("/debug/pprof/heap", http.HandlerFunc(Heap)) |
45 http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol)) | 49 http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol)) |
| 50 http.Handle("/debug/pprof/thread", http.HandlerFunc(Thread)) |
46 } | 51 } |
47 | 52 |
48 // Cmdline responds with the running program's | 53 // Cmdline responds with the running program's |
49 // command line, with arguments separated by NUL bytes. | 54 // command line, with arguments separated by NUL bytes. |
50 // The package initialization registers it as /debug/pprof/cmdline. | 55 // The package initialization registers it as /debug/pprof/cmdline. |
51 func Cmdline(w http.ResponseWriter, r *http.Request) { | 56 func Cmdline(w http.ResponseWriter, r *http.Request) { |
52 w.Header().Set("Content-Type", "text/plain; charset=utf-8") | 57 w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
53 fmt.Fprintf(w, strings.Join(os.Args, "\x00")) | 58 fmt.Fprintf(w, strings.Join(os.Args, "\x00")) |
54 } | 59 } |
55 | 60 |
56 // Heap responds with the pprof-formatted heap profile. | 61 // Heap responds with the pprof-formatted heap profile. |
57 // The package initialization registers it as /debug/pprof/heap. | 62 // The package initialization registers it as /debug/pprof/heap. |
58 func Heap(w http.ResponseWriter, r *http.Request) { | 63 func Heap(w http.ResponseWriter, r *http.Request) { |
59 w.Header().Set("Content-Type", "text/plain; charset=utf-8") | 64 w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
60 pprof.WriteHeapProfile(w) | 65 pprof.WriteHeapProfile(w) |
| 66 } |
| 67 |
| 68 // Thread responds with the pprof-formatted thread creation profile. |
| 69 // The package initialization registers it as /debug/pprof/thread. |
| 70 func Thread(w http.ResponseWriter, r *http.Request) { |
| 71 w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 72 pprof.WriteThreadProfile(w) |
61 } | 73 } |
62 | 74 |
63 // Profile responds with the pprof-formatted cpu profile. | 75 // Profile responds with the pprof-formatted cpu profile. |
64 // The package initialization registers it as /debug/pprof/profile. | 76 // The package initialization registers it as /debug/pprof/profile. |
65 func Profile(w http.ResponseWriter, r *http.Request) { | 77 func Profile(w http.ResponseWriter, r *http.Request) { |
66 sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64) | 78 sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64) |
67 if sec == 0 { | 79 if sec == 0 { |
68 sec = 30 | 80 sec = 30 |
69 } | 81 } |
70 | 82 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 if err != nil { | 136 if err != nil { |
125 if err != io.EOF { | 137 if err != io.EOF { |
126 fmt.Fprintf(&buf, "reading request: %v\n", err) | 138 fmt.Fprintf(&buf, "reading request: %v\n", err) |
127 } | 139 } |
128 break | 140 break |
129 } | 141 } |
130 } | 142 } |
131 | 143 |
132 w.Write(buf.Bytes()) | 144 w.Write(buf.Bytes()) |
133 } | 145 } |
LEFT | RIGHT |