]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: stats: add abstract type to store counters
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Mon, 5 Oct 2020 09:49:41 +0000 (11:49 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 5 Oct 2020 10:02:14 +0000 (12:02 +0200)
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.

include/haproxy/stats-t.h

index 7d41b78f4bf1b08457c869fb6e13ae138d2a9a4e..3be2b4a6ae15540092a17330901775c064c4d1b2 100644 (file)
@@ -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 */