From da813ae4d7cb77137edf278815ee2763c68c4323 Mon Sep 17 00:00:00 2001 From: Olivier Houchard Date: Tue, 13 Jan 2026 08:01:28 +0100 Subject: [PATCH] MEDIUM: counters: Remove some extra tests Before updating counters, a few tests are made to check if the counters exits. but those counters should always exist at this point, so just remmove them. This commit should have no impact, but can easily be reverted with no functional impact if various crashes appear. --- include/haproxy/backend.h | 3 +-- include/haproxy/proxy.h | 39 +++++++++++++++++---------------------- include/haproxy/server.h | 10 ++++------ src/backend.c | 9 +++------ src/cache.c | 12 ++++-------- src/check.c | 6 ++---- src/server.c | 9 +++------ 7 files changed, 34 insertions(+), 54 deletions(-) diff --git a/include/haproxy/backend.h b/include/haproxy/backend.h index 70709480a..47ecfff33 100644 --- a/include/haproxy/backend.h +++ b/include/haproxy/backend.h @@ -88,8 +88,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) { - if (be->be_counters.shared.tg && be->be_counters.shared.tg[tgid - 1]) - HA_ATOMIC_STORE(&be->be_counters.shared.tg[tgid - 1]->last_sess, ns_to_sec(now_ns)); + HA_ATOMIC_STORE(&be->be_counters.shared.tg[tgid - 1]->last_sess, ns_to_sec(now_ns)); } /* This function returns non-zero if the designated server will be diff --git a/include/haproxy/proxy.h b/include/haproxy/proxy.h index 9ddd46244..34274756b 100644 --- a/include/haproxy/proxy.h +++ b/include/haproxy/proxy.h @@ -166,11 +166,10 @@ static inline int proxy_abrt_close(const struct proxy *px) /* increase the number of cumulated connections received on the designated frontend */ static inline void proxy_inc_fe_conn_ctr(struct listener *l, struct proxy *fe) { - if (fe->fe_counters.shared.tg && fe->fe_counters.shared.tg[tgid - 1]) { - _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_conn); - update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->conn_per_sec, 1); - } - if (l && l->counters && l->counters->shared.tg && l->counters->shared.tg[tgid - 1]) + _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_conn); + update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->conn_per_sec, 1); + + if (l && l->counters) _HA_ATOMIC_INC(&l->counters->shared.tg[tgid - 1]->cum_conn); HA_ATOMIC_UPDATE_MAX(&fe->fe_counters.cps_max, update_freq_ctr(&fe->fe_counters._conn_per_sec, 1)); @@ -179,11 +178,10 @@ static inline void proxy_inc_fe_conn_ctr(struct listener *l, struct proxy *fe) /* increase the number of cumulated connections accepted by the designated frontend */ static inline void proxy_inc_fe_sess_ctr(struct listener *l, struct proxy *fe) { - if (fe->fe_counters.shared.tg && fe->fe_counters.shared.tg[tgid - 1]) { - _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_sess); - update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->sess_per_sec, 1); - } - if (l && l->counters && l->counters->shared.tg && l->counters->shared.tg[tgid - 1]) + _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_sess); + update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->sess_per_sec, 1); + + if (l && l->counters) _HA_ATOMIC_INC(&l->counters->shared.tg[tgid - 1]->cum_sess); HA_ATOMIC_UPDATE_MAX(&fe->fe_counters.sps_max, update_freq_ctr(&fe->fe_counters._sess_per_sec, 1)); @@ -199,19 +197,17 @@ static inline void proxy_inc_fe_cum_sess_ver_ctr(struct listener *l, struct prox http_ver > sizeof(fe->fe_counters.shared.tg[tgid - 1]->cum_sess_ver) / sizeof(*fe->fe_counters.shared.tg[tgid - 1]->cum_sess_ver)) return; - if (fe->fe_counters.shared.tg && fe->fe_counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_sess_ver[http_ver - 1]); - if (l && l->counters && l->counters->shared.tg && l->counters->shared.tg[tgid - 1]) + _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->cum_sess_ver[http_ver - 1]); + if (l && l->counters) _HA_ATOMIC_INC(&l->counters->shared.tg[tgid - 1]->cum_sess_ver[http_ver - 1]); } /* increase the number of cumulated streams on the designated backend */ static inline void proxy_inc_be_ctr(struct proxy *be) { - if (be->be_counters.shared.tg && be->be_counters.shared.tg[tgid - 1]) { - _HA_ATOMIC_INC(&be->be_counters.shared.tg[tgid - 1]->cum_sess); - update_freq_ctr(&be->be_counters.shared.tg[tgid - 1]->sess_per_sec, 1); - } + _HA_ATOMIC_INC(&be->be_counters.shared.tg[tgid - 1]->cum_sess); + update_freq_ctr(&be->be_counters.shared.tg[tgid - 1]->sess_per_sec, 1); + HA_ATOMIC_UPDATE_MAX(&be->be_counters.sps_max, update_freq_ctr(&be->be_counters._sess_per_sec, 1)); } @@ -226,11 +222,10 @@ static inline void proxy_inc_fe_req_ctr(struct listener *l, struct proxy *fe, if (http_ver >= sizeof(fe->fe_counters.shared.tg[tgid - 1]->p.http.cum_req) / sizeof(*fe->fe_counters.shared.tg[tgid - 1]->p.http.cum_req)) return; - if (fe->fe_counters.shared.tg && fe->fe_counters.shared.tg[tgid - 1]) { - _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->p.http.cum_req[http_ver]); - update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->req_per_sec, 1); - } - if (l && l->counters && l->counters->shared.tg && l->counters->shared.tg[tgid - 1]) + _HA_ATOMIC_INC(&fe->fe_counters.shared.tg[tgid - 1]->p.http.cum_req[http_ver]); + update_freq_ctr(&fe->fe_counters.shared.tg[tgid - 1]->req_per_sec, 1); + + if (l && l->counters) _HA_ATOMIC_INC(&l->counters->shared.tg[tgid - 1]->p.http.cum_req[http_ver]); HA_ATOMIC_UPDATE_MAX(&fe->fe_counters.p.http.rps_max, update_freq_ctr(&fe->fe_counters.p.http._req_per_sec, 1)); diff --git a/include/haproxy/server.h b/include/haproxy/server.h index 71a953b57..3dd438dd7 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -207,10 +207,9 @@ static inline void server_index_id(struct proxy *px, struct server *srv) /* increase the number of cumulated streams on the designated server */ static inline void srv_inc_sess_ctr(struct server *s) { - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) { - _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->cum_sess); - update_freq_ctr(&s->counters.shared.tg[tgid - 1]->sess_per_sec, 1); - } + _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->cum_sess); + update_freq_ctr(&s->counters.shared.tg[tgid - 1]->sess_per_sec, 1); + HA_ATOMIC_UPDATE_MAX(&s->counters.sps_max, update_freq_ctr(&s->counters._sess_per_sec, 1)); } @@ -218,8 +217,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) { - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) - HA_ATOMIC_STORE(&s->counters.shared.tg[tgid - 1]->last_sess, ns_to_sec(now_ns)); + HA_ATOMIC_STORE(&s->counters.shared.tg[tgid - 1]->last_sess, ns_to_sec(now_ns)); } /* returns the current server throttle rate between 0 and 100% */ diff --git a/src/backend.c b/src/backend.c index 3000548c7..54787f5ed 100644 --- a/src/backend.c +++ b/src/backend.c @@ -823,8 +823,7 @@ int assign_server(struct stream *s) else if (srv != prev_srv) { if (s->be_tgcounters) _HA_ATOMIC_INC(&s->be_tgcounters->cum_lbconn); - if (srv->counters.shared.tg && srv->counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&srv->counters.shared.tg[tgid - 1]->cum_lbconn); + _HA_ATOMIC_INC(&srv->counters.shared.tg[tgid - 1]->cum_lbconn); } stream_set_srv_target(s, srv); } @@ -998,13 +997,11 @@ int assign_server_and_queue(struct stream *s) s->txn->flags |= TX_CK_DOWN; } s->flags |= SF_REDISP; - if (prev_srv->counters.shared.tg && prev_srv->counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&prev_srv->counters.shared.tg[tgid - 1]->redispatches); + _HA_ATOMIC_INC(&prev_srv->counters.shared.tg[tgid - 1]->redispatches); if (s->be_tgcounters) _HA_ATOMIC_INC(&s->be_tgcounters->redispatches); } else { - if (prev_srv->counters.shared.tg && prev_srv->counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&prev_srv->counters.shared.tg[tgid - 1]->retries); + _HA_ATOMIC_INC(&prev_srv->counters.shared.tg[tgid - 1]->retries); if (s->be_tgcounters) _HA_ATOMIC_INC(&s->be_tgcounters->retries); } diff --git a/src/cache.c b/src/cache.c index 92a1fb9be..67edf1889 100644 --- a/src/cache.c +++ b/src/cache.c @@ -2133,12 +2133,10 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p return ACT_RET_CONT; if (px == strm_fe(s)) { - if (px->fe_counters.shared.tg && px->fe_counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&px->fe_counters.shared.tg[tgid - 1]->p.http.cache_lookups); + _HA_ATOMIC_INC(&px->fe_counters.shared.tg[tgid - 1]->p.http.cache_lookups); } else { - if (px->be_counters.shared.tg && px->be_counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&px->be_counters.shared.tg[tgid - 1]->p.http.cache_lookups); + _HA_ATOMIC_INC(&px->be_counters.shared.tg[tgid - 1]->p.http.cache_lookups); } cache_tree = get_cache_tree_from_hash(cache, read_u32(s->txn->cache_hash)); @@ -2226,12 +2224,10 @@ enum act_return http_action_req_cache_use(struct act_rule *rule, struct proxy *p should_send_notmodified_response(cache, htxbuf(&s->req.buf), res); if (px == strm_fe(s)) { - if (px->fe_counters.shared.tg && px->fe_counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&px->fe_counters.shared.tg[tgid - 1]->p.http.cache_hits); + _HA_ATOMIC_INC(&px->fe_counters.shared.tg[tgid - 1]->p.http.cache_hits); } else { - if (px->be_counters.shared.tg && px->be_counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&px->be_counters.shared.tg[tgid - 1]->p.http.cache_hits); + _HA_ATOMIC_INC(&px->be_counters.shared.tg[tgid - 1]->p.http.cache_hits); } return ACT_RET_CONT; } else { diff --git a/src/check.c b/src/check.c index 7835ddce2..0102f0c36 100644 --- a/src/check.c +++ b/src/check.c @@ -513,8 +513,7 @@ void set_server_check_status(struct check *check, short status, const char *desc if ((!(check->state & CHK_ST_AGENT) || (check->status >= HCHK_STATUS_L57DATA)) && (check->health > 0)) { - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_checks); + _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_checks); report = 1; check->health--; if (check->health < check->rise) @@ -741,8 +740,7 @@ void __health_adjust(struct server *s, short status) HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock); HA_ATOMIC_STORE(&s->consecutive_errors, 0); - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) - _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_hana); + _HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->failed_hana); if (s->check.fastinter) { /* timer might need to be advanced, it might also already be diff --git a/src/server.c b/src/server.c index d023e2033..9a2c3b852 100644 --- a/src/server.c +++ b/src/server.c @@ -7159,8 +7159,7 @@ static void srv_update_status(struct server *s, int type, int cause) } else if (s->cur_state == SRV_ST_STOPPED) { /* server was up and is currently down */ - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) - HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->down_trans); + HA_ATOMIC_INC(&s->counters.shared.tg[tgid - 1]->down_trans); _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, cb_data.common, s); } @@ -7174,8 +7173,7 @@ static void srv_update_status(struct server *s, int type, int cause) } s->last_change = ns_to_sec(now_ns); - if (s->counters.shared.tg && s->counters.shared.tg[tgid - 1]) - HA_ATOMIC_STORE(&s->counters.shared.tg[tgid - 1]->last_state_change, s->last_change); + HA_ATOMIC_STORE(&s->counters.shared.tg[tgid - 1]->last_state_change, s->last_change); /* publish the state change */ _srv_event_hdl_prepare_state(&cb_data.state, @@ -7195,8 +7193,7 @@ static void srv_update_status(struct server *s, int type, int cause) if (last_change < ns_to_sec(now_ns)) // ignore negative times s->proxy->down_time += ns_to_sec(now_ns) - last_change; s->proxy->last_change = ns_to_sec(now_ns); - if (s->proxy->be_counters.shared.tg && s->proxy->be_counters.shared.tg[tgid - 1]) - HA_ATOMIC_STORE(&s->proxy->be_counters.shared.tg[tgid - 1]->last_state_change, s->proxy->last_change); + HA_ATOMIC_STORE(&s->proxy->be_counters.shared.tg[tgid - 1]->last_state_change, s->proxy->last_change); } } -- 2.47.3