mirror of
https://github.com/golang/go
synced 2024-11-19 18:54:41 -07:00
runtime/pprof: merge internal/protopprof into pprof package
These are very tightly coupled, and internal/protopprof is small. There's no point to having a separate package. Change-Id: I2c8aa49c9e18a7128657bf2b05323860151b5606 Reviewed-on: https://go-review.googlesource.com/36711 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
3a20928157
commit
9a7544395a
@ -78,7 +78,6 @@ import (
|
|||||||
"internal/pprof/profile"
|
"internal/pprof/profile"
|
||||||
"io"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof/internal/protopprof"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -500,7 +499,7 @@ func writeHeap(w io.Writer, debug int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if debug == 0 {
|
if debug == 0 {
|
||||||
pp := protopprof.EncodeMemProfile(p, int64(runtime.MemProfileRate), time.Now())
|
pp := encodeMemProfile(p, int64(runtime.MemProfileRate), time.Now())
|
||||||
return pp.Write(w)
|
return pp.Write(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -709,7 +708,7 @@ func profileWriter(w io.Writer) {
|
|||||||
buf.Write(data)
|
buf.Write(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
profile, err := protopprof.TranslateCPUProfile(buf.Bytes(), startTime)
|
profile, err := translateCPUProfile(buf.Bytes(), startTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// The runtime should never produce an invalid or truncated profile.
|
// The runtime should never produce an invalid or truncated profile.
|
||||||
// It drops records that can't fit into its log buffers.
|
// It drops records that can't fit into its log buffers.
|
||||||
|
@ -2,10 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package protopprof converts the runtime's raw profile logs
|
package pprof
|
||||||
// to Profile structs containing a representation of the pprof
|
|
||||||
// protocol buffer profile format.
|
|
||||||
package protopprof
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -18,9 +15,9 @@ import (
|
|||||||
"internal/pprof/profile"
|
"internal/pprof/profile"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TranslateCPUProfile parses binary CPU profiling stack trace data
|
// translateCPUProfile parses binary CPU profiling stack trace data
|
||||||
// generated by runtime.CPUProfile() into a profile struct.
|
// generated by runtime.CPUProfile() into a profile struct.
|
||||||
func TranslateCPUProfile(b []byte, startTime time.Time) (*profile.Profile, error) {
|
func translateCPUProfile(b []byte, startTime time.Time) (*profile.Profile, error) {
|
||||||
const wordSize = unsafe.Sizeof(uintptr(0))
|
const wordSize = unsafe.Sizeof(uintptr(0))
|
||||||
const minRawProfile = 5 * wordSize // Need a minimum of 5 words.
|
const minRawProfile = 5 * wordSize // Need a minimum of 5 words.
|
||||||
if uintptr(len(b)) < minRawProfile {
|
if uintptr(len(b)) < minRawProfile {
|
@ -2,7 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package protopprof
|
package pprof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -62,14 +62,14 @@ func createProfileWithTwoSamples(t *testing.T, periodMs uintptr, count1 uintptr,
|
|||||||
return *buf
|
return *buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests TranslateCPUProfile parses correct sampling period in an otherwise empty cpu profile.
|
// Tests translateCPUProfile parses correct sampling period in an otherwise empty cpu profile.
|
||||||
func TestTranlateCPUProfileSamplingPeriod(t *testing.T) {
|
func TestTranlateCPUProfileSamplingPeriod(t *testing.T) {
|
||||||
// A test server with mock cpu profile data.
|
// A test server with mock cpu profile data.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
b := createEmptyProfileWithPeriod(t, 2000)
|
b := createEmptyProfileWithPeriod(t, 2000)
|
||||||
p, err := TranslateCPUProfile(b.Bytes(), startTime)
|
p, err := translateCPUProfile(b.Bytes(), startTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("translate failed: %v", err)
|
t.Fatalf("translate failed: %v", err)
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ func getSampleAsString(sample []*profile.Sample) string {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests TranslateCPUProfile parses a cpu profile with sample values present.
|
// Tests translateCPUProfile parses a cpu profile with sample values present.
|
||||||
func TestTranslateCPUProfileWithSamples(t *testing.T) {
|
func TestTranslateCPUProfileWithSamples(t *testing.T) {
|
||||||
if runtime.GOOS != "linux" {
|
if runtime.GOOS != "linux" {
|
||||||
t.Skip("test requires a system with /proc/self/maps")
|
t.Skip("test requires a system with /proc/self/maps")
|
||||||
@ -134,7 +134,7 @@ func TestTranslateCPUProfileWithSamples(t *testing.T) {
|
|||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
b := createProfileWithTwoSamples(t, 2000, 20, 40, uintptr(address1), uintptr(address2))
|
b := createProfileWithTwoSamples(t, 2000, 20, 40, uintptr(address1), uintptr(address2))
|
||||||
p, err := TranslateCPUProfile(b.Bytes(), startTime)
|
p, err := translateCPUProfile(b.Bytes(), startTime)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Could not parse Profile profile: %v", err)
|
t.Fatalf("Could not parse Profile profile: %v", err)
|
@ -2,7 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package protopprof
|
package pprof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"internal/pprof/profile"
|
"internal/pprof/profile"
|
||||||
@ -11,8 +11,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EncodeMemProfile converts MemProfileRecords to a Profile.
|
// encodeMemProfile converts MemProfileRecords to a Profile.
|
||||||
func EncodeMemProfile(mr []runtime.MemProfileRecord, rate int64, t time.Time) *profile.Profile {
|
func encodeMemProfile(mr []runtime.MemProfileRecord, rate int64, t time.Time) *profile.Profile {
|
||||||
p := &profile.Profile{
|
p := &profile.Profile{
|
||||||
Period: rate,
|
Period: rate,
|
||||||
PeriodType: &profile.ValueType{Type: "space", Unit: "bytes"},
|
PeriodType: &profile.ValueType{Type: "space", Unit: "bytes"},
|
@ -2,7 +2,7 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package protopprof
|
package pprof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -42,7 +42,7 @@ func TestSampledHeapAllocProfile(t *testing.T) {
|
|||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
rec, rate := testMemRecords(address1, address2)
|
rec, rate := testMemRecords(address1, address2)
|
||||||
p := EncodeMemProfile(rec, rate, time.Now())
|
p := encodeMemProfile(rec, rate, time.Now())
|
||||||
if err := p.Write(&buf); err != nil {
|
if err := p.Write(&buf); err != nil {
|
||||||
t.Fatalf("Failed to write profile: %v", err)
|
t.Fatalf("Failed to write profile: %v", err)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user