From: Willy Tarreau Date: Fri, 28 Apr 2023 05:39:44 +0000 (+0200) Subject: MINOR: clock: do not use now.tv_sec anymore X-Git-Tag: v2.8-dev9~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eed5da10371b756e095c5e814d8943657013271c;p=thirdparty%2Fhaproxy.git 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. --- diff --git a/include/haproxy/backend.h b/include/haproxy/backend.h index e0ec1e4018..d0bcdf1f30 100644 --- a/include/haproxy/backend.h +++ b/include/haproxy/backend.h @@ -28,6 +28,7 @@ #include #include #include +#include int assign_server(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 */ 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 diff --git a/include/haproxy/server.h b/include/haproxy/server.h index 96c7a04bcb..1b5dffb15e 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -183,7 +183,7 @@ static inline void srv_inc_sess_ctr(struct server *s) /* set the time of last session on the designated server */ 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% */ diff --git a/src/backend.c b/src/backend.c index 7b8e12d3f3..b28b531928 100644 --- a/src/backend.c +++ b/src/backend.c @@ -64,7 +64,7 @@ int be_lastsession(const struct proxy *be) { 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; } @@ -2505,7 +2505,7 @@ void back_handle_st_rdy(struct stream *s) */ 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); if (!(global.mode & MODE_STARTING)) { @@ -2578,10 +2578,10 @@ no_cookie: } 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 now.tv_sec - px->last_change + px->down_time; + return ns_to_sec(tv_to_ns(&now)) - px->last_change + px->down_time; } /* diff --git a/src/cfgparse.c b/src/cfgparse.c index 2472a21598..f2ed7e6f53 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -660,7 +660,7 @@ static struct peer *cfg_peers_add_peer(struct peers *peers, peers->remote = p; p->conf.file = strdup(file); 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->sock_init_arg = NULL; 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; curpeers->conf.file = strdup(file); 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->disabled = 0; } diff --git a/src/check.c b/src/check.c index 1d67dfd49e..5dd3b89e52 100644 --- a/src/check.c +++ b/src/check.c @@ -1029,9 +1029,9 @@ int httpchk_build_status_header(struct server *s, struct buffer *buf) s->queue.length); if ((s->cur_state == SRV_ST_STARTING) && - now.tv_sec < s->last_change + s->slowstart && - now.tv_sec >= s->last_change) { - ratio = MAX(1, 100 * (now.tv_sec - s->last_change) / s->slowstart); + ns_to_sec(tv_to_ns(&now)) < s->last_change + s->slowstart && + ns_to_sec(tv_to_ns(&now)) >= s->last_change) { + ratio = MAX(1, 100 * (ns_to_sec(tv_to_ns(&now)) - s->last_change) / s->slowstart); chunk_appendf(buf, "; throttle=%d%%", ratio); } diff --git a/src/cli.c b/src/cli.c index 5af4804fd3..1b440b4495 100644 --- a/src/cli.c +++ b/src/cli.c @@ -454,7 +454,7 @@ static struct proxy *cli_alloc_fe(const char *name, const char *file, int line) init_new_proxy(fe); fe->next = proxies_list; proxies_list = fe; - fe->last_change = now.tv_sec; + fe->last_change = ns_to_sec(tv_to_ns(&now)); fe->id = strdup("GLOBAL"); fe->cap = PR_CAP_FE|PR_CAP_INT; fe->maxconn = 10; /* default to 10 concurrent connections */ diff --git a/src/flt_spoe.c b/src/flt_spoe.c index 71c10a93a9..b53f654697 100644 --- a/src/flt_spoe.c +++ b/src/flt_spoe.c @@ -3019,7 +3019,7 @@ spoe_init(struct proxy *px, struct flt_conf *fconf) /* conf->agent_fe was already initialized during the config * 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.mode = PR_MODE_TCP; conf->agent_fe.maxconn = 0; diff --git a/src/log.c b/src/log.c index 60c7daad85..82a4fa288f 100644 --- a/src/log.c +++ b/src/log.c @@ -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.line = linenum; 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->maxconn = 10; px->timeout.client = TICK_ETERNITY; diff --git a/src/mworker-prog.c b/src/mworker-prog.c index 502c54991e..1909d4a481 100644 --- a/src/mworker-prog.c +++ b/src/mworker-prog.c @@ -79,7 +79,7 @@ int mworker_ext_launch_all() continue; } - child->timestamp = now.tv_sec; + child->timestamp = ns_to_sec(tv_to_ns(&now)); ret = fork(); if (ret < 0) { diff --git a/src/peers.c b/src/peers.c index 725f704c23..f9c3ed8b42 100644 --- a/src/peers.c +++ b/src/peers.c @@ -3247,7 +3247,7 @@ static void peer_session_forceshutdown(struct peer *peer) /* Pre-configures a peers frontend to accept incoming connections */ 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->mode = PR_MODE_PEERS; fe->maxconn = 0; diff --git a/src/proxy.c b/src/proxy.c index f2eb6a8e05..3a3bb30b19 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1650,7 +1650,7 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg) } 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->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->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; srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0; diff --git a/src/queue.c b/src/queue.c index 73bd3c26ee..f6044c5a32 100644 --- a/src/queue.c +++ b/src/queue.c @@ -114,10 +114,10 @@ unsigned int srv_dynamic_maxconn(const struct server *s) s->proxy->beconn * s->maxconn / s->proxy->fullconn); if ((s->cur_state == SRV_ST_STARTING) && - now.tv_sec < s->last_change + s->slowstart && - now.tv_sec >= s->last_change) { + ns_to_sec(tv_to_ns(&now)) < s->last_change + s->slowstart && + ns_to_sec(tv_to_ns(&now)) >= s->last_change) { 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); } return max; diff --git a/src/resolvers.c b/src/resolvers.c index ac0b0158f3..443a6883cc 100644 --- a/src/resolvers.c +++ b/src/resolvers.c @@ -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) { - 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->maxconn = 0; px->conn_retries = 1; diff --git a/src/server.c b/src/server.c index 8172f8f530..f8162b1a06 100644 --- a/src/server.c +++ b/src/server.c @@ -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) { - 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 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) { 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; } @@ -1867,7 +1867,7 @@ void server_recalc_eweight(struct server *sv, int must_update) struct proxy *px = sv->proxy; 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 */ if (sv->next_state == SRV_ST_STARTING) 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. */ 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 w = px->lbprm.wdiv; @@ -2358,7 +2358,7 @@ struct server *new_server(struct proxy *proxy) event_hdl_sub_list_init(&srv->e_subs); 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.status = HCHK_STATUS_INI; @@ -4685,7 +4685,7 @@ static int init_srv_slowstart(struct server *srv) if (srv->next_state == SRV_ST_STARTING) { task_schedule(srv->warmup, 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 == SRV_ST_STOPPED) { /* server was down and no longer is */ - if (s->last_change < now.tv_sec) // ignore negative times - s->down_time += now.tv_sec - s->last_change; + if (s->last_change < ns_to_sec(tv_to_ns(&now))) // ignore negative times + s->down_time += ns_to_sec(tv_to_ns(&now)) - s->last_change; } else if (s->cur_state == SRV_ST_STOPPED) { /* server was up and is currently down */ 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 */ @@ -5769,9 +5769,9 @@ static void srv_update_status(struct server *s, int type, int cause) /* backend was down and is back up again: * no helper function, updating last_change and backend downtime stats */ - if (s->proxy->last_change < now.tv_sec) // ignore negative times - s->proxy->down_time += now.tv_sec - s->proxy->last_change; - s->proxy->last_change = now.tv_sec; + if (s->proxy->last_change < ns_to_sec(tv_to_ns(&now))) // ignore negative times + s->proxy->down_time += ns_to_sec(tv_to_ns(&now)) - s->proxy->last_change; + s->proxy->last_change = ns_to_sec(tv_to_ns(&now)); } } diff --git a/src/server_state.c b/src/server_state.c index 677de88054..da5b102208 100644 --- a/src/server_state.c +++ b/src/server_state.c @@ -321,7 +321,7 @@ static void srv_state_srv_update(struct server *srv, int version, char **params) 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.result = srv_check_result; diff --git a/src/sink.c b/src/sink.c index af0e89171d..b10ea2b7ef 100644 --- a/src/sink.c +++ b/src/sink.c @@ -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 */ 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->maxconn = 0; px->conn_retries = 1; diff --git a/src/stats.c b/src/stats.c index d673cee396..9979c7b4a6 100644 --- a/src/stats.c +++ b/src/stats.c @@ -2345,7 +2345,7 @@ int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags, metric = mkf_str(FO_STATUS, fld_status); break; 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; case ST_F_WEIGHT: 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); break; 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; case ST_F_DOWNTIME: 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 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]; const char *scope_ptr = stats_scope_ptr(appctx, sc); unsigned long long bps; diff --git a/src/stream.c b/src/stream.c index 82a06ed766..4e0ece3dff 100644 --- a/src/stream.c +++ b/src/stream.c @@ -3372,7 +3372,7 @@ static int stats_dump_full_strm_to_buffer(struct stconn *sc, struct stream *strm chunk_appendf(&trash, " 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) chunk_appendf(&trash, @@ -3699,7 +3699,7 @@ static int cli_io_handler_dump_sess(struct appctx *appctx) chunk_appendf(&trash, " ts=%02x epoch=%#x age=%s calls=%u rate=%u cpu=%llu lat=%llu", 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), (unsigned long long)curr_strm->cpu_time, (unsigned long long)curr_strm->lat_time);