Mercurial > archived > louis > perso > liblxcstats
annotate utils.c @ 7:8339ab15527d
Add running/stopped containers global statistics and refactor some things
author | Louis Opter <louis@dotcloud.com> |
---|---|
date | Sat, 01 Jan 2011 17:46:40 +0100 |
parents | 50215911acb3 |
children | 3a1977ecccc7 |
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 #include <sys/mman.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
3 #include <sys/stat.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
4 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
5 #include <assert.h> |
4
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
6 #include <ctype.h> |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
7 #include <err.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
8 #include <errno.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
9 #include <fcntl.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
10 #include <dirent.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
11 #include <stdlib.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
12 #include <string.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
13 #include <unistd.h> |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
14 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
15 #include "_lxcstats.h" |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
16 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
17 char * |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
18 _lxcst_join_path(char *dest, size_t size, const char *left, const char *right) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
19 { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
20 if (*right != '/') { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
21 _lxcst_strlcpy(dest, left, size); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
22 _lxcst_strlcat(dest, "/", size); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
23 _lxcst_strlcat(dest, right, size); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
24 } else { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
25 _lxcst_strlcpy(dest, right, size); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
26 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
27 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
28 return (dest); |
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 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
31 int |
7
8339ab15527d
Add running/stopped containers global statistics and refactor some things
Louis Opter <louis@dotcloud.com>
parents:
4
diff
changeset
|
32 _lxcst_isdir(const char *path) |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
33 { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
34 struct stat sb; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
35 |
7
8339ab15527d
Add running/stopped containers global statistics and refactor some things
Louis Opter <louis@dotcloud.com>
parents:
4
diff
changeset
|
36 if (stat(path, &sb) == -1) { |
8339ab15527d
Add running/stopped containers global statistics and refactor some things
Louis Opter <louis@dotcloud.com>
parents:
4
diff
changeset
|
37 warn("can't stat path: %s", path); |
0
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
38 return (0); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
39 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
40 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
41 return (sb.st_mode & S_IFDIR); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
42 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
43 |
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 * Files under cgroups are virtual and always have a size of zero so we have to |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
46 * do some reallocs. |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
47 */ |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
48 ssize_t |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
49 _lxcst_read_file(const char *path, char **content) |
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 char buf[1024]; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
52 int fd; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
53 ssize_t ret; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
54 ssize_t size; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
55 char *p; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
56 int sverrno; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
57 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
58 assert(path); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
59 assert(content); |
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 *content = NULL; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
62 size = 0; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
63 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
64 fd = open(path, O_RDONLY); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
65 if (fd != -1) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
66 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
67 while (1) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
68 ret = read(fd, buf, sizeof(buf)); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
69 if (ret == -1) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
70 if (errno == EINTR) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
71 continue ; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
72 break ; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
73 } |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
74 if (ret == 0) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
75 break ; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
76 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
77 p = realloc(*content, size + ret); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
78 if (!p) |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
79 break ; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
80 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
81 *content = p; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
82 memcpy(&content[0][size], buf, ret); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
83 size += ret; |
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 if (*content) { |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
87 close(fd); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
88 return (size); |
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 |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
91 sverrno = errno; |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
92 close(fd); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
93 errno = sverrno; |
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 free(*content); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
97 return (ret); |
6ce4443e7545
Add the draft of an API to collect statistics on LXC
Louis Opter <kalessin@kalessin.fr>
parents:
diff
changeset
|
98 } |
4
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
99 |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
100 int |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
101 _lxcst_strsplit(char *str, char **fields, size_t nfields) |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
102 { |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
103 char prev; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
104 int i; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
105 |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
106 prev = ' '; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
107 i = 0; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
108 while (*str && nfields) { |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
109 if (!isspace(*str)) { |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
110 if (isspace(prev)) { /* new word */ |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
111 fields[i] = str; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
112 ++i; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
113 --nfields; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
114 } |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
115 prev = *str; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
116 } else { |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
117 prev = *str; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
118 *str = '\0'; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
119 } |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
120 str++; |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
121 } |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
122 |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
123 return (i); |
50215911acb3
Add a strsplit() function and stop to build probes into a separate library
Louis Opter <louis@dotcloud.com>
parents:
0
diff
changeset
|
124 } |