2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <sys/resource.h>
28 #include "fatal-signal.h"
32 #define THIS_MODULE VLM_timeval
37 /* Has a timer tick occurred? */
38 static volatile sig_atomic_t tick;
40 /* The current time, as of the last refresh. */
41 static struct timeval now;
43 /* Time at which to die with SIGALRM (if not TIME_MIN). */
44 static time_t deadline = TIME_MIN;
46 static void sigalrm_handler(int);
47 static void refresh_if_ticked(void);
48 static time_t time_add(time_t, time_t);
49 static void block_sigalrm(sigset_t *);
50 static void unblock_sigalrm(const sigset_t *);
51 static void log_poll_interval(long long int last_wakeup,
52 const struct rusage *last_rusage);
53 static long long int timeval_to_msec(const struct timeval *);
55 /* Initializes the timetracking module. */
60 struct itimerval itimer;
67 gettimeofday(&now, NULL);
70 /* Set up signal handler. */
71 memset(&sa, 0, sizeof sa);
72 sa.sa_handler = sigalrm_handler;
73 sigemptyset(&sa.sa_mask);
74 sa.sa_flags = SA_RESTART;
75 if (sigaction(SIGALRM, &sa, NULL)) {
76 ovs_fatal(errno, "sigaction(SIGALRM) failed");
79 /* Set up periodic timer. */
80 itimer.it_interval.tv_sec = 0;
81 itimer.it_interval.tv_usec = TIME_UPDATE_INTERVAL * 1000;
82 itimer.it_value = itimer.it_interval;
83 if (setitimer(ITIMER_REAL, &itimer, NULL)) {
84 ovs_fatal(errno, "setitimer failed");
88 /* Forces a refresh of the current time from the kernel. It is not usually
89 * necessary to call this function, since the time will be refreshed
90 * automatically at least every TIME_UPDATE_INTERVAL milliseconds. */
94 gettimeofday(&now, NULL);
98 /* Returns the current time, in seconds. */
106 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
111 return timeval_to_msec(&now);
114 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
117 time_timeval(struct timeval *tv)
123 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
124 * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
126 time_alarm(unsigned int secs)
131 block_sigalrm(&oldsigs);
132 deadline = secs ? time_add(time_now(), secs) : TIME_MIN;
133 unblock_sigalrm(&oldsigs);
136 /* Like poll(), except:
138 * - On error, returns a negative error code (instead of setting errno).
140 * - If interrupted by a signal, retries automatically until the original
141 * 'timeout' expires. (Because of this property, this function will
142 * never return -EINTR.)
144 * - As a side effect, refreshes the current time (like time_refresh()).
147 time_poll(struct pollfd *pollfds, int n_pollfds, int timeout)
149 static long long int last_wakeup;
150 static struct rusage last_rusage;
157 log_poll_interval(last_wakeup, &last_rusage);
164 long long int elapsed = time_msec() - start;
165 time_left = timeout >= elapsed ? timeout - elapsed : 0;
170 retval = poll(pollfds, n_pollfds, time_left);
175 if (retval != -EINTR) {
179 if (!blocked && deadline == TIME_MIN) {
180 block_sigalrm(&oldsigs);
185 unblock_sigalrm(&oldsigs);
187 last_wakeup = time_msec();
188 getrusage(RUSAGE_SELF, &last_rusage);
192 /* Returns the sum of 'a' and 'b', with saturation on overflow or underflow. */
194 time_add(time_t a, time_t b)
197 ? (b > TIME_MAX - a ? TIME_MAX : a + b)
198 : (b < TIME_MIN - a ? TIME_MIN : a + b));
202 sigalrm_handler(int sig_nr)
205 if (deadline != TIME_MIN && time(0) > deadline) {
206 fatal_signal_handler(sig_nr);
211 refresh_if_ticked(void)
220 block_sigalrm(sigset_t *oldsigs)
223 sigemptyset(&sigalrm);
224 sigaddset(&sigalrm, SIGALRM);
225 if (sigprocmask(SIG_BLOCK, &sigalrm, oldsigs)) {
226 ovs_fatal(errno, "sigprocmask");
231 unblock_sigalrm(const sigset_t *oldsigs)
233 if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) {
234 ovs_fatal(errno, "sigprocmask");
239 timeval_to_msec(const struct timeval *tv)
241 return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
245 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
247 return timeval_to_msec(a) - timeval_to_msec(b);
251 log_poll_interval(long long int last_wakeup, const struct rusage *last_rusage)
253 static unsigned int mean_interval; /* In 16ths of a millisecond. */
254 static unsigned int n_samples;
257 unsigned int interval; /* In 16ths of a millisecond. */
259 /* Compute interval from last wakeup to now in 16ths of a millisecond,
260 * capped at 10 seconds (16000 in this unit). */
262 interval = MIN(10000, now - last_wakeup) << 4;
264 /* Warn if we took too much time between polls. */
265 if (n_samples > 10 && interval > mean_interval * 8) {
266 struct rusage rusage;
268 getrusage(RUSAGE_SELF, &rusage);
269 VLOG_WARN("%u ms poll interval (%lld ms user, %lld ms system) "
270 "is over %u times the weighted mean interval %u ms "
273 timeval_diff_msec(&rusage.ru_utime, &last_rusage->ru_utime),
274 timeval_diff_msec(&rusage.ru_stime, &last_rusage->ru_stime),
275 interval / mean_interval,
276 (mean_interval + 8) / 16, n_samples);
277 if (rusage.ru_minflt > last_rusage->ru_minflt
278 || rusage.ru_majflt > last_rusage->ru_majflt) {
279 VLOG_WARN("faults: %ld minor, %ld major",
280 rusage.ru_minflt - last_rusage->ru_minflt,
281 rusage.ru_majflt - last_rusage->ru_majflt);
283 if (rusage.ru_inblock > last_rusage->ru_inblock
284 || rusage.ru_oublock > last_rusage->ru_oublock) {
285 VLOG_WARN("disk: %ld reads, %ld writes",
286 rusage.ru_inblock - last_rusage->ru_inblock,
287 rusage.ru_oublock - last_rusage->ru_oublock);
289 if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
290 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
291 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
292 rusage.ru_nvcsw - last_rusage->ru_nvcsw,
293 rusage.ru_nivcsw - last_rusage->ru_nivcsw);
295 coverage_log(VLL_WARN);
298 /* Update exponentially weighted moving average. With these parameters, a
299 * given value decays to 1% of its value in about 100 time steps. */
301 mean_interval = (mean_interval * 122 + interval * 6 + 64) / 128;
303 mean_interval = interval;