From: Willy Tarreau Date: Thu, 5 Mar 2009 13:54:50 +0000 (+0100) Subject: [MINOR] add curr_sec_ms and curr_sec_ms_scaled for current second. X-Git-Tag: v1.3.16-rc1~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=755905857ac26c28d9886ab94c410b6fec4de192;p=thirdparty%2Fhaproxy.git [MINOR] add curr_sec_ms and curr_sec_ms_scaled for current second. Several algorithms will need to know the millisecond value within the current second. Instead of doing a divide every time it is needed, it's better to compute it when it changes, which is when now and now_ms are recomputed. curr_sec_ms_scaled is the same multiplied by 2^32/1000, which will be useful to compute some ratios based on the position within last second. --- diff --git a/include/common/time.h b/include/common/time.h index 07d3070eb6..abc1ccfa3f 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -54,6 +54,8 @@ #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) #define SETNOW(a) (*a=now) +extern unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ +extern unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ extern unsigned int now_ms; /* internal date in milliseconds (may wrap) */ extern struct timeval now; /* internal date is a monotonic function of real clock */ extern struct timeval date; /* the real current date */ diff --git a/src/time.c b/src/time.c index 5318dcdf26..1b0f72c45b 100644 --- a/src/time.c +++ b/src/time.c @@ -1,7 +1,7 @@ /* * Time calculation functions. * - * Copyright 2000-2008 Willy Tarreau + * Copyright 2000-2009 Willy Tarreau * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -16,6 +16,8 @@ #include #include +unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ +unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ unsigned int now_ms; /* internal date in milliseconds (may wrap) */ struct timeval now; /* internal date is a monotonic function of real clock */ struct timeval date; /* the real current date */ @@ -162,7 +164,7 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted) gettimeofday(&date, NULL); if (unlikely(max_wait < 0)) { tv_zero(&tv_offset); - now = date; + adjusted = date; goto to_ms; } __tv_add(&adjusted, &date, &tv_offset); @@ -175,26 +177,26 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted) * MAX_DELAY_MS to cover additional time. */ _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS); - if (unlikely(__tv_isge(&adjusted, &deadline))) { - goto fixup; /* jump in the future */ - } - now = adjusted; - goto to_ms; + if (likely(__tv_islt(&adjusted, &deadline))) + goto to_ms; /* OK time is within expected range */ fixup: /* Large jump. If the poll was interrupted, we consider that the date * has not changed (immediate wake-up), otherwise we add the poll * time-out to the previous date. The new offset is recomputed. */ - if (!interrupted) - _tv_ms_add(&now, &now, max_wait); - tv_offset.tv_sec = now.tv_sec - date.tv_sec; - tv_offset.tv_usec = now.tv_usec - date.tv_usec; + _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait); + + tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec; + tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec; if (tv_offset.tv_usec < 0) { tv_offset.tv_usec += 1000000; tv_offset.tv_sec--; } to_ms: - now_ms = now.tv_sec * 1000 + now.tv_usec / 1000; + now = adjusted; + curr_sec_ms = now.tv_usec / 1000; /* ms of current second */ + curr_sec_ms_scaled = curr_sec_ms * 4294971; /* ms * 2^32 / 1000 */ + now_ms = now.tv_sec * 1000 + curr_sec_ms; return; }