OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
IpplMemoryUsage.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef IPPL_MEMPRYUSAGE_H
12 #define IPPL_MEMPRYUSAGE_H
13 
14 /*************************************************************************
15  * IpplMemoryUsage - a simple singleton class which lets the user watch the
16  * memory consumption of a program.
17  *
18  * ATTENTION: We use following memory convention
19  *
20  * 8 bit = 1 byte = 1e-3 kB = 1e-6 MB
21  * = 1e-3 / 1.024 KiB (KibiByte)
22  * = 1e-3 / 1.024 / 1.024 MiB (MebiByte)
23  *
24  * instead of the usually taken but wrong relation 1024 kB = 1 MB.
25  *
26  * General usage
27  * 1) create the instance using IpplMemoryUsage::getInstance(unit, reset).
28  * The reset boolean indicates wether the memory at creation should be
29  * subtracted from the measurements later on.
30  * The class is based on t getrusage() that returns the memory
31  * consumption in kB (KiloByte). You can specify the return value of
32  * IpplMemoryUsage by the first argument.
33  * Although one can use those input parameters they are only applied
34  * at the first call. Additional calls with different input do NOT
35  * modify the instance.
36  * 2) At any point in the program you can call IpplMemoryUsage::sample()
37  * to collect the data.
38  *************************************************************************/
39 
40 // include files
41 #include "Ippl.h"
42 #include <memory>
43 
44 #include <sys/resource.h>
45 #include <sys/time.h> // not required but increases portability
46 
47 
49 
50 public:
51 
53  typedef std::unique_ptr<IpplMemoryUsage> IpplMemory_t;
54 
55  enum Unit {
56  BIT,
57  B,
58  KB,
59  KiB,
60  MB,
61  MiB,
62  GB,
63  GiB
64  };
65 
66 public:
72  static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset = true);
73 
79  double getMemoryUsage(int core) const;
80 
84  void sample();
85 
89  const std::string& getUnit() const;
90 
91 private:
96 
103  IpplMemoryUsage(Unit unit = Unit::GB, bool reset = true);
104 
109  void sample_m();
110 
111 private:
113  std::unique_ptr<double[]> globalMemPerCore_m;
114  std::string unit_m;
116  double max_rss_m;
118  double who_m;
119 };
120 
121 #endif
std::unique_ptr< IpplMemoryUsage > IpplMemory_t
static IpplMemory_t instance_mp
*this
double initial_memory_m
memory usage at construction time [GB] or [GiB]
IpplMemoryUsage * IpplMemory_p
double conversion_factor_m
to various units. getrusage() returns in kB
static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset=true)
double who_m
RUSAGE_SELF, RUSAGE_CHILDREN (, RUSAGE_THREAD)
std::string unit_m
what&#39;s the unit of the memory
const std::string & getUnit() const
double max_rss_m
max. resident set size [GB] or [GiB]
std::unique_ptr< double[]> globalMemPerCore_m
memory of all cores
double getMemoryUsage(int core) const