comparison 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
comparison
equal deleted inserted replaced
5:8d8c49b066d4 6:e2b88f50e136
2 2
3 #include <assert.h> 3 #include <assert.h>
4 #include <errno.h> 4 #include <errno.h>
5 #include <limits.h> 5 #include <limits.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h> 7 #include <string.h>
9 #include <unistd.h> 8 #include <unistd.h>
10 9
11 #include "lxcstats.h" 10 #include "lxcstats.h"
12 #include "_lxcstats.h" 11 #include "_lxcstats.h"
23 static int 22 static int
24 read_stat(struct lxcst *c) 23 read_stat(struct lxcst *c)
25 { 24 {
26 char path[PATH_MAX]; 25 char path[PATH_MAX];
27 char *values; 26 char *values;
27 char *fields[4];
28 int ret; 28 int ret;
29 29
30 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.stat"); 30 _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.stat");
31 ret = _lxcst_read_file(path, &values); 31 ret = _lxcst_read_file(path, &values);
32 if (ret > 0) { 32 if (ret > 0) {
33 values[ret - 1] = '\0'; /* Replace the last \n by a \0 */ 33 values[ret - 1] = '\0'; /* Replace the last \n by a \0 */
34 ret = sscanf(values, "user %u\nsystem %u", &c->cpuacct.user, &c->cpuacct.system); 34 if (_lxcst_strsplit(values, fields, ARRAY_SIZE(fields)) == ARRAY_SIZE(fields)) {
35 errno = 0;
36 c->cpuacct.user = strtoul(fields[1], NULL, 10);
37 c->cpuacct.system = strtoul(fields[3], NULL, 10);
38 if (!errno) {
39 free(values);
40 return (0);
41 }
42 }
35 free(values); 43 free(values);
36 if (ret == 2)
37 return (0);
38 } 44 }
39 45
40 c->cpuacct.user = 0; 46 c->cpuacct.user = 0;
41 c->cpuacct.system = 0; 47 c->cpuacct.system = 0;
42 return (1); 48 return (1);
69 75
70 if (c->cpuacct.percpu) { 76 if (c->cpuacct.percpu) {
71 errno = 0; 77 errno = 0;
72 p = values; 78 p = values;
73 for (i = 0; i != ncpus && *p; ++i) { 79 for (i = 0; i != ncpus && *p; ++i) {
74 c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoll(p, &p, 10)); 80 c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoul(p, &p, 10));
75 if (errno) 81 if (errno)
76 goto err; 82 goto err;
77 p += strcspn(p, "0123456789"); 83 p += strcspn(p, "0123456789");
78 } 84 }
79 85