From: Cherry Mui Date: Mon, 7 Nov 2022 21:46:28 +0000 (-0500) Subject: cmd/compile/internal/pgo: allow and ignore profiles with no sample X-Git-Tag: go1.20rc1~380 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=efe541d4e53f0e229e7069adbdcedb7f0b117d8e;p=gostls13.git cmd/compile/internal/pgo: allow and ignore profiles with no sample 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 TryBot-Result: Gopher Robot Reviewed-by: Michael Pratt --- diff --git a/src/cmd/compile/internal/pgo/irgraph.go b/src/cmd/compile/internal/pgo/irgraph.go index 6ca86e7684..311f20ed81 100644 --- a/src/cmd/compile/internal/pgo/irgraph.go +++ b/src/cmd/compile/internal/pgo/irgraph.go @@ -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.