From 2aeacca908bac43fc97d59f03bf16e6bae1e3fe1 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Thu, 8 Jul 2021 09:23:55 +0300 Subject: [PATCH] Finished adding final_memprof file in stop api --- apis/cores_it_test.go | 2 +- cmd/cgr-engine/cgr-engine.go | 5 ++++- cores/core.go | 16 +++++++++------- services/cores.go | 2 ++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/apis/cores_it_test.go b/apis/cores_it_test.go index 5bbd9d7b8..149340c0f 100644 --- a/apis/cores_it_test.go +++ b/apis/cores_it_test.go @@ -292,7 +292,7 @@ func testCoreItStartCPUProfiling(t *testing.T) { func testCoreItSleep(t *testing.T) { args := &utils.DurationArgs{ - Duration: 500 * time.Millisecond, + Duration: 600 * time.Millisecond, } var reply string if err := coreSBiRpc.Call(context.Background(), utils.CoreSv1Sleep, diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 9c0d038e8..8e3d0c67f 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -346,9 +346,11 @@ func main() { var cS *cores.CoreService var stopMemProf chan struct{} + var memPrfDirForCores string if *memProfDir != utils.EmptyString { shdWg.Add(1) stopMemProf = make(chan struct{}) + memPrfDirForCores = *memProfDir go cores.MemProfiling(*memProfDir, *memProfInterval, *memProfNrFiles, shdWg, stopMemProf, shdChan) defer func() { if cS == nil { @@ -582,7 +584,8 @@ func main() { } // init CoreSv1 - coreS := services.NewCoreService(cfg, caps, server, internalCoreSv1Chan, anz, cpuProfileFile, shdWg, stopMemProf, shdChan, srvDep) + + coreS := services.NewCoreService(cfg, caps, server, internalCoreSv1Chan, anz, cpuProfileFile, memPrfDirForCores, shdWg, stopMemProf, shdChan, srvDep) shdWg.Add(1) if err := coreS.Start(); err != nil { fmt.Println(err) diff --git a/cores/core.go b/cores/core.go index a38fbfc78..d1f4ba4ea 100644 --- a/cores/core.go +++ b/cores/core.go @@ -34,7 +34,7 @@ import ( "github.com/cgrates/cgrates/utils" ) -func NewCoreService(cfg *config.CGRConfig, caps *engine.Caps, file io.Closer, stopChan chan struct{}, +func NewCoreService(cfg *config.CGRConfig, caps *engine.Caps, fileCPU io.Closer, fileMem string, stopChan chan struct{}, shdWg *sync.WaitGroup, stopMemPrf chan struct{}, shdChan *utils.SyncedChan) *CoreService { var st *engine.CapsStats if caps.IsLimited() && cfg.CoreSCfg().CapsStatsInterval != 0 { @@ -46,7 +46,8 @@ func NewCoreService(cfg *config.CGRConfig, caps *engine.Caps, file io.Closer, st shdChan: shdChan, cfg: cfg, CapsStats: st, - fileCPU: file, + fileCPU: fileCPU, + fileMEM: fileMem, } } @@ -56,6 +57,7 @@ type CoreService struct { shdWg *sync.WaitGroup stopMemPrf chan struct{} shdChan *utils.SyncedChan + fileMEM string fileCPU io.Closer fileMx sync.Mutex } @@ -73,13 +75,12 @@ func (cS *CoreService) Shutdown() { // StopChanMemProf will stop the MemoryProfiling Channel in order to create // the final MemoryProfiling when CoreS subsystem will stop. -func (cS *CoreService) StopChanMemProf() bool { +func (cS *CoreService) StopChanMemProf() { if cS.stopMemPrf != nil { + MemProfFile(cS.fileMEM) close(cS.stopMemPrf) cS.stopMemPrf = nil - return true } - return false } func StartCPUProfiling(path string) (file io.WriteCloser, err error) { @@ -190,6 +191,7 @@ func (cS *CoreService) StartMemoryProfiling(args *utils.MemoryPrf) (err error) { } cS.shdWg.Add(1) cS.stopMemPrf = make(chan struct{}) + cS.fileMEM = args.DirPath go MemProfiling(args.DirPath, args.Interval, args.NrFiles, cS.shdWg, cS.stopMemPrf, cS.shdChan) return } @@ -199,7 +201,7 @@ func (cS *CoreService) StopMemoryProfiling() (err error) { if cS.stopMemPrf == nil { return errors.New(" Memory Profiling is not started") } - close(cS.stopMemPrf) - cS.stopMemPrf = nil + cS.fileMEM = path.Join(cS.fileMEM, utils.MemProfFileCgr) + cS.StopChanMemProf() return } diff --git a/services/cores.go b/services/cores.go index 25f019cfa..1fb2eb45f 100644 --- a/services/cores.go +++ b/services/cores.go @@ -44,6 +44,7 @@ func NewCoreService(cfg *config.CGRConfig, caps *engine.Caps, server *cores.Serv cfg: cfg, caps: caps, fileCpu: fileCpu, + fileMem: fileMEM, server: server, anz: anz, srvDep: srvDep, @@ -61,6 +62,7 @@ type CoreService struct { stopMemPrf chan struct{} shdChan *utils.SyncedChan fileCpu io.Closer + fileMem string cS *cores.CoreService rpc *apis.CoreSv1 connChan chan birpc.ClientConnector