libgo: update to go1.9
[official-gcc.git] / libgo / go / runtime / pprof / proto_test.go
bloba268c3a95d9d2266f3bd55580648792c0c0e275a
1 // Copyright 2016 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.
5 package pprof
7 import (
8 "bytes"
9 "encoding/json"
10 "fmt"
11 "io/ioutil"
12 "reflect"
13 "runtime"
14 "runtime/pprof/internal/profile"
15 "strings"
16 "testing"
19 // translateCPUProfile parses binary CPU profiling stack trace data
20 // generated by runtime.CPUProfile() into a profile struct.
21 // This is only used for testing. Real conversions stream the
22 // data into the profileBuilder as it becomes available.
23 func translateCPUProfile(data []uint64) (*profile.Profile, error) {
24 var buf bytes.Buffer
25 b := newProfileBuilder(&buf)
26 if err := b.addCPUData(data, nil); err != nil {
27 return nil, err
29 b.build()
30 return profile.Parse(&buf)
33 // fmtJSON returns a pretty-printed JSON form for x.
34 // It works reasonbly well for printing protocol-buffer
35 // data structures like profile.Profile.
36 func fmtJSON(x interface{}) string {
37 js, _ := json.MarshalIndent(x, "", "\t")
38 return string(js)
41 func TestConvertCPUProfileEmpty(t *testing.T) {
42 // A test server with mock cpu profile data.
43 var buf bytes.Buffer
45 b := []uint64{3, 0, 500} // empty profile at 500 Hz (2ms sample period)
46 p, err := translateCPUProfile(b)
47 if err != nil {
48 t.Fatalf("translateCPUProfile: %v", err)
50 if err := p.Write(&buf); err != nil {
51 t.Fatalf("writing profile: %v", err)
54 p, err = profile.Parse(&buf)
55 if err != nil {
56 t.Fatalf("profile.Parse: %v", err)
59 // Expected PeriodType and SampleType.
60 periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"}
61 sampleType := []*profile.ValueType{
62 {Type: "samples", Unit: "count"},
63 {Type: "cpu", Unit: "nanoseconds"},
66 checkProfile(t, p, 2000*1000, periodType, sampleType, nil)
69 // For gccgo make these functions different so that gccgo doesn't
70 // merge them with each other and with lostProfileEvent.
71 func f1(i int) { f1(i + 1) }
72 func f2(i int) { f2(i + 2) }
74 // testPCs returns two PCs and two corresponding memory mappings
75 // to use in test profiles.
76 func testPCs(t *testing.T) (addr1, addr2 uint64, map1, map2 *profile.Mapping) {
77 switch runtime.GOOS {
78 case "linux", "android", "netbsd":
79 // Figure out two addresses from /proc/self/maps.
80 mmap, err := ioutil.ReadFile("/proc/self/maps")
81 if err != nil {
82 t.Fatal(err)
84 mprof := &profile.Profile{}
85 if err = mprof.ParseMemoryMap(bytes.NewReader(mmap)); err != nil {
86 t.Fatalf("parsing /proc/self/maps: %v", err)
88 if len(mprof.Mapping) < 2 {
89 // It is possible for a binary to only have 1 executable
90 // region of memory.
91 t.Skipf("need 2 or more mappings, got %v", len(mprof.Mapping))
93 addr1 = mprof.Mapping[0].Start
94 map1 = mprof.Mapping[0]
95 map1.BuildID, _ = elfBuildID(map1.File)
96 addr2 = mprof.Mapping[1].Start
97 map2 = mprof.Mapping[1]
98 map2.BuildID, _ = elfBuildID(map2.File)
99 default:
100 addr1 = uint64(funcPC(f1))
101 addr2 = uint64(funcPC(f2))
103 return
106 func TestConvertCPUProfile(t *testing.T) {
107 addr1, addr2, map1, map2 := testPCs(t)
109 b := []uint64{
110 3, 0, 500, // hz = 500
111 5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1
112 5, 0, 40, uint64(addr2), uint64(addr2 + 2), // 40 samples in addr2
113 5, 0, 10, uint64(addr1), uint64(addr1 + 2), // 10 samples in addr1
115 p, err := translateCPUProfile(b)
116 if err != nil {
117 t.Fatalf("translating profile: %v", err)
119 period := int64(2000 * 1000)
120 periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"}
121 sampleType := []*profile.ValueType{
122 {Type: "samples", Unit: "count"},
123 {Type: "cpu", Unit: "nanoseconds"},
125 samples := []*profile.Sample{
126 {Value: []int64{20, 20 * 2000 * 1000}, Location: []*profile.Location{
127 {ID: 1, Mapping: map1, Address: addr1},
128 {ID: 2, Mapping: map1, Address: addr1 + 1},
130 {Value: []int64{40, 40 * 2000 * 1000}, Location: []*profile.Location{
131 {ID: 3, Mapping: map2, Address: addr2},
132 {ID: 4, Mapping: map2, Address: addr2 + 1},
135 checkProfile(t, p, period, periodType, sampleType, samples)
138 func checkProfile(t *testing.T, p *profile.Profile, period int64, periodType *profile.ValueType, sampleType []*profile.ValueType, samples []*profile.Sample) {
139 if p.Period != period {
140 t.Fatalf("p.Period = %d, want %d", p.Period, period)
142 if !reflect.DeepEqual(p.PeriodType, periodType) {
143 t.Fatalf("p.PeriodType = %v\nwant = %v", fmtJSON(p.PeriodType), fmtJSON(periodType))
145 if !reflect.DeepEqual(p.SampleType, sampleType) {
146 t.Fatalf("p.SampleType = %v\nwant = %v", fmtJSON(p.SampleType), fmtJSON(sampleType))
148 // Clear line info since it is not in the expected samples.
149 // If we used f1 and f2 above, then the samples will have line info.
150 for _, s := range p.Sample {
151 for _, l := range s.Location {
152 l.Line = nil
155 if fmtJSON(p.Sample) != fmtJSON(samples) { // ignore unexported fields
156 if len(p.Sample) == len(samples) {
157 for i := range p.Sample {
158 if !reflect.DeepEqual(p.Sample[i], samples[i]) {
159 t.Errorf("sample %d = %v\nwant = %v\n", i, fmtJSON(p.Sample[i]), fmtJSON(samples[i]))
162 if t.Failed() {
163 t.FailNow()
166 t.Fatalf("p.Sample = %v\nwant = %v", fmtJSON(p.Sample), fmtJSON(samples))
170 var profSelfMapsTests = `
171 00400000-0040b000 r-xp 00000000 fc:01 787766 /bin/cat
172 0060a000-0060b000 r--p 0000a000 fc:01 787766 /bin/cat
173 0060b000-0060c000 rw-p 0000b000 fc:01 787766 /bin/cat
174 014ab000-014cc000 rw-p 00000000 00:00 0 [heap]
175 7f7d76af8000-7f7d7797c000 r--p 00000000 fc:01 1318064 /usr/lib/locale/locale-archive
176 7f7d7797c000-7f7d77b36000 r-xp 00000000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so
177 7f7d77b36000-7f7d77d36000 ---p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so
178 7f7d77d36000-7f7d77d3a000 r--p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so
179 7f7d77d3a000-7f7d77d3c000 rw-p 001be000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so
180 7f7d77d3c000-7f7d77d41000 rw-p 00000000 00:00 0
181 7f7d77d41000-7f7d77d64000 r-xp 00000000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so
182 7f7d77f3f000-7f7d77f42000 rw-p 00000000 00:00 0
183 7f7d77f61000-7f7d77f63000 rw-p 00000000 00:00 0
184 7f7d77f63000-7f7d77f64000 r--p 00022000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so
185 7f7d77f64000-7f7d77f65000 rw-p 00023000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so
186 7f7d77f65000-7f7d77f66000 rw-p 00000000 00:00 0
187 7ffc342a2000-7ffc342c3000 rw-p 00000000 00:00 0 [stack]
188 7ffc34343000-7ffc34345000 r-xp 00000000 00:00 0 [vdso]
189 ffffffffff600000-ffffffffff601000 r-xp 00000090 00:00 0 [vsyscall]
191 00400000 0040b000 00000000 /bin/cat
192 7f7d7797c000 7f7d77b36000 00000000 /lib/x86_64-linux-gnu/libc-2.19.so
193 7f7d77d41000 7f7d77d64000 00000000 /lib/x86_64-linux-gnu/ld-2.19.so
194 7ffc34343000 7ffc34345000 00000000 [vdso]
195 ffffffffff600000 ffffffffff601000 00000090 [vsyscall]
197 00400000-07000000 r-xp 00000000 00:00 0
198 07000000-07093000 r-xp 06c00000 00:2e 536754 /path/to/gobench_server_main
199 07093000-0722d000 rw-p 06c92000 00:2e 536754 /path/to/gobench_server_main
200 0722d000-07b21000 rw-p 00000000 00:00 0
201 c000000000-c000036000 rw-p 00000000 00:00 0
203 07000000 07093000 06c00000 /path/to/gobench_server_main
206 func TestProcSelfMaps(t *testing.T) {
207 for tx, tt := range strings.Split(profSelfMapsTests, "\n\n") {
208 i := strings.Index(tt, "->\n")
209 if i < 0 {
210 t.Fatal("malformed test case")
212 in, out := tt[:i], tt[i+len("->\n"):]
213 if len(out) > 0 && out[len(out)-1] != '\n' {
214 out += "\n"
216 var buf bytes.Buffer
217 parseProcSelfMaps([]byte(in), func(lo, hi, offset uint64, file, buildID string) {
218 fmt.Fprintf(&buf, "%08x %08x %08x %s\n", lo, hi, offset, file)
220 if buf.String() != out {
221 t.Errorf("#%d: have:\n%s\nwant:\n%s\n%q\n%q", tx, buf.String(), out, buf.String(), out)