2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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.
26 #include <sys/resource.h>
30 #include "openvswitch/dynamic-string.h"
31 #include "fatal-signal.h"
35 #include "ovs-thread.h"
40 #include "openvswitch/vlog.h"
42 VLOG_DEFINE_THIS_MODULE(timeval);
44 #if defined(_WIN32) || defined(__MACH__)
45 typedef unsigned int clockid_t;
46 static int clock_gettime(clock_t id, struct timespec *ts);
48 #ifndef CLOCK_MONOTONIC
49 #define CLOCK_MONOTONIC 1
52 #ifndef CLOCK_REALTIME
53 #define CLOCK_REALTIME 2
55 #endif /* defined(_WIN32) || defined(__MACH__) */
58 /* Number of 100 ns intervals from January 1, 1601 till January 1, 1970. */
59 const static unsigned long long unix_epoch = 116444736000000000;
62 /* Structure set by unixctl time/warp command. */
64 struct unixctl_conn *conn; /* Connection waiting for warp response. */
65 long long int total_warp; /* Total offset to be added to monotonic time. */
66 long long int warp; /* 'total_warp' offset done in steps of 'warp'. */
67 unsigned int main_thread_id; /* Identification for the main thread. */
71 clockid_t id; /* CLOCK_MONOTONIC or CLOCK_REALTIME. */
73 /* Features for use by unit tests. Protected by 'mutex'. */
74 struct ovs_mutex mutex;
75 atomic_bool slow_path; /* True if warped or stopped. */
76 struct timespec warp OVS_GUARDED; /* Offset added for unit tests. */
77 bool stopped OVS_GUARDED; /* Disable real-time updates if true. */
78 struct timespec cache OVS_GUARDED; /* Last time read from kernel. */
79 struct large_warp large_warp OVS_GUARDED; /* Connection information waiting
84 static struct clock monotonic_clock; /* CLOCK_MONOTONIC, if available. */
85 static struct clock wall_clock; /* CLOCK_REALTIME. */
87 /* The monotonic time at which the time module was initialized. */
88 static long long int boot_time;
90 /* True only when timeval_dummy_register() is called. */
91 static bool timewarp_enabled;
92 /* Reference to the seq struct. Threads other than main thread can
93 * wait on timewarp_seq and be waken up when time is warped. */
94 static struct seq *timewarp_seq;
95 /* Last value of 'timewarp_seq'. */
96 DEFINE_STATIC_PER_THREAD_DATA(uint64_t, last_seq, 0);
98 /* Monotonic time in milliseconds at which to die with SIGALRM (if not
100 static long long int deadline = LLONG_MAX;
102 /* Monotonic time, in milliseconds, at which the last call to time_poll() woke
104 DEFINE_STATIC_PER_THREAD_DATA(long long int, last_wakeup, 0);
106 static void log_poll_interval(long long int last_wakeup);
107 static struct rusage *get_recent_rusage(void);
108 static int getrusage_thread(struct rusage *);
109 static void refresh_rusage(void);
110 static void timespec_add(struct timespec *sum,
111 const struct timespec *a, const struct timespec *b);
114 init_clock(struct clock *c, clockid_t id)
116 memset(c, 0, sizeof *c);
118 ovs_mutex_init(&c->mutex);
119 atomic_init(&c->slow_path, false);
120 xclock_gettime(c->id, &c->cache);
130 timewarp_seq = seq_create();
131 init_clock(&monotonic_clock, (!clock_gettime(CLOCK_MONOTONIC, &ts)
134 init_clock(&wall_clock, CLOCK_REALTIME);
135 boot_time = timespec_to_msec(&monotonic_clock.cache);
138 /* Initializes the timetracking module, if not already initialized. */
142 static pthread_once_t once = PTHREAD_ONCE_INIT;
143 pthread_once(&once, do_init_time);
147 time_timespec__(struct clock *c, struct timespec *ts)
153 atomic_read_relaxed(&c->slow_path, &slow_path);
155 xclock_gettime(c->id, ts);
157 struct timespec warp;
158 struct timespec cache;
161 ovs_mutex_lock(&c->mutex);
162 stopped = c->stopped;
165 ovs_mutex_unlock(&c->mutex);
168 xclock_gettime(c->id, &cache);
170 timespec_add(ts, &cache, &warp);
174 /* Stores a monotonic timer, accurate within TIME_UPDATE_INTERVAL ms, into
177 time_timespec(struct timespec *ts)
179 time_timespec__(&monotonic_clock, ts);
182 /* Stores the current time, accurate within TIME_UPDATE_INTERVAL ms, into
185 time_wall_timespec(struct timespec *ts)
187 time_timespec__(&wall_clock, ts);
191 time_sec__(struct clock *c)
195 time_timespec__(c, &ts);
199 /* Returns a monotonic timer, in seconds. */
203 return time_sec__(&monotonic_clock);
206 /* Returns the current time, in seconds. */
210 return time_sec__(&wall_clock);
214 time_msec__(struct clock *c)
218 time_timespec__(c, &ts);
219 return timespec_to_msec(&ts);
222 /* Returns a monotonic timer, in ms (within TIME_UPDATE_INTERVAL ms). */
226 return time_msec__(&monotonic_clock);
229 /* Returns the current time, in ms (within TIME_UPDATE_INTERVAL ms). */
233 return time_msec__(&wall_clock);
236 /* Configures the program to die with SIGALRM 'secs' seconds from now, if
237 * 'secs' is nonzero, or disables the feature if 'secs' is zero. */
239 time_alarm(unsigned int secs)
244 assert_single_threaded();
248 msecs = secs * 1000LL;
249 deadline = now < LLONG_MAX - msecs ? now + msecs : LLONG_MAX;
252 /* Like poll(), except:
254 * - The timeout is specified as an absolute time, as defined by
255 * time_msec(), instead of a duration.
257 * - On error, returns a negative error code (instead of setting errno).
259 * - If interrupted by a signal, retries automatically until the original
260 * timeout is reached. (Because of this property, this function will
261 * never return -EINTR.)
263 * Stores the number of milliseconds elapsed during poll in '*elapsed'. */
265 time_poll(struct pollfd *pollfds, int n_pollfds, HANDLE *handles OVS_UNUSED,
266 long long int timeout_when, int *elapsed)
268 long long int *last_wakeup = last_wakeup_get();
276 if (*last_wakeup && !thread_is_pmd()) {
277 log_poll_interval(*last_wakeup);
281 timeout_when = MIN(timeout_when, deadline);
282 quiescent = ovsrcu_is_quiescent();
285 long long int now = time_msec();
288 if (now >= timeout_when) {
290 } else if ((unsigned long long int) timeout_when - now > INT_MAX) {
293 time_left = timeout_when - now;
300 ovsrcu_quiesce_start();
305 retval = poll(pollfds, n_pollfds, time_left);
310 if (n_pollfds > MAXIMUM_WAIT_OBJECTS) {
311 VLOG_ERR("Cannot handle more than maximum wait objects\n");
312 } else if (n_pollfds != 0) {
313 retval = WaitForMultipleObjects(n_pollfds, handles, FALSE,
317 /* XXX This will be replace by a win error to errno
318 conversion function */
319 retval = -WSAGetLastError();
324 if (!quiescent && time_left) {
325 ovsrcu_quiesce_end();
328 if (deadline <= time_msec()) {
330 fatal_signal_handler(SIGALRM);
332 VLOG_ERR("wake up from WaitForMultipleObjects after deadline");
333 fatal_signal_handler(SIGTERM);
341 if (retval != -EINTR) {
345 *last_wakeup = time_msec();
347 *elapsed = *last_wakeup - start;
352 timespec_to_msec(const struct timespec *ts)
354 return (long long int) ts->tv_sec * 1000 + ts->tv_nsec / (1000 * 1000);
358 timeval_to_msec(const struct timeval *tv)
360 return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000;
363 /* Returns the monotonic time at which the "time" module was initialized, in
373 static ULARGE_INTEGER
376 ULARGE_INTEGER current_time;
377 FILETIME current_time_ft;
379 /* Returns current time in UTC as a 64-bit value representing the number
380 * of 100-nanosecond intervals since January 1, 1601 . */
381 GetSystemTimePreciseAsFileTime(¤t_time_ft);
382 current_time.LowPart = current_time_ft.dwLowDateTime;
383 current_time.HighPart = current_time_ft.dwHighDateTime;
389 clock_gettime(clock_t id, struct timespec *ts)
391 if (id == CLOCK_MONOTONIC) {
392 static LARGE_INTEGER freq;
396 if (!freq.QuadPart) {
397 /* Number of counts per second. */
398 QueryPerformanceFrequency(&freq);
400 /* Total number of counts from a starting point. */
401 QueryPerformanceCounter(&count);
403 /* Total nano seconds from a starting point. */
404 ns = (double) count.QuadPart / freq.QuadPart * 1000000000;
406 ts->tv_sec = count.QuadPart / freq.QuadPart;
407 ts->tv_nsec = ns % 1000000000;
408 } else if (id == CLOCK_REALTIME) {
409 ULARGE_INTEGER current_time = xgetfiletime();
411 /* Time from Epoch to now. */
412 ts->tv_sec = (current_time.QuadPart - unix_epoch) / 10000000;
413 ts->tv_nsec = ((current_time.QuadPart - unix_epoch) %
424 #include <mach/clock.h>
425 #include <mach/mach.h>
427 clock_gettime(clock_t id, struct timespec *ts)
433 if (id == CLOCK_MONOTONIC) {
435 } else if (id == CLOCK_REALTIME) {
436 cid = CALENDAR_CLOCK;
441 host_get_clock_service(mach_host_self(), cid, &clk);
442 clock_get_time(clk, &mts);
443 mach_port_deallocate(mach_task_self(), clk);
444 ts->tv_sec = mts.tv_sec;
445 ts->tv_nsec = mts.tv_nsec;
452 xgettimeofday(struct timeval *tv)
455 if (gettimeofday(tv, NULL) == -1) {
456 VLOG_FATAL("gettimeofday failed (%s)", ovs_strerror(errno));
459 ULARGE_INTEGER current_time = xgetfiletime();
461 tv->tv_sec = (current_time.QuadPart - unix_epoch) / 10000000;
462 tv->tv_usec = ((current_time.QuadPart - unix_epoch) %
468 xclock_gettime(clock_t id, struct timespec *ts)
470 if (clock_gettime(id, ts) == -1) {
471 /* It seems like a bad idea to try to use vlog here because it is
472 * likely to try to check the current time. */
473 ovs_abort(errno, "xclock_gettime() failed");
478 msec_to_timespec(long long int ms, struct timespec *ts)
480 ts->tv_sec = ms / 1000;
481 ts->tv_nsec = (ms % 1000) * 1000 * 1000;
487 struct clock *c = &monotonic_clock;
488 struct timespec warp;
490 ovs_mutex_lock(&c->mutex);
491 if (!c->large_warp.conn) {
492 ovs_mutex_unlock(&c->mutex);
496 if (c->large_warp.total_warp >= c->large_warp.warp) {
497 msec_to_timespec(c->large_warp.warp, &warp);
498 timespec_add(&c->warp, &c->warp, &warp);
499 c->large_warp.total_warp -= c->large_warp.warp;
500 } else if (c->large_warp.total_warp) {
501 msec_to_timespec(c->large_warp.total_warp, &warp);
502 timespec_add(&c->warp, &c->warp, &warp);
503 c->large_warp.total_warp = 0;
505 /* c->large_warp.total_warp is 0. */
506 msec_to_timespec(c->large_warp.warp, &warp);
507 timespec_add(&c->warp, &c->warp, &warp);
510 if (!c->large_warp.total_warp) {
511 unixctl_command_reply(c->large_warp.conn, "warped");
512 c->large_warp.conn = NULL;
515 ovs_mutex_unlock(&c->mutex);
516 seq_change(timewarp_seq);
518 /* give threads (eg. monitor) some chances to run */
526 /* Perform work needed for "timewarp_seq"'s producer and consumers. */
530 /* The function is a no-op unless timeval_dummy_register() is called. */
531 if (timewarp_enabled) {
532 unsigned int thread_id;
533 ovs_mutex_lock(&monotonic_clock.mutex);
534 thread_id = monotonic_clock.large_warp.main_thread_id;
535 ovs_mutex_unlock(&monotonic_clock.mutex);
537 if (thread_id != ovsthread_id_self()) {
538 /* For threads other than the thread that changes the sequence,
540 uint64_t *last_seq = last_seq_get();
542 *last_seq = seq_read(timewarp_seq);
543 seq_wait(timewarp_seq, *last_seq);
545 /* Work on adding the remaining warps. */
552 timeval_diff_msec(const struct timeval *a, const struct timeval *b)
554 return timeval_to_msec(a) - timeval_to_msec(b);
558 timespec_add(struct timespec *sum,
559 const struct timespec *a,
560 const struct timespec *b)
564 tmp.tv_sec = a->tv_sec + b->tv_sec;
565 tmp.tv_nsec = a->tv_nsec + b->tv_nsec;
566 if (tmp.tv_nsec >= 1000 * 1000 * 1000) {
567 tmp.tv_nsec -= 1000 * 1000 * 1000;
575 is_warped(const struct clock *c)
579 ovs_mutex_lock(&c->mutex);
580 warped = monotonic_clock.warp.tv_sec || monotonic_clock.warp.tv_nsec;
581 ovs_mutex_unlock(&c->mutex);
587 log_poll_interval(long long int last_wakeup)
589 long long int interval = time_msec() - last_wakeup;
591 if (interval >= 1000 && !is_warped(&monotonic_clock)) {
592 const struct rusage *last_rusage = get_recent_rusage();
593 struct rusage rusage;
595 if (!getrusage_thread(&rusage)) {
596 VLOG_WARN("Unreasonably long %lldms poll interval"
597 " (%lldms user, %lldms system)",
599 timeval_diff_msec(&rusage.ru_utime,
600 &last_rusage->ru_utime),
601 timeval_diff_msec(&rusage.ru_stime,
602 &last_rusage->ru_stime));
604 if (rusage.ru_minflt > last_rusage->ru_minflt
605 || rusage.ru_majflt > last_rusage->ru_majflt) {
606 VLOG_WARN("faults: %ld minor, %ld major",
607 rusage.ru_minflt - last_rusage->ru_minflt,
608 rusage.ru_majflt - last_rusage->ru_majflt);
610 if (rusage.ru_inblock > last_rusage->ru_inblock
611 || rusage.ru_oublock > last_rusage->ru_oublock) {
612 VLOG_WARN("disk: %ld reads, %ld writes",
613 rusage.ru_inblock - last_rusage->ru_inblock,
614 rusage.ru_oublock - last_rusage->ru_oublock);
616 if (rusage.ru_nvcsw > last_rusage->ru_nvcsw
617 || rusage.ru_nivcsw > last_rusage->ru_nivcsw) {
618 VLOG_WARN("context switches: %ld voluntary, %ld involuntary",
619 rusage.ru_nvcsw - last_rusage->ru_nvcsw,
620 rusage.ru_nivcsw - last_rusage->ru_nivcsw);
623 VLOG_WARN("Unreasonably long %lldms poll interval", interval);
629 /* CPU usage tracking. */
632 long long int when; /* Time that this sample was taken. */
633 unsigned long long int cpu; /* Total user+system CPU usage when sampled. */
637 struct cpu_usage older;
638 struct cpu_usage newer;
641 struct rusage recent_rusage;
643 DEFINE_PER_THREAD_MALLOCED_DATA(struct cpu_tracker *, cpu_tracker_var);
645 static struct cpu_tracker *
646 get_cpu_tracker(void)
648 struct cpu_tracker *t = cpu_tracker_var_get();
650 t = xzalloc(sizeof *t);
651 t->older.when = LLONG_MIN;
652 t->newer.when = LLONG_MIN;
653 cpu_tracker_var_set_unsafe(t);
658 static struct rusage *
659 get_recent_rusage(void)
661 return &get_cpu_tracker()->recent_rusage;
665 getrusage_thread(struct rusage *rusage OVS_UNUSED)
668 return getrusage(RUSAGE_THREAD, rusage);
678 struct cpu_tracker *t = get_cpu_tracker();
679 struct rusage *recent_rusage = &t->recent_rusage;
681 if (!getrusage_thread(recent_rusage)) {
682 long long int now = time_msec();
683 if (now >= t->newer.when + 3 * 1000) {
686 t->newer.cpu = (timeval_to_msec(&recent_rusage->ru_utime) +
687 timeval_to_msec(&recent_rusage->ru_stime));
689 if (t->older.when != LLONG_MIN && t->newer.cpu > t->older.cpu) {
690 unsigned int dividend = t->newer.cpu - t->older.cpu;
691 unsigned int divisor = (t->newer.when - t->older.when) / 100;
692 t->cpu_usage = divisor > 0 ? dividend / divisor : -1;
700 /* Returns an estimate of this process's CPU usage, as a percentage, over the
701 * past few seconds of wall-clock time. Returns -1 if no estimate is available
702 * (which will happen if the process has not been running long enough to have
703 * an estimate, and can happen for other reasons as well). */
707 return get_cpu_tracker()->cpu_usage;
710 /* Unixctl interface. */
712 /* "time/stop" stops the monotonic time returned by e.g. time_msec() from
713 * advancing, except due to later calls to "time/warp". */
715 timeval_stop_cb(struct unixctl_conn *conn,
716 int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
717 void *aux OVS_UNUSED)
719 ovs_mutex_lock(&monotonic_clock.mutex);
720 atomic_store_relaxed(&monotonic_clock.slow_path, true);
721 monotonic_clock.stopped = true;
722 xclock_gettime(monotonic_clock.id, &monotonic_clock.cache);
723 ovs_mutex_unlock(&monotonic_clock.mutex);
725 unixctl_command_reply(conn, NULL);
728 /* "time/warp MSECS" advances the current monotonic time by the specified
729 * number of milliseconds. Unless "time/stop" has also been executed, the
730 * monotonic clock continues to tick forward at the normal rate afterward.
732 * "time/warp LARGE_MSECS MSECS" is a variation of the above command. It
733 * advances the current monotonic time by LARGE_MSECS. This is done MSECS
734 * at a time in each run of the main thread. This gives other threads
735 * time to run after the clock has been advanced by MSECS.
737 * Does not affect wall clock readings. */
739 timeval_warp_cb(struct unixctl_conn *conn,
740 int argc OVS_UNUSED, const char *argv[], void *aux OVS_UNUSED)
742 long long int total_warp = argc > 2 ? atoll(argv[1]) : 0;
743 long long int msecs = argc > 2 ? atoll(argv[2]) : atoll(argv[1]);
744 if (msecs <= 0 || total_warp < 0) {
745 unixctl_command_reply_error(conn, "invalid MSECS");
749 ovs_mutex_lock(&monotonic_clock.mutex);
750 if (monotonic_clock.large_warp.conn) {
751 ovs_mutex_unlock(&monotonic_clock.mutex);
752 unixctl_command_reply_error(conn, "A previous warp in progress");
755 atomic_store_relaxed(&monotonic_clock.slow_path, true);
756 monotonic_clock.large_warp.conn = conn;
757 monotonic_clock.large_warp.total_warp = total_warp;
758 monotonic_clock.large_warp.warp = msecs;
759 monotonic_clock.large_warp.main_thread_id = ovsthread_id_self();
760 ovs_mutex_unlock(&monotonic_clock.mutex);
766 timeval_dummy_register(void)
768 timewarp_enabled = true;
769 unixctl_command_register("time/stop", "", 0, 0, timeval_stop_cb, NULL);
770 unixctl_command_register("time/warp", "[large_msecs] msecs", 1, 2,
771 timeval_warp_cb, NULL);
776 /* strftime() with an extension for high-resolution timestamps. Any '#'s in
777 * 'format' will be replaced by subseconds, e.g. use "%S.###" to obtain results
780 strftime_msec(char *s, size_t max, const char *format,
781 const struct tm_msec *tm)
785 /* Visual Studio 2013's behavior is to crash when 0 is passed as second
786 * argument to strftime. */
787 n = max ? strftime(s, max, format, &tm->tm) : 0;
792 sprintf(decimals, "%03d", tm->msec);
793 for (p = strchr(s, '#'); p; p = strchr(p, '#')) {
796 *p++ = *d ? *d++ : '0';
805 localtime_msec(long long int now, struct tm_msec *result)
807 time_t now_sec = now / 1000;
808 localtime_r(&now_sec, &result->tm);
809 result->msec = now % 1000;
814 gmtime_msec(long long int now, struct tm_msec *result)
816 time_t now_sec = now / 1000;
817 gmtime_r(&now_sec, &result->tm);
818 result->msec = now % 1000;