Merge previous profiles, write to latest profile properly

This commit is contained in:
DataHoarder 2023-05-21 10:45:37 +02:00
parent ab33b83bf4
commit b322f65c8b
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -37,6 +37,8 @@ func main() {
log.Printf("Profiling for %s every %s", *profileDuration, *profileInterval)
var compactProfile *pprofile.Profile
for range time.Tick(*profileInterval) {
var wg sync.WaitGroup
profiles := make([]*pprofile.Profile, len(endpoints))
@ -80,8 +82,19 @@ func main() {
if mergedProfile, err := pprofile.Merge(profiles); err != nil {
log.Printf("could not merge profiles: %s", err)
} else {
//maybe redundant
compactProfile := mergedProfile.Compact()
if compactProfile != nil {
if mergedWithPreviousProfile, err := pprofile.Merge([]*pprofile.Profile{mergedProfile, compactProfile}); err != nil {
log.Printf("could not merge with previous profile: %s", err)
//maybe redundant
compactProfile = mergedProfile.Compact()
} else {
//maybe redundant
compactProfile = mergedWithPreviousProfile.Compact()
}
} else {
//maybe redundant
compactProfile = mergedProfile.Compact()
}
profilePath := path.Join(*profileDirectory, fmt.Sprintf("pgo-profile-%d.pprof", time.Now().UTC().Unix()))
if f, err := os.Create(profilePath); err != nil {
log.Printf("error opening output profile %s: %s", profilePath, err)
@ -97,19 +110,26 @@ func main() {
}
//copies to latest atomically
func() {
if err := func() error {
r, err := os.Open(profilePath)
if err != nil {
panic(err)
return err
}
defer r.Close()
w, err := os.Create(path.Join(*profileDirectory, "pgo-profile-latest-temp.pprof"))
if err != nil {
panic(err)
return err
}
defer w.Close()
}()
_ = os.Rename(path.Join(*profileDirectory, "pgo-profile-latest-temp.pprof"), path.Join(*profileDirectory, "pgo-profile-latest.pprof"))
if _, err := io.Copy(w, r); err != nil {
return err
}
return nil
}(); err != nil {
log.Printf("error writing latest profile: %s", err)
} else {
_ = os.Rename(path.Join(*profileDirectory, "pgo-profile-latest-temp.pprof"), path.Join(*profileDirectory, "pgo-profile-latest.pprof"))
}
}
}