Add pprof labels

Add pprof labels to the larger stages of blueprint, including
labels for each mutator.

Change-Id: Ie91daff51811187c1b702a775c05d0b32f7170fc
This commit is contained in:
Colin Cross 2019-01-23 13:21:48 -08:00
parent 716137ec38
commit 3a8c025648

View file

@ -16,6 +16,7 @@ package blueprint
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -24,6 +25,7 @@ import (
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"runtime/pprof"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -66,6 +68,8 @@ const MockModuleListFile = "bplist"
// write phase generates the Ninja manifest text based on the generated build // write phase generates the Ninja manifest text based on the generated build
// actions. // actions.
type Context struct { type Context struct {
context.Context
// set at instantiation // set at instantiation
moduleFactories map[string]ModuleFactory moduleFactories map[string]ModuleFactory
nameInterface NameInterface nameInterface NameInterface
@ -275,6 +279,7 @@ type mutatorInfo struct {
func newContext() *Context { func newContext() *Context {
return &Context{ return &Context{
Context: context.Background(),
moduleFactories: make(map[string]ModuleFactory), moduleFactories: make(map[string]ModuleFactory),
nameInterface: NewSimpleNameInterface(), nameInterface: NewSimpleNameInterface(),
moduleInfo: make(map[Module]*moduleInfo), moduleInfo: make(map[Module]*moduleInfo),
@ -1329,27 +1334,39 @@ func (c *Context) addModule(module *moduleInfo) []error {
// the modules depended upon are defined and that no circular dependencies // the modules depended upon are defined and that no circular dependencies
// exist. // exist.
func (c *Context) ResolveDependencies(config interface{}) (deps []string, errs []error) { func (c *Context) ResolveDependencies(config interface{}) (deps []string, errs []error) {
return c.resolveDependencies(c.Context, config)
}
func (c *Context) resolveDependencies(ctx context.Context, config interface{}) (deps []string, errs []error) {
pprof.Do(ctx, pprof.Labels("blueprint", "ResolveDependencies"), func(ctx context.Context) {
c.liveGlobals = newLiveTracker(config) c.liveGlobals = newLiveTracker(config)
deps, errs = c.generateSingletonBuildActions(config, c.preSingletonInfo, c.liveGlobals) deps, errs = c.generateSingletonBuildActions(config, c.preSingletonInfo, c.liveGlobals)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
errs = c.updateDependencies() errs = c.updateDependencies()
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
mutatorDeps, errs := c.runMutators(config) var mutatorDeps []string
mutatorDeps, errs = c.runMutators(ctx, config)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
deps = append(deps, mutatorDeps...) deps = append(deps, mutatorDeps...)
c.cloneModules() c.cloneModules()
c.dependenciesReady = true c.dependenciesReady = true
})
if len(errs) > 0 {
return nil, errs
}
return deps, nil return deps, nil
} }
@ -1863,31 +1880,39 @@ func (c *Context) updateDependencies() (errs []error) {
// SingletonContext.AddNinjaFileDeps(), and PackageContext.AddNinjaFileDeps() // SingletonContext.AddNinjaFileDeps(), and PackageContext.AddNinjaFileDeps()
// methods. // methods.
func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs []error) { func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs []error) {
pprof.Do(c.Context, pprof.Labels("blueprint", "PrepareBuildActions"), func(ctx context.Context) {
c.buildActionsReady = false c.buildActionsReady = false
if !c.dependenciesReady { if !c.dependenciesReady {
extraDeps, errs := c.ResolveDependencies(config) var extraDeps []string
extraDeps, errs = c.resolveDependencies(ctx, config)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
deps = append(deps, extraDeps...) deps = append(deps, extraDeps...)
} }
depsModules, errs := c.generateModuleBuildActions(config, c.liveGlobals) var depsModules []string
depsModules, errs = c.generateModuleBuildActions(config, c.liveGlobals)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
depsSingletons, errs := c.generateSingletonBuildActions(config, c.singletonInfo, c.liveGlobals) var depsSingletons []string
depsSingletons, errs = c.generateSingletonBuildActions(config, c.singletonInfo, c.liveGlobals)
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
deps = append(deps, depsModules...) deps = append(deps, depsModules...)
deps = append(deps, depsSingletons...) deps = append(deps, depsSingletons...)
if c.ninjaBuildDir != nil { if c.ninjaBuildDir != nil {
c.liveGlobals.addNinjaStringDeps(c.ninjaBuildDir) err := c.liveGlobals.addNinjaStringDeps(c.ninjaBuildDir)
if err != nil {
errs = []error{err}
return
}
} }
pkgNames, depsPackages := c.makeUniquePackageNames(c.liveGlobals) pkgNames, depsPackages := c.makeUniquePackageNames(c.liveGlobals)
@ -1903,17 +1928,24 @@ func (c *Context) PrepareBuildActions(config interface{}) (deps []string, errs [
c.globalRules = c.liveGlobals.rules c.globalRules = c.liveGlobals.rules
c.buildActionsReady = true c.buildActionsReady = true
})
if len(errs) > 0 {
return nil, errs
}
return deps, nil return deps, nil
} }
func (c *Context) runMutators(config interface{}) (deps []string, errs []error) { func (c *Context) runMutators(ctx context.Context, config interface{}) (deps []string, errs []error) {
var mutators []*mutatorInfo var mutators []*mutatorInfo
pprof.Do(ctx, pprof.Labels("blueprint", "runMutators"), func(ctx context.Context) {
mutators = append(mutators, c.earlyMutatorInfo...) mutators = append(mutators, c.earlyMutatorInfo...)
mutators = append(mutators, c.mutatorInfo...) mutators = append(mutators, c.mutatorInfo...)
for _, mutator := range mutators { for _, mutator := range mutators {
pprof.Do(ctx, pprof.Labels("mutator", mutator.name), func(context.Context) {
var newDeps []string var newDeps []string
if mutator.topDownMutator != nil { if mutator.topDownMutator != nil {
newDeps, errs = c.runMutator(config, mutator, topDownMutator) newDeps, errs = c.runMutator(config, mutator, topDownMutator)
@ -1923,9 +1955,18 @@ func (c *Context) runMutators(config interface{}) (deps []string, errs []error)
panic("no mutator set on " + mutator.name) panic("no mutator set on " + mutator.name)
} }
if len(errs) > 0 { if len(errs) > 0 {
return nil, errs return
} }
deps = append(deps, newDeps...) deps = append(deps, newDeps...)
})
if len(errs) > 0 {
return
}
}
})
if len(errs) > 0 {
return nil, errs
} }
return deps, nil return deps, nil
@ -2932,55 +2973,63 @@ func (c *Context) VisitAllModuleVariants(module Module,
// actions to w. If this is called before PrepareBuildActions successfully // actions to w. If this is called before PrepareBuildActions successfully
// completes then ErrBuildActionsNotReady is returned. // completes then ErrBuildActionsNotReady is returned.
func (c *Context) WriteBuildFile(w io.Writer) error { func (c *Context) WriteBuildFile(w io.Writer) error {
var err error
pprof.Do(c.Context, pprof.Labels("blueprint", "WriteBuildFile"), func(ctx context.Context) {
if !c.buildActionsReady { if !c.buildActionsReady {
return ErrBuildActionsNotReady err = ErrBuildActionsNotReady
return
} }
nw := newNinjaWriter(w) nw := newNinjaWriter(w)
err := c.writeBuildFileHeader(nw) err = c.writeBuildFileHeader(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeNinjaRequiredVersion(nw) err = c.writeNinjaRequiredVersion(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeSubninjas(nw) err = c.writeSubninjas(nw)
if err != nil { if err != nil {
return err return
} }
// TODO: Group the globals by package. // TODO: Group the globals by package.
err = c.writeGlobalVariables(nw) err = c.writeGlobalVariables(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeGlobalPools(nw) err = c.writeGlobalPools(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeBuildDir(nw) err = c.writeBuildDir(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeGlobalRules(nw) err = c.writeGlobalRules(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeAllModuleActions(nw) err = c.writeAllModuleActions(nw)
if err != nil { if err != nil {
return err return
} }
err = c.writeAllSingletonActions(nw) err = c.writeAllSingletonActions(nw)
if err != nil {
return
}
})
if err != nil { if err != nil {
return err return err
} }