|
| 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 protopprof |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "internal/pprof/profile" |
| 10 | + "io/ioutil" |
| 11 | + "reflect" |
| 12 | + "runtime" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +// TestSampledHeapAllocProfile tests encoding of a memory profile from |
| 18 | +// runtime.MemProfileRecord data. |
| 19 | +func TestSampledHeapAllocProfile(t *testing.T) { |
| 20 | + if runtime.GOOS != "linux" { |
| 21 | + t.Skip("Test requires a system with /proc/self/maps") |
| 22 | + } |
| 23 | + |
| 24 | + // Figure out two addresses from /proc/self/maps. |
| 25 | + mmap, err := ioutil.ReadFile("/proc/self/maps") |
| 26 | + if err != nil { |
| 27 | + t.Fatal("Cannot read /proc/self/maps") |
| 28 | + } |
| 29 | + rd := bytes.NewReader(mmap) |
| 30 | + mprof := &profile.Profile{} |
| 31 | + if err = mprof.ParseMemoryMap(rd); err != nil { |
| 32 | + t.Fatalf("Cannot parse /proc/self/maps") |
| 33 | + } |
| 34 | + if len(mprof.Mapping) < 2 { |
| 35 | + t.Fatalf("Less than two mappings") |
| 36 | + } |
| 37 | + address1 := mprof.Mapping[0].Start |
| 38 | + address2 := mprof.Mapping[1].Start |
| 39 | + |
| 40 | + var buf bytes.Buffer |
| 41 | + |
| 42 | + rec, rate := testMemRecords(address1, address2) |
| 43 | + p := EncodeMemProfile(rec, rate, time.Now()) |
| 44 | + if err := p.Write(&buf); err != nil { |
| 45 | + t.Fatalf("Failed to write profile: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + p, err = profile.Parse(&buf) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("Could not parse Profile profile: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + // Expected PeriodType, SampleType and Sample. |
| 54 | + expectedPeriodType := &profile.ValueType{Type: "space", Unit: "bytes"} |
| 55 | + expectedSampleType := []*profile.ValueType{ |
| 56 | + {Type: "alloc_objects", Unit: "count"}, |
| 57 | + {Type: "alloc_space", Unit: "bytes"}, |
| 58 | + {Type: "inuse_objects", Unit: "count"}, |
| 59 | + {Type: "inuse_space", Unit: "bytes"}, |
| 60 | + } |
| 61 | + // Expected samples, with values unsampled according to the profiling rate. |
| 62 | + expectedSample := []*profile.Sample{ |
| 63 | + {Value: []int64{2050, 2099200, 1537, 1574400}, Location: []*profile.Location{ |
| 64 | + {ID: 1, Mapping: mprof.Mapping[0], Address: address1}, |
| 65 | + {ID: 2, Mapping: mprof.Mapping[1], Address: address2}, |
| 66 | + }}, |
| 67 | + {Value: []int64{1, 829411, 1, 829411}, Location: []*profile.Location{ |
| 68 | + {ID: 3, Mapping: mprof.Mapping[1], Address: address2 + 1}, |
| 69 | + {ID: 4, Mapping: mprof.Mapping[1], Address: address2 + 2}, |
| 70 | + }}, |
| 71 | + {Value: []int64{1, 829411, 0, 0}, Location: []*profile.Location{ |
| 72 | + {ID: 5, Mapping: mprof.Mapping[0], Address: address1 + 1}, |
| 73 | + {ID: 6, Mapping: mprof.Mapping[0], Address: address1 + 2}, |
| 74 | + {ID: 7, Mapping: mprof.Mapping[1], Address: address2 + 3}, |
| 75 | + }}, |
| 76 | + } |
| 77 | + |
| 78 | + if p.Period != 512*1024 { |
| 79 | + t.Fatalf("Sampling periods do not match") |
| 80 | + } |
| 81 | + if !reflect.DeepEqual(p.PeriodType, expectedPeriodType) { |
| 82 | + t.Fatalf("Period types do not match") |
| 83 | + } |
| 84 | + if !reflect.DeepEqual(p.SampleType, expectedSampleType) { |
| 85 | + t.Fatalf("Sample types do not match") |
| 86 | + } |
| 87 | + if !reflect.DeepEqual(p.Sample, expectedSample) { |
| 88 | + t.Fatalf("Samples do not match: Expected: %v, Got:%v", getSampleAsString(expectedSample), |
| 89 | + getSampleAsString(p.Sample)) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func testMemRecords(a1, a2 uint64) ([]runtime.MemProfileRecord, int64) { |
| 94 | + addr1, addr2 := uintptr(a1), uintptr(a2) |
| 95 | + rate := int64(512 * 1024) |
| 96 | + rec := []runtime.MemProfileRecord{ |
| 97 | + {4096, 1024, 4, 1, [32]uintptr{addr1, addr2}}, |
| 98 | + {512 * 1024, 0, 1, 0, [32]uintptr{addr2 + 1, addr2 + 2}}, |
| 99 | + {512 * 1024, 512 * 1024, 1, 1, [32]uintptr{addr1 + 1, addr1 + 2, addr2 + 3}}, |
| 100 | + } |
| 101 | + return rec, rate |
| 102 | +} |
0 commit comments