From eebb6b06e99777e102670438b5ca7525d40c7e41 Mon Sep 17 00:00:00 2001 From: zolliker Date: Tue, 13 Apr 2010 14:31:58 +0000 Subject: [PATCH] - do not allow logger to write more than once per period - fixed bug in statistics.c --- logger.c | 27 ++++++++++++++++++++++++++- logsetup.c | 23 ++++++++++++++++------- statistics.c | 10 +++++----- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/logger.c b/logger.c index e3e023ae..e175f8cd 100644 --- a/logger.c +++ b/logger.c @@ -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; } diff --git a/logsetup.c b/logsetup.c index a43b0207..a57c7ee6 100644 --- a/logsetup.c +++ b/logsetup.c @@ -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); - str = formatValue(value, node); - LoggerWrite(logger, time(NULL), LoggerPeriod(logger), GetCharArray(str)); - DeleteDynString(str); + 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 [ []]"); + /* or logsetup 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 { diff --git a/statistics.c b/statistics.c index cddbbc8c..406be5a4 100644 --- a/statistics.c +++ b/statistics.c @@ -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; }