Started apis for CpuProfiling

This commit is contained in:
porosnicuadrian
2021-06-23 17:09:49 +03:00
committed by Dan Christian Bogos
parent 1449d220b4
commit 2e7d3e7449
2 changed files with 50 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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
}