Harald Welte | ffa55a4 | 2009-12-22 19:07:32 +0100 | [diff] [blame] | 1 | /* utility routines for keeping some statistics */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <openbsc/gsm_data.h> |
| 27 | #include <openbsc/signal.h> |
| 28 | #include <openbsc/linuxlist.h> |
| 29 | #include <openbsc/talloc.h> |
| 30 | #include <openbsc/statistics.h> |
| 31 | #include <openbsc/db.h> |
| 32 | #include <openbsc/timer.h> |
| 33 | |
| 34 | static LLIST_HEAD(counters); |
| 35 | |
| 36 | static struct timer_list db_sync_timer; |
| 37 | |
| 38 | #define DB_SYNC_INTERVAL 60, 0 |
| 39 | |
| 40 | struct counter *counter_alloc(const char *name) |
| 41 | { |
| 42 | struct counter *ctr = talloc_zero(tall_bsc_ctx, struct counter); |
| 43 | |
| 44 | if (!ctr) |
| 45 | return NULL; |
| 46 | |
| 47 | ctr->name = name; |
| 48 | llist_add_tail(&ctr->list, &counters); |
| 49 | |
| 50 | return ctr; |
| 51 | } |
| 52 | |
| 53 | void counter_free(struct counter *ctr) |
| 54 | { |
| 55 | llist_del(&ctr->list); |
| 56 | talloc_free(ctr); |
| 57 | } |
| 58 | |
| 59 | int counters_store_db(void) |
| 60 | { |
| 61 | struct counter *ctr; |
| 62 | int rc = 0; |
| 63 | |
| 64 | llist_for_each_entry(ctr, &counters, list) { |
| 65 | rc = db_store_counter(ctr); |
| 66 | if (rc < 0) |
| 67 | return rc; |
| 68 | } |
| 69 | |
| 70 | return rc; |
| 71 | } |
| 72 | |
| 73 | static void db_sync_timer_cb(void *data) |
| 74 | { |
| 75 | /* store counters to database and re-schedule */ |
| 76 | counters_store_db(); |
| 77 | bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); |
| 78 | } |
| 79 | |
| 80 | static __attribute__((constructor)) void on_dso_load_stat(void) |
| 81 | { |
| 82 | db_sync_timer.cb = db_sync_timer_cb; |
| 83 | db_sync_timer.data = NULL; |
| 84 | bsc_schedule_timer(&db_sync_timer, DB_SYNC_INTERVAL); |
| 85 | } |