90446f2e2d1f585451eb646942358e96d0b77458
[sliver-openvswitch.git] / vswitchd / system-stats.c
1 /* Copyright (c) 2010, 2012 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 <assert.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #if HAVE_MNTENT_H
25 #include <mntent.h>
26 #endif
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #if HAVE_SYS_STATVFS_H
31 #include <sys/statvfs.h>
32 #endif
33 #include <unistd.h>
34
35 #include "daemon.h"
36 #include "dirs.h"
37 #include "dynamic-string.h"
38 #include "json.h"
39 #include "ofpbuf.h"
40 #include "poll-loop.h"
41 #include "shash.h"
42 #include "smap.h"
43 #include "timeval.h"
44 #include "vlog.h"
45 #include "worker.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         long int phys_pages = sysconf(_SC_PHYS_PAGES);
102 #ifdef _SC_AVPHYS_PAGES
103         long int avphys_pages = sysconf(_SC_AVPHYS_PAGES);
104 #else
105         long int avphys_pages = 0;
106 #endif
107         int mem_total, mem_used;
108
109         if (pagesize <= 0 || phys_pages <= 0 || avphys_pages <= 0) {
110             return;
111         }
112
113         mem_total = phys_pages * (pagesize / 1024);
114         mem_used = (phys_pages - avphys_pages) * (pagesize / 1024);
115         smap_add_format(stats, "memory", "%d,%d", mem_total, mem_used);
116     } else {
117         static const char file_name[] = "/proc/meminfo";
118         int mem_used, mem_cache, swap_used;
119         int mem_free = 0;
120         int buffers = 0;
121         int cached = 0;
122         int swap_free = 0;
123         int mem_total = 0;
124         int swap_total = 0;
125         struct shash dict;
126         char line[128];
127         FILE *stream;
128
129         stream = fopen(file_name, "r");
130         if (!stream) {
131             VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
132             return;
133         }
134
135         shash_init(&dict);
136         shash_add(&dict, "MemTotal", &mem_total);
137         shash_add(&dict, "MemFree", &mem_free);
138         shash_add(&dict, "Buffers", &buffers);
139         shash_add(&dict, "Cached", &cached);
140         shash_add(&dict, "SwapTotal", &swap_total);
141         shash_add(&dict, "SwapFree", &swap_free);
142         while (fgets(line, sizeof line, stream)) {
143             char key[16];
144             int value;
145
146             if (sscanf(line, "%15[^:]: %u", key, &value) == 2) {
147                 int *valuep = shash_find_data(&dict, key);
148                 if (valuep) {
149                     *valuep = value;
150                 }
151             }
152         }
153         fclose(stream);
154         shash_destroy(&dict);
155
156         mem_used = mem_total - mem_free;
157         mem_cache = buffers + cached;
158         swap_used = swap_total - swap_free;
159         smap_add_format(stats, "memory", "%d,%d,%d,%d,%d",
160                         mem_total, mem_used, mem_cache, swap_total, swap_used);
161     }
162 }
163
164 /* Returns the time at which the system booted, as the number of milliseconds
165  * since the epoch, or 0 if the time of boot cannot be determined. */
166 static long long int
167 get_boot_time(void)
168 {
169     static long long int cache_expiration = LLONG_MIN;
170     static long long int boot_time;
171
172     assert(LINUX_DATAPATH);
173
174     if (time_msec() >= cache_expiration) {
175         static const char stat_file[] = "/proc/stat";
176         char line[128];
177         FILE *stream;
178
179         cache_expiration = time_msec() + 5 * 1000;
180
181         stream = fopen(stat_file, "r");
182         if (!stream) {
183             VLOG_ERR_ONCE("%s: open failed (%s)", stat_file, strerror(errno));
184             return boot_time;
185         }
186
187         while (fgets(line, sizeof line, stream)) {
188             long long int btime;
189             if (sscanf(line, "btime %lld", &btime) == 1) {
190                 boot_time = btime * 1000;
191                 goto done;
192             }
193         }
194         VLOG_ERR_ONCE("%s: btime not found", stat_file);
195     done:
196         fclose(stream);
197     }
198     return boot_time;
199 }
200
201 static unsigned long long int
202 ticks_to_ms(unsigned long long int ticks)
203 {
204     assert(LINUX_DATAPATH);
205
206 #ifndef USER_HZ
207 #define USER_HZ 100
208 #endif
209
210 #if USER_HZ == 100              /* Common case. */
211     return ticks * (1000 / USER_HZ);
212 #else  /* Alpha and some other architectures.  */
213     double factor = 1000.0 / USER_HZ;
214     return ticks * factor + 0.5;
215 #endif
216 }
217
218 struct raw_process_info {
219     unsigned long int vsz;      /* Virtual size, in kB. */
220     unsigned long int rss;      /* Resident set size, in kB. */
221     long long int uptime;       /* ms since started. */
222     long long int cputime;      /* ms of CPU used during 'uptime'. */
223     pid_t ppid;                 /* Parent. */
224     char name[18];              /* Name (surrounded by parentheses). */
225 };
226
227 static bool
228 get_raw_process_info(pid_t pid, struct raw_process_info *raw)
229 {
230     unsigned long long int vsize, rss, start_time, utime, stime;
231     long long int start_msec;
232     unsigned long ppid;
233     char file_name[128];
234     FILE *stream;
235     int n;
236
237     assert(LINUX_DATAPATH);
238
239     sprintf(file_name, "/proc/%lu/stat", (unsigned long int) pid);
240     stream = fopen(file_name, "r");
241     if (!stream) {
242         VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
243         return false;
244     }
245
246     n = fscanf(stream,
247                "%*d "           /* (1. pid) */
248                "%17s "          /* 2. process name */
249                "%*c "           /* (3. state) */
250                "%lu "           /* 4. ppid */
251                "%*d "           /* (5. pgid) */
252                "%*d "           /* (6. sid) */
253                "%*d "           /* (7. tty_nr) */
254                "%*d "           /* (8. tty_pgrp) */
255                "%*u "           /* (9. flags) */
256                "%*u "           /* (10. min_flt) */
257                "%*u "           /* (11. cmin_flt) */
258                "%*u "           /* (12. maj_flt) */
259                "%*u "           /* (13. cmaj_flt) */
260                "%llu "          /* 14. utime */
261                "%llu "          /* 15. stime */
262                "%*d "           /* (16. cutime) */
263                "%*d "           /* (17. cstime) */
264                "%*d "           /* (18. priority) */
265                "%*d "           /* (19. nice) */
266                "%*d "           /* (20. num_threads) */
267                "%*d "           /* (21. always 0) */
268                "%llu "          /* 22. start_time */
269                "%llu "          /* 23. vsize */
270                "%llu "          /* 24. rss */
271 #if 0
272                /* These are here for documentation but #if'd out to save
273                 * actually parsing them from the stream for no benefit. */
274                "%*lu "          /* (25. rsslim) */
275                "%*lu "          /* (26. start_code) */
276                "%*lu "          /* (27. end_code) */
277                "%*lu "          /* (28. start_stack) */
278                "%*lu "          /* (29. esp) */
279                "%*lu "          /* (30. eip) */
280                "%*lu "          /* (31. pending signals) */
281                "%*lu "          /* (32. blocked signals) */
282                "%*lu "          /* (33. ignored signals) */
283                "%*lu "          /* (34. caught signals) */
284                "%*lu "          /* (35. whcan) */
285                "%*lu "          /* (36. always 0) */
286                "%*lu "          /* (37. always 0) */
287                "%*d "           /* (38. exit_signal) */
288                "%*d "           /* (39. task_cpu) */
289                "%*u "           /* (40. rt_priority) */
290                "%*u "           /* (41. policy) */
291                "%*llu "         /* (42. blkio_ticks) */
292                "%*lu "          /* (43. gtime) */
293                "%*ld"           /* (44. cgtime) */
294 #endif
295                , raw->name, &ppid, &utime, &stime, &start_time, &vsize, &rss);
296     fclose(stream);
297     if (n != 7) {
298         VLOG_ERR_ONCE("%s: fscanf failed", file_name);
299         return false;
300     }
301
302     start_msec = get_boot_time() + ticks_to_ms(start_time);
303
304     raw->vsz = vsize / 1024;
305     raw->rss = rss * (getpagesize() / 1024);
306     raw->uptime = time_wall_msec() - start_msec;
307     raw->cputime = ticks_to_ms(utime + stime);
308     raw->ppid = ppid;
309
310     return true;
311 }
312
313 static int
314 count_crashes(pid_t pid)
315 {
316     char file_name[128];
317     const char *paren;
318     char line[128];
319     int crashes = 0;
320     FILE *stream;
321
322     assert(LINUX_DATAPATH);
323
324     sprintf(file_name, "/proc/%lu/cmdline", (unsigned long int) pid);
325     stream = fopen(file_name, "r");
326     if (!stream) {
327         VLOG_WARN_ONCE("%s: open failed (%s)", file_name, strerror(errno));
328         goto exit;
329     }
330
331     if (!fgets(line, sizeof line, stream)) {
332         VLOG_WARN_ONCE("%s: read failed (%s)", file_name,
333                        feof(stream) ? "end of file" : strerror(errno));
334         goto exit_close;
335     }
336
337     paren = strchr(line, '(');
338     if (paren) {
339         int x;
340         if (sscanf(paren + 1, "%d", &x) == 1) {
341             crashes = x;
342         }
343     }
344
345 exit_close:
346     fclose(stream);
347 exit:
348     return crashes;
349 }
350
351 struct process_info {
352     unsigned long int vsz;      /* Virtual size, in kB. */
353     unsigned long int rss;      /* Resident set size, in kB. */
354     long long int booted;       /* ms since monitor started. */
355     int crashes;                /* # of crashes (usually 0). */
356     long long int uptime;       /* ms since last (re)started by monitor. */
357     long long int cputime;      /* ms of CPU used during 'uptime'. */
358 };
359
360 static bool
361 get_process_info(pid_t pid, struct process_info *pinfo)
362 {
363     struct raw_process_info child;
364
365     assert(LINUX_DATAPATH);
366     if (!get_raw_process_info(pid, &child)) {
367         return false;
368     }
369
370     pinfo->vsz = child.vsz;
371     pinfo->rss = child.rss;
372     pinfo->booted = child.uptime;
373     pinfo->crashes = 0;
374     pinfo->uptime = child.uptime;
375     pinfo->cputime = child.cputime;
376
377     if (child.ppid) {
378         struct raw_process_info parent;
379
380         get_raw_process_info(child.ppid, &parent);
381         if (!strcmp(child.name, parent.name)) {
382             pinfo->booted = parent.uptime;
383             pinfo->crashes = count_crashes(child.ppid);
384         }
385     }
386
387     return true;
388 }
389
390 static void
391 get_process_stats(struct smap *stats)
392 {
393     struct dirent *de;
394     DIR *dir;
395
396     dir = opendir(ovs_rundir());
397     if (!dir) {
398         VLOG_ERR_ONCE("%s: open failed (%s)", ovs_rundir(), strerror(errno));
399         return;
400     }
401
402     while ((de = readdir(dir)) != NULL) {
403         struct process_info pinfo;
404         char *file_name;
405         char *extension;
406         char *key;
407         pid_t pid;
408
409 #ifdef _DIRENT_HAVE_D_TYPE
410         if (de->d_type != DT_UNKNOWN && de->d_type != DT_REG) {
411             continue;
412         }
413 #endif
414
415         extension = strrchr(de->d_name, '.');
416         if (!extension || strcmp(extension, ".pid")) {
417             continue;
418         }
419
420         file_name = xasprintf("%s/%s", ovs_rundir(), de->d_name);
421         pid = read_pidfile(file_name);
422         free(file_name);
423         if (pid < 0) {
424             continue;
425         }
426
427         key = xasprintf("process_%.*s",
428                         (int) (extension - de->d_name), de->d_name);
429         if (!smap_get(stats, key)) {
430             if (LINUX_DATAPATH && get_process_info(pid, &pinfo)) {
431                 smap_add_format(stats, key, "%lu,%lu,%lld,%d,%lld,%lld",
432                                 pinfo.vsz, pinfo.rss, pinfo.cputime,
433                                 pinfo.crashes, pinfo.booted, pinfo.uptime);
434             } else {
435                 smap_add(stats, key, "");
436             }
437         }
438         free(key);
439     }
440
441     closedir(dir);
442 }
443
444 static void
445 get_filesys_stats(struct smap *stats OVS_UNUSED)
446 {
447 #if HAVE_SETMNTENT && HAVE_STATVFS
448     static const char file_name[] = "/etc/mtab";
449     struct mntent *me;
450     FILE *stream;
451     struct ds s;
452
453     stream = setmntent(file_name, "r");
454     if (!stream) {
455         VLOG_ERR_ONCE("%s: open failed (%s)", file_name, strerror(errno));
456         return;
457     }
458
459     ds_init(&s);
460     while ((me = getmntent(stream)) != NULL) {
461         unsigned long long int total, free;
462         struct statvfs vfs;
463         char *p;
464
465         /* Skip non-local and read-only filesystems. */
466         if (strncmp(me->mnt_fsname, "/dev", 4)
467             || !strstr(me->mnt_opts, "rw")) {
468             continue;
469         }
470
471         /* Given the mount point we can stat the file system. */
472         if (statvfs(me->mnt_dir, &vfs) && vfs.f_flag & ST_RDONLY) {
473             /* That's odd... */
474             continue;
475         }
476
477         /* Now format the data. */
478         if (s.length) {
479             ds_put_char(&s, ' ');
480         }
481         for (p = me->mnt_dir; *p != '\0'; p++) {
482             ds_put_char(&s, *p == ' ' || *p == ',' ? '_' : *p);
483         }
484         total = (unsigned long long int) vfs.f_frsize * vfs.f_blocks / 1024;
485         free = (unsigned long long int) vfs.f_frsize * vfs.f_bfree / 1024;
486         ds_put_format(&s, ",%llu,%llu", total, total - free);
487     }
488     endmntent(stream);
489
490     if (s.length) {
491         smap_add(stats, "file_systems", ds_cstr(&s));
492     }
493     ds_destroy(&s);
494 #endif  /* HAVE_SETMNTENT && HAVE_STATVFS */
495 }
496 \f
497 #define SYSTEM_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
498
499 /* Whether the client wants us to report system stats. */
500 static bool enabled;
501
502 static enum {
503     S_DISABLED,                 /* Not enabled, nothing going on. */
504     S_WAITING,                  /* Sleeping for SYSTEM_STATS_INTERVAL ms. */
505     S_REQUEST_SENT,             /* Sent a request to worker. */
506     S_REPLY_RECEIVED            /* Received a reply from worker. */
507 } state;
508
509 /* In S_WAITING state: the next time to wake up.
510  * In other states: not meaningful. */
511 static long long int next_refresh;
512
513 /* In S_REPLY_RECEIVED: the stats that have just been received.
514  * In other states: not meaningful. */
515 static struct smap *received_stats;
516
517 static worker_request_func system_stats_request_cb;
518 static worker_reply_func system_stats_reply_cb;
519
520 /* Enables or disables system stats collection, according to 'new_enable'.
521  *
522  * Even if system stats are disabled, the caller should still periodically call
523  * system_stats_run(). */
524 void
525 system_stats_enable(bool new_enable)
526 {
527     if (new_enable != enabled) {
528         if (new_enable) {
529             if (state == S_DISABLED) {
530                 state = S_WAITING;
531                 next_refresh = time_msec();
532             }
533         } else {
534             if (state == S_WAITING) {
535                 state = S_DISABLED;
536             }
537         }
538         enabled = new_enable;
539     }
540 }
541
542 /* Tries to obtain a new snapshot of system stats every SYSTEM_STATS_INTERVAL
543  * milliseconds.
544  *
545  * When a new snapshot is available (which only occurs if system stats are
546  * enabled), returns it as an smap owned by the caller.  The caller must use
547  * both smap_destroy() and free() to complete free the returned data.
548  *
549  * When no new snapshot is available, returns NULL. */
550 struct smap *
551 system_stats_run(void)
552 {
553     switch (state) {
554     case S_DISABLED:
555         break;
556
557     case S_WAITING:
558         if (time_msec() >= next_refresh) {
559             worker_request(NULL, 0, NULL, 0, system_stats_request_cb,
560                            system_stats_reply_cb, NULL);
561             state = S_REQUEST_SENT;
562         }
563         break;
564
565     case S_REQUEST_SENT:
566         break;
567
568     case S_REPLY_RECEIVED:
569         if (enabled) {
570             state = S_WAITING;
571             next_refresh = time_msec() + SYSTEM_STATS_INTERVAL;
572             return received_stats;
573         } else {
574             smap_destroy(received_stats);
575             free(received_stats);
576             state = S_DISABLED;
577         }
578         break;
579     }
580
581     return NULL;
582 }
583
584 /* Causes poll_block() to wake up when system_stats_run() needs to be
585  * called. */
586 void
587 system_stats_wait(void)
588 {
589     switch (state) {
590     case S_DISABLED:
591         break;
592
593     case S_WAITING:
594         poll_timer_wait_until(next_refresh);
595         break;
596
597     case S_REQUEST_SENT:
598         /* Someone else should be calling worker_wait() to wake up when the
599          * reply arrives, otherwise there's a bug. */
600         break;
601
602     case S_REPLY_RECEIVED:
603         poll_immediate_wake();
604         break;
605     }
606 }
607
608 static void
609 system_stats_request_cb(struct ofpbuf *request OVS_UNUSED,
610                         const int fds[] OVS_UNUSED, size_t n_fds OVS_UNUSED)
611 {
612     struct smap stats;
613     struct json *json;
614     char *s;
615
616     smap_init(&stats);
617     get_cpu_cores(&stats);
618     get_load_average(&stats);
619     get_memory_stats(&stats);
620     get_process_stats(&stats);
621     get_filesys_stats(&stats);
622
623     json = smap_to_json(&stats);
624     s = json_to_string(json, 0);
625     worker_reply(s, strlen(s) + 1, NULL, 0);
626
627     free(s);
628     json_destroy(json);
629     smap_destroy(&stats);
630 }
631
632 static void
633 system_stats_reply_cb(struct ofpbuf *reply,
634                       const int fds[] OVS_UNUSED, size_t n_fds OVS_UNUSED,
635                       void *aux OVS_UNUSED)
636 {
637     struct json *json = json_from_string(reply->data);
638
639     received_stats = xmalloc(sizeof *received_stats);
640     smap_init(received_stats);
641     smap_from_json(received_stats, json);
642
643     assert(state == S_REQUEST_SENT);
644     state = S_REPLY_RECEIVED;
645
646     json_destroy(json);
647 }