github.com/c12o16h1/go/src@v0.0.0-20200114212001-5a151c0f00ed/runtime/pprof/proto_test.go (about) 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. 4 5 package pprof 6 7 import ( 8 "bytes" 9 "encoding/json" 10 "fmt" 11 "github.com/c12o16h1/go/src/internal/testenv" 12 "io/ioutil" 13 "os" 14 "os/exec" 15 "reflect" 16 "runtime" 17 "runtime/pprof/internal/profile" 18 "strings" 19 "testing" 20 ) 21 22 // translateCPUProfile parses binary CPU profiling stack trace data 23 // generated by runtime.CPUProfile() into a profile struct. 24 // This is only used for testing. Real conversions stream the 25 // data into the profileBuilder as it becomes available. 26 func translateCPUProfile(data []uint64) (*profile.Profile, error) { 27 var buf bytes.Buffer 28 b := newProfileBuilder(&buf) 29 if err := b.addCPUData(data, nil); err != nil { 30 return nil, err 31 } 32 b.build() 33 return profile.Parse(&buf) 34 } 35 36 // fmtJSON returns a pretty-printed JSON form for x. 37 // It works reasonbly well for printing protocol-buffer 38 // data structures like profile.Profile. 39 func fmtJSON(x interface{}) string { 40 js, _ := json.MarshalIndent(x, "", "\t") 41 return string(js) 42 } 43 44 func TestConvertCPUProfileEmpty(t *testing.T) { 45 // A test server with mock cpu profile data. 46 var buf bytes.Buffer 47 48 b := []uint64{3, 0, 500} // empty profile at 500 Hz (2ms sample period) 49 p, err := translateCPUProfile(b) 50 if err != nil { 51 t.Fatalf("translateCPUProfile: %v", err) 52 } 53 if err := p.Write(&buf); err != nil { 54 t.Fatalf("writing profile: %v", err) 55 } 56 57 p, err = profile.Parse(&buf) 58 if err != nil { 59 t.Fatalf("profile.Parse: %v", err) 60 } 61 62 // Expected PeriodType and SampleType. 63 periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"} 64 sampleType := []*profile.ValueType{ 65 {Type: "samples", Unit: "count"}, 66 {Type: "cpu", Unit: "nanoseconds"}, 67 } 68 69 checkProfile(t, p, 2000*1000, periodType, sampleType, nil, "") 70 } 71 72 func f1() { f1() } 73 func f2() { f2() } 74 75 // testPCs returns two PCs and two corresponding memory mappings 76 // to use in test profiles. 77 func testPCs(t *testing.T) (addr1, addr2 uint64, map1, map2 *profile.Mapping) { 78 switch runtime.GOOS { 79 case "linux", "android", "netbsd": 80 // Figure out two addresses from /proc/self/maps. 81 mmap, err := ioutil.ReadFile("/proc/self/maps") 82 if err != nil { 83 t.Fatal(err) 84 } 85 mprof := &profile.Profile{} 86 if err = mprof.ParseMemoryMap(bytes.NewReader(mmap)); err != nil { 87 t.Fatalf("parsing /proc/self/maps: %v", err) 88 } 89 if len(mprof.Mapping) < 2 { 90 // It is possible for a binary to only have 1 executable 91 // region of memory. 92 t.Skipf("need 2 or more mappings, got %v", len(mprof.Mapping)) 93 } 94 addr1 = mprof.Mapping[0].Start 95 map1 = mprof.Mapping[0] 96 map1.BuildID, _ = elfBuildID(map1.File) 97 addr2 = mprof.Mapping[1].Start 98 map2 = mprof.Mapping[1] 99 map2.BuildID, _ = elfBuildID(map2.File) 100 case "js": 101 addr1 = uint64(funcPC(f1)) 102 addr2 = uint64(funcPC(f2)) 103 default: 104 addr1 = uint64(funcPC(f1)) 105 addr2 = uint64(funcPC(f2)) 106 // Fake mapping - HasFunctions will be true because two PCs from Go 107 // will be fully symbolized. 108 fake := &profile.Mapping{ID: 1, HasFunctions: true} 109 map1, map2 = fake, fake 110 } 111 return 112 } 113 114 func TestConvertCPUProfile(t *testing.T) { 115 addr1, addr2, map1, map2 := testPCs(t) 116 117 b := []uint64{ 118 3, 0, 500, // hz = 500 119 5, 0, 10, uint64(addr1 + 1), uint64(addr1 + 2), // 10 samples in addr1 120 5, 0, 40, uint64(addr2 + 1), uint64(addr2 + 2), // 40 samples in addr2 121 5, 0, 10, uint64(addr1 + 1), uint64(addr1 + 2), // 10 samples in addr1 122 } 123 p, err := translateCPUProfile(b) 124 if err != nil { 125 t.Fatalf("translating profile: %v", err) 126 } 127 period := int64(2000 * 1000) 128 periodType := &profile.ValueType{Type: "cpu", Unit: "nanoseconds"} 129 sampleType := []*profile.ValueType{ 130 {Type: "samples", Unit: "count"}, 131 {Type: "cpu", Unit: "nanoseconds"}, 132 } 133 samples := []*profile.Sample{ 134 {Value: []int64{20, 20 * 2000 * 1000}, Location: []*profile.Location{ 135 {ID: 1, Mapping: map1, Address: addr1}, 136 {ID: 2, Mapping: map1, Address: addr1 + 1}, 137 }}, 138 {Value: []int64{40, 40 * 2000 * 1000}, Location: []*profile.Location{ 139 {ID: 3, Mapping: map2, Address: addr2}, 140 {ID: 4, Mapping: map2, Address: addr2 + 1}, 141 }}, 142 } 143 checkProfile(t, p, period, periodType, sampleType, samples, "") 144 } 145 146 func checkProfile(t *testing.T, p *profile.Profile, period int64, periodType *profile.ValueType, sampleType []*profile.ValueType, samples []*profile.Sample, defaultSampleType string) { 147 t.Helper() 148 149 if p.Period != period { 150 t.Errorf("p.Period = %d, want %d", p.Period, period) 151 } 152 if !reflect.DeepEqual(p.PeriodType, periodType) { 153 t.Errorf("p.PeriodType = %v\nwant = %v", fmtJSON(p.PeriodType), fmtJSON(periodType)) 154 } 155 if !reflect.DeepEqual(p.SampleType, sampleType) { 156 t.Errorf("p.SampleType = %v\nwant = %v", fmtJSON(p.SampleType), fmtJSON(sampleType)) 157 } 158 if defaultSampleType != p.DefaultSampleType { 159 t.Errorf("p.DefaultSampleType = %v\nwant = %v", p.DefaultSampleType, defaultSampleType) 160 } 161 // Clear line info since it is not in the expected samples. 162 // If we used f1 and f2 above, then the samples will have line info. 163 for _, s := range p.Sample { 164 for _, l := range s.Location { 165 l.Line = nil 166 } 167 } 168 if fmtJSON(p.Sample) != fmtJSON(samples) { // ignore unexported fields 169 if len(p.Sample) == len(samples) { 170 for i := range p.Sample { 171 if !reflect.DeepEqual(p.Sample[i], samples[i]) { 172 t.Errorf("sample %d = %v\nwant = %v\n", i, fmtJSON(p.Sample[i]), fmtJSON(samples[i])) 173 } 174 } 175 if t.Failed() { 176 t.FailNow() 177 } 178 } 179 t.Fatalf("p.Sample = %v\nwant = %v", fmtJSON(p.Sample), fmtJSON(samples)) 180 } 181 } 182 183 var profSelfMapsTests = ` 184 00400000-0040b000 r-xp 00000000 fc:01 787766 /bin/cat 185 0060a000-0060b000 r--p 0000a000 fc:01 787766 /bin/cat 186 0060b000-0060c000 rw-p 0000b000 fc:01 787766 /bin/cat 187 014ab000-014cc000 rw-p 00000000 00:00 0 [heap] 188 7f7d76af8000-7f7d7797c000 r--p 00000000 fc:01 1318064 /usr/lib/locale/locale-archive 189 7f7d7797c000-7f7d77b36000 r-xp 00000000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 190 7f7d77b36000-7f7d77d36000 ---p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 191 7f7d77d36000-7f7d77d3a000 r--p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 192 7f7d77d3a000-7f7d77d3c000 rw-p 001be000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 193 7f7d77d3c000-7f7d77d41000 rw-p 00000000 00:00 0 194 7f7d77d41000-7f7d77d64000 r-xp 00000000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 195 7f7d77f3f000-7f7d77f42000 rw-p 00000000 00:00 0 196 7f7d77f61000-7f7d77f63000 rw-p 00000000 00:00 0 197 7f7d77f63000-7f7d77f64000 r--p 00022000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 198 7f7d77f64000-7f7d77f65000 rw-p 00023000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 199 7f7d77f65000-7f7d77f66000 rw-p 00000000 00:00 0 200 7ffc342a2000-7ffc342c3000 rw-p 00000000 00:00 0 [stack] 201 7ffc34343000-7ffc34345000 r-xp 00000000 00:00 0 [vdso] 202 ffffffffff600000-ffffffffff601000 r-xp 00000090 00:00 0 [vsyscall] 203 -> 204 00400000 0040b000 00000000 /bin/cat 205 7f7d7797c000 7f7d77b36000 00000000 /lib/x86_64-linux-gnu/libc-2.19.so 206 7f7d77d41000 7f7d77d64000 00000000 /lib/x86_64-linux-gnu/ld-2.19.so 207 7ffc34343000 7ffc34345000 00000000 [vdso] 208 ffffffffff600000 ffffffffff601000 00000090 [vsyscall] 209 210 00400000-07000000 r-xp 00000000 00:00 0 211 07000000-07093000 r-xp 06c00000 00:2e 536754 /path/to/gobench_server_main 212 07093000-0722d000 rw-p 06c92000 00:2e 536754 /path/to/gobench_server_main 213 0722d000-07b21000 rw-p 00000000 00:00 0 214 c000000000-c000036000 rw-p 00000000 00:00 0 215 -> 216 07000000 07093000 06c00000 /path/to/gobench_server_main 217 ` 218 219 var profSelfMapsTestsWithDeleted = ` 220 00400000-0040b000 r-xp 00000000 fc:01 787766 /bin/cat (deleted) 221 0060a000-0060b000 r--p 0000a000 fc:01 787766 /bin/cat (deleted) 222 0060b000-0060c000 rw-p 0000b000 fc:01 787766 /bin/cat (deleted) 223 014ab000-014cc000 rw-p 00000000 00:00 0 [heap] 224 7f7d76af8000-7f7d7797c000 r--p 00000000 fc:01 1318064 /usr/lib/locale/locale-archive 225 7f7d7797c000-7f7d77b36000 r-xp 00000000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 226 7f7d77b36000-7f7d77d36000 ---p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 227 7f7d77d36000-7f7d77d3a000 r--p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 228 7f7d77d3a000-7f7d77d3c000 rw-p 001be000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 229 7f7d77d3c000-7f7d77d41000 rw-p 00000000 00:00 0 230 7f7d77d41000-7f7d77d64000 r-xp 00000000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 231 7f7d77f3f000-7f7d77f42000 rw-p 00000000 00:00 0 232 7f7d77f61000-7f7d77f63000 rw-p 00000000 00:00 0 233 7f7d77f63000-7f7d77f64000 r--p 00022000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 234 7f7d77f64000-7f7d77f65000 rw-p 00023000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 235 7f7d77f65000-7f7d77f66000 rw-p 00000000 00:00 0 236 7ffc342a2000-7ffc342c3000 rw-p 00000000 00:00 0 [stack] 237 7ffc34343000-7ffc34345000 r-xp 00000000 00:00 0 [vdso] 238 ffffffffff600000-ffffffffff601000 r-xp 00000090 00:00 0 [vsyscall] 239 -> 240 00400000 0040b000 00000000 /bin/cat 241 7f7d7797c000 7f7d77b36000 00000000 /lib/x86_64-linux-gnu/libc-2.19.so 242 7f7d77d41000 7f7d77d64000 00000000 /lib/x86_64-linux-gnu/ld-2.19.so 243 7ffc34343000 7ffc34345000 00000000 [vdso] 244 ffffffffff600000 ffffffffff601000 00000090 [vsyscall] 245 246 00400000-0040b000 r-xp 00000000 fc:01 787766 /bin/cat with space 247 0060a000-0060b000 r--p 0000a000 fc:01 787766 /bin/cat with space 248 0060b000-0060c000 rw-p 0000b000 fc:01 787766 /bin/cat with space 249 014ab000-014cc000 rw-p 00000000 00:00 0 [heap] 250 7f7d76af8000-7f7d7797c000 r--p 00000000 fc:01 1318064 /usr/lib/locale/locale-archive 251 7f7d7797c000-7f7d77b36000 r-xp 00000000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 252 7f7d77b36000-7f7d77d36000 ---p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 253 7f7d77d36000-7f7d77d3a000 r--p 001ba000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 254 7f7d77d3a000-7f7d77d3c000 rw-p 001be000 fc:01 1180226 /lib/x86_64-linux-gnu/libc-2.19.so 255 7f7d77d3c000-7f7d77d41000 rw-p 00000000 00:00 0 256 7f7d77d41000-7f7d77d64000 r-xp 00000000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 257 7f7d77f3f000-7f7d77f42000 rw-p 00000000 00:00 0 258 7f7d77f61000-7f7d77f63000 rw-p 00000000 00:00 0 259 7f7d77f63000-7f7d77f64000 r--p 00022000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 260 7f7d77f64000-7f7d77f65000 rw-p 00023000 fc:01 1180217 /lib/x86_64-linux-gnu/ld-2.19.so 261 7f7d77f65000-7f7d77f66000 rw-p 00000000 00:00 0 262 7ffc342a2000-7ffc342c3000 rw-p 00000000 00:00 0 [stack] 263 7ffc34343000-7ffc34345000 r-xp 00000000 00:00 0 [vdso] 264 ffffffffff600000-ffffffffff601000 r-xp 00000090 00:00 0 [vsyscall] 265 -> 266 00400000 0040b000 00000000 /bin/cat with space 267 7f7d7797c000 7f7d77b36000 00000000 /lib/x86_64-linux-gnu/libc-2.19.so 268 7f7d77d41000 7f7d77d64000 00000000 /lib/x86_64-linux-gnu/ld-2.19.so 269 7ffc34343000 7ffc34345000 00000000 [vdso] 270 ffffffffff600000 ffffffffff601000 00000090 [vsyscall] 271 ` 272 273 func TestProcSelfMaps(t *testing.T) { 274 275 f := func(t *testing.T, input string) { 276 for tx, tt := range strings.Split(input, "\n\n") { 277 i := strings.Index(tt, "->\n") 278 if i < 0 { 279 t.Fatal("malformed test case") 280 } 281 in, out := tt[:i], tt[i+len("->\n"):] 282 if len(out) > 0 && out[len(out)-1] != '\n' { 283 out += "\n" 284 } 285 var buf bytes.Buffer 286 parseProcSelfMaps([]byte(in), func(lo, hi, offset uint64, file, buildID string) { 287 fmt.Fprintf(&buf, "%08x %08x %08x %s\n", lo, hi, offset, file) 288 }) 289 if buf.String() != out { 290 t.Errorf("#%d: have:\n%s\nwant:\n%s\n%q\n%q", tx, buf.String(), out, buf.String(), out) 291 } 292 } 293 } 294 295 t.Run("Normal", func(t *testing.T) { 296 f(t, profSelfMapsTests) 297 }) 298 299 t.Run("WithDeletedFile", func(t *testing.T) { 300 f(t, profSelfMapsTestsWithDeleted) 301 }) 302 } 303 304 // TestMapping checks the mapping section of CPU profiles 305 // has the HasFunctions field set correctly. If all PCs included 306 // in the samples are successfully symbolized, the corresponding 307 // mapping entry (in this test case, only one entry) should have 308 // its HasFunctions field set true. 309 // The test generates a CPU profile that includes PCs from C side 310 // that the runtime can't symbolize. See ./testdata/mappingtest. 311 func TestMapping(t *testing.T) { 312 testenv.MustHaveGoRun(t) 313 testenv.MustHaveCGO(t) 314 315 prog := "./testdata/mappingtest/main.go" 316 317 // GoOnly includes only Go symbols that runtime will symbolize. 318 // Go+C includes C symbols that runtime will not symbolize. 319 for _, traceback := range []string{"GoOnly", "Go+C"} { 320 t.Run("traceback"+traceback, func(t *testing.T) { 321 cmd := exec.Command(testenv.GoToolPath(t), "run", prog) 322 if traceback != "GoOnly" { 323 cmd.Env = append(os.Environ(), "SETCGOTRACEBACK=1") 324 } 325 cmd.Stderr = new(bytes.Buffer) 326 327 out, err := cmd.Output() 328 if err != nil { 329 t.Fatalf("failed to run the test program %q: %v\n%v", prog, err, cmd.Stderr) 330 } 331 332 prof, err := profile.Parse(bytes.NewReader(out)) 333 if err != nil { 334 t.Fatalf("failed to parse the generated profile data: %v", err) 335 } 336 t.Logf("Profile: %s", prof) 337 338 hit := make(map[*profile.Mapping]bool) 339 miss := make(map[*profile.Mapping]bool) 340 for _, loc := range prof.Location { 341 if symbolized(loc) { 342 hit[loc.Mapping] = true 343 } else { 344 miss[loc.Mapping] = true 345 } 346 } 347 if len(miss) == 0 { 348 t.Log("no location with missing symbol info was sampled") 349 } 350 351 for _, m := range prof.Mapping { 352 if miss[m] && m.HasFunctions { 353 t.Errorf("mapping %+v has HasFunctions=true, but contains locations with failed symbolization", m) 354 continue 355 } 356 if !miss[m] && hit[m] && !m.HasFunctions { 357 t.Errorf("mapping %+v has HasFunctions=false, but all referenced locations from this lapping were symbolized successfully", m) 358 continue 359 } 360 } 361 362 if traceback == "Go+C" { 363 // The test code was arranged to have PCs from C and 364 // they are not symbolized. 365 // Check no Location containing those unsymbolized PCs contains multiple lines. 366 for i, loc := range prof.Location { 367 if !symbolized(loc) && len(loc.Line) > 1 { 368 t.Errorf("Location[%d] contains unsymbolized PCs and multiple lines: %v", i, loc) 369 } 370 } 371 } 372 }) 373 } 374 } 375 376 func symbolized(loc *profile.Location) bool { 377 if len(loc.Line) == 0 { 378 return false 379 } 380 l := loc.Line[0] 381 f := l.Function 382 if l.Line == 0 || f == nil || f.Name == "" || f.Filename == "" { 383 return false 384 } 385 return true 386 } 387 388 // TestFakeMapping tests if at least one mapping exists 389 // (including a fake mapping), and their HasFunctions bits 390 // are set correctly. 391 func TestFakeMapping(t *testing.T) { 392 var buf bytes.Buffer 393 if err := Lookup("heap").WriteTo(&buf, 0); err != nil { 394 t.Fatalf("failed to write heap profile: %v", err) 395 } 396 prof, err := profile.Parse(&buf) 397 if err != nil { 398 t.Fatalf("failed to parse the generated profile data: %v", err) 399 } 400 t.Logf("Profile: %s", prof) 401 if len(prof.Mapping) == 0 { 402 t.Fatal("want profile with at least one mapping entry, got 0 mapping") 403 } 404 405 hit := make(map[*profile.Mapping]bool) 406 miss := make(map[*profile.Mapping]bool) 407 for _, loc := range prof.Location { 408 if symbolized(loc) { 409 hit[loc.Mapping] = true 410 } else { 411 miss[loc.Mapping] = true 412 } 413 } 414 for _, m := range prof.Mapping { 415 if miss[m] && m.HasFunctions { 416 t.Errorf("mapping %+v has HasFunctions=true, but contains locations with failed symbolization", m) 417 continue 418 } 419 if !miss[m] && hit[m] && !m.HasFunctions { 420 t.Errorf("mapping %+v has HasFunctions=false, but all referenced locations from this lapping were symbolized successfully", m) 421 continue 422 } 423 } 424 }