From 34637c8712feac5cf12c8920b6d0c1690918dd47 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Tue, 14 Mar 2017 17:31:16 +0100 Subject: [PATCH] Debug libstatgrab With previous commits, we have to use libstatgrab version >=0.90 now --- cmake/build_opt.cmake | 2 +- common/clitkMemoryUsage.cxx | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmake/build_opt.cmake b/cmake/build_opt.cmake index 1ed1fa5..54a3531 100644 --- a/cmake/build_opt.cmake +++ b/cmake/build_opt.cmake @@ -20,7 +20,7 @@ endif(BUILD_DOXYGEN) # Compilation options option(CLITK_EXPERIMENTAL "Enable experimental software and features" OFF) mark_as_advanced(CLITK_EXPERIMENTAL) -option(CLITK_MEMORY_INFO "Enable memory info (need libstatgrab)" OFF) +option(CLITK_MEMORY_INFO "Enable memory info (need libstatgrab >= v.0.90)" OFF) mark_as_advanced(CLITK_MEMORY_INFO) option(CLITK_BUILD_TOOLS "Build command-line tools" OFF) option(CLITK_BUILD_SEGMENTATION "Build command-line segmentation tools" OFF) diff --git a/common/clitkMemoryUsage.cxx b/common/clitkMemoryUsage.cxx index a8be21a..9cc75d0 100644 --- a/common/clitkMemoryUsage.cxx +++ b/common/clitkMemoryUsage.cxx @@ -42,14 +42,24 @@ double clitk::GetMemoryUsageInMb() { #if CLITK_MEMORY_INFO == 1 //sleep(1); // wait to refresh memory ? need to let the system refresh the mem - size_t * entries = new size_t; - sg_process_stats * stat = new sg_process_stats; + size_t entries; + sg_process_stats *stat; int i=0; - stat = sg_get_process_stats(entries); + sg_init(1); + + stat = sg_get_process_stats(&entries); + if(stat == NULL) + return 0.0; + // Search the current pid in the list of processes - while (stat[i].pid != getpid()) i++; + while (stat->pid != getpid() && i < entries) + { + ++i; + ++stat; + } + // Display total memory size - double mem = stat[i].proc_resident/1000/1000; // in MB + double mem = stat->proc_resident/1000/1000; // in MB return mem; #else return 0.0; -- 2.47.1