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 <chrono>
12#include <string>
13
15{
16public:
17 explicit memoryLoad(std::string memInfo = "/proc/meminfo",
18 std::string memInfoOfProcess = "/proc/self/status",
19 std::string memInfoOfProcessPrefix = "/proc/self/") :
20 totalMemoryInKB(0),
21 currentMemoryUsageInKB(0),
22 memInfoFile(std::move(memInfo)),
23 memInfoOfProcessFile(std::move(memInfoOfProcess)),
24 memInfoOfProcessPrefixFile(std::move(memInfoOfProcessPrefix)){};
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 */
51
52private:
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};
uint64_t getTotalMemoryInKB()
get total memory of the system in KB
uint64_t getMemoryUsageByThisProcess()
get memory usage of this process (self)
memoryLoad(std::string memInfo="/proc/meminfo", std::string memInfoOfProcess="/proc/self/status", std::string memInfoOfProcessPrefix="/proc/self/")
float getCurrentMemUsageInPercent()
get current memory usage of the system in percent 0-100%
uint64_t getCurrentMemUsageInKB()
get current Memory Usage of the system in KB
static uint64_t getMemoryUsedByProcess(int pid)
get the current memory usage of a process