]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pprof: handle empty profile gracefully
authorHÃ¥vard Haugen <havard.haugen@gmail.com>
Thu, 8 Jan 2015 23:11:44 +0000 (00:11 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 26 Apr 2015 20:12:17 +0000 (20:12 +0000)
The command "go tool pprof -top $GOROOT/bin/go /dev/null" now logs that
profile is empty instead of panicking.

Fixes #9207

Change-Id: I3d55c179277cb19ad52c8f24f1aca85db53ee08d
Reviewed-on: https://go-review.googlesource.com/2571
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/pprof/internal/driver/driver.go
src/cmd/pprof/internal/profile/profile.go
src/cmd/pprof/internal/profile/profile_test.go [new file with mode: 0644]

index 9703bafa633e01cb116c35bc4940bd496eb3c86e..acb29d13aa3361c55593595e23a05e7ed326926c 100644 (file)
@@ -1013,6 +1013,10 @@ func generate(interactive bool, prof *profile.Profile, obj plugin.ObjTool, ui pl
                w = outputFile
        }
 
+       if prof.Empty() {
+               return fmt.Errorf("profile is empty")
+       }
+
        value, stype, unit := sampleFormat(prof, f)
        o.SampleType = stype
        rpt := report.New(prof, *o, value, unit)
index 7ee58eee77f354941dd26f7a79e78c8fc1bb4945..5eb641f7cd207917e18870cf5c2c5eb795214eb8 100644 (file)
@@ -565,3 +565,8 @@ func (p *Profile) Demangle(d Demangler) error {
        }
        return nil
 }
+
+// Empty returns true if the profile contains no samples.
+func (p *Profile) Empty() bool {
+       return len(p.Sample) == 0
+}
diff --git a/src/cmd/pprof/internal/profile/profile_test.go b/src/cmd/pprof/internal/profile/profile_test.go
new file mode 100644 (file)
index 0000000..09b11a4
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2015 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.
+
+package profile
+
+import (
+       "bytes"
+       "testing"
+)
+
+func TestEmptyProfile(t *testing.T) {
+       var buf bytes.Buffer
+       p, err := Parse(&buf)
+       if err != nil {
+               t.Error("Want no error, got", err)
+       }
+       if p == nil {
+               t.Fatal("Want a valid profile, got <nil>")
+       }
+       if !p.Empty() {
+               t.Errorf("Profile should be empty, got %#v", p)
+       }
+}