EventAnalysis  1.0.0
Memory.h
Go to the documentation of this file.
1 /*
2  * Memory.h
3  *
4  * Created on: 14 Feb 2020
5  * Author: Nicola Mori
6  */
7 
10 #ifndef MEMORY_H_
11 #define MEMORY_H_
12 
13 #include "core/Exception.h"
14 
15 #include <array>
16 
17 namespace EA {
18 
20 class Memory {
21 public:
23  enum class Status : int { NORMAL = 0, WARNING = 1, CRITICAL = 2, EXHAUSTED = 3 };
24 
26  struct BadLevelsException : public Exception {
28  };
29 
36  static unsigned long long GetOccupation();
37 
45  static Status GetStatus();
46 
53  static void SetMaxMemory(unsigned long long maxMemory) { _maxMemory = maxMemory; }
54 
59  static unsigned long long GetMaxMemory() { return _maxMemory; }
60 
78  static void SetLevels(const std::array<float, 3> &levels);
79 
85  static const std::array<float, 3> &GetLevels() { return _levels; }
86 
88  using UnitType = unsigned long int;
89  static constexpr UnitType KB = 1024UL, MB = 1024UL * 1024UL, GB = 1024UL * 1024UL * 1024UL;
90 
91 private:
92  static std::array<float, 3> _levels;
93  static const int _pageSize;
94  static unsigned long long _maxMemory;
95 };
96 
98 const std::array<const std::string, 4> memLevelNames{"NORMAL", "WARNING", "CRITICAL", "EXHAUSTED"};
100 inline std::ostream &operator<<(std::ostream &out, const Memory::Status status) {
101  return out << memLevelNames[static_cast<int>(status)];
102 }
103 
104 } // namespace EA
105 #endif /* MEMORY_H_ */
A static class containing helper functions for interrogating the memory status.
Definition: Memory.h:20
static void SetMaxMemory(unsigned long long maxMemory)
Set the maximum allocable memory.
Definition: Memory.h:53
unsigned long int UnitType
Conversion factors for common memory usage units.
Definition: Memory.h:88
static Status GetStatus()
Returns a status code for the current memory occupancy.
Definition: Memory.cpp:55
static void SetLevels(const std::array< float, 3 > &levels)
Set the memory levels.
Definition: Memory.cpp:69
static unsigned long long GetMaxMemory()
Get the maximum allocable memory.
Definition: Memory.h:59
static constexpr UnitType MB
Definition: Memory.h:89
Exception(std::string msg="")
Definition: Exception.h:31
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
static unsigned long long GetOccupation()
Returns the current memory occupation in bytes.
Definition: Memory.cpp:27
static const int _pageSize
Definition: Memory.h:93
static unsigned long long _maxMemory
Definition: Memory.h:94
Exception class for badly-defined memory levels.
Definition: Memory.h:26
std::ostream & operator<<(std::ostream &out, InsertionResult ir)
Insertion operator for InsertionResult.
Definition: InsertionResult.h:36
static constexpr UnitType KB
Definition: Memory.h:89
static constexpr UnitType GB
Definition: Memory.h:89
const std::array< const std::string, 4 > memLevelNames
Printable names for the memory levels.
Definition: Memory.h:98
Status
Aliases for memory occupation levels.
Definition: Memory.h:23
Definition: Exception.h:24
static const std::array< float, 3 > & GetLevels()
Set the memory levels.
Definition: Memory.h:85
static std::array< float, 3 > _levels
Definition: Memory.h:92