From: Amaury Denoyelle Date: Mon, 29 Apr 2024 14:34:22 +0000 (+0200) Subject: MINOR: stats: support rate in stats-file X-Git-Tag: v3.0-dev10~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fec2ae9b7688a8dfc2f3d492478975361c3bc437;p=thirdparty%2Fhaproxy.git MINOR: stats: support rate in stats-file Implement support for FN_RATE stat column into stat-file. For the output part, only minimal change is required. Reuse the function read_freq_ctr() to print the same value in both stats output and stats-file dump. For counter preloading, define a new utility function preload_freq_ctr(). This can be used to initialize a freq-ctr type by preloading previous period value. Reuse this function in load_ctr() during stats-file parsing. At the moment, no rate column is defined as generic. Thus, this commit does not have functional change. This will be changed as soon as FN_RATE are converted to generic columns. --- diff --git a/include/haproxy/freq_ctr.h b/include/haproxy/freq_ctr.h index f3f6903903..f037cbbade 100644 --- a/include/haproxy/freq_ctr.h +++ b/include/haproxy/freq_ctr.h @@ -32,6 +32,14 @@ ullong freq_ctr_total(const struct freq_ctr *ctr, uint period, int pend); int freq_ctr_overshoot_period(const struct freq_ctr *ctr, uint period, uint freq); uint update_freq_ctr_period_slow(struct freq_ctr *ctr, uint period, uint inc); +/* Only usable during single threaded startup phase. */ +static inline void preload_freq_ctr(struct freq_ctr *ctr, uint value) +{ + ctr->curr_ctr = 0; + ctr->prev_ctr = value; + ctr->curr_tick = now_ms & ~1; +} + /* Update a frequency counter by incremental units. It is automatically * rotated if the period is over. It is important that it correctly initializes * a null area. diff --git a/src/stats-file.c b/src/stats-file.c index 828c0a1bf2..4f07ddc903 100644 --- a/src/stats-file.c +++ b/src/stats-file.c @@ -211,6 +211,10 @@ static int load_ctr(const struct stat_col *col, const struct ist token, value.u.u64 = read_uint64(&ptr, istend(token)); break; + case FF_U32: + value.u.u32 = read_uint(&ptr, istend(token)); + break; + default: /* Unsupported field nature. */ return 1; @@ -223,6 +227,9 @@ static int load_ctr(const struct stat_col *col, const struct ist token, if (fn == FN_COUNTER && ff == FF_U64) { *(uint64_t *)counter = value.u.u64; } + else if (fn == FN_RATE && ff == FF_U32) { + preload_freq_ctr(counter, value.u.u32); + } else { /* Unsupported field format/nature combination. */ return 1; diff --git a/src/stats.c b/src/stats.c index 65b701fdb6..0a26a01ec5 100644 --- a/src/stats.c +++ b/src/stats.c @@ -801,6 +801,11 @@ static struct field me_generate_field(const struct stat_col *col, ABORT_NOW(); } } + else if (fn == FN_RATE) { + /* freq-ctr always uses FF_U32 */ + BUG_ON(stcol_format(col) != FF_U32); + value = mkf_u32(FN_RATE, read_freq_ctr(counter)); + } else { /* No generic column available for other field nature. */ ABORT_NOW();