-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_linux.cpp
76 lines (62 loc) · 1.28 KB
/
utils_linux.cpp
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
70
71
72
73
74
75
76
#include "piler2.h"
#if linux || __linux__
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
double GetRAMSize()
{
const double DEFAULT_RAM = 1e9;
static double RAMMB = 0;
if (RAMMB != 0)
return RAMMB;
int fd = open("/proc/meminfo", O_RDONLY);
if (-1 == fd)
return DEFAULT_RAM;
char Buffer[128];
int n = read(fd, Buffer, sizeof(Buffer) - 1);
close(fd);
fd = -1;
if (n <= 0)
return DEFAULT_RAM;
Buffer[n] = 0;
char *pMem = strstr(Buffer, "Mem: ");
if (0 == pMem)
return DEFAULT_RAM;
int Bytes = atoi(pMem+4);
return (double) Bytes;
}
static unsigned g_uPeakMemUseBytes;
unsigned GetMaxMemUseBytes()
{
return g_uPeakMemUseBytes;
}
unsigned GetMemUseBytes()
{
static char statm[64];
static int PageSize;
if (0 == statm[0])
{
PageSize = sysconf(_SC_PAGESIZE);
pid_t pid = getpid();
sprintf(statm, "/proc/%d/statm", (int) pid);
}
int fd = open(statm, O_RDONLY);
if (-1 == fd)
return 1000000;
char Buffer[64];
int n = read(fd, Buffer, sizeof(Buffer) - 1);
close(fd);
fd = -1;
if (n <= 0)
return 1000000;
Buffer[n] = 0;
int Pages = atoi(Buffer);
unsigned uBytes = Pages*PageSize;
if (uBytes > g_uPeakMemUseBytes)
g_uPeakMemUseBytes = uBytes;
return uBytes;
}
#endif