Skip to content

Commit

Permalink
main, adds CPU profiling support
Browse files Browse the repository at this point in the history
manage.sh, release binaries are compiled using the cpu profiling data.
  • Loading branch information
lsh-0 committed Oct 11, 2023
1 parent 7253b8c commit 08d4e96
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path"
"path/filepath"
"runtime"
"runtime/pprof"
"slices"
"sort"
"strconv"
Expand Down Expand Up @@ -204,7 +205,7 @@ func die(b bool, msg string) {
}
}

func main() {
func do() {
var err error
args := os.Args[1:]

Expand Down Expand Up @@ -318,3 +319,29 @@ func main() {
}
}
}

func do_with_profiling(output_filename string) {
f, err := os.Create(output_filename)
die(err != nil, "could not create CPU profile")
defer f.Close()

err = pprof.StartCPUProfile(f)
die(err != nil, "could not start CPU profile")

defer pprof.StopCPUProfile()

do()
}

func main() {
profile := os.Getenv("VAJ_PROFILE")
if profile != "" {
println("profiling is on")
println("---")
do_with_profiling("cpu.prof")
println("---")
println("wrote cpu.prof")
} else {
do()
}
}
2 changes: 2 additions & 0 deletions manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ elif test "$cmd" = "release"; then
# ld -w is 'disable DWARF generation'
# -trimpath removes leading paths to source files
# -v 'verbose'
# -pgo 'profile-guided-optimisation' using the cpu profile 'cpu.prof'
# -o 'output'
GOOS=linux CGO_ENABLED=0 go build \
-ldflags="-s -w" \
-trimpath \
-v \
-pgo cpu.prof \
-o linux-amd64
sha256sum linux-amd64 > linux-amd64.sha256
echo ---
Expand Down

0 comments on commit 08d4e96

Please sign in to comment.