mv most of profiling logic to profile.go
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-02-24 16:00:00 +01:00
parent cf61dd4795
commit adfba66f38
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
2 changed files with 55 additions and 24 deletions

46
profile.go Normal file

@ -0,0 +1,46 @@
// Copyright 2023 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"os"
"runtime/pprof"
)
// profileCPU checks whether CPU profiling was requested and if so, enables it.
// NOTE: deferred pprof.StopCPUProfile() needs to be called at the call site of
// this function, otherwise the profiling would end when this function goes out
// of scope.
func profileCPU(prof *string) error {
if *prof != "" {
f, err := os.Create(*prof)
if err != nil {
return err
}
err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
}
return nil
}
// profileMem checks if memory profiling was requested and if so, enables it.
func profileMem(prof *string) error {
if *prof != "" {
f, err := os.Create(*prof)
if err != nil {
return err
}
err = pprof.WriteHeapProfile(f)
if err != nil {
return err
}
}
return nil
}

33
run.go

@ -7,7 +7,6 @@ import (
"errors"
"flag"
"log"
"os"
"runtime/pprof"
"sync"
@ -36,26 +35,19 @@ var (
c2SOMAT3A = flag.Bool("c2somat3a", false, "run CEC2020 version of the SOMA Team-to-Team Adaptive (T3A)")
)
// nolint: gocognit
func run() error {
log.Println("starting math-optim version", "'"+version+"'")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
return err
}
err = pprof.StartCPUProfile(f)
if err != nil {
return err
}
defer pprof.StopCPUProfile()
err := profileCPU(cpuprofile)
if err != nil {
return err
}
// see profile.go on why we're calling this here.
defer pprof.StopCPUProfile()
if *generate {
if !*jDE && !*c2jDE && !*c2SOMAT3A && !*sHC && !*rS {
log.Println("at least one algo needs to be specified, exiting...")
@ -120,16 +112,9 @@ func run() error {
report.SaveAndPrint(*doPrint)
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
return err
}
err = pprof.WriteHeapProfile(f)
if err != nil {
return err
}
err = profileMem(memprofile)
if err != nil {
return err
}
log.Println("looks like we're done")