Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/pprof/protomem_test.go

Documentation: runtime/pprof

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

View as plain text