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 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;
69 gettimeofday(&now, NULL);
72 /* Set up signal handler. */
73 memset(&sa, 0, sizeof sa);
74 sa.sa_handler = sigalrm_handler;
75 sigemptyset(&sa.sa_mask);
76 sa.sa_flags = SA_RESTART;
77 if (sigaction(SIGALRM, &sa, NULL)) {
78 ovs_fatal(errno, "sigaction(SIGALRM) failed");
81 /* Set up periodic timer. */
82 itimer.it_interval.tv_sec = 0;
83 itimer.it_interval.tv_usec = TIME_UPDATE_INTERVAL * 1000;
84 itimer.it_value = itimer.it_interval;
85 if (setitimer(ITIMER_REAL, &itimer, NULL)) {
86 ovs_fatal(errno, "setitimer failed");
90 /* Forces a refresh of the current time from the kernel. It is not usually
91 * necessary to call this function, since the time will be refreshed
92 * automatically at least every TIME_UPDATE_INTERVAL milliseconds. */
96 gettimeofday(&now, NULL);
100 /* Returns the current time, in seconds. */
108 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
113 return timeval_to_msec(&now);
116 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
119 time_timeval(struct timeval *tv)
125 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
126 * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
128 time_alarm(unsigned int secs)
133 block_sigalrm(&oldsigs);
134 deadline = secs ? time_add(time_now(), secs) : TIME_MIN;
135 unblock_sigalrm(&oldsigs);
138 /* Like poll(), except:
140 * - On error, returns a negative error code (instead of setting errno).
142 * - If interrupted by a signal, retries automatically until the original
143 * 'timeout' expires. (Because of this property, this function will
144 * never return -EINTR.)
146 * - As a side effect, refreshes the current time (like time_refresh()).
149 time_poll(struct pollfd *pollfds, int n_pollfds, int timeout)
151 static long long int last_wakeup;
152 static struct rusage last_rusage;
159 log_poll_interval(last_wakeup, &last_rusage);
166 long long int elapsed = time_msec() - start;
167 time_left = timeout >= elapsed ? timeout - elapsed : 0;
172 retval = poll(pollfds, n_pollfds, time_left);
177 if (retval != -EINTR) {
181 if (!blocked && deadline == TIME_MIN) {
182 block_sigalrm(&oldsigs);
187 unblock_sigalrm(&oldsigs);
189 last_wakeup = time_msec();
190 getrusage(RUSAGE_SELF, &last_rusage);
194 /* Returns the sum of 'a' and 'b', with saturation on overflow or underflow. */
196 time_add(time_t a, time_t b)
199 ? (b > TIME_MAX - a ? TIME_MAX : a + b)
200 : (b < TIME_MIN - a ? TIME_MIN : a + b));
204 sigalrm_handler(int sig_nr)
207 if (deadline != TIME_MIN && time(0) > deadline) {
208 fatal_signal_handler(sig_nr);
213 refresh_if_ticked(void)
222 block_sigalrm(sigset_t *oldsigs)
225 sigemptyset(&sigalrm);
226 sigaddset(&sigalrm, SIGALRM);
227 if (sigprocmask(SIG_BLOCK, &sigalrm, oldsigs)) {
228 ovs_fatal(errno, "sigprocmask");
233 unblock_sigalrm(const sigset_t *oldsigs)
235 if (sigprocmask(SIG_SETMASK, oldsigs, NULL)) {
236 ovs_fatal(errno, "sigprocmask");
241 timeval_to_msec(const struct timeval *tv)
243 return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
247 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
249 return timeval_to_msec(a) - timeval_to_msec(b);
253 log_poll_interval(long long int last_wakeup, const struct rusage *last_rusage)
255 static unsigned int mean_interval; /* In 16ths of a millisecond. */
256 static unsigned int n_samples;
259 unsigned int interval; /* In 16ths of a millisecond. */
261 /* Compute interval from last wakeup to now in 16ths of a millisecond,
262 * capped at 10 seconds (16000 in this unit). */
264 interval = MIN(10000, now - last_wakeup) << 4;
266 /* Warn if we took too much time between polls. */
267 if (n_samples > 10 && interval > mean_interval * 8) {
268 struct rusage rusage;
270 getrusage(RUSAGE_SELF, &rusage);
271 VLOG_WARN("%u ms poll interval (%lld ms user, %lld ms system) "
272 "is over %u times the weighted mean interval %u ms "
275 timeval_diff_msec(&rusage.ru_utime, &last_rusage->ru_utime),
276 timeval_diff_msec(&rusage.ru_stime, &last_rusage->ru_stime),
277 interval / mean_interval,
278 (mean_interval + 8) / 16, n_samples);
279 if (rusage.ru_minflt > last_rusage->ru_minflt
280 || rusage.ru_majflt > last_rusage->ru_majflt) {
281 VLOG_WARN("faults: %ld minor, %ld major",
282 rusage.ru_minflt - last_rusage->ru_minflt,
283 rusage.ru_majflt - last_rusage->ru_majflt);
285 if (rusage.ru_inblock > last_rusage->ru_inblock
286 || rusage.ru_oublock > last_rusage->ru_oublock) {
287 VLOG_WARN("disk: %ld reads, %ld writes",
288 rusage.ru_inblock - last_rusage->ru_inblock,
289 rusage.ru_oublock - last_rusage->ru_oublock);
291 if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
292 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
293 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
294 rusage.ru_nvcsw - last_rusage->ru_nvcsw,
295 rusage.ru_nivcsw - last_rusage->ru_nivcsw);
297 coverage_log(VLL_WARN, true);
300 /* Update exponentially weighted moving average. With these parameters, a
301 * given value decays to 1% of its value in about 100 time steps. */
303 mean_interval = (mean_interval * 122 + interval * 6 + 64) / 128;
305 mean_interval = interval;