1 /* Copyright (c) 2010 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
29 #if HAVE_SYS_STATVFS_H
30 #include <sys/statvfs.h>
36 #include "dynamic-string.h"
38 #include "system-stats.h"
42 VLOG_DEFINE_THIS_MODULE(system_stats);
44 /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
45 * Thus, this file tries to compile as much of the code as possible regardless
46 * of the target, by writing "if (LINUX)" instead of "#ifdef __linux__" where
47 * this is possible. */
49 #include <asm/param.h>
56 get_cpu_cores(struct shash *stats)
58 long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
60 shash_add(stats, "cpu", xasprintf("%ld", n_cores));
65 get_load_average(struct shash *stats OVS_UNUSED)
70 if (getloadavg(loadavg, 3) == 3) {
71 shash_add(stats, "load_average",
72 xasprintf("%.2f,%.2f,%.2f",
73 loadavg[0], loadavg[1], loadavg[2]));
81 static unsigned int cached;
84 long int value = sysconf(_SC_PAGESIZE);
94 get_memory_stats(struct shash *stats)
97 unsigned int pagesize = get_page_size();
98 long int phys_pages = sysconf(_SC_PHYS_PAGES);
99 long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
100 int mem_total, mem_used;
102 if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
106 mem_total = phys_pages * (pagesize / 1024);
107 mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
108 shash_add(stats, "memory", xasprintf("%d,%d", mem_total, mem_used));
110 static const char file_name[] = "/proc/meminfo";
111 int mem_used, mem_cache, swap_used;
122 stream = fopen(file_name, "r");
124 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
129 shash_add(&dict, "MemTotal", &mem_total);
130 shash_add(&dict, "MemFree", &mem_free);
131 shash_add(&dict, "Buffers", &buffers);
132 shash_add(&dict, "Cached", &cached);
133 shash_add(&dict, "SwapTotal", &swap_total);
134 shash_add(&dict, "SwapFree", &swap_free);
135 while (fgets(line, sizeof line, stream)) {
139 if (sscanf(line, "%15[^:]: %u", key, &value) == 2) {
140 int *valuep = shash_find_data(&dict, key);
147 shash_destroy(&dict);
149 mem_used = mem_total - mem_free;
150 mem_cache = buffers + cached;
151 swap_used = swap_total - swap_free;
152 shash_add(stats, "memory",
153 xasprintf("%d,%d,%d,%d,%d", mem_total, mem_used, mem_cache,
154 swap_total, swap_used));
158 /* Returns the time at which the system booted, as the number of milliseconds
159 * since the epoch, or 0 if the time of boot cannot be determined. */
163 static long long int cache_expiration = LLONG_MIN;
164 static long long int boot_time;
168 if (time_msec() >= cache_expiration) {
169 static const char stat_file[] = "/proc/stat";
173 cache_expiration = time_msec() + 5 * 1000;
175 stream = fopen(stat_file, "r");
177 VLOG_ERR_ONCE("%s: open failed (%s)", stat_file, strerror(errno));
181 while (fgets(line, sizeof line, stream)) {
183 if (sscanf(line, "btime %lld", &btime) == 1) {
184 boot_time = btime * 1000;
188 VLOG_ERR_ONCE("%s: btime not found", stat_file);
195 static unsigned long long int
196 ticks_to_ms(unsigned long long int ticks)
204 #if USER_HZ == 100 /* Common case. */
205 return ticks * (1000 / USER_HZ);
206 #else /* Alpha and some other architectures. */
207 double factor = 1000.0 / USER_HZ;
208 return ticks * factor + 0.5;
212 struct raw_process_info {
213 unsigned long int vsz; /* Virtual size, in kB. */
214 unsigned long int rss; /* Resident set size, in kB. */
215 long long int uptime; /* ms since started. */
216 long long int cputime; /* ms of CPU used during 'uptime'. */
217 pid_t ppid; /* Parent. */
218 char name[18]; /* Name (surrounded by parentheses). */
222 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
224 unsigned long long int vsize, rss, start_time, utime, stime;
225 long long int start_msec;
233 sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
234 stream = fopen(file_name, "r");
236 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
241 "%*d " /* (1. pid) */
242 "%17s " /* 2. process name */
243 "%*c " /* (3. state) */
245 "%*d " /* (5. pgid) */
246 "%*d " /* (6. sid) */
247 "%*d " /* (7. tty_nr) */
248 "%*d " /* (8. tty_pgrp) */
249 "%*u " /* (9. flags) */
250 "%*u " /* (10. min_flt) */
251 "%*u " /* (11. cmin_flt) */
252 "%*u " /* (12. maj_flt) */
253 "%*u " /* (13. cmaj_flt) */
254 "%llu " /* 14. utime */
255 "%llu " /* 15. stime */
256 "%*d " /* (16. cutime) */
257 "%*d " /* (17. cstime) */
258 "%*d " /* (18. priority) */
259 "%*d " /* (19. nice) */
260 "%*d " /* (20. num_threads) */
261 "%*d " /* (21. always 0) */
262 "%llu " /* 22. start_time */
263 "%llu " /* 23. vsize */
264 "%llu " /* 24. rss */
266 /* These are here for documentation but #if'd out to save
267 * actually parsing them from the stream for no benefit. */
268 "%*lu " /* (25. rsslim) */
269 "%*lu " /* (26. start_code) */
270 "%*lu " /* (27. end_code) */
271 "%*lu " /* (28. start_stack) */
272 "%*lu " /* (29. esp) */
273 "%*lu " /* (30. eip) */
274 "%*lu " /* (31. pending signals) */
275 "%*lu " /* (32. blocked signals) */
276 "%*lu " /* (33. ignored signals) */
277 "%*lu " /* (34. caught signals) */
278 "%*lu " /* (35. whcan) */
279 "%*lu " /* (36. always 0) */
280 "%*lu " /* (37. always 0) */
281 "%*d " /* (38. exit_signal) */
282 "%*d " /* (39. task_cpu) */
283 "%*u " /* (40. rt_priority) */
284 "%*u " /* (41. policy) */
285 "%*llu " /* (42. blkio_ticks) */
286 "%*lu " /* (43. gtime) */
287 "%*ld" /* (44. cgtime) */
289 , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
292 VLOG_ERR_ONCE("%s: fscanf failed", file_name);
296 start_msec = get_boot_time() + ticks_to_ms(start_time);
298 raw->vsz = vsize / 1024;
299 raw->rss = rss * (getpagesize() / 1024);
300 raw->uptime = time_wall_msec() - start_msec;
301 raw->cputime = ticks_to_ms(utime + stime);
308 count_crashes(pid_t pid)
318 sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
319 stream = fopen(file_name, "r");
321 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
325 if (!fgets(line, sizeof line, stream)) {
326 VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
327 feof(stream) ? "end of file" : strerror(errno));
331 paren = strchr(line, '(');
334 if (sscanf(paren + 1, "%d", &x) == 1) {
345 struct process_info {
346 unsigned long int vsz; /* Virtual size, in kB. */
347 unsigned long int rss; /* Resident set size, in kB. */
348 long long int booted; /* ms since monitor started. */
349 int crashes; /* # of crashes (usually 0). */
350 long long int uptime; /* ms since last (re)started by monitor. */
351 long long int cputime; /* ms of CPU used during 'uptime'. */
355 get_process_info(pid_t pid, struct process_info *pinfo)
357 struct raw_process_info child;
360 if (!get_raw_process_info(pid, &child)) {
364 pinfo->vsz = child.vsz;
365 pinfo->rss = child.rss;
366 pinfo->booted = child.uptime;
368 pinfo->uptime = child.uptime;
369 pinfo->cputime = child.cputime;
372 struct raw_process_info parent;
374 get_raw_process_info(child.ppid, &parent);
375 if (!strcmp(child.name, parent.name)) {
376 pinfo->booted = parent.uptime;
377 pinfo->crashes = count_crashes(child.ppid);
385 get_process_stats(struct shash *stats)
390 dir = opendir(ovs_rundir());
392 VLOG_ERR_ONCE("%s: open failed (%s)", ovs_rundir(), strerror(errno));
396 while ((de = readdir(dir)) != NULL) {
397 struct process_info pinfo;
403 #ifdef _DIRENT_HAVE_D_TYPE
404 if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
409 extension = strrchr(de->d_name, '.');
410 if (!extension || strcmp(extension, ".pid")) {
414 file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
415 pid = read_pidfile(file_name);
417 if (pid < 0 || kill(pid, 0)) {
421 key = xasprintf("process_%.*s",
422 (int) (extension - de->d_name), de->d_name);
423 if (shash_find(stats, key)) {
428 if (LINUX && get_process_info(pid, &pinfo)) {
429 value = xasprintf("%lu,%lu,%lld,%d,%lld,%lld",
430 pinfo.vsz, pinfo.rss, pinfo.cputime,
431 pinfo.crashes, pinfo.booted, pinfo.uptime);
436 shash_add_nocopy(stats, key, value);
443 get_filesys_stats(struct shash *stats OVS_UNUSED)
445 #if HAVE_SETMNTENT && HAVE_STATVFS
446 static const char file_name[] = "/etc/mtab";
451 stream = setmntent(file_name, "r");
453 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
458 while ((me = getmntent(stream)) != NULL) {
459 unsigned long long int total, free;
463 /* Skip non-local and read-only filesystems. */
464 if (strncmp(me->mnt_fsname, "/dev", 4)
465 || !strstr(me->mnt_opts, "rw")) {
469 /* Given the mount point we can stat the file system. */
470 if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
475 /* Now format the data. */
477 ds_put_char(&s, ' ');
479 for (p = me->mnt_dir; *p != '\0'; p++) {
480 ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
482 total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
483 free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
484 ds_put_format(&s, ",%llu,%llu", total, total - free);
489 shash_add(stats, "file_systems", ds_steal_cstr(&s));
492 #endif /* HAVE_SETMNTENT && HAVE_STATVFS */
496 get_system_stats(struct shash *stats)
498 get_cpu_cores(stats);
499 get_load_average(stats);
500 get_memory_stats(stats);
501 get_process_stats(stats);
502 get_filesys_stats(stats);