]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/pgo: allow and ignore profiles with no sample
authorCherry Mui <cherryyz@google.com>
Mon, 7 Nov 2022 21:46:28 +0000 (16:46 -0500)
committerCherry Mui <cherryyz@google.com>
Tue, 8 Nov 2022 18:10:34 +0000 (18:10 +0000)
Passing a profile with no sample is arguably not a user error.
Accept such a profile, and ignore it as it doesn't indicate any
optimizations. This also makes testing easier.

Change-Id: Iae49a4260e20757419643153f50d8d5d51478411
Reviewed-on: https://go-review.googlesource.com/c/go/+/448495
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/cmd/compile/internal/pgo/irgraph.go

index 6ca86e7684230f6efb5d50bd0c2667b47c9786e2..311f20ed8175423b751b2bcd6ff1b180a0f4aafb 100644 (file)
@@ -153,7 +153,9 @@ func New(profileFile string) *Profile {
        }
 
        // Build the node map and totals from the profile graph.
-       p.processprofileGraph(g)
+       if !p.processprofileGraph(g) {
+               return nil
+       }
 
        // Create package-level call graph with weights from profile and IR.
        p.initializeIRGraph()
@@ -166,7 +168,8 @@ func New(profileFile string) *Profile {
 // It initializes NodeMap and Total{Node,Edge}Weight based on the name and
 // callsite to compute node and edge weights which will be used later on to
 // create edges for WeightedCG.
-func (p *Profile) processprofileGraph(g *Graph) {
+// Returns whether it successfully processed the profile.
+func (p *Profile) processprofileGraph(g *Graph) bool {
        nFlat := make(map[string]int64)
        nCum := make(map[string]int64)
        seenStartLine := false
@@ -206,12 +209,18 @@ func (p *Profile) processprofileGraph(g *Graph) {
                }
        }
 
+       if p.TotalNodeWeight == 0 || p.TotalEdgeWeight == 0 {
+               return false // accept but ignore profile with no sample
+       }
+
        if !seenStartLine {
                // TODO(prattic): If Function.start_line is missing we could
                // fall back to using absolute line numbers, which is better
                // than nothing.
                log.Fatal("PGO profile missing Function.start_line data")
        }
+
+       return true
 }
 
 // initializeIRGraph builds the IRGraph by visting all the ir.Func in decl list
@@ -352,11 +361,7 @@ func (p *Profile) createIRGraphEdge(fn *ir.Func, callernode *IRNode, name string
 
 // WeightInPercentage converts profile weights to a percentage.
 func WeightInPercentage(value int64, total int64) float64 {
-       var ratio float64
-       if total != 0 {
-               ratio = (float64(value) / float64(total)) * 100
-       }
-       return ratio
+       return (float64(value) / float64(total)) * 100
 }
 
 // PrintWeightedCallGraphDOT prints IRGraph in DOT format.