linux_memoryload.hpp
Go to the documentation of this file.
1 /**
2  * @author: Daniel Fuchs
3  * @contact: fuxeysolutions@gmail.com
4  *
5  * distributed under the MIT License (MIT).
6  * Copyright (c) Daniel Fuchs
7  *
8  */
9 #pragma once
10 
11 #include <string>
12 #include <chrono>
13 
14 class memoryLoad {
15 public:
16  explicit memoryLoad(std::string memInfo = "/proc/meminfo",
17  std::string memInfoOfProcess = "/proc/self/status",
18  std::string memInfoOfProcessPrefix = "/proc/self/"):
19  totalMemoryInKB(0),
20  currentMemoryUsageInKB(0),
21  memInfoFile(std::move(memInfo)),
22  memInfoOfProcessFile(std::move(memInfoOfProcess)),
23  memInfoOfProcessPrefixFile(std::move(memInfoOfProcessPrefix))
24  {};
25  /**
26  * @brief get total memory of the system in KB
27  * @return total memory in KB
28  */
29  uint64_t getTotalMemoryInKB();
30  /**
31  * @brief get current Memory Usage of the system in KB
32  * @return used memory in KB
33  */
34  uint64_t getCurrentMemUsageInKB();
35  /**
36  * @brief get current memory usage of the system in percent 0-100%
37  * @return 0-100%
38  */
40  /**
41  * @brief get the current memory usage of a process
42  * @param pid - process id
43  * @return memory usage in KB
44  */
45  static uint64_t getMemoryUsedByProcess(int pid);
46  /**
47  * @brief get memory usage of this process (self)
48  * @return memory usage in KB
49  */
50  uint64_t getMemoryUsageByThisProcess();
51 
52 private:
53  bool parseMemoryFile();
54  static uint64_t parseProcessMemoryFile(std::string fileToParse);
55  uint64_t totalMemoryInKB;
56  uint64_t currentMemoryUsageInKB;
57  std::string memInfoFile;
58  std::string memInfoOfProcessFile;
59  std::string memInfoOfProcessPrefixFile;
60  std::chrono::time_point<std::chrono::steady_clock> timeStamp;
61 };
memoryLoad::getMemoryUsageByThisProcess
uint64_t getMemoryUsageByThisProcess()
get memory usage of this process (self)
Definition: linux_memoryload.cpp:50
memoryLoad::getCurrentMemUsageInKB
uint64_t getCurrentMemUsageInKB()
get current Memory Usage of the system in KB
Definition: linux_memoryload.cpp:39
memoryLoad
Definition: linux_memoryload.hpp:14
memoryLoad::getCurrentMemUsageInPercent
float getCurrentMemUsageInPercent()
get current memory usage of the system in percent 0-100%
Definition: linux_memoryload.cpp:44
std
Definition: Application.h:66
memoryLoad::memoryLoad
memoryLoad(std::string memInfo="/proc/meminfo", std::string memInfoOfProcess="/proc/self/status", std::string memInfoOfProcessPrefix="/proc/self/")
Definition: linux_memoryload.hpp:16
memoryLoad::getTotalMemoryInKB
uint64_t getTotalMemoryInKB()
get total memory of the system in KB
Definition: linux_memoryload.cpp:34
memoryLoad::getMemoryUsedByProcess
static uint64_t getMemoryUsedByProcess(int pid)
get the current memory usage of a process
Definition: linux_memoryload.cpp:54