Merge branch 'mainstream'
[sliver-openvswitch.git] / vswitchd / system-stats.c
1 /* Copyright (c) 2010, 2012, 2013, 2014 Nicira, Inc.
2  *
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <config.h>
17
18 #include "system-stats.h"
19
20 #include <ctype.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #if HAVE_MNTENT_H
24 #include <mntent.h>
25 #endif
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #if HAVE_SYS_STATVFS_H
30 #include <sys/statvfs.h>
31 #endif
32 #include <unistd.h>
33
34 #include "daemon.h"
35 #include "dirs.h"
36 #include "dynamic-string.h"
37 #include "json.h"
38 #include "latch.h"
39 #include "ofpbuf.h"
40 #include "ovs-thread.h"
41 #include "poll-loop.h"
42 #include "shash.h"
43 #include "smap.h"
44 #include "timeval.h"
45 #include "vlog.h"
46
47 VLOG_DEFINE_THIS_MODULE(system_stats);
48
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. */
53 #ifdef __linux__
54 #define LINUX 1
55 #include <asm/param.h>
56 #else
57 #define LINUX 0
58 #endif
59
60 static void
61 get_cpu_cores(struct smap *stats)
62 {
63     long int n_cores = count_cpu_cores();
64     if (n_cores > 0) {
65         smap_add_format(stats, "cpu", "%ld", n_cores);
66     }
67 }
68
69 static void
70 get_load_average(struct smap *stats OVS_UNUSED)
71 {
72 #if HAVE_GETLOADAVG
73     double loadavg[3];
74
75     if (getloadavg(loadavg, 3) == 3) {
76         smap_add_format(stats, "load_average", "%.2f,%.2f,%.2f",
77                         loadavg[0], loadavg[1], loadavg[2]);
78     }
79 #endif
80 }
81
82 static unsigned int
83 get_page_size(void)
84 {
85     static unsigned int cached;
86
87     if (!cached) {
88         long int value = sysconf(_SC_PAGESIZE);
89         if (value >= 0) {
90             cached = value;
91         }
92     }
93
94     return cached;
95 }
96
97 static void
98 get_memory_stats(struct smap *stats)
99 {
100     if (!LINUX) {
101         unsigned int pagesize = get_page_size();
102 #ifdef _SC_PHYS_PAGES
103         long int phys_pages = sysconf(_SC_PHYS_PAGES);
104 #else
105         long int phys_pages = 0;
106 #endif
107 #ifdef _SC_AVPHYS_PAGES
108         long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
109 #else
110         long int avphys_pages = 0;
111 #endif
112         int mem_total, mem_used;
113
114         if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
115             return;
116         }
117
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);
121     } else {
122         static const char file_name[] = "/proc/meminfo";
123         int mem_used, mem_cache, swap_used;
124         int mem_free = 0;
125         int buffers = 0;
126         int cached = 0;
127         int swap_free = 0;
128         int mem_total = 0;
129         int swap_total = 0;
130         struct shash dict;
131         char line[128];
132         FILE *stream;
133
134         stream = fopen(file_name, "r");
135         if (!stream) {
136             VLOG_WARN_ONCE("%s: open failed (%s)",
137                            file_name, ovs_strerror(errno));
138             return;
139         }
140
141         shash_init(&dict);
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)) {
149             char key[16];
150             int value;
151
152             if (ovs_scan(line, "%15[^:]: %u", key, &value)) {
153                 int *valuep = shash_find_data(&dict, key);
154                 if (valuep) {
155                     *valuep = value;
156                 }
157             }
158         }
159         fclose(stream);
160         shash_destroy(&dict);
161
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);
167     }
168 }
169
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. */
172 static long long int
173 get_boot_time(void)
174 {
175     static long long int cache_expiration = LLONG_MIN;
176     static long long int boot_time;
177
178     ovs_assert(LINUX);
179
180     if (time_msec() >= cache_expiration) {
181         static const char stat_file[] = "/proc/stat";
182         char line[128];
183         FILE *stream;
184
185         cache_expiration = time_msec() + 5 * 1000;
186
187         stream = fopen(stat_file, "r");
188         if (!stream) {
189             VLOG_ERR_ONCE("%s: open failed (%s)",
190                           stat_file, ovs_strerror(errno));
191             return boot_time;
192         }
193
194         while (fgets(line, sizeof line, stream)) {
195             long long int btime;
196             if (ovs_scan(line, "btime %lld", &btime)) {
197                 boot_time = btime * 1000;
198                 goto done;
199             }
200         }
201         VLOG_ERR_ONCE("%s: btime not found", stat_file);
202     done:
203         fclose(stream);
204     }
205     return boot_time;
206 }
207
208 static unsigned long long int
209 ticks_to_ms(unsigned long long int ticks)
210 {
211     ovs_assert(LINUX);
212
213 #ifndef USER_HZ
214 #define USER_HZ 100
215 #endif
216
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;
222 #endif
223 }
224
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). */
232 };
233
234 static bool
235 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
236 {
237     unsigned long long int vsize, rss, start_time, utime, stime;
238     long long int start_msec;
239     unsigned long ppid;
240     char file_name[128];
241     FILE *stream;
242     int n;
243
244     ovs_assert(LINUX);
245
246     sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
247     stream = fopen(file_name, "r");
248     if (!stream) {
249         VLOG_ERR_ONCE("%s: open failed (%s)",
250                       file_name, ovs_strerror(errno));
251         return false;
252     }
253
254     n = fscanf(stream,
255                "%*d "           /* (1. pid) */
256                "%17s "          /* 2. process name */
257                "%*c "           /* (3. state) */
258                "%lu "           /* 4. ppid */
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 */
279 #if 0
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) */
302 #endif
303                , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
304     fclose(stream);
305     if (n != 7) {
306         VLOG_ERR_ONCE("%s: fscanf failed", file_name);
307         return false;
308     }
309
310     start_msec = get_boot_time() + ticks_to_ms(start_time);
311
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);
316     raw->ppid = ppid;
317
318     return true;
319 }
320
321 static int
322 count_crashes(pid_t pid)
323 {
324     char file_name[128];
325     const char *paren;
326     char line[128];
327     int crashes = 0;
328     FILE *stream;
329
330     ovs_assert(LINUX);
331
332     sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
333     stream = fopen(file_name, "r");
334     if (!stream) {
335         VLOG_WARN_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
336         goto exit;
337     }
338
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));
342         goto exit_close;
343     }
344
345     paren = strchr(line, '(');
346     if (paren) {
347         int x;
348         if (ovs_scan(paren + 1, "%d", &x)) {
349             crashes = x;
350         }
351     }
352
353 exit_close:
354     fclose(stream);
355 exit:
356     return crashes;
357 }
358
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'. */
366 };
367
368 static bool
369 get_process_info(pid_t pid, struct process_info *pinfo)
370 {
371     struct raw_process_info child;
372
373     ovs_assert(LINUX);
374     if (!get_raw_process_info(pid, &child)) {
375         return false;
376     }
377
378     pinfo->vsz = child.vsz;
379     pinfo->rss = child.rss;
380     pinfo->booted = child.uptime;
381     pinfo->crashes = 0;
382     pinfo->uptime = child.uptime;
383     pinfo->cputime = child.cputime;
384
385     if (child.ppid) {
386         struct raw_process_info parent;
387
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);
392         }
393     }
394
395     return true;
396 }
397
398 static void
399 get_process_stats(struct smap *stats)
400 {
401     struct dirent *de;
402     DIR *dir;
403
404     dir = opendir(ovs_rundir());
405     if (!dir) {
406         VLOG_ERR_ONCE("%s: open failed (%s)",
407                       ovs_rundir(), ovs_strerror(errno));
408         return;
409     }
410
411     while ((de = readdir(dir)) != NULL) {
412         struct process_info pinfo;
413         char *file_name;
414         char *extension;
415         char *key;
416         pid_t pid;
417
418 #ifdef _DIRENT_HAVE_D_TYPE
419         if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
420             continue;
421         }
422 #endif
423
424         extension = strrchr(de->d_name, '.');
425         if (!extension || strcmp(extension, ".pid")) {
426             continue;
427         }
428
429         file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
430         pid = read_pidfile(file_name);
431         free(file_name);
432         if (pid < 0) {
433             continue;
434         }
435
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);
443             } else {
444                 smap_add(stats, key, "");
445             }
446         }
447         free(key);
448     }
449
450     closedir(dir);
451 }
452
453 static void
454 get_filesys_stats(struct smap *stats OVS_UNUSED)
455 {
456 #if HAVE_GETMNTENT_R && HAVE_STATVFS
457     static const char file_name[] = "/etc/mtab";
458     struct mntent mntent;
459     struct mntent *me;
460     char buf[4096];
461     FILE *stream;
462     struct ds s;
463
464     stream = setmntent(file_name, "r");
465     if (!stream) {
466         VLOG_ERR_ONCE("%s: open failed (%s)", file_name, ovs_strerror(errno));
467         return;
468     }
469
470     ds_init(&s);
471     while ((me = getmntent_r(stream, &mntent, buf, sizeof buf)) != NULL) {
472         unsigned long long int total, free;
473         struct statvfs vfs;
474         char *p;
475
476         /* Skip non-local and read-only filesystems. */
477         if (strncmp(me->mnt_fsname, "/dev", 4)
478             || !strstr(me->mnt_opts, "rw")) {
479             continue;
480         }
481
482         /* Given the mount point we can stat the file system. */
483         if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
484             /* That's odd... */
485             continue;
486         }
487
488         /* Now format the data. */
489         if (s.length) {
490             ds_put_char(&s, ' ');
491         }
492         for (p = me->mnt_dir; *p != '\0'; p++) {
493             ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
494         }
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);
498     }
499     endmntent(stream);
500
501     if (s.length) {
502         smap_add(stats, "file_systems", ds_cstr(&s));
503     }
504     ds_destroy(&s);
505 #endif  /* HAVE_GETMNTENT_R && HAVE_STATVFS */
506 }
507 \f
508 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
509
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);
513 static bool enabled;
514 static bool started OVS_GUARDED_BY(mutex);
515 static struct smap *system_stats OVS_GUARDED_BY(mutex);
516
517 static void *system_stats_thread_func(void *);
518 static void discard_stats(void);
519
520 /* Enables or disables system stats collection, according to 'enable'. */
521 void
522 system_stats_enable(bool enable)
523 {
524     if (enabled != enable) {
525         ovs_mutex_lock(&mutex);
526         if (enable) {
527             if (!started) {
528                 xpthread_create(NULL, NULL, system_stats_thread_func, NULL);
529                 latch_init(&latch);
530                 started = true;
531             }
532             discard_stats();
533             xpthread_cond_signal(&cond);
534         }
535         enabled = enable;
536         ovs_mutex_unlock(&mutex);
537     }
538 }
539
540 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
541  * milliseconds.
542  *
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.
546  *
547  * When no new snapshot is available, returns NULL. */
548 struct smap *
549 system_stats_run(void)
550 {
551     struct smap *stats = NULL;
552
553     ovs_mutex_lock(&mutex);
554     if (system_stats) {
555         latch_poll(&latch);
556
557         if (enabled) {
558             stats = system_stats;
559             system_stats = NULL;
560         } else {
561             discard_stats();
562         }
563     }
564     ovs_mutex_unlock(&mutex);
565
566     return stats;
567 }
568
569 /* Causes poll_block() to wake up when system_stats_run() needs to be
570  * called. */
571 void
572 system_stats_wait(void)
573 {
574     if (enabled) {
575         latch_wait(&latch);
576     }
577 }
578
579 static void
580 discard_stats(void) OVS_REQUIRES(mutex)
581 {
582     if (system_stats) {
583         smap_destroy(system_stats);
584         free(system_stats);
585         system_stats = NULL;
586     }
587 }
588
589 static void * NO_RETURN
590 system_stats_thread_func(void *arg OVS_UNUSED)
591 {
592     pthread_detach(pthread_self());
593
594     for (;;) {
595         long long int next_refresh;
596         struct smap *stats;
597
598         ovs_mutex_lock(&mutex);
599         while (!enabled) {
600             ovs_mutex_cond_wait(&cond, &mutex);
601         }
602         ovs_mutex_unlock(&mutex);
603
604         stats = xmalloc(sizeof *stats);
605         smap_init(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);
611
612         ovs_mutex_lock(&mutex);
613         discard_stats();
614         system_stats = stats;
615         latch_set(&latch);
616         ovs_mutex_unlock(&mutex);
617
618         next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
619         do {
620             poll_timer_wait_until(next_refresh);
621             poll_block();
622         } while (time_msec() < next_refresh);
623     }
624 }