blob: b404b258147628a9f969d5e57660284bf9c8efc4 [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
Willy Tarreaued72d822018-10-17 19:01:24 +020013#include <stdint.h>
14#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020016
17#include <common/config.h>
Willy Tarreau5e8f0662007-02-12 00:59:08 +010018#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020019#include <common/time.h>
Christopher Faulet99aad922017-10-31 09:03:51 +010020#include <common/hathreads.h>
Willy Tarreaued72d822018-10-17 19:01:24 +020021#include <types/global.h>
22#include <proto/freq_ctr.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020023
Christopher Faulet9a655712017-05-11 11:00:15 +020024THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
25THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */
26THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
27THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
28THREAD_LOCAL unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
29THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
30THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020031struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020032THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
33THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaued72d822018-10-17 19:01:24 +020034THREAD_LOCAL uint64_t prev_cpu_time = 0; /* previous per thread CPU time */
35THREAD_LOCAL uint64_t prev_mono_time = 0; /* previous system wide monotonic time */
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
Willy Tarreau9fefc512017-11-23 14:52:28 +010037static THREAD_LOCAL struct timeval tv_offset; /* per-thread time ofsset relative to global time */
38volatile unsigned long long global_now; /* common date between all threads (32:32) */
39
Willy Tarreaubaaee002006-06-26 02:48:02 +020040/*
41 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
42 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020043REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020044{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020045 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
46 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 while (tv->tv_usec >= 1000000) {
48 tv->tv_usec -= 1000000;
49 tv->tv_sec++;
50 }
51 return tv;
52}
53
54/*
55 * 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 +020056 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020058REGPRM2 int _tv_ms_cmp(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_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020061}
62
63/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 * 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 +020065 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020066 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020067REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020068{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020069 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020070}
71
72/*
73 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
74 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
75 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020076 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020077 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020078REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020079{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020080 return __tv_ms_le2(tv1, tv2);
81}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020082
Willy Tarreau42aae5c2007-04-29 17:43:56 +020083/*
84 * returns the remaining time between tv1=now and event=tv2
85 * if tv2 is passed, 0 is returned.
86 * Must not be used when either argument is eternity.
87 */
88REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
89{
90 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020091}
92
93/*
94 * returns the remaining time between tv1=now and event=tv2
95 * if tv2 is passed, 0 is returned.
96 * Returns TIME_ETERNITY if tv2 is eternity.
97 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020098REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020099{
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 if (tv_iseternity(tv2))
101 return TIME_ETERNITY;
102
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200103 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104}
105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200107 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 * Must not be used when either argument is eternity.
109 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200110REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200112 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113}
114
115/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
117 */
118REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
119{
120 return __tv_add(tv, from, inc);
121}
122
123/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200124 * If <inc> is set, then add it to <from> and set the result to <tv>, then
125 * return 1, otherwise return 0. It is meant to be used in if conditions.
126 */
127REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
128{
129 return __tv_add_ifset(tv, from, inc);
130}
131
132/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200133 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
134 * 0 is returned. The result is stored into tv.
135 */
136REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
137{
138 return __tv_remain(tv1, tv2, tv);
139}
140
141/*
142 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
143 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
144 * eternity.
145 */
146REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
147{
148 return __tv_remain2(tv1, tv2, tv);
149}
150
Willy Tarreau0481c202007-05-13 16:03:27 +0200151/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
152REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
153{
154 return __tv_isle(tv1, tv2);
155}
156
157/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
158REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
159{
160 return __tv_isgt(tv1, tv2);
161}
162
Willy Tarreau7649aac2017-11-23 11:52:55 +0100163/* tv_update_date: sets <date> to system time, and sets <now> to something as
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200164 * close as possible to real time, following a monotonic function. The main
165 * principle consists in detecting backwards and forwards time jumps and adjust
166 * an offset to correct them. This function should be called once after each
167 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
168 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
169 * value means that we have not expired the timeout). Calling it with (-1,*)
170 * sets both <date> and <now> to current date, and calling it with (0,1) simply
171 * updates the values.
Christopher Faulet99aad922017-10-31 09:03:51 +0100172 *
Willy Tarreau7649aac2017-11-23 11:52:55 +0100173 * An offset is used to adjust the current time (date), to have a monotonic time
Christopher Faulet99aad922017-10-31 09:03:51 +0100174 * (now). It must be global and thread-safe. But a timeval cannot be atomically
Willy Tarreau7649aac2017-11-23 11:52:55 +0100175 * updated. So instead, we store it in a 64-bits integer (offset) whose 32 MSB
176 * contain the signed seconds adjustment andthe 32 LSB contain the unsigned
177 * microsecond adjustment. We cannot use a timeval for this since it's never
178 * clearly specified whether a timeval may hold negative values or not.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200179 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200180REGPRM2 void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200181{
Willy Tarreaua3315442018-02-05 20:11:38 +0100182 struct timeval adjusted, deadline, tmp_now, tmp_adj;
Willy Tarreau351b3a12017-03-29 15:24:33 +0200183 unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
Willy Tarreau9fefc512017-11-23 14:52:28 +0100184 unsigned long long old_now;
185 unsigned long long new_now;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200186
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200187 gettimeofday(&date, NULL);
188 if (unlikely(max_wait < 0)) {
Willy Tarreau9fefc512017-11-23 14:52:28 +0100189 tv_zero(&tv_offset);
Willy Tarreau75590582009-03-05 14:54:50 +0100190 adjusted = date;
Willy Tarreau45a12512011-09-10 16:56:42 +0200191 after_poll = date;
192 samp_time = idle_time = 0;
193 idle_pct = 100;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100194 global_now = (((unsigned long long)adjusted.tv_sec) << 32) +
195 (unsigned int)adjusted.tv_usec;
Willy Tarreaue6313a32008-06-29 13:47:25 +0200196 goto to_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200197 }
Willy Tarreau7649aac2017-11-23 11:52:55 +0100198
Willy Tarreau9fefc512017-11-23 14:52:28 +0100199 __tv_add(&adjusted, &date, &tv_offset);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100200
Willy Tarreau9fefc512017-11-23 14:52:28 +0100201 /* compute the minimum and maximum local date we may have reached based
202 * on our past date and the associated timeout.
203 */
204 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100205
Willy Tarreau9fefc512017-11-23 14:52:28 +0100206 if (unlikely(__tv_islt(&adjusted, &now) || __tv_islt(&deadline, &adjusted))) {
207 /* Large jump. If the poll was interrupted, we consider that the
208 * date has not changed (immediate wake-up), otherwise we add
209 * the poll time-out to the previous date. The new offset is
210 * recomputed.
211 */
212 _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200213 }
214
Willy Tarreau9fefc512017-11-23 14:52:28 +0100215 /* now that we have bounded the local time, let's check if it's
216 * realistic regarding the global date, which only moves forward,
217 * otherwise catch up.
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200218 */
Willy Tarreau9fefc512017-11-23 14:52:28 +0100219 old_now = global_now;
Willy Tarreau75590582009-03-05 14:54:50 +0100220
Willy Tarreau9fefc512017-11-23 14:52:28 +0100221 do {
222 tmp_now.tv_sec = (unsigned int)(old_now >> 32);
223 tmp_now.tv_usec = old_now & 0xFFFFFFFFU;
Willy Tarreaua3315442018-02-05 20:11:38 +0100224 tmp_adj = adjusted;
Willy Tarreau7649aac2017-11-23 11:52:55 +0100225
Willy Tarreaua3315442018-02-05 20:11:38 +0100226 if (__tv_islt(&tmp_adj, &tmp_now))
227 tmp_adj = tmp_now;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100228
229 /* now <adjusted> is expected to be the most accurate date,
230 * equal to <global_now> or newer.
231 */
Willy Tarreaua3315442018-02-05 20:11:38 +0100232 new_now = (((unsigned long long)tmp_adj.tv_sec) << 32) + (unsigned int)tmp_adj.tv_usec;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100233
234 /* let's try to update the global <now> or loop again */
235 } while (!HA_ATOMIC_CAS(&global_now, &old_now, new_now));
236
Willy Tarreaua3315442018-02-05 20:11:38 +0100237 adjusted = tmp_adj;
238
Willy Tarreau9fefc512017-11-23 14:52:28 +0100239 /* the new global date when we looked was old_now, and the new one is
240 * new_now == adjusted. We can recompute our local offset.
241 */
242 tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec;
243 tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec;
244 if (tv_offset.tv_usec < 0) {
245 tv_offset.tv_usec += 1000000;
246 tv_offset.tv_sec--;
247 }
Willy Tarreau7649aac2017-11-23 11:52:55 +0100248
Willy Tarreaue6313a32008-06-29 13:47:25 +0200249 to_ms:
Willy Tarreau75590582009-03-05 14:54:50 +0100250 now = adjusted;
251 curr_sec_ms = now.tv_usec / 1000; /* ms of current second */
Willy Tarreaueab777c2012-12-29 21:50:07 +0100252
253 /* For frequency counters, we'll need to know the ratio of the previous
254 * value to add to current value depending on the current millisecond.
255 * The principle is that during the first millisecond, we use 999/1000
256 * of the past value and that during the last millisecond we use 0/1000
257 * of the past value. In summary, we only use the past value during the
258 * first 999 ms of a second, and the last ms is used to complete the
259 * current measure. The value is scaled to (2^32-1) so that a simple
260 * multiply followed by a shift gives us the final value.
261 */
262 ms_left_scaled = (999U - curr_sec_ms) * 4294967U;
Willy Tarreau75590582009-03-05 14:54:50 +0100263 now_ms = now.tv_sec * 1000 + curr_sec_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200264 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200265}
266
Willy Tarreaued72d822018-10-17 19:01:24 +0200267/* Updates the current thread's statistics about stolen CPU time. The unit for
268 * <stolen> is half-milliseconds.
269 */
270REGPRM1 void report_stolen_time(uint64_t stolen)
271{
272 activity[tid].cpust_total += stolen;
273 update_freq_ctr(&activity[tid].cpust_1s, stolen);
274 update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen);
275}
276
Willy Tarreaud825eef2007-05-12 22:35:00 +0200277/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 * Local variables:
279 * c-indent-level: 8
280 * c-basic-offset: 8
281 * End:
282 */