github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/runtime/pprof/protomem_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  	"runtime"
    10  	"runtime/pprof/internal/profile"
    11  	"testing"
    12  )
    13  
    14  func TestConvertMemProfile(t *testing.T) {
    15  	addr1, addr2, map1, map2 := testPCs(t)
    16  
    17  	var buf bytes.Buffer
    18  	// MemProfileRecord stacks are return PCs, so add one to the
    19  	// addresses recorded in the "profile". The proto profile
    20  	// locations are call PCs, so conversion will subtract one
    21  	// from these and get back to addr1 and addr2.
    22  	a1, a2 := uintptr(addr1)+1, uintptr(addr2)+1
    23  	rate := int64(512 * 1024)
    24  	rec := []runtime.MemProfileRecord{
    25  		{AllocBytes: 4096, FreeBytes: 1024, AllocObjects: 4, FreeObjects: 1, Stack0: [32]uintptr{a1, a2}},
    26  		{AllocBytes: 512 * 1024, FreeBytes: 0, AllocObjects: 1, FreeObjects: 0, Stack0: [32]uintptr{a2 + 1, a2 + 2}},
    27  		{AllocBytes: 512 * 1024, FreeBytes: 512 * 1024, AllocObjects: 1, FreeObjects: 1, Stack0: [32]uintptr{a1 + 1, a1 + 2, a2 + 3}},
    28  	}
    29  
    30  	if err := writeHeapProto(&buf, rec, rate); err != nil {
    31  		t.Fatalf("writing profile: %v", err)
    32  	}
    33  
    34  	p, err := profile.Parse(&buf)
    35  	if err != nil {
    36  		t.Fatalf("profile.Parse: %v", err)
    37  	}
    38  
    39  	periodType := &profile.ValueType{Type: "space", Unit: "bytes"}
    40  	sampleType := []*profile.ValueType{
    41  		{Type: "alloc_objects", Unit: "count"},
    42  		{Type: "alloc_space", Unit: "bytes"},
    43  		{Type: "inuse_objects", Unit: "count"},
    44  		{Type: "inuse_space", Unit: "bytes"},
    45  	}
    46  	samples := []*profile.Sample{
    47  		{
    48  			Value: []int64{2050, 2099200, 1537, 1574400},
    49  			Location: []*profile.Location{
    50  				{ID: 1, Mapping: map1, Address: addr1},
    51  				{ID: 2, Mapping: map2, Address: addr2},
    52  			},
    53  			NumLabel: map[string][]int64{"bytes": {1024}},
    54  		},
    55  		{
    56  			Value: []int64{1, 829411, 1, 829411},
    57  			Location: []*profile.Location{
    58  				{ID: 3, Mapping: map2, Address: addr2 + 1},
    59  				{ID: 4, Mapping: map2, Address: addr2 + 2},
    60  			},
    61  			NumLabel: map[string][]int64{"bytes": {829411}},
    62  		},
    63  		{
    64  			Value: []int64{1, 829411, 0, 0},
    65  			Location: []*profile.Location{
    66  				{ID: 5, Mapping: map1, Address: addr1 + 1},
    67  				{ID: 6, Mapping: map1, Address: addr1 + 2},
    68  				{ID: 7, Mapping: map2, Address: addr2 + 3},
    69  			},
    70  			NumLabel: map[string][]int64{"bytes": {829411}},
    71  		},
    72  	}
    73  	checkProfile(t, p, rate, periodType, sampleType, samples)
    74  }