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