- inserted command statistic
- add runscript parameter to environment object - added Arg2Tcl0 function
This commit is contained in:
149
statistics.c
Normal file
149
statistics.c
Normal file
@ -0,0 +1,149 @@
|
||||
#include <sics.h>
|
||||
#include <sys/time.h>
|
||||
#include "statistics.h"
|
||||
|
||||
typedef struct timeval tv_t;
|
||||
|
||||
struct Statistics {
|
||||
tv_t tim;
|
||||
long cnt;
|
||||
char *name;
|
||||
Statistics *next;
|
||||
};
|
||||
|
||||
static Statistics *current;
|
||||
static tv_t last, lastStat;
|
||||
static Statistics *idle = NULL, *list;
|
||||
static int init = 1;
|
||||
/*-----------------------------------------------------------------------*/
|
||||
tv_t timeDif(tv_t t1, tv_t t2) {
|
||||
tv_t result;
|
||||
|
||||
result.tv_usec = t2.tv_usec - t1.tv_usec;
|
||||
result.tv_sec = t2.tv_sec - t1.tv_sec;
|
||||
if (result.tv_usec < 0) {
|
||||
result.tv_usec += 1000000;
|
||||
result.tv_sec --;
|
||||
}
|
||||
if (result.tv_sec < 0) {
|
||||
result.tv_sec += 24*3600;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void timeAdd(tv_t *t1, tv_t t2) {
|
||||
t1->tv_usec += t2.tv_usec;
|
||||
t1->tv_sec += t2.tv_sec + (t1->tv_usec / 1000000);
|
||||
t1->tv_usec %= 1000000;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
double timeFloat(tv_t t) {
|
||||
return t.tv_sec + 1e-6 * t.tv_usec;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int StatisticsCommand(SConnection *con, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]) {
|
||||
Statistics *p;
|
||||
tv_t now;
|
||||
double dif, percent, dt;
|
||||
|
||||
gettimeofday(&now, 0);
|
||||
dif = timeFloat(timeDif(lastStat, now));
|
||||
SCPrintf(con, eStatus, " calls time[%] mean[ms] command");
|
||||
SCPrintf(con, eStatus, "--------------------------------------");
|
||||
for (p = list; p != NULL; p = p->next) {
|
||||
if (dif > 0) {
|
||||
percent = timeFloat(p->tim) * 100 / dif;
|
||||
if (percent > 0) {
|
||||
if (p->cnt > 0) {
|
||||
dt = timeFloat(p->tim) * 1000.0 / p->cnt;
|
||||
} else {
|
||||
dt = 0;
|
||||
}
|
||||
SCPrintf(con, eStatus, "%7ld %7.1f %8.2f %s", p->cnt,
|
||||
percent, dt, p->name);
|
||||
}
|
||||
}
|
||||
p->cnt = 0;
|
||||
p->tim.tv_sec = 0;
|
||||
p->tim.tv_usec = 0;
|
||||
}
|
||||
lastStat = now;
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
Statistics *StatisticsNew(char *name) {
|
||||
Statistics *new;
|
||||
|
||||
if (init) {
|
||||
gettimeofday(&lastStat, 0);
|
||||
last = lastStat;
|
||||
init = 0;
|
||||
}
|
||||
new = calloc(1,sizeof(*new));
|
||||
if (new) {
|
||||
new->cnt = 0;
|
||||
new->tim.tv_sec = 0;
|
||||
new->tim.tv_usec = 0;
|
||||
new->next = list;
|
||||
new->name = strdup(name);
|
||||
list = new;
|
||||
}
|
||||
return new;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatisticsKill(Statistics *stat) {
|
||||
Statistics *p, **last;
|
||||
|
||||
/* find in list */
|
||||
last = &list;
|
||||
for (p = list; p != NULL && p != stat; p = p->next) {
|
||||
last = &p->next;
|
||||
}
|
||||
|
||||
/* remove from list */
|
||||
if (p == stat) {
|
||||
*last = p->next;
|
||||
}
|
||||
|
||||
/* kill it */
|
||||
if (stat->name) {
|
||||
free(stat->name);
|
||||
stat->name = NULL;
|
||||
}
|
||||
free(stat);
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static void StatisticsSet(Statistics *stat) {
|
||||
tv_t now;
|
||||
|
||||
if (stat != NULL) {
|
||||
gettimeofday(&now, 0);
|
||||
timeAdd(¤t->tim, timeDif(last, now));
|
||||
last = now;
|
||||
}
|
||||
current = stat;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
Statistics *StatisticsBegin(Statistics *stat) {
|
||||
Statistics *res;
|
||||
|
||||
res = current;
|
||||
StatisticsSet(stat);
|
||||
current->cnt ++;
|
||||
return res;
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatisticsEnd(Statistics *stat) {
|
||||
StatisticsSet(stat);
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
void StatisticsInit(void) {
|
||||
if (idle == NULL) {
|
||||
AddCmd("statistics", StatisticsCommand);
|
||||
last = lastStat;
|
||||
idle = StatisticsNew("<idle>");
|
||||
current = idle;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user