diff --git a/apier/v1/core.go b/apier/v1/core.go index 1c5f280ad..fd551c7fb 100644 --- a/apier/v1/core.go +++ b/apier/v1/core.go @@ -56,3 +56,21 @@ func (cS *CoreSv1) Sleep(arg *utils.DurationArgs, reply *string) error { *reply = utils.OK return nil } + +// StartCPUProfiling is used to start CPUProfiling in the given path +func (cS *CoreSv1) StartCPUProfiling(args, reply *string) error { + if err := cS.cS.StartCPUProfiling(args); err != nil { + return err + } + *reply = utils.OK + return nil +} + +// StopCPUProfiling is used to stop CPUProfiling in the given path +func (cS *CoreSv1) StopCPUProfiling(args, reply *string) error { + if err := cS.cS.StopCPUProfiling(args); err != nil { + return err + } + *reply = utils.OK + return nil +} diff --git a/cores/core.go b/cores/core.go index 89a1fe7b6..86c19e010 100644 --- a/cores/core.go +++ b/cores/core.go @@ -20,7 +20,9 @@ package cores import ( "fmt" + "os" "runtime" + "runtime/pprof" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" @@ -67,3 +69,33 @@ func (cS *CoreService) Status(arg *utils.TenantWithAPIOpts, reply *map[string]in *reply = response return } + +// StartCPUProfiling is used to start CPUProfiling in the given path +func (cS *CoreService) StartCPUProfiling(argPath *string) (err error) { + if *argPath == utils.EmptyString { + return utils.NewErrMandatoryIeMissing("Path") + } + f, err := os.Create(*argPath) + if err != nil { + return fmt.Errorf("could not create CPU profile: %v", err) + } + if err := pprof.StartCPUProfile(f); err != nil { + return fmt.Errorf("could not create CPU profile: %v", err) + } + defer f.Close() + return +} + +// StopCPUProfiling is used to stop CPUProfiling in the given path +func (cS *CoreService) StopCPUProfiling(argPath *string) (err error) { + f, err := os.Create(*argPath) + if err != nil { + return fmt.Errorf("could not create CPU profile: %v", err) + } + if err := pprof.StartCPUProfile(f); err != nil { + // this means CPUProfiling is already active,so we can shut down now + pprof.StopCPUProfile() + return nil + } + return +}