Mercurial > archived > louis > perso > liblxcstats
annotate probes/cpuacct.c @ 6:e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
author | Louis Opter <louis@dotcloud.com> |
---|---|
date | Sat, 01 Jan 2011 16:18:17 +0100 |
parents | 2cb8a6cbe468 |
children | 6f2e13f5fcfd |
rev | line source |
---|---|
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
1 #include <sys/types.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
2 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
3 #include <assert.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
4 #include <errno.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
5 #include <limits.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
6 #include <stdlib.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
7 #include <string.h> |
3
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
8 #include <unistd.h> |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
9 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
10 #include "lxcstats.h" |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
11 #include "_lxcstats.h" |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
12 |
3
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
13 static inline uint32_t |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
14 nsecs_to_user_hz(uint64_t counter) |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
15 { |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
16 /* |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
17 * XXX I don't know if it will work for custom values of HZ in the kernel. |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
18 */ |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
19 return (counter / (NSEC_PER_SEC / sysconf(_SC_CLK_TCK))); |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
20 } |
2cb8a6cbe468
cpuacct: convert per cpu usage from nanoseconds to jiffies
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
21 |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
22 static int |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
23 read_stat(struct lxcst *c) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
24 { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
25 char path[PATH_MAX]; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
26 char *values; |
6
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
27 char *fields[4]; |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
28 int ret; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
29 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
30 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.stat"); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
31 ret = _lxcst_read_file(path, &values); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
32 if (ret > 0) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
33 values[ret - 1] = '\0'; /* Replace the last \n by a \0 */ |
6
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
34 if (_lxcst_strsplit(values, fields, ARRAY_SIZE(fields)) == ARRAY_SIZE(fields)) { |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
35 errno = 0; |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
36 c->cpuacct.user = strtoul(fields[1], NULL, 10); |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
37 c->cpuacct.system = strtoul(fields[3], NULL, 10); |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
38 if (!errno) { |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
39 free(values); |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
40 return (0); |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
41 } |
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
42 } |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
43 free(values); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
44 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
45 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
46 c->cpuacct.user = 0; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
47 c->cpuacct.system = 0; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
48 return (1); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
49 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
50 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
51 static int |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
52 read_usage_percpu(struct lxcst *c) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
53 { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
54 char path[PATH_MAX]; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
55 char *values; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
56 int ret; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
57 char *p; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
58 int ncpus; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
59 int i; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
60 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
61 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.usage_percpu"); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
62 ret = _lxcst_read_file(path, &values); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
63 if (ret > 0) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
64 values[ret - 1] = '\0'; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
65 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
66 p = values; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
67 for (ncpus = 0; ; ncpus++) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
68 p = strchr(p + 1, ' '); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
69 if (!p) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
70 break ; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
71 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
72 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
73 if (ncpus) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
74 c->cpuacct.percpu = calloc(ncpus + 1, sizeof(*c->cpuacct.percpu)); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
75 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
76 if (c->cpuacct.percpu) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
77 errno = 0; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
78 p = values; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
79 for (i = 0; i != ncpus && *p; ++i) { |
6
e2b88f50e136
cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
Louis Opter <louis@dotcloud.com>
parents:
3
diff
changeset
|
80 c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoul(p, &p, 10)); |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
81 if (errno) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
82 goto err; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
83 p += strcspn(p, "0123456789"); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
84 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
85 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
86 free(values); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
87 return (0); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
88 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
89 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
90 err: |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
91 free(c->cpuacct.percpu); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
92 c->cpuacct.percpu = NULL; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
93 free(values); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
94 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
95 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
96 return (1); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
97 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
98 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
99 int |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
100 _lxcst_probe_cpuacct(struct lxcst *c) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
101 { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
102 assert(c); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
103 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
104 return (read_stat(c) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
105 || read_usage_percpu(c)); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
106 } |