-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemMeasure.h
69 lines (65 loc) · 1.96 KB
/
memMeasure.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <unistd.h>
#include <ios>
#include <fstream>
#include <string>
using namespace std;
class ProcessMemoryTracker
{
public:
// returns the current resident set size (RSS) in bytes
static size_t getCurrentMemoryUsage()
{
string token;
size_t size = 0;
// try reading from /proc/self/status for more accurate RSS
ifstream status("/proc/self/status");
while (status >> token)
if (token == "VmRSS:")
{
status >> size;
return size * 1024;
}
// fallback to /proc/self/statm if status read fails
ifstream statm("/proc/self/statm");
size_t vm, rss;
if (statm >> vm >> rss)
return rss * sysconf(_SC_PAGESIZE);
return 0;
}
// format bytes to a human-readable string with better precision
static string formatBytes(size_t bytes)
{
double size = static_cast<double>(bytes);
if (bytes < 1024)
return to_string(bytes) + " B";
if (bytes < 1048576)
return to_string(static_cast<int>(size / 1024)) + " KB";
if (bytes < 1073741824)
return to_string(static_cast<int>(size / 1048576)) + " MB";
return to_string(static_cast<int>(size / 1073741824)) + " GB";
}
// structure to hold memory usage metrics
struct MemorySnapshot
{
size_t memory_usage;
size_t peak_usage;
};
// take a snapshot of the current memory usage
static MemorySnapshot takeSnapshot()
{
// read current memory usage
size_t current = getCurrentMemoryUsage();
// read peak memory usage from status file
ifstream status("/proc/self/status");
string token;
size_t peak = 0;
while (status >> token)
if (token == "VmHWM:")
{
status >> peak;
peak *= 1024;
break;
}
return {current, peak};
}
};