]>
git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/system-stats.c
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"
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_DATAPATH)" instead of "#ifdef
52 * __linux__" where this is possible. */
54 #include <asm/param.h>
56 #define LINUX_DATAPATH 0
60 get_cpu_cores(struct smap
*stats
)
62 long int n_cores
= count_cpu_cores();
64 smap_add_format(stats
, "cpu", "%ld", n_cores
);
69 get_load_average(struct smap
*stats OVS_UNUSED
)
74 if (getloadavg(loadavg
, 3) == 3) {
75 smap_add_format(stats
, "load_average", "%.2f,%.2f,%.2f",
76 loadavg
[0], loadavg
[1], loadavg
[2]);
84 static unsigned int cached
;
87 long int value
= sysconf(_SC_PAGESIZE
);
97 get_memory_stats(struct smap
*stats
)
99 if (!LINUX_DATAPATH
) {
100 unsigned int pagesize
= get_page_size();
101 #ifdef _SC_PHYS_PAGES
102 long int phys_pages
= sysconf(_SC_PHYS_PAGES
);
104 long int phys_pages
= 0;
106 #ifdef _SC_AVPHYS_PAGES
107 long int avphys_pages
= sysconf(_SC_AVPHYS_PAGES
);
109 long int avphys_pages
= 0;
111 int mem_total
, mem_used
;
113 if (pagesize
<= 0 || phys_pages
<= 0 || avphys_pages
<= 0) {
117 mem_total
= phys_pages
* (pagesize
/ 1024);
118 mem_used
= (phys_pages
- avphys_pages
) * (pagesize
/ 1024);
119 smap_add_format(stats
, "memory", "%d,%d", mem_total
, mem_used
);
121 static const char file_name
[] = "/proc/meminfo";
122 int mem_used
, mem_cache
, swap_used
;
133 stream
= fopen(file_name
, "r");
135 VLOG_WARN_ONCE("%s: open failed (%s)",
136 file_name
, ovs_strerror(errno
));
141 shash_add(&dict
, "MemTotal", &mem_total
);
142 shash_add(&dict
, "MemFree", &mem_free
);
143 shash_add(&dict
, "Buffers", &buffers
);
144 shash_add(&dict
, "Cached", &cached
);
145 shash_add(&dict
, "SwapTotal", &swap_total
);
146 shash_add(&dict
, "SwapFree", &swap_free
);
147 while (fgets(line
, sizeof line
, stream
)) {
151 if (ovs_scan(line
, "%15[^:]: %u", key
, &value
)) {
152 int *valuep
= shash_find_data(&dict
, key
);
159 shash_destroy(&dict
);
161 mem_used
= mem_total
- mem_free
;
162 mem_cache
= buffers
+ cached
;
163 swap_used
= swap_total
- swap_free
;
164 smap_add_format(stats
, "memory", "%d,%d,%d,%d,%d",
165 mem_total
, mem_used
, mem_cache
, swap_total
, swap_used
);
169 /* Returns the time at which the system booted, as the number of milliseconds
170 * since the epoch, or 0 if the time of boot cannot be determined. */
174 static long long int cache_expiration
= LLONG_MIN
;
175 static long long int boot_time
;
177 ovs_assert(LINUX_DATAPATH
);
179 if (time_msec() >= cache_expiration
) {
180 static const char stat_file
[] = "/proc/stat";
184 cache_expiration
= time_msec() + 5 * 1000;
186 stream
= fopen(stat_file
, "r");
188 VLOG_ERR_ONCE("%s: open failed (%s)",
189 stat_file
, ovs_strerror(errno
));
193 while (fgets(line
, sizeof line
, stream
)) {
195 if (ovs_scan(line
, "btime %lld", &btime
)) {
196 boot_time
= btime
* 1000;
200 VLOG_ERR_ONCE("%s: btime not found", stat_file
);
207 static unsigned long long int
208 ticks_to_ms(unsigned long long int ticks
)
210 ovs_assert(LINUX_DATAPATH
);
216 #if USER_HZ == 100 /* Common case. */
217 return ticks
* (1000 / USER_HZ
);
218 #else /* Alpha and some other architectures. */
219 double factor
= 1000.0 / USER_HZ
;
220 return ticks
* factor
+ 0.5;
224 struct raw_process_info
{
225 unsigned long int vsz
; /* Virtual size, in kB. */
226 unsigned long int rss
; /* Resident set size, in kB. */
227 long long int uptime
; /* ms since started. */
228 long long int cputime
; /* ms of CPU used during 'uptime'. */
229 pid_t ppid
; /* Parent. */
230 char name
[18]; /* Name (surrounded by parentheses). */
234 get_raw_process_info(pid_t pid
, struct raw_process_info
*raw
)
236 unsigned long long int vsize
, rss
, start_time
, utime
, stime
;
237 long long int start_msec
;
243 ovs_assert(LINUX_DATAPATH
);
245 sprintf(file_name
, "/proc/%lu/stat", (unsigned long int) pid
);
246 stream
= fopen(file_name
, "r");
248 VLOG_ERR_ONCE("%s: open failed (%s)",
249 file_name
, ovs_strerror(errno
));
254 "%*d " /* (1. pid) */
255 "%17s " /* 2. process name */
256 "%*c " /* (3. state) */
258 "%*d " /* (5. pgid) */
259 "%*d " /* (6. sid) */
260 "%*d " /* (7. tty_nr) */
261 "%*d " /* (8. tty_pgrp) */
262 "%*u " /* (9. flags) */
263 "%*u " /* (10. min_flt) */
264 "%*u " /* (11. cmin_flt) */
265 "%*u " /* (12. maj_flt) */
266 "%*u " /* (13. cmaj_flt) */
267 "%llu " /* 14. utime */
268 "%llu " /* 15. stime */
269 "%*d " /* (16. cutime) */
270 "%*d " /* (17. cstime) */
271 "%*d " /* (18. priority) */
272 "%*d " /* (19. nice) */
273 "%*d " /* (20. num_threads) */
274 "%*d " /* (21. always 0) */
275 "%llu " /* 22. start_time */
276 "%llu " /* 23. vsize */
277 "%llu " /* 24. rss */
279 /* These are here for documentation but #if'd out to save
280 * actually parsing them from the stream for no benefit. */
281 "%*lu " /* (25. rsslim) */
282 "%*lu " /* (26. start_code) */
283 "%*lu " /* (27. end_code) */
284 "%*lu " /* (28. start_stack) */
285 "%*lu " /* (29. esp) */
286 "%*lu " /* (30. eip) */
287 "%*lu " /* (31. pending signals) */
288 "%*lu " /* (32. blocked signals) */
289 "%*lu " /* (33. ignored signals) */
290 "%*lu " /* (34. caught signals) */
291 "%*lu " /* (35. whcan) */
292 "%*lu " /* (36. always 0) */
293 "%*lu " /* (37. always 0) */
294 "%*d " /* (38. exit_signal) */
295 "%*d " /* (39. task_cpu) */
296 "%*u " /* (40. rt_priority) */
297 "%*u " /* (41. policy) */
298 "%*llu " /* (42. blkio_ticks) */
299 "%*lu " /* (43. gtime) */
300 "%*ld" /* (44. cgtime) */
302 , raw
->name
, &ppid
, &utime
, &stime
, &start_time
, &vsize
, &rss
);
305 VLOG_ERR_ONCE("%s: fscanf failed", file_name
);
309 start_msec
= get_boot_time() + ticks_to_ms(start_time
);
311 raw
->vsz
= vsize
/ 1024;
312 raw
->rss
= rss
* (getpagesize() / 1024);
313 raw
->uptime
= time_wall_msec() - start_msec
;
314 raw
->cputime
= ticks_to_ms(utime
+ stime
);
321 count_crashes(pid_t pid
)
329 ovs_assert(LINUX_DATAPATH
);
331 sprintf(file_name
, "/proc/%lu/cmdline", (unsigned long int) pid
);
332 stream
= fopen(file_name
, "r");
334 VLOG_WARN_ONCE("%s: open failed (%s)", file_name
, ovs_strerror(errno
));
338 if (!fgets(line
, sizeof line
, stream
)) {
339 VLOG_WARN_ONCE("%s: read failed (%s)", file_name
,
340 feof(stream
) ? "end of file" : ovs_strerror(errno
));
344 paren
= strchr(line
, '(');
347 if (ovs_scan(paren
+ 1, "%d", &x
)) {
358 struct process_info
{
359 unsigned long int vsz
; /* Virtual size, in kB. */
360 unsigned long int rss
; /* Resident set size, in kB. */
361 long long int booted
; /* ms since monitor started. */
362 int crashes
; /* # of crashes (usually 0). */
363 long long int uptime
; /* ms since last (re)started by monitor. */
364 long long int cputime
; /* ms of CPU used during 'uptime'. */
368 get_process_info(pid_t pid
, struct process_info
*pinfo
)
370 struct raw_process_info child
;
372 ovs_assert(LINUX_DATAPATH
);
373 if (!get_raw_process_info(pid
, &child
)) {
377 pinfo
->vsz
= child
.vsz
;
378 pinfo
->rss
= child
.rss
;
379 pinfo
->booted
= child
.uptime
;
381 pinfo
->uptime
= child
.uptime
;
382 pinfo
->cputime
= child
.cputime
;
385 struct raw_process_info parent
;
387 get_raw_process_info(child
.ppid
, &parent
);
388 if (!strcmp(child
.name
, parent
.name
)) {
389 pinfo
->booted
= parent
.uptime
;
390 pinfo
->crashes
= count_crashes(child
.ppid
);
398 get_process_stats(struct smap
*stats
)
403 dir
= opendir(ovs_rundir());
405 VLOG_ERR_ONCE("%s: open failed (%s)",
406 ovs_rundir(), ovs_strerror(errno
));
410 while ((de
= readdir(dir
)) != NULL
) {
411 struct process_info pinfo
;
417 #ifdef _DIRENT_HAVE_D_TYPE
418 if (de
->d_type
!= DT_UNKNOWN
&& de
->d_type
!= DT_REG
) {
423 extension
= strrchr(de
->d_name
, '.');
424 if (!extension
|| strcmp(extension
, ".pid")) {
428 file_name
= xasprintf("%s/%s", ovs_rundir(), de
->d_name
);
429 pid
= read_pidfile(file_name
);
435 key
= xasprintf("process_%.*s",
436 (int) (extension
- de
->d_name
), de
->d_name
);
437 if (!smap_get(stats
, key
)) {
438 if (LINUX_DATAPATH
&& get_process_info(pid
, &pinfo
)) {
439 smap_add_format(stats
, key
, "%lu,%lu,%lld,%d,%lld,%lld",
440 pinfo
.vsz
, pinfo
.rss
, pinfo
.cputime
,
441 pinfo
.crashes
, pinfo
.booted
, pinfo
.uptime
);
443 smap_add(stats
, key
, "");
453 get_filesys_stats(struct smap
*stats OVS_UNUSED
)
455 #if HAVE_GETMNTENT_R && HAVE_STATVFS
456 static const char file_name
[] = "/etc/mtab";
457 struct mntent mntent
;
463 stream
= setmntent(file_name
, "r");
465 VLOG_ERR_ONCE("%s: open failed (%s)", file_name
, ovs_strerror(errno
));
470 while ((me
= getmntent_r(stream
, &mntent
, buf
, sizeof buf
)) != NULL
) {
471 unsigned long long int total
, free
;
475 /* Skip non-local and read-only filesystems. */
476 if (strncmp(me
->mnt_fsname
, "/dev", 4)
477 || !strstr(me
->mnt_opts
, "rw")) {
481 /* Given the mount point we can stat the file system. */
482 if (statvfs(me
->mnt_dir
, &vfs
) && vfs
.f_flag
& ST_RDONLY
) {
487 /* Now format the data. */
489 ds_put_char(&s
, ' ');
491 for (p
= me
->mnt_dir
; *p
!= '\0'; p
++) {
492 ds_put_char(&s
, *p
== ' ' || *p
== ',' ? '_' : *p
);
494 total
= (unsigned long long int) vfs
.f_frsize
* vfs
.f_blocks
/ 1024;
495 free
= (unsigned long long int) vfs
.f_frsize
* vfs
.f_bfree
/ 1024;
496 ds_put_format(&s
, ",%llu,%llu", total
, total
- free
);
501 smap_add(stats
, "file_systems", ds_cstr(&s
));
504 #endif /* HAVE_GETMNTENT_R && HAVE_STATVFS */
507 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
509 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
510 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
511 static struct latch latch
OVS_GUARDED_BY(mutex
);
513 static bool started
OVS_GUARDED_BY(mutex
);
514 static struct smap
*system_stats
OVS_GUARDED_BY(mutex
);
516 static void *system_stats_thread_func(void *);
517 static void discard_stats(void);
519 /* Enables or disables system stats collection, according to 'enable'. */
521 system_stats_enable(bool enable
)
523 if (enabled
!= enable
) {
524 ovs_mutex_lock(&mutex
);
527 xpthread_create(NULL
, NULL
, system_stats_thread_func
, NULL
);
532 xpthread_cond_signal(&cond
);
535 ovs_mutex_unlock(&mutex
);
539 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
542 * When a new snapshot is available (which only occurs if system stats are
543 * enabled), returns it as an smap owned by the caller. The caller must use
544 * both smap_destroy() and free() to completely free the returned data.
546 * When no new snapshot is available, returns NULL. */
548 system_stats_run(void)
550 struct smap
*stats
= NULL
;
552 ovs_mutex_lock(&mutex
);
557 stats
= system_stats
;
563 ovs_mutex_unlock(&mutex
);
568 /* Causes poll_block() to wake up when system_stats_run() needs to be
571 system_stats_wait(void)
579 discard_stats(void) OVS_REQUIRES(mutex
)
582 smap_destroy(system_stats
);
588 static void * NO_RETURN
589 system_stats_thread_func(void *arg OVS_UNUSED
)
591 pthread_detach(pthread_self());
594 long long int next_refresh
;
597 ovs_mutex_lock(&mutex
);
599 ovs_mutex_cond_wait(&cond
, &mutex
);
601 ovs_mutex_unlock(&mutex
);
603 stats
= xmalloc(sizeof *stats
);
605 get_cpu_cores(stats
);
606 get_load_average(stats
);
607 get_memory_stats(stats
);
608 get_process_stats(stats
);
609 get_filesys_stats(stats
);
611 ovs_mutex_lock(&mutex
);
613 system_stats
= stats
;
615 ovs_mutex_unlock(&mutex
);
617 next_refresh
= time_msec() + SYSTEM_STATS_INTERVAL
;
619 poll_timer_wait_until(next_refresh
);
621 } while (time_msec() < next_refresh
);