|
@@ -25,6 +25,7 @@
|
|
|
* 2006-01-16 first version (bogdan)
|
|
|
* 2006-11-28 added get_stat_var_from_num_code() (Jeffrey Magder -
|
|
|
* SOMA Networks)
|
|
|
+ * 2010-08-08 removed all the parts emulated by kstats_wrapper.[ch] (andrei)
|
|
|
*/
|
|
|
|
|
|
/*!
|
|
@@ -37,26 +38,14 @@
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
-#include "../../mem/shm_mem.h"
|
|
|
#include "../../ut.h"
|
|
|
#include "../../dprint.h"
|
|
|
-#include "../../locking.h"
|
|
|
#include "../../socket_info.h"
|
|
|
#include "km_ut.h"
|
|
|
-#include "hash_func.h"
|
|
|
#include "statistics.h"
|
|
|
|
|
|
#ifdef STATISTICS
|
|
|
|
|
|
-static stats_collector *collector = NULL;
|
|
|
-
|
|
|
-
|
|
|
-#define stat_hash(_s) core_hash( _s, 0, STATS_HASH_SIZE)
|
|
|
-
|
|
|
-stats_collector* get_stats_collector(void)
|
|
|
-{
|
|
|
- return collector;
|
|
|
-}
|
|
|
|
|
|
/*! \brief
|
|
|
* Returns the statistic associated with 'numerical_code' and 'out_codes'.
|
|
@@ -89,244 +78,6 @@ stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int out_codes)
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-int init_stats_collector(void)
|
|
|
-{
|
|
|
- if(collector != NULL) /* already initialized */
|
|
|
- return 0;
|
|
|
- /* init the collector */
|
|
|
- collector = (stats_collector*)shm_malloc(sizeof(stats_collector));
|
|
|
- if (collector==0) {
|
|
|
- LM_ERR("no more shm mem\n");
|
|
|
- goto error;
|
|
|
- }
|
|
|
- memset( collector, 0 , sizeof(stats_collector));
|
|
|
-
|
|
|
- LM_DBG("statistics manager successfully initialized\n");
|
|
|
-
|
|
|
- return 0;
|
|
|
-error:
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void destroy_stats_collector(void)
|
|
|
-{
|
|
|
- stat_var *stat;
|
|
|
- stat_var *tmp_stat;
|
|
|
- int i;
|
|
|
-
|
|
|
- if (collector) {
|
|
|
- /* destroy hash table */
|
|
|
- for( i=0 ; i<STATS_HASH_SIZE ; i++ ) {
|
|
|
- for( stat=collector->hstats[i] ; stat ; ) {
|
|
|
- tmp_stat = stat;
|
|
|
- stat = stat->hnext;
|
|
|
- if ((tmp_stat->flags&STAT_IS_FUNC)==0 && tmp_stat->u.val)
|
|
|
- shm_free(tmp_stat->u.val);
|
|
|
- if ( (tmp_stat->flags&STAT_SHM_NAME) && tmp_stat->name.s)
|
|
|
- shm_free(tmp_stat->name.s);
|
|
|
- shm_free(tmp_stat);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /* destroy sts_module array */
|
|
|
- if (collector->amodules)
|
|
|
- shm_free(collector->amodules);
|
|
|
-
|
|
|
- /* destroy the collector */
|
|
|
- shm_free(collector);
|
|
|
- collector = NULL;
|
|
|
- }
|
|
|
-
|
|
|
- return;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-module_stats* get_stat_module(str *module)
|
|
|
-{
|
|
|
- int i;
|
|
|
-
|
|
|
- if ( (module==0) || module->s==0 || module->len==0 )
|
|
|
- return 0;
|
|
|
-
|
|
|
- for( i=0 ; i<collector->mod_no ; i++ ) {
|
|
|
- if ( (collector->amodules[i].name.len == module->len) &&
|
|
|
- (strncasecmp(collector->amodules[i].name.s,module->s,module->len)==0) )
|
|
|
- return &collector->amodules[i];
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static inline module_stats* add_stat_module( char *module)
|
|
|
-{
|
|
|
- module_stats *amods;
|
|
|
- module_stats *mods;
|
|
|
- int len;
|
|
|
-
|
|
|
- if ( (module==0) || ((len = strlen(module))==0 ) )
|
|
|
- return 0;
|
|
|
- if(init_stats_collector()!=0)
|
|
|
- return 0;
|
|
|
-
|
|
|
- amods = (module_stats*)shm_realloc( collector->amodules,
|
|
|
- (collector->mod_no+1)*sizeof(module_stats) );
|
|
|
- if (amods==0) {
|
|
|
- LM_ERR("no more shm memory\n");
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- collector->amodules = amods;
|
|
|
- collector->mod_no++;
|
|
|
-
|
|
|
- mods = &amods[collector->mod_no-1];
|
|
|
- memset( mods, 0, sizeof(module_stats) );
|
|
|
-
|
|
|
- mods->name.s = module;
|
|
|
- mods->name.len = len;
|
|
|
-
|
|
|
- return mods;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-int register_stat( char *module, char *name, stat_var **pvar, int flags)
|
|
|
-{
|
|
|
- module_stats* mods;
|
|
|
- stat_var *stat;
|
|
|
- stat_var *it;
|
|
|
- str smodule;
|
|
|
- int hash;
|
|
|
-
|
|
|
- if (module==0 || name==0 || pvar==0) {
|
|
|
- LM_ERR("invalid parameters module=%p, name=%p, pvar=%p \n",
|
|
|
- module, name, pvar);
|
|
|
- goto error;
|
|
|
- }
|
|
|
-
|
|
|
- if(init_stats_collector()!=0)
|
|
|
- return -1;
|
|
|
-
|
|
|
- stat = (stat_var*)shm_malloc(sizeof(stat_var));
|
|
|
- if (stat==0) {
|
|
|
- LM_ERR("no more shm memory\n");
|
|
|
- goto error;
|
|
|
- }
|
|
|
- memset( stat, 0, sizeof(stat_var));
|
|
|
-
|
|
|
- if ( (flags&STAT_IS_FUNC)==0 ) {
|
|
|
- stat->u.val = (stat_val*)shm_malloc(sizeof(stat_val));
|
|
|
- if (stat->u.val==0) {
|
|
|
- LM_ERR("no more shm memory\n");
|
|
|
- goto error1;
|
|
|
- }
|
|
|
- atomic_set(stat->u.val,0);
|
|
|
- *pvar = stat;
|
|
|
- } else {
|
|
|
- stat->u.f = (stat_function)(pvar);
|
|
|
- }
|
|
|
-
|
|
|
- /* is the module already recorded? */
|
|
|
- smodule.s = module;
|
|
|
- smodule.len = strlen(module);
|
|
|
- mods = get_stat_module(&smodule);
|
|
|
- if (mods==0) {
|
|
|
- mods = add_stat_module(module);
|
|
|
- if (mods==0) {
|
|
|
- LM_ERR("failed to add new module\n");
|
|
|
- goto error2;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /* fill the stat record */
|
|
|
- stat->mod_idx = collector->mod_no-1;
|
|
|
-
|
|
|
- stat->name.s = name;
|
|
|
- stat->name.len = strlen(name);
|
|
|
- stat->flags = flags;
|
|
|
-
|
|
|
-
|
|
|
- /* compute the hash by name */
|
|
|
- hash = stat_hash( &stat->name );
|
|
|
-
|
|
|
- /* link it */
|
|
|
- if (collector->hstats[hash]==0) {
|
|
|
- collector->hstats[hash] = stat;
|
|
|
- } else {
|
|
|
- it = collector->hstats[hash];
|
|
|
- while(it->hnext)
|
|
|
- it = it->hnext;
|
|
|
- it->hnext = stat;
|
|
|
- }
|
|
|
- collector->stats_no++;
|
|
|
-
|
|
|
- /* add the statistic also to the module statistic list */
|
|
|
- if (mods->tail) {
|
|
|
- mods->tail->lnext = stat;
|
|
|
- } else {
|
|
|
- mods->head = stat;
|
|
|
- }
|
|
|
- mods->tail = stat;
|
|
|
- mods->no++;
|
|
|
-
|
|
|
- return 0;
|
|
|
-error2:
|
|
|
- if ( (flags&STAT_IS_FUNC)==0 ) {
|
|
|
- shm_free(*pvar);
|
|
|
- *pvar = 0;
|
|
|
- }
|
|
|
-error1:
|
|
|
- shm_free(stat);
|
|
|
-error:
|
|
|
- *pvar = 0;
|
|
|
- return -1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-int register_module_stats(char *module, stat_export_t *stats)
|
|
|
-{
|
|
|
- int ret;
|
|
|
-
|
|
|
- if (module==0 || module[0]==0 || !stats || !stats[0].name)
|
|
|
- return 0;
|
|
|
-
|
|
|
- for( ; stats->name ; stats++) {
|
|
|
- ret = register_stat( module, stats->name, stats->stat_pointer,
|
|
|
- stats->flags);
|
|
|
- if (ret!=0) {
|
|
|
- LM_CRIT("failed to add statistic\n");
|
|
|
- return -1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-stat_var* get_stat( str *name )
|
|
|
-{
|
|
|
- stat_var *stat;
|
|
|
- int hash;
|
|
|
-
|
|
|
- if (name==0 || name->s==0 || name->len==0)
|
|
|
- return 0;
|
|
|
-
|
|
|
- /* compute the hash by name */
|
|
|
- hash = stat_hash( name );
|
|
|
-
|
|
|
- /* and look for it */
|
|
|
- for( stat=collector->hstats[hash] ; stat ; stat=stat->hnext ) {
|
|
|
- if ( (stat->name.len==name->len) &&
|
|
|
- (strncasecmp( stat->name.s, name->s, name->len)==0) )
|
|
|
- return stat;
|
|
|
- }
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
#endif /*STATISTICS*/
|
|
|
|
|
|
#define MAX_PROC_BUFFER 256
|