9
|
1 #include <sys/types.h>
|
|
2
|
|
3 #include <assert.h>
|
|
4 #include <errno.h>
|
|
5 #include <limits.h>
|
|
6 #include <stdlib.h>
|
|
7 #include <string.h>
|
|
8
|
|
9 #include "lxcstats.h"
|
|
10 #include "_lxcstats.h"
|
|
11
|
|
12 static int
|
|
13 read_stat(struct lxcst *c)
|
|
14 {
|
|
15 char path[PATH_MAX];
|
|
16 char *values;
|
|
17 char *fields[12];
|
|
18 int ret;
|
|
19
|
|
20 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "memory.stat");
|
|
21 ret = _lxcst_read_file(path, &values);
|
|
22 if (ret > 0) {
|
|
23 values[ret - 1] = '\0'; /* Replace the last \n by a \0 */
|
|
24 if (_lxcst_strsplit(values, fields, ARRAY_SIZE(fields)) == ARRAY_SIZE(fields)) {
|
|
25 errno = 0;
|
|
26 c->memory.cached = strtoull(fields[1], NULL, 10);
|
|
27 c->memory.used = strtoull(fields[3], NULL, 10);
|
|
28 c->memory.mapped = strtoull(fields[5], NULL, 10);
|
|
29 c->memory.swapped = strtoull(fields[11], NULL, 10);
|
|
30 if (!errno) {
|
|
31 free(values);
|
|
32 return (0);
|
|
33 }
|
|
34 }
|
|
35 free(values);
|
|
36 }
|
|
37
|
|
38 memset(&c->memory, 0, sizeof(c->memory));
|
|
39 return (-1);
|
|
40 }
|
|
41
|
|
42 int
|
|
43 _lxcst_probe_memory(struct lxcst *c)
|
|
44 {
|
|
45 assert(c);
|
|
46
|
|
47 return (read_stat(c));
|
|
48 }
|