MINOR: clock: do not use now.tv_sec anymore

Instead we're using ns_to_sec(tv_to_ns(&now)) which allows the tv_sec
part to disappear. At this point, "now" is only used as a timeval in
clock.c where it is updated.
This commit is contained in:
Willy Tarreau 2023-04-28 07:39:44 +02:00
parent e8e4712771
commit eed5da1037
18 changed files with 43 additions and 42 deletions

View File

@ -28,6 +28,7 @@
#include <haproxy/proxy-t.h> #include <haproxy/proxy-t.h>
#include <haproxy/server-t.h> #include <haproxy/server-t.h>
#include <haproxy/stream-t.h> #include <haproxy/stream-t.h>
#include <haproxy/time.h>
int assign_server(struct stream *s); int assign_server(struct stream *s);
int assign_server_address(struct stream *s); int assign_server_address(struct stream *s);
@ -64,7 +65,7 @@ static inline int be_usable_srv(struct proxy *be)
/* set the time of last session on the backend */ /* set the time of last session on the backend */
static inline void be_set_sess_last(struct proxy *be) static inline void be_set_sess_last(struct proxy *be)
{ {
be->be_counters.last_sess = now.tv_sec; be->be_counters.last_sess = ns_to_sec(tv_to_ns(&now));
} }
/* This function returns non-zero if the designated server will be /* This function returns non-zero if the designated server will be

View File

@ -183,7 +183,7 @@ static inline void srv_inc_sess_ctr(struct server *s)
/* set the time of last session on the designated server */ /* set the time of last session on the designated server */
static inline void srv_set_sess_last(struct server *s) static inline void srv_set_sess_last(struct server *s)
{ {
s->counters.last_sess = now.tv_sec; s->counters.last_sess = ns_to_sec(tv_to_ns(&now));
} }
/* returns the current server throttle rate between 0 and 100% */ /* returns the current server throttle rate between 0 and 100% */

View File

@ -64,7 +64,7 @@
int be_lastsession(const struct proxy *be) int be_lastsession(const struct proxy *be)
{ {
if (be->be_counters.last_sess) if (be->be_counters.last_sess)
return now.tv_sec - be->be_counters.last_sess; return ns_to_sec(tv_to_ns(&now)) - be->be_counters.last_sess;
return -1; return -1;
} }
@ -2505,7 +2505,7 @@ void back_handle_st_rdy(struct stream *s)
*/ */
void set_backend_down(struct proxy *be) void set_backend_down(struct proxy *be)
{ {
be->last_change = now.tv_sec; be->last_change = ns_to_sec(tv_to_ns(&now));
_HA_ATOMIC_INC(&be->down_trans); _HA_ATOMIC_INC(&be->down_trans);
if (!(global.mode & MODE_STARTING)) { if (!(global.mode & MODE_STARTING)) {
@ -2578,10 +2578,10 @@ int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit)
} }
int be_downtime(struct proxy *px) { int be_downtime(struct proxy *px) {
if (px->lbprm.tot_weight && px->last_change < now.tv_sec) // ignore negative time if (px->lbprm.tot_weight && px->last_change < ns_to_sec(tv_to_ns(&now))) // ignore negative time
return px->down_time; return px->down_time;
return now.tv_sec - px->last_change + px->down_time; return ns_to_sec(tv_to_ns(&now)) - px->last_change + px->down_time;
} }
/* /*

View File

@ -660,7 +660,7 @@ static struct peer *cfg_peers_add_peer(struct peers *peers,
peers->remote = p; peers->remote = p;
p->conf.file = strdup(file); p->conf.file = strdup(file);
p->conf.line = linenum; p->conf.line = linenum;
p->last_change = now.tv_sec; p->last_change = ns_to_sec(tv_to_ns(&now));
p->xprt = xprt_get(XPRT_RAW); p->xprt = xprt_get(XPRT_RAW);
p->sock_init_arg = NULL; p->sock_init_arg = NULL;
HA_SPIN_INIT(&p->lock); HA_SPIN_INIT(&p->lock);
@ -838,7 +838,7 @@ int cfg_parse_peers(const char *file, int linenum, char **args, int kwm)
cfg_peers = curpeers; cfg_peers = curpeers;
curpeers->conf.file = strdup(file); curpeers->conf.file = strdup(file);
curpeers->conf.line = linenum; curpeers->conf.line = linenum;
curpeers->last_change = now.tv_sec; curpeers->last_change = ns_to_sec(tv_to_ns(&now));
curpeers->id = strdup(args[1]); curpeers->id = strdup(args[1]);
curpeers->disabled = 0; curpeers->disabled = 0;
} }

View File

@ -1029,9 +1029,9 @@ int httpchk_build_status_header(struct server *s, struct buffer *buf)
s->queue.length); s->queue.length);
if ((s->cur_state == SRV_ST_STARTING) && if ((s->cur_state == SRV_ST_STARTING) &&
now.tv_sec < s->last_change + s->slowstart && ns_to_sec(tv_to_ns(&now)) < s->last_change + s->slowstart &&
now.tv_sec >= s->last_change) { ns_to_sec(tv_to_ns(&now)) >= s->last_change) {
ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); ratio = MAX(1, 100 * (ns_to_sec(tv_to_ns(&now)) - s->last_change) / s->slowstart);
chunk_appendf(buf, "; throttle=%d%%", ratio); chunk_appendf(buf, "; throttle=%d%%", ratio);
} }

View File

@ -454,7 +454,7 @@ static struct proxy *cli_alloc_fe(const char *name, const char *file, int line)
init_new_proxy(fe); init_new_proxy(fe);
fe->next = proxies_list; fe->next = proxies_list;
proxies_list = fe; proxies_list = fe;
fe->last_change = now.tv_sec; fe->last_change = ns_to_sec(tv_to_ns(&now));
fe->id = strdup("GLOBAL"); fe->id = strdup("GLOBAL");
fe->cap = PR_CAP_FE|PR_CAP_INT; fe->cap = PR_CAP_FE|PR_CAP_INT;
fe->maxconn = 10; /* default to 10 concurrent connections */ fe->maxconn = 10; /* default to 10 concurrent connections */

View File

@ -3019,7 +3019,7 @@ spoe_init(struct proxy *px, struct flt_conf *fconf)
/* conf->agent_fe was already initialized during the config /* conf->agent_fe was already initialized during the config
* parsing. Finish initialization. */ * parsing. Finish initialization. */
conf->agent_fe.last_change = now.tv_sec; conf->agent_fe.last_change = ns_to_sec(tv_to_ns(&now));
conf->agent_fe.cap = PR_CAP_FE; conf->agent_fe.cap = PR_CAP_FE;
conf->agent_fe.mode = PR_MODE_TCP; conf->agent_fe.mode = PR_MODE_TCP;
conf->agent_fe.maxconn = 0; conf->agent_fe.maxconn = 0;

View File

@ -3760,7 +3760,7 @@ int cfg_parse_log_forward(const char *file, int linenum, char **args, int kwm)
px->conf.file = strdup(file); px->conf.file = strdup(file);
px->conf.line = linenum; px->conf.line = linenum;
px->mode = PR_MODE_SYSLOG; px->mode = PR_MODE_SYSLOG;
px->last_change = now.tv_sec; px->last_change = ns_to_sec(tv_to_ns(&now));
px->cap = PR_CAP_FE; px->cap = PR_CAP_FE;
px->maxconn = 10; px->maxconn = 10;
px->timeout.client = TICK_ETERNITY; px->timeout.client = TICK_ETERNITY;

View File

@ -79,7 +79,7 @@ int mworker_ext_launch_all()
continue; continue;
} }
child->timestamp = now.tv_sec; child->timestamp = ns_to_sec(tv_to_ns(&now));
ret = fork(); ret = fork();
if (ret < 0) { if (ret < 0) {

View File

@ -3247,7 +3247,7 @@ static void peer_session_forceshutdown(struct peer *peer)
/* Pre-configures a peers frontend to accept incoming connections */ /* Pre-configures a peers frontend to accept incoming connections */
void peers_setup_frontend(struct proxy *fe) void peers_setup_frontend(struct proxy *fe)
{ {
fe->last_change = now.tv_sec; fe->last_change = ns_to_sec(tv_to_ns(&now));
fe->cap = PR_CAP_FE | PR_CAP_BE; fe->cap = PR_CAP_FE | PR_CAP_BE;
fe->mode = PR_MODE_PEERS; fe->mode = PR_MODE_PEERS;
fe->maxconn = 0; fe->maxconn = 0;

View File

@ -1650,7 +1650,7 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
} }
init_new_proxy(curproxy); init_new_proxy(curproxy);
curproxy->last_change = now.tv_sec; curproxy->last_change = ns_to_sec(tv_to_ns(&now));
curproxy->id = strdup(name); curproxy->id = strdup(name);
curproxy->cap = cap; curproxy->cap = cap;
@ -2839,7 +2839,7 @@ static int dump_servers_state(struct stconn *sc)
dump_server_addr(&srv->check.addr, srv_check_addr); dump_server_addr(&srv->check.addr, srv_check_addr);
dump_server_addr(&srv->agent.addr, srv_agent_addr); dump_server_addr(&srv->agent.addr, srv_agent_addr);
srv_time_since_last_change = now.tv_sec - srv->last_change; srv_time_since_last_change = ns_to_sec(tv_to_ns(&now)) - srv->last_change;
bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0; bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0; srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;

View File

@ -114,10 +114,10 @@ unsigned int srv_dynamic_maxconn(const struct server *s)
s->proxy->beconn * s->maxconn / s->proxy->fullconn); s->proxy->beconn * s->maxconn / s->proxy->fullconn);
if ((s->cur_state == SRV_ST_STARTING) && if ((s->cur_state == SRV_ST_STARTING) &&
now.tv_sec < s->last_change + s->slowstart && ns_to_sec(tv_to_ns(&now)) < s->last_change + s->slowstart &&
now.tv_sec >= s->last_change) { ns_to_sec(tv_to_ns(&now)) >= s->last_change) {
unsigned int ratio; unsigned int ratio;
ratio = 100 * (now.tv_sec - s->last_change) / s->slowstart; ratio = 100 * (ns_to_sec(tv_to_ns(&now)) - s->last_change) / s->slowstart;
max = MAX(1, max * ratio / 100); max = MAX(1, max * ratio / 100);
} }
return max; return max;

View File

@ -3234,7 +3234,7 @@ int check_action_do_resolve(struct act_rule *rule, struct proxy *px, char **err)
void resolvers_setup_proxy(struct proxy *px) void resolvers_setup_proxy(struct proxy *px)
{ {
px->last_change = now.tv_sec; px->last_change = ns_to_sec(tv_to_ns(&now));
px->cap = PR_CAP_FE | PR_CAP_BE; px->cap = PR_CAP_FE | PR_CAP_BE;
px->maxconn = 0; px->maxconn = 0;
px->conn_retries = 1; px->conn_retries = 1;

View File

@ -138,16 +138,16 @@ const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause)
int srv_downtime(const struct server *s) int srv_downtime(const struct server *s)
{ {
if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= ns_to_sec(tv_to_ns(&now))) // ignore negative time
return s->down_time; return s->down_time;
return now.tv_sec - s->last_change + s->down_time; return ns_to_sec(tv_to_ns(&now)) - s->last_change + s->down_time;
} }
int srv_lastsession(const struct server *s) int srv_lastsession(const struct server *s)
{ {
if (s->counters.last_sess) if (s->counters.last_sess)
return now.tv_sec - s->counters.last_sess; return ns_to_sec(tv_to_ns(&now)) - s->counters.last_sess;
return -1; return -1;
} }
@ -1867,7 +1867,7 @@ void server_recalc_eweight(struct server *sv, int must_update)
struct proxy *px = sv->proxy; struct proxy *px = sv->proxy;
unsigned w; unsigned w;
if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) { if (ns_to_sec(tv_to_ns(&now)) < sv->last_change || ns_to_sec(tv_to_ns(&now)) >= sv->last_change + sv->slowstart) {
/* go to full throttle if the slowstart interval is reached */ /* go to full throttle if the slowstart interval is reached */
if (sv->next_state == SRV_ST_STARTING) if (sv->next_state == SRV_ST_STARTING)
sv->next_state = SRV_ST_RUNNING; sv->next_state = SRV_ST_RUNNING;
@ -1877,7 +1877,7 @@ void server_recalc_eweight(struct server *sv, int must_update)
* It must also start immediately, at least at the minimal step when leaving maintenance. * It must also start immediately, at least at the minimal step when leaving maintenance.
*/ */
if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN)) if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart; w = (px->lbprm.wdiv * (ns_to_sec(tv_to_ns(&now)) - sv->last_change) + sv->slowstart) / sv->slowstart;
else else
w = px->lbprm.wdiv; w = px->lbprm.wdiv;
@ -2358,7 +2358,7 @@ struct server *new_server(struct proxy *proxy)
event_hdl_sub_list_init(&srv->e_subs); event_hdl_sub_list_init(&srv->e_subs);
srv->next_state = SRV_ST_RUNNING; /* early server setup */ srv->next_state = SRV_ST_RUNNING; /* early server setup */
srv->last_change = now.tv_sec; srv->last_change = ns_to_sec(tv_to_ns(&now));
srv->check.obj_type = OBJ_TYPE_CHECK; srv->check.obj_type = OBJ_TYPE_CHECK;
srv->check.status = HCHK_STATUS_INI; srv->check.status = HCHK_STATUS_INI;
@ -4685,7 +4685,7 @@ static int init_srv_slowstart(struct server *srv)
if (srv->next_state == SRV_ST_STARTING) { if (srv->next_state == SRV_ST_STARTING) {
task_schedule(srv->warmup, task_schedule(srv->warmup,
tick_add(now_ms, tick_add(now_ms,
MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20))); MS_TO_TICKS(MAX(1000, (ns_to_sec(tv_to_ns(&now)) - srv->last_change)) / 20)));
} }
} }
@ -5752,14 +5752,14 @@ static void srv_update_status(struct server *s, int type, int cause)
if (srv_prev_state != s->cur_state) { if (srv_prev_state != s->cur_state) {
if (srv_prev_state == SRV_ST_STOPPED) { if (srv_prev_state == SRV_ST_STOPPED) {
/* server was down and no longer is */ /* server was down and no longer is */
if (s->last_change < now.tv_sec) // ignore negative times if (s->last_change < ns_to_sec(tv_to_ns(&now))) // ignore negative times
s->down_time += now.tv_sec - s->last_change; s->down_time += ns_to_sec(tv_to_ns(&now)) - s->last_change;
} }
else if (s->cur_state == SRV_ST_STOPPED) { else if (s->cur_state == SRV_ST_STOPPED) {
/* server was up and is currently down */ /* server was up and is currently down */
s->counters.down_trans++; s->counters.down_trans++;
} }
s->last_change = now.tv_sec; s->last_change = ns_to_sec(tv_to_ns(&now));
} }
/* check if backend stats must be updated due to the server state change */ /* check if backend stats must be updated due to the server state change */
@ -5769,9 +5769,9 @@ static void srv_update_status(struct server *s, int type, int cause)
/* backend was down and is back up again: /* backend was down and is back up again:
* no helper function, updating last_change and backend downtime stats * no helper function, updating last_change and backend downtime stats
*/ */
if (s->proxy->last_change < now.tv_sec) // ignore negative times if (s->proxy->last_change < ns_to_sec(tv_to_ns(&now))) // ignore negative times
s->proxy->down_time += now.tv_sec - s->proxy->last_change; s->proxy->down_time += ns_to_sec(tv_to_ns(&now)) - s->proxy->last_change;
s->proxy->last_change = now.tv_sec; s->proxy->last_change = ns_to_sec(tv_to_ns(&now));
} }
} }

View File

@ -321,7 +321,7 @@ static void srv_state_srv_update(struct server *srv, int version, char **params)
srv_adm_set_drain(srv); srv_adm_set_drain(srv);
} }
srv->last_change = now.tv_sec - srv_last_time_change; srv->last_change = ns_to_sec(tv_to_ns(&now)) - srv_last_time_change;
srv->check.status = srv_check_status; srv->check.status = srv_check_status;
srv->check.result = srv_check_result; srv->check.result = srv_check_result;

View File

@ -289,7 +289,7 @@ static int cli_parse_show_events(char **args, char *payload, struct appctx *appc
/* Pre-configures a ring proxy to emit connections */ /* Pre-configures a ring proxy to emit connections */
void sink_setup_proxy(struct proxy *px) void sink_setup_proxy(struct proxy *px)
{ {
px->last_change = now.tv_sec; px->last_change = ns_to_sec(tv_to_ns(&now));
px->cap = PR_CAP_BE; px->cap = PR_CAP_BE;
px->maxconn = 0; px->maxconn = 0;
px->conn_retries = 1; px->conn_retries = 1;

View File

@ -2345,7 +2345,7 @@ int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
metric = mkf_str(FO_STATUS, fld_status); metric = mkf_str(FO_STATUS, fld_status);
break; break;
case ST_F_LASTCHG: case ST_F_LASTCHG:
metric = mkf_u32(FN_AGE, now.tv_sec - sv->last_change); metric = mkf_u32(FN_AGE, ns_to_sec(tv_to_ns(&now)) - sv->last_change);
break; break;
case ST_F_WEIGHT: case ST_F_WEIGHT:
metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv); metric = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
@ -2800,7 +2800,7 @@ int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int le
metric = mkf_u64(FN_COUNTER, px->down_trans); metric = mkf_u64(FN_COUNTER, px->down_trans);
break; break;
case ST_F_LASTCHG: case ST_F_LASTCHG:
metric = mkf_u32(FN_AGE, now.tv_sec - px->last_change); metric = mkf_u32(FN_AGE, ns_to_sec(tv_to_ns(&now)) - px->last_change);
break; break;
case ST_F_DOWNTIME: case ST_F_DOWNTIME:
if (px->srv) if (px->srv)
@ -3555,7 +3555,7 @@ static void stats_dump_html_info(struct stconn *sc, struct uri_auth *uri)
{ {
struct appctx *appctx = __sc_appctx(sc); struct appctx *appctx = __sc_appctx(sc);
struct show_stat_ctx *ctx = appctx->svcctx; struct show_stat_ctx *ctx = appctx->svcctx;
unsigned int up = (now.tv_sec - start_time.tv_sec); unsigned int up = (ns_to_sec(tv_to_ns(&now)) - start_time.tv_sec);
char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN]; char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
const char *scope_ptr = stats_scope_ptr(appctx, sc); const char *scope_ptr = stats_scope_ptr(appctx, sc);
unsigned long long bps; unsigned long long bps;

View File

@ -3372,7 +3372,7 @@ static int stats_dump_full_strm_to_buffer(struct stconn *sc, struct stream *strm
chunk_appendf(&trash, chunk_appendf(&trash,
" age=%s)\n", " age=%s)\n",
human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1)); human_time(ns_to_sec(tv_to_ns(&now)) - strm->logs.accept_date.tv_sec, 1));
if (strm->txn) if (strm->txn)
chunk_appendf(&trash, chunk_appendf(&trash,
@ -3699,7 +3699,7 @@ static int cli_io_handler_dump_sess(struct appctx *appctx)
chunk_appendf(&trash, chunk_appendf(&trash,
" ts=%02x epoch=%#x age=%s calls=%u rate=%u cpu=%llu lat=%llu", " ts=%02x epoch=%#x age=%s calls=%u rate=%u cpu=%llu lat=%llu",
curr_strm->task->state, curr_strm->stream_epoch, curr_strm->task->state, curr_strm->stream_epoch,
human_time(now.tv_sec - ns_to_sec(curr_strm->logs.accept_ts), 1), human_time(ns_to_sec(tv_to_ns(&now)) - ns_to_sec(curr_strm->logs.accept_ts), 1),
curr_strm->task->calls, read_freq_ctr(&curr_strm->call_rate), curr_strm->task->calls, read_freq_ctr(&curr_strm->call_rate),
(unsigned long long)curr_strm->cpu_time, (unsigned long long)curr_strm->lat_time); (unsigned long long)curr_strm->cpu_time, (unsigned long long)curr_strm->lat_time);