Use a global array that gets initialized by constructor functions per coverage definition.
Signed-off-by: Helmut Schaa <helmut.sc...@googlemail.com> --- The realloc is not the nicest thing to do but looks reasonable as initial approach. Using a list would cause more intrusive changes to the coverage implementation. lib/coverage.c | 33 ++------------------------------- lib/coverage.h | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 45 deletions(-) diff --git a/lib/coverage.c b/lib/coverage.c index aae9937..777ede9 100644 --- a/lib/coverage.c +++ b/lib/coverage.c @@ -29,37 +29,8 @@ VLOG_DEFINE_THIS_MODULE(coverage); /* The coverage counters. */ -#if USE_LINKER_SECTIONS -extern struct coverage_counter *__start_coverage[]; -extern struct coverage_counter *__stop_coverage[]; -#define coverage_counters __start_coverage -#define n_coverage_counters (__stop_coverage - __start_coverage) -#else /* !USE_LINKER_SECTIONS */ -#define COVERAGE_COUNTER(COUNTER) \ - DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, \ - counter_##COUNTER); \ - DEFINE_EXTERN_PER_THREAD_DATA(counter_##COUNTER, 0); \ - static unsigned int COUNTER##_count(void) \ - { \ - unsigned int *countp = counter_##COUNTER##_get(); \ - unsigned int count = *countp; \ - *countp = 0; \ - return count; \ - } \ - extern struct coverage_counter counter_##COUNTER; \ - struct coverage_counter counter_##COUNTER \ - = { #COUNTER, COUNTER##_count, 0 }; -#include "coverage.def" -#undef COVERAGE_COUNTER - -extern struct coverage_counter *coverage_counters[]; -struct coverage_counter *coverage_counters[] = { -#define COVERAGE_COUNTER(NAME) &counter_##NAME, -#include "coverage.def" -#undef COVERAGE_COUNTER -}; -#define n_coverage_counters ARRAY_SIZE(coverage_counters) -#endif /* !USE_LINKER_SECTIONS */ +struct coverage_counter **coverage_counters = NULL; +unsigned int n_coverage_counters = 0; static struct ovs_mutex coverage_mutex = OVS_MUTEX_INITIALIZER; diff --git a/lib/coverage.h b/lib/coverage.h index 4e6c050..fa8b39b 100644 --- a/lib/coverage.h +++ b/lib/coverage.h @@ -29,6 +29,8 @@ #include "ovs-thread.h" #include "vlog.h" +#include "util.h" +#include "compiler.h" /* Makes coverage_run run every 5000 ms (5 seconds). * If this value is redefined, the new value must @@ -54,9 +56,13 @@ struct coverage_counter { unsigned int hr[HR_AVG_LEN]; }; + +/* Global list of all coverage counters */ +extern struct coverage_counter **coverage_counters; +extern unsigned int n_coverage_counters; + /* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ -#if USE_LINKER_SECTIONS #define COVERAGE_DEFINE(COUNTER) \ DEFINE_STATIC_PER_THREAD_DATA(unsigned int, \ counter_##COUNTER, 0); \ @@ -74,19 +80,13 @@ struct coverage_counter { extern struct coverage_counter counter_##COUNTER; \ struct coverage_counter counter_##COUNTER \ = { #COUNTER, COUNTER##_count, 0, 0, {0}, {0} }; \ - extern struct coverage_counter *counter_ptr_##COUNTER; \ - struct coverage_counter *counter_ptr_##COUNTER \ - __attribute__((section("coverage"))) = &counter_##COUNTER -#else -#define COVERAGE_DEFINE(COUNTER) \ - DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, \ - counter_##COUNTER); \ - static inline void COUNTER##_add(unsigned int n) \ - { \ - *counter_##COUNTER##_get() += n; \ - } \ - extern struct coverage_counter counter_##COUNTER -#endif + OVS_CONSTRUCTOR( \ + static void COUNTER##_init(void) { \ + n_coverage_counters++; \ + coverage_counters = xrealloc(coverage_counters, sizeof(struct coverage_counter*) * n_coverage_counters); \ + coverage_counters[n_coverage_counters-1] = &counter_##COUNTER; \ + } \ + ) /* Adds 1 to COUNTER. */ #define COVERAGE_INC(COUNTER) COVERAGE_ADD(COUNTER, 1) -- 1.8.1.4 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev