Skip to content

Commit

Permalink
Merge pull request #1149 from k0l11/profiling
Browse files Browse the repository at this point in the history
add command line options for profiling
  • Loading branch information
k0l11 authored Dec 18, 2022
2 parents 93c385e + 2b64906 commit 2d1443e
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions cmd/gcsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"runtime"
"runtime/pprof"
"sync"
"time"

Expand All @@ -27,14 +28,15 @@ type opts struct {
config string
out string //file result name
gz bool
prof bool
serve bool
nobrowser bool
keepserving bool
substatOptim bool
verbose bool
options string
debugMinMax bool
cpuprofile string
memprofile string
}

// command line tool; following options are available:
Expand All @@ -43,10 +45,9 @@ func main() {
var opt opts
var version bool
flag.BoolVar(&version, "version", false, "check gcsim version (git hash)")
flag.StringVar(&opt.config, "c", "config.txt", "which profile to use; default config.txt")
flag.StringVar(&opt.config, "c", "config.txt", "which profile to use")
flag.StringVar(&opt.out, "out", "", "output result to file? supply file path (otherwise empty string for disabled). default disabled")
flag.BoolVar(&opt.gz, "gz", false, "gzip json results; require out flag")
flag.BoolVar(&opt.prof, "p", false, "run cpu profile; default false")
flag.BoolVar(&opt.serve, "s", false, "serve file to viewer (local). default false")
flag.BoolVar(&opt.nobrowser, "nb", false, "disable opening default browser")
flag.BoolVar(&opt.keepserving, "ks", false, "keep serving same file without terminating web server")
Expand All @@ -60,18 +61,30 @@ func main() {
- sim_iter (default = 350): RECOMMENDED TO NOT TOUCH. Number of iterations used when optimizing. Only change (increase) this if you are working with a team with extremely high standard deviation (>25% of mean)
- tol_mean (default = 0.015): RECOMMENDED TO NOT TOUCH. Tolerance of changes in DPS mean used in ER optimization
- tol_sd (default = 0.33): RECOMMENDED TO NOT TOUCH. Tolerance of changes in DPS SD used in ER optimization`)
flag.StringVar(&opt.cpuprofile, "cpuprofile", "", `write cpu profile to a file. supply file path (otherwise empty string for disabled).
can be viewed in the browser via "go tool pprof -http=localhost:3000 cpu.prof" (insert your desired host/port/filename, requires Graphviz)`)
flag.StringVar(&opt.memprofile, "memprofile", "", `write memory profile to a file. supply file path (otherwise empty string for disabled).
can be viewed in the browser via "go tool pprof -http=localhost:3000 mem.prof" (insert your desired host/port/filename, requires Graphviz)`)

flag.Parse()

if opt.cpuprofile != "" {
f, err := os.Create(opt.cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}

if version {
fmt.Println(sha1ver)
return
}

if opt.prof {
// defer profile.Start(profile.ProfilePath("./"), profile.CPUProfile).Stop()
}

if opt.serve {
os.Remove("serve_data.json.gz") //not really needed since we truncate anyways
opt.out = "serve_data.json"
Expand Down Expand Up @@ -120,6 +133,18 @@ func main() {
}
serverDone.Wait()
}

if opt.memprofile != "" {
f, err := os.Create(fmt.Sprintf(opt.memprofile))
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}

// open opens the specified URL in the default browser of the user.
Expand Down

0 comments on commit 2d1443e

Please sign in to comment.