From 079353ad1f894853b80cda4db8ee0ce08d682b28 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Wed, 23 Dec 2009 05:29:04 +0100 Subject: [PATCH] [statistics] Do the syncing to db from within the bsc_hack Change the counters_store_db function to be a generic for_each function taking a function pointer and data. Use that in bsc_hack to store it to the DB. This is removing the DB requirement and will allow to handle the counter values in different ways without making the counter list public. I verified that the syncing is still taking place. --- openbsc/include/openbsc/statistics.h | 2 ++ openbsc/src/bsc_hack.c | 23 +++++++++++++++++++++++ openbsc/src/statistics.c | 21 ++------------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/openbsc/include/openbsc/statistics.h b/openbsc/include/openbsc/statistics.h index 9d761c62b..1d56054ab 100644 --- a/openbsc/include/openbsc/statistics.h +++ b/openbsc/include/openbsc/statistics.h @@ -26,4 +26,6 @@ static inline void counter_reset(struct counter *ctr) struct counter *counter_alloc(const char *name); void counter_free(struct counter *ctr); +int counters_for_each(int (*handle_counter)(struct counter *, void *), void *data); + #endif /* _STATISTICS_H */ diff --git a/openbsc/src/bsc_hack.c b/openbsc/src/bsc_hack.c index 0d20d43d3..8792cc36c 100644 --- a/openbsc/src/bsc_hack.c +++ b/openbsc/src/bsc_hack.c @@ -43,6 +43,11 @@ struct gsm_network *bsc_gsmnet = 0; static const char *database_name = "hlr.sqlite3"; static const char *config_file = "openbsc.cfg"; + +/* timer to store statistics */ +#define DB_SYNC_INTERVAL 60, 0 +static struct timer_list db_sync_timer; + extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *), const char *cfg_file); extern int bsc_shutdown_net(struct gsm_network *net); @@ -155,6 +160,19 @@ static void signal_handler(int signal) } } +/* timer handling */ +static int _db_store_counter(struct counter *counter, void *data) +{ + return db_store_counter(counter); +} + +static void db_sync_timer_cb(void *data) +{ + /* store counters to database and re-schedule */ + counters_for_each(_db_store_counter, NULL); + bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); +} + int main(int argc, char **argv) { int rc; @@ -189,6 +207,11 @@ int main(int argc, char **argv) } printf("DB: Database prepared.\n"); + /* setup the timer */ + db_sync_timer.cb = db_sync_timer_cb; + db_sync_timer.data = NULL; + bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); + rc = bsc_bootstrap_network(mncc_recv, config_file); if (rc < 0) exit(1); diff --git a/openbsc/src/statistics.c b/openbsc/src/statistics.c index 4cc281d5a..9bd44f3d4 100644 --- a/openbsc/src/statistics.c +++ b/openbsc/src/statistics.c @@ -33,10 +33,6 @@ static LLIST_HEAD(counters); -static struct timer_list db_sync_timer; - -#define DB_SYNC_INTERVAL 60, 0 - struct counter *counter_alloc(const char *name) { struct counter *ctr = talloc_zero(tall_bsc_ctx, struct counter); @@ -56,13 +52,13 @@ void counter_free(struct counter *ctr) talloc_free(ctr); } -static int counters_store_db(void) +int counters_for_each(int (*handle_counter)(struct counter *, void *), void *data) { struct counter *ctr; int rc = 0; llist_for_each_entry(ctr, &counters, list) { - rc = db_store_counter(ctr); + rc = handle_counter(ctr, data); if (rc < 0) return rc; } @@ -70,16 +66,3 @@ static int counters_store_db(void) return rc; } -static void db_sync_timer_cb(void *data) -{ - /* store counters to database and re-schedule */ - counters_store_db(); - bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); -} - -static __attribute__((constructor)) void on_dso_load_stat(void) -{ - db_sync_timer.cb = db_sync_timer_cb; - db_sync_timer.data = NULL; - bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); -}