OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
IpplMemoryUsage.h
Go to the documentation of this file.
1 //
2 // Class IpplMemoryUsage
3 // A simple singleton class which lets the user watch the memory consumption of a program.
4 // ATTENTION: We use following memory convention
5 //
6 // 8 bit = 1 byte = 1e-3 kB = 1e-6 MB
7 // = 1e-3 / 1.024 KiB (KibiByte)
8 // = 1e-3 / 1.024 / 1.024 MiB (MebiByte)
9 //
10 // instead of the usually taken but wrong relation 1024 kB = 1 MB.
11 //
12 // General usage
13 // 1) create the instance using IpplMemoryUsage::getInstance(unit, reset).
14 // The reset boolean indicates wether the memory at creation should be
15 // subtracted from the measurements later on.
16 // The class is based on t getrusage() that returns the memory
17 // consumption in kB (KiloByte). You can specify the return value of
18 // IpplMemoryUsage by the first argument.
19 // Although one can use those input parameters they are only applied
20 // at the first call. Additional calls with different input do NOT
21 // modify the instance.
22 // 2) At any point in the program you can call IpplMemoryUsage::sample()
23 // to collect the data.
24 //
25 // Copyright (c) 2017 - 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
26 // All rights reserved
27 //
28 // Implemented as part of the PhD thesis
29 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
30 //
31 // This file is part of OPAL.
32 //
33 // OPAL is free software: you can redistribute it and/or modify
34 // it under the terms of the GNU General Public License as published by
35 // the Free Software Foundation, either version 3 of the License, or
36 // (at your option) any later version.
37 //
38 // You should have received a copy of the GNU General Public License
39 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
40 //
41 #ifndef IPPL_MEMORYUSAGE_H
42 #define IPPL_MEMORYUSAGE_H
43 
44 #include <memory>
45 #include <string>
46 
47 #include <sys/resource.h>
48 #include <sys/time.h> // not required but increases portability
49 
50 
52 
53 public:
54 
56  typedef std::unique_ptr<IpplMemoryUsage> IpplMemory_t;
57 
58  enum Unit {
59  BIT,
60  B,
61  KB,
62  KiB,
63  MB,
64  MiB,
65  GB,
66  GiB
67  };
68 
69 public:
75  static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset = true);
76 
82  double getMemoryUsage(int core) const;
83 
87  void sample();
88 
92  const std::string& getUnit() const;
93 
94 private:
99 
106  IpplMemoryUsage(Unit unit = Unit::GB, bool reset = true);
107 
112  void sample_m();
113 
114 private:
116  std::unique_ptr<double[]> globalMemPerCore_m;
117  std::string unit_m;
119  double max_rss_m;
121  double who_m;
122 };
123 
124 #endif
const std::string & getUnit() const
static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset=true)
static IpplMemory_t instance_mp
*this
double getMemoryUsage(int core) const
double max_rss_m
max. resident set size [GB] or [GiB]
double conversion_factor_m
to various units. getrusage() returns in kB
std::unique_ptr< double[]> globalMemPerCore_m
memory of all cores
std::unique_ptr< IpplMemoryUsage > IpplMemory_t
std::string unit_m
what's the unit of the memory
IpplMemoryUsage * IpplMemory_p
double who_m
RUSAGE_SELF, RUSAGE_CHILDREN (, RUSAGE_THREAD)
double initial_memory_m
memory usage at construction time [GB] or [GiB]