From 4693ee0ff7a5fa4a12ff69b1a33adca142e781ac Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Wed, 22 Oct 2025 09:34:17 +0200 Subject: [PATCH] MEDIUM: freq-ctr: use explicit-size types for freq-ctr struct freq-ctr struct is used by the shm_stats_file API, and more precisely, it is used in the shm_stats_file_object struct for counters. shm_stats_file_object struct requires to be plateform-independent, thus we switch to using explicit size types (AKA fixed width integer types) for freq-ctr, in the attempt to make freq-ctr size and memory mapping consistent from one platform to another. We cannot simply use fixed-width integer because some of them are involved in atomic operations, and forcing a given width could cause build issues on some platforms where atomic ops are not implemented for large integers. Instead we leverage the FIXED_SIZE macro to keep handling the integers as before, but forcing them to be stored using expected number of bytes (unused bytes will simply be ignored). No change of behavior should be expected. --- include/haproxy/freq_ctr-t.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/include/haproxy/freq_ctr-t.h b/include/haproxy/freq_ctr-t.h index d5f1a8912..4eed37aab 100644 --- a/include/haproxy/freq_ctr-t.h +++ b/include/haproxy/freq_ctr-t.h @@ -28,11 +28,16 @@ * period has to be known by the user. The period is measured in ticks and * must be at least 2 ticks long. This form is slightly more CPU intensive for * reads than the per-second form as it involves a divide. + * + * /!\ any change performed here will impact shm-stats-file mapping because the + * struct is embedded in shm_stats_file_object struct, so proceed with caution + * and change shm stats file version if needed. Also, fixed width integer types + * should be used (for portability) */ struct freq_ctr { - unsigned int curr_tick; /* start date of current period (wrapping ticks) */ - unsigned int curr_ctr; /* cumulated value for current period */ - unsigned int prev_ctr; /* value for last period */ + FIXED_SIZE(4, unsigned int, curr_tick); /* start date of current period (wrapping ticks) */ + FIXED_SIZE(4, unsigned int, curr_ctr); /* cumulated value for current period */ + FIXED_SIZE(4, unsigned int, prev_ctr); /* value for last period */ }; #endif /* _HAPROXY_FREQ_CTR_T_H */