1 /* Copyright (c) 2010, 2012, 2013, 2014 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"
40 #include "ovs-thread.h"
41 #include "poll-loop.h"
47 VLOG_DEFINE_THIS_MODULE(system_stats);
49 /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
50 * Thus, this file tries to compile as much of the code as possible regardless
51 * of the target, by writing "if (LINUX)" instead of "#ifdef __linux__" where
52 * this is possible. */
55 #include <asm/param.h>
61 get_cpu_cores(struct smap *stats)
63 long int n_cores = count_cpu_cores();
65 smap_add_format(stats, "cpu", "%ld", n_cores);
70 get_load_average(struct smap *stats OVS_UNUSED)
75 if (getloadavg(loadavg, 3) == 3) {
76 smap_add_format(stats, "load_average", "%.2f,%.2f,%.2f",
77 loadavg[0], loadavg[1], loadavg[2]);
85 static unsigned int cached;
88 long int value = sysconf(_SC_PAGESIZE);
98 get_memory_stats(struct smap *stats)
101 unsigned int pagesize = get_page_size();
102 #ifdef _SC_PHYS_PAGES
103 long int phys_pages = sysconf(_SC_PHYS_PAGES);
105 long int phys_pages = 0;
107 #ifdef _SC_AVPHYS_PAGES
108 long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
110 long int avphys_pages = 0;
112 int mem_total, mem_used;
114 if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
118 mem_total = phys_pages * (pagesize / 1024);
119 mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
120 smap_add_format(stats, "memory", "%d,%d", mem_total, mem_used);
122 static const char file_name[] = "/proc/meminfo";
123 int mem_used, mem_cache, swap_used;
134 stream = fopen(file_name, "r");
136 VLOG_WARN_ONCE("%s: open failed (%s)",
137 file_name, ovs_strerror(errno));
142 shash_add(&dict, "MemTotal", &mem_total);
143 shash_add(&dict, "MemFree", &mem_free);
144 shash_add(&dict, "Buffers", &buffers);
145 shash_add(&dict, "Cached", &cached);
146 shash_add(&dict, "SwapTotal", &swap_total);
147 shash_add(&dict, "SwapFree", &swap_free);
148 while (fgets(line, sizeof line, stream)) {
152 if (ovs_scan(line, "%15[^:]: %u", key, &value)) {
153 int *valuep = shash_find_data(&dict, key);
160 shash_destroy(&dict);
162 mem_used = mem_total - mem_free;
163 mem_cache = buffers + cached;
164 swap_used = swap_total - swap_free;
165 smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
166 mem_total, mem_used, mem_cache, swap_total, swap_used);
170 /* Returns the time at which the system booted, as the number of milliseconds
171 * since the epoch, or 0 if the time of boot cannot be determined. */
175 static long long int cache_expiration = LLONG_MIN;
176 static long long int boot_time;
180 if (time_msec() >= cache_expiration) {
181 static const char stat_file[] = "/proc/stat";
185 cache_expiration = time_msec() + 5 * 1000;
187 stream = fopen(stat_file, "r");
189 VLOG_ERR_ONCE("%s: open failed (%s)",
190 stat_file, ovs_strerror(errno));
194 while (fgets(line, sizeof line, stream)) {
196 if (ovs_scan(line, "btime %lld", &btime)) {
197 boot_time = btime * 1000;
201 VLOG_ERR_ONCE("%s: btime not found", stat_file);
208 static unsigned long long int
209 ticks_to_ms(unsigned long long int ticks)
217 #if USER_HZ == 100 /* Common case. */
218 return ticks * (1000 / USER_HZ);
219 #else /* Alpha and some other architectures. */
220 double factor = 1000.0 / USER_HZ;
221 return ticks * factor + 0.5;
225 struct raw_process_info {
226 unsigned long int vsz; /* Virtual size, in kB. */
227 unsigned long int rss; /* Resident set size, in kB. */
228 long long int uptime; /* ms since started. */
229 long long int cputime; /* ms of CPU used during 'uptime'. */
230 pid_t ppid; /* Parent. */
231 char name[18]; /* Name (surrounded by parentheses). */
235 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
237 unsigned long long int vsize, rss, start_time, utime, stime;
238 long long int start_msec;
246 sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
247 stream = fopen(file_name, "r");
249 VLOG_ERR_ONCE("%s: open failed (%s)",
250 file_name, ovs_strerror(errno));
255 "%*d " /* (1. pid) */
256 "%17s " /* 2. process name */
257 "%*c " /* (3. state) */
259 "%*d " /* (5. pgid) */
260 "%*d " /* (6. sid) */
261 "%*d " /* (7. tty_nr) */
262 "%*d " /* (8. tty_pgrp) */
263 "%*u " /* (9. flags) */
264 "%*u " /* (10. min_flt) */
265 "%*u " /* (11. cmin_flt) */
266 "%*u " /* (12. maj_flt) */
267 "%*u " /* (13. cmaj_flt) */
268 "%llu " /* 14. utime */
269 "%llu " /* 15. stime */
270 "%*d " /* (16. cutime) */
271 "%*d " /* (17. cstime) */
272 "%*d " /* (18. priority) */
273 "%*d " /* (19. nice) */
274 "%*d " /* (20. num_threads) */
275 "%*d " /* (21. always 0) */
276 "%llu " /* 22. start_time */
277 "%llu " /* 23. vsize */
278 "%llu " /* 24. rss */
280 /* These are here for documentation but #if'd out to save
281 * actually parsing them from the stream for no benefit. */
282 "%*lu " /* (25. rsslim) */
283 "%*lu " /* (26. start_code) */
284 "%*lu " /* (27. end_code) */
285 "%*lu " /* (28. start_stack) */
286 "%*lu " /* (29. esp) */
287 "%*lu " /* (30. eip) */
288 "%*lu " /* (31. pending signals) */
289 "%*lu " /* (32. blocked signals) */
290 "%*lu " /* (33. ignored signals) */
291 "%*lu " /* (34. caught signals) */
292 "%*lu " /* (35. whcan) */
293 "%*lu " /* (36. always 0) */
294 "%*lu " /* (37. always 0) */
295 "%*d " /* (38. exit_signal) */
296 "%*d " /* (39. task_cpu) */
297 "%*u " /* (40. rt_priority) */
298 "%*u " /* (41. policy) */
299 "%*llu " /* (42. blkio_ticks) */
300 "%*lu " /* (43. gtime) */
301 "%*ld" /* (44. cgtime) */
303 , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
306 VLOG_ERR_ONCE("%s: fscanf failed", file_name);
310 start_msec = get_boot_time() + ticks_to_ms(start_time);
312 raw->vsz = vsize / 1024;
313 raw->rss = rss * (getpagesize() / 1024);
314 raw->uptime = time_wall_msec() - start_msec;
315 raw->cputime = ticks_to_ms(utime + stime);
322 count_crashes(pid_t pid)
332 sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
333 stream = fopen(file_name, "r");
335 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
339 if (!fgets(line, sizeof line, stream)) {
340 VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
341 feof(stream) ? "end of file" : ovs_strerror(errno));
345 paren = strchr(line, '(');
348 if (ovs_scan(paren + 1, "%d", &x)) {
359 struct process_info {
360 unsigned long int vsz; /* Virtual size, in kB. */
361 unsigned long int rss; /* Resident set size, in kB. */
362 long long int booted; /* ms since monitor started. */
363 int crashes; /* # of crashes (usually 0). */
364 long long int uptime; /* ms since last (re)started by monitor. */
365 long long int cputime; /* ms of CPU used during 'uptime'. */
369 get_process_info(pid_t pid, struct process_info *pinfo)
371 struct raw_process_info child;
374 if (!get_raw_process_info(pid, &child)) {
378 pinfo->vsz = child.vsz;
379 pinfo->rss = child.rss;
380 pinfo->booted = child.uptime;
382 pinfo->uptime = child.uptime;
383 pinfo->cputime = child.cputime;
386 struct raw_process_info parent;
388 get_raw_process_info(child.ppid, &parent);
389 if (!strcmp(child.name, parent.name)) {
390 pinfo->booted = parent.uptime;
391 pinfo->crashes = count_crashes(child.ppid);
399 get_process_stats(struct smap *stats)
404 dir = opendir(ovs_rundir());
406 VLOG_ERR_ONCE("%s: open failed (%s)",
407 ovs_rundir(), ovs_strerror(errno));
411 while ((de = readdir(dir)) != NULL) {
412 struct process_info pinfo;
418 #ifdef _DIRENT_HAVE_D_TYPE
419 if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
424 extension = strrchr(de->d_name, '.');
425 if (!extension || strcmp(extension, ".pid")) {
429 file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
430 pid = read_pidfile(file_name);
436 key = xasprintf("process_%.*s",
437 (int) (extension - de->d_name), de->d_name);
438 if (!smap_get(stats, key)) {
439 if (LINUX && get_process_info(pid, &pinfo)) {
440 smap_add_format(stats, key, "%lu,%lu,%lld,%d,%lld,%lld",
441 pinfo.vsz, pinfo.rss, pinfo.cputime,
442 pinfo.crashes, pinfo.booted, pinfo.uptime);
444 smap_add(stats, key, "");
454 get_filesys_stats(struct smap *stats OVS_UNUSED)
456 #if HAVE_GETMNTENT_R && HAVE_STATVFS
457 static const char file_name[] = "/etc/mtab";
458 struct mntent mntent;
464 stream = setmntent(file_name, "r");
466 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
471 while ((me = getmntent_r(stream, &mntent, buf, sizeof buf)) != NULL) {
472 unsigned long long int total, free;
476 /* Skip non-local and read-only filesystems. */
477 if (strncmp(me->mnt_fsname, "/dev", 4)
478 || !strstr(me->mnt_opts, "rw")) {
482 /* Given the mount point we can stat the file system. */
483 if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
488 /* Now format the data. */
490 ds_put_char(&s, ' ');
492 for (p = me->mnt_dir; *p != '\0'; p++) {
493 ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
495 total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
496 free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
497 ds_put_format(&s, ",%llu,%llu", total, total - free);
502 smap_add(stats, "file_systems", ds_cstr(&s));
505 #endif /* HAVE_GETMNTENT_R && HAVE_STATVFS */
508 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
510 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
511 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
512 static struct latch latch OVS_GUARDED_BY(mutex);
514 static bool started OVS_GUARDED_BY(mutex);
515 static struct smap *system_stats OVS_GUARDED_BY(mutex);
517 static void *system_stats_thread_func(void *);
518 static void discard_stats(void);
520 /* Enables or disables system stats collection, according to 'enable'. */
522 system_stats_enable(bool enable)
524 if (enabled != enable) {
525 ovs_mutex_lock(&mutex);
528 xpthread_create(NULL, NULL, system_stats_thread_func, NULL);
533 xpthread_cond_signal(&cond);
536 ovs_mutex_unlock(&mutex);
540 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
543 * When a new snapshot is available (which only occurs if system stats are
544 * enabled), returns it as an smap owned by the caller. The caller must use
545 * both smap_destroy() and free() to completely free the returned data.
547 * When no new snapshot is available, returns NULL. */
549 system_stats_run(void)
551 struct smap *stats = NULL;
553 ovs_mutex_lock(&mutex);
558 stats = system_stats;
564 ovs_mutex_unlock(&mutex);
569 /* Causes poll_block() to wake up when system_stats_run() needs to be
572 system_stats_wait(void)
580 discard_stats(void) OVS_REQUIRES(mutex)
583 smap_destroy(system_stats);
589 static void * NO_RETURN
590 system_stats_thread_func(void *arg OVS_UNUSED)
592 pthread_detach(pthread_self());
595 long long int next_refresh;
598 ovs_mutex_lock(&mutex);
600 ovs_mutex_cond_wait(&cond, &mutex);
602 ovs_mutex_unlock(&mutex);
604 stats = xmalloc(sizeof *stats);
606 get_cpu_cores(stats);
607 get_load_average(stats);
608 get_memory_stats(stats);
609 get_process_stats(stats);
610 get_filesys_stats(stats);
612 ovs_mutex_lock(&mutex);
614 system_stats = stats;
616 ovs_mutex_unlock(&mutex);
618 next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
620 poll_timer_wait_until(next_refresh);
622 } while (time_msec() < next_refresh);