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 setup_timer(void);
47 static void sigalrm_handler(int);
48 static void refresh_if_ticked(void);
49 static time_t time_add(time_t, time_t);
50 static void block_sigalrm(sigset_t *);
51 static void unblock_sigalrm(const sigset_t *);
52 static void log_poll_interval(long long int last_wakeup,
53 const struct rusage *last_rusage);
55 /* Initializes the timetracking module. */
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 signal. */
86 struct itimerval itimer;
88 itimer.it_interval.tv_sec = 0;
89 itimer.it_interval.tv_usec = TIME_UPDATE_INTERVAL * 1000;
90 itimer.it_value = itimer.it_interval;
91 if (setitimer(ITIMER_REAL, &itimer, NULL)) {
92 ovs_fatal(errno, "setitimer failed");
96 /* Set up the interval timer, to ensure that time advances even without calling
99 * A child created with fork() does not inherit the parent's interval timer, so
100 * this function needs to be called from the child after fork(). */
107 /* Forces a refresh of the current time from the kernel. It is not usually
108 * necessary to call this function, since the time will be refreshed
109 * automatically at least every TIME_UPDATE_INTERVAL milliseconds. */
113 gettimeofday(&now, NULL);
117 /* Returns the current time, in seconds. */
125 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
130 return timeval_to_msec(&now);
133 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
136 time_timeval(struct timeval *tv)
142 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
143 * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
145 time_alarm(unsigned int secs)
150 block_sigalrm(&oldsigs);
151 deadline = secs ? time_add(time_now(), secs) : TIME_MIN;
152 unblock_sigalrm(&oldsigs);
155 /* Like poll(), except:
157 * - On error, returns a negative error code (instead of setting errno).
159 * - If interrupted by a signal, retries automatically until the original
160 * 'timeout' expires. (Because of this property, this function will
161 * never return -EINTR.)
163 * - As a side effect, refreshes the current time (like time_refresh()).
166 time_poll(struct pollfd *pollfds, int n_pollfds, int timeout)
168 static long long int last_wakeup;
169 static struct rusage last_rusage;
176 log_poll_interval(last_wakeup, &last_rusage);
183 long long int elapsed = time_msec() - start;
184 time_left = timeout >= elapsed ? timeout - elapsed : 0;
189 retval = poll(pollfds, n_pollfds, time_left);
194 if (retval != -EINTR) {
198 if (!blocked && deadline == TIME_MIN) {
199 block_sigalrm(&oldsigs);
204 unblock_sigalrm(&oldsigs);
206 last_wakeup = time_msec();
207 getrusage(RUSAGE_SELF, &last_rusage);
211 /* Returns the sum of 'a' and 'b', with saturation on overflow or underflow. */
213 time_add(time_t a, time_t b)
216 ? (b > TIME_MAX - a ? TIME_MAX : a + b)
217 : (b < TIME_MIN - a ? TIME_MIN : a + b));
221 sigalrm_handler(int sig_nr)
224 if (deadline != TIME_MIN && time(0) > deadline) {
225 fatal_signal_handler(sig_nr);
230 refresh_if_ticked(void)
239 block_sigalrm(sigset_t *oldsigs)
242 sigemptyset(&sigalrm);
243 sigaddset(&sigalrm, SIGALRM);
244 if (sigprocmask(SIG_BLOCK, &sigalrm, oldsigs)) {
245 ovs_fatal(errno, "sigprocmask");
250 unblock_sigalrm(const sigset_t *oldsigs)
252 if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) {
253 ovs_fatal(errno, "sigprocmask");
258 timeval_to_msec(const struct timeval *tv)
260 return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
264 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
266 return timeval_to_msec(a) - timeval_to_msec(b);
270 log_poll_interval(long long int last_wakeup, const struct rusage *last_rusage)
272 static unsigned int mean_interval; /* In 16ths of a millisecond. */
273 static unsigned int n_samples;
276 unsigned int interval; /* In 16ths of a millisecond. */
278 /* Compute interval from last wakeup to now in 16ths of a millisecond,
279 * capped at 10 seconds (16000 in this unit). */
281 interval = MIN(10000, now - last_wakeup) << 4;
283 /* Warn if we took too much time between polls. */
284 if (n_samples > 10 && interval > mean_interval * 8) {
285 struct rusage rusage;
287 getrusage(RUSAGE_SELF, &rusage);
288 VLOG_WARN("%u ms poll interval (%lld ms user, %lld ms system) "
289 "is over %u times the weighted mean interval %u ms "
292 timeval_diff_msec(&rusage.ru_utime, &last_rusage->ru_utime),
293 timeval_diff_msec(&rusage.ru_stime, &last_rusage->ru_stime),
294 interval / mean_interval,
295 (mean_interval + 8) / 16, n_samples);
296 if (rusage.ru_minflt > last_rusage->ru_minflt
297 || rusage.ru_majflt > last_rusage->ru_majflt) {
298 VLOG_WARN("faults: %ld minor, %ld major",
299 rusage.ru_minflt - last_rusage->ru_minflt,
300 rusage.ru_majflt - last_rusage->ru_majflt);
302 if (rusage.ru_inblock > last_rusage->ru_inblock
303 || rusage.ru_oublock > last_rusage->ru_oublock) {
304 VLOG_WARN("disk: %ld reads, %ld writes",
305 rusage.ru_inblock - last_rusage->ru_inblock,
306 rusage.ru_oublock - last_rusage->ru_oublock);
308 if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
309 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
310 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
311 rusage.ru_nvcsw - last_rusage->ru_nvcsw,
312 rusage.ru_nivcsw - last_rusage->ru_nivcsw);
315 /* Care should be taken in the value chosen for logging. Depending
316 * on the configuration, syslog can write changes synchronously,
317 * which can cause the coverage messages to take longer to log
318 * than the processing delay that triggered it. */
319 coverage_log(VLL_INFO, true);
322 /* Update exponentially weighted moving average. With these parameters, a
323 * given value decays to 1% of its value in about 100 time steps. */
325 mean_interval = (mean_interval * 122 + interval * 6 + 64) / 128;
327 mean_interval = interval;