2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 set_up_timer(void);
47 static void set_up_signal(int flags);
48 static void sigalrm_handler(int);
49 static void refresh_if_ticked(void);
50 static time_t time_add(time_t, time_t);
51 static void block_sigalrm(sigset_t *);
52 static void unblock_sigalrm(const sigset_t *);
53 static void log_poll_interval(long long int last_wakeup,
54 const struct rusage *last_rusage);
56 /* Initializes the timetracking module. */
67 gettimeofday(&now, NULL);
70 set_up_signal(SA_RESTART);
75 set_up_signal(int flags)
79 memset(&sa, 0, sizeof sa);
80 sa.sa_handler = sigalrm_handler;
81 sigemptyset(&sa.sa_mask);
83 if (sigaction(SIGALRM, &sa, NULL)) {
84 ovs_fatal(errno, "sigaction(SIGALRM) failed");
88 /* Remove SA_RESTART from the flags for SIGALRM, so that any system call that
89 * is interrupted by the periodic timer interrupt will return EINTR instead of
90 * continuing after the signal handler returns.
92 * time_disable_restart() and time_enable_restart() may be usefully wrapped
93 * around function calls that might otherwise block forever unless interrupted
96 * time_disable_restart();
97 * fcntl(fd, F_SETLKW, &lock);
98 * time_enable_restart();
101 time_disable_restart(void)
106 /* Add SA_RESTART to the flags for SIGALRM, so that any system call that
107 * is interrupted by the periodic timer interrupt will continue after the
108 * signal handler returns instead of returning EINTR. */
110 time_enable_restart(void)
112 set_up_signal(SA_RESTART);
118 struct itimerval itimer;
120 itimer.it_interval.tv_sec = 0;
121 itimer.it_interval.tv_usec = TIME_UPDATE_INTERVAL * 1000;
122 itimer.it_value = itimer.it_interval;
123 if (setitimer(ITIMER_REAL, &itimer, NULL)) {
124 ovs_fatal(errno, "setitimer failed");
128 /* Set up the interval timer, to ensure that time advances even without calling
131 * A child created with fork() does not inherit the parent's interval timer, so
132 * this function needs to be called from the child after fork(). */
139 /* Forces a refresh of the current time from the kernel. It is not usually
140 * necessary to call this function, since the time will be refreshed
141 * automatically at least every TIME_UPDATE_INTERVAL milliseconds. */
145 gettimeofday(&now, NULL);
149 /* Returns the current time, in seconds. */
157 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
162 return timeval_to_msec(&now);
165 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
168 time_timeval(struct timeval *tv)
174 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
175 * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
177 time_alarm(unsigned int secs)
182 block_sigalrm(&oldsigs);
183 deadline = secs ? time_add(time_now(), secs) : TIME_MIN;
184 unblock_sigalrm(&oldsigs);
187 /* Like poll(), except:
189 * - On error, returns a negative error code (instead of setting errno).
191 * - If interrupted by a signal, retries automatically until the original
192 * 'timeout' expires. (Because of this property, this function will
193 * never return -EINTR.)
195 * - As a side effect, refreshes the current time (like time_refresh()).
198 time_poll(struct pollfd *pollfds, int n_pollfds, int timeout)
200 static long long int last_wakeup;
201 static struct rusage last_rusage;
208 log_poll_interval(last_wakeup, &last_rusage);
215 long long int elapsed = time_msec() - start;
216 time_left = timeout >= elapsed ? timeout - elapsed : 0;
221 retval = poll(pollfds, n_pollfds, time_left);
226 if (retval != -EINTR) {
230 if (!blocked && deadline == TIME_MIN) {
231 block_sigalrm(&oldsigs);
236 unblock_sigalrm(&oldsigs);
238 last_wakeup = time_msec();
239 getrusage(RUSAGE_SELF, &last_rusage);
243 /* Returns the sum of 'a' and 'b', with saturation on overflow or underflow. */
245 time_add(time_t a, time_t b)
248 ? (b > TIME_MAX - a ? TIME_MAX : a + b)
249 : (b < TIME_MIN - a ? TIME_MIN : a + b));
253 sigalrm_handler(int sig_nr)
256 if (deadline != TIME_MIN && time(0) > deadline) {
257 fatal_signal_handler(sig_nr);
262 refresh_if_ticked(void)
271 block_sigalrm(sigset_t *oldsigs)
274 sigemptyset(&sigalrm);
275 sigaddset(&sigalrm, SIGALRM);
276 if (sigprocmask(SIG_BLOCK, &sigalrm, oldsigs)) {
277 ovs_fatal(errno, "sigprocmask");
282 unblock_sigalrm(const sigset_t *oldsigs)
284 if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) {
285 ovs_fatal(errno, "sigprocmask");
290 timeval_to_msec(const struct timeval *tv)
292 return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
296 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
298 return timeval_to_msec(a) - timeval_to_msec(b);
302 log_poll_interval(long long int last_wakeup, const struct rusage *last_rusage)
304 static unsigned int mean_interval; /* In 16ths of a millisecond. */
305 static unsigned int n_samples;
308 unsigned int interval; /* In 16ths of a millisecond. */
310 /* Compute interval from last wakeup to now in 16ths of a millisecond,
311 * capped at 10 seconds (16000 in this unit). */
313 interval = MIN(10000, now - last_wakeup) << 4;
315 /* Warn if we took too much time between polls. */
316 if (n_samples > 10 && interval > mean_interval * 8) {
317 struct rusage rusage;
319 getrusage(RUSAGE_SELF, &rusage);
320 VLOG_WARN("%u ms poll interval (%lld ms user, %lld ms system) "
321 "is over %u times the weighted mean interval %u ms "
324 timeval_diff_msec(&rusage.ru_utime, &last_rusage->ru_utime),
325 timeval_diff_msec(&rusage.ru_stime, &last_rusage->ru_stime),
326 interval / mean_interval,
327 (mean_interval + 8) / 16, n_samples);
328 if (rusage.ru_minflt > last_rusage->ru_minflt
329 || rusage.ru_majflt > last_rusage->ru_majflt) {
330 VLOG_WARN("faults: %ld minor, %ld major",
331 rusage.ru_minflt - last_rusage->ru_minflt,
332 rusage.ru_majflt - last_rusage->ru_majflt);
334 if (rusage.ru_inblock > last_rusage->ru_inblock
335 || rusage.ru_oublock > last_rusage->ru_oublock) {
336 VLOG_WARN("disk: %ld reads, %ld writes",
337 rusage.ru_inblock - last_rusage->ru_inblock,
338 rusage.ru_oublock - last_rusage->ru_oublock);
340 if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
341 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
342 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
343 rusage.ru_nvcsw - last_rusage->ru_nvcsw,
344 rusage.ru_nivcsw - last_rusage->ru_nivcsw);
347 /* Care should be taken in the value chosen for logging. Depending
348 * on the configuration, syslog can write changes synchronously,
349 * which can cause the coverage messages to take longer to log
350 * than the processing delay that triggered it. */
351 coverage_log(VLL_INFO, true);
354 /* Update exponentially weighted moving average. With these parameters, a
355 * given value decays to 1% of its value in about 100 time steps. */
357 mean_interval = (mean_interval * 122 + interval * 6 + 64) / 128;
359 mean_interval = interval;