From: Amaury Denoyelle Date: Mon, 5 Oct 2020 09:49:41 +0000 (+0200) Subject: MEDIUM: stats: add abstract type to store counters X-Git-Tag: v2.3-dev6~100 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=730c727ea34a695279e1919fd4506688cfe3711b;p=thirdparty%2Fhaproxy.git MEDIUM: stats: add abstract type to store counters Implement a small API to easily add extra counters inside a structure instance. This will be used to implement dynamic statistics linked on every type of object as needed. The counters are stored in a dynamic array inside the relevant objects. --- diff --git a/include/haproxy/stats-t.h b/include/haproxy/stats-t.h index 7d41b78f4b..3be2b4a6ae 100644 --- a/include/haproxy/stats-t.h +++ b/include/haproxy/stats-t.h @@ -479,6 +479,12 @@ struct stats_module { uint32_t domain_flags; /* stats application domain for this module */ }; +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, @@ -497,4 +503,50 @@ enum stats_domain_px_cap { STATS_PX_CAP_MASK = 0xff }; +#define EXTRA_COUNTERS(name) \ + struct extra_counters *name + +#define EXTRA_COUNTERS_GET(counters, mod) \ + (void *)((counters)->data + (mod)->counters_off[(counters)->type]) + +#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 */