summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 61c19cc)
raw | patch | inline | side by side (parent: 61c19cc)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 3 Feb 2009 12:05:21 +0000 (13:05 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 5 Feb 2009 20:10:26 +0000 (21:10 +0100) |
The third (return) argument is now of type 'struct timeval' (instead of
'struct timespec') as well. Also, it may now be NULL, in which case the
difference is not returned. This is a more general and thus more flexible
approach that permitted to remove the custom timeval_sub*() implementations in
the multimeter and ntpd plugins.
The return value has been changed to reflect the return value used by
functions like strcmp(), i.e. it is less than, equal to, or greater than zero
if the first argument is less than, equal to, or greater than the second
argument respectively.
In the cases where a 'struct timespec' is required, the return value is now
converted by multiplying the tv_usec member with 1000.
'struct timespec') as well. Also, it may now be NULL, in which case the
difference is not returned. This is a more general and thus more flexible
approach that permitted to remove the custom timeval_sub*() implementations in
the multimeter and ntpd plugins.
The return value has been changed to reflect the return value used by
functions like strcmp(), i.e. it is less than, equal to, or greater than zero
if the first argument is less than, equal to, or greater than the second
argument respectively.
In the cases where a 'struct timespec' is required, the return value is now
converted by multiplying the tv_usec member with 1000.
src/collectd.c | patch | blob | history | |
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history | |
src/multimeter.c | patch | blob | history | |
src/ntpd.c | patch | blob | history | |
src/rrdtool.c | patch | blob | history |
diff --git a/src/collectd.c b/src/collectd.c
index 548a8fdd989c0772cb8b361079cdccbda4377edd..17905c74f95a674eddee10958077ff43bd44c469 100644 (file)
--- a/src/collectd.c
+++ b/src/collectd.c
{
struct timeval tv_now;
struct timeval tv_next;
+ struct timeval tv_wait;
struct timespec ts_wait;
while (loop == 0)
return (-1);
}
- if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0)
+ if (timeval_cmp (tv_next, tv_now, &tv_wait) <= 0)
{
- WARNING ("Not sleeping because "
- "`timeval_sub_timespec' returned "
- "non-zero!");
+ WARNING ("Not sleeping because the next interval is "
+ "%i.%06i seconds in the past!",
+ (int) tv_wait.tv_sec, (int) tv_wait.tv_usec);
continue;
}
+ ts_wait.tv_sec = tv_wait.tv_sec;
+ ts_wait.tv_nsec = (long) (1000 * tv_wait.tv_usec);
+
while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1))
{
if (errno != EINTR)
diff --git a/src/common.c b/src/common.c
index aeea28d3529e6b4ef766de64e4688adc10ca7e46..7cc1d9d44e591f5a93a917016752350cf2d2a649 100644 (file)
--- a/src/common.c
+++ b/src/common.c
return (0);
} /* int escape_slashes */
-int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret)
+int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta)
{
- if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL))
- return (-2);
+ struct timeval *larger;
+ struct timeval *smaller;
- if ((tv0->tv_sec < tv1->tv_sec)
- || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
- return (-1);
+ int status;
- ret->tv_sec = tv0->tv_sec - tv1->tv_sec;
- ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec));
+ NORMALIZE_TIMEVAL (tv0);
+ NORMALIZE_TIMEVAL (tv1);
- if (ret->tv_nsec < 0)
+ if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec))
{
- assert (ret->tv_sec > 0);
+ if (delta != NULL) {
+ delta->tv_sec = 0;
+ delta->tv_usec = 0;
+ }
+ return (0);
+ }
- ret->tv_nsec += 1000000000;
- ret->tv_sec -= 1;
+ if ((tv0.tv_sec < tv1.tv_sec)
+ || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec)))
+ {
+ larger = &tv1;
+ smaller = &tv0;
+ status = -1;
+ }
+ else
+ {
+ larger = &tv0;
+ smaller = &tv1;
+ status = 1;
}
- return (0);
-}
+ if (delta != NULL) {
+ delta->tv_sec = larger->tv_sec - smaller->tv_sec;
+
+ if (smaller->tv_usec <= larger->tv_usec)
+ delta->tv_usec = larger->tv_usec - smaller->tv_usec;
+ else
+ {
+ --delta->tv_sec;
+ delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec;
+ }
+ }
+
+ assert ((delta == NULL)
+ || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000)));
+
+ return (status);
+} /* int timeval_cmp */
int check_create_dir (const char *file_orig)
{
diff --git a/src/common.h b/src/common.h
index d372872a30d135dd6743f6365b2b76f1f26b8013..85db3adb35faf5981477b1dd90179f6aaffa9c6b 100644 (file)
--- a/src/common.h
+++ b/src/common.h
int strsubstitute (char *str, char c_from, char c_to);
-/* FIXME: `timeval_sub_timespec' needs a description */
-int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret);
+/*
+ * NAME
+ * timeval_cmp
+ *
+ * DESCRIPTION
+ * Compare the two time values `tv0' and `tv1' and store the absolut value
+ * of the difference in the time value pointed to by `delta' if it does not
+ * equal NULL.
+ *
+ * RETURN VALUE
+ * Returns an integer less than, equal to, or greater than zero if `tv0' is
+ * less than, equal to, or greater than `tv1' respectively.
+ */
+int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta);
+
+/* make sure tv_usec stores less than a second */
+#define NORMALIZE_TIMEVAL(tv) \
+ do { \
+ (tv).tv_sec += (tv).tv_usec / 1000000; \
+ (tv).tv_usec = (tv).tv_usec % 1000000; \
+ } while (0)
int check_create_dir (const char *file_orig);
diff --git a/src/multimeter.c b/src/multimeter.c
index 9c9c2c152093760db11fd2560d2f17e3c538806d..e3da00a1520a98109d210b4217f84375cce2e25e 100644 (file)
--- a/src/multimeter.c
+++ b/src/multimeter.c
static int fd = -1;
-static int multimeter_timeval_sub (struct timeval *tv1, struct timeval *tv2,
- struct timeval *res)
-{
- if ((tv1->tv_sec < tv2->tv_sec) ||
- ((tv1->tv_sec == tv2->tv_sec) && (tv1->tv_usec < tv2->tv_usec)))
- return (-1);
-
- res->tv_sec = tv1->tv_sec - tv2->tv_sec;
- res->tv_usec = tv1->tv_usec - tv2->tv_usec;
-
- assert ((res->tv_sec > 0) || ((res->tv_sec == 0) && (res->tv_usec > 0)));
-
- while (res->tv_usec < 0)
- {
- res->tv_usec += 1000000;
- res->tv_sec--;
- }
- return (0);
-}
-
#define LINE_LENGTH 14
static int multimeter_read_value(double *value)
{
sizeof (errbuf)));
return (-1);
}
- if (multimeter_timeval_sub (&time_end, &time_now, &timeout) == -1)
+ if (timeval_cmp (time_end, time_now, &timeout) < 0)
break;
status = select(fd+1, &rfds, NULL, NULL, &timeout);
diff --git a/src/ntpd.c b/src/ntpd.c
index 44964bb5a8dbd936d23ef2e828e4d05b2561c543..14f06ebcd887b1c37d9f67a847ce478a4cf1f89f 100644 (file)
--- a/src/ntpd.c
+++ b/src/ntpd.c
plugin_dispatch_values (&vl);
}
-/* returns `tv0 - tv1' in milliseconds or 0 if `tv1 > tv0' */
-static int timeval_sub (const struct timeval *tv0, const struct timeval *tv1)
-{
- int sec;
- int usec;
-
- if ((tv0->tv_sec < tv1->tv_sec)
- || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec)))
- return (0);
-
- sec = tv0->tv_sec - tv1->tv_sec;
- usec = tv0->tv_usec - tv1->tv_usec;
-
- while (usec < 0)
- {
- usec += 1000000;
- sec -= 1;
- }
-
- if (sec < 0)
- return (0);
-
- return ((sec * 1000) + ((usec + 500) / 1000));
-}
-
static int ntpd_connect (void)
{
char *host;
done = 0;
while (done == 0)
{
+ struct timeval time_left;
+
if (gettimeofday (&time_now, NULL) < 0)
{
char errbuf[1024];
return (-1);
}
+ if (timeval_cmp (time_end, time_now, &time_left) <= 0)
+ timeout = 0;
+ else
+ timeout = 1000 * time_left.tv_sec
+ + ((time_left.tv_usec + 500) / 1000);
+
/* timeout reached */
- if ((timeout = timeval_sub (&time_end, &time_now)) == 0)
+ if (timeout <= 0)
break;
poll_s.fd = sd;
diff --git a/src/rrdtool.c b/src/rrdtool.c
index 3265561673fb2263c76965c2bea5f4f7ab0d78d2..95bde6941e459b03b68ab29da00d31c6b03ab34b 100644 (file)
--- a/src/rrdtool.c
+++ b/src/rrdtool.c
break;
gettimeofday (&tv_now, /* timezone = */ NULL);
- status = timeval_sub_timespec (&tv_next_update, &tv_now,
- &ts_wait);
+ status = timeval_cmp (tv_next_update, tv_now, NULL);
/* We're good to go */
- if (status != 0)
+ if (status <= 0)
break;
/* We're supposed to wait a bit with this update, so we'll