From d9e64c16e52357bae1eb00fc8403c4e63d6365ca Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 4 Jun 2010 12:45:31 +0200 Subject: finshed implementation of strgen modules and also provided four build-in modules for the most common use cases, hopefully resulting in a speedup of around 5% for typical rsyslog processing. --- tools/smtradfile.c | 119 +++++++++++++++++++++-------------------------------- 1 file changed, 47 insertions(+), 72 deletions(-) (limited to 'tools/smtradfile.c') diff --git a/tools/smtradfile.c b/tools/smtradfile.c index dcbc1d47..103c367e 100644 --- a/tools/smtradfile.c +++ b/tools/smtradfile.c @@ -1,6 +1,9 @@ /* smtradfile.c * This is a strgen module for the traditional file format. * + * Format generated: + * "%TIMESTAMP% %HOSTNAME% %syslogtag%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n" + * * NOTE: read comments in module-template.h to understand how this file * works! * @@ -37,10 +40,6 @@ #include "template.h" #include "msg.h" #include "module-template.h" -#include "glbl.h" -#include "errmsg.h" -#include "parser.h" -#include "datetime.h" #include "unicode-helper.h" MODULE_TYPE_STRGEN @@ -49,74 +48,61 @@ STRGEN_NAME("RSYSLOG_TraditionalFileFormat") /* internal structures */ DEF_SMOD_STATIC_DATA -DEFobjCurrIf(errmsg) -DEFobjCurrIf(glbl) -DEFobjCurrIf(parser) -DEFobjCurrIf(datetime) /* config data */ -#warning TODO: ExtendBuf via object interface! -extern rsRetVal ExtendBuf(uchar **pBuf, size_t *pLenBuf, size_t iMinSize); -extern char *getTimeReported(msg_t *pM, enum tplFormatTypes eFmt); - +/* This strgen tries to minimize the amount of reallocs be first obtaining pointers to all strings + * needed (including their length) and then calculating the actual space required. So when we + * finally copy, we know exactly what we need. So we do at most one alloc. + */ BEGINstrgen - int iBuf; - uchar *pVal; - size_t iLenVal; + register int iBuf; + uchar *pTimeStamp; + uchar *pHOSTNAME; + size_t lenHOSTNAME; + uchar *pTAG; + int lenTAG; + uchar *pMSG; + size_t lenMSG; + size_t lenTotal; CODESTARTstrgen - dbgprintf("XXX: strgen module, *ppBuf = %p\n", *ppBuf); - iBuf = 0; - dbgprintf("TIMESTAMP\n"); - /* TIMESTAMP + ' ' */ - dbgprintf("getTimeReported\n"); - pVal = (uchar*) getTimeReported(pMsg, tplFmtDefault); - dbgprintf("obtain iLenVal ptr %p\n", pVal); - iLenVal = ustrlen(pVal); - dbgprintf("TIMESTAMP pVal='%p', iLenVal=%d\n", pVal, iLenVal); - if(iBuf + iLenVal + 1 >= *pLenBuf) /* we reserve one char for the final \0! */ - CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1)); - memcpy(*ppBuf + iBuf, pVal, iLenVal); - iBuf += iLenVal; + DBGPRINTF("XXX: smtradfile strgen called\n"); + /* first obtain all strings and their length (if not fixed) */ + pTimeStamp = (uchar*) getTimeReported(pMsg, tplFmtRFC3164Date); + pHOSTNAME = (uchar*) getHOSTNAME(pMsg); + lenHOSTNAME = getHOSTNAMELen(pMsg); + getTAG(pMsg, &pTAG, &lenTAG); + pMSG = getMSG(pMsg); + lenMSG = getMSGLen(pMsg); + + /* calculate len, constants for spaces and similar fixed strings */ + lenTotal = CONST_LEN_TIMESTAMP_3164 + 1 + lenHOSTNAME + 1 + lenTAG + lenMSG + 2; + if(pMSG[0] != ' ') + ++lenTotal; /* then we need to introduce one additional space */ + + /* now make sure buffer is large enough */ + if(lenTotal >= *pLenBuf) + CHKiRet(ExtendBuf(ppBuf, pLenBuf, lenTotal)); + + /* and concatenate the resulting string */ + memcpy(*ppBuf, pTimeStamp, CONST_LEN_TIMESTAMP_3164); + *(*ppBuf + CONST_LEN_TIMESTAMP_3164) = ' '; + + memcpy(*ppBuf + CONST_LEN_TIMESTAMP_3164 + 1, pHOSTNAME, lenHOSTNAME); + iBuf = CONST_LEN_TIMESTAMP_3164 + 1 + lenHOSTNAME; *(*ppBuf + iBuf++) = ' '; - dbgprintf("HOSTNAME\n"); - /* HOSTNAME + ' ' */ - pVal = (uchar*) getHOSTNAME(pMsg); - iLenVal = getHOSTNAMELen(pMsg); - if(iBuf + iLenVal + 1 >= *pLenBuf) /* we reserve one char for the ' '! */ - CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1)); - memcpy(*ppBuf + iBuf, pVal, iLenVal); - iBuf += iLenVal; - *(*ppBuf + iBuf++) = ' '; + memcpy(*ppBuf + iBuf, pTAG, lenTAG); + iBuf += lenTAG; - dbgprintf("TAG\n"); - /* syslogtag */ - /* max size for TAG assumed 200 * TODO: check! */ - if(iBuf + 200 >= *pLenBuf) - CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + 200)); - getTAG(pMsg, &pVal, &iLenVal); - memcpy(*ppBuf + iBuf, pVal, iLenVal); - iBuf += iLenVal; - - dbgprintf("MSG\n"); - /* MSG, plus leading space if necessary */ - pVal = getMSG(pMsg); - iLenVal = getMSGLen(pMsg); - if(iBuf + iLenVal + 1 >= *pLenBuf) /* we reserve one char for the leading SP*/ - CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1)); - if(pVal[0] != ' ') + if(pMSG[0] != ' ') *(*ppBuf + iBuf++) = ' '; - memcpy(*ppBuf + iBuf, pVal, iLenVal); - iBuf += iLenVal; - - dbgprintf("Trailer\n"); - /* end sequence */ - iLenVal = 2; - if(iBuf + iLenVal >= *pLenBuf) /* we reserve one char for the final \0! */ - CHKiRet(ExtendBuf(ppBuf, pLenBuf, iBuf + iLenVal + 1)); + memcpy(*ppBuf + iBuf, pMSG, lenMSG); + iBuf += lenMSG; + + /* trailer */ *(*ppBuf + iBuf++) = '\n'; *(*ppBuf + iBuf) = '\0'; @@ -126,11 +112,6 @@ ENDstrgen BEGINmodExit CODESTARTmodExit - /* release what we no longer need */ - objRelease(errmsg, CORE_COMPONENT); - objRelease(glbl, CORE_COMPONENT); - objRelease(parser, CORE_COMPONENT); - objRelease(datetime, CORE_COMPONENT); ENDmodExit @@ -144,11 +125,5 @@ BEGINmodInit(smtradfile) CODESTARTmodInit *ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ CODEmodInit_QueryRegCFSLineHdlr - CHKiRet(objUse(glbl, CORE_COMPONENT)); - CHKiRet(objUse(errmsg, CORE_COMPONENT)); - CHKiRet(objUse(parser, CORE_COMPONENT)); - CHKiRet(objUse(datetime, CORE_COMPONENT)); - dbgprintf("traditional file format strgen init called, compiled with version %s\n", VERSION); - dbgprintf("GetStrgenName addr %p\n", GetStrgenName); ENDmodInit -- cgit