kstats_wrapper.c

00001 /* 
00002  * $Id$
00003  * 
00004  * Copyright (C) 2010 iptelorg GmbH
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00022 /*
00023  * History:
00024  * --------
00025  *  2010-08-08  initial version (andrei)
00026 */
00027 
00028 #include "kstats_wrapper.h"
00029 
00030 #ifdef STATISTICS
00031 
00032 
00041 static counter_val_t cnt_cbk_wrapper(counter_handle_t h, void* param)
00042 {
00043         stat_function k_stat_f;
00044         
00045         k_stat_f = param;
00046         return k_stat_f();
00047 }
00048 
00049 
00050 
00051 int register_stat( char *module, char *name, stat_var **pvar, int flags)
00052 {
00053         int cnt_flags;
00054         counter_handle_t h;
00055         int ret;
00056         
00057         if (module == 0 || name == 0 || pvar == 0) {
00058                 BUG("invalid parameters (%p, %p, %p)\n", module, name, pvar);
00059                 return -1;
00060         }
00061         /* translate kamailio stat flags into sr counter flags */
00062         cnt_flags = (flags & STAT_NO_RESET) ? CNT_F_NO_RESET : 0;
00063         if (flags & STAT_IS_FUNC)
00064                 ret = counter_register(&h, module, name, cnt_flags,
00065                                         cnt_cbk_wrapper,(stat_function)pvar,
00066                                         "kamailio statistic (no description)",
00067                                         0);
00068         else
00069                 ret = counter_register(&h, module, name, cnt_flags, 0, 0,
00070                                         "kamailio statistic (no description)", 0);
00071         if (ret < 0) {
00072                 if (ret == -2)
00073                         ERR("counter %s.%s already registered\n", module, name);
00074                 goto error;
00075         }
00076         if (!(flags & STAT_IS_FUNC))
00077                 *pvar = (void*)(unsigned long)h.id;
00078         return 0;
00079 error:
00080         if (!(flags & STAT_IS_FUNC))
00081                 *pvar = 0;
00082         return -1;
00083 }
00084 
00085 
00086 
00087 int register_module_stats(char *module, stat_export_t *stats)
00088 {
00089         if (module == 0 || *module == 0) {
00090                 BUG("null or empty module name\n");
00091                 goto error;
00092         }
00093         if (stats == 0 || stats[0].name == 0)
00094                 /* empty stats */
00095                 return 0;
00096         for (; stats->name; stats++)
00097                 if (register_stat(module, stats->name, stats->stat_pointer,
00098                                                         stats->flags) < 0 ){
00099                         ERR("failed to add statistic %s.%s\n", module, stats->name);
00100                         goto error;
00101                 }
00102         return 0;
00103 error:
00104         return -1;
00105 }
00106 
00107 #endif /* STATISTICS */
00108 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */