elvish/pkg/pprof/pprof.go
Qi Xiao 33a04f8dc1 Revamp flag handling in pkg/prog.
Instead of putting all possible flags in prog.Flags, flags are now registered by
the individual subprograms. The 3 flags -sock, -db and -json are shared by
multiple subprograms and still handled by the prog package.

This new design allows separating the support for -cpuprofile into a separate
subprogram, which is no longer included by the default entry point, making the
binary slightly smaller. A new entrypoint "withpprof" is created.

Also include the LSP subprogram in the nodaemon entry point.
2022-02-05 14:52:29 +00:00

34 lines
742 B
Go

// Package pprof adds profiling support to the Elvish program.
package pprof
import (
"fmt"
"os"
"runtime/pprof"
"src.elv.sh/pkg/prog"
)
// Program adds support for the -cpuprofile flag.
type Program struct {
cpuProfile string
}
func (p *Program) RegisterFlags(f *prog.FlagSet) {
f.StringVar(&p.cpuProfile, "cpuprofile", "", "write CPU profile to file")
}
func (p *Program) Run(fds [3]*os.File, _ []string) error {
if p.cpuProfile != "" {
f, err := os.Create(p.cpuProfile)
if err != nil {
fmt.Fprintln(fds[2], "Warning: cannot create CPU profile:", err)
fmt.Fprintln(fds[2], "Continuing without CPU profiling.")
} else {
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
}
return prog.ErrNextProgram
}