comparison container.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 2cb8a6cbe468
children fe254ba0818d
comparison
equal deleted inserted replaced
6:e2b88f50e136 7:8339ab15527d
16 { 16 {
17 return (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")); 17 return (strcmp(d->d_name, ".") && strcmp(d->d_name, ".."));
18 } 18 }
19 19
20 struct lxcst * 20 struct lxcst *
21 _lxcst_container_new(struct _lxcst_controller *ct, const char *name) 21 _lxcst_container_new(const struct _lxcst_controller *ct, const char *name)
22 { 22 {
23 char buf[256]; 23 char buf[256];
24 struct lxcst *c; 24 struct lxcst *c;
25 25
26 assert(ct); 26 assert(ct);
54 free(c->cpuacct.percpu); 54 free(c->cpuacct.percpu);
55 free(c); 55 free(c);
56 } 56 }
57 } 57 }
58 58
59 /*
60 * Should be equivalent to the number of directories found under /var/lib/lxc/
61 */
62 int
63 _lxcst_container_count(const struct _lxcst_controller *ct)
64 {
65 int n;
66 struct dirent **c_vec;
67 int count;
68 char buf[PATH_MAX];
69
70 n = scandir(ct->lxc_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL);
71 if (n != -1) {
72 count = n;
73 while (n--) {
74 if (!_lxcst_isdir(_lxcst_join_path(buf, sizeof(buf), ct->lxc_dir, c_vec[n]->d_name)))
75 --count;
76 free(c_vec[n]);
77 }
78 free(c_vec);
79 return (count);
80 }
81
82 return (-1);
83 }
84
59 int 85 int
60 _lxcst_container_read_infos(struct lxcst *c) 86 _lxcst_container_read_infos(struct lxcst *c)
61 { 87 {
62 int i; 88 int i;
63 89
74 lxcst_span_containers(lxcst_handle *hdl, int (*cb)(void *, const struct lxcst *), void *ctx) 100 lxcst_span_containers(lxcst_handle *hdl, int (*cb)(void *, const struct lxcst *), void *ctx)
75 { 101 {
76 int n; 102 int n;
77 struct dirent **c_vec; 103 struct dirent **c_vec;
78 struct lxcst *c; 104 struct lxcst *c;
105 char buf[PATH_MAX];
79 106
80 assert(hdl); 107 assert(hdl);
81 assert(hdl->cgroup_dir); 108 assert(hdl->cgroup_dir);
82 assert(cb); 109 assert(cb);
83 110
111 _lxcst_globals_reset();
112
84 n = scandir(hdl->cgroup_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL); 113 n = scandir(hdl->cgroup_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL);
85 if (n > 0) { 114 if (n > 0) {
86 while (n--) { 115 while (n--) {
87 if (_lxcst_isdir(hdl, c_vec[n])) { 116 if (_lxcst_isdir(_lxcst_join_path(buf, sizeof(buf), hdl->cgroup_dir, c_vec[n]->d_name))) {
88 c = _lxcst_container_new(hdl, c_vec[n]->d_name); 117 c = _lxcst_container_new(hdl, c_vec[n]->d_name);
118 _lxcst_globals.running_containers++;
89 if (c && _lxcst_container_read_infos(c) == 0 119 if (c && _lxcst_container_read_infos(c) == 0
90 && cb(ctx, c)) { 120 && cb(ctx, c)) {
91 errno = EINTR; 121 errno = EINTR;
92 goto abort_by_cb; 122 goto abort_by_cb;
93 } 123 }
97 free(c_vec); 127 free(c_vec);
98 } else if (n < 0) { 128 } else if (n < 0) {
99 return (-1); 129 return (-1);
100 } 130 }
101 131
132 n = _lxcst_container_count(hdl);
133 if (n == -1) {
134 warn("can't get the total number of containers");
135 return (-1);
136 }
137
138 _lxcst_globals.stopped_containers = n - _lxcst_globals.running_containers;
139
102 return (0); 140 return (0);
103 141
104 abort_by_cb: 142 abort_by_cb:
105 free(c_vec[n + 1]); 143 free(c_vec[n + 1]);
106 while (n--) 144 while (n--)