Skip to content

Commit

Permalink
cmd/addchain: comprehensive profiling options (#98)
Browse files Browse the repository at this point in the history
Implement comprehensive profiling options in the search subcommand, using the https://github.com/mmcloughlin/profile module. Profiles are configured with the ADDCHAIN_PROFILE environment variable. Removes the `-cpuprofile` option.

Updates #60
Updates #25
  • Loading branch information
mmcloughlin authored May 12, 2021
1 parent 10bb4ec commit db3ac33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
41 changes: 9 additions & 32 deletions cmd/addchain/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package main
import (
"context"
"flag"
"os"
"os/signal"
"runtime"
"runtime/pprof"

"github.com/google/subcommands"
"github.com/mmcloughlin/profile"

"github.com/mmcloughlin/addchain/acc"
"github.com/mmcloughlin/addchain/acc/printer"
Expand All @@ -24,13 +22,12 @@ type search struct {

concurrency int
verbose bool
cpuprofile string
}

func (*search) Name() string { return "search" }
func (*search) Synopsis() string { return "search for an addition chain." }
func (*search) Usage() string {
return `Usage: search [-v] [-p <N>] [-cpuprofile <file>] <expr>
return `Usage: search [-v] [-p <N>] <expr>
Search for an addition chain for <expr>.
Expand All @@ -40,10 +37,16 @@ Search for an addition chain for <expr>.
func (cmd *search) SetFlags(f *flag.FlagSet) {
f.IntVar(&cmd.concurrency, "p", runtime.NumCPU(), "run `N` algorithms in parallel")
f.BoolVar(&cmd.verbose, "v", false, "verbose output")
f.StringVar(&cmd.cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
}

func (cmd *search) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (status subcommands.ExitStatus) {
// Enable profiling.
defer profile.Start(
profile.AllProfiles,
profile.ConfigEnvVar("ADDCHAIN_PROFILE"),
).Stop()

// Parse arguments.
if f.NArg() < 1 {
return cmd.UsageError("missing expression")
}
Expand All @@ -60,32 +63,6 @@ func (cmd *search) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
cmd.Log.Printf("hex: %x", n)
cmd.Log.Printf("dec: %s", n)

// Start profiling.
if cmd.cpuprofile != "" {
f, err := os.Create(cmd.cpuprofile)
if err != nil {
return cmd.Fail("could not create cpu profile: %v", err)
}
defer cmd.CheckClose(&status, f)

if err := pprof.StartCPUProfile(f); err != nil {
return cmd.Fail("could not start cpu profile: %v", err)
}

go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
s := <-c

cmd.Log.Printf("caught %s: stopping cpu profile", s)
pprof.StopCPUProfile()

os.Exit(0)
}()

defer pprof.StopCPUProfile()
}

// Execute an ensemble of algorithms.
ex := exec.NewParallel()
if cmd.verbose {
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/mmcloughlin/addchain

go 1.13

require github.com/google/subcommands v1.2.0
require (
github.com/google/subcommands v1.2.0
github.com/mmcloughlin/profile v0.1.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/mmcloughlin/profile v0.1.1 h1:jhDmAqPyebOsVDOCICJoINoLb/AnLBaUw58nFzxWS2w=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=

0 comments on commit db3ac33

Please sign in to comment.