REORG: stats/counters: move extra_counters to counters not stats

It was always difficult to find extra_counters when the rest of the
counters are now in counters-t.h. Let's move the types to counters-t.h
and the macros to counters.h. Stats include them since they're used
there. But some users could be cleaned from the stats definitions now.
This commit is contained in:
Willy Tarreau 2026-02-24 18:57:05 +01:00
parent 9910af6117
commit 2b463e9b1f
5 changed files with 72 additions and 66 deletions

View file

@ -185,6 +185,27 @@ struct be_counters {
} p; /* protocol-specific stats */
};
/* extra counters that are registered at boot by various modules */
enum counters_type {
COUNTERS_FE = 0,
COUNTERS_BE,
COUNTERS_SV,
COUNTERS_LI,
COUNTERS_RSLV,
COUNTERS_OFF_END /* must always be last */
};
struct extra_counters {
char *data; /* heap containing counters allocated in a linear fashion */
size_t size; /* size of allocated data */
enum counters_type type; /* type of object containing the counters */
};
#define EXTRA_COUNTERS(name) \
struct extra_counters *name
#endif /* _HAPROXY_COUNTERS_T_H */
/*

View file

@ -27,6 +27,8 @@
#include <haproxy/counters-t.h>
#include <haproxy/guid-t.h>
extern THREAD_LOCAL void *trash_counters;
int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid, char **errmsg);
int counters_be_shared_prepare(struct be_counters_shared *counters, const struct guid_node *guid, char **errmsg);
@ -101,4 +103,50 @@ void counters_be_shared_drop(struct be_counters_shared *counters);
__ret; \
})
/* Manipulation of extra_counters, for boot-time registrable modules */
#define EXTRA_COUNTERS_GET(counters, mod) \
(likely(counters) ? \
((void *)((counters)->data + (mod)->counters_off[(counters)->type])) : \
(trash_counters))
#define EXTRA_COUNTERS_REGISTER(counters, ctype, alloc_failed_label) \
do { \
typeof(*counters) _ctr; \
_ctr = calloc(1, sizeof(*_ctr)); \
if (!_ctr) \
goto alloc_failed_label; \
_ctr->type = (ctype); \
*(counters) = _ctr; \
} while (0)
#define EXTRA_COUNTERS_ADD(mod, counters, new_counters, csize) \
do { \
typeof(counters) _ctr = (counters); \
(mod)->counters_off[_ctr->type] = _ctr->size; \
_ctr->size += (csize); \
} while (0)
#define EXTRA_COUNTERS_ALLOC(counters, alloc_failed_label) \
do { \
typeof(counters) _ctr = (counters); \
_ctr->data = malloc((_ctr)->size); \
if (!_ctr->data) \
goto alloc_failed_label; \
} while (0)
#define EXTRA_COUNTERS_INIT(counters, mod, init_counters, init_counters_size) \
do { \
typeof(counters) _ctr = (counters); \
memcpy(_ctr->data + mod->counters_off[_ctr->type], \
(init_counters), (init_counters_size)); \
} while (0)
#define EXTRA_COUNTERS_FREE(counters) \
do { \
if (counters) { \
free((counters)->data); \
free(counters); \
} \
} while (0)
#endif /* _HAPROXY_COUNTERS_H */

View file

@ -30,6 +30,7 @@
#include <import/eb64tree.h>
#include <import/ebmbtree.h>
#include <haproxy/counters.h>
#include <haproxy/chunk.h>
#include <haproxy/dynbuf.h>
#include <haproxy/ncbmbuf.h>

View file

@ -25,6 +25,7 @@
#include <import/ebtree-t.h>
#include <haproxy/api-t.h>
#include <haproxy/buf-t.h>
#include <haproxy/counters-t.h>
/* Flags for applet.ctx.stats.flags */
#define STAT_F_FMT_HTML 0x00000001 /* dump the stats in HTML format */
@ -515,16 +516,6 @@ struct field {
} u;
};
enum counters_type {
COUNTERS_FE = 0,
COUNTERS_BE,
COUNTERS_SV,
COUNTERS_LI,
COUNTERS_RSLV,
COUNTERS_OFF_END
};
/* Entity used to generate statistics on an HAProxy component */
struct stats_module {
struct list list;
@ -543,12 +534,6 @@ struct stats_module {
char clearable; /* reset on a clear counters */
};
struct extra_counters {
char *data; /* heap containing counters allocated in a linear fashion */
size_t size; /* size of allocated data */
enum counters_type type; /* type of object containing the counters */
};
/* stats_domain is used in a flag as a 1 byte field */
enum stats_domain {
STATS_DOMAIN_PROXY = 0,
@ -597,54 +582,4 @@ struct show_stat_ctx {
enum stat_state state; /* phase of output production */
};
extern THREAD_LOCAL void *trash_counters;
#define EXTRA_COUNTERS(name) \
struct extra_counters *name
#define EXTRA_COUNTERS_GET(counters, mod) \
(likely(counters) ? \
((void *)((counters)->data + (mod)->counters_off[(counters)->type])) : \
(trash_counters))
#define EXTRA_COUNTERS_REGISTER(counters, ctype, alloc_failed_label) \
do { \
typeof(*counters) _ctr; \
_ctr = calloc(1, sizeof(*_ctr)); \
if (!_ctr) \
goto alloc_failed_label; \
_ctr->type = (ctype); \
*(counters) = _ctr; \
} while (0)
#define EXTRA_COUNTERS_ADD(mod, counters, new_counters, csize) \
do { \
typeof(counters) _ctr = (counters); \
(mod)->counters_off[_ctr->type] = _ctr->size; \
_ctr->size += (csize); \
} while (0)
#define EXTRA_COUNTERS_ALLOC(counters, alloc_failed_label) \
do { \
typeof(counters) _ctr = (counters); \
_ctr->data = malloc((_ctr)->size); \
if (!_ctr->data) \
goto alloc_failed_label; \
} while (0)
#define EXTRA_COUNTERS_INIT(counters, mod, init_counters, init_counters_size) \
do { \
typeof(counters) _ctr = (counters); \
memcpy(_ctr->data + mod->counters_off[_ctr->type], \
(init_counters), (init_counters_size)); \
} while (0)
#define EXTRA_COUNTERS_FREE(counters) \
do { \
if (counters) { \
free((counters)->data); \
free(counters); \
} \
} while (0)
#endif /* _HAPROXY_STATS_T_H */

View file

@ -24,6 +24,7 @@
#define _HAPROXY_STATS_H
#include <haproxy/api.h>
#include <haproxy/counters.h>
#include <haproxy/listener-t.h>
#include <haproxy/stats-t.h>
#include <haproxy/tools-t.h>