1 /* Copyright (c) 2010, 2012, 2013 Nicira, Inc.
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.
18 #include "system-stats.h"
29 #if HAVE_SYS_STATVFS_H
30 #include <sys/statvfs.h>
36 #include "dynamic-string.h"
39 #include "poll-loop.h"
45 VLOG_DEFINE_THIS_MODULE(system_stats);
47 /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
48 * Thus, this file tries to compile as much of the code as possible regardless
49 * of the target, by writing "if (LINUX_DATAPATH)" instead of "#ifdef
50 * __linux__" where this is possible. */
52 #include <asm/param.h>
54 #define LINUX_DATAPATH 0
58 get_cpu_cores(struct smap *stats)
60 long int n_cores = sysconf(_SC_NPROCESSORS_ONLN);
62 smap_add_format(stats, "cpu", "%ld", n_cores);
67 get_load_average(struct smap *stats OVS_UNUSED)
72 if (getloadavg(loadavg, 3) == 3) {
73 smap_add_format(stats, "load_average", "%.2f,%.2f,%.2f",
74 loadavg[0], loadavg[1], loadavg[2]);
82 static unsigned int cached;
85 long int value = sysconf(_SC_PAGESIZE);
95 get_memory_stats(struct smap *stats)
97 if (!LINUX_DATAPATH) {
98 unsigned int pagesize = get_page_size();
100 long int phys_pages = sysconf(_SC_PHYS_PAGES);
102 long int phys_pages = 0;
104 #ifdef _SC_AVPHYS_PAGES
105 long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
107 long int avphys_pages = 0;
109 int mem_total, mem_used;
111 if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
115 mem_total = phys_pages * (pagesize / 1024);
116 mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
117 smap_add_format(stats, "memory", "%d,%d", mem_total, mem_used);
119 static const char file_name[] = "/proc/meminfo";
120 int mem_used, mem_cache, swap_used;
131 stream = fopen(file_name, "r");
133 VLOG_WARN_ONCE("%s: open failed (%s)",
134 file_name, ovs_strerror(errno));
139 shash_add(&dict, "MemTotal", &mem_total);
140 shash_add(&dict, "MemFree", &mem_free);
141 shash_add(&dict, "Buffers", &buffers);
142 shash_add(&dict, "Cached", &cached);
143 shash_add(&dict, "SwapTotal", &swap_total);
144 shash_add(&dict, "SwapFree", &swap_free);
145 while (fgets(line, sizeof line, stream)) {
149 if (sscanf(line, "%15[^:]: %u", key, &value) == 2) {
150 int *valuep = shash_find_data(&dict, key);
157 shash_destroy(&dict);
159 mem_used = mem_total - mem_free;
160 mem_cache = buffers + cached;
161 swap_used = swap_total - swap_free;
162 smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
163 mem_total, mem_used, mem_cache, swap_total, swap_used);
167 /* Returns the time at which the system booted, as the number of milliseconds
168 * since the epoch, or 0 if the time of boot cannot be determined. */
172 static long long int cache_expiration = LLONG_MIN;
173 static long long int boot_time;
175 ovs_assert(LINUX_DATAPATH);
177 if (time_msec() >= cache_expiration) {
178 static const char stat_file[] = "/proc/stat";
182 cache_expiration = time_msec() + 5 * 1000;
184 stream = fopen(stat_file, "r");
186 VLOG_ERR_ONCE("%s: open failed (%s)",
187 stat_file, ovs_strerror(errno));
191 while (fgets(line, sizeof line, stream)) {
193 if (sscanf(line, "btime %lld", &btime) == 1) {
194 boot_time = btime * 1000;
198 VLOG_ERR_ONCE("%s: btime not found", stat_file);
205 static unsigned long long int
206 ticks_to_ms(unsigned long long int ticks)
208 ovs_assert(LINUX_DATAPATH);
214 #if USER_HZ == 100 /* Common case. */
215 return ticks * (1000 / USER_HZ);
216 #else /* Alpha and some other architectures. */
217 double factor = 1000.0 / USER_HZ;
218 return ticks * factor + 0.5;
222 struct raw_process_info {
223 unsigned long int vsz; /* Virtual size, in kB. */
224 unsigned long int rss; /* Resident set size, in kB. */
225 long long int uptime; /* ms since started. */
226 long long int cputime; /* ms of CPU used during 'uptime'. */
227 pid_t ppid; /* Parent. */
228 char name[18]; /* Name (surrounded by parentheses). */
232 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
234 unsigned long long int vsize, rss, start_time, utime, stime;
235 long long int start_msec;
241 ovs_assert(LINUX_DATAPATH);
243 sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
244 stream = fopen(file_name, "r");
246 VLOG_ERR_ONCE("%s: open failed (%s)",
247 file_name, ovs_strerror(errno));
252 "%*d " /* (1. pid) */
253 "%17s " /* 2. process name */
254 "%*c " /* (3. state) */
256 "%*d " /* (5. pgid) */
257 "%*d " /* (6. sid) */
258 "%*d " /* (7. tty_nr) */
259 "%*d " /* (8. tty_pgrp) */
260 "%*u " /* (9. flags) */
261 "%*u " /* (10. min_flt) */
262 "%*u " /* (11. cmin_flt) */
263 "%*u " /* (12. maj_flt) */
264 "%*u " /* (13. cmaj_flt) */
265 "%llu " /* 14. utime */
266 "%llu " /* 15. stime */
267 "%*d " /* (16. cutime) */
268 "%*d " /* (17. cstime) */
269 "%*d " /* (18. priority) */
270 "%*d " /* (19. nice) */
271 "%*d " /* (20. num_threads) */
272 "%*d " /* (21. always 0) */
273 "%llu " /* 22. start_time */
274 "%llu " /* 23. vsize */
275 "%llu " /* 24. rss */
277 /* These are here for documentation but #if'd out to save
278 * actually parsing them from the stream for no benefit. */
279 "%*lu " /* (25. rsslim) */
280 "%*lu " /* (26. start_code) */
281 "%*lu " /* (27. end_code) */
282 "%*lu " /* (28. start_stack) */
283 "%*lu " /* (29. esp) */
284 "%*lu " /* (30. eip) */
285 "%*lu " /* (31. pending signals) */
286 "%*lu " /* (32. blocked signals) */
287 "%*lu " /* (33. ignored signals) */
288 "%*lu " /* (34. caught signals) */
289 "%*lu " /* (35. whcan) */
290 "%*lu " /* (36. always 0) */
291 "%*lu " /* (37. always 0) */
292 "%*d " /* (38. exit_signal) */
293 "%*d " /* (39. task_cpu) */
294 "%*u " /* (40. rt_priority) */
295 "%*u " /* (41. policy) */
296 "%*llu " /* (42. blkio_ticks) */
297 "%*lu " /* (43. gtime) */
298 "%*ld" /* (44. cgtime) */
300 , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
303 VLOG_ERR_ONCE("%s: fscanf failed", file_name);
307 start_msec = get_boot_time() + ticks_to_ms(start_time);
309 raw->vsz = vsize / 1024;
310 raw->rss = rss * (getpagesize() / 1024);
311 raw->uptime = time_wall_msec() - start_msec;
312 raw->cputime = ticks_to_ms(utime + stime);
319 count_crashes(pid_t pid)
327 ovs_assert(LINUX_DATAPATH);
329 sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
330 stream = fopen(file_name, "r");
332 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
336 if (!fgets(line, sizeof line, stream)) {
337 VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
338 feof(stream) ? "end of file" : ovs_strerror(errno));
342 paren = strchr(line, '(');
345 if (sscanf(paren + 1, "%d", &x) == 1) {
356 struct process_info {
357 unsigned long int vsz; /* Virtual size, in kB. */
358 unsigned long int rss; /* Resident set size, in kB. */
359 long long int booted; /* ms since monitor started. */
360 int crashes; /* # of crashes (usually 0). */
361 long long int uptime; /* ms since last (re)started by monitor. */
362 long long int cputime; /* ms of CPU used during 'uptime'. */
366 get_process_info(pid_t pid, struct process_info *pinfo)
368 struct raw_process_info child;
370 ovs_assert(LINUX_DATAPATH);
371 if (!get_raw_process_info(pid, &child)) {
375 pinfo->vsz = child.vsz;
376 pinfo->rss = child.rss;
377 pinfo->booted = child.uptime;
379 pinfo->uptime = child.uptime;
380 pinfo->cputime = child.cputime;
383 struct raw_process_info parent;
385 get_raw_process_info(child.ppid, &parent);
386 if (!strcmp(child.name, parent.name)) {
387 pinfo->booted = parent.uptime;
388 pinfo->crashes = count_crashes(child.ppid);
396 get_process_stats(struct smap *stats)
401 dir = opendir(ovs_rundir());
403 VLOG_ERR_ONCE("%s: open failed (%s)",
404 ovs_rundir(), ovs_strerror(errno));
408 while ((de = readdir(dir)) != NULL) {
409 struct process_info pinfo;
415 #ifdef _DIRENT_HAVE_D_TYPE
416 if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
421 extension = strrchr(de->d_name, '.');
422 if (!extension || strcmp(extension, ".pid")) {
426 file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
427 pid = read_pidfile(file_name);
433 key = xasprintf("process_%.*s",
434 (int) (extension - de->d_name), de->d_name);
435 if (!smap_get(stats, key)) {
436 if (LINUX_DATAPATH && get_process_info(pid, &pinfo)) {
437 smap_add_format(stats, key, "%lu,%lu,%lld,%d,%lld,%lld",
438 pinfo.vsz, pinfo.rss, pinfo.cputime,
439 pinfo.crashes, pinfo.booted, pinfo.uptime);
441 smap_add(stats, key, "");
451 get_filesys_stats(struct smap *stats OVS_UNUSED)
453 #if HAVE_GETMNTENT_R && HAVE_STATVFS
454 static const char file_name[] = "/etc/mtab";
455 struct mntent mntent;
461 stream = setmntent(file_name, "r");
463 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
468 while ((me = getmntent_r(stream, &mntent, buf, sizeof buf)) != NULL) {
469 unsigned long long int total, free;
473 /* Skip non-local and read-only filesystems. */
474 if (strncmp(me->mnt_fsname, "/dev", 4)
475 || !strstr(me->mnt_opts, "rw")) {
479 /* Given the mount point we can stat the file system. */
480 if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
485 /* Now format the data. */
487 ds_put_char(&s, ' ');
489 for (p = me->mnt_dir; *p != '\0'; p++) {
490 ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
492 total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
493 free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
494 ds_put_format(&s, ",%llu,%llu", total, total - free);
499 smap_add(stats, "file_systems", ds_cstr(&s));
502 #endif /* HAVE_GETMNTENT_R && HAVE_STATVFS */
505 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
507 /* The next time to wake up, or LLONG_MAX if stats are disabled. */
508 static long long int next_refresh = LLONG_MAX;
510 /* Enables or disables system stats collection, according to 'enable'. */
512 system_stats_enable(bool enable)
515 next_refresh = LLONG_MAX;
516 } else if (next_refresh == LLONG_MAX) {
517 next_refresh = time_msec();
521 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
524 * When a new snapshot is available (which only occurs if system stats are
525 * enabled), returns it as an smap owned by the caller. The caller must use
526 * both smap_destroy() and free() to completely free the returned data.
528 * When no new snapshot is available, returns NULL. */
530 system_stats_run(void)
532 if (time_msec() >= next_refresh) {
535 stats = xmalloc(sizeof *stats);
537 get_cpu_cores(stats);
538 get_load_average(stats);
539 get_memory_stats(stats);
540 get_process_stats(stats);
541 get_filesys_stats(stats);
543 next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
551 /* Causes poll_block() to wake up when system_stats_run() needs to be
554 system_stats_wait(void)
556 if (next_refresh != LLONG_MAX) {
557 poll_timer_wait_until(next_refresh);