blob: 1b0f72c45bd7e1ee0bb089f166da3b58a87a01d9 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreau75590582009-03-05 14:54:50 +01004 * Copyright 2000-2009 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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
Willy Tarreau75590582009-03-05 14:54:50 +010019unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
20unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */
Willy Tarreaue6313a32008-06-29 13:47:25 +020021unsigned int now_ms; /* internal date in milliseconds (may wrap) */
Willy Tarreaub7f694f2008-06-22 17:18:02 +020022struct timeval now; /* internal date is a monotonic function of real clock */
23struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020024struct timeval start_date; /* the process's start date */
25
26/*
27 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
28 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020029REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020030{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020031 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
32 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020033 while (tv->tv_usec >= 1000000) {
34 tv->tv_usec -= 1000000;
35 tv->tv_sec++;
36 }
37 return tv;
38}
39
40/*
41 * 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 +020042 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020043 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020044REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020045{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020046 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020047}
48
49/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 * 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 +020051 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020053REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020055 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020056}
57
58/*
59 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
60 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
61 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020062 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020063 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020064REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020065{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020066 return __tv_ms_le2(tv1, tv2);
67}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020068
Willy Tarreau42aae5c2007-04-29 17:43:56 +020069/*
70 * returns the remaining time between tv1=now and event=tv2
71 * if tv2 is passed, 0 is returned.
72 * Must not be used when either argument is eternity.
73 */
74REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
75{
76 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077}
78
79/*
80 * returns the remaining time between tv1=now and event=tv2
81 * if tv2 is passed, 0 is returned.
82 * Returns TIME_ETERNITY if tv2 is eternity.
83 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020084REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020085{
Willy Tarreaubaaee002006-06-26 02:48:02 +020086 if (tv_iseternity(tv2))
87 return TIME_ETERNITY;
88
Willy Tarreau42aae5c2007-04-29 17:43:56 +020089 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020090}
91
Willy Tarreaubaaee002006-06-26 02:48:02 +020092/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020093 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 * Must not be used when either argument is eternity.
95 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020096REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020097{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020098 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020099}
100
101/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200102 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
103 */
104REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
105{
106 return __tv_add(tv, from, inc);
107}
108
109/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200110 * If <inc> is set, then add it to <from> and set the result to <tv>, then
111 * return 1, otherwise return 0. It is meant to be used in if conditions.
112 */
113REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
114{
115 return __tv_add_ifset(tv, from, inc);
116}
117
118/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200119 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
120 * 0 is returned. The result is stored into tv.
121 */
122REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
123{
124 return __tv_remain(tv1, tv2, tv);
125}
126
127/*
128 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
129 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
130 * eternity.
131 */
132REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
133{
134 return __tv_remain2(tv1, tv2, tv);
135}
136
Willy Tarreau0481c202007-05-13 16:03:27 +0200137/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
138REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
139{
140 return __tv_isle(tv1, tv2);
141}
142
143/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
144REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
145{
146 return __tv_isgt(tv1, tv2);
147}
148
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200149/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
150 * close as possible to real time, following a monotonic function. The main
151 * principle consists in detecting backwards and forwards time jumps and adjust
152 * an offset to correct them. This function should be called once after each
153 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
154 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
155 * value means that we have not expired the timeout). Calling it with (-1,*)
156 * sets both <date> and <now> to current date, and calling it with (0,1) simply
157 * updates the values.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200158 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200159REGPRM2 void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200160{
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200161 static struct timeval tv_offset; /* warning: signed offset! */
162 struct timeval adjusted, deadline;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200163
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200164 gettimeofday(&date, NULL);
165 if (unlikely(max_wait < 0)) {
166 tv_zero(&tv_offset);
Willy Tarreau75590582009-03-05 14:54:50 +0100167 adjusted = date;
Willy Tarreaue6313a32008-06-29 13:47:25 +0200168 goto to_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200169 }
170 __tv_add(&adjusted, &date, &tv_offset);
171 if (unlikely(__tv_islt(&adjusted, &now))) {
172 goto fixup; /* jump in the past */
173 }
174
175 /* OK we did not jump backwards, let's see if we have jumped too far
176 * forwards. The poll value was in <max_wait>, we accept that plus
177 * MAX_DELAY_MS to cover additional time.
178 */
179 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
Willy Tarreau75590582009-03-05 14:54:50 +0100180 if (likely(__tv_islt(&adjusted, &deadline)))
181 goto to_ms; /* OK time is within expected range */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200182 fixup:
183 /* Large jump. If the poll was interrupted, we consider that the date
184 * has not changed (immediate wake-up), otherwise we add the poll
185 * time-out to the previous date. The new offset is recomputed.
186 */
Willy Tarreau75590582009-03-05 14:54:50 +0100187 _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait);
188
189 tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec;
190 tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200191 if (tv_offset.tv_usec < 0) {
192 tv_offset.tv_usec += 1000000;
193 tv_offset.tv_sec--;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200194 }
Willy Tarreaue6313a32008-06-29 13:47:25 +0200195 to_ms:
Willy Tarreau75590582009-03-05 14:54:50 +0100196 now = adjusted;
197 curr_sec_ms = now.tv_usec / 1000; /* ms of current second */
198 curr_sec_ms_scaled = curr_sec_ms * 4294971; /* ms * 2^32 / 1000 */
199 now_ms = now.tv_sec * 1000 + curr_sec_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200200 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200201}
202
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100203char *human_time(int t, short hz_div) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200204 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
205 char *p = rv;
206 int cnt=2; // print two numbers
207
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100208 if (unlikely(t < 0 || hz_div <= 0)) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200209 sprintf(p, "?");
210 return rv;
211 }
212
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100213 if (unlikely(hz_div > 1))
214 t /= hz_div;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200215
216 if (t >= DAY) {
217 p += sprintf(p, "%dd", t / DAY);
218 cnt--;
219 }
220
221 if (cnt && t % DAY / HOUR) {
222 p += sprintf(p, "%dh", t % DAY / HOUR);
223 cnt--;
224 }
225
226 if (cnt && t % HOUR / MINUTE) {
227 p += sprintf(p, "%dm", t % HOUR / MINUTE);
228 cnt--;
229 }
230
231 if ((cnt && t % MINUTE) || !t) // also display '0s'
232 p += sprintf(p, "%ds", t % MINUTE / SEC);
233
234 return rv;
235}
236
Willy Tarreaud825eef2007-05-12 22:35:00 +0200237/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238 * Local variables:
239 * c-indent-level: 8
240 * c-basic-offset: 8
241 * End:
242 */