forked from gonzus/JavaScript-V8-XS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_util.cc
57 lines (53 loc) · 1.56 KB
/
pl_util.cc
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
#include <stdio.h>
#include <sys/time.h>
#include "pl_util.h"
#define FILE_MEMORY_STATUS "/proc/self/statm"
double now_us(void)
{
struct timeval tv;
double now = 0.0;
int rc = gettimeofday(&tv, 0);
if (rc == 0) {
now = 1000000.0 * tv.tv_sec + tv.tv_usec;
}
return now;
}
long total_memory_pages(void)
{
long pages = 0;
/*
* /proc/[pid]/statm
* Provides information about memory usage, measured in pages.
* size total program size
* (same as VmSize in /proc/[pid]/status)
* resident resident set size
* (same as VmRSS in /proc/[pid]/status)
* share shared pages (from shared mappings)
* text text (code)
* lib library (unused in Linux 2.6)
* data data + stack
* dirty dirty pages (unused in Linux 2.6)
*/
FILE* fp = 0;
do {
long size, resident, share, text, lib, data, pdirty;
int nread;
fp = fopen(FILE_MEMORY_STATUS, "r");
if (!fp) {
/* silently ignore, some OSs do not have this file */
break;
}
nread = fscanf(fp, "%ld %ld %ld %ld %ld %ld %ld",
&size, &resident, &share, &text, &lib, &data, &pdirty);
if (nread != 7) {
/* silently ignore, avoid noisy errors */
break;
}
pages = size;
} while (0);
if (fp) {
fclose(fp);
fp = 0;
}
return pages;
}