]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: clock: do not use now.tv_sec anymore
authorWilly Tarreau <w@1wt.eu>
Fri, 28 Apr 2023 05:39:44 +0000 (07:39 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 28 Apr 2023 14:08:08 +0000 (16:08 +0200)
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.

18 files changed:
include/haproxy/backend.h
include/haproxy/server.h
src/backend.c
src/cfgparse.c
src/check.c
src/cli.c
src/flt_spoe.c
src/log.c
src/mworker-prog.c
src/peers.c
src/proxy.c
src/queue.c
src/resolvers.c
src/server.c
src/server_state.c
src/sink.c
src/stats.c
src/stream.c

index e0ec1e4018bd1fa3fe0bae00a4eb00ba3849c6af..d0bcdf1f30be9cdb6fcfec8f03b55230e2e6dc57 100644 (file)
@@ -28,6 +28,7 @@
 #include <haproxy/proxy-t.h>
 #include <haproxy/server-t.h>
 #include <haproxy/stream-t.h>
+#include <haproxy/time.h>
 
 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
index 96c7a04bcb1839501610dee5e5311cfc4f4b7f57..1b5dffb15e98117ef128e6c6b45273e2871156a3 100644 (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 */
 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% */
index 7b8e12d3f3d4691c340476fa7acfb11b5490f9d4..b28b53192881de2f396a75c24797e1739adf7714 100644 (file)
@@ -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;
 }
 
 /*
index 2472a215982b597df1dbf118c4cede73565aff58..f2ed7e6f534eef76c92f21a6b27143bf92cbafdb 100644 (file)
@@ -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;
        }
index 1d67dfd49ed6875a092ca50e77f86ad2352db716..5dd3b89e525f34ee63222f6138fa6f3d07d8a52b 100644 (file)
@@ -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);
        }
 
index 5af4804fd3231629486da3292e2c95fab0dca078..1b440b4495078681bf9aef6ca8cab3af0e056c67 100644 (file)
--- 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 */
index 71c10a93a98ccef2a855afed80be9644aa62e0d7..b53f6546972ee297372eb60efabce39bd706a59d 100644 (file)
@@ -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;
index 60c7daad8567b2df78de2ca6c23a1c8c26d1e202..82a4fa288fdb73d18bcc564d431447474643a6ac 100644 (file)
--- 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;
index 502c54991e5ece8d240da2706ad9029590bf616c..1909d4a481734102e70187447105216707f04060 100644 (file)
@@ -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) {
index 725f704c2336088ecfc5d0046b6dc0a6a94d03d7..f9c3ed8b425b1185030dd28c4eb1a787844e2504 100644 (file)
@@ -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;
index f2eb6a8e0531cbad875e9a023228f0c3f61eb928..3a3bb30b19c30fff3a0c3cf00ca3dd323dc723bb 100644 (file)
@@ -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;
 
index 73bd3c26ee1a0ad4961e981d503d3c9a98822ce3..f6044c5a3267837120417de3f1e0f904269b7818 100644 (file)
@@ -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;
index ac0b0158f302eae7b0909c43f386d3b4c7bf3647..443a6883cc2e6edf655e4e23c09e2258dc8a6684 100644 (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)
 {
-       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;
index 8172f8f5306f943590314b4906948fd0ee81848d..f8162b1a06e17afda3aa90b8884492e89e232830 100644 (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)
 {
-       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));
        }
 }
 
index 677de88054c43090e7a4bbc3aef49c8a66f85d5f..da5b102208c89f000e8e99bdc54ef6f9417bf710 100644 (file)
@@ -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;
 
index af0e89171d76cd638b789c1bb4f4dc194a944c46..b10ea2b7efd344715d9cdb45a5f3cb94ca96d8f6 100644 (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 */
 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;
index d673cee39660f38731393b68e1c47db31dcfb7ee..9979c7b4a62308cc7e47b425c8b5c9a2aa8471a9 100644 (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);
                                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;
index 82a06ed7664e7ba5e1cef4ab1840fe538fbd276f..4e0ece3dff4763d6100f76498135a7050bb67e22 100644 (file)
@@ -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);