1 Star 0 Fork 0

souhoiryo / opensips

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
statistics.c 21.37 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
/*
* $Id$
*
* Copyright (C) 2006 Voice Sistem SRL
* Copyright (C) 2010-2012 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* History:
* ---------
* 2006-01-16 first version (bogdan)
* 2006-11-28 added get_stat_var_from_num_code() (Jeffrey Magder -
* SOMA Networks)
* 2009-04-23 function var accepts a context parameter (bogdan)
* 2012-09-21 support for dynamic statistics (created of demand at runtime)
* (bogdan)
*/
/*!
* \file
* \brief Statistics support
*/
#include <string.h>
#include "mem/shm_mem.h"
#include "mi/mi.h"
#include "ut.h"
#include "dprint.h"
#include "locking.h"
#include "core_stats.h"
#include "statistics.h"
#include "pt.h"
#include "atomic.h"
#include "rw_locking.h"
#ifdef STATISTICS
static stats_collector *collector = NULL;
static int stats_ready;
static struct mi_root *mi_get_stats(struct mi_root *cmd, void *param);
static struct mi_root *mi_list_stats(struct mi_root *cmd, void *param);
static struct mi_root *mi_reset_stats(struct mi_root *cmd, void *param);
static mi_export_t mi_stat_cmds[] = {
{ "get_statistics",
"prints the statistics (all, group or one) realtime values.",
mi_get_stats, 0 , 0, 0 },
{ "list_statistics",
"lists all the registered statistics and their types",
mi_list_stats, 0 , 0, 0 },
{ "reset_statistics", "resets the value of a statistic variable",
mi_reset_stats, 0 , 0, 0 },
{ 0, 0, 0, 0, 0, 0}
};
#ifdef NO_ATOMIC_OPS
#warning STATISTICS: Architecture with no support for atomic operations. \
Using Locks!!
gen_lock_t *stat_lock = 0;
#endif
#define stat_hash(_s) core_hash( _s, 0, STATS_HASH_SIZE)
/*! \brief
* Returns the statistic associated with 'numerical_code' and 'out_codes'.
* Specifically:
*
* - if out_codes is nonzero, then the stat_var for the number of messages
* _sent out_ with the 'numerical_code' will be returned if it exists.
* - otherwise, the stat_var for the number of messages _received_ with the
* 'numerical_code' will be returned, if the stat exists.
*/
stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int out_codes)
{
static char msg_code[INT2STR_MAX_LEN+4];
str stat_name;
stat_name.s = int2bstr( (unsigned long)numerical_code, msg_code,
&stat_name.len);
stat_name.s[stat_name.len++] = '_';
if (out_codes) {
stat_name.s[stat_name.len++] = 'o';
stat_name.s[stat_name.len++] = 'u';
stat_name.s[stat_name.len++] = 't';
} else {
stat_name.s[stat_name.len++] = 'i';
stat_name.s[stat_name.len++] = 'n';
}
return get_stat(&stat_name);
}
char *build_stat_name( str* prefix, char *var_name)
{
int n;
char *s;
char *p;
n = prefix->len + 1 + strlen(var_name) + 1;
s = (char*)shm_malloc( n );
if (s==0) {
LM_ERR("no more shm mem\n");
return 0;
}
memcpy( s, prefix->s, prefix->len);
p = s + prefix->len;
*(p++) = '-';
memcpy( p , var_name, strlen(var_name));
p += strlen(var_name);
*(p++) = 0;
return s;
}
unsigned int calc_udp_load(void *val)
{
return ( get_stat_val((stat_var*)val) * 100) / *((int*)(((stat_var*)val)+1));
}
#ifdef USE_TCP
unsigned int calc_tcp_load(void *val)
{
return ( get_stat_val((stat_var*)val) * 100)/tcp_children_no;
}
#endif
int register_udp_load_stat(str *name, stat_var **s, int children)
{
char *stat_name;
/* in a single mem chunk, we put both the stat variable and the
number of children for this UDP interface */
*s = shm_malloc(sizeof(stat_var) + sizeof(int));
if (!*s) {
LM_ERR("no more shm\n");
return -1;
}
memset(*s,0,sizeof(stat_var));
*((int*)((*s)+1)) = children;
(*s)->u.val = shm_malloc(sizeof(stat_val));
if (!(*s)->u.val) {
LM_ERR("no more shm\n");
return -1;
}
memset((*s)->u.val,0,sizeof(stat_val));
if ( (stat_name = build_stat_name(name,"load")) == 0 ||
register_stat2("load",stat_name,(stat_var**)calc_udp_load,
STAT_IS_FUNC, *s, 0) != 0) {
LM_ERR("failed to add load stat\n");
return -1;
}
return 0;
}
#ifdef USE_TCP
int register_tcp_load_stat(stat_var **s)
{
*s = shm_malloc(sizeof(stat_var));
if (!*s) {
LM_ERR("no more shm\n");
return -1;
}
memset(*s,0,sizeof(stat_var));
(*s)->u.val = shm_malloc(sizeof(stat_val));
if (!(*s)->u.val) {
LM_ERR("no more shm\n");
return -1;
}
memset((*s)->u.val,0,sizeof(stat_val));
if (register_stat2("load","tcp-load",(stat_var**)calc_tcp_load,
STAT_IS_FUNC, *s, 0) != 0) {
LM_ERR("failed to add load stat\n");
return -1;
}
return 0;
}
#endif
/************* Functions for handling MODULEs(groups) of stats ***************/
static inline 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;
}
#define add_stat_module(module) \
__add_stat_module(module, 0)
static inline module_stats* __add_stat_module( char *module, int unsafe)
{
module_stats *amods;
module_stats *mods;
int len;
if ( (module==0) || ((len = strlen(module))==0 ) )
return 0;
amods = unsafe ?
(module_stats*)shm_realloc_unsafe( collector->amodules,
(collector->mod_no+1)*sizeof(module_stats))
:
(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;
mods->idx = collector->mod_no-1;
return mods;
}
/***************** Init / Destroy STATS support functions *******************/
struct pending_stat_name {
str* name;
struct pending_stat_name *next;
};
struct pending_stat_name *pending_name_list = NULL;
int clone_pv_stat_name(str *name, str *clone)
{
struct pending_stat_name *psn;
if (collector) {
/* if collector init'ed, simply clone it in shm */
clone->s = (char*)shm_malloc(name->len);
if (clone->s==NULL) {
LM_ERR("failed to allocated more shm mem (%d)\n",name->len);
return -1;
}
clone->len = name->len;
memcpy(clone->s,name->s,name->len);
} else {
/* just link it, we will move it to shm when callector available */
*clone = *name;
/* add to pending list */
psn = (struct pending_stat_name *)pkg_malloc( sizeof(struct pending_stat_name) );
if (psn==NULL) {
LM_ERR("failed to allocated more shm mem (%u)\n",
(unsigned int)sizeof(struct pending_stat_name));
return -1;
}
psn->name = clone;
psn->next = pending_name_list;
pending_name_list = psn;
}
return 0;
}
int init_stats_collector(void)
{
struct pending_stat_name *psn, *next;
module_stats *dy_mod;
char *s;
/* init the collector */
collector = (stats_collector*)shm_malloc_unsafe(sizeof(stats_collector));
if (collector==0) {
LM_ERR("no more shm mem\n");
goto error;
}
memset( collector, 0 , sizeof(stats_collector));
/* move pending stat names in shm */
for ( psn=pending_name_list ; psn ; psn=next ) {
next = psn->next;
s = (char*)shm_malloc_unsafe( psn->name->len );
if (s==NULL) {
LM_ERR("no more shm mem (%d)\n", psn->name->len);
goto error;
}
memcpy( s, psn->name->s, psn->name->len);
psn->name->s = s;
pkg_free(psn);
}
/*
* register shm statistics in an unsafe manner, as some allocators
* would actually attempt to update these statistics
* during their "safe" allocations -- Liviu
*/
if (__register_module_stats( "shmem", shm_stats, 1) != 0) {
LM_ERR("failed to register sh_mem statistics\n");
goto error;
}
#ifdef NO_ATOMIC_OPS
/* init BIG (really BIG) lock */
stat_lock = lock_alloc();
if (stat_lock==0 || lock_init( stat_lock )==0 ) {
LM_ERR("failed to init the really BIG lock\n");
goto error;
}
#endif
collector->rwl = (void*)lock_init_rw();
if (collector->rwl==NULL) {
LM_ERR("failed to create RW lock dynamic stats\n");
goto error;
}
/* register MI commands */
if (register_mi_mod( "statistics", mi_stat_cmds)<0) {
LM_ERR("unable to register MI cmds\n");
goto error;
}
/* register core statistics */
if (register_module_stats( "core", core_stats)!=0 ) {
LM_ERR("failed to register core statistics\n");
goto error;
}
/* register sh_mem statistics */
if (register_module_stats( "net", net_stats)!=0 ) {
LM_ERR("failed to register network statistics\n");
goto error;
}
/* create the module for "dynamic" statistics */
dy_mod = add_stat_module( DYNAMIC_MODULE_NAME );
if (dy_mod==NULL) {
LM_ERR("failed to create <%s> module\n",DYNAMIC_MODULE_NAME);
goto error;
}
/* mark it as dynamic, so it will require locking */
dy_mod->is_dyn = 1 ;
stats_ready = 1;
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;
#ifdef NO_ATOMIC_OPS
/* destroy big lock */
if (stat_lock)
lock_destroy( stat_lock );
#endif
if (collector) {
/* destroy hash tables */
for( i=0 ; i<STATS_HASH_SIZE ; i++ ) {
/* static stats */
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);
}
/* dynamic stats*/
for( stat=collector->dy_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 RW lock */
if (collector->rwl)
lock_destroy_rw( (rw_lock_t *)collector->rwl);
/* destroy the collector */
shm_free(collector);
}
return;
}
int stats_are_ready(void)
{
return stats_ready;
}
/********************* Create/Register STATS functions ***********************/
/**
* Note: certain statistics (e.g. shm statistics) require different handling,
* hence the <unsafe> parameter
*/
int register_stat2( char *module, char *name, stat_var **pvar,
unsigned short flags, void *ctx, int unsafe)
{
module_stats* mods;
stat_var **shash;
stat_var *stat;
stat_var *it;
str smodule;
int hash;
int name_len;
if (module==0 || name==0 || pvar==0) {
LM_ERR("invalid parameters module=%p, name=%p, pvar=%p \n",
module, name, pvar);
goto error;
}
name_len = strlen(name);
stat = unsafe ?
(stat_var*)shm_malloc_unsafe(sizeof(stat_var) +
((flags&STAT_SHM_NAME)==0)*name_len)
:
(stat_var*)shm_malloc(sizeof(stat_var) +
((flags&STAT_SHM_NAME)==0)*name_len);
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 = unsafe ?
(stat_val*)shm_malloc_unsafe(sizeof(stat_val)) :
(stat_val*)shm_malloc(sizeof(stat_val));
if (stat->u.val==0) {
LM_ERR("no more shm memory\n");
goto error1;
}
#ifdef NO_ATOMIC_OPS
*(stat->u.val) = 0;
#else
atomic_set(stat->u.val,0);
#endif
*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, 1);
if (mods==0) {
LM_ERR("failed to add new module\n");
goto error2;
}
}
/* fill the stat record */
stat->mod_idx = mods->idx;
stat->name.len = name_len;
if ( (flags&STAT_SHM_NAME)==0 ) {
stat->name.s = (char*)(stat+1);
memcpy(stat->name.s, name, name_len);
} else {
stat->name.s = name;
}
stat->flags = flags;
stat->context = ctx;
/* compute the hash by name */
hash = stat_hash( &stat->name );
/* link it into appropriate hash table , with or without locking */
if (mods->is_dyn) {
lock_start_write((rw_lock_t *)collector->rwl);
shash = collector->dy_hstats;
/* double check for duplicates (due race conditions) */
for( it=shash[hash] ; it ; it=stat->hnext ) {
if ( (it->name.len==stat->name.len) &&
(strncasecmp( it->name.s, stat->name.s, stat->name.len)==0) ) {
/* duplicate found -> drop current stat and return the
* found one */
lock_stop_write((rw_lock_t *)collector->rwl);
if (unsafe) {
if (flags&STAT_SHM_NAME)
shm_free_unsafe(stat->name.s);
if ((flags&STAT_IS_FUNC)==0)
shm_free_unsafe(stat->u.val);
shm_free_unsafe(stat);
} else {
if (flags&STAT_SHM_NAME)
shm_free(stat->name.s);
if ((flags&STAT_IS_FUNC)==0)
shm_free(stat->u.val);
shm_free(stat);
}
*pvar = it;
return 0;
}
}
/* new genuin stat-> continue */
} else {
shash = collector->hstats;
}
if (shash[hash]==0) {
shash[hash] = stat;
} else {
it = shash[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++;
if (mods->is_dyn)
lock_stop_write((rw_lock_t *)collector->rwl);
return 0;
error2:
if ( (flags&STAT_IS_FUNC)==0 ) {
if (unsafe)
shm_free_unsafe(*pvar);
else
shm_free(*pvar);
*pvar = 0;
}
error1:
if (unsafe)
shm_free_unsafe(stat);
else
shm_free(stat);
error:
if ( (flags&STAT_IS_FUNC)==0 )
*pvar = 0;
return -1;
}
int register_dynamic_stat( str *name, stat_var **pvar)
{
char *p;
int ret;
/*FIXME - what we do here is reallt stupid - convert from str to
* char and next function does the other way around - from char to
* str - this is temporary, before fixing the register_stat2 function
* prototype to accept str rather than char */
if ( (p=pkg_malloc( name->len + 1))==NULL ) {
LM_ERR("no more pkg mem (%d)\n",name->len + 1);
return -1;
}
memcpy( p, name->s, name->len);
p[name->len] = 0;
ret = register_stat( DYNAMIC_MODULE_NAME, p, pvar, 0/*flags*/);
pkg_free(p);
return ret;
}
int __register_module_stats(char *module, stat_export_t *stats, int unsafe)
{
int ret;
if (module==0 || module[0]==0 || !stats || !stats[0].name)
return 0;
for( ; stats->name ; stats++) {
ret = register_stat2( module, stats->name, stats->stat_pointer,
stats->flags, NULL, unsafe);
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 (collector==NULL || name==0 || name->s==0 || name->len==0)
return 0;
/* compute the hash by name */
hash = stat_hash( name );
/* and look for it , first in the hash for static stats */
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;
}
/* and then in the hash for dynamic stats */
lock_start_read((rw_lock_t *)collector->rwl);
for( stat=collector->dy_hstats[hash] ; stat ; stat=stat->hnext ) {
if ( (stat->name.len==name->len) &&
(strncasecmp( stat->name.s, name->s, name->len)==0) ) {
lock_stop_read((rw_lock_t *)collector->rwl);
return stat;
}
}
lock_stop_read((rw_lock_t *)collector->rwl);
return 0;
}
int mi_stat_name(str *mod, str *stat, str *out)
{
static str tmp_buf = {0, 0};
char *tmp;
if (mod) {
tmp = pkg_realloc(tmp_buf.s, mod->len + stat->len + 1);
if (!tmp) {
LM_ERR("no more pkg memory\n");
return -1;
}
tmp_buf.s = tmp;
memcpy(tmp_buf.s, mod->s, mod->len);
tmp_buf.len = mod->len;
tmp_buf.s[tmp_buf.len++] = ':';
memcpy(tmp_buf.s + tmp_buf.len, stat->s, stat->len);
tmp_buf.len += stat->len;
out->len = tmp_buf.len;
out->s = tmp_buf.s;
} else {
out->len = stat->len;
out->s = stat->s;
}
return 0;
}
int mi_print_stat(struct mi_node *rpl, str *mod, str *stat, unsigned long val)
{
str tmp_buf;
if (mi_stat_name(mod, stat, &tmp_buf) < 0) {
LM_ERR("cannot get stat name\n");
return -1;
}
if (!addf_mi_node_child(rpl, MI_DUP_NAME, tmp_buf.s, tmp_buf.len, "%lu", val)) {
LM_ERR("cannot add stat\n");
return -1;
}
return 0;
}
/***************************** MI STUFF ********************************/
inline static int mi_add_stat(struct mi_node *rpl, stat_var *stat)
{
return mi_print_stat(rpl, &collector->amodules[stat->mod_idx].name,
&stat->name, get_stat_val(stat));
}
inline static int mi_list_stat(struct mi_node *rpl, str *mod, stat_var *stat)
{
str tmp_buf;
char *buf;
if (mi_stat_name(mod, &stat->name, &tmp_buf) < 0) {
LM_ERR("cannot get stat name\n");
return -1;
}
if (stat->flags & STAT_IS_FUNC)
buf = "function";
if (stat->flags & STAT_NO_RESET)
buf = "non-incremental";
else
buf = "incremental";
if (!addf_mi_node_child(rpl, MI_DUP_NAME, tmp_buf.s, tmp_buf.len, "%s", buf)) {
LM_ERR("cannot add stat\n");
return -1;
}
return 0;
}
inline static int mi_add_module_stats(struct mi_node *rpl,
module_stats *mods)
{
stat_var *stat;
int ret = 0;
if (mods->is_dyn)
lock_start_read((rw_lock_t *)collector->rwl);
for( stat=mods->head ; stat ; stat=stat->lnext) {
ret = mi_print_stat(rpl, &mods->name, &stat->name,
get_stat_val(stat));
if (ret < 0)
break;
}
if (mods->is_dyn)
lock_stop_read((rw_lock_t *)collector->rwl);
return ret;
}
inline static int mi_list_module_stats(struct mi_node *rpl,
module_stats *mods)
{
stat_var *stat;
int ret = 0;
if (mods->is_dyn)
lock_start_read((rw_lock_t *)collector->rwl);
for( stat=mods->head ; stat ; stat=stat->lnext) {
ret = mi_list_stat(rpl, &mods->name, stat);
if (ret < 0)
break;
}
if (mods->is_dyn)
lock_stop_read((rw_lock_t *)collector->rwl);
return ret;
}
static struct mi_root *mi_get_stats(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *arg;
module_stats *mods;
stat_var *stat;
str val;
int i;
if (cmd->node.kids==NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
for( arg=cmd->node.kids ; arg ; arg=arg->next) {
if (arg->value.len==0)
continue;
val = arg->value;
if ( val.len==3 && memcmp(val.s,"all",3)==0) {
/* add all statistic variables */
for( i=0 ; i<collector->mod_no ;i++ ) {
if (mi_add_module_stats( rpl, &collector->amodules[i] )!=0)
goto error;
}
} else if ( val.len>1 && val.s[val.len-1]==':') {
/* add module statistics */
val.len--;
mods = get_stat_module( &val );
if (mods==0)
continue;
if (mi_add_module_stats( rpl, mods )!=0)
goto error;
} else {
/* add only one statistic */
stat = get_stat( &val );
if (stat==0)
continue;
if (mi_add_stat(rpl,stat)!=0)
goto error;
}
}
if (rpl->kids==0) {
free_mi_tree(rpl_tree);
return init_mi_tree( 404, "Statistics Not Found", 20);
}
return rpl_tree;
error:
free_mi_tree(rpl_tree);
return 0;
}
static struct mi_root *mi_list_stats(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *rpl;
struct mi_node *arg;
module_stats *mods;
stat_var *stat;
str val;
int i;
rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
if (rpl_tree==0)
return 0;
rpl = &rpl_tree->node;
if (cmd->node.kids == NULL) {
for( i=0 ; i<collector->mod_no ;i++ ) {
if (mi_list_module_stats( rpl, &collector->amodules[i] )!=0)
goto error;
}
} else {
for( arg=cmd->node.kids ; arg ; arg=arg->next) {
if (arg->value.len==0)
continue;
val = arg->value;
if ( val.len>1 && val.s[val.len-1]==':') {
/* add module statistics */
val.len--;
mods = get_stat_module( &val );
if (mods==0)
continue;
if (mi_list_module_stats( rpl, mods )!=0)
goto error;
} else {
/* add only one statistic */
stat = get_stat( &val );
if (stat==0)
continue;
if (mi_list_stat(rpl,NULL, stat)!=0)
goto error;
}
}
}
if (rpl->kids==0) {
free_mi_tree(rpl_tree);
return init_mi_tree( 404, "Statistics Not Found", 20);
}
return rpl_tree;
error:
free_mi_tree(rpl_tree);
return 0;
}
static struct mi_root *mi_reset_stats(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node *arg;
stat_var *stat;
int found;
if (cmd->node.kids==NULL)
return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
if (rpl_tree==0)
return 0;
found = 0;
for( arg=cmd->node.kids ; arg ; arg=arg->next) {
if (arg->value.len==0)
continue;
stat = get_stat( &arg->value );
if (stat==0)
continue;
reset_stat( stat );
found = 1;
}
if (!found) {
free_mi_tree(rpl_tree);
return init_mi_tree( 404, "Statistics Not Found", 20);
}
return rpl_tree;
}
#endif /*STATISTICS*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/maplerain/opensips.git
git@gitee.com:maplerain/opensips.git
maplerain
opensips
opensips
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891