blob: 0975b292df15da778c6dfbdfe4b97fa67927162b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreau45a12512011-09-10 16:56:42 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020014
15#include <common/config.h>
Willy Tarreau5e8f0662007-02-12 00:59:08 +010016#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/time.h>
Christopher Faulet99aad922017-10-31 09:03:51 +010018#include <common/hathreads.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019
Christopher Faulet9a655712017-05-11 11:00:15 +020020THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
21THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */
22THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
23THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
24THREAD_LOCAL unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
25THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
26THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020027struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020028THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
29THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
31/*
32 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
33 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020034REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020035{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020036 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
37 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020038 while (tv->tv_usec >= 1000000) {
39 tv->tv_usec -= 1000000;
40 tv->tv_sec++;
41 }
42 return tv;
43}
44
45/*
46 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
Willy Tarreau42aae5c2007-04-29 17:43:56 +020047 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020048 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020049REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020050{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020051 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052}
53
54/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020056 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020058REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020059{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020060 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020061}
62
63/*
64 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
65 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
66 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020067 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020068 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020069REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020070{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020071 return __tv_ms_le2(tv1, tv2);
72}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020073
Willy Tarreau42aae5c2007-04-29 17:43:56 +020074/*
75 * returns the remaining time between tv1=now and event=tv2
76 * if tv2 is passed, 0 is returned.
77 * Must not be used when either argument is eternity.
78 */
79REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
80{
81 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020082}
83
84/*
85 * returns the remaining time between tv1=now and event=tv2
86 * if tv2 is passed, 0 is returned.
87 * Returns TIME_ETERNITY if tv2 is eternity.
88 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020089REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020090{
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 if (tv_iseternity(tv2))
92 return TIME_ETERNITY;
93
Willy Tarreau42aae5c2007-04-29 17:43:56 +020094 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095}
96
Willy Tarreaubaaee002006-06-26 02:48:02 +020097/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020098 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 * Must not be used when either argument is eternity.
100 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200101REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200103 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104}
105
106/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200107 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
108 */
109REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
110{
111 return __tv_add(tv, from, inc);
112}
113
114/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200115 * If <inc> is set, then add it to <from> and set the result to <tv>, then
116 * return 1, otherwise return 0. It is meant to be used in if conditions.
117 */
118REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
119{
120 return __tv_add_ifset(tv, from, inc);
121}
122
123/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200124 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
125 * 0 is returned. The result is stored into tv.
126 */
127REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
128{
129 return __tv_remain(tv1, tv2, tv);
130}
131
132/*
133 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
134 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
135 * eternity.
136 */
137REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
138{
139 return __tv_remain2(tv1, tv2, tv);
140}
141
Willy Tarreau0481c202007-05-13 16:03:27 +0200142/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
143REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
144{
145 return __tv_isle(tv1, tv2);
146}
147
148/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
149REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
150{
151 return __tv_isgt(tv1, tv2);
152}
153
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200154/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
155 * close as possible to real time, following a monotonic function. The main
156 * principle consists in detecting backwards and forwards time jumps and adjust
157 * an offset to correct them. This function should be called once after each
158 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
159 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
160 * value means that we have not expired the timeout). Calling it with (-1,*)
161 * sets both <date> and <now> to current date, and calling it with (0,1) simply
162 * updates the values.
Christopher Faulet99aad922017-10-31 09:03:51 +0100163 *
164 * tv_offset is used to adjust the current time (date), to have a monotonic time
165 * (now). It must be global and thread-safe. But a timeval cannot be atomically
166 * updated. So instead, we store it in a 64-bits integer (offset). And in
167 * tv_update_date, we convert this integer into a timeval (tv_offset). Once
168 * updated, it is converted back into an integer to be atomically stored.
169 *
170 * To store a tv_offset into an integer, we use 32 bits from tv_sec and 32 bits
171 * tv_usec to avoid shift operations.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200172 */
Christopher Faulet99aad922017-10-31 09:03:51 +0100173#define OFFSET_TO_TIMEVAL(off, tv) \
174 do { \
175 unsigned long long __i = (off); \
176 (tv)->tv_sec = (__i << 32); \
177 (tv)->tv_usec = (__i & 0xFFFFFFFFU); \
178 } while (0)
179
180#define TIMEVAL_TO_OFFSET(tv, off) \
181 do { \
182 unsigned long long __i = (((tv).tv_sec & 0xFFFFFFFFULL) << 32) + (unsigned int)(tv).tv_usec; \
183 HA_ATOMIC_STORE((off), __i); \
184 } while (0)
185
186#define RESET_OFFSET(off) \
187 do { \
188 HA_ATOMIC_STORE((off), 0); \
189 } while (0)
190
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200191REGPRM2 void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200192{
Christopher Faulet99aad922017-10-31 09:03:51 +0100193 static long long offset = 0; /* warning: signed offset! */
194 struct timeval tv_offset; /* offset converted into a timeval */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200195 struct timeval adjusted, deadline;
Willy Tarreau351b3a12017-03-29 15:24:33 +0200196 unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200197
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200198 gettimeofday(&date, NULL);
199 if (unlikely(max_wait < 0)) {
Christopher Faulet99aad922017-10-31 09:03:51 +0100200 RESET_OFFSET(&offset);
Willy Tarreau75590582009-03-05 14:54:50 +0100201 adjusted = date;
Willy Tarreau45a12512011-09-10 16:56:42 +0200202 after_poll = date;
203 samp_time = idle_time = 0;
204 idle_pct = 100;
Willy Tarreaue6313a32008-06-29 13:47:25 +0200205 goto to_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200206 }
Christopher Faulet99aad922017-10-31 09:03:51 +0100207 OFFSET_TO_TIMEVAL(offset, &tv_offset);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200208 __tv_add(&adjusted, &date, &tv_offset);
209 if (unlikely(__tv_islt(&adjusted, &now))) {
210 goto fixup; /* jump in the past */
211 }
212
213 /* OK we did not jump backwards, let's see if we have jumped too far
214 * forwards. The poll value was in <max_wait>, we accept that plus
215 * MAX_DELAY_MS to cover additional time.
216 */
217 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
Willy Tarreau75590582009-03-05 14:54:50 +0100218 if (likely(__tv_islt(&adjusted, &deadline)))
219 goto to_ms; /* OK time is within expected range */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200220 fixup:
221 /* Large jump. If the poll was interrupted, we consider that the date
222 * has not changed (immediate wake-up), otherwise we add the poll
223 * time-out to the previous date. The new offset is recomputed.
224 */
Willy Tarreau75590582009-03-05 14:54:50 +0100225 _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait);
226
227 tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec;
228 tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200229 if (tv_offset.tv_usec < 0) {
230 tv_offset.tv_usec += 1000000;
231 tv_offset.tv_sec--;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200232 }
Christopher Faulet99aad922017-10-31 09:03:51 +0100233 TIMEVAL_TO_OFFSET(tv_offset, &offset);
Willy Tarreaue6313a32008-06-29 13:47:25 +0200234 to_ms:
Willy Tarreau75590582009-03-05 14:54:50 +0100235 now = adjusted;
236 curr_sec_ms = now.tv_usec / 1000; /* ms of current second */
Willy Tarreaueab777c2012-12-29 21:50:07 +0100237
238 /* For frequency counters, we'll need to know the ratio of the previous
239 * value to add to current value depending on the current millisecond.
240 * The principle is that during the first millisecond, we use 999/1000
241 * of the past value and that during the last millisecond we use 0/1000
242 * of the past value. In summary, we only use the past value during the
243 * first 999 ms of a second, and the last ms is used to complete the
244 * current measure. The value is scaled to (2^32-1) so that a simple
245 * multiply followed by a shift gives us the final value.
246 */
247 ms_left_scaled = (999U - curr_sec_ms) * 4294967U;
Willy Tarreau75590582009-03-05 14:54:50 +0100248 now_ms = now.tv_sec * 1000 + curr_sec_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200249 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200250}
251
Willy Tarreaud825eef2007-05-12 22:35:00 +0200252/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 * Local variables:
254 * c-indent-level: 8
255 * c-basic-offset: 8
256 * End:
257 */