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;
89 long int value = sysconf(_SC_PAGESIZE);
93 GetSystemInfo(&sysinfo);
94 value = sysinfo.dwPageSize;
105 get_memory_stats(struct smap *stats)
108 unsigned int pagesize = get_page_size();
109 #ifdef _SC_PHYS_PAGES
110 long int phys_pages = sysconf(_SC_PHYS_PAGES);
112 long int phys_pages = 0;
114 #ifdef _SC_AVPHYS_PAGES
115 long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
117 long int avphys_pages = 0;
119 int mem_total, mem_used;
122 if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
126 mem_total = phys_pages * (pagesize / 1024);
127 mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
129 MEMORYSTATUS memory_status;
130 GlobalMemoryStatus(&memory_status);
132 mem_total = memory_status.dwTotalPhys;
133 mem_used = memory_status.dwTotalPhys - memory_status.dwAvailPhys;
135 smap_add_format(stats, "memory", "%d,%d", mem_total, mem_used);
137 static const char file_name[] = "/proc/meminfo";
138 int mem_used, mem_cache, swap_used;
149 stream = fopen(file_name, "r");
151 VLOG_WARN_ONCE("%s: open failed (%s)",
152 file_name, ovs_strerror(errno));
157 shash_add(&dict, "MemTotal", &mem_total);
158 shash_add(&dict, "MemFree", &mem_free);
159 shash_add(&dict, "Buffers", &buffers);
160 shash_add(&dict, "Cached", &cached);
161 shash_add(&dict, "SwapTotal", &swap_total);
162 shash_add(&dict, "SwapFree", &swap_free);
163 while (fgets(line, sizeof line, stream)) {
167 if (ovs_scan(line, "%15[^:]: %u", key, &value)) {
168 int *valuep = shash_find_data(&dict, key);
175 shash_destroy(&dict);
177 mem_used = mem_total - mem_free;
178 mem_cache = buffers + cached;
179 swap_used = swap_total - swap_free;
180 smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
181 mem_total, mem_used, mem_cache, swap_total, swap_used);
185 /* Returns the time at which the system booted, as the number of milliseconds
186 * since the epoch, or 0 if the time of boot cannot be determined. */
190 static long long int cache_expiration = LLONG_MIN;
191 static long long int boot_time;
195 if (time_msec() >= cache_expiration) {
196 static const char stat_file[] = "/proc/stat";
200 cache_expiration = time_msec() + 5 * 1000;
202 stream = fopen(stat_file, "r");
204 VLOG_ERR_ONCE("%s: open failed (%s)",
205 stat_file, ovs_strerror(errno));
209 while (fgets(line, sizeof line, stream)) {
211 if (ovs_scan(line, "btime %lld", &btime)) {
212 boot_time = btime * 1000;
216 VLOG_ERR_ONCE("%s: btime not found", stat_file);
223 static unsigned long long int
224 ticks_to_ms(unsigned long long int ticks)
232 #if USER_HZ == 100 /* Common case. */
233 return ticks * (1000 / USER_HZ);
234 #else /* Alpha and some other architectures. */
235 double factor = 1000.0 / USER_HZ;
236 return ticks * factor + 0.5;
240 struct raw_process_info {
241 unsigned long int vsz; /* Virtual size, in kB. */
242 unsigned long int rss; /* Resident set size, in kB. */
243 long long int uptime; /* ms since started. */
244 long long int cputime; /* ms of CPU used during 'uptime'. */
245 pid_t ppid; /* Parent. */
246 char name[18]; /* Name (surrounded by parentheses). */
250 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
252 unsigned long long int vsize, rss, start_time, utime, stime;
253 long long int start_msec;
261 sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
262 stream = fopen(file_name, "r");
264 VLOG_ERR_ONCE("%s: open failed (%s)",
265 file_name, ovs_strerror(errno));
270 "%*d " /* (1. pid) */
271 "%17s " /* 2. process name */
272 "%*c " /* (3. state) */
274 "%*d " /* (5. pgid) */
275 "%*d " /* (6. sid) */
276 "%*d " /* (7. tty_nr) */
277 "%*d " /* (8. tty_pgrp) */
278 "%*u " /* (9. flags) */
279 "%*u " /* (10. min_flt) */
280 "%*u " /* (11. cmin_flt) */
281 "%*u " /* (12. maj_flt) */
282 "%*u " /* (13. cmaj_flt) */
283 "%llu " /* 14. utime */
284 "%llu " /* 15. stime */
285 "%*d " /* (16. cutime) */
286 "%*d " /* (17. cstime) */
287 "%*d " /* (18. priority) */
288 "%*d " /* (19. nice) */
289 "%*d " /* (20. num_threads) */
290 "%*d " /* (21. always 0) */
291 "%llu " /* 22. start_time */
292 "%llu " /* 23. vsize */
293 "%llu " /* 24. rss */
295 /* These are here for documentation but #if'd out to save
296 * actually parsing them from the stream for no benefit. */
297 "%*lu " /* (25. rsslim) */
298 "%*lu " /* (26. start_code) */
299 "%*lu " /* (27. end_code) */
300 "%*lu " /* (28. start_stack) */
301 "%*lu " /* (29. esp) */
302 "%*lu " /* (30. eip) */
303 "%*lu " /* (31. pending signals) */
304 "%*lu " /* (32. blocked signals) */
305 "%*lu " /* (33. ignored signals) */
306 "%*lu " /* (34. caught signals) */
307 "%*lu " /* (35. whcan) */
308 "%*lu " /* (36. always 0) */
309 "%*lu " /* (37. always 0) */
310 "%*d " /* (38. exit_signal) */
311 "%*d " /* (39. task_cpu) */
312 "%*u " /* (40. rt_priority) */
313 "%*u " /* (41. policy) */
314 "%*llu " /* (42. blkio_ticks) */
315 "%*lu " /* (43. gtime) */
316 "%*ld" /* (44. cgtime) */
318 , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
321 VLOG_ERR_ONCE("%s: fscanf failed", file_name);
325 start_msec = get_boot_time() + ticks_to_ms(start_time);
327 raw->vsz = vsize / 1024;
328 raw->rss = rss * (getpagesize() / 1024);
329 raw->uptime = time_wall_msec() - start_msec;
330 raw->cputime = ticks_to_ms(utime + stime);
337 count_crashes(pid_t pid)
347 sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
348 stream = fopen(file_name, "r");
350 VLOG_WARN_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
354 if (!fgets(line, sizeof line, stream)) {
355 VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
356 feof(stream) ? "end of file" : ovs_strerror(errno));
360 paren = strchr(line, '(');
363 if (ovs_scan(paren + 1, "%d", &x)) {
374 struct process_info {
375 unsigned long int vsz; /* Virtual size, in kB. */
376 unsigned long int rss; /* Resident set size, in kB. */
377 long long int booted; /* ms since monitor started. */
378 int crashes; /* # of crashes (usually 0). */
379 long long int uptime; /* ms since last (re)started by monitor. */
380 long long int cputime; /* ms of CPU used during 'uptime'. */
384 get_process_info(pid_t pid, struct process_info *pinfo)
386 struct raw_process_info child;
389 if (!get_raw_process_info(pid, &child)) {
393 pinfo->vsz = child.vsz;
394 pinfo->rss = child.rss;
395 pinfo->booted = child.uptime;
397 pinfo->uptime = child.uptime;
398 pinfo->cputime = child.cputime;
401 struct raw_process_info parent;
403 get_raw_process_info(child.ppid, &parent);
404 if (!strcmp(child.name, parent.name)) {
405 pinfo->booted = parent.uptime;
406 pinfo->crashes = count_crashes(child.ppid);
414 get_process_stats(struct smap *stats)
420 dir = opendir(ovs_rundir());
422 VLOG_ERR_ONCE("%s: open failed (%s)",
423 ovs_rundir(), ovs_strerror(errno));
427 while ((de = readdir(dir)) != NULL) {
428 struct process_info pinfo;
434 #ifdef _DIRENT_HAVE_D_TYPE
435 if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
440 extension = strrchr(de->d_name, '.');
441 if (!extension || strcmp(extension, ".pid")) {
445 file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
446 pid = read_pidfile(file_name);
452 key = xasprintf("process_%.*s",
453 (int) (extension - de->d_name), de->d_name);
454 if (!smap_get(stats, key)) {
455 if (LINUX && get_process_info(pid, &pinfo)) {
456 smap_add_format(stats, key, "%lu,%lu,%lld,%d,%lld,%lld",
457 pinfo.vsz, pinfo.rss, pinfo.cputime,
458 pinfo.crashes, pinfo.booted, pinfo.uptime);
460 smap_add(stats, key, "");
471 get_filesys_stats(struct smap *stats OVS_UNUSED)
473 #if HAVE_GETMNTENT_R && HAVE_STATVFS
474 static const char file_name[] = "/etc/mtab";
475 struct mntent mntent;
481 stream = setmntent(file_name, "r");
483 VLOG_ERR_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
488 while ((me = getmntent_r(stream, &mntent, buf, sizeof buf)) != NULL) {
489 unsigned long long int total, free;
493 /* Skip non-local and read-only filesystems. */
494 if (strncmp(me->mnt_fsname, "/dev", 4)
495 || !strstr(me->mnt_opts, "rw")) {
499 /* Given the mount point we can stat the file system. */
500 if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
505 /* Now format the data. */
507 ds_put_char(&s, ' ');
509 for (p = me->mnt_dir; *p != '\0'; p++) {
510 ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
512 total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
513 free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
514 ds_put_format(&s, ",%llu,%llu", total, total - free);
519 smap_add(stats, "file_systems", ds_cstr(&s));
522 #endif /* HAVE_GETMNTENT_R && HAVE_STATVFS */
525 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
527 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
528 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
529 static struct latch latch OVS_GUARDED_BY(mutex);
531 static bool started OVS_GUARDED_BY(mutex);
532 static struct smap *system_stats OVS_GUARDED_BY(mutex);
534 static void *system_stats_thread_func(void *);
535 static void discard_stats(void);
537 /* Enables or disables system stats collection, according to 'enable'. */
539 system_stats_enable(bool enable)
541 if (enabled != enable) {
542 ovs_mutex_lock(&mutex);
545 xpthread_create(NULL, NULL, system_stats_thread_func, NULL);
550 xpthread_cond_signal(&cond);
553 ovs_mutex_unlock(&mutex);
557 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
560 * When a new snapshot is available (which only occurs if system stats are
561 * enabled), returns it as an smap owned by the caller. The caller must use
562 * both smap_destroy() and free() to completely free the returned data.
564 * When no new snapshot is available, returns NULL. */
566 system_stats_run(void)
568 struct smap *stats = NULL;
570 ovs_mutex_lock(&mutex);
575 stats = system_stats;
581 ovs_mutex_unlock(&mutex);
586 /* Causes poll_block() to wake up when system_stats_run() needs to be
589 system_stats_wait(void)
597 discard_stats(void) OVS_REQUIRES(mutex)
600 smap_destroy(system_stats);
606 static void * NO_RETURN
607 system_stats_thread_func(void *arg OVS_UNUSED)
609 pthread_detach(pthread_self());
612 long long int next_refresh;
615 ovs_mutex_lock(&mutex);
617 ovs_mutex_cond_wait(&cond, &mutex);
619 ovs_mutex_unlock(&mutex);
621 stats = xmalloc(sizeof *stats);
623 get_cpu_cores(stats);
624 get_load_average(stats);
625 get_memory_stats(stats);
626 get_process_stats(stats);
627 get_filesys_stats(stats);
629 ovs_mutex_lock(&mutex);
631 system_stats = stats;
633 ovs_mutex_unlock(&mutex);
635 next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
637 poll_timer_wait_until(next_refresh);
639 } while (time_msec() < next_refresh);