2021-06-10 04:27:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-06-10 04:51:59 +00:00
|
|
|
"path/filepath"
|
2021-06-10 04:27:33 +00:00
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func takeProfiles(ctx context.Context) (fname string, _err error) {
|
2021-06-10 04:51:59 +00:00
|
|
|
dir, err := os.MkdirTemp(".", ".profiles-temp*")
|
2021-06-10 04:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:51:59 +00:00
|
|
|
if err := writeProfiles(ctx, dir); err != nil {
|
|
|
|
_ = os.RemoveAll(dir)
|
2021-06-10 04:27:33 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:51:59 +00:00
|
|
|
fname = fmt.Sprintf("pprof-simulation-%s", time.Now().Format(time.RFC3339))
|
|
|
|
if err := os.Rename(dir, fname); err != nil {
|
|
|
|
_ = os.RemoveAll(dir)
|
2021-06-10 04:27:33 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fname, nil
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:51:59 +00:00
|
|
|
func writeProfiles(ctx context.Context, dir string) error {
|
2021-06-10 04:27:33 +00:00
|
|
|
for _, profile := range pprof.Profiles() {
|
2021-06-10 04:51:59 +00:00
|
|
|
file, err := os.Create(filepath.Join(dir, profile.Name()+".pprof.gz"))
|
|
|
|
if err != nil {
|
2021-06-10 04:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-10 04:51:59 +00:00
|
|
|
if err := profile.WriteTo(file, 0); err != nil {
|
|
|
|
_ = file.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := file.Close(); err != nil {
|
2021-06-10 04:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 04:51:59 +00:00
|
|
|
file, err := os.Create(filepath.Join(dir, "cpu.pprof.gz"))
|
|
|
|
if err != nil {
|
2021-06-10 04:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-10 04:51:59 +00:00
|
|
|
|
|
|
|
if err := pprof.StartCPUProfile(file); err != nil {
|
|
|
|
_ = file.Close()
|
2021-06-10 04:27:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
pprof.StopCPUProfile()
|
2021-06-10 04:51:59 +00:00
|
|
|
err = file.Close()
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
2021-06-10 04:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func profileOnSignal(cctx *cli.Context, signals ...os.Signal) {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, signals...)
|
|
|
|
defer signal.Stop(ch)
|
|
|
|
|
2021-06-10 04:35:26 +00:00
|
|
|
for {
|
2021-06-10 04:27:33 +00:00
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
fname, err := takeProfiles(cctx.Context)
|
|
|
|
switch err {
|
|
|
|
case context.Canceled:
|
|
|
|
return
|
|
|
|
case nil:
|
2024-06-21 18:48:22 +00:00
|
|
|
_, _ = fmt.Fprintf(cctx.App.ErrWriter, "Wrote profile to %q\n", fname)
|
2021-06-10 04:27:33 +00:00
|
|
|
default:
|
2024-06-21 18:48:22 +00:00
|
|
|
_, _ = fmt.Fprintf(cctx.App.ErrWriter, "ERROR: failed to write profile: %s\n", err)
|
2021-06-10 04:27:33 +00:00
|
|
|
}
|
|
|
|
case <-cctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|