From: Frederic Lecaille Date: Wed, 29 Jul 2026 07:47:30 +0000 (+0200) Subject: CLEANUP: haload: embed rate_task into hld_thr_info structure X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=ca2ef0d72c03be6516725e92f4644f7537ae8c65;p=thirdparty%2Fhaproxy.git CLEANUP: haload: embed rate_task into hld_thr_info structure Store the rate task directly inside the per-thread struct instead of keeping a separate global array. This simplifies memory management by grouping thread-specific tasks and data together: - move rate task creation into - clean up rate tasks directly inside - remove the separate helper and global array --- diff --git a/src/haload.c b/src/haload.c index 6ae233c86..d2c0fb8b0 100644 --- a/src/haload.c +++ b/src/haload.c @@ -61,6 +61,7 @@ struct hld_thr_info { uint64_t *ttfb_pct; // counts per ttfb value for percentile uint64_t *ttlb_pct; // counts per ttlb value for percentile uint64_t tot_sc[5]; // total status codes on this thread: 1xx,2xx,3xx,4xx,5xx + struct task *rate_task; // task used when is set __attribute__((aligned(64))) union { } __pad; }; @@ -78,7 +79,6 @@ struct hld_usr { }; struct hld_thr_info *thrs_info; -static struct task **hld_rate_tasks; struct list hld_hdrs = LIST_HEAD_INIT(hld_hdrs); struct proxy hld_proxy; @@ -1818,42 +1818,19 @@ void sigint_handler(int sig) /* Deallocate the thread information structs */ static void hld_dealloc_thrs_info(void) -{ - free(thrs_info); - thrs_info = NULL; -} - -/* Deallocate the array of conn rate tasks */ -static void hld_dealloc_rate_task(void) { int i; - if (!hld_rate_tasks) + if (!thrs_info) return; for (i = 0; i < arg_thrd; i++) { - task_destroy(hld_rate_tasks[i]); - hld_rate_tasks[i] = NULL; - } - - ha_free(hld_rate_tasks); -} - -/* Allocate all thread information structs */ -static int hld_alloc_thrs_info(void) -{ - int i; - - thrs_info = calloc(arg_thrd, sizeof(*thrs_info)); - if (!thrs_info) { - ha_alert("failed to alloct threads information array.\n"); - return 0; + task_destroy(thrs_info[i].rate_task); + thrs_info[i].rate_task = NULL; } - for (i = 0; i < arg_thrd; i++) - thrs_info[i].maxusrs = (arg_usr + i) / arg_thrd; - - return 1; + free(thrs_info); + thrs_info = NULL; } /* Thread task launched to handle the connection and request rate */ @@ -1898,13 +1875,48 @@ static struct task *hld_rate_task(struct task *t, void *context, unsigned int st HA_ATOMIC_DEC(&running_tasks); t->expire = TICK_ETERNITY; task_destroy(t); - hld_rate_tasks[tid] = NULL; + thrs_info[tid].rate_task = NULL; t = NULL; } return t; } +/* Allocate all thread information structs */ +static int hld_alloc_thrs_info(void) +{ + int i, ret = 0; + + thrs_info = calloc(arg_thrd, sizeof(*thrs_info)); + if (!thrs_info) { + ha_alert("failed to alloct threads information array.\n"); + goto out; + } + + for (i = 0; i < arg_thrd; i++) { + thrs_info[i].maxusrs = (arg_usr + i) / arg_thrd; + if (arg_rate) { + struct task *t; + + t = task_new_on(i); + if (!t) { + ha_alert("could not allocate a new task for req rate\n"); + goto out; + } + + t->process = hld_rate_task; + t->expire = TICK_ETERNITY; + task_wakeup(t, TASK_WOKEN_INIT); + thrs_info[i].rate_task = t; + HA_ATOMIC_INC(&running_tasks); + } + } + + ret = 1; + out: + return ret; +} + static int hld_init(void) { int ret = ERR_ALERT | ERR_FATAL; @@ -1943,32 +1955,6 @@ static int hld_init(void) goto err; } - if (arg_rate) { - int i; - - hld_rate_tasks = calloc(arg_thrd, sizeof(*hld_rate_tasks)); - if (!hld_rate_tasks) { - ha_alert("could not allocate arg rate tasks\n"); - goto err; - } - - for (i = 0; i < arg_thrd; i++) { - struct task *t; - - t = task_new_on(i); - if (!t) { - ha_alert("could not allocate a new thread task\n"); - goto err; - } - - hld_rate_tasks[i] = t; - t->process = hld_rate_task; - t->expire = TICK_ETERNITY; - task_wakeup(t, TASK_WOKEN_INIT); - HA_ATOMIC_INC(&running_tasks); - } - } - mtask.t->process = mtask_cb; mtask.t->state |= TASK_RT; mtask.t->expire = TICK_ETERNITY; @@ -1989,7 +1975,7 @@ static int hld_init(void) ha_free(&errmsg); return ret; err: - hld_dealloc_rate_task(); + hld_dealloc_thrs_info(); goto leave; } REGISTER_POST_CHECK(hld_init);