timeval: Block SIGALRM when sleeping.
[cascardo/ovs.git] / lib / timeval.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "timeval.h"
19 #include <assert.h>
20 #include <errno.h>
21 #if HAVE_EXECINFO_H
22 #include <execinfo.h>
23 #endif
24 #include <poll.h>
25 #include <signal.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #include <unistd.h>
31 #include "coverage.h"
32 #include "dummy.h"
33 #include "dynamic-string.h"
34 #include "fatal-signal.h"
35 #include "signals.h"
36 #include "unixctl.h"
37 #include "util.h"
38 #include "vlog.h"
39
40 #ifndef HAVE_EXECINFO_H
41 #define HAVE_EXECINFO_H 0
42 #endif
43
44 VLOG_DEFINE_THIS_MODULE(timeval);
45
46 /* The clock to use for measuring time intervals.  This is CLOCK_MONOTONIC by
47  * preference, but on systems that don't have a monotonic clock we fall back
48  * to CLOCK_REALTIME. */
49 static clockid_t monotonic_clock;
50
51 /* Has a timer tick occurred? Only relevant if CACHE_TIME is true.
52  *
53  * We initialize these to true to force time_init() to get called on the first
54  * call to time_msec() or another function that queries the current time. */
55 static volatile sig_atomic_t wall_tick = true;
56 static volatile sig_atomic_t monotonic_tick = true;
57
58 /* The current time, as of the last refresh. */
59 static struct timespec wall_time;
60 static struct timespec monotonic_time;
61
62 /* The monotonic time at which the time module was initialized. */
63 static long long int boot_time;
64
65 /* features for use by unit tests. */
66 static struct timespec warp_offset; /* Offset added to monotonic_time. */
67 static bool time_stopped;           /* Disables real-time updates, if true. */
68
69 /* Time in milliseconds at which to die with SIGALRM (if not LLONG_MAX). */
70 static long long int deadline = LLONG_MAX;
71
72 struct trace {
73     void *backtrace[32]; /* Populated by backtrace(). */
74     size_t n_frames;     /* Number of frames in 'backtrace'. */
75 };
76
77 #define MAX_TRACES 50
78 static struct unixctl_conn *backtrace_conn = NULL;
79 static struct trace *traces = NULL;
80 static size_t n_traces = 0;
81
82 static void set_up_timer(void);
83 static void set_up_signal(int flags);
84 static void sigalrm_handler(int);
85 static void refresh_wall_if_ticked(void);
86 static void refresh_monotonic_if_ticked(void);
87 static void block_sigalrm(sigset_t *);
88 static void unblock_sigalrm(const sigset_t *);
89 static void log_poll_interval(long long int last_wakeup);
90 static struct rusage *get_recent_rusage(void);
91 static void refresh_rusage(void);
92 static void timespec_add(struct timespec *sum,
93                          const struct timespec *a, const struct timespec *b);
94 static void trace_run(void);
95 static unixctl_cb_func backtrace_cb;
96
97 /* Initializes the timetracking module, if not already initialized. */
98 static void
99 time_init(void)
100 {
101     static bool inited;
102
103     /* The best place to do this is probably a timeval_run() function.
104      * However, none exists and this function is usually so fast that doing it
105      * here seems fine for now. */
106     trace_run();
107
108     if (inited) {
109         return;
110     }
111     inited = true;
112
113     if (HAVE_EXECINFO_H && CACHE_TIME) {
114         unixctl_command_register("backtrace", "", 0, 0, backtrace_cb, NULL);
115     }
116
117     coverage_init();
118
119     if (!clock_gettime(CLOCK_MONOTONIC, &monotonic_time)) {
120         monotonic_clock = CLOCK_MONOTONIC;
121     } else {
122         monotonic_clock = CLOCK_REALTIME;
123         VLOG_DBG("monotonic timer not available");
124     }
125
126     set_up_signal(SA_RESTART);
127     set_up_timer();
128
129     boot_time = time_msec();
130 }
131
132 static void
133 set_up_signal(int flags)
134 {
135     struct sigaction sa;
136
137     memset(&sa, 0, sizeof sa);
138     sa.sa_handler = sigalrm_handler;
139     sigemptyset(&sa.sa_mask);
140     sa.sa_flags = flags;
141     xsigaction(SIGALRM, &sa, NULL);
142 }
143
144 /* Remove SA_RESTART from the flags for SIGALRM, so that any system call that
145  * is interrupted by the periodic timer interrupt will return EINTR instead of
146  * continuing after the signal handler returns.
147  *
148  * time_disable_restart() and time_enable_restart() may be usefully wrapped
149  * around function calls that might otherwise block forever unless interrupted
150  * by a signal, e.g.:
151  *
152  *   time_disable_restart();
153  *   fcntl(fd, F_SETLKW, &lock);
154  *   time_enable_restart();
155  */
156 void
157 time_disable_restart(void)
158 {
159     time_init();
160     set_up_signal(0);
161 }
162
163 /* Add SA_RESTART to the flags for SIGALRM, so that any system call that
164  * is interrupted by the periodic timer interrupt will continue after the
165  * signal handler returns instead of returning EINTR. */
166 void
167 time_enable_restart(void)
168 {
169     time_init();
170     set_up_signal(SA_RESTART);
171 }
172
173 static void
174 set_up_timer(void)
175 {
176     static timer_t timer_id;    /* "static" to avoid apparent memory leak. */
177     struct itimerspec itimer;
178
179     if (!CACHE_TIME) {
180         return;
181     }
182
183     if (timer_create(monotonic_clock, NULL, &timer_id)) {
184         VLOG_FATAL("timer_create failed (%s)", strerror(errno));
185     }
186
187     itimer.it_interval.tv_sec = 0;
188     itimer.it_interval.tv_nsec = TIME_UPDATE_INTERVAL * 1000 * 1000;
189     itimer.it_value = itimer.it_interval;
190
191     if (timer_settime(timer_id, 0, &itimer, NULL)) {
192         VLOG_FATAL("timer_settime failed (%s)", strerror(errno));
193     }
194 }
195
196 /* Set up the interval timer, to ensure that time advances even without calling
197  * time_refresh().
198  *
199  * A child created with fork() does not inherit the parent's interval timer, so
200  * this function needs to be called from the child after fork(). */
201 void
202 time_postfork(void)
203 {
204     time_init();
205     set_up_timer();
206 }
207
208 static void
209 refresh_wall(void)
210 {
211     time_init();
212     clock_gettime(CLOCK_REALTIME, &wall_time);
213     wall_tick = false;
214 }
215
216 static void
217 refresh_monotonic(void)
218 {
219     time_init();
220
221     if (!time_stopped) {
222         if (monotonic_clock == CLOCK_MONOTONIC) {
223             clock_gettime(monotonic_clock, &monotonic_time);
224         } else {
225             refresh_wall_if_ticked();
226             monotonic_time = wall_time;
227         }
228         timespec_add(&monotonic_time, &monotonic_time, &warp_offset);
229
230         monotonic_tick = false;
231     }
232 }
233
234 /* Forces a refresh of the current time from the kernel.  It is not usually
235  * necessary to call this function, since the time will be refreshed
236  * automatically at least every TIME_UPDATE_INTERVAL milliseconds.  If
237  * CACHE_TIME is false, we will always refresh the current time so this
238  * function has no effect. */
239 void
240 time_refresh(void)
241 {
242     wall_tick = monotonic_tick = true;
243 }
244
245 /* Returns a monotonic timer, in seconds. */
246 time_t
247 time_now(void)
248 {
249     refresh_monotonic_if_ticked();
250     return monotonic_time.tv_sec;
251 }
252
253 /* Returns the current time, in seconds. */
254 time_t
255 time_wall(void)
256 {
257     refresh_wall_if_ticked();
258     return wall_time.tv_sec;
259 }
260
261 /* Returns a monotonic timer, in ms (within TIME_UPDATE_INTERVAL ms). */
262 long long int
263 time_msec(void)
264 {
265     refresh_monotonic_if_ticked();
266     return timespec_to_msec(&monotonic_time);
267 }
268
269 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
270 long long int
271 time_wall_msec(void)
272 {
273     refresh_wall_if_ticked();
274     return timespec_to_msec(&wall_time);
275 }
276
277 /* Stores a monotonic timer, accurate within TIME_UPDATE_INTERVAL ms, into
278  * '*ts'. */
279 void
280 time_timespec(struct timespec *ts)
281 {
282     refresh_monotonic_if_ticked();
283     *ts = monotonic_time;
284 }
285
286 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
287  * '*ts'. */
288 void
289 time_wall_timespec(struct timespec *ts)
290 {
291     refresh_wall_if_ticked();
292     *ts = wall_time;
293 }
294
295 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
296  * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
297 void
298 time_alarm(unsigned int secs)
299 {
300     long long int now;
301     long long int msecs;
302
303     sigset_t oldsigs;
304
305     time_init();
306     time_refresh();
307
308     now = time_msec();
309     msecs = secs * 1000;
310
311     block_sigalrm(&oldsigs);
312     deadline = now < LLONG_MAX - msecs ? now + msecs : LLONG_MAX;
313     unblock_sigalrm(&oldsigs);
314 }
315
316 /* Like poll(), except:
317  *
318  *      - The timeout is specified as an absolute time, as defined by
319  *        time_msec(), instead of a duration.
320  *
321  *      - On error, returns a negative error code (instead of setting errno).
322  *
323  *      - If interrupted by a signal, retries automatically until the original
324  *        timeout is reached.  (Because of this property, this function will
325  *        never return -EINTR.)
326  *
327  *      - As a side effect, refreshes the current time (like time_refresh()).
328  *
329  * Stores the number of milliseconds elapsed during poll in '*elapsed'. */
330 int
331 time_poll(struct pollfd *pollfds, int n_pollfds, long long int timeout_when,
332           int *elapsed)
333 {
334     static long long int last_wakeup;
335     long long int start;
336     sigset_t oldsigs;
337     bool blocked;
338     int retval;
339
340     time_refresh();
341     log_poll_interval(last_wakeup);
342     coverage_clear();
343     start = time_msec();
344     blocked = false;
345
346     timeout_when = MIN(timeout_when, deadline);
347
348     for (;;) {
349         long long int now = time_msec();
350         int time_left;
351
352         if (now >= timeout_when) {
353             time_left = 0;
354         } else if ((unsigned long long int) timeout_when - now > INT_MAX) {
355             time_left = INT_MAX;
356         } else {
357             time_left = timeout_when - now;
358         }
359
360         retval = poll(pollfds, n_pollfds, time_left);
361         if (retval < 0) {
362             retval = -errno;
363         }
364
365         time_refresh();
366         if (deadline <= time_msec()) {
367             fatal_signal_handler(SIGALRM);
368             if (retval < 0) {
369                 retval = 0;
370             }
371             break;
372         }
373
374         if (retval != -EINTR) {
375             break;
376         }
377
378         if (!blocked && CACHE_TIME && !backtrace_conn) {
379             block_sigalrm(&oldsigs);
380             blocked = true;
381         }
382     }
383     if (blocked) {
384         unblock_sigalrm(&oldsigs);
385     }
386     last_wakeup = time_msec();
387     refresh_rusage();
388     *elapsed = last_wakeup - start;
389     return retval;
390 }
391
392 static void
393 sigalrm_handler(int sig_nr OVS_UNUSED)
394 {
395     wall_tick = true;
396     monotonic_tick = true;
397
398 #if HAVE_EXECINFO_H
399     if (backtrace_conn && n_traces < MAX_TRACES) {
400         struct trace *trace = &traces[n_traces++];
401         trace->n_frames = backtrace(trace->backtrace,
402                                     ARRAY_SIZE(trace->backtrace));
403     }
404 #endif
405 }
406
407 static void
408 refresh_wall_if_ticked(void)
409 {
410     if (!CACHE_TIME || wall_tick) {
411         refresh_wall();
412     }
413 }
414
415 static void
416 refresh_monotonic_if_ticked(void)
417 {
418     if (!CACHE_TIME || monotonic_tick) {
419         refresh_monotonic();
420     }
421 }
422
423 static void
424 block_sigalrm(sigset_t *oldsigs)
425 {
426     sigset_t sigalrm;
427     sigemptyset(&sigalrm);
428     sigaddset(&sigalrm, SIGALRM);
429     xsigprocmask(SIG_BLOCK, &sigalrm, oldsigs);
430 }
431
432 static void
433 unblock_sigalrm(const sigset_t *oldsigs)
434 {
435     xsigprocmask(SIG_SETMASK, oldsigs, NULL);
436 }
437
438 long long int
439 timespec_to_msec(const struct timespec *ts)
440 {
441     return (long long int) ts->tv_sec * 1000 + ts->tv_nsec / (1000 * 1000);
442 }
443
444 long long int
445 timeval_to_msec(const struct timeval *tv)
446 {
447     return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
448 }
449
450 /* Returns the monotonic time at which the "time" module was initialized, in
451  * milliseconds(). */
452 long long int
453 time_boot_msec(void)
454 {
455     time_init();
456     return boot_time;
457 }
458
459 void
460 xgettimeofday(struct timeval *tv)
461 {
462     if (gettimeofday(tv, NULL) == -1) {
463         VLOG_FATAL("gettimeofday failed (%s)", strerror(errno));
464     }
465 }
466
467 static long long int
468 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
469 {
470     return timeval_to_msec(a) - timeval_to_msec(b);
471 }
472
473 static void
474 timespec_add(struct timespec *sum,
475              const struct timespec *a,
476              const struct timespec *b)
477 {
478     struct timespec tmp;
479
480     tmp.tv_sec = a->tv_sec + b->tv_sec;
481     tmp.tv_nsec = a->tv_nsec + b->tv_nsec;
482     if (tmp.tv_nsec >= 1000 * 1000 * 1000) {
483         tmp.tv_nsec -= 1000 * 1000 * 1000;
484         tmp.tv_sec++;
485     }
486
487     *sum = tmp;
488 }
489
490 static void
491 log_poll_interval(long long int last_wakeup)
492 {
493     static unsigned int mean_interval; /* In 16ths of a millisecond. */
494     static unsigned int n_samples;
495
496     long long int now;
497     unsigned int interval;      /* In 16ths of a millisecond. */
498
499     /* Compute interval from last wakeup to now in 16ths of a millisecond,
500      * capped at 10 seconds (16000 in this unit). */
501     now = time_msec();
502     interval = MIN(10000, now - last_wakeup) << 4;
503
504     /* Warn if we took too much time between polls: at least 50 ms and at least
505      * 8X the mean interval. */
506     if (n_samples > 10 && interval > mean_interval * 8 && interval > 50 * 16) {
507         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 3);
508
509         if (!VLOG_DROP_WARN(&rl)) {
510             const struct rusage *last_rusage = get_recent_rusage();
511             struct rusage rusage;
512
513             getrusage(RUSAGE_SELF, &rusage);
514             VLOG_WARN("%lld ms poll interval (%lld ms user, %lld ms system) "
515                       "is over %u times the weighted mean interval %u ms "
516                       "(%u samples)",
517                       now - last_wakeup,
518                       timeval_diff_msec(&rusage.ru_utime,
519                                         &last_rusage->ru_utime),
520                       timeval_diff_msec(&rusage.ru_stime,
521                                         &last_rusage->ru_stime),
522                       interval / mean_interval,
523                       (mean_interval + 8) / 16, n_samples);
524             if (rusage.ru_minflt > last_rusage->ru_minflt
525                 || rusage.ru_majflt > last_rusage->ru_majflt) {
526                 VLOG_WARN("faults: %ld minor, %ld major",
527                           rusage.ru_minflt - last_rusage->ru_minflt,
528                           rusage.ru_majflt - last_rusage->ru_majflt);
529             }
530             if (rusage.ru_inblock > last_rusage->ru_inblock
531                 || rusage.ru_oublock > last_rusage->ru_oublock) {
532                 VLOG_WARN("disk: %ld reads, %ld writes",
533                           rusage.ru_inblock - last_rusage->ru_inblock,
534                           rusage.ru_oublock - last_rusage->ru_oublock);
535             }
536             if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
537                 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
538                 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
539                           rusage.ru_nvcsw - last_rusage->ru_nvcsw,
540                           rusage.ru_nivcsw - last_rusage->ru_nivcsw);
541             }
542         }
543         coverage_log();
544     }
545
546     /* Update exponentially weighted moving average.  With these parameters, a
547      * given value decays to 1% of its value in about 100 time steps.  */
548     if (n_samples++) {
549         mean_interval = (mean_interval * 122 + interval * 6 + 64) / 128;
550     } else {
551         mean_interval = interval;
552     }
553 }
554 \f
555 /* CPU usage tracking. */
556
557 struct cpu_usage {
558     long long int when;         /* Time that this sample was taken. */
559     unsigned long long int cpu; /* Total user+system CPU usage when sampled. */
560 };
561
562 static struct rusage recent_rusage;
563 static struct cpu_usage older = { LLONG_MIN, 0 };
564 static struct cpu_usage newer = { LLONG_MIN, 0 };
565 static int cpu_usage = -1;
566
567 static struct rusage *
568 get_recent_rusage(void)
569 {
570     return &recent_rusage;
571 }
572
573 static void
574 refresh_rusage(void)
575 {
576     long long int now;
577
578     now = time_msec();
579     getrusage(RUSAGE_SELF, &recent_rusage);
580
581     if (now >= newer.when + 3 * 1000) {
582         older = newer;
583         newer.when = now;
584         newer.cpu = (timeval_to_msec(&recent_rusage.ru_utime) +
585                      timeval_to_msec(&recent_rusage.ru_stime));
586
587         if (older.when != LLONG_MIN && newer.cpu > older.cpu) {
588             unsigned int dividend = newer.cpu - older.cpu;
589             unsigned int divisor = (newer.when - older.when) / 100;
590             cpu_usage = divisor > 0 ? dividend / divisor : -1;
591         } else {
592             cpu_usage = -1;
593         }
594     }
595 }
596
597 /* Returns an estimate of this process's CPU usage, as a percentage, over the
598  * past few seconds of wall-clock time.  Returns -1 if no estimate is available
599  * (which will happen if the process has not been running long enough to have
600  * an estimate, and can happen for other reasons as well). */
601 int
602 get_cpu_usage(void)
603 {
604     return cpu_usage;
605 }
606
607 static void
608 trace_run(void)
609 {
610 #if HAVE_EXECINFO_H
611     if (backtrace_conn && n_traces >= MAX_TRACES) {
612         struct unixctl_conn *reply_conn = backtrace_conn;
613         struct ds ds = DS_EMPTY_INITIALIZER;
614         sigset_t oldsigs;
615         size_t i;
616
617         block_sigalrm(&oldsigs);
618
619         for (i = 0; i < n_traces; i++) {
620             struct trace *trace = &traces[i];
621             char **frame_strs;
622             size_t j;
623
624             frame_strs = backtrace_symbols(trace->backtrace, trace->n_frames);
625
626             ds_put_format(&ds, "Backtrace %zu\n", i + 1);
627             for (j = 0; j < trace->n_frames; j++) {
628                 ds_put_format(&ds, "%s\n", frame_strs[j]);
629             }
630             ds_put_cstr(&ds, "\n");
631
632             free(frame_strs);
633         }
634
635         free(traces);
636         traces = NULL;
637         n_traces = 0;
638         backtrace_conn = NULL;
639
640         unblock_sigalrm(&oldsigs);
641
642         unixctl_command_reply(reply_conn, ds_cstr(&ds));
643         ds_destroy(&ds);
644     }
645 #endif
646 }
647 \f
648 /* Unixctl interface. */
649
650 /* "time/stop" stops the monotonic time returned by e.g. time_msec() from
651  * advancing, except due to later calls to "time/warp". */
652 static void
653 timeval_stop_cb(struct unixctl_conn *conn,
654                  int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
655                  void *aux OVS_UNUSED)
656 {
657     time_stopped = true;
658     unixctl_command_reply(conn, NULL);
659 }
660
661 /* "time/warp MSECS" advances the current monotonic time by the specified
662  * number of milliseconds.  Unless "time/stop" has also been executed, the
663  * monotonic clock continues to tick forward at the normal rate afterward.
664  *
665  * Does not affect wall clock readings. */
666 static void
667 timeval_warp_cb(struct unixctl_conn *conn,
668                 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
669 {
670     struct timespec ts;
671     int msecs;
672
673     msecs = atoi(argv[1]);
674     if (msecs <= 0) {
675         unixctl_command_reply_error(conn, "invalid MSECS");
676         return;
677     }
678
679     ts.tv_sec = msecs / 1000;
680     ts.tv_nsec = (msecs % 1000) * 1000 * 1000;
681     timespec_add(&warp_offset, &warp_offset, &ts);
682     timespec_add(&monotonic_time, &monotonic_time, &ts);
683     unixctl_command_reply(conn, "warped");
684 }
685
686 static void
687 backtrace_cb(struct unixctl_conn *conn,
688              int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
689              void *aux OVS_UNUSED)
690 {
691     sigset_t oldsigs;
692
693     assert(HAVE_EXECINFO_H && CACHE_TIME);
694
695     if (backtrace_conn) {
696         unixctl_command_reply_error(conn, "In Use");
697         return;
698     }
699     assert(!traces);
700
701     block_sigalrm(&oldsigs);
702     backtrace_conn = conn;
703     traces = xmalloc(MAX_TRACES * sizeof *traces);
704     n_traces = 0;
705     unblock_sigalrm(&oldsigs);
706 }
707
708 void
709 timeval_dummy_register(void)
710 {
711     unixctl_command_register("time/stop", "", 0, 0, timeval_stop_cb, NULL);
712     unixctl_command_register("time/warp", "MSECS", 1, 1,
713                              timeval_warp_cb, NULL);
714 }