build: -cpuprofile flag

This commit is contained in:
Michael McLoughlin
2018-12-21 17:38:19 -08:00
parent 1ce7acee1d
commit 6d1424dc66

View File

@@ -5,19 +5,30 @@ import (
"io" "io"
"log" "log"
"os" "os"
"runtime/pprof"
"github.com/mmcloughlin/avo/pass" "github.com/mmcloughlin/avo/pass"
"github.com/mmcloughlin/avo/printer" "github.com/mmcloughlin/avo/printer"
) )
type Config struct { type Config struct {
ErrOut io.Writer ErrOut io.Writer
Passes []pass.Interface CPUProfile io.WriteCloser
Passes []pass.Interface
} }
func Main(cfg *Config, context *Context) int { func Main(cfg *Config, context *Context) int {
diag := log.New(cfg.ErrOut, "", 0) diag := log.New(cfg.ErrOut, "", 0)
if cfg.CPUProfile != nil {
defer cfg.CPUProfile.Close()
if err := pprof.StartCPUProfile(cfg.CPUProfile); err != nil {
diag.Println("could not start CPU profile: ", err)
return 1
}
defer pprof.StopCPUProfile()
}
f, errs := context.Result() f, errs := context.Result()
if errs != nil { if errs != nil {
for _, err := range errs { for _, err := range errs {
@@ -37,6 +48,7 @@ func Main(cfg *Config, context *Context) int {
type Flags struct { type Flags struct {
errout *outputValue errout *outputValue
cpuprof *outputValue
printers []*printerValue printers []*printerValue
} }
@@ -46,6 +58,9 @@ func NewFlags(fs *flag.FlagSet) *Flags {
f.errout = newOutputValue(os.Stderr) f.errout = newOutputValue(os.Stderr)
fs.Var(f.errout, "log", "diagnostics output") fs.Var(f.errout, "log", "diagnostics output")
f.cpuprof = newOutputValue(nil)
fs.Var(f.cpuprof, "cpuprofile", "write cpu profile to `file`")
goasm := newPrinterValue(printer.NewGoAsm, os.Stdout) goasm := newPrinterValue(printer.NewGoAsm, os.Stdout)
fs.Var(goasm, "out", "assembly output") fs.Var(goasm, "out", "assembly output")
f.printers = append(f.printers, goasm) f.printers = append(f.printers, goasm)
@@ -67,8 +82,9 @@ func (f *Flags) Config() *Config {
} }
} }
return &Config{ return &Config{
ErrOut: f.errout.w, ErrOut: f.errout.w,
Passes: passes, CPUProfile: f.cpuprof.w,
Passes: passes,
} }
} }