]>
git.proxmox.com Git - mirror_ovs.git/blob - vswitchd/system-stats.c
1 /* Copyright (c) 2010, 2012 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"
30 #if HAVE_SYS_STATVFS_H
31 #include <sys/statvfs.h>
37 #include "dynamic-string.h"
43 VLOG_DEFINE_THIS_MODULE(system_stats
);
45 /* #ifdefs make it a pain to maintain code: you have to try to build both ways.
46 * Thus, this file tries to compile as much of the code as possible regardless
47 * of the target, by writing "if (LINUX)" instead of "#ifdef __linux__" where
48 * this is possible. */
50 #include <asm/param.h>
57 get_cpu_cores(struct smap
*stats
)
59 long int n_cores
= sysconf(_SC_NPROCESSORS_ONLN
);
61 smap_add_format(stats
, "cpu", "%ld", n_cores
);
66 get_load_average(struct smap
*stats OVS_UNUSED
)
71 if (getloadavg(loadavg
, 3) == 3) {
72 smap_add_format(stats
, "load_average", "%.2f,%.2f,%.2f",
73 loadavg
[0], loadavg
[1], loadavg
[2]);
81 static unsigned int cached
;
84 long int value
= sysconf(_SC_PAGESIZE
);
94 get_memory_stats(struct smap
*stats
)
97 unsigned int pagesize
= get_page_size();
98 long int phys_pages
= sysconf(_SC_PHYS_PAGES
);
99 #ifdef _SC_AVPHYS_PAGES
100 long int avphys_pages
= sysconf(_SC_AVPHYS_PAGES
);
102 long int avphys_pages
= 0;
104 int mem_total
, mem_used
;
106 if (pagesize
<= 0 || phys_pages
<= 0 || avphys_pages
<= 0) {
110 mem_total
= phys_pages
* (pagesize
/ 1024);
111 mem_used
= (phys_pages
- avphys_pages
) * (pagesize
/ 1024);
112 smap_add_format(stats
, "memory", "%d,%d", mem_total
, mem_used
);
114 static const char file_name
[] = "/proc/meminfo";
115 int mem_used
, mem_cache
, swap_used
;
126 stream
= fopen(file_name
, "r");
128 VLOG_WARN_ONCE("%s: open failed (%s)", file_name
, strerror(errno
));
133 shash_add(&dict
, "MemTotal", &mem_total
);
134 shash_add(&dict
, "MemFree", &mem_free
);
135 shash_add(&dict
, "Buffers", &buffers
);
136 shash_add(&dict
, "Cached", &cached
);
137 shash_add(&dict
, "SwapTotal", &swap_total
);
138 shash_add(&dict
, "SwapFree", &swap_free
);
139 while (fgets(line
, sizeof line
, stream
)) {
143 if (sscanf(line
, "%15[^:]: %u", key
, &value
) == 2) {
144 int *valuep
= shash_find_data(&dict
, key
);
151 shash_destroy(&dict
);
153 mem_used
= mem_total
- mem_free
;
154 mem_cache
= buffers
+ cached
;
155 swap_used
= swap_total
- swap_free
;
156 smap_add_format(stats
, "memory", "%d,%d,%d,%d,%d",
157 mem_total
, mem_used
, mem_cache
, 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 smap
*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 (!smap_get(stats
, key
)) {
427 if (LINUX
&& get_process_info(pid
, &pinfo
)) {
428 smap_add_format(stats
, key
, "%lu,%lu,%lld,%d,%lld,%lld",
429 pinfo
.vsz
, pinfo
.rss
, pinfo
.cputime
,
430 pinfo
.crashes
, pinfo
.booted
, pinfo
.uptime
);
432 smap_add(stats
, key
, "");
442 get_filesys_stats(struct smap
*stats OVS_UNUSED
)
444 #if HAVE_SETMNTENT && HAVE_STATVFS
445 static const char file_name
[] = "/etc/mtab";
450 stream
= setmntent(file_name
, "r");
452 VLOG_ERR_ONCE("%s: open failed (%s)", file_name
, strerror(errno
));
457 while ((me
= getmntent(stream
)) != NULL
) {
458 unsigned long long int total
, free
;
462 /* Skip non-local and read-only filesystems. */
463 if (strncmp(me
->mnt_fsname
, "/dev", 4)
464 || !strstr(me
->mnt_opts
, "rw")) {
468 /* Given the mount point we can stat the file system. */
469 if (statvfs(me
->mnt_dir
, &vfs
) && vfs
.f_flag
& ST_RDONLY
) {
474 /* Now format the data. */
476 ds_put_char(&s
, ' ');
478 for (p
= me
->mnt_dir
; *p
!= '\0'; p
++) {
479 ds_put_char(&s
, *p
== ' ' || *p
== ',' ? '_' : *p
);
481 total
= (unsigned long long int) vfs
.f_frsize
* vfs
.f_blocks
/ 1024;
482 free
= (unsigned long long int) vfs
.f_frsize
* vfs
.f_bfree
/ 1024;
483 ds_put_format(&s
, ",%llu,%llu", total
, total
- free
);
488 smap_add(stats
, "file_systems", ds_cstr(&s
));
491 #endif /* HAVE_SETMNTENT && HAVE_STATVFS */
495 get_system_stats(struct smap
*stats
)
497 get_cpu_cores(stats
);
498 get_load_average(stats
);
499 get_memory_stats(stats
);
500 get_process_stats(stats
);
501 get_filesys_stats(stats
);