]> Creatis software - clitk.git/blob - common/clitkMemoryUsage.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / common / clitkMemoryUsage.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 // clitk include
20 #include "clitkCommon.h"
21 #include "clitkMemoryUsage.h"
22 #ifndef _WIN32
23 #  include <unistd.h>
24 #endif
25
26 void clitk::PrintMemory(bool verbose, std::string s) 
27 {
28 #if CLITK_MEMORY_INFO == 1
29   if (verbose) {
30     static double previous=0;
31     double mem = GetMemoryUsageInMb();
32     if (s != "") std::cout << "==> " << s << ": ";
33     std::cout << mem << "MB (" << mem-previous << "MB)" << std::endl;
34     previous = mem;
35     //DD(stat[i].proc_resident/1000/1000);
36     //DD(stat[i].pid);
37   }
38 #endif
39   }
40
41 double clitk::GetMemoryUsageInMb() 
42 {
43 #if CLITK_MEMORY_INFO == 1
44   //sleep(1); // wait to refresh memory ? need to let the system refresh the mem
45   int * entries = new int;
46   sg_process_stats * stat = new sg_process_stats;
47   int i=0; 
48   stat = sg_get_process_stats(entries);
49   // Search the current pid in the list of processes
50   while (stat[i].pid != getpid()) i++;
51   // Display total memory size 
52   double mem = stat[i].proc_resident/1024/1024; // in Mb
53   return mem;
54 #else
55   return 0.0;
56 #endif
57   }
58