diff options
| author | Michael Pratt <mpratt@google.com> | 2023-12-06 13:29:03 -0500 |
|---|---|---|
| committer | Michael Pratt <mpratt@google.com> | 2023-12-07 19:52:28 +0000 |
| commit | e1c0349a7c607cdfcfa8f7c0c6b70aceff9d3752 (patch) | |
| tree | 867a43ae4a6ca02387b5074d273e0be3b4714ca3 /src/net/http/pprof | |
| parent | c71eedf90aff3fc73a645b88d2e5166b8a0179fd (diff) | |
| download | go-e1c0349a7c607cdfcfa8f7c0c6b70aceff9d3752.tar.xz | |
internal/profile: fully decode proto even if there are no samples
This is a partial revert of CL 483137.
CL 483137 started checking errors in postDecode, which is good. Now we
can catch more malformed pprof protos. However this made
TestEmptyProfile fail, so an early return was added when the profile was
"empty" (no samples).
Unfortunately, this was problematic. Profiles with no samples can still
be valid, but skipping postDecode meant that the resulting Profile was
missing values from the string table. In particular, net/http/pprof
needs to parse empty profiles in order to pass through the sample and
period types to a final output proto. CL 483137 broke this behavior.
internal/profile.Parse is only used in two places: in cmd/compile to
parse PGO pprof profiles, and in net/http/pprof to parse before/after
pprof profiles for delta profiles. In both cases, the input is never
literally empty (0 bytes). Even a pprof proto with no samples still
contains some header fields, such as sample and period type. Upstream
github.com/google/pprof/profile even has an explicit error on 0 byte
input, so `go tool pprof` will not support such an input.
Thus TestEmptyProfile was misleading; this profile doesn't need to
support empty input at all.
Resolve this by removing TestEmptyProfile and replacing it with an
explicit error on empty input, as upstream
github.com/google/pprof/profile has. For non-empty input, always run
postDecode to ensure the string table is processed.
TestConvertCPUProfileEmpty is reverted back to assert the values from
before CL 483137. Note that in this case "Empty" means no samples, not a
0 byte input.
Continue to allow empty files for PGO in order to minimize the chance of
last minute breakage if some users have empty files.
Fixes #64566.
Change-Id: I83a1f0200ae225ac6da0009d4b2431fe215b283f
Reviewed-on: https://go-review.googlesource.com/c/go/+/547996
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/net/http/pprof')
| -rw-r--r-- | src/net/http/pprof/pprof_test.go | 63 | ||||
| -rw-r--r-- | src/net/http/pprof/testdata/delta_mutex.go | 43 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src/net/http/pprof/pprof_test.go b/src/net/http/pprof/pprof_test.go index f82ad45bf6..24ad59ab39 100644 --- a/src/net/http/pprof/pprof_test.go +++ b/src/net/http/pprof/pprof_test.go @@ -6,12 +6,14 @@ package pprof import ( "bytes" + "encoding/base64" "fmt" "internal/profile" "internal/testenv" "io" "net/http" "net/http/httptest" + "path/filepath" "runtime" "runtime/pprof" "strings" @@ -261,3 +263,64 @@ func seen(p *profile.Profile, fname string) bool { } return false } + +// TestDeltaProfileEmptyBase validates that we still receive a valid delta +// profile even if the base contains no samples. +// +// Regression test for https://go.dev/issue/64566. +func TestDeltaProfileEmptyBase(t *testing.T) { + if testing.Short() { + // Delta profile collection has a 1s minimum. + t.Skip("skipping in -short mode") + } + + testenv.MustHaveGoRun(t) + + gotool, err := testenv.GoTool() + if err != nil { + t.Fatalf("error finding go tool: %v", err) + } + + out, err := testenv.Command(t, gotool, "run", filepath.Join("testdata", "delta_mutex.go")).CombinedOutput() + if err != nil { + t.Fatalf("error running profile collection: %v\noutput: %s", err, out) + } + + // Log the binary output for debugging failures. + b64 := make([]byte, base64.StdEncoding.EncodedLen(len(out))) + base64.StdEncoding.Encode(b64, out) + t.Logf("Output in base64.StdEncoding: %s", b64) + + p, err := profile.Parse(bytes.NewReader(out)) + if err != nil { + t.Fatalf("Parse got err %v want nil", err) + } + + t.Logf("Output as parsed Profile: %s", p) + + if len(p.SampleType) != 2 { + t.Errorf("len(p.SampleType) got %d want 2", len(p.SampleType)) + } + if p.SampleType[0].Type != "contentions" { + t.Errorf(`p.SampleType[0].Type got %q want "contentions"`, p.SampleType[0].Type) + } + if p.SampleType[0].Unit != "count" { + t.Errorf(`p.SampleType[0].Unit got %q want "count"`, p.SampleType[0].Unit) + } + if p.SampleType[1].Type != "delay" { + t.Errorf(`p.SampleType[1].Type got %q want "delay"`, p.SampleType[1].Type) + } + if p.SampleType[1].Unit != "nanoseconds" { + t.Errorf(`p.SampleType[1].Unit got %q want "nanoseconds"`, p.SampleType[1].Unit) + } + + if p.PeriodType == nil { + t.Fatal("p.PeriodType got nil want not nil") + } + if p.PeriodType.Type != "contentions" { + t.Errorf(`p.PeriodType.Type got %q want "contentions"`, p.PeriodType.Type) + } + if p.PeriodType.Unit != "count" { + t.Errorf(`p.PeriodType.Unit got %q want "count"`, p.PeriodType.Unit) + } +} diff --git a/src/net/http/pprof/testdata/delta_mutex.go b/src/net/http/pprof/testdata/delta_mutex.go new file mode 100644 index 0000000000..634069c8a0 --- /dev/null +++ b/src/net/http/pprof/testdata/delta_mutex.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This binary collects a 1s delta mutex profile and dumps it to os.Stdout. +// +// This is in a subprocess because we want the base mutex profile to be empty +// (as a regression test for https://go.dev/issue/64566) and the only way to +// force reset the profile is to create a new subprocess. +// +// This manually collects the HTTP response and dumps to stdout in order to +// avoid any flakiness around port selection for a real HTTP server. +package main + +import ( + "bytes" + "fmt" + "log" + "net/http" + "net/http/pprof" + "net/http/httptest" + "runtime" +) + +func main() { + // Disable the mutex profiler. This is the default, but that default is + // load-bearing for this test, which needs the base profile to be empty. + runtime.SetMutexProfileFraction(0) + + h := pprof.Handler("mutex") + + req := httptest.NewRequest("GET", "/debug/pprof/mutex?seconds=1", nil) + rec := httptest.NewRecorder() + rec.Body = new(bytes.Buffer) + + h.ServeHTTP(rec, req) + resp := rec.Result() + if resp.StatusCode != http.StatusOK { + log.Fatalf("Request failed: %s\n%s", resp.Status, rec.Body) + } + + fmt.Print(rec.Body) +} |
