- do not allow logger to write more than once per period

- fixed bug in statistics.c
This commit is contained in:
zolliker
2010-04-13 14:31:58 +00:00
parent 6c4f57ec6f
commit eebb6b06e9
3 changed files with 47 additions and 13 deletions

View File

@ -20,8 +20,9 @@ struct Logger {
char *old;
int oldsize;
int period;
time_t last, lastWrite;
time_t last, lastWrite, omitTime;
int numeric;
float omitValue;
int exact;
Logger *next;
};
@ -30,6 +31,7 @@ static char *dir = NULL;
static Logger *list;
static time_t lastLife = 0;
static time_t lastWritten = 0;
static time_t omitCheck = 0;
/*--------------------------------------------------------------------------*/
char *LoggerName(Logger * log)
{
@ -238,6 +240,8 @@ int LoggerWrite(Logger * log, time_t now, int period, char *value)
time_t h0;
static int yday = -1;
struct tm *tm;
char *valp;
char buf[32];
int l;
FILE *fil;
@ -283,6 +287,25 @@ int LoggerWrite(Logger * log, time_t now, int period, char *value)
if (fil)
fclose(fil);
}
if (now < log->last + log->period && strcmp(log->old, value) != 0) {
log->omitValue = strtod(value, &valp);
if (value != valp) {
log->omitTime = now;
return 1;
}
} else {
log->omitTime = 0;
}
if (now > omitCheck + 5) { /* check for omitted values only every 5 seconds */
for (p = list; p != NULL; p = p->next) {
if (p->omitTime > 0 && now > p->omitTime + p->period) {
snprintf(buf, sizeof buf, "%g", p->omitValue);
LoggerWrite0(p, p->omitTime, p->period, buf);
p->omitTime = 0;
}
}
omitCheck = now;
}
return LoggerWrite0(log, now, period, value);
}
@ -398,6 +421,8 @@ Logger *LoggerMake(char *name, int period, int exact)
t = lastLife + period;
}
LoggerWrite(log, t, period, ""); /* value was undefined since last life of server */
} else {
LoggerSetPeriod(log, period);
}
return log;
}

View File

@ -14,6 +14,7 @@ static hdbCallbackReturn LoggerUpdateCallback(pHdb node,
hdbValue value;
pHdbDataMessage mm = NULL;
pHdbDataSearch dsm = NULL;
time_t now;
if ((dsm = GetHdbDataSearchMessage(message)) != NULL) {
if (dsm->testPtr == loggerID) {
@ -29,9 +30,12 @@ static hdbCallbackReturn LoggerUpdateCallback(pHdb node,
value = *(mm->v);
time(&now);
if (now > LoggerLastTime(logger)) { /* never write more than once per second */
str = formatValue(value, node);
LoggerWrite(logger, time(NULL), LoggerPeriod(logger), GetCharArray(str));
DeleteDynString(str);
}
return hdbContinue;
}
@ -50,6 +54,7 @@ static int LogSetup(SConnection * pCon, SicsInterp * pSics, void *pData,
if (argc < 2) {
SCPrintf(pCon, eError,
"ERROR: should be: logsetup <node> [<period> [<filename>]]");
/* or logsetup <node> clear */
return 0;
}
if (strcasecmp(argv[1], "basepath") == 0) {
@ -77,10 +82,6 @@ static int LogSetup(SConnection * pCon, SicsInterp * pSics, void *pData,
SCPrintf(pCon, eError, "ERROR: %s not found", argv[1]);
return 0;
}
period = 0;
if (argc > 2) {
period = atoi(argv[2]);
}
if (argc > 3) {
snprintf(buf, sizeof buf, "%s", argv[3]);
} else {
@ -101,6 +102,14 @@ static int LogSetup(SConnection * pCon, SicsInterp * pSics, void *pData,
numeric = 0;
}
logger = FindHdbCallbackData(node, loggerID);
period = 0;
if (argc > 2) {
if (logger != NULL && strcasecmp(argv[2], "clear") == 0) {
LoggerWrite(logger, time(NULL), LoggerPeriod(logger), "");
return 1;
}
period = atoi(argv[2]);
}
if (logger != 0) { /* logger exists already, changed only period */
LoggerSetPeriod(logger, period);
} else {

View File

@ -58,8 +58,8 @@ int StatisticsCommand(SConnection * con, SicsInterp * pSics, void *pData,
gettimeofday(&now, 0);
dif = timeFloat(timeDif(lastStat, now));
SCPrintf(con, eValue, "calls/s time[%] full[%] mean[ms] command");
SCPrintf(con, eValue, "----------------------------------------------");
SCPrintf(con, eLog, "calls/s time[%] full[%] mean[ms] command");
SCPrintf(con, eLog, "----------------------------------------------");
for (p = list; p != NULL; p = p->next) {
if (dif > 0) {
calls = p->cnt / dif;
@ -71,7 +71,7 @@ int StatisticsCommand(SConnection * con, SicsInterp * pSics, void *pData,
} else {
dt = 0;
}
SCPrintf(con, eValue, "%7.1f %7.1f %7.1f %8.2f %s", calls,
SCPrintf(con, eLog, "%7.1f %7.1f %7.1f %8.2f %s", calls,
percent, full, dt, p->name);
}
}
@ -81,8 +81,8 @@ int StatisticsCommand(SConnection * con, SicsInterp * pSics, void *pData,
p->total.tv_sec = 0;
p->total.tv_usec = 0;
}
SCPrintf(con, eValue, "----------------------------------------------");
SCPrintf(con, eValue, "total time %.2f", dif);
SCPrintf(con, eLog, "----------------------------------------------");
SCPrintf(con, eLog, "total time %.2f", dif);
lastStat = now;
return 1;
}