comparison probes/cpuacct.c @ 0:6ce4443e7545

Add the draft of an API to collect statistics on LXC
author Louis Opter <kalessin@kalessin.fr>
date Wed, 29 Dec 2010 23:28:14 +0100
parents
children 2cb8a6cbe468
comparison
equal deleted inserted replaced
-1:000000000000 0:6ce4443e7545
1 #include <sys/types.h>
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "lxcstats.h"
11 #include "_lxcstats.h"
12
13 static int
14 read_stat(struct lxcst *c)
15 {
16 char path[PATH_MAX];
17 char *values;
18 int ret;
19
20 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.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 ret = sscanf(values, "user %u\nsystem %u", &c->cpuacct.user, &c->cpuacct.system);
25 free(values);
26 if (ret == 2)
27 return (0);
28 }
29
30 c->cpuacct.user = 0;
31 c->cpuacct.system = 0;
32 return (1);
33 }
34
35 static int
36 read_usage_percpu(struct lxcst *c)
37 {
38 char path[PATH_MAX];
39 char *values;
40 int ret;
41 char *p;
42 int ncpus;
43 int i;
44
45 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.usage_percpu");
46 ret = _lxcst_read_file(path, &values);
47 if (ret > 0) {
48 values[ret - 1] = '\0';
49
50 p = values;
51 for (ncpus = 0; ; ncpus++) {
52 p = strchr(p + 1, ' ');
53 if (!p)
54 break ;
55 }
56
57 if (ncpus)
58 c->cpuacct.percpu = calloc(ncpus + 1, sizeof(*c->cpuacct.percpu));
59
60 if (c->cpuacct.percpu) {
61 errno = 0;
62 p = values;
63 for (i = 0; i != ncpus && *p; ++i) {
64 c->cpuacct.percpu[i] = strtoll(p, &p, 10);
65 if (errno)
66 goto err;
67 p += strcspn(p, "0123456789");
68 }
69
70 free(values);
71 return (0);
72 }
73
74 err:
75 free(c->cpuacct.percpu);
76 c->cpuacct.percpu = NULL;
77 free(values);
78 }
79
80 return (1);
81 }
82
83 int
84 _lxcst_probe_cpuacct(struct lxcst *c)
85 {
86 assert(c);
87
88 return (read_stat(c)
89 || read_usage_percpu(c));
90 }