/*
- * Copyright (c) 2009 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
+#include "command-line.h"
#include "daemon.h"
#include "util.h"
{
struct timeval tv;
- assert(!gettimeofday(&tv, NULL));
+ xgettimeofday(&tv);
return timeval_to_msec(&tv);
}
* setitimer()). Then ensure that, if time has really advanced by
* TIME_UPDATE_INTERVAL, then time_msec() reports that it advanced.
*/
- long long int start_time_msec;
+ long long int start_time_msec, start_time_wall;
long long int start_gtod;
start_time_msec = time_msec();
+ start_time_wall = time_wall_msec();
start_gtod = gettimeofday_in_msec();
for (;;) {
/* Wait up to 1 second. Using select() to do the timeout avoids
* interfering with the interval timer. */
struct timeval timeout;
+ int retval;
+
timeout.tv_sec = 1;
timeout.tv_usec = 0;
- assert(select(0, NULL, NULL, NULL, &timeout) == -1 && errno == EINTR);
+ retval = select(0, NULL, NULL, NULL, &timeout);
+ if (retval != -1) {
+ ovs_fatal(0, "select returned %d", retval);
+ } else if (errno != EINTR) {
+ ovs_fatal(errno, "select reported unexpected error");
+ }
if (gettimeofday_in_msec() - start_gtod >= TIME_UPDATE_INTERVAL) {
- assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL);
+ /* gettimeofday() and time_msec() have different granularities in
+ * their time sources. Depending on the rounding used this could
+ * result in a slight difference, so we allow for 1 ms of slop. */
+ assert(time_msec() - start_time_msec >= TIME_UPDATE_INTERVAL - 1);
+ assert(time_wall_msec() - start_time_wall >=
+ TIME_UPDATE_INTERVAL - 1);
break;
}
}
int
main(int argc, char *argv[])
{
+ proctitle_init(argc, argv);
set_program_name(argv[0]);
- time_init();
if (argc != 2) {
usage();
} else if (!strcmp(argv[1], "plain")) {
+ /* If we're not caching time there isn't much to test and SIGALRM won't
+ * be around to pull us out of the select() call, so just skip out */
+ if (!CACHE_TIME) {
+ exit (77);
+ }
+
do_test();
} else if (!strcmp(argv[1], "daemon")) {
/* Test that time still advances even in a daemon. This is an
* interesting test because fork() cancels the interval timer. */
- char cwd[1024];
+ char cwd[1024], *pidfile;
FILE *success;
+ if (!CACHE_TIME) {
+ exit (77);
+ }
+
assert(getcwd(cwd, sizeof cwd) == cwd);
unlink("test-timeval.success");
/* Daemonize, with a pidfile in the current directory. */
set_detach();
- set_pidfile(xasprintf("%s/test-timeval.pid", cwd));
+ pidfile = xasprintf("%s/test-timeval.pid", cwd);
+ set_pidfile(pidfile);
+ free(pidfile);
set_no_chdir();
daemonize();