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.
28 #if HAVE_SYS_STATVFS_H
29 #include <sys/statvfs.h>
35 #include "dynamic-string.h"
37 #include "system-stats.h"
41 VLOG_DEFINE_THIS_MODULE(system_stats);
43 /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
44 * Thus, this file tries to compile as much of the code as possible regardless
45 * of the target, by writing "if (LINUX)" instead of "#ifdef __linux__" where
46 * this is possible. */
48 #include <asm/param.h>
55 get_cpu_cores(struct shash *stats)
57 long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
59 shash_add(stats, "cpu", xasprintf("%ld", n_cores));
64 get_load_average(struct shash *stats OVS_UNUSED)
69 if (getloadavg(loadavg, 3) == 3) {
70 shash_add(stats, "load_average",
71 xasprintf("%.2f,%.2f,%.2f",
72 loadavg[0], loadavg[1], loadavg[2]));
80 static unsigned int cached;
83 long int value = sysconf(_SC_PAGESIZE);
93 get_memory_stats(struct shash *stats)
96 unsigned int pagesize = get_page_size();
97 long int phys_pages = sysconf(_SC_PHYS_PAGES);
98 #ifdef _SC_AVPHYS_PAGES
99 long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
101 long int avphys_pages = 0;
103 int mem_total, mem_used;
105 if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
109 mem_total = phys_pages * (pagesize / 1024);
110 mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
111 shash_add(stats, "memory", xasprintf("%d,%d", mem_total, mem_used));
113 static const char file_name[] = "/proc/meminfo";
114 int mem_used, mem_cache, swap_used;
125 stream = fopen(file_name, "r");
127 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
132 shash_add(&dict, "MemTotal", &mem_total);
133 shash_add(&dict, "MemFree", &mem_free);
134 shash_add(&dict, "Buffers", &buffers);
135 shash_add(&dict, "Cached", &cached);
136 shash_add(&dict, "SwapTotal", &swap_total);
137 shash_add(&dict, "SwapFree", &swap_free);
138 while (fgets(line, sizeof line, stream)) {
142 if (sscanf(line, "%15[^:]: %u", key, &value) == 2) {
143 int *valuep = shash_find_data(&dict, key);
150 shash_destroy(&dict);
152 mem_used = mem_total - mem_free;
153 mem_cache = buffers + cached;
154 swap_used = swap_total - swap_free;
155 shash_add(stats, "memory",
156 xasprintf("%d,%d,%d,%d,%d", mem_total, mem_used, mem_cache,
157 swap_total, swap_used));
161 /* Returns the time at which the system booted, as the number of milliseconds
162 * since the epoch, or 0 if the time of boot cannot be determined. */
166 static long long int cache_expiration = LLONG_MIN;
167 static long long int boot_time;
171 if (time_msec() >= cache_expiration) {
172 static const char stat_file[] = "/proc/stat";
176 cache_expiration = time_msec() + 5 * 1000;
178 stream = fopen(stat_file, "r");
180 VLOG_ERR_ONCE("%s: open failed (%s)", stat_file, strerror(errno));
184 while (fgets(line, sizeof line, stream)) {
186 if (sscanf(line, "btime %lld", &btime) == 1) {
187 boot_time = btime * 1000;
191 VLOG_ERR_ONCE("%s: btime not found", stat_file);
198 static unsigned long long int
199 ticks_to_ms(unsigned long long int ticks)
207 #if USER_HZ == 100 /* Common case. */
208 return ticks * (1000 / USER_HZ);
209 #else /* Alpha and some other architectures. */
210 double factor = 1000.0 / USER_HZ;
211 return ticks * factor + 0.5;
215 struct raw_process_info {
216 unsigned long int vsz; /* Virtual size, in kB. */
217 unsigned long int rss; /* Resident set size, in kB. */
218 long long int uptime; /* ms since started. */
219 long long int cputime; /* ms of CPU used during 'uptime'. */
220 pid_t ppid; /* Parent. */
221 char name[18]; /* Name (surrounded by parentheses). */
225 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
227 unsigned long long int vsize, rss, start_time, utime, stime;
228 long long int start_msec;
236 sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
237 stream = fopen(file_name, "r");
239 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
244 "%*d " /* (1. pid) */
245 "%17s " /* 2. process name */
246 "%*c " /* (3. state) */
248 "%*d " /* (5. pgid) */
249 "%*d " /* (6. sid) */
250 "%*d " /* (7. tty_nr) */
251 "%*d " /* (8. tty_pgrp) */
252 "%*u " /* (9. flags) */
253 "%*u " /* (10. min_flt) */
254 "%*u " /* (11. cmin_flt) */
255 "%*u " /* (12. maj_flt) */
256 "%*u " /* (13. cmaj_flt) */
257 "%llu " /* 14. utime */
258 "%llu " /* 15. stime */
259 "%*d " /* (16. cutime) */
260 "%*d " /* (17. cstime) */
261 "%*d " /* (18. priority) */
262 "%*d " /* (19. nice) */
263 "%*d " /* (20. num_threads) */
264 "%*d " /* (21. always 0) */
265 "%llu " /* 22. start_time */
266 "%llu " /* 23. vsize */
267 "%llu " /* 24. rss */
269 /* These are here for documentation but #if'd out to save
270 * actually parsing them from the stream for no benefit. */
271 "%*lu " /* (25. rsslim) */
272 "%*lu " /* (26. start_code) */
273 "%*lu " /* (27. end_code) */
274 "%*lu " /* (28. start_stack) */
275 "%*lu " /* (29. esp) */
276 "%*lu " /* (30. eip) */
277 "%*lu " /* (31. pending signals) */
278 "%*lu " /* (32. blocked signals) */
279 "%*lu " /* (33. ignored signals) */
280 "%*lu " /* (34. caught signals) */
281 "%*lu " /* (35. whcan) */
282 "%*lu " /* (36. always 0) */
283 "%*lu " /* (37. always 0) */
284 "%*d " /* (38. exit_signal) */
285 "%*d " /* (39. task_cpu) */
286 "%*u " /* (40. rt_priority) */
287 "%*u " /* (41. policy) */
288 "%*llu " /* (42. blkio_ticks) */
289 "%*lu " /* (43. gtime) */
290 "%*ld" /* (44. cgtime) */
292 , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
295 VLOG_ERR_ONCE("%s: fscanf failed", file_name);
299 start_msec = get_boot_time() + ticks_to_ms(start_time);
301 raw->vsz = vsize / 1024;
302 raw->rss = rss * (getpagesize() / 1024);
303 raw->uptime = time_wall_msec() - start_msec;
304 raw->cputime = ticks_to_ms(utime + stime);
311 count_crashes(pid_t pid)
321 sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
322 stream = fopen(file_name, "r");
324 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
328 if (!fgets(line, sizeof line, stream)) {
329 VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
330 feof(stream) ? "end of file" : strerror(errno));
334 paren = strchr(line, '(');
337 if (sscanf(paren + 1, "%d", &x) == 1) {
348 struct process_info {
349 unsigned long int vsz; /* Virtual size, in kB. */
350 unsigned long int rss; /* Resident set size, in kB. */
351 long long int booted; /* ms since monitor started. */
352 int crashes; /* # of crashes (usually 0). */
353 long long int uptime; /* ms since last (re)started by monitor. */
354 long long int cputime; /* ms of CPU used during 'uptime'. */
358 get_process_info(pid_t pid, struct process_info *pinfo)
360 struct raw_process_info child;
363 if (!get_raw_process_info(pid, &child)) {
367 pinfo->vsz = child.vsz;
368 pinfo->rss = child.rss;
369 pinfo->booted = child.uptime;
371 pinfo->uptime = child.uptime;
372 pinfo->cputime = child.cputime;
375 struct raw_process_info parent;
377 get_raw_process_info(child.ppid, &parent);
378 if (!strcmp(child.name, parent.name)) {
379 pinfo->booted = parent.uptime;
380 pinfo->crashes = count_crashes(child.ppid);
388 get_process_stats(struct shash *stats)
393 dir = opendir(ovs_rundir());
395 VLOG_ERR_ONCE("%s: open failed (%s)", ovs_rundir(), strerror(errno));
399 while ((de = readdir(dir)) != NULL) {
400 struct process_info pinfo;
406 #ifdef _DIRENT_HAVE_D_TYPE
407 if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
412 extension = strrchr(de->d_name, '.');
413 if (!extension || strcmp(extension, ".pid")) {
417 file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
418 pid = read_pidfile(file_name);
424 key = xasprintf("process_%.*s",
425 (int) (extension - de->d_name), de->d_name);
426 if (shash_find(stats, key)) {
431 if (LINUX && get_process_info(pid, &pinfo)) {
432 value = xasprintf("%lu,%lu,%lld,%d,%lld,%lld",
433 pinfo.vsz, pinfo.rss, pinfo.cputime,
434 pinfo.crashes, pinfo.booted, pinfo.uptime);
439 shash_add_nocopy(stats, key, value);
446 get_filesys_stats(struct shash *stats OVS_UNUSED)
448 #if HAVE_SETMNTENT && HAVE_STATVFS
449 static const char file_name[] = "/etc/mtab";
454 stream = setmntent(file_name, "r");
456 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
461 while ((me = getmntent(stream)) != NULL) {
462 unsigned long long int total, free;
466 /* Skip non-local and read-only filesystems. */
467 if (strncmp(me->mnt_fsname, "/dev", 4)
468 || !strstr(me->mnt_opts, "rw")) {
472 /* Given the mount point we can stat the file system. */
473 if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
478 /* Now format the data. */
480 ds_put_char(&s, ' ');
482 for (p = me->mnt_dir; *p != '\0'; p++) {
483 ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
485 total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
486 free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
487 ds_put_format(&s, ",%llu,%llu", total, total - free);
492 shash_add(stats, "file_systems", ds_steal_cstr(&s));
495 #endif /* HAVE_SETMNTENT && HAVE_STATVFS */
499 get_system_stats(struct shash *stats)
501 get_cpu_cores(stats);
502 get_load_average(stats);
503 get_memory_stats(stats);
504 get_process_stats(stats);
505 get_filesys_stats(stats);