diff --git a/source/bulkdata/profile.c b/source/bulkdata/profile.c index cb4284fc..6d7c094e 100644 --- a/source/bulkdata/profile.c +++ b/source/bulkdata/profile.c @@ -37,6 +37,7 @@ #include "dcautil.h" #include "t2parser.h" #include "rbusInterface.h" +#include "legacyutils.h" #if defined(PRIVACYMODES_CONTROL) #include "rdkservices_privacyutils.h" @@ -172,6 +173,10 @@ static void freeProfile(void *data) { Vector_Destroy(profile->gMarkerList, freeGMarker); } + if(profile->topMarkerList) + { + Vector_Destroy(profile->topMarkerList, freeGMarker); + } if(profile->paramList) { Vector_Destroy(profile->paramList, freeParam); @@ -322,6 +327,7 @@ static void* CollectAndReport(void* data) pthread_cond_init(&profile->reuseThread, NULL); pthread_mutex_lock(&profile->reuseThreadMutex); profile->threadExists = true; + //GrepSeekProfile *GPF = profile->GrepSeekProfle; do { T2Info("%s while Loop -- START \n", __FUNCTION__); @@ -330,6 +336,8 @@ static void* CollectAndReport(void* data) pthread_cond_signal(&profile->reportInProgressCond); pthread_mutex_unlock(&profile->reportInProgressMutex); + int count = profile->grepSeekProfile->execCounter; + Vector *profileParamVals = NULL; Vector *grepResultList = NULL; cJSON *valArray = NULL; @@ -344,6 +352,7 @@ static void* CollectAndReport(void* data) char* customLogPath = NULL; + T2ERROR ret = T2ERROR_FAILURE; if( profile->name == NULL || profile->encodingType == NULL || profile->protocol == NULL ) { @@ -434,16 +443,33 @@ static void* CollectAndReport(void* data) if(profile->paramList != NULL && Vector_Size(profile->paramList) > 0) { T2Debug("Fetching TR-181 Object/Parameter Values\n"); - profileParamVals = getProfileParameterValues(profile->paramList); + profileParamVals = getProfileParameterValues(profile->paramList, count); if(profileParamVals != NULL) { encodeParamResultInJSON(valArray, profile->paramList, profileParamVals); } Vector_Destroy(profileParamVals, freeProfileValues); } + if(profile->topMarkerList != NULL && Vector_Size(profile->topMarkerList) > 0) + { + Vector *topMarkerResultList = NULL; + Vector_Create(&topMarkerResultList); + processTopPattern(profile->name, profile->topMarkerList, topMarkerResultList, 0); + long int reportSize = Vector_Size(topMarkerResultList); + if(reportSize != 0) + { + T2Info("Top markers report is compleated report size %ld\n", (unsigned long)reportSize); + encodeGrepResultInJSON(valArray, topMarkerResultList); + } + else + { + T2Debug("Top markers report generated but is empty possabliy the memory value is changed"); + } + Vector_Destroy(topMarkerResultList, freeGResult); + } if(profile->gMarkerList != NULL && Vector_Size(profile->gMarkerList) > 0) { - getGrepResults(profile->name, profile->gMarkerList, &grepResultList, profile->bClearSeekMap, false, customLogPath); // Passing 5th argument as false so that it doesn't check rotated logs for the first reporting after bootup for multiprofiles. + getGrepResults(&(profile->grepSeekProfile), profile->gMarkerList, &grepResultList, profile->bClearSeekMap, false, customLogPath); // Passing 5th argument as false so that it doesn't check rotated logs for the first reporting after bootup for multiprofiles. encodeGrepResultInJSON(valArray, grepResultList); Vector_Destroy(grepResultList, freeGResult); } @@ -484,7 +510,7 @@ static void* CollectAndReport(void* data) #ifdef PERSIST_LOG_MON_REF if(profile->saveSeekConfig) { - saveSeekConfigtoFile(profile->name); + saveSeekConfigtoFile(profile->name, profile->grepSeekProfile); } if(profile->checkPreviousSeek) { @@ -1168,9 +1194,9 @@ T2ERROR deleteAllProfiles(bool delFromDisk) pthread_join(tempProfile->reportThread, NULL); tempProfile->threadExists = false; } - if (Vector_Size(tempProfile->gMarkerList) > 0) + if(tempProfile->grepSeekProfile) { - removeGrepConfig(tempProfile->name, true, true); + freeGrepSeekProfile(tempProfile->grepSeekProfile); } pthread_mutex_unlock(&plMutex); if(delFromDisk == true) @@ -1273,9 +1299,9 @@ T2ERROR deleteProfile(const char *profileName) rbusT2ConsumerUnReg(profile->triggerConditionList); } - if (Vector_Size(profile->gMarkerList) > 0) + if(profile->grepSeekProfile) { - removeGrepConfig((char*)profileName, true, true); + freeGrepSeekProfile(profile->grepSeekProfile); } pthread_mutex_destroy(&profile->reportInProgressMutex); @@ -1392,7 +1418,7 @@ static void loadReportProfilesFromDisk(bool checkPreviousSeek) if(T2ERROR_SUCCESS == addProfile(profile)) { #ifdef PERSIST_LOG_MON_REF - if(checkPreviousSeek && profile->generateNow == false && profile->triggerConditionList == NULL && loadSavedSeekConfig(profile->name) == T2ERROR_SUCCESS && firstBootStatus() ) + if(checkPreviousSeek && profile->generateNow == false && profile->triggerConditionList == NULL && profile->grepSeekProfile && loadSavedSeekConfig(profile->name, profile->grepSeekProfile) == T2ERROR_SUCCESS && firstBootStatus() ) { profile->checkPreviousSeek = true; } diff --git a/source/bulkdata/profile.h b/source/bulkdata/profile.h index ff7df7fa..5a382864 100644 --- a/source/bulkdata/profile.h +++ b/source/bulkdata/profile.h @@ -31,6 +31,7 @@ #include "t2common.h" #include "vector.h" #include "reportgen.h" +#include "legacyutils.h" typedef struct _JSONEncoding { @@ -77,6 +78,7 @@ typedef struct _Profile T2RBUS *t2RBUSDest; Vector *eMarkerList; Vector *gMarkerList; + Vector *topMarkerList; Vector *cachedReportList; cJSON *jsonReportObj; pthread_t reportThread; @@ -90,6 +92,7 @@ typedef struct _Profile pthread_cond_t reuseThread; pthread_mutex_t reuseThreadMutex; bool threadExists; + GrepSeekProfile *grepSeekProfile; // To store GrepConfig } Profile; T2ERROR initProfileList(bool checkPreviousSeek); diff --git a/source/bulkdata/profilexconf.c b/source/bulkdata/profilexconf.c index dedd9128..0b59be6c 100644 --- a/source/bulkdata/profilexconf.c +++ b/source/bulkdata/profilexconf.c @@ -35,6 +35,7 @@ #include "vector.h" #include "dcautil.h" #include "t2parserxconf.h" +#include "legacyutils.h" #define T2REPORT_HEADER "T2" #define T2REPORT_HEADERVAL "1.0" @@ -119,6 +120,10 @@ static void freeProfileXConf() { Vector_Destroy(singleProfile->gMarkerList, freeGMarker); } + if(singleProfile->topMarkerList) + { + Vector_Destroy(singleProfile->topMarkerList, freeGMarker); + } if(singleProfile->paramList) { Vector_Destroy(singleProfile->paramList, freeParam); @@ -136,6 +141,10 @@ static void freeProfileXConf() free(singleProfile->cachedReportList); singleProfile->cachedReportList = NULL; } + if(singleProfile->grepSeekProfile) + { + freeGrepSeekProfile(singleProfile->grepSeekProfile); + } free(singleProfile); singleProfile = NULL; } @@ -202,6 +211,7 @@ static void* CollectAndReportXconf(void* data) } pthread_cond_init(&reuseThread, NULL); reportThreadExits = true; + //GrepSeekProfile *GPF = profile->grepSeekProfile; do { T2Info("%s while Loop -- START \n", __FUNCTION__); @@ -213,10 +223,15 @@ static void* CollectAndReportXconf(void* data) char* customLogPath = NULL; bool checkRotated = true; + int count = profile->grepSeekProfile->execCounter; + T2Debug("CollectAndReportXconf count = %d\n", count); + struct timespec startTime; struct timespec endTime; struct timespec elapsedTime; + + T2ERROR ret = T2ERROR_FAILURE; if(profile->name != NULL) { @@ -252,7 +267,7 @@ static void* CollectAndReportXconf(void* data) if(profile->paramList != NULL && Vector_Size(profile->paramList) > 0) { - profileParamVals = getProfileParameterValues(profile->paramList); + profileParamVals = getProfileParameterValues(profile->paramList, count); T2Info("Fetch complete for TR-181 Object/Parameter Values for parameters \n"); if(profileParamVals != NULL) { @@ -260,9 +275,26 @@ static void* CollectAndReportXconf(void* data) } Vector_Destroy(profileParamVals, freeProfileValues); } + if(profile->topMarkerList != NULL && Vector_Size(profile->topMarkerList) > 0) + { + Vector *topMarkerResultList = NULL; + Vector_Create(&topMarkerResultList); + processTopPattern(profile->name, profile->topMarkerList, topMarkerResultList, count); + long int reportSize = Vector_Size(topMarkerResultList); + if(reportSize != 0) + { + T2Info("Top markers report is compleated report size %ld\n", (unsigned long)reportSize); + encodeGrepResultInJSON(valArray, topMarkerResultList); + } + else + { + T2Debug("Top markers report generated but is empty possabliy the memory value is changed"); + } + Vector_Destroy(topMarkerResultList, freeGResult); + } if(profile->gMarkerList != NULL && Vector_Size(profile->gMarkerList) > 0) { - getGrepResults(profile->name, profile->gMarkerList, &grepResultList, profile->bClearSeekMap, checkRotated, customLogPath); // Passing 5th argument as true to check rotated logs only in case of single profile + getGrepResults(&(profile->grepSeekProfile), profile->gMarkerList, &grepResultList, profile->bClearSeekMap, checkRotated, customLogPath); // Passing 5th argument as true to check rotated logs only in case of single profile T2Info("Grep complete for %lu markers \n", (unsigned long)Vector_Size(profile->gMarkerList)); encodeGrepResultInJSON(valArray, grepResultList); Vector_Destroy(grepResultList, freeGResult); @@ -277,7 +309,8 @@ static void* CollectAndReportXconf(void* data) ret = prepareJSONReport(profile->jsonReportObj, &jsonReport); destroyJSONReport(profile->jsonReportObj); profile->jsonReportObj = NULL; - + clock_gettime(CLOCK_REALTIME, &endTime); + T2Info("Processing time for profile %s is %ld seconds\n", profile->name, (long)(endTime.tv_sec - startTime.tv_sec)); if(ret != T2ERROR_SUCCESS) { T2Error("Unable to generate report for : %s\n", profile->name); @@ -348,7 +381,7 @@ static void* CollectAndReportXconf(void* data) #ifdef PERSIST_LOG_MON_REF if(profile->saveSeekConfig) { - saveSeekConfigtoFile(profile->name); + saveSeekConfigtoFile(profile->name, profile->grepSeekProfile); } if(profile->checkPreviousSeek) { @@ -405,7 +438,7 @@ static void* CollectAndReportXconf(void* data) } # ifdef PERSIST_LOG_MON_REF - if(T2ERROR_SUCCESS == saveSeekConfigtoFile(profile->name)) + if(T2ERROR_SUCCESS == saveSeekConfigtoFile(profile->name, profile->grepSeekProfile)) { T2Info("Successfully saved grep config to file for profile: %s\n", profile->name); } @@ -497,7 +530,7 @@ T2ERROR ProfileXConf_init(bool checkPreviousSeek) if(T2ERROR_SUCCESS == processConfigurationXConf(config->configData, &profile)) { #ifdef PERSIST_LOG_MON_REF - if(checkPreviousSeek && loadSavedSeekConfig(profile->name) == T2ERROR_SUCCESS && firstBootStatus()) + if(checkPreviousSeek && profile->grepSeekProfile && loadSavedSeekConfig(profile->name, profile->grepSeekProfile) == T2ERROR_SUCCESS && firstBootStatus()) { profile->checkPreviousSeek = true; } @@ -640,8 +673,9 @@ bool ProfileXConf_isNameEqual(char* profileName) { if(singleProfile && (singleProfile->name != NULL) && (profileName != NULL) && !strcmp(singleProfile->name, profileName)) //Adding NULL check to avoid strcmp crash { - T2Info("singleProfile->name = %s and profileName = %s\n", singleProfile->name, profileName); isName = true; + T2Info("singleProfile->name = %s and profileName = %s and return %s\n", singleProfile->name, profileName, isName ? "true" : "false"); + } } pthread_mutex_unlock(&plMutex); @@ -656,7 +690,7 @@ T2ERROR ProfileXConf_delete(ProfileXConf *profile) T2Error("profile list is not initialized yet, ignoring\n"); return T2ERROR_FAILURE; } - + T2Debug("calling ProfileXConf_isNameEqual function form %s and line %d\n", __FUNCTION__, __LINE__); bool isNameEqual = ProfileXConf_isNameEqual(profile->name); pthread_mutex_lock(&plMutex); @@ -787,10 +821,12 @@ T2ERROR ProfileXConf_delete(ProfileXConf *profile) if (Vector_Size(singleProfile->gMarkerList) > 0 ) { - bool clearSeekMap = true; if(isNameEqual) { - clearSeekMap = false; + freeGrepSeekProfile(profile->grepSeekProfile); + profile->grepSeekProfile = singleProfile->grepSeekProfile; + profile->grepSeekProfile->execCounter = 0; + singleProfile->grepSeekProfile = NULL; } #ifdef PERSIST_LOG_MON_REF else @@ -798,7 +834,6 @@ T2ERROR ProfileXConf_delete(ProfileXConf *profile) removeProfileFromDisk(SEEKFOLDER, singleProfile->name); } #endif - removeGrepConfig(singleProfile->name, clearSeekMap, true); } diff --git a/source/bulkdata/profilexconf.h b/source/bulkdata/profilexconf.h index f55f3589..f22dfb90 100644 --- a/source/bulkdata/profilexconf.h +++ b/source/bulkdata/profilexconf.h @@ -29,6 +29,8 @@ #include "t2eventreceiver.h" #include "vector.h" #include "reportgen.h" +#include "t2collection.h" +#include "legacyutils.h" typedef enum { @@ -61,9 +63,11 @@ typedef struct _ProfileXConf T2HTTP *t2HTTPDest; Vector *eMarkerList; Vector *gMarkerList; + Vector *topMarkerList; Vector *cachedReportList; cJSON *jsonReportObj; pthread_t reportThread; + GrepSeekProfile *grepSeekProfile; // To store GrepConfig } ProfileXConf; T2ERROR ProfileXConf_init(bool checkPreviousSeek); diff --git a/source/bulkdata/reportprofiles.c b/source/bulkdata/reportprofiles.c index c5f3a6f5..dbe48be5 100644 --- a/source/bulkdata/reportprofiles.c +++ b/source/bulkdata/reportprofiles.c @@ -219,6 +219,7 @@ void ReportProfiles_TimeoutCb(char* profileName, bool isClearSeekMap) { T2Info("%s ++in\n", __FUNCTION__); + T2Info("calling ProfileXConf_isNameEqual function form %s and line %d\n", __FUNCTION__, __LINE__); if(ProfileXConf_isNameEqual(profileName)) { T2Debug("isclearSeekmap = %s \n", isClearSeekMap ? "true" : "false"); @@ -238,6 +239,8 @@ void ReportProfiles_ActivationTimeoutCb(char* profileName) T2Info("%s ++in\n", __FUNCTION__); bool isDeleteRequired = false; + T2Debug("calling ProfileXConf_isNameEqual function form %s and line %d\n", __FUNCTION__, __LINE__); + if(ProfileXConf_isNameEqual(profileName)) { T2Error("ActivationTimeout received for Xconf profile. Ignoring!!!! \n"); @@ -281,6 +284,7 @@ void ReportProfiles_ActivationTimeoutCb(char* profileName) T2ERROR ReportProfiles_storeMarkerEvent(char *profileName, T2Event *eventInfo) { T2Debug("%s ++in\n", __FUNCTION__); + T2Debug("calling ProfileXConf_isNameEqual function form %s and line %d\n", __FUNCTION__, __LINE__); if(ProfileXConf_isNameEqual(profileName)) { @@ -636,6 +640,10 @@ T2ERROR initReportProfiles() } + //Initialise the properties file RDK-58222 + T2InitProperties(); + T2Info("InitProperties is successful\n"); + // This indicates telemetry has started FILE* bootFlag = NULL ; bootFlag = fopen(BOOTFLAG, "w+"); @@ -1380,7 +1388,7 @@ int __ReportProfiles_ProcessReportProfilesMsgPackBlob(void *msgpack, bool checkP { ReportProfiles_addReportProfile(profile); #ifdef PERSIST_LOG_MON_REF - if(checkPreviousSeek && profile->generateNow == false && profile->triggerConditionList == NULL && loadSavedSeekConfig(profile->name) == T2ERROR_SUCCESS && firstBootStatus() ) + if(checkPreviousSeek && profile->generateNow == false && profile->triggerConditionList == NULL && profile->grepSeekProfile && loadSavedSeekConfig(profile->name, profile->grepSeekProfile) == T2ERROR_SUCCESS && firstBootStatus() ) { T2Info("Previous seek is enabled for profile %s \n", profile->name); profile->checkPreviousSeek = true; diff --git a/source/bulkdata/t2eventreceiver.c b/source/bulkdata/t2eventreceiver.c index 1ef4c974..5099e0e0 100644 --- a/source/bulkdata/t2eventreceiver.c +++ b/source/bulkdata/t2eventreceiver.c @@ -44,6 +44,42 @@ static pthread_mutex_t sTDMutex; T2ERROR ReportProfiles_storeMarkerEvent(char *profileName, T2Event *eventInfo); +/** +* @brief Function like strstr but based on the string delimiter. +* +* @param[in] str String. +* @param[in] delim Delimiter. +* +* @return Returns the output string. +*/ +static char *strSplit(char *str, char *delim) +{ + static char *next_str; + char *last = NULL; + if(str != NULL) + { + next_str = str; + } + + if(NULL == next_str) + { + return next_str; + } + + last = strstr(next_str, delim); + if(NULL == last) + { + char *ret = next_str; + next_str = NULL; + return ret; + } + + char *ret = next_str; + *last = '\0'; + next_str = last + strlen(delim); + return ret; +} + void freeT2Event(void *data) { if(data != NULL) diff --git a/source/ccspinterface/Makefile.am b/source/ccspinterface/Makefile.am index 67d01c3b..af372e01 100644 --- a/source/ccspinterface/Makefile.am +++ b/source/ccspinterface/Makefile.am @@ -37,7 +37,9 @@ libccspinterface_la_CPPFLAGS = -fPIC $(CPPFLAGS) -I${PKG_CONFIG_SYSROOT_DIR}$(in -I${top_srcdir}/source/utils \ -I${top_srcdir}/source/t2ssp \ -I${top_srcdir}/source/bulkdata \ - -I${top_srcdir}/source/reportgen + -I${top_srcdir}/source/reportgen \ + -I${top_srcdir}/source/utils \ + -I${top_srcdir}/source/dcautil if IS_PRIVACYCONTROL_ENABLED libccspinterface_la_CFLAGS += $(PRIVACYCONTROL_FLAG) diff --git a/source/ccspinterface/busInterface.c b/source/ccspinterface/busInterface.c index 228586d4..60c0051a 100644 --- a/source/ccspinterface/busInterface.c +++ b/source/ccspinterface/busInterface.c @@ -88,7 +88,7 @@ T2ERROR getParameterValue(const char* paramName, char **paramValue) return ret; } -Vector* getProfileParameterValues(Vector *paramList) +Vector* getProfileParameterValues(Vector *paramList, int count) { T2Debug("%s ++in\n", __FUNCTION__); Vector *profileValueList = NULL; @@ -99,12 +99,12 @@ Vector* getProfileParameterValues(Vector *paramList) if(isRbus) { - profileValueList = getRbusProfileParamValues(paramList); + profileValueList = getRbusProfileParamValues(paramList, count); } #if defined(CCSP_SUPPORT_ENABLED) else { - profileValueList = getCCSPProfileParamValues(paramList); + profileValueList = getCCSPProfileParamValues(paramList, count); } #endif diff --git a/source/ccspinterface/busInterface.h b/source/ccspinterface/busInterface.h index 96dd6706..de0f33e9 100644 --- a/source/ccspinterface/busInterface.h +++ b/source/ccspinterface/busInterface.h @@ -85,7 +85,7 @@ bool isRbusEnabled(); T2ERROR getParameterValue(const char* paramName, char **paramValue); -Vector* getProfileParameterValues(Vector *paramList); +Vector* getProfileParameterValues(Vector *paramList, int count); void freeParamValueSt(tr181ValStruct_t **valStructs, int valSize); diff --git a/source/ccspinterface/ccspinterface.c b/source/ccspinterface/ccspinterface.c index 2ffd24de..945884f3 100644 --- a/source/ccspinterface/ccspinterface.c +++ b/source/ccspinterface/ccspinterface.c @@ -224,7 +224,7 @@ T2ERROR getCCSPParamVal(const char* paramName, char **paramValue) return T2ERROR_SUCCESS; } -Vector* getCCSPProfileParamValues(Vector *paramList) +Vector* getCCSPProfileParamValues(Vector *paramList, int execount) { unsigned int i = 0; int count = Vector_Size(paramList); @@ -257,13 +257,22 @@ Vector* getCCSPProfileParamValues(Vector *paramList) T2Error("Unable allocate memory for profVals\n"); continue; } - paramNames[0] = strdup(((Param *) Vector_At(paramList, i))->alias); + Param *param = (Param *) Vector_At(paramList, i); + paramNames[0] = strdup(param->alias); if(paramNames[0] == NULL) { T2Error("Unable allocate memory for paramNames[0]\n"); free(profVals); continue; } + if(param->skipFreq > 0 && (execount % param->skipFreq + 1) != 0) + { + T2Info("Skipping parameter : %s as per skipFreq : %d\n", paramNames[0], param->skipFreq); + free(paramNames[0]); + profVals->paramValues = NULL; + Vector_PushBack(profileValueList, profVals); + continue; + } if(T2ERROR_SUCCESS != ccspGetParameterValues((const char**)paramNames, 1, &ccspParamValues, ¶mValCount)) { T2Error("Failed to retrieve param : %s\n", paramNames[0]); diff --git a/source/ccspinterface/ccspinterface.h b/source/ccspinterface/ccspinterface.h index 780297e8..446dd480 100644 --- a/source/ccspinterface/ccspinterface.h +++ b/source/ccspinterface/ccspinterface.h @@ -33,7 +33,7 @@ bool isCCSPInitialized(); T2ERROR getCCSPParamVal(const char* paramName, char **paramValue); -Vector* getCCSPProfileParamValues(Vector *paramList); +Vector* getCCSPProfileParamValues(Vector *paramList, int execount); T2ERROR registerCcspT2EventListener(TelemetryEventCallback eventCB); diff --git a/source/ccspinterface/rbusInterface.c b/source/ccspinterface/rbusInterface.c index a087a09f..c1420aaa 100644 --- a/source/ccspinterface/rbusInterface.c +++ b/source/ccspinterface/rbusInterface.c @@ -208,7 +208,7 @@ T2ERROR getRbusParameterVal(const char* paramName, char **paramValue) return T2ERROR_SUCCESS; } -Vector* getRbusProfileParamValues(Vector *paramList) +Vector* getRbusProfileParamValues(Vector *paramList, int execcount) { T2Debug("%s ++in\n", __FUNCTION__); unsigned int i = 0; @@ -230,8 +230,17 @@ Vector* getRbusProfileParamValues(Vector *paramList) rbusProperty_t rbusPropertyValues = NULL; int paramValCount = 0; int iterate = 0; + Param *parmTemp = Vector_At(paramList, i); profileValues* profVals = (profileValues *) malloc(sizeof(profileValues)); - char *param = (char*)((Param *) Vector_At(paramList, i))->alias ; + if(parmTemp->skipFreq > 0 && (execcount % parmTemp->skipFreq + 1) != 0) + { + T2Info("Skipping parameter : %s as per skipFreq : %d\n", parmTemp->name, parmTemp->skipFreq); + profVals->paramValues = NULL; + Vector_PushBack(profileValueList, profVals); + continue; + } + + char *param = (char*)parmTemp->alias ; if(param != NULL) { paramNames[0] = strdup(param); diff --git a/source/ccspinterface/rbusInterface.h b/source/ccspinterface/rbusInterface.h index 4984457e..2a54687f 100644 --- a/source/ccspinterface/rbusInterface.h +++ b/source/ccspinterface/rbusInterface.h @@ -35,7 +35,7 @@ typedef void (*rbusMethodCallBackPtr) (rbusHandle_t handle, char const* methodNa T2ERROR getRbusParameterVal(const char* paramName, char **paramValue); -Vector* getRbusProfileParamValues(Vector *paramList); +Vector* getRbusProfileParamValues(Vector *paramList, int count); T2ERROR registerRbusT2EventListener(TelemetryEventCallback eventCB); diff --git a/source/dcautil/Makefile.am b/source/dcautil/Makefile.am index a230ab6e..333bcf2b 100644 --- a/source/dcautil/Makefile.am +++ b/source/dcautil/Makefile.am @@ -18,7 +18,7 @@ ########################################################################## lib_LTLIBRARIES = libdcautil.la -libdcautil_la_SOURCES = dcautil.c dca.c dcalist.c legacyutils.c dcaproc.c dcajson.c rdk_linkedlist.c +libdcautil_la_SOURCES = dcautil.c dca.c legacyutils.c dcaproc.c libdcautil_la_CFLAGS = libdcautil_la_LDFLAGS = -shared -fPIC -lcjson libdcautil_la_CFLAGS += $(DBUS_CFLAGS) -DUSE_TR181_CCSP_MESSAGEBUS diff --git a/source/dcautil/dca.c b/source/dcautil/dca.c index 8a92ceae..f217a4a0 100644 --- a/source/dcautil/dca.c +++ b/source/dcautil/dca.c @@ -18,18 +18,23 @@ */ - #include #include #include #include #include #include +#include +#include +#include +#include +#include +#include #include -#include "dcalist.h" #include "dcautil.h" +#include "dca.h" #include "legacyutils.h" #include "rdk_linkedlist.h" @@ -40,28 +45,81 @@ #include "t2log_wrapper.h" #include "t2common.h" #include "busInterface.h" + + static bool check_rotated_logs = false; // using this variable to indicate whether it needs to check the rotated logs or not . Initialising it with false. static bool firstreport_after_bootup = false; // the rotated logs check should run only for the first time. + // Using same error code used by safeclib for buffer overflow for easy metrics collection -// safeclib is not compleately introduced in T2 but to be in sync with legacy +// safeclib is not completely introduced in T2 but to be in sync with legacy #define INVALID_COUNT -406 +#define BUFFER_SIZE 4096 // TODO fine tune this value based on the size of the data +#define LARGE_FILE_THRESHOLD 1000000 // 1MB + /** * @addtogroup DCA_TYPES * @{ */ +// Define a struct to hold the file descriptor and size +typedef struct +{ + int fd; + off_t cf_file_size; + off_t rf_file_size; + char* cfaddr; + char* rfaddr; + void* baseAddr; + void* rotatedAddr; +} FileDescriptor; + +/** + * Portable implementation of strnstr (BSD function). + * Searches for the first occurrence of the substring 'needle' in the + * first 'len' bytes of 'haystack'. + * Returns pointer to the beginning of the match, or NULL if not found. + */ +static const char *strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t needle_len; + + if (*needle == '\0') + { + return haystack; + } + + needle_len = strlen(needle); + if (needle_len == 0) + { + return haystack; + } + + for (size_t i = 0; i + needle_len <= len; i++) + { + if (memcmp(haystack + i, needle, needle_len) == 0) + { + return haystack + i; + } + if (haystack[i] == '\0') + { + break; + } + } + return NULL; +} cJSON *SEARCH_RESULT_JSON = NULL, *ROOT_JSON = NULL; -static char *logPath = NULL; -static char *persistentPath = NULL; +static char *LOGPATH = NULL; +static char *PERSISTENTPATH = NULL; +static long PAGESIZE; static pthread_mutex_t dcaMutex = PTHREAD_MUTEX_INITIALIZER; -#if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) -static pthread_mutex_t topOutputMutex = PTHREAD_MUTEX_INITIALIZER; -#endif + + + /* @} */ // End of group DCA_TYPES /** * @addtogroup DCA_APIS @@ -71,733 +129,763 @@ static pthread_mutex_t topOutputMutex = PTHREAD_MUTEX_INITIALIZER; /** @brief This API processes the top command log file patterns to retrieve load average and process usage. * * @param[in] logfile top_log file - * @param[in] pchead Node head - * @param[in] pcIndex Node count * * @return Returns the status of the operation. * @retval Returns zero on success, appropriate errorcode otherwise. */ -int processTopPattern(rdkList_t *pchead, Vector* grepResultList) +int processTopPattern(char* profileName, Vector* topMarkerList, Vector* out_grepResultList, int profileExecCounter) { T2Debug("%s ++in\n", __FUNCTION__); - if(pchead == NULL || grepResultList == NULL) + if(profileName == NULL || topMarkerList == NULL || out_grepResultList == NULL) { T2Error("Invalid arguments for %s\n", __FUNCTION__); return -1; } - rdkList_t *tlist = pchead; - pcdata_t *tmp = NULL; - while(NULL != tlist) + size_t var = 0; + size_t vCount = Vector_Size(topMarkerList); + T2Debug("topMarkerList for profile %s is of count = %lu \n", profileName, (unsigned long )vCount); + // Get logfile -> seek value map associated with the profile + + // We are getting the exec count directly from the profileExecCounter parameter + //int profileExecCounter = gsProfile->execCounter; + char* filename = NULL; + + for (var = 0; var < vCount; ++var) { - tmp = tlist->m_pUserData; - if(NULL != tmp) + GrepMarker* grepMarkerObj = (GrepMarker*) Vector_At(topMarkerList, var); + if (!grepMarkerObj || !grepMarkerObj->logFile || !grepMarkerObj->searchString || !grepMarkerObj->markerName) { - if((NULL != tmp->header) && (NULL != strstr(tmp->header, "Load_Average"))) - { - if(0 == getLoadAvg(grepResultList, tmp->trimparam, tmp->regexparam)) - { - T2Debug("getLoadAvg() Failed with error"); - } - } - else - { - if(NULL != tmp->pattern) - { - // save top output + continue; + } + int tmp_skip_interval, is_skip_param; + tmp_skip_interval = grepMarkerObj->skipFreq; + is_skip_param = (profileExecCounter % (tmp_skip_interval + 1) == 0) ? 0 : 1; + if (is_skip_param != 0) + { + + T2Debug("Skipping marker %s for profile %s as per skip frequency %d \n", grepMarkerObj->markerName, profileName, tmp_skip_interval); + continue; + } + else + { + #if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) - pthread_mutex_lock(&topOutputMutex); - saveTopOutput(); - getProcUsage(tmp->pattern, grepResultList, tmp->trimparam, tmp->regexparam); - pthread_mutex_unlock(&topOutputMutex); -#else - getProcUsage(tmp->pattern, grepResultList, tmp->trimparam, tmp->regexparam); + filename = saveTopOutput(profileName); #endif + break; + } + T2Debug("topMarkerList[%lu] markerName = %s, logFile = %s, searchString = %s \n", (unsigned long)var, grepMarkerObj->markerName, grepMarkerObj->logFile, grepMarkerObj->searchString); + } - } + for (; var < vCount; ++var) // Loop of marker list starts here + { + GrepMarker* grepMarkerObj = (GrepMarker*) Vector_At(topMarkerList, var); + if (!grepMarkerObj || !grepMarkerObj->logFile || !grepMarkerObj->searchString || !grepMarkerObj->markerName) + { + continue; + } + if (strcmp(grepMarkerObj->searchString, "") == 0 || strcmp(grepMarkerObj->logFile, "") == 0) + { + continue; + } + + + // If the skip frequency is set, skip the marker processing for this interval + int tmp_skip_interval, is_skip_param; + tmp_skip_interval = grepMarkerObj->skipFreq; + is_skip_param = (profileExecCounter % (tmp_skip_interval + 1) == 0) ? 0 : 1; + if (is_skip_param != 0) + { + + T2Debug("Skipping marker %s for profile %s as per skip frequency %d \n", grepMarkerObj->markerName, profileName, tmp_skip_interval); + continue; + } + + + if (strcmp(grepMarkerObj->markerName, "Load_Average") == 0) // This block is for device level load average + { + if (0 == getLoadAvg(out_grepResultList, grepMarkerObj->trimParam, grepMarkerObj->regexParam)) + { + T2Debug("getLoadAvg() Failed with error"); } } - tlist = rdk_list_find_next_node(tlist); + else + { + getProcUsage(grepMarkerObj->searchString, out_grepResultList, grepMarkerObj->trimParam, grepMarkerObj->regexParam, filename); + } + } +#if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) + removeTopOutput(filename); +#endif T2Debug("%s --out\n", __FUNCTION__); return 0; } -/** @brief This API appends tr181 object value to telemetry node. +/** + * @brief Function to read the rotated Log file. + * + * @param[in] name Log file name. + * @param[in] seek_value Position to seek. * - * @param[in] dst Object node - * @param[in] src Data value + * @return Returns the status of the operation. + * @retval Returns -1 on failure, appropriate errorcode otherwise. */ -static void appendData(pcdata_t* dst, const char* src) +static int getLogSeekValue(hash_map_t *logSeekMap, const char *name, long *seek_value) { - T2Debug("%s ++in\n", __FUNCTION__); - int dst_len, src_len = 0; - - if(NULL == dst || NULL == src) - { - return; - } - - //Copy data - if(NULL == dst->data) + T2Debug("%s ++in for file %s \n", __FUNCTION__, name); + int rc = 0; + if (logSeekMap) { - src_len = strlen(src) + 1; - dst->data = (char*) malloc(src_len); - if(NULL != dst->data) + long *data = (long*) hash_map_get(logSeekMap, name) ; + if (data) { - snprintf(dst->data, src_len, "%s", src); + *seek_value = *data ; } else { - T2Debug("Failed to allocate memory for telemetry node data\n"); + T2Debug("data is null .. Setting seek value to 0 from getLogSeekValue \n"); + *seek_value = 0 ; } } - else //Append data + else { - dst_len = strlen(dst->data) + 1; - src_len = strlen(src) + 1; - dst->data = (char*) realloc(dst->data, dst_len + src_len); - if(NULL != dst->data) - { - dst->data[dst_len - 1] = ','; - dst->data[dst_len] = '\0'; - snprintf((dst->data) + dst_len, src_len, "%s", src); - } - else - { - T2Debug("Failed to re-allocate memory for telemetry node data\n"); - } + T2Debug("logSeekMap is null .. Setting seek value to 0 \n"); + *seek_value = 0 ; } - T2Debug("%s --out\n", __FUNCTION__); + T2Debug("%s --out \n", __FUNCTION__); + return rc; } - /** - * @brief This API process tr181 objects through ccsp message bus + * @brief Function to write the rotated Log file. * - * @param[in] logfile DCA pattern file - * @param[in] pchead Node head - * @param[in] pcIndex Node count + * @param[in] name Log file name. * - * @return Returns status of the operation. - * @retval Returns 1 on failure, 0 on success - * Retaining this for skip frequency param confined to XCONF profile + * @return Returns the status of the operation. + * @retval Returns -1 on failure, appropriate errorcode otherwise. */ -static T2ERROR processTr181Objects(rdkList_t *pchead) +static T2ERROR updateLogSeek(hash_map_t *logSeekMap, const char* logFileName, const long logfileSize) { T2Debug("%s ++in\n", __FUNCTION__); - T2ERROR ret_val = T2ERROR_FAILURE; - int length, obj_count, i = 0; - rdkList_t *tlist = NULL; - pcdata_t *tmp = NULL; - char tr181objBuff[TR181BUF_LENGTH + 15] = { '\0' }; - char *tck, *first_tck = NULL; - if(pchead == NULL) - { - T2Error("pchead is NULL for %s\n", __FUNCTION__); + if(logSeekMap == NULL || logFileName == NULL) + { + T2Error("Invalid or NULL arguments\n"); return T2ERROR_FAILURE; } - //Loop through the given list and fill the data field of each node - for( tlist = pchead; tlist != NULL; tlist = rdk_list_find_next_node(tlist) ) + T2Debug("Adding seekvalue of %ld for %s to logSeekMap \n", logfileSize, logFileName); + long* val = (long *) malloc(sizeof(long)); + if(NULL != val) { - char* tr181dataBuff = NULL; - tmp = tlist->m_pUserData; - if(NULL != tmp) - { - if(NULL != tmp->header && NULL != tmp->pattern && strlen(tmp->pattern) < TR181BUF_LENGTH && NULL == tmp->data) - { - - //Check whether given object has multi-instance token, if no token found it will be treated as a single instance object - //Or if more than one token found, skip the object as it is not valid/supported - //Check for first multi-instance token - tck = strstr(tmp->pattern, OBJ_DELIMITER); - if(NULL == tck) //Single instance check - { - ret_val = getParameterValue(tmp->pattern, &tr181dataBuff); - if(T2ERROR_SUCCESS == ret_val) - { - appendData(tmp, tr181dataBuff); - free(tr181dataBuff); - tr181dataBuff = NULL; - } - else - { - T2Debug("Telemetry data source not found. Type = . Content string = %s\n", tmp->pattern); - } - } - else //Multi-instance check - { - first_tck = tck; - //Check for a next multi-instance token - tck = strstr(tck + DELIMITER_SIZE, OBJ_DELIMITER); - if(NULL == tck) - { - //Get NumberOfEntries of a multi-instance object - length = first_tck - tmp->pattern; - snprintf(tr181objBuff, sizeof(tr181objBuff), "%sNumberOfEntries", tmp->pattern); - ret_val = getParameterValue(tr181objBuff, &tr181dataBuff); - if(T2ERROR_SUCCESS == ret_val) - { - obj_count = atoi(tr181dataBuff); - free(tr181dataBuff); - tr181dataBuff = NULL ; - //Collect all all instance value of a object - if(obj_count > 0) - { - for( i = 1; i <= obj_count; i++ ) - { - //Replace multi-instance token with an object instance number - snprintf(tr181objBuff, sizeof(tr181objBuff), "%s%d%s", tmp->pattern, i, (tmp->pattern + length + DELIMITER_SIZE)); - ret_val = getParameterValue(tr181objBuff, &tr181dataBuff); - if(T2ERROR_SUCCESS == ret_val) - { - appendData(tmp, tr181dataBuff); - } - else - { - T2Debug("Telemetry data source not found. Type = . Content string = %s\n", tr181objBuff); - } - free(tr181dataBuff); - tr181dataBuff = NULL; - } //End of for loop - } - } - else - { - T2Debug("Failed to get NumberOfEntries. Type = . Content string = %s\n", tr181objBuff); - } - } - else - { - T2Debug("Skipping Telemetry object due to invalid format. Type = . Content string = %s\n", tmp->pattern); - } - } //End of Mult-instance check - } - } - - } //End of node loop through for loop + memset(val, 0, sizeof(long)); + *val = logfileSize; + hash_map_put(logSeekMap, strdup(logFileName), (void *)val, free); + } + else + { + T2Warning("Unable to allocate memory for seek value pointer \n"); + } T2Debug("%s --out\n", __FUNCTION__); - return ret_val; + return T2ERROR_SUCCESS; } + + /** - * @brief This function adds the value to the telemetry output json object. - * - * @param[in] pchead Header field in the telemetry profile - * - * @return Returns status of operation. + * @brief This API updates the filename if it is different from the current one. + * @param currentFile The current filename. + * @param newFile The new filename to update to. + * @return The updated filename. */ -static void addToJson(rdkList_t *pchead) +static char* updateFilename(char* previousFile, const char* newFile) { - if(pchead == NULL) + if (!newFile) { - T2Error("pchead is NULL for %s\n", __FUNCTION__); - return; + return NULL; } - T2Debug("%s ++in\n", __FUNCTION__); - rdkList_t *tlist = pchead; - pcdata_t *tmp = NULL; - while(NULL != tlist) + + if (!previousFile || strcmp(previousFile, newFile) != 0) { - tmp = tlist->m_pUserData; - if(NULL != tmp) + if(previousFile) { - if(tmp->pattern) - { - if(tmp->d_type == OCCURENCE) - { - if(tmp->count != 0) - { - char tmp_str[5] = { 0 }; - sprintf(tmp_str, "%d", tmp->count); - addToSearchResult(tmp->header, tmp_str); - } - } - else if(tmp->d_type == STR) - { - if(NULL != tmp->data && (strcmp(tmp->data, "0") != 0)) - { - addToSearchResult(tmp->header, tmp->data); - } - } - } + free(previousFile); + } + previousFile = strdup(newFile); + if (!previousFile) + { + T2Error("Insufficient memory to allocate string %s\n", newFile); } - tlist = rdk_list_find_next_node(tlist); } - T2Debug("%s --out\n", __FUNCTION__); + return previousFile; } +static GrepResult* createGrepResultObj(const char* markerName, const char* markerValue, bool trimParameter, char* regexParameter) +{ + GrepResult* grepResult = (GrepResult*) malloc(sizeof(GrepResult)); + if (grepResult == NULL) + { + T2Error("Failed to allocate memory for GrepResult\n"); + return NULL; + } + grepResult->markerName = strdup(markerName); + grepResult->markerValue = strdup(markerValue); + grepResult->trimParameter = trimParameter; + if (regexParameter != NULL) + { + grepResult->regexParameter = strdup(regexParameter); + } + else + { + grepResult->regexParameter = NULL; + } + return grepResult; +} /** - * @brief This function adds the value to the telemetry output vector object. + * @brief This function formats the count value to a string. + * Formatting is to handle the case where the count of error occurence in an interval exceeds 9999. * - * @param[in] pchead Header field in the telemetry profile - * - * @return Returns status of operation. + * @param[out] buffer The buffer to store the formatted string. + * @param[in] size The size of the buffer. + * @param[in] count The count value to format. */ -static int addToVector(rdkList_t *pchead, Vector* grepResultList) +static inline void formatCount(char* buffer, size_t size, int count) { + if (count > 9999) + { + count = INVALID_COUNT; + } + snprintf(buffer, size, "%d", count); +} - T2Debug("%s ++in\n", __FUNCTION__); - if(pchead == NULL || grepResultList == NULL) +static int getCountPatternMatch(FileDescriptor* fileDescriptor, const char* pattern) +{ + if (!fileDescriptor || !fileDescriptor->cfaddr || !pattern || !*pattern || fileDescriptor->cf_file_size <= 0) { - T2Error("Inavlid arguments for %s\n", __FUNCTION__); - return -1; + T2Error("Invalid file descriptor arguments pattern match\n"); + return -1; // Invalid arguments } - rdkList_t *tlist = pchead; - pcdata_t *tmp = NULL; - while(NULL != tlist) + const char* buffer; + size_t buflen = 0; + size_t patlen = strlen(pattern); + int count = 0; + + for(int i = 0; i < 2; i++) { - tmp = tlist->m_pUserData; - if(NULL != tmp) + if (i == 0) + { + buffer = fileDescriptor->cfaddr; + buflen = (size_t)fileDescriptor->cf_file_size; + } + else + { + buffer = fileDescriptor->rfaddr; + buflen = (size_t)fileDescriptor->rf_file_size; + } + if(buffer == NULL) + { + T2Debug("Invalid file descriptor arguments pattern match\n"); + continue; + } + if (patlen == 0 || buflen < patlen) { + T2Info("File size is less than pattern length so ignoring the file\n"); + continue; + } + + const char *cur = buffer; + size_t bytes_left = buflen; - if(tmp->pattern && grepResultList != NULL ) + while (bytes_left >= patlen) + { + const char *found = strnstr(cur, pattern, bytes_left); + if (!found) { - if(tmp->d_type == OCCURENCE) - { - if(tmp->count != 0) - { - char tmp_str[5] = { 0 }; - if(tmp->count > 9999) - { - T2Debug("Count value is %d higher than limit of 9999 changing the value to %d to track buffer overflow", tmp->count, INVALID_COUNT); - tmp->count = INVALID_COUNT; - } - snprintf(tmp_str, sizeof(tmp_str), "%d", tmp->count); - GrepResult* grepResult = (GrepResult*) malloc(sizeof(GrepResult)); - grepResult->markerName = strdup(tmp->header); - grepResult->markerValue = strdup(tmp_str); - grepResult->trimParameter = tmp->trimparam; - grepResult->regexParameter = tmp->regexparam; - if(tmp->header) - { - free(tmp->header); - tmp->header = NULL; - } - T2Debug("Adding OCCURENCE to result list %s : %s \n", grepResult->markerName, grepResult->markerValue); - Vector_PushBack(grepResultList, grepResult); - } - } - else if(tmp->d_type == STR) - { - if(NULL != tmp->data && (strcmp(tmp->data, "0") != 0)) - { - GrepResult* grepResult = (GrepResult*) malloc(sizeof(GrepResult)); - grepResult->markerName = strdup(tmp->header); - grepResult->markerValue = strdup(tmp->data); - grepResult->trimParameter = tmp->trimparam; - grepResult->regexParameter = tmp->regexparam; - if(tmp->header) - { - free(tmp->header); - tmp->header = NULL; - } - if(tmp->data) - { - free(tmp->data); - tmp->data = NULL; - } - T2Debug("Adding STR to result list %s : %s \n", grepResult->markerName, grepResult->markerValue); - Vector_PushBack(grepResultList, grepResult); - - } - } + break; } - else + count++; + size_t advance = (size_t)(found - cur) + patlen; + cur = found + patlen; + if (bytes_left < advance) { - T2Debug("%s : grepResultList is NULL \n", __FUNCTION__); + break; } + bytes_left -= advance; } - tlist = rdk_list_find_next_node(tlist); + } - T2Debug("%s --out\n", __FUNCTION__); - return 0; + return count; } -/** - * @brief Function to process pattern if it has split text in the header - * - * @param[in] line Log file matched line - * @param[in] pcnode Pattern to be verified. - * - * @return Returns status of operation. - * @retval Return 0 on success, -1 on failure - */ -static int getSplitParameterValue(char *line, pcdata_t *pcnode) +static char* getAbsolutePatternMatch(FileDescriptor* fileDescriptor, const char* pattern) { - - char *strFound = NULL; - if(line == NULL || pcnode == NULL) + if (!fileDescriptor || !fileDescriptor->cfaddr || fileDescriptor->cf_file_size <= 0 || !pattern || !*pattern) { - T2Error("Invalid arguments for %s\n", __FUNCTION__); - return -1; + T2Error("Invalid file descriptor arguments absolute\n"); + return NULL; } - strFound = strstr(line, pcnode->pattern); - if(strFound != NULL) + const char* buffer; + size_t buflen = 0; + size_t patlen = strlen(pattern); + const char *last_found = NULL; + + for ( int i = 0; i < 2; i++ ) { - int tlen = 0, plen = 0, vlen = 0; - tlen = (int) strlen(line); - plen = (int) strlen(pcnode->pattern); - strFound = strFound + plen; - if(tlen > plen) + if (i == 0) + { + buffer = fileDescriptor->cfaddr; + buflen = (size_t)fileDescriptor->cf_file_size; + } + else + { + buffer = fileDescriptor->rfaddr; + buflen = (size_t)fileDescriptor->rf_file_size; + } + + if(buffer == NULL) + { + T2Debug("Invalid file descriptor arguments absolute match\n"); + continue; + } + const char *cur = buffer; + size_t bytes_left = buflen; + + while (bytes_left >= patlen) { - vlen = strlen(strFound); - // If value is only single char make sure its not an empty space . - // Ideally component should not print logs with empty values but we have to consider logs from OSS components - if((1 == vlen) && isspace(strFound[plen])) + const char *found = strnstr(cur, pattern, bytes_left); + if (!found) { - return 0; + break; } - - if(vlen > 0) + last_found = found; + size_t advance = (size_t)(found - cur) + patlen; + cur = found + patlen; + if (bytes_left < advance) { - if(NULL == pcnode->data) - { - pcnode->data = (char *) malloc(MAXLINE); - } - - if(NULL == pcnode->data) - { - return (-1); - } - - strncpy(pcnode->data, strFound, MAXLINE); - pcnode->data[tlen - plen] = '\0'; //For Boundary Safety + break; } + bytes_left -= advance; + } + + if (!last_found) + { + continue; + } + if(last_found && i == 0) + { + break; } } - return 0; + if(!last_found) + { + return NULL; + } + // Move pointer just after the pattern + const char *start = last_found + patlen; + size_t chars_left = buflen - (start - buffer); + + // Find next newline or end of buffer + const char *end = memchr(start, '\n', chars_left); + size_t length = end ? (size_t)(end - start) : chars_left; + + char *result = (char*)malloc(length + 1); + if (!result) + { + return NULL; + } + memcpy(result, start, length); + result[length] = '\0'; + T2Debug("Found pattern '%s' in file, result: '%s'\n", pattern, result); + return result; } -/** - * @brief To get RDK error code. - * - * @param[in] str Source string. - * @param[out] ec Error code. - * - * @return Returns status of operation. - * @retval Return 0 upon success. - */ -int getErrorCode(char *str, char *ec) +static int processPatternWithOptimizedFunction(const GrepMarker* marker, Vector* out_grepResultList, FileDescriptor* filedescriptor) { + // Sanitize the input - T2Debug("%s ++in\n", __FUNCTION__); - int i = 0, j = 0, len = 0; -// Solution: Check if str is NULL before dereferencing it. - if(str == NULL) + const char* memmmapped_data_cf = filedescriptor->cfaddr; + if (!marker || !out_grepResultList || !memmmapped_data_cf) { - T2Error("Str is NULL for %s\n", __FUNCTION__); + T2Error("Invalid arguments for %s\n", __FUNCTION__); return -1; } - len = strlen(str); - char tmpEC[LEN] = { 0 }; - while(str[i] != '\0') + // Extract the pattern and other parameters from the marker + const char* pattern = marker->searchString; + bool trimParameter = marker->trimParam; + char* regexParameter = marker->regexParam; + char* header = marker->markerName; + int count = 0; + char* last_found = NULL; + MarkerType mType = marker->mType; + + if (mType == MTYPE_COUNTER) { - if(len >= 4 && str[i] == 'R' && str[i + 1] == 'D' && str[i + 2] == 'K' && str[i + 3] == '-') + // Count the number of occurrences of the pattern in the memory-mapped data + count = getCountPatternMatch(filedescriptor, pattern); + if (count > 0) { - i += 4; - j = 0; - if(str[i] == '0' || str[i] == '1') + // If matches are found, process them accordingly + char tmp_str[5] = { 0 }; + formatCount(tmp_str, sizeof(tmp_str), count); + GrepResult* result = createGrepResultObj(header, tmp_str, trimParameter, regexParameter); + if (result == NULL) { - tmpEC[j] = str[i]; - i++; - j++; - if(str[i] == '0' || str[i] == '3') - { - tmpEC[j] = str[i]; - i++; - j++; - if(0 != isdigit(str[i])) - { - while(i <= len && 0 != isdigit(str[i]) && j < RDK_EC_MAXLEN) - { - tmpEC[j] = str[i]; - i++; - j++; - ec[j] = '\0'; - strncpy(ec, tmpEC, LEN); - } - break; - } - } + T2Error("Failed to create GrepResult\n"); + return -1; } + Vector_PushBack(out_grepResultList, result); + } + } + else + { + // Get the last occurrence of the pattern in the memory-mapped data + last_found = getAbsolutePatternMatch(filedescriptor, pattern); + // TODO : If trimParameter is true, trim the pattern before adding to the result list + if (last_found) + { + // If a match is found, process it accordingly + GrepResult* result = createGrepResultObj(header, last_found, trimParameter, regexParameter); + if(last_found) + { + free(last_found); + last_found = NULL; + } + if (result == NULL) + { + T2Error("Failed to create GrepResult\n"); + return -1; + } + Vector_PushBack(out_grepResultList, result); } - i++; } - T2Debug("%s --out\n", __FUNCTION__); return 0; } -/** - * @brief Function to handle error codes received from the log file. - * - * @param[in] rdkec_head Node head. - * @param[in] line Logfile matched line. - * - * @return Returns status of operation. - * @retval Return 0 upon success, -1 on failure. - */ -static int handleRDKErrCodes(rdkList_t **rdkec_head, char *line) + +static int getLogFileDescriptor(GrepSeekProfile* gsProfile, const char* logPath, const char* logFile, int old_fd, off_t* out_seek_value) { - T2Debug("%s ++in\n", __FUNCTION__); - char err_code[20] = { 0 }, rdkec[30] = { 0 }; - pcdata_t *tnode = NULL; + long seek_value_from_map = 0; + getLogSeekValue(gsProfile->logFileSeekMap, logFile, &seek_value_from_map); + if (old_fd != -1) + { + close(old_fd); + } - getErrorCode(line, err_code); - if(strcmp(err_code, "") != 0) + char logFilePath[PATH_MAX]; + if(logFile[0] == '/') { - snprintf(rdkec, sizeof(rdkec), "RDK-%s", err_code); - tnode = searchPCNode(*rdkec_head, rdkec); - if(NULL != tnode) - { - tnode->count++; - } - else - { - /* Args: rdkList_t **pch, char *pattern, char *header, DType_t dtype, int count, char *data, bool trim, char *regex */ - insertPCNode(rdkec_head, rdkec, rdkec, OCCURENCE, 1, NULL, false, NULL); - } - T2Debug("%s --out\n", __FUNCTION__); - return 0; + // If the logFile is an absolute path, use it directly + T2Info("Log file is an absolute path not prefix in the directory: %s\n", logFile); + snprintf(logFilePath, sizeof(logFilePath), "%s", logFile); + } + else + { + snprintf(logFilePath, sizeof(logFilePath), "%s/%s", logPath, logFile); + } + + T2Debug("Opening log file %s\n", logFilePath); + int fd = open(logFilePath, O_RDONLY); + if (fd == -1) + { + T2Error("Failed to open log file %s\n", logFilePath); + return -1; + } + + // Calculate the file size + struct stat sb; + if (fstat(fd, &sb) == -1) + { + T2Error("Error getting file size for %s\n", logFile); + close(fd); + return -1; + } + + // Check if the file size is 0 + if (sb.st_size == 0) + { + T2Error("The size of the logfile is 0 for %s\n", logFile); + close(fd); + return -1; // Consistent error return value + } + + // Check if the file size matches the seek value from the map + if (sb.st_size == seek_value_from_map) + { + T2Error("The logfile size matches the seek value (%ld) for %s\n", seek_value_from_map, logFile); + close(fd); + return -1; // Consistent error return value } - T2Debug("%s --out Error .... \n", __FUNCTION__); - return -1; + updateLogSeek(gsProfile->logFileSeekMap, logFile, sb.st_size); + *out_seek_value = seek_value_from_map; + return fd; } -/** - * @brief Function to process pattern count (loggrep) - * - * @param[in] logfile Current log file - * @param[in] pchead Node head - * @param[in] pcIndex Node count - * @param[in] rdkec_head RDK errorcode head - * - * @return Returns status of operation. - * @retval Return 0 upon success, -1 on failure. - */ -static int processCountPattern(hash_map_t *logSeekMap, char *logfile, rdkList_t *pchead, rdkList_t **rdkec_head, int *firstSeekFromEOF, bool check_rotated_logs) +static int getRotatedLogFileDescriptor(const char* logPath, const char* logFile) { - T2Debug("%s ++in\n", __FUNCTION__); - char temp[MAXLINE]; - T2Debug("Read from log file %s \n", logfile); - while(getLogLine(logSeekMap, temp, MAXLINE, logfile, firstSeekFromEOF, check_rotated_logs) != NULL) + char logFilePath[PATH_MAX]; + snprintf(logFilePath, sizeof(logFilePath), "%s/%s", logPath, logFile); + //get the rotated filename + char *fileExtn = ".1"; + char rotatedlogFilePath[PATH_MAX]; + size_t name_len = strlen(logFilePath); + if(logFile[0] == '/') + { + // If the logFile is an absolute path, use it directly + T2Debug("RotatedLog file is an absolute path not prefix in the directory: %s\n", logFile); + snprintf(rotatedlogFilePath, sizeof(logFilePath), "%s", logFile); + } + else + { + // If the logFile is not an absolute path, prefix it with the logPath + T2Debug("RotatedLog file is not an absolute path, prefixing with directory: %s\n", logPath); + snprintf(rotatedlogFilePath, sizeof(rotatedlogFilePath), "%s/%s", logPath, logFile); + } + if(name_len > 2 && logFilePath[name_len - 2] == '.' && logFilePath[name_len - 1] == '0') { + rotatedlogFilePath[name_len - 1] = '1'; + T2Debug("Log file name seems to be having .0 extension hence Rotated log file name is %s\n", rotatedlogFilePath); + } + else + { + strncat(rotatedlogFilePath, fileExtn, sizeof(rotatedlogFilePath) - strlen(rotatedlogFilePath) - 1); + T2Debug("Rotated log file name is %s\n", rotatedlogFilePath); + } - int len = strlen(temp); - if(len > 0 && temp[len - 1] == '\n') + int rd = open(rotatedlogFilePath, O_RDONLY); + if (rd == -1) + { + T2Error("Failed to open log file %s\n", rotatedlogFilePath); + return -1; + } + + // Calculate the file size + struct stat rb; + if (fstat(rd, &rb) == -1) + { + T2Error("Error getting file size for %s\n", rotatedlogFilePath); + close(rd); + return -1; + } + + // Check if the file size is 0 + if (rb.st_size == 0) + { + T2Error("The size of the logfile is 0 for %s\n", rotatedlogFilePath); + close(rd); + return -1; // Consistent error return value + } + return rd; +} + +// Caller should free the FileDescriptor struct after use +static void freeFileDescriptor(FileDescriptor* fileDescriptor) +{ + if (fileDescriptor) + { + if(fileDescriptor->baseAddr) { - temp[--len] = '\0'; + munmap(fileDescriptor->baseAddr, fileDescriptor->cf_file_size); } - - pcdata_t *pc_node = searchPCNode(pchead, temp); - if(NULL != pc_node) + if(fileDescriptor->rotatedAddr) { - if(pc_node->d_type == OCCURENCE) - { - pc_node->count++; - } - else - { - if(NULL != pc_node->header) - { - getSplitParameterValue(temp, pc_node); - } - } + munmap(fileDescriptor->rotatedAddr, fileDescriptor->rf_file_size); } - else + fileDescriptor->cfaddr = NULL; + fileDescriptor->rfaddr = NULL; + if(fileDescriptor->fd != -1) { - // This is a RDK-V specific calls for reporting RDK error codes . Retaining for video porting - if(NULL != strstr(temp, "RDK-")) - { - handleRDKErrCodes(rdkec_head, temp); - } + close(fileDescriptor->fd); + fileDescriptor->fd = -1; } + free(fileDescriptor); } - T2Debug("%s --out\n", __FUNCTION__); - return 0; } -/** - * @brief Generic pattern function based on pattern to call top/count or using ccsp message bus. - * - * @param[in] prev_file The previous log file. - * @param[in] logfile The current log file. - * @param[in] rdkec_head RDK errorcode head - * @param[in] pchead Node head - * @param[in] pcIndex Node count - * - * @return Returns status on operation. - * @retval Returns 0 upon success. - */ -static int processPattern(char **prev_file, char *logfile, rdkList_t **rdkec_head, rdkList_t *pchead, Vector *grepResultList, hash_map_t* logSeekMap, int *firstSeekFromEOF, bool check_rotated_logs) +static FileDescriptor* getFileDeltaInMemMapAndSearch(const int fd, const off_t seek_value, const char* logPath, const char* logFile, bool check_rotated ) { + char *addrcf = NULL; + char *addrrf = NULL; + if (fd == -1) + { + T2Error("Error opening file\n"); + return NULL; + } + // Read the file contents using mmap + struct stat sb; + struct stat rb; + if(fstat(fd, &sb) == -1) + { + T2Error("Error getting file size\n"); + return NULL; + } - T2Debug("%s ++in\n", __FUNCTION__); - if(NULL != logfile) + if(sb.st_size == 0) + { + T2Error("The Size of the logfile is 0\n"); + return NULL; + } + + FileDescriptor* fileDescriptor = NULL; + off_t offset_in_page_size_multiple ; + unsigned int bytes_ignored = 0, bytes_ignored_main = 0, bytes_ignored_rotated = 0; + // Find the nearest multiple of page size + if (seek_value > 0) + { + offset_in_page_size_multiple = (seek_value / PAGESIZE) * PAGESIZE; + bytes_ignored = seek_value - offset_in_page_size_multiple; + } + else { + offset_in_page_size_multiple = 0; + bytes_ignored = 0; + } - if((NULL == *prev_file) || (strcmp(*prev_file, logfile) != 0)) + if(seek_value > sb.st_size || check_rotated == true) + { + int rd = getRotatedLogFileDescriptor(logPath, logFile); + if (rd == -1) + { + T2Error("Error opening rotated file. Start search in current file\n"); + T2Debug("File size rounded to nearest page size used for offset read: %jd bytes\n", (intmax_t)offset_in_page_size_multiple); + addrcf = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, offset_in_page_size_multiple); + bytes_ignored_main = bytes_ignored; + } + else { - if(NULL == *prev_file) + int fs = 0; + fs = fstat(rd, &rb); + if(fs == -1) { - *prev_file = strdup(logfile); - if(*prev_file == NULL) - { - T2Error("Insufficient memory available to allocate duplicate string %s\n", logfile); - } + T2Error("Error getting file size\n"); + close(rd); } else { - updateLogSeek(logSeekMap, *prev_file); - free(*prev_file); - *prev_file = strdup(logfile); - if(*prev_file == NULL) + if(rb.st_size == 0) { - T2Error("Insufficient memory available to allocate duplicate string %s\n", logfile); + T2Error("The Size of the logfile is 0\n"); + close(rd); } } - } - // Process - if(NULL != pchead) - { - if(0 == strcmp(logfile, "top_log.txt")) - { - if(grepResultList != NULL) - { - processTopPattern(pchead, grepResultList); - } - } - else if(0 == strcmp(logfile, "")) + + if(rb.st_size > 0) { - processTr181Objects( pchead); - if (grepResultList != NULL) - { - addToVector(pchead, grepResultList); - } - else + addrcf = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + addrrf = mmap(NULL, rb.st_size, PROT_READ, MAP_PRIVATE, rd, offset_in_page_size_multiple); + bytes_ignored_rotated = bytes_ignored; + if(rd != -1) { - addToJson(pchead); + close(rd); + rd = -1; } } - else + + + if(rb.st_size == 0 && fs == -1) { - processCountPattern(logSeekMap, logfile, pchead, rdkec_head, firstSeekFromEOF, check_rotated_logs); - if (grepResultList != NULL) - { - addToVector(pchead, grepResultList); - } - else - { - addToJson(pchead); - } + T2Debug("No contents in rotated log file. File size rounded to nearest page size used for offset read: %jd bytes\n", (intmax_t)offset_in_page_size_multiple); + addrcf = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, offset_in_page_size_multiple); + bytes_ignored_main = bytes_ignored; } } - clearPCNodes(&pchead); } - T2Debug("%s --out\n", __FUNCTION__); - return 0; -} - -/** - * @brief Function like strstr but based on the string delimiter. - * - * @param[in] str String. - * @param[in] delim Delimiter. - * - * @return Returns the output string. - */ -char *strSplit(char *str, char *delim) -{ - static char *next_str; - char *last = NULL; - if(str != NULL) + else { - next_str = str; + T2Info("File size rounded to nearest page size used for offset read: %jd bytes\n", (intmax_t)offset_in_page_size_multiple); + addrcf = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, offset_in_page_size_multiple); + bytes_ignored_main = bytes_ignored; + addrrf = NULL; // No rotated file in this case } - if(NULL == next_str) + close(fd); + + if (addrcf == MAP_FAILED) { - return next_str; + if(addrrf != NULL) + { + munmap(addrrf, rb.st_size); + } + T2Error("Error in memory mapping file %d: %s\n", fd, strerror(errno)); + return NULL; } - - last = strstr(next_str, delim); - if(NULL == last) + if (addrrf == MAP_FAILED) { - char *ret = next_str; - next_str = NULL; - return ret; + munmap(addrcf, sb.st_size); + T2Error("Error in memory mapping file %d: %s\n", fd, strerror(errno)); + return NULL; } - - char *ret = next_str; - *last = '\0'; - next_str = last + strlen(delim); - return ret; -} - -/** - * @brief To get node data type based on pattern. - * - * @param[in] filename Conf filename. - * @param[in] header node header. - * @param[out] dtype Data type - * - * @return Returns status of operation. - */ -void getDType(char *filename, MarkerType mType, DType_t *dtype) -{ - if (mType != MTYPE_COUNTER) + fileDescriptor = (FileDescriptor*)malloc(sizeof(FileDescriptor)); + if (!fileDescriptor) + { + T2Error("Error allocating memory for FileDescriptor\n"); + return NULL; + } + memset(fileDescriptor, 0, sizeof(FileDescriptor)); + fileDescriptor->baseAddr = (void *)addrcf; + addrcf += bytes_ignored_main; + if(addrrf != NULL) + { + fileDescriptor->rotatedAddr = (void *)addrrf; + addrrf += bytes_ignored_rotated; + fileDescriptor->rfaddr = addrrf; + } + else { - *dtype = STR; + fileDescriptor->rotatedAddr = NULL; + fileDescriptor->rfaddr = NULL; } - else if(0 == strcmp(filename, "top_log.txt") || 0 == strcmp(filename, "")) + fileDescriptor->cfaddr = addrcf; + fileDescriptor->fd = fd; + fileDescriptor->cf_file_size = sb.st_size; + if(fileDescriptor->rfaddr != NULL) { - *dtype = STR; + fileDescriptor->rf_file_size = rb.st_size; } else { - *dtype = OCCURENCE; + fileDescriptor->rf_file_size = 0; } + return fileDescriptor; } +// Call 2 /** @description: Main logic function to parse sorted vector list and to process the pattern list * @param filename * @return -1 on failure, 0 on success */ -static int parseMarkerList(char* profileName, Vector* vMarkerList, Vector* grepResultList, bool check_rotated) +static int parseMarkerListOptimized(GrepSeekProfile *gsProfile, Vector * ip_vMarkerList, Vector * out_grepResultList, bool check_rotated, char* logPath) { T2Debug("%s ++in \n", __FUNCTION__); - char *filename = NULL, *prevfile = NULL; - rdkList_t *pchead = NULL, *rdkec_head = NULL; - GrepSeekProfile* gsProfile = NULL; - size_t var = 0; - - size_t vCount = Vector_Size(vMarkerList); - T2Debug("vMarkerList for profile %s is of count = %lu \n", profileName, (unsigned long )vCount); - - // Get logfile -> seek value map associated with the profile - gsProfile = (GrepSeekProfile *) getLogSeekMapForProfile(profileName); - if(NULL == gsProfile) + if(NULL == gsProfile || NULL == ip_vMarkerList || NULL == out_grepResultList) { - T2Debug("logSeekMap is null, add logSeekMap for %s \n", profileName); - gsProfile = (GrepSeekProfile *) addToProfileSeekMap(profileName); + T2Error("Invalid arguments for %s\n", __FUNCTION__); + return -1; } + char *prevfile = NULL; + //GrepSeekProfile* gsProfile = NULL; + size_t var = 0; + size_t vCount = Vector_Size(ip_vMarkerList); + + // Get logfile -> seek value map associated with the profile + //gsProfile = (GrepSeekProfile *) getLogSeekMapForProfile(profileName); if(NULL == gsProfile) { - T2Error("%s Unable to retrive / create logSeekMap for profile %s \n", __FUNCTION__, profileName); + T2Error("%s Unable to retrieve/create logSeekMap for profile \n", __FUNCTION__); return -1; } - int profileExecCounter = gsProfile->execCounter; - if((gsProfile->execCounter == 1) && (firstreport_after_bootup == false)) //checking the execution count and first report after bootup because after config reload again execution count will get initialised and again reaches 1. check_rotated logs flag is to check the rotated log files even when seekvalue is less than filesize for the first time. + // checking the execution count and first report after bootup because after config reload again execution count will get initialised and again reaches 1. + // check_rotated logs flag is to check the rotated log files even when seekvalue is less than filesize for the first time. + if((gsProfile->execCounter == 1) && (firstreport_after_bootup == false)) { check_rotated_logs = check_rotated; firstreport_after_bootup = true; @@ -807,205 +895,146 @@ static int parseMarkerList(char* profileName, Vector* vMarkerList, Vector* grepR check_rotated_logs = false; } - // Traverse through marker list - for( var = 0; var < vCount; ++var ) - { - - GrepMarker* markerList = (GrepMarker*) Vector_At(vMarkerList, var); - int tmp_skip_interval, is_skip_param; + // Loops start here - This should be completed here + // Traverse through sorted ip_vMarkerList marker list + // Reuse the file descriptor or memmory mapped I/O when the log file is same between iterations - char *temp_header = markerList->markerName; - char *temp_pattern = markerList->searchString; - char *temp_file = markerList->logFile; - bool trim = markerList->trimParam; - char *regex = markerList->regexParam; - tmp_skip_interval = markerList->skipFreq; + int fd = -1; + FileDescriptor* fileDescriptor = NULL; - DType_t dtype; - - if(NULL == temp_file || NULL == temp_pattern || NULL == temp_header) + for (var = 0; var < vCount; ++var) // Loop of marker list starts here + { + GrepMarker* grepMarkerObj = (GrepMarker*) Vector_At(ip_vMarkerList, var); + if (!grepMarkerObj || !grepMarkerObj->logFile || !grepMarkerObj->searchString || !grepMarkerObj->markerName) { continue; } - - if((0 == strcmp(temp_pattern, "")) || (0 == strcmp(temp_file, ""))) + if (strcmp(grepMarkerObj->searchString, "") == 0 || strcmp(grepMarkerObj->logFile, "") == 0) { continue; } - if(0 == strcasecmp(temp_file, "snmp")) - { - continue; - } + int tmp_skip_interval, is_skip_param; + tmp_skip_interval = grepMarkerObj->skipFreq; - getDType(temp_file, markerList->mType, &dtype); + char *log_file_for_this_iteration = grepMarkerObj->logFile; - if(tmp_skip_interval <= 0) + // For first iteration and when the log file changes + if (NULL == prevfile || strcmp(log_file_for_this_iteration, prevfile) != 0) { - tmp_skip_interval = 0; - } + if (prevfile != NULL) + { + free(prevfile); + prevfile = NULL; + } - if(profileExecCounter % (tmp_skip_interval + 1) == 0) - { - is_skip_param = 0; - } - else - { - is_skip_param = 1; - } + if (fd != -1) + { + close(fd); + fd = -1; + } - if(NULL == filename) - { - filename = strdup(temp_file); - if(filename == NULL) + if (fileDescriptor != NULL) { - T2Error("Insufficient memory available to allocate duplicate string %s\n", temp_file); + freeFileDescriptor(fileDescriptor); + fileDescriptor = NULL; } - } - else - { - if(0 != strcmp(filename, temp_file)) + + // Get a valid file descriptor for the current log file + off_t seek_value = 0; + fd = getLogFileDescriptor(gsProfile, logPath, log_file_for_this_iteration, fd, &seek_value); + prevfile = updateFilename(prevfile, log_file_for_this_iteration); + if (fd == -1) + { + T2Error("Error opening file %s\n", log_file_for_this_iteration); + continue; + } + + fileDescriptor = getFileDeltaInMemMapAndSearch(fd, seek_value, logPath, log_file_for_this_iteration, check_rotated_logs); + if (fileDescriptor == NULL) { - free(filename); - filename = strdup(temp_file); - if(filename == NULL) + T2Error("Failed to get file descriptor for %s\n", log_file_for_this_iteration); + if (fd != -1) { - T2Error("Insufficient memory available to allocate duplicate string %s\n", temp_file); + close(fd); + fd = -1; } + continue; } } - // TODO optimize the list search in US - if(is_skip_param == 0) + if(tmp_skip_interval <= 0) { - if(0 == insertPCNode(&pchead, temp_pattern, temp_header, dtype, 0, NULL, trim, regex)) - { - processPattern(&prevfile, filename, &rdkec_head, pchead, grepResultList, gsProfile->logFileSeekMap, &(markerList->firstSeekFromEOF), check_rotated_logs); - pchead = NULL; - } + tmp_skip_interval = 0; } - else + is_skip_param = (profileExecCounter % (tmp_skip_interval + 1) == 0) ? 0 : 1; + // If skip param is 0, then process the pattern with optimized function + if (is_skip_param == 0 && fileDescriptor != NULL) { - T2Debug("Current iteration for this parameter needs to be excluded, but the seek values needs to be updated in case of logfile based marker\n"); - // TODO optimize seek update logic for skip intervals - updateLastSeekval(gsProfile->logFileSeekMap, &prevfile, filename); + + // Call the optimized function to process the pattern + processPatternWithOptimizedFunction(grepMarkerObj, out_grepResultList, fileDescriptor); } - } // End of adding list to node -#if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) - // remove the saved top information - pthread_mutex_lock(&topOutputMutex); - removeTopOutput(); - pthread_mutex_unlock(&topOutputMutex); -#endif + } // Loop of marker list ends here - if(filename) - { - updateLogSeek(gsProfile->logFileSeekMap, filename); - } - gsProfile->execCounter += 1; T2Debug("Execution Count = %d\n", gsProfile->execCounter); - /* max limit not maintained for rdkec_head FIXME */ - if(NULL != rdkec_head) + if (prevfile != NULL) { - addToJson(rdkec_head); - // clear nodes memory after process - clearPCNodes(&rdkec_head); - rdkec_head = NULL; + free(prevfile); + prevfile = NULL; } - if(NULL != filename) + if (fd != -1) { - free(filename); + close(fd); + fd = -1; } - if(NULL != prevfile) + if (fileDescriptor != NULL) { - free(prevfile); + freeFileDescriptor(fileDescriptor); + fileDescriptor = NULL; } T2Debug("%s --out \n", __FUNCTION__); return 0; } -int getDCAResultsInJson(char* profileName, void* markerList, cJSON** grepResultList) +void T2InitProperties() { - - T2Debug("%s ++in \n", __FUNCTION__); - int rc = -1; - - /* - * Keeping the lock here to be aligned with getDCAResultsInVector(). - * Although the lock here is redundant, coz for multiprocess devices - * dcautil is having pInterChipLock for all inter-processor communications. - */ - - pthread_mutex_lock(&dcaMutex); - if(NULL != markerList) - { - if (!isPropsInitialized()) - { - initProperties(logPath, persistentPath); - } - - initSearchResultJson(&ROOT_JSON, &SEARCH_RESULT_JSON); - - rc = parseMarkerList(profileName, markerList, NULL, false); - *grepResultList = ROOT_JSON; - } - pthread_mutex_unlock(&dcaMutex); - - T2Debug("%s --out \n", __FUNCTION__); - return rc; + initProperties(&LOGPATH, &PERSISTENTPATH, &PAGESIZE); } -int getDCAResultsInVector(char* profileName, Vector* vecMarkerList, Vector** grepResultList, bool check_rotated, char* customLogPath) +// Call 1 +int getDCAResultsInVector(GrepSeekProfile *gSeekProfile, Vector * vecMarkerList, Vector** out_grepResultList, bool check_rotated, char* customLogPath) { T2Debug("%s ++in \n", __FUNCTION__); int rc = -1; - - /* - * Serializing grep result functionality, - * to avoid synchronization issue on single processor device when - * multiple profiles tries to get grep result. - * TODO: Remove static variables from log files processing apis. - */ + if(NULL == gSeekProfile || NULL == vecMarkerList || NULL == out_grepResultList) + { + T2Error("Invalid arguments for %s\n", __FUNCTION__); + return rc; + } pthread_mutex_lock(&dcaMutex); if(NULL != vecMarkerList) { - if (!isPropsInitialized()) - { - initProperties(logPath, persistentPath); - } + char* logPath = customLogPath ? customLogPath : LOGPATH; - if (customLogPath) - { - initProperties(customLogPath, persistentPath); - } + Vector_Create(out_grepResultList); - Vector_Create(grepResultList); - if( (rc = parseMarkerList(profileName, vecMarkerList, *grepResultList, check_rotated)) == -1 ) + // Go for looping through the marker list + if( (rc = parseMarkerListOptimized(gSeekProfile, vecMarkerList, *out_grepResultList, check_rotated, logPath)) == -1 ) { T2Debug("Error in fetching grep results\n"); } - if (customLogPath) - { - initProperties(logPath, persistentPath); - } } pthread_mutex_unlock(&dcaMutex); T2Debug("%s --out \n", __FUNCTION__); return rc; } - - -/** @} */ - -/** @} */ -/** @} */ - diff --git a/source/dcautil/dca.h b/source/dcautil/dca.h index a0db1d05..ce333937 100644 --- a/source/dcautil/dca.h +++ b/source/dcautil/dca.h @@ -21,16 +21,16 @@ #include #include "vector.h" #include +#include "legacyutils.h" /** * Caller should be freeing vectorMarkerList and grepResultList */ -int getDCAResultsInJson(char* profileName, void* vectorMarkerList, cJSON** grepResultList); -int getDCAResultsInVector(char* profileName, Vector* vectorMarkerList, Vector** grepResultList, bool check_rotated, char* customLogPath); +int getDCAResultsInVector(GrepSeekProfile *gSeekProfile, Vector* vectorMarkerList, Vector** out_grepResultList, bool check_rotated, char* customLogPath); -char *strSplit(char *str, char *delim); +void T2InitProperties(); // declaration for initProperties -int getErrorCode(char *str, char *ec); +int processTopPattern(char* profileName, Vector* topMarkerList, Vector* out_grepResultList, int execCount); #endif /* SRC_DCA_H_ */ diff --git a/source/dcautil/dcajson.c b/source/dcautil/dcajson.c deleted file mode 100644 index 1cb1fb8d..00000000 --- a/source/dcautil/dcajson.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2016 RDK Management - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @defgroup dca - * @{ - **/ - -/** - * @defgroup dca - * @{ - * @defgroup src - * @{ - **/ - -// To provide telemetry data in a JSON format -#include "legacyutils.h" -#include "t2log_wrapper.h" - -/** - * @addtogroup DCA_APIS - * @{ - */ - -/** - * @brief This API creates "searchResult" JSON array. - * - * The search result list contains collection of telemetry marker headers with their value. - * - * Eg: {"searchResult":[{"MOCA_INFO_pnc_enabled":"1"},{"samv2_boardver_split":" V3.0 ##"},{"RF_ERR_DS_lockfail":"1"},{"RF_ERR_T3_timeout":"2"}]} - * - * @param[out] root JSON object - * @param[in] sr Search result JSON array - */ - -// {"searchResult":[{}]} Telemetry 2.0 is not bound to any such data type - Get rid of this !!! -void initSearchResultJson(cJSON **root, cJSON **sr) -{ - T2Debug("%s ++in \n", __FUNCTION__); - *root = cJSON_CreateObject(); - if(NULL != *root) - { - cJSON_AddItemToObject(*root, "searchResult", *sr = cJSON_CreateArray()); - } - T2Debug("%s --out \n", __FUNCTION__); -} - -/** - * @brief This API is to append the key/value pair to the SearchResult JSON array . - * - * @param[in] key marker name - * @param[in] value metric count - */ -void addToSearchResult(char *key, char *value) -{ - T2Debug("%s ++in \n", __FUNCTION__); - if(key == NULL || value == NULL) - { - T2Error("Key or Value is NULL\n"); - return; - } - if(NULL != SEARCH_RESULT_JSON) - { - cJSON *obj = cJSON_CreateObject(); - if(NULL != obj) - { - cJSON_AddStringToObject(obj, key, value); - cJSON_AddItemToArray(SEARCH_RESULT_JSON, obj); - } - } - T2Debug("%s --out \n", __FUNCTION__); -} - -/** - * @brief This API deletes the result JSON object. - * - * @param[in] root JSON object to be deleted. - */ -void clearSearchResultJson(cJSON **root) -{ - T2Debug("%s ++in \n", __FUNCTION__); - if(root == NULL) - { - T2Error("root is NULL, can't be deleted\n"); - return; - } - cJSON_Delete(*root); - T2Debug("%s --out \n", __FUNCTION__); -} - -/** @} */ //END OF GROUP DCA_APIS -/** @} */ - -/** @} */ -/** @} */ diff --git a/source/dcautil/dcalist.c b/source/dcautil/dcalist.c deleted file mode 100644 index 34ffa94c..00000000 --- a/source/dcautil/dcalist.c +++ /dev/null @@ -1,248 +0,0 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2016 RDK Management - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - - -/** - * @defgroup dca - * @{ - **/ - - - - -/** - * @defgroup dca - * @{ - * @defgroup src - * @{ - **/ - -#include "dcalist.h" - -#include "legacyutils.h" -#include "t2log_wrapper.h" - -/* - * @addtogroup DCA_APIS - * @{ - */ - -/** - * @brief To insert a new pattern node at the beginning of the list. - * - * @param[in] pch Node head. - * @param[in] pattern Search pattern. - * @param[in] header Header. - * @param[in] dtype Data type. - * @param[in] count Pattern count. - * @param[in] data Data. - * @param[in] trim bool value of trim - * - * @return Returns the value of rc. - */ -int insertPCNode(rdkList_t **pch, char *pattern, char *header, DType_t dtype, int count, char *data, bool trim, char *regex) -{ - pcdata_t *new = NULL; - int rc = -1; - new = (pcdata_t *) malloc(sizeof(*new)); - if (NULL != new) - { - if (pattern != NULL) - { - new->pattern = strdup(pattern); - } - else - { - new->pattern = NULL; - } - if (header != NULL) - { - new->header = strdup(header); - } - else - { - new->header = NULL; - } - - new->d_type = dtype; - if (dtype == OCCURENCE) - { - new->count = count; - } - else if (dtype == STR) - { - if (NULL != data) - { - new->data = strdup(data); - } - else - { - new->data = NULL; - } - } - new->trimparam = trim; - new->regexparam = regex; - *pch = rdk_list_add_node(*pch, new); - rc = 0; - } - return rc; -} - -/** - * @brief To do custom comparison. - * - * @param[in] np Node pattern. - * @param[in] sp Search pattern. - * - * @return Returns status of the operation. - * @retval Returns 0 on success and NULL on failure. - */ -int comparePattern(const void *np, const void *sp) -{ - pcdata_t *tmp = (pcdata_t *)np; - if (tmp && tmp->pattern && (NULL != sp) && (NULL != strstr(sp, tmp->pattern))) - { - return 0; - } - return -1; -} - -/** - * @brief To search node from the list based on the given pattern. - * - * @param[in] pch Node head. - * @param[in] pattern Pattern to search. - * - * @return Returns node on success and NULL on failure. - */ -pcdata_t* searchPCNode(rdkList_t *pch, char *pattern) -{ - rdkList_t *fnode = NULL; - if(pch == NULL && pattern == NULL) - { - return NULL; - } - fnode = rdk_list_find_node_custom(pch, pattern, (fnRDKListCustomCompare)comparePattern); - if (NULL != fnode) - { - return fnode->m_pUserData; - } - else - { - return NULL; - } -} - -/** - * @brief Debug function to print the node. - * - * @param[in] data node data - * @param[in] user_data user data - */ -void print_pc_node(void *data, void *user_data) -{ - (void) user_data; - if(data == NULL) - { - return; - } - pcdata_t *node = (pcdata_t *)data; - if (node) - { - T2Debug("node pattern:%s, header:%s", node->pattern, node->header); - if (node->d_type == OCCURENCE) - { - T2Debug("\tcount:%d", node->count); - } - else if (node->d_type == STR) - { - T2Debug("\tdata:%s", node->data); - } - } -} - -/** - * @brief Debug function to print all nodes in the list. - * - * @param[in] pch node head - */ -void printPCNodes(rdkList_t *pch) -{ - if(pch == NULL) - { - return; - } - rdk_list_foreach(pch, (fnRDKListCustomExecute)print_pc_node, NULL); -} - -/** - * @brief To delete a node. - * - * @param[in] node Node head. - */ -void freePCNode(void *node) -{ - pcdata_t *tmp = (pcdata_t *)(node); - if (NULL != tmp) - { - if (NULL != tmp->pattern) - { - free(tmp->pattern); - tmp->pattern = NULL; - } - if (NULL != tmp->header) - { - free(tmp->header); - tmp->header = NULL; - } - if (tmp->d_type == STR) - { - if (NULL != tmp->data) - { - free(tmp->data); - tmp->data = NULL; - } - } - free(tmp); - } -} - -/** - * @brief To delete/clear all the nodes in the list. - * - * @param[in] pch Node head. - */ -void clearPCNodes(rdkList_t **pch) -{ - if(pch == NULL) - { - return; - } - rdk_list_free_all_nodes_custom(*pch, &freePCNode); -} - -/** @} */ //END OF GROUP DCA_APIS - -/** @} */ - - -/** @} */ -/** @} */ - diff --git a/source/dcautil/dcalist.h b/source/dcautil/dcalist.h index daa3d402..0c10e140 100644 --- a/source/dcautil/dcalist.h +++ b/source/dcautil/dcalist.h @@ -71,7 +71,6 @@ void printPCNodes(rdkList_t *pch); void clearPCNodes(rdkList_t **pch); int comparePattern(const void *np, const void *sp); void print_pc_node(void *data, void *user_data); -int processTopPattern(rdkList_t *pchead, Vector* grepResultList); /** @} */ diff --git a/source/dcautil/dcaproc.c b/source/dcautil/dcaproc.c index 3d6970ba..e4b8d62c 100644 --- a/source/dcautil/dcaproc.c +++ b/source/dcautil/dcaproc.c @@ -63,11 +63,6 @@ * @{ */ -/** - * @addtogroup DCA_APIS - * @{ - */ - /** * @brief To get process usage. * @@ -77,7 +72,7 @@ * @retval 0 on sucess, appropiate errorcode otherwise. */ -int getProcUsage(char *processName, Vector* grepResultList, bool trim, char* regex) +int getProcUsage(char *processName, Vector* grepResultList, bool trim, char* regex, char* filename) { T2Debug("%s ++in \n", __FUNCTION__); if(grepResultList == NULL || processName == NULL) @@ -233,7 +228,7 @@ int getProcUsage(char *processName, Vector* grepResultList, bool trim, char* reg pInfo.total_instance = index; pInfo.pid = pid; - if(0 != getProcInfo(&pInfo)) + if(0 != getProcInfo(&pInfo, filename)) { mem_key = malloc(pname_prefix_len); cpu_key = malloc(pname_prefix_len); @@ -366,7 +361,7 @@ int getProcPidStat(int pid, procinfo * pinfo) * @return Returns status of operation. * @retval Return 1 on success. */ -int getProcInfo(procMemCpuInfo *pmInfo) +int getProcInfo(procMemCpuInfo *pmInfo, char* filename) { T2Debug("%s ++in \n", __FUNCTION__); if(0 == getMemInfo(pmInfo)) @@ -374,7 +369,7 @@ int getProcInfo(procMemCpuInfo *pmInfo) return 0; } - if(0 == getCPUInfo(pmInfo)) + if(0 == getCPUInfo(pmInfo, filename)) { return 0; } @@ -433,19 +428,31 @@ int getMemInfo(procMemCpuInfo *pmInfo) #if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) -void saveTopOutput() +char* saveTopOutput(char* profilename) { + char filename[128] = {"\0"}; + char* retfile = NULL; + if(profilename != NULL) + { + snprintf(filename, sizeof(filename), "%s_%s", TOPTEMP, profilename); + retfile = strdup(filename); + } + else + { + return retfile; + } + T2Debug("%s ++in \n", __FUNCTION__); - if(access(TOPTEMP, F_OK) == 0) + if(access(filename, F_OK) == 0) { T2Debug("%s --out \n", __FUNCTION__); - return; + removeTopOutput(strdup(retfile)); } int ret = 0; char command[CMD_LEN] = { '\0' }; int cmd_option = 0; /* Check Whether -c option is supported */ - sprintf(command, "top -b -n 1 -c > %s ", TOPTEMP); + sprintf(command, "top -b -n 1 -c > %s ", filename); #ifdef LIBSYSWRAPPER_BUILD ret = v_secure_system(command); #else @@ -462,16 +469,16 @@ void saveTopOutput() /* Format Use: `top n 1 | grep Receiver` */ if ( 1 == cmd_option ) { - sprintf(command, "COLUMNS=512 top -n %d -c > %s ", TOPITERATION, TOPTEMP); + sprintf(command, "COLUMNS=512 top -n %d -c > %s ", TOPITERATION, filename); } else { - sprintf(command, "top -n %d > %s", TOPITERATION, TOPTEMP); + sprintf(command, "top -n %d > %s", TOPITERATION, filename); } #else /* ps -C Receiver -o %cpu -o %mem */ //sprintf(command, "ps -C '%s' -o %%cpu -o %%mem | sed 1d", pInfo->processName); - snprintf(command, CMD_LEN, "%s top -b -n %d %s > %s", (cmd_option == 1) ? "COLUMNS=512" : "", TOPITERATION, (cmd_option == 1) ? "-c" : "", TOPTEMP); + snprintf(command, CMD_LEN, "%s top -b -n %d %s > %s", (cmd_option == 1) ? "COLUMNS=512" : "", TOPITERATION, (cmd_option == 1) ? "-c" : "", filename); #endif @@ -480,39 +487,49 @@ void saveTopOutput() #else ret = system(command); #endif - if(ret != 0) + if(ret == 0) { - T2Debug("return value of system command to create %s is success with code %d\n", TOPTEMP, ret); + T2Debug("return value of system command to create %s is success with code\n", filename); } else { - T2Error("return value of system command to create %s is not successful with code \n", TOPTEMP); - return; + T2Error("return value of system command to create %s is not successful with code %d \n", filename, ret); + if(retfile != NULL) + { + free(retfile); + } + return NULL; } T2Debug("%s --out \n", __FUNCTION__); + return retfile; } -void removeTopOutput() +void removeTopOutput(char* filename) { T2Debug("%s ++in \n", __FUNCTION__); - int ret = 0; - char command[256] = {'\0'}; - snprintf(command, sizeof(command), "rm -rf %s", TOPTEMP); + if(filename != NULL) + { + int ret = 0; + char command[256] = {'\0'}; + snprintf(command, sizeof(command), "rm -rf %s", filename); #ifdef LIBSYSWRAPPER_BUILD - ret = v_secure_system(command); + ret = v_secure_system(command); #else - ret = system(command); + ret = system(command); #endif - if(ret == 0) - { - T2Debug("return value of system command to remove %s is success with code %d \n", TOPTEMP, ret); - } - else - { - T2Error("return value of system command to remove %s is not successful with code %d \n", TOPTEMP, ret); + if(ret == 0) + { + T2Debug("return value of system command to remove %s is success with code %d \n", filename, ret); + } + else + { + T2Error("return value of system command to remove %s is not successful with code %d \n", filename, ret); + } + free(filename); + T2Debug("%s --out \n", __FUNCTION__); + return; } - T2Debug("%s --out \n", __FUNCTION__); } //#if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) @@ -524,7 +541,7 @@ void removeTopOutput() * @return Returns status of operation. * @retval Return 1 on success,appropiate errorcode otherwise. */ -int getCPUInfo(procMemCpuInfo *pInfo) +int getCPUInfo(procMemCpuInfo *pInfo, char* filename) { int ret = 0, pclose_ret = 0; FILE *inFp = NULL; @@ -549,9 +566,9 @@ int getCPUInfo(procMemCpuInfo *pInfo) return 0; } - if(access(TOPTEMP, F_OK) != 0) + if((filename != NULL) && (access(filename, F_OK) != 0)) { - T2Debug("%s ++in the savad temp log %s is not available \n", __FUNCTION__, TOPTEMP); + T2Debug("%s ++in the savad temp log %s is not available \n", __FUNCTION__, filename); /* Check Whether -c option is supported */ #ifdef LIBSYSWRAPPER_BUILD ret = v_secure_system(" top -c -n 1 2> /dev/null 1> /dev/null"); @@ -597,11 +614,11 @@ int getCPUInfo(procMemCpuInfo *pInfo) } else { - T2Debug("%s ++in the savad temp log %s is available \n", __FUNCTION__, TOPTEMP); + T2Debug("%s ++in the savad temp log %s is available \n", __FUNCTION__, filename); #ifdef LIBSYSWRAPPER_BUILD inFp = v_secure_popen("r", "cat %s |grep -i '%s'", TOPTEMP, pInfo->processName); #else - snprintf(command, sizeof(command), "cat %s |grep -i '%s'", TOPTEMP, pInfo->processName); + snprintf(command, sizeof(command), "cat %s |grep -i '%s'", filename, pInfo->processName); inFp = popen(command, "r"); #endif normalize = TOPITERATION; @@ -759,8 +776,9 @@ int getProcessCpuUtilization(int pid, float *procCpuUtil) return 1; } -int getCPUInfo(procMemCpuInfo *pmInfo) +int getCPUInfo(procMemCpuInfo *pmInfo, char* filename) { + (void)filename; // Unused parameter, can be removed if not needed float cpu = 0; float total_cpu = 0; int index = 0; diff --git a/source/dcautil/dcautil.c b/source/dcautil/dcautil.c index 7eb25e03..f0dd7300 100644 --- a/source/dcautil/dcautil.c +++ b/source/dcautil/dcautil.c @@ -28,43 +28,40 @@ #include "legacyutils.h" #include "persistence.h" +/** + * @brief Get the Grep Results object. Main function called by rest of the consumers. + * + * @param profileName + * @param markerList + * @param grepResultList + * @param isClearSeekMap + * @param check_rotated + * @param customLogPath + * @return T2ERROR + */ T2ERROR -getGrepResults (char *profileName, Vector *markerList, Vector **grepResultList, bool isClearSeekMap, bool check_rotated, char *customLogPath) +getGrepResults (GrepSeekProfile **GSP, Vector *markerList, Vector **grepResultList, bool isClearSeekMap, bool check_rotated, char *customLogPath) { T2Debug("%s ++in\n", __FUNCTION__); - if(profileName == NULL || markerList == NULL || grepResultList == NULL) + if(GSP == NULL || markerList == NULL || grepResultList == NULL) { T2Error("Invalid Args or Args are NULL\n"); return T2ERROR_FAILURE; } - getDCAResultsInVector(profileName, markerList, grepResultList, check_rotated, customLogPath); + getDCAResultsInVector(*GSP, markerList, grepResultList, check_rotated, customLogPath); if (isClearSeekMap) { - removeProfileFromSeekMap(profileName); + int count = (*GSP)->execCounter; + freeGrepSeekProfile(*GSP); + *GSP = createGrepSeekProfile(count); } T2Debug("%s --out\n", __FUNCTION__); return T2ERROR_SUCCESS; } -void removeGrepConfig(char* profileName, bool clearSeekMap, bool clearExecMap) -{ - T2Debug("%s ++in\n", __FUNCTION__); - - if(clearSeekMap) - { - removeProfileFromSeekMap(profileName); - } - - if (clearExecMap) - { - removeProfileFromExecMap(profileName); - } - T2Debug("%s ++out\n", __FUNCTION__); -} - // dcaFlagReportCompleation this function is used to create legacy DCA Flag DCADONEFLAG void dcaFlagReportCompleation() { @@ -82,7 +79,7 @@ void dcaFlagReportCompleation() } # ifdef PERSIST_LOG_MON_REF -T2ERROR saveSeekConfigtoFile(char* profileName) +T2ERROR saveSeekConfigtoFile(char* profileName, GrepSeekProfile *ProfileSeekMap) { T2Debug("%s ++in\n", __FUNCTION__); if(profileName == NULL) @@ -90,7 +87,6 @@ T2ERROR saveSeekConfigtoFile(char* profileName) T2Error("Profile Name is not available\n"); return T2ERROR_FAILURE; } - GrepSeekProfile *ProfileSeekMap = getLogSeekMapForProfile(profileName); if(ProfileSeekMap == NULL) { T2Error("ProfileSeekMap is NULL\n"); @@ -128,7 +124,7 @@ T2ERROR saveSeekConfigtoFile(char* profileName) return T2ERROR_SUCCESS; } -T2ERROR loadSavedSeekConfig(char *profileName) +T2ERROR loadSavedSeekConfig(char *profileName, GrepSeekProfile *ProfileSeekMap) { T2Debug("%s ++in\n", __FUNCTION__); @@ -163,12 +159,7 @@ T2ERROR loadSavedSeekConfig(char *profileName) data[fileSize] = '\0'; cJSON *json = cJSON_Parse(data); cJSON *item = NULL; - GrepSeekProfile *ProfileSeekMap = NULL; - ProfileSeekMap = (GrepSeekProfile *) getLogSeekMapForProfile(profileName); - if (ProfileSeekMap == NULL) - { - ProfileSeekMap = (GrepSeekProfile *) addToProfileSeekMap(profileName); - } + //GrepSeekProfile *ProfileSeekMap = NULL; cJSON_ArrayForEach(item, json) { // Each `item` is an object in the array diff --git a/source/dcautil/dcautil.h b/source/dcautil/dcautil.h index 7bd2e43b..7b09c23d 100644 --- a/source/dcautil/dcautil.h +++ b/source/dcautil/dcautil.h @@ -23,8 +23,12 @@ #include #include "telemetry2_0.h" #include "vector.h" +#include "dca.h" +#include +#include +#include "legacyutils.h" -#define TOPTEMP "/tmp/.t2toplog" +#define TOPTEMP "/tmp/t2toplog" #define DCADONEFLAG "/tmp/.dca_done" #define PREVIOUS_LOG "PREVIOUS_LOG" @@ -40,14 +44,14 @@ typedef struct _GrepResult } GrepResult; #if !defined(ENABLE_RDKC_SUPPORT) && !defined(ENABLE_RDKB_SUPPORT) -void saveTopOutput(); -void removeTopOutput(); +char* saveTopOutput(char* profilename); +void removeTopOutput(char* filename); #endif void removeGrepConfig(char* profileName, bool clearSeek, bool clearExec); void freeGResult(void *data); T2ERROR saveGrepConfig(char *name, Vector* grepMarkerList); -T2ERROR getGrepResults(char* profileName, Vector *markerList, Vector **grepResultList, bool isClearSeekMap, bool check_rotated, char *customLogPath); +T2ERROR getGrepResults(GrepSeekProfile **GSP, Vector *markerList, Vector **grepResultList, bool isClearSeekMap, bool check_rotated, char *customLogPath); #define PREFIX_SIZE 5 #define BUF_LEN 16 @@ -70,17 +74,17 @@ typedef struct _procMemCpuInfo } procMemCpuInfo; /* @} */ // End of group DCA_TYPES -int getProcInfo(procMemCpuInfo *pInfo); +int getProcInfo(procMemCpuInfo *pInfo, char* filename); int getMemInfo(procMemCpuInfo *pmInfo); -int getCPUInfo(procMemCpuInfo *pInfo); +int getCPUInfo(procMemCpuInfo *pInfo, char* filename); int getProcPidStat(int pid, procinfo * pinfo); int getTotalCpuTimes(int * totalTime); #ifdef PERSIST_LOG_MON_REF typedef void (*freeconfigdata)(void *data); -T2ERROR saveSeekConfigtoFile(char* profileName); -T2ERROR loadSavedSeekConfig(char *profileName); +T2ERROR saveSeekConfigtoFile(char* profileName, GrepSeekProfile *ProfileSeekMap); +T2ERROR loadSavedSeekConfig(char *profileName, GrepSeekProfile *ProfileSeekMap); bool firstBootStatus(); #endif diff --git a/source/dcautil/legacyutils.c b/source/dcautil/legacyutils.c index 098837a8..03f57421 100644 --- a/source/dcautil/legacyutils.c +++ b/source/dcautil/legacyutils.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "t2log_wrapper.h" #include "legacyutils.h" @@ -32,16 +33,6 @@ static char* PERSISTENT_PATH = NULL; static char* LOG_PATH = NULL; static char* DEVICE_TYPE = NULL; -static bool isPropsIntialized = false ; -static long LAST_SEEK_VALUE = 0; - -// Map holding profile name to Map ( logfile -> seek value) ] -static hash_map_t *profileSeekMap = NULL; -static pthread_mutex_t pSeekLock = PTHREAD_MUTEX_INITIALIZER; - -// Map holding profile name to Exec Count -static hash_map_t *profileExecCountMap = NULL; -static pthread_mutex_t pExecCountLock = PTHREAD_MUTEX_INITIALIZER; /** * Start of functions dealing with log seek values @@ -50,27 +41,27 @@ static pthread_mutex_t pExecCountLock = PTHREAD_MUTEX_INITIALIZER; static void freeLogFileSeekMap(void *data) { T2Debug("%s ++in\n", __FUNCTION__); - if (data != NULL) + if (data) { - hash_element_t *element = (hash_element_t *) data; + hash_element_t *element = (hash_element_t *)data; if (element->key) { - T2Debug("Freeing hash entry element for Log file Name:%s\n", element->key); free(element->key); + element->key = NULL; } if (element->data) { - T2Debug("Freeing element data\n"); free(element->data); + element->data = NULL; } - free(element); + element = NULL; } T2Debug("%s --out\n", __FUNCTION__); } -static void freeGrepSeekProfile(GrepSeekProfile *gsProfile) +void freeGrepSeekProfile(GrepSeekProfile *gsProfile) { T2Debug("%s ++in\n", __FUNCTION__); if (gsProfile) @@ -81,6 +72,23 @@ static void freeGrepSeekProfile(GrepSeekProfile *gsProfile) T2Debug("%s --out\n", __FUNCTION__); } +GrepSeekProfile *createGrepSeekProfile(int execCounter) +{ + T2Debug("%s ++in\n", __FUNCTION__); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + if (gsProfile) + { + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = execCounter; + } + else + { + T2Error("Failed to allocate memory for GrepSeekProfile\n"); + } + T2Debug("%s --out\n", __FUNCTION__); + return gsProfile; +} +/* This is used to free the hash map for profile seek map but it is no more needed static void freeProfileSeekHashMap(void *data) { T2Debug("%s ++in\n", __FUNCTION__); @@ -106,244 +114,8 @@ static void freeProfileSeekHashMap(void *data) } T2Debug("%s --out\n", __FUNCTION__); } +*/ -GrepSeekProfile *addToProfileSeekMap(char* profileName) -{ - if(profileName == NULL) - { - T2Error("profileName is NULL\n"); - return NULL; - } - T2Debug("%s ++in for profileName = %s \n", __FUNCTION__, profileName); - - pthread_mutex_lock(&pSeekLock); - GrepSeekProfile *gsProfile = NULL; - if (profileSeekMap) - { - T2Debug("Adding GrepSeekProfile for profile %s in profileSeekMap\n", profileName); - gsProfile = malloc(sizeof(GrepSeekProfile)); - gsProfile->logFileSeekMap = hash_map_create(); - - pthread_mutex_lock(&pExecCountLock); - if(profileExecCountMap == NULL) - { - T2Debug("ExecCount map doesnot exist, creatinag a new hash map\n"); - profileExecCountMap = hash_map_create(); - gsProfile->execCounter = 0; - } - else - { - int *execCount = (int *) hash_map_get(profileExecCountMap, profileName); - if(execCount) - { - gsProfile->execCounter = *execCount; - T2Info("Update the Exec Count value to %d\n", gsProfile->execCounter); - } - } - pthread_mutex_unlock(&pExecCountLock); - - hash_map_put(profileSeekMap, strdup(profileName), (void*)gsProfile, freeProfileSeekHashMap); - } - else - { - T2Debug("profileSeekMap exists .. \n"); - } - pthread_mutex_unlock(&pSeekLock); - T2Debug("%s --out\n", __FUNCTION__); - return gsProfile; -} - -void removeProfileFromSeekMap(char *profileName) -{ - T2Debug("%s ++in\n", __FUNCTION__); - pthread_mutex_lock(&pSeekLock); - if (profileSeekMap) - { - GrepSeekProfile* gsProfile = (GrepSeekProfile *)hash_map_remove(profileSeekMap, profileName); - if (NULL != gsProfile) - { - - if(gsProfile->execCounter != 0) - { - int *temp = (int *) malloc(sizeof(int)); - T2Debug("Saving the ExecCount : %d for thr profile : %s\n", gsProfile->execCounter, profileName); - pthread_mutex_lock(&pExecCountLock); - if (profileExecCountMap == NULL) - { - T2Debug("ExecCount map doesnot exist, creatinag a new hash map\n"); - profileExecCountMap = hash_map_create(); - } - *temp = gsProfile->execCounter; - hash_map_put(profileExecCountMap, strdup(profileName), (void *) temp, free); - pthread_mutex_unlock(&pExecCountLock); - } - freeGrepSeekProfile(gsProfile); - - } - else - { - T2Debug("%s: Matching grep profile not found for %s\n", __FUNCTION__, profileName); - } - } - else - { - T2Debug("%s: profileSeekMap is empty \n", __FUNCTION__); - } - pthread_mutex_unlock(&pSeekLock); - T2Debug("%s --out\n", __FUNCTION__); -} - -void removeProfileFromExecMap(char *profileName) -{ - T2Debug("%s ++in\n", __FUNCTION__); - pthread_mutex_lock(&pExecCountLock); - if (profileExecCountMap) - { - int* value = (int *)hash_map_remove(profileExecCountMap, profileName); - if (NULL != value) - { - free(value); - T2Debug("%s : Released the exec count for profile %s \n", __FUNCTION__, profileName); - } - else - { - T2Debug("%s: Matching grep profile not found for %s\n", __FUNCTION__, profileName); - } - } - - pthread_mutex_unlock(&pExecCountLock); - // Adding this logic if profile name is same then we will not remove the seekMap in this case - // we need to check if the seek map exists, and assign the execCounter to 0 - pthread_mutex_lock(&pSeekLock); - if (profileSeekMap) - { - GrepSeekProfile* gsProfile = (GrepSeekProfile *)hash_map_get(profileSeekMap, profileName); - if (NULL != gsProfile) - { - gsProfile->execCounter = 0; - } - } - pthread_mutex_unlock(&pSeekLock); - T2Debug("%s --out\n", __FUNCTION__); -} - -// Returns map if found else NULL -GrepSeekProfile *getLogSeekMapForProfile(char* profileName) -{ - T2Debug("%s ++in\n", __FUNCTION__); - if(profileName == NULL) - { - T2Error("profileName is NULL for getLogSeekMap\n"); - return NULL; - } - GrepSeekProfile* gsProfile = NULL ; - T2Debug("Get profileseek map for %s \n", profileName); - pthread_mutex_lock(&pSeekLock); - - if(profileSeekMap == NULL) - { - T2Debug("Profile seek map doesn't exist, creating one ... \n"); - profileSeekMap = hash_map_create(); - } - - T2Debug("profileSeekMap count %d \n", hash_map_count(profileSeekMap)); - - if(profileSeekMap) - { - gsProfile = hash_map_get(profileSeekMap, profileName); - } - else - { - T2Debug("Profile seek map is NULL from getProfileSeekMap \n"); - } - - pthread_mutex_unlock(&pSeekLock); - T2Debug("%s --out\n", __FUNCTION__); - - return gsProfile; -} - -/** - * @brief Function to read the rotated Log file. - * - * @param[in] name Log file name. - * @param[in] seek_value Position to seek. - * - * @return Returns the status of the operation. - * @retval Returns -1 on failure, appropriate errorcode otherwise. - */ -static int getLogSeekValue(hash_map_t *logSeekMap, char *name, long *seek_value) -{ - - T2Debug("%s ++in for file %s \n", __FUNCTION__, name); - int rc = 0; - if (logSeekMap) - { - long *data = (long*) hash_map_get(logSeekMap, name) ; - if (data) - { - *seek_value = *data ; - } - else - { - T2Debug("data is null .. Setting seek value to 0 from getLogSeekValue \n"); - *seek_value = 0 ; - } - } - else - { - T2Debug("logSeekMap is null .. Setting seek value to 0 \n"); - *seek_value = 0 ; - } - - T2Debug("%s --out \n", __FUNCTION__); - return rc; -} - -/** - * @brief Function to write the rotated Log file. - * - * @param[in] name Log file name. - * - * @return Returns the status of the operation. - * @retval Returns -1 on failure, appropriate errorcode otherwise. - */ -T2ERROR updateLogSeek(hash_map_t *logSeekMap, char* logFileName) -{ - T2Debug("%s ++in\n", __FUNCTION__); - if(logSeekMap == NULL || logFileName == NULL) - { - T2Error("Invalid or NULL arguments\n"); - return T2ERROR_FAILURE; - } - - pthread_mutex_lock(&pSeekLock); - if(NULL != logSeekMap) - { - T2Debug("Adding seekvalue of %ld for %s to logSeekMap \n", LAST_SEEK_VALUE, logFileName); - long* val = (long *) malloc(sizeof(long)); - if(NULL != val) - { - *val = LAST_SEEK_VALUE ; - hash_map_put(logSeekMap, strdup(logFileName), (void *)val, free); - } - else - { - T2Warning("Unable to allocate memory for seek value pointer \n"); - } - } - else - { - T2Debug("logSeekMap is null, unable to update logSeekMap \n"); - } - pthread_mutex_unlock(&pSeekLock); - T2Debug("%s --out\n", __FUNCTION__); - return T2ERROR_SUCCESS; -} - -/** - * Start of functions dealing with log seek values - */ /** * @addtogroup DCA_APIS @@ -372,6 +144,7 @@ int getLoadAvg(Vector* grepResultList, bool trim, char* regex) T2Debug("Error in opening /proc/loadavg file"); return 0; } + if(fread(str, 1, LEN, fp) != LEN) { T2Debug("Error in reading loadavg"); @@ -379,6 +152,7 @@ int getLoadAvg(Vector* grepResultList, bool trim, char* regex) return 0; } fclose(fp); + str[LEN] = '\0'; if(grepResultList != NULL) @@ -386,8 +160,8 @@ int getLoadAvg(Vector* grepResultList, bool trim, char* regex) GrepResult* loadAvg = (GrepResult*) malloc(sizeof(GrepResult)); if(loadAvg) { - loadAvg->markerName = strdup("Load_Average"); - loadAvg->markerValue = strdup(str); + loadAvg->markerName = strndup("Load_Average", (strlen("Load_Average") + 1)); + loadAvg->markerValue = strndup(str, LEN); loadAvg->trimParameter = trim; loadAvg->regexParameter = regex; Vector_PushBack(grepResultList, loadAvg); @@ -397,28 +171,7 @@ int getLoadAvg(Vector* grepResultList, bool trim, char* regex) return 1; } -/** - * @brief This function returns file size. - * - * @param[in] fp File name - * - * @return Returns size of file. - */ -static int fsize(FILE *fp) -{ - // TODO optimize with fstat functions - int prev = ftell(fp); - fseek(fp, 0L, SEEK_END); - int sz = ftell(fp); - if(prev >= 0) - { - if(fseek(fp, prev, SEEK_SET) != 0) - { - T2Error("Cannot set the file position indicator for the stream pointed to by stream\n"); - } - } - return sz; -} + /** * @brief This function is to clear/free the global paths. @@ -443,318 +196,14 @@ void clearConfVal(void) T2Debug("%s --out \n", __FUNCTION__); } -void updateLastSeekval(hash_map_t *logSeekMap, char **prev_file, char* filename) -{ - - FILE *pcurrentLogFile = NULL; - char* currentLogFile = NULL; - long fileSize = 0; - - if((NULL == PERSISTENT_PATH) || (NULL == LOG_PATH) || (NULL == filename) || (NULL == logSeekMap)) - { - T2Debug("Path variables are empty"); - return; - } - - if((NULL == *prev_file) || (strcmp(*prev_file, filename) != 0)) - { - if(NULL == *prev_file) - { - *prev_file = strdup(filename); - if(*prev_file == NULL) - { - T2Error("Insufficient memory available to allocate duplicate string %s\n", filename); - } - } - else - { - updateLogSeek(logSeekMap, *prev_file); - free(*prev_file); - *prev_file = strdup(filename); - if(*prev_file == NULL) - { - T2Error("Insufficient memory available to allocate duplicate string %s\n", filename); - } - } - } - - int logname_len = strlen(LOG_PATH) + strlen(filename) + 1; - currentLogFile = malloc(logname_len); - if(currentLogFile) - { - snprintf(currentLogFile, logname_len, "%s%s", LOG_PATH, filename); - pcurrentLogFile = fopen(currentLogFile, "rb"); - if(pcurrentLogFile) - { - fileSize = fsize(pcurrentLogFile); - LAST_SEEK_VALUE = fileSize; - fclose(pcurrentLogFile); - } - free(currentLogFile); - } -} -/** - * @brief Function to return rotated log file. - * - * @param[in] buf Buffer - * @param[in] buflen Maximum buffer length - * @param[in] name Current Log file - * - * @return Returns Seek Log file. - */ -char *getLogLine(hash_map_t *logSeekMap, char *buf, int buflen, char *name, int *firstSeekFromEOF, bool check_rotated_logs) -{ - static FILE *pcurrentLogFile = NULL; - static int is_rotated_log = 0; - char *rval = NULL; - - if((NULL == PERSISTENT_PATH) || (NULL == LOG_PATH) || (NULL == name)) - { - T2Debug("Path variables are empty"); - return NULL; - } - if(logSeekMap == NULL || buf == NULL) - { - T2Debug("Invalid arguments or NULL arguments\n"); - return NULL; - } - char *logpath = LOG_PATH; - - /* If name is already an absolute path then prepend empty string instead of LOG_PATH */ - if (name[0] == '/') - { - logpath = ""; - } - - int logname_len = strlen(logpath) + strlen(name) + 1; - char *fileExtn = ".1"; - int fileExtn_len = logname_len + strlen(fileExtn); - if(NULL == pcurrentLogFile) - { - char *currentLogFile = NULL; - long seek_value = 0; - - currentLogFile = malloc(logname_len); - if(NULL != currentLogFile) - { - snprintf(currentLogFile, logname_len, "%s%s", logpath, name); - if(0 != getLogSeekValue(logSeekMap, name, &seek_value)) - { - pcurrentLogFile = fopen(currentLogFile, "rb"); - free(currentLogFile); - if(NULL == pcurrentLogFile) - { - return NULL; - } - } - else - { - long fileSize = 0; - pcurrentLogFile = fopen(currentLogFile, "rb"); - - if(NULL == pcurrentLogFile) - { - LAST_SEEK_VALUE = 0; - free(currentLogFile); - return NULL; - } - fileSize = fsize(pcurrentLogFile); - - // if the seek value is given in the profile use that value to calculate the seek value from the END OF FILE - // the seek value becomes filesize - firstSeekFromEOF since it cant be negetive set it zero if negative - if (*firstSeekFromEOF != 0) - { - seek_value = (fileSize > (*firstSeekFromEOF)) ? (fileSize - (*firstSeekFromEOF)) : 0 ; - T2Debug("First Seek from the EOF is given value : %d; the size of file is %ld; current seek after calculations %ld\n", *firstSeekFromEOF, fileSize, seek_value); - *firstSeekFromEOF = 0; // update this to zero as this initial seek update should only run for the first time - } - if(seek_value <= fileSize) - { - if(check_rotated_logs) // considering the rotated log file within few minutes after bootup, it should check only for the first time - { - char * rotatedLog; - size_t name_len = strlen(currentLogFile); - if(name_len > 2 && currentLogFile[name_len - 2] == '.' && currentLogFile[name_len - 1] == '0') - { - rotatedLog = strdup(currentLogFile); - if(NULL != rotatedLog) - { - rotatedLog[name_len - 1] = '1'; - T2Debug("Log file name seems to be having .0 extension hence Rotated log file name is %s\n", rotatedLog); - } - } - else - { - rotatedLog = malloc(fileExtn_len); - if(NULL != rotatedLog) - { - snprintf(rotatedLog, fileExtn_len, "%s%s%s", logpath, name, fileExtn); - T2Debug("Rotated log file name is %s\n", rotatedLog); - } - } - if(NULL != rotatedLog) - { - fclose(pcurrentLogFile); - pcurrentLogFile = NULL; - pcurrentLogFile = fopen(rotatedLog, "rb"); - is_rotated_log = 1; - if(NULL == pcurrentLogFile) - { - T2Debug("Error in opening file %s\n", rotatedLog); - is_rotated_log = 0; - pcurrentLogFile = fopen(currentLogFile, "rb"); - if(pcurrentLogFile == NULL) - { - T2Debug("Error in opening file %s\n", currentLogFile); - free(currentLogFile); - free(rotatedLog); - return NULL; - } - } - free(currentLogFile); - free(rotatedLog); - if(fseek(pcurrentLogFile, seek_value, 0) != 0) - { - T2Error("Cannot set the file position indicator for the stream pointed to by stream\n"); - } - } - else - { - T2Error("Malloc failure for rotated log\n"); - free(currentLogFile); - fclose(pcurrentLogFile); - return NULL; - } - } - else - { - free(currentLogFile); - if( fseek(pcurrentLogFile, seek_value, 0) != 0) - { - T2Error("Cannot set the file position indicator for the stream pointed to by stream\n"); - } - } - } - else - { - if((NULL != DEVICE_TYPE) && (0 == strcmp("broadband", DEVICE_TYPE))) - { - T2Debug("Telemetry file pointer corrupted"); - if(fseek(pcurrentLogFile, 0, 0) != 0) - { - T2Error("Cannot set the file position indicator for the stream pointed to by stream\n"); - } - if(currentLogFile != NULL) - { - free(currentLogFile); - } - } - else - { - char * rotatedLog; - size_t name_len = strlen(currentLogFile); - if(name_len > 2 && currentLogFile[name_len - 2] == '.' && currentLogFile[name_len - 1] == '0') - { - rotatedLog = strdup(currentLogFile); - if(NULL != rotatedLog) - { - rotatedLog[name_len - 1] = '1'; - T2Debug("Log file name seems to be having .0 extension hence Rotated log file name is %s\n", rotatedLog); - } - } - else - { - rotatedLog = malloc(fileExtn_len); - if(NULL != rotatedLog) - { - snprintf(rotatedLog, fileExtn_len, "%s%s%s", logpath, name, fileExtn); - T2Debug("Rotated log file name is %s\n", rotatedLog); - } - } - if(currentLogFile != NULL) - { - free(currentLogFile); - } - - if(NULL != rotatedLog) - { - fclose(pcurrentLogFile); - pcurrentLogFile = NULL; - pcurrentLogFile = fopen(rotatedLog, "rb"); - - if(NULL == pcurrentLogFile) - { - T2Debug("Error in opening file %s", rotatedLog); - LAST_SEEK_VALUE = 0; - free(rotatedLog); - return NULL; - } - free(rotatedLog); - - if(fseek(pcurrentLogFile, seek_value, 0) != 0) - { - T2Error("Cannot set the file position indicator for the stream pointed to by stream\n"); - } - is_rotated_log = 1; - } - } - } - } - } - } - if(NULL != pcurrentLogFile) - { - rval = fgets(buf, buflen, pcurrentLogFile); - if(NULL == rval) - { - long seek_value = ftell(pcurrentLogFile); - LAST_SEEK_VALUE = seek_value; - - fclose(pcurrentLogFile); - pcurrentLogFile = NULL; - - if(is_rotated_log == 1) - { - char *curLog = NULL; - is_rotated_log = 0; - curLog = malloc(logname_len); - if(NULL != curLog) - { - snprintf(curLog, logname_len, "%s%s", logpath, name); - pcurrentLogFile = fopen(curLog, "rb"); - - if(NULL == pcurrentLogFile) - { - T2Debug("Error in opening file %s", curLog); - LAST_SEEK_VALUE = 0; - free(curLog); - return NULL; - } - - free(curLog); - rval = fgets(buf, buflen, pcurrentLogFile); - if(NULL == rval) - { - seek_value = ftell(pcurrentLogFile); - - LAST_SEEK_VALUE = seek_value; - fclose(pcurrentLogFile); - pcurrentLogFile = NULL; - } - } - } - } - } - return rval; -} /** * @brief Function to update the global paths like PERSISTENT_PATH,LOG_PATH from include.properties file. * * @param[in] logpath Log file path * @param[in] perspath Persistent path */ -void updateIncludeConfVal(char *logpath, char *perspath) +void updateIncludeConfVal(char **logpath, char **perspath) { T2Debug("%s ++in \n", __FUNCTION__); @@ -790,55 +239,29 @@ void updateIncludeConfVal(char *logpath, char *perspath) } fclose(file); } - - - if(NULL != logpath && strcmp(logpath, "") != 0) + if(LOG_PATH == NULL) { - char *tmp = NULL; - int logpath_len = strlen(logpath) + 1; - tmp = realloc(LOG_PATH, logpath_len); - if(NULL != tmp) - { - LOG_PATH = tmp; - strncpy(LOG_PATH, logpath, logpath_len); - } - else - { - free(LOG_PATH); - LOG_PATH = NULL; - } + LOG_PATH = DEFAULT_LOG_PATH; } - - if(NULL != perspath && strcmp(perspath, "") != 0) + if (perspath) { - char *tmp = NULL; - int perspath_len = strlen(perspath) + 1; - tmp = realloc(PERSISTENT_PATH, perspath_len); - if(NULL != tmp) - { - PERSISTENT_PATH = tmp; - strncpy(PERSISTENT_PATH, perspath, perspath_len); - } - else - { - free(PERSISTENT_PATH); - PERSISTENT_PATH = NULL; - } + *perspath = PERSISTENT_PATH; + } + if (logpath) + { + *logpath = LOG_PATH; } - T2Debug("%s --out \n", __FUNCTION__); - } + /** * @brief Function to update the configuration values from device.properties file. * * @param[in] logpath Log file path * @param[in] perspath Persistent path */ -void initProperties(char *logpath, char *perspath) +void initProperties(char **logpath, char **perspath, long* pagesize) { - - T2Debug("%s ++in \n", __FUNCTION__); FILE *file = NULL; @@ -866,93 +289,7 @@ void initProperties(char *logpath, char *perspath) updateIncludeConfVal(logpath, perspath); - if(NULL != DEVICE_TYPE && NULL != PERSISTENT_PATH && NULL != LOG_PATH) - { - int logpath_len = strlen(LOG_PATH); - int perspath_len = strlen(PERSISTENT_PATH); - if(0 == strcmp("broadband", DEVICE_TYPE)) // Update config for broadband - { - char *tmp_seek_file = "/.telemetry/tmp/rtl_"; - char *tmp_log_file = "/"; - char *tmp = NULL; - int tmp_seek_len = strlen(tmp_seek_file) + 1; - int tmp_log_len = strlen(tmp_log_file) + 1; - if(NULL == perspath || strcmp(perspath, "") == 0) - { - tmp = realloc(PERSISTENT_PATH, perspath_len + tmp_seek_len); - if(NULL != tmp) - { - PERSISTENT_PATH = tmp; - strncat(PERSISTENT_PATH, tmp_seek_file, tmp_seek_len); - } - else - { - free(PERSISTENT_PATH); - PERSISTENT_PATH = NULL; - } - } - - if(NULL == logpath || strcmp(logpath, "") == 0) - { - tmp = realloc(LOG_PATH, logpath_len + tmp_log_len);; - if(NULL != tmp) - { - LOG_PATH = tmp; - strncat(LOG_PATH, tmp_log_file, tmp_log_len); - } - else - { - free(LOG_PATH); - LOG_PATH = NULL; - } - } - } - else - { - /* FIXME */ - char *tmp_seek_file = DEFAULT_SEEK_PREFIX; - char *tmp_log_file = DEFAULT_LOG_PATH; - char *tmp = NULL; - int tmp_seek_len = strlen(tmp_seek_file) + 1; - int tmp_log_len = strlen(tmp_log_file) + 1; - if(NULL == perspath || strcmp(perspath, "") == 0) - { - tmp = realloc(PERSISTENT_PATH, tmp_seek_len); - if(NULL != tmp) - { - PERSISTENT_PATH = tmp; - strncpy(PERSISTENT_PATH, tmp_seek_file, tmp_seek_len); - } - else - { - free(PERSISTENT_PATH); - PERSISTENT_PATH = NULL; - } - } - - if(NULL == logpath || strcmp(logpath, "") == 0) - { - tmp = realloc(LOG_PATH, tmp_log_len); - if(NULL != tmp) - { - LOG_PATH = tmp; - strncpy(LOG_PATH, tmp_log_file, tmp_log_len); - } - else - { - free(LOG_PATH); - LOG_PATH = NULL; - } - } - } - } + *pagesize = sysconf(_SC_PAGESIZE); - isPropsIntialized = true ; T2Debug("%s --out \n", __FUNCTION__); } - -bool isPropsInitialized() -{ - - return isPropsIntialized ; -} \ No newline at end of file diff --git a/source/dcautil/legacyutils.h b/source/dcautil/legacyutils.h index fcd3ab3d..9052a38c 100644 --- a/source/dcautil/legacyutils.h +++ b/source/dcautil/legacyutils.h @@ -29,6 +29,8 @@ * @{ **/ +#ifndef SRC_LEGACY_H_ +#define SRC_LEGACY_H_ #include #include #include @@ -59,26 +61,26 @@ #define DEFAULT_LOG_PATH "/opt/logs/" #endif +/* + * Used to store the log file seek position for each profile. + * The key is the profile name and + * value is a hash_map_t which contains the log file name as key and the seek position as value. + */ typedef struct _GrepSeekProfile { hash_map_t *logFileSeekMap; int execCounter; } GrepSeekProfile; + +/** If this can access the Profile object, then seek map can be updated accordingly */ + extern cJSON *SEARCH_RESULT_JSON; extern cJSON *ROOT_JSON; /* utility functions */ int getLoadAvg(Vector* grepResultList, bool trim, char* regex); -void removeProfileFromSeekMap(char *profileName); - -void removeProfileFromExecMap(char *profileName); - -GrepSeekProfile *addToProfileSeekMap(char* profileName); - -GrepSeekProfile *getLogSeekMapForProfile(char* profileName); - /** * Get log line from log file including the rotated log file if applicable */ @@ -88,11 +90,8 @@ void clearConfVal(void); void updatePropsFromIncludeFile(char *logpath, char *perspath); -void initProperties(char *logpath, char *perspath); +void initProperties(char **logpath, char **perspath, long *pagesize); -T2ERROR updateLogSeek(hash_map_t *logSeekMap, char *name); - -void updateLastSeekval(hash_map_t *logSeekMap, char **prev_file, char* filename); /* JSON functions */ void initSearchResultJson(cJSON **root, cJSON **sr); @@ -101,10 +100,13 @@ void addToSearchResult(char *key, char *value); void clearSearchResultJson(cJSON **root); -int getProcUsage(char *processName, Vector* grepResultList, bool trim, char* regex); +int getProcUsage(char *processName, Vector* grepResultList, bool trim, char* regex, char* filename); + +void freeGrepSeekProfile(GrepSeekProfile *gsProfile); -bool isPropsInitialized(); +GrepSeekProfile *createGrepSeekProfile(int execCounter); +#endif /* SRC_LEGACY_H_ */ /** @} */ /** @} */ diff --git a/source/dcautil/rdk_linkedlist.c b/source/dcautil/rdk_linkedlist.c deleted file mode 100644 index 3636cc35..00000000 --- a/source/dcautil/rdk_linkedlist.c +++ /dev/null @@ -1,373 +0,0 @@ -/* - * If not stated otherwise in this file or this component's Licenses.txt file the - * following copyright and licenses apply: - * - * Copyright 2019 RDK Management - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -*/ - -#include -#include - -#include "rdk_linkedlist.h" - -static void insertNode(rdkList_t* new, rdkList_t* tail) -{ - new->m_pForward = NULL; - new->m_pBackward = NULL; - if (tail) - { - tail->m_pForward = new; - new->m_pBackward = tail; - } -} - -static void removeNode(rdkList_t* node) -{ - if(node->m_pForward == NULL && node->m_pBackward == NULL) - { - return; - } - else if(node->m_pForward == NULL) - { - node->m_pBackward->m_pForward = NULL; - } - else if(node->m_pBackward == NULL) - { - node->m_pForward->m_pBackward = NULL; - } - else - { - node->m_pBackward->m_pForward = node->m_pForward; - node->m_pForward->m_pBackward = node->m_pBackward; - } - node->m_pForward = NULL; - node->m_pBackward = NULL; -} - -rdkList_t* rdk_list_add_node (rdkList_t* rdkListHndl, void* pData) -{ - rdkList_t* tmpHndl; - tmpHndl = (rdkList_t*) malloc (sizeof(rdkList_t)); - rdkList_t* tail = NULL; - if (!tmpHndl) - { - printf ("Failed to allocate memory\n"); - } - else - { - tmpHndl->m_pUserData = pData; - } - - if (rdkListHndl) - { - tail = rdkListHndl; - while (tail->m_pForward) - { - tail = tail->m_pForward; - } - } - - /* Add to List */ - insertNode(tmpHndl, tail); - - if (!rdkListHndl) - { - rdkListHndl = tmpHndl; - } - - return rdkListHndl; -} - -rdkList_t* rdk_list_prepend_node (rdkList_t* rdkListHndl, void* pData) -{ - rdkList_t* tmpHndl; - tmpHndl = (rdkList_t*) malloc (sizeof(rdkList_t)); - if (!tmpHndl) - { - printf ("Failed to allocate memory\n"); - return rdkListHndl; - } - else - { - tmpHndl->m_pUserData = pData; - tmpHndl->m_pForward = NULL; - tmpHndl->m_pBackward = NULL; - } - - if (rdkListHndl) - { - tmpHndl->m_pForward = rdkListHndl; - tmpHndl->m_pBackward = rdkListHndl->m_pBackward; - if (rdkListHndl->m_pBackward) - { - rdkListHndl->m_pBackward->m_pForward = tmpHndl; - } - rdkListHndl->m_pBackward = tmpHndl; - } - - return tmpHndl; -} - -rdkList_t* rdk_list_add_node_before(rdkList_t* rdkListHndl, rdkList_t* sibling, void* pData) -{ - rdkList_t* tmpHndl; - tmpHndl = (rdkList_t*) malloc (sizeof(rdkList_t)); - if (!tmpHndl) - { - printf ("Failed to allocate memory\n"); - return rdkListHndl; - } - else - { - tmpHndl->m_pUserData = pData; - tmpHndl->m_pForward = NULL; - tmpHndl->m_pBackward = NULL; - } - - if (!rdkListHndl) - { - rdkListHndl = tmpHndl; - if (NULL != sibling) - { - printf("rdk_list_add_node_before(): sibling not NULL for an empty list\n"); - } - } - else if (sibling) - { - tmpHndl->m_pForward = sibling; - tmpHndl->m_pBackward = sibling->m_pBackward; - if (sibling->m_pBackward) - { - sibling->m_pBackward->m_pForward = tmpHndl; - } - sibling->m_pBackward = tmpHndl; - - if (!tmpHndl->m_pBackward) - { - rdkListHndl = tmpHndl; - } - } - else - { - rdkList_t* tail = rdkListHndl; - while (tail->m_pForward) - { - tail = tail->m_pForward; - } - insertNode(tmpHndl, tail); - } - - return rdkListHndl; -} - -void rdk_list_delete_node (rdkList_t* rdkListHndl) -{ - if (rdkListHndl) - { - removeNode(rdkListHndl); - free (rdkListHndl); - } - else - { - printf("rdk_list_delete_node(): invalid pointer provided\n"); - } -} - -rdkList_t* rdk_list_remove_node (rdkList_t* rdkListHndl, rdkList_t* node) -{ - if (!rdkListHndl) - { - return rdkListHndl; - } - - removeNode(node); - - if (node == rdkListHndl) - { - rdkListHndl = rdkListHndl->m_pForward; - } - - return rdkListHndl; -} - -rdkList_t* rdk_list_remove (rdkList_t* rdkListHndl, const void *pData) -{ - rdkList_t* tmpHndl = rdkListHndl; - - while (tmpHndl) - { - if (tmpHndl->m_pUserData != pData) - { - tmpHndl = tmpHndl->m_pForward; - } - else - { - rdkListHndl = rdk_list_remove_node(rdkListHndl, tmpHndl); - free(tmpHndl); - break; - } - } - - return rdkListHndl; -} - -rdkList_t* rdk_list_find_first_node (rdkList_t* rdkListHndl) -{ - if (rdkListHndl) - { - while (rdkListHndl->m_pBackward) - { - rdkListHndl = rdkListHndl->m_pBackward; - } - } - return rdkListHndl; -} - -rdkList_t* rdk_list_find_next_node (rdkList_t* rdkListHndl) -{ - if (rdkListHndl) - { - return rdkListHndl->m_pForward; - } - return rdkListHndl; -} - -rdkList_t* rdk_list_find_previous_node (rdkList_t* rdkListHndl) -{ - if (rdkListHndl) - { - return rdkListHndl->m_pBackward; - } - return rdkListHndl; -} - -rdkList_t* rdk_list_find_node_custom (rdkList_t* rdkListHndl, void* pData, fnRDKListCustomCompare compareFunction) -{ - if (rdkListHndl && compareFunction) - { - rdkList_t* tmpHndl = rdkListHndl; - do - { - if (0 == compareFunction(tmpHndl->m_pUserData, pData)) - { - return tmpHndl; - } - tmpHndl = tmpHndl->m_pBackward; - } - while(tmpHndl); - rdkListHndl = rdkListHndl->m_pForward; - while (rdkListHndl) - { - if (0 == compareFunction(rdkListHndl->m_pUserData, pData)) - { - return rdkListHndl; - } - rdkListHndl = rdkListHndl->m_pForward; - } - } - return NULL; -} - -void rdk_list_free_all_nodes (rdkList_t* rdkListHndl) -{ - if (rdkListHndl) - { - rdkList_t* backTmpHndl = rdkListHndl; - rdkListHndl = rdkListHndl->m_pForward; - do - { - rdkList_t* tmpHndl = backTmpHndl->m_pBackward; - removeNode(backTmpHndl); - free(backTmpHndl); - backTmpHndl = tmpHndl; - - } - while (backTmpHndl); - while (rdkListHndl) - { - rdkList_t* tmpHndl = rdkListHndl; - rdkListHndl = rdkListHndl->m_pForward; - removeNode(tmpHndl); - free (tmpHndl); - } - } -} - -void rdk_list_foreach (rdkList_t* rdkListHndl, fnRDKListCustomExecute executeFunction, void* pUserActionData) -{ - if (rdkListHndl && executeFunction) - { - rdkList_t* backTmpHndl = rdkListHndl; - do - { - rdkList_t* tmpHndl = backTmpHndl->m_pBackward; - executeFunction(backTmpHndl->m_pUserData, pUserActionData); - backTmpHndl = tmpHndl; - - } - while (backTmpHndl); - rdkListHndl = rdkListHndl->m_pForward; - while (rdkListHndl) - { - rdkList_t* tmpHndl = rdkListHndl->m_pForward; - executeFunction(rdkListHndl->m_pUserData, pUserActionData); - rdkListHndl = tmpHndl; - } - } -} - -void rdk_list_free_all_nodes_custom (rdkList_t* rdkListHndl, fnRDKListCustomFree freeFunction) -{ - if (rdkListHndl && freeFunction) - { - rdkList_t* backTmpHndl = rdkListHndl; - rdkListHndl = rdkListHndl->m_pForward; - do - { - rdkList_t* tmpHndl = backTmpHndl->m_pBackward; - freeFunction(backTmpHndl->m_pUserData); - removeNode(backTmpHndl); - free(backTmpHndl); - backTmpHndl = tmpHndl; - - } - while (backTmpHndl); - while (rdkListHndl) - { - rdkList_t* tmpHndl = rdkListHndl->m_pForward; - freeFunction(rdkListHndl->m_pUserData); - removeNode(rdkListHndl); - free (rdkListHndl); - rdkListHndl = tmpHndl; - } - } -} - -rdkList_t* rdk_list_reverse(rdkList_t* rdkListHndl) -{ - rdkList_t* prev = NULL; - rdkList_t* curr = rdkListHndl; - - while (curr) - { - rdkList_t* next = curr->m_pForward; - curr->m_pForward = prev; - curr->m_pBackward = next; - prev = curr; - curr = next; - } - - return prev; -} diff --git a/source/protocol/http/curlinterface.c b/source/protocol/http/curlinterface.c index 85544f1b..54143827 100644 --- a/source/protocol/http/curlinterface.c +++ b/source/protocol/http/curlinterface.c @@ -371,6 +371,7 @@ T2ERROR sendReportOverHTTP(char *httpUrl, char *payload, pid_t* outForkedPid) memset(waninterface, 0, sizeof(waninterface)); snprintf(waninterface, sizeof(waninterface), "%s", paramVal); } + free(paramVal); paramVal = NULL; } diff --git a/source/reportgen/reportgen.c b/source/reportgen/reportgen.c index 04fe31bc..ea8a0fa0 100644 --- a/source/reportgen/reportgen.c +++ b/source/reportgen/reportgen.c @@ -162,10 +162,12 @@ T2ERROR encodeParamResultInJSON(cJSON *valArray, Vector *paramNameList, Vector * } size_t index = 0; T2Debug("%s ++in \n", __FUNCTION__); + //printf("%ld %ld\n", Vector_Size(paramNameList), Vector_Size(paramValueList)); for(; index < Vector_Size(paramNameList); index++) { Param* param = (Param *)Vector_At(paramNameList, index); + //printf("Parameter Name : %s\n", param->name); tr181ValStruct_t **paramValues = ((profileValues *)Vector_At(paramValueList, index))->paramValues; if(param == NULL || paramValues == NULL ) { diff --git a/source/scheduler/Makefile.am b/source/scheduler/Makefile.am index 82871466..7837501b 100644 --- a/source/scheduler/Makefile.am +++ b/source/scheduler/Makefile.am @@ -32,5 +32,6 @@ libscheduler_la_CPPFLAGS = -fPIC -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/dbus-1 -I${top_srcdir}/source/ccspinterface \ -I${top_srcdir}/source/bulkdata \ -I${top_srcdir}/source/reportgen \ + -I${top_srcdir}/source/dcautil \ -I${top_srcdir}/source/utils libscheduler_la_LIBADD = ${top_builddir}/source/utils/libt2utils.la diff --git a/source/t2parser/Makefile.am b/source/t2parser/Makefile.am index d2c170a6..a7b0c4c6 100644 --- a/source/t2parser/Makefile.am +++ b/source/t2parser/Makefile.am @@ -31,6 +31,7 @@ libt2parser_la_CPPFLAGS = -fPIC -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/dbus-1. -I${top_srcdir}/source/bulkdata \ -I${top_srcdir}/source/reportgen \ -I${top_srcdir}/source/ccspinterface \ + -I${top_srcdir}/source/dcautil \ -I${top_srcdir}/source/utils libt2parser_la_DEPENDENCIES = ${top_builddir}/source/ccspinterface/libccspinterface.la ${top_builddir}/source/utils/libt2utils.la diff --git a/source/t2parser/t2parser.c b/source/t2parser/t2parser.c index 8f29bb25..a8384e62 100644 --- a/source/t2parser/t2parser.c +++ b/source/t2parser/t2parser.c @@ -244,6 +244,7 @@ static T2ERROR addParameter(Profile *profile, const char* name, const char* ref, { param->regexParam = strdup(regex); } + param->skipFreq = 0; // adding this parameter to make this work in sync with single profile tr181 parameter. Vector_PushBack(profile->paramList, param); } } @@ -353,7 +354,15 @@ static T2ERROR addParameter(Profile *profile, const char* name, const char* ref, } gMarker->skipFreq = skipFreq; gMarker->firstSeekFromEOF = firstSeekFromEOF; - Vector_PushBack(profile->gMarkerList, gMarker); + if(strncmp("top_log.txt", fileName, sizeof("top_log.txt")) == 0) + { + T2Debug("This is a TopMarker name :%s and value: %s add it to topmarker list \n", name, ref); + Vector_PushBack(profile->topMarkerList, gMarker); + } + else + { + Vector_PushBack(profile->gMarkerList, gMarker); + } #ifdef PERSIST_LOG_MON_REF profile->saveSeekConfig = true; #endif @@ -775,8 +784,12 @@ T2ERROR addParameter_marker_config(Profile* profile, cJSON *jprofileParameter, i Vector_Create(&profile->staticParamList); Vector_Create(&profile->eMarkerList); Vector_Create(&profile->gMarkerList); + Vector_Create(&profile->topMarkerList); Vector_Create(&profile->cachedReportList); + profile->grepSeekProfile = createGrepSeekProfile(0); + + char* paramtype = NULL; char* use = NULL; bool reportEmpty = false; @@ -1353,6 +1366,10 @@ T2ERROR processConfiguration(char** configData, char *profileName, char* profile profile->isSchedulerstarted = false; profile->saveSeekConfig = false; profile->checkPreviousSeek = false; + + profile->grepSeekProfile = NULL; + + if(jprofileDeleteOnTimeout) { profile->deleteonTimeout = (cJSON_IsTrue(jprofileDeleteOnTimeout) == 1); @@ -1856,8 +1873,11 @@ T2ERROR addParameterMsgpack_marker_config(Profile* profile, msgpack_object* valu Vector_Create(&profile->staticParamList); Vector_Create(&profile->eMarkerList); Vector_Create(&profile->gMarkerList); + Vector_Create(&profile->topMarkerList); Vector_Create(&profile->cachedReportList); + profile->grepSeekProfile = createGrepSeekProfile(0); + Parameter_array = msgpack_get_map_value(value_map, "Parameter"); if(Parameter_array == NULL) { @@ -2441,6 +2461,8 @@ T2ERROR processMsgPackConfiguration(msgpack_object *profiles_array_map, Profile profile->saveSeekConfig = false; profile->checkPreviousSeek = false; + profile->grepSeekProfile = NULL; + DeleteOnTimout_boolean = msgpack_get_map_value(value_map, "DeleteOnTimeout"); msgpack_print(DeleteOnTimout_boolean, msgpack_get_obj_name(DeleteOnTimeout_boolean)); MSGPACK_GET_NUMBER(DeleteOnTimout_boolean, profile->deleteonTimeout); diff --git a/source/t2parser/t2parserxconf.c b/source/t2parser/t2parserxconf.c index 4484a89d..dd44c96d 100644 --- a/source/t2parser/t2parserxconf.c +++ b/source/t2parser/t2parserxconf.c @@ -1,3 +1,4 @@ + /* * If not stated otherwise in this file or this component's LICENSE file the * following copyright and licenses apply: @@ -37,6 +38,7 @@ #define MT_EVENT_PATTERN "" #define MT_EVENT_PATTERN_LENGTH 7 #define MT_TR181PARAM_PATTERN "" +#define ALIAS_DATAMODEL "" #define MT_TR181PATAM_PATTERN_LENGTH 13 #define SPLITMARKER_SUFFIX "_split" #define ACCUMULATE_MARKER_SUFFIX "_accum" @@ -77,18 +79,7 @@ static int getScheduleInSeconds(const char* cronPattern) static T2ERROR addParameter(ProfileXConf *profile, const char* name, const char* ref, const char* fileName, int skipFreq) { - if(skipFreq == -1) - { - // T2Debug("Adding TR-181 Parameter : %s\n", ref); - Param *param = (Param *)malloc(sizeof(Param)); - memset(param, 0, sizeof(Param)); - param->name = strdup(name); - param->alias = strdup(ref); - param->regexParam = NULL; - - Vector_PushBack(profile->paramList, param); - } - else if(fileName == NULL) //Event Marker + if(fileName == NULL) //Event Marker { char *splitSuffix = NULL; char *accumulateSuffix = NULL; @@ -119,6 +110,17 @@ static T2ERROR addParameter(ProfileXConf *profile, const char* name, const char* Vector_PushBack(profile->eMarkerList, eMarker); } + else if(skipFreq == -1 || strncmp(fileName, ALIAS_DATAMODEL, 5) == 0) + { + // T2Debug("Adding TR-181 Parameter : %s\n", ref); + Param *param = (Param *)malloc(sizeof(Param)); + memset(param, 0, sizeof(Param)); + param->name = strdup(name); + param->alias = strdup(ref); + param->regexParam = NULL; + param->skipFreq = skipFreq; + Vector_PushBack(profile->paramList, param); + } else //Grep Marker { char *splitSuffix = NULL; @@ -142,8 +144,15 @@ static T2ERROR addParameter(ProfileXConf *profile, const char* name, const char* gMarker->u.count = 0; } gMarker->skipFreq = skipFreq; - - Vector_PushBack(profile->gMarkerList, gMarker); + if(strncmp("top_log.txt", fileName, sizeof("top_log.txt")) == 0) + { + T2Debug("This is a TopMarker name :%s and value: %s add it to topmarker list \n", name, ref); + Vector_PushBack(profile->topMarkerList, gMarker); + } + else + { + Vector_PushBack(profile->gMarkerList, gMarker); + } } profile->paramNumOfEntries++; return T2ERROR_SUCCESS; @@ -211,9 +220,12 @@ T2ERROR processConfigurationXConf(char* configData, ProfileXConf **localProfile) profile->t2HTTPDest->URL = strdup(juploadUrl->valuestring); profile->encodingType = strdup("JSON"); + profile->grepSeekProfile = createGrepSeekProfile(0); + Vector_Create(&profile->paramList); Vector_Create(&profile->eMarkerList); Vector_Create(&profile->gMarkerList); + Vector_Create(&profile->topMarkerList); Vector_Create(&profile->cachedReportList); #if defined(PRIVACYMODES_CONTROL) @@ -221,34 +233,34 @@ T2ERROR processConfigurationXConf(char* configData, ProfileXConf **localProfile) getPrivacyMode(¶mValue); if(strncmp(paramValue, "DO_NOT_SHARE", MAX_PARAM_LEN) == 0) { - addParameter(profile, "PrivacyMode", PRIVACYMODES_RFC, NULL, -1); + addParameter(profile, "PrivacyMode", PRIVACYMODES_RFC, ALIAS_DATAMODEL, -1); } else { - addParameter(profile, "mac", TR181_DEVICE_WAN_MAC, NULL, -1); - addParameter(profile, "StbIp", TR181_DEVICE_WAN_IPv6, NULL, -1); - isWhoAmiEnabled() ? addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_NAME, NULL, -1) : addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_ID, NULL, -1); - addParameter(profile, "Version", TR181_DEVICE_FW_VERSION, NULL, -1); - addParameter(profile, "AccountId", TR181_DEVICE_ACCOUNT_ID, NULL, -1); - addParameter(profile, "immui_ver_split", TR181_IUI_VERSION, NULL, -1); + addParameter(profile, "mac", TR181_DEVICE_WAN_MAC, ALIAS_DATAMODEL, -1); + addParameter(profile, "StbIp", TR181_DEVICE_WAN_IPv6, ALIAS_DATAMODEL, -1); + isWhoAmiEnabled() ? addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_NAME, ALIAS_DATAMODEL, -1) : addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_ID, ALIAS_DATAMODEL, -1); + addParameter(profile, "Version", TR181_DEVICE_FW_VERSION, ALIAS_DATAMODEL, -1); + addParameter(profile, "AccountId", TR181_DEVICE_ACCOUNT_ID, ALIAS_DATAMODEL, -1); + addParameter(profile, "immui_ver_split", TR181_IUI_VERSION, ALIAS_DATAMODEL, -1); } free(paramValue); paramValue = NULL; #else - addParameter(profile, "mac", TR181_DEVICE_WAN_MAC, NULL, -1); + addParameter(profile, "mac", TR181_DEVICE_WAN_MAC, ALIAS_DATAMODEL, -1); #if defined(ENABLE_RDKB_SUPPORT) - addParameter(profile, "erouterIpv4", TR181_DEVICE_WAN_IPv4, NULL, -1); - addParameter(profile, "erouterIpv6", TR181_DEVICE_WAN_IPv6, NULL, -1); + addParameter(profile, "erouterIpv4", TR181_DEVICE_WAN_IPv4, ALIAS_DATAMODEL, -1); + addParameter(profile, "erouterIpv6", TR181_DEVICE_WAN_IPv6, ALIAS_DATAMODEL, -1); #elif defined (ENABLE_RDKC_SUPPORT) - addParameter(profile, "camIp", TR181_DEVICE_WAN_IPv4, NULL, -1); - addParameter(profile, "camIpv6", TR181_DEVICE_WAN_IPv6, NULL, -1); + addParameter(profile, "camIp", TR181_DEVICE_WAN_IPv4, ALIAS_DATAMODEL, -1); + addParameter(profile, "camIpv6", TR181_DEVICE_WAN_IPv6, ALIAS_DATAMODEL, -1); #else - addParameter(profile, "StbIp", TR181_DEVICE_WAN_IPv6, NULL, -1); - addParameter(profile, "immui_ver_split", TR181_IUI_VERSION, NULL, -1); + addParameter(profile, "StbIp", TR181_DEVICE_WAN_IPv6, ALIAS_DATAMODEL, -1); + addParameter(profile, "immui_ver_split", TR181_IUI_VERSION, ALIAS_DATAMODEL, -1); #endif - isWhoAmiEnabled() ? addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_NAME, NULL, -1) : addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_ID, NULL, -1); - addParameter(profile, "Version", TR181_DEVICE_FW_VERSION, NULL, -1); - addParameter(profile, "AccountId", TR181_DEVICE_ACCOUNT_ID, NULL, -1); + isWhoAmiEnabled() ? addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_NAME, ALIAS_DATAMODEL, -1) : addParameter(profile, "PartnerId", TR181_DEVICE_PARTNER_ID, ALIAS_DATAMODEL, -1); + addParameter(profile, "Version", TR181_DEVICE_FW_VERSION, ALIAS_DATAMODEL, -1); + addParameter(profile, "AccountId", TR181_DEVICE_ACCOUNT_ID, ALIAS_DATAMODEL, -1); #endif int markerIndex = 0; char* header = NULL; @@ -292,19 +304,13 @@ T2ERROR processConfigurationXConf(char* configData, ProfileXConf **localProfile) if(header != NULL && content != NULL && logfile != NULL) { - if(skipFrequency > 0) - { - // T2Debug("Skip Frequency is Present, Need to do grep\n"); - - ret = addParameter(profile, header, content, logfile, skipFrequency); - } - else if(!strncmp(logfile, MT_TR181PARAM_PATTERN, MT_TR181PATAM_PATTERN_LENGTH)) + if(!strncmp(logfile, MT_TR181PARAM_PATTERN, MT_TR181PATAM_PATTERN_LENGTH)) { - ret = addParameter(profile, header, content, NULL, -1); + ret = addParameter(profile, header, content, ALIAS_DATAMODEL, skipFrequency); } else if(!strncmp(logfile, MT_EVENT_PATTERN, MT_EVENT_PATTERN_LENGTH)) { - ret = addParameter(profile, header, content, NULL, skipFrequency); + ret = addParameter(profile, header, content, NULL, 0); //skip freq is not supported for event markers } else { diff --git a/source/test/dcautils/Makefile.am b/source/test/dcautils/Makefile.am index f941f317..74b587ed 100644 --- a/source/test/dcautils/Makefile.am +++ b/source/test/dcautils/Makefile.am @@ -29,6 +29,6 @@ bin_PROGRAMS = dcautil_gtest.bin dcautil_gtest_bin_CPPFLAGS = -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/gtest -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/glib-2.0 -I$(PKG_CONFIG_SYSROOT_DIR)/usr/lib/x86_64-linux-gnu/glib-2.0/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/local/lib -I$(PKG_CONFIG_SYSROOT_DIR)$(includedir)/gtest -I${top_srcdir}/gtest/include -I${top_srcdir}/source/include -I${top_srcdir}/source -I${top_srcdir}/source/test/mocks -I${top_srcdir}/source/test/rbus -I${top_srcdir}/source/test/rdk_logger -I${top_srcdir}/include -I${top_srcdir}/source/utils -I${top_srcdir}/source/privacycontrol -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/rbus -I${top_srcdir}/source/dcautil -I${top_srcdir}/source/ccspinterface -I${top_srcdir}/source/reportgen -I${top_srcdir}/source/xconf-client -I${top_srcdir}/source/protocol/http -I${top_srcdir}/source/protocol/rbusMethod -I${top_srcdir}/source/t2parser -I${top_srcdir}/source/bulkdata -I${PKG_CONFIG_SYSROOT_DIR}$(includedir) -I${PKG_CONFIG_SYSROOT_DIR}/usr/src/googletest/googlemock/include -I${RDK_PROJECT_ROOT_PATH}/$(GLIB_CFLAGS) -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/glib-2.0 -I${PKG_CONFIG_SYSROOT_DIR}$(libdir)/glib-2.0/include -dcautil_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp dcautilTest.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../xconf-client/xconfclientTest.cpp ../xconf-client/xconfclientMock.cpp ../protocol/ProtocolTest.cpp ../mocks/rdkconfigMock.cpp ../utils/UtilsTest.cpp ../../utils/persistence.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/vector.c ../../utils/t2MtlsUtils.c ../../utils/t2log_wrapper.c ../../dcautil/dcautil.c ../../dcautil/dca.c ../../dcautil/dcalist.c ../../dcautil/legacyutils.c ../../dcautil/dcaproc.c ../../dcautil/dcajson.c ../../dcautil/rdk_linkedlist.c ../../xconf-client/xconfclient.c ../../protocol/http/curlinterface.c ../../protocol/rbusMethod/rbusmethodinterface.c ../../privacycontrol/rdkservices_privacyutils.c +dcautil_gtest_bin_SOURCES = gtest_main.cpp ../mocks/SystemMock.cpp ../mocks/FileioMock.cpp dcautilTest.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp ../xconf-client/xconfclientTest.cpp ../xconf-client/xconfclientMock.cpp ../protocol/ProtocolTest.cpp ../mocks/rdkconfigMock.cpp ../utils/UtilsTest.cpp ../../utils/persistence.c ../../utils/t2common.c ../../utils/t2collection.c ../../utils/vector.c ../../utils/t2MtlsUtils.c ../../utils/t2log_wrapper.c ../../dcautil/dcautil.c ../../dcautil/dca.c ../../dcautil/legacyutils.c ../../dcautil/dcaproc.c ../../xconf-client/xconfclient.c ../../protocol/http/curlinterface.c ../../protocol/rbusMethod/rbusmethodinterface.c ../../privacycontrol/rdkservices_privacyutils.c dcautil_gtest_bin_LDFLAGS = -L/usr/src/googletest/googletest/lib/.libs -lgcov -L/src/googletest/googlemock/lib -L/usr/src/googletest/googlemock/lib/.libs -L/usr/include/glib-2.0 -lgmock -lcjson -lcurl -lmsgpackc -lgtest -lgtest_main -lglib-2.0 diff --git a/source/test/dcautils/dcautilTest.cpp b/source/test/dcautils/dcautilTest.cpp index 2a431855..4f8cbb11 100644 --- a/source/test/dcautils/dcautilTest.cpp +++ b/source/test/dcautils/dcautilTest.cpp @@ -55,272 +55,30 @@ FileMock *g_fileIOMock = NULL; SystemMock * g_systemMock = NULL; rdklogMock *m_rdklogMock = NULL; -//Insert node to the list - -TEST(rdk_list_add_node, add_node_to_empty_list){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node = (rdkList_t*) malloc (sizeof(rdkList_t)); - node->m_pUserData = (void*)"test"; - node->m_pForward = NULL; - node->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node->m_pUserData); - EXPECT_EQ(node->m_pUserData, rdkListHndl->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -// Insert node to the list which is not empty -TEST(rdk_list_add_node, add_node_to_non_empty_list){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = node1; - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pForward); - EXPECT_EQ(node1->m_pForward, rdkListHndl->m_pForward); - rdk_list_free_all_nodes(rdkListHndl); -} - -//Prepend node to the list -TEST(rdk_list_prepend_node, prepend_node_to_empty_list){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node = (rdkList_t*) malloc (sizeof(rdkList_t)); - node->m_pUserData = (void*)"test"; - node->m_pForward = NULL; - node->m_pBackward = NULL; - rdkListHndl = rdk_list_prepend_node(rdkListHndl, node->m_pUserData); - EXPECT_EQ(node->m_pUserData, rdkListHndl->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -// Prepend node to the list which is not empty -TEST(rdk_list_prepend_node, prepend_node_to_non_empty_list){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_prepend_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(node2->m_pUserData, rdkListHndl->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -// Add node before sibling node -TEST(rdk_list_add_node_before, add_node_before_sibling){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node_before(rdkListHndl, node1, node2->m_pUserData); - EXPECT_EQ(node2->m_pUserData, rdkListHndl->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_add_node_before, add_node_to_empty_list_with_sibling){ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node_before(rdkListHndl, node1, node2->m_pUserData); - EXPECT_EQ(node2->m_pUserData, rdkListHndl->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_find_first_node, find_first_node) -{ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(node1->m_pUserData, rdk_list_find_first_node(rdkListHndl)->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_find_next_node, find_next_node) -{ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(NULL, rdk_list_find_next_node(rdkListHndl)->m_pForward); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_reverse, reverse_list) -{ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(node2->m_pUserData, rdk_list_reverse(rdkListHndl)->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_remove_node, remove_node) -{ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(node1->m_pUserData, rdk_list_remove_node(rdkListHndl, node1)->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -TEST(rdk_list_remove, remove_list) -{ - rdkList_t *rdkListHndl = NULL; - rdkList_t *node1 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node1->m_pUserData = (void*)"test1"; - node1->m_pForward = NULL; - node1->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node1->m_pUserData); - rdkList_t *node2 = (rdkList_t*) malloc (sizeof(rdkList_t)); - node2->m_pUserData = (void*)"test2"; - node2->m_pForward = NULL; - node2->m_pBackward = NULL; - rdkListHndl = rdk_list_add_node(rdkListHndl, node2->m_pUserData); - EXPECT_EQ(node1->m_pUserData, rdk_list_remove(rdkListHndl, node2->m_pUserData)->m_pUserData); - rdk_list_free_all_nodes(rdkListHndl); -} - -//dcalist.c - -rdkList_t *pch = NULL; - -TEST(insertPCNode, pattern_is_NULL) //when pattern is NULL -{ - EXPECT_EQ(0, insertPCNode(&pch, NULL, "SYS_INFO", STR, 0, "SYS_INFO_DATA", true, NULL)); -} - -TEST(insertPCNode, head_is_NULL) //when head is NULL -{ - EXPECT_EQ(0, insertPCNode(&pch, "info is", NULL, STR, 0, "SYS_INFO_DATA", true, NULL)); -} - -//When data is NULL -TEST(insertPCNode, data_is_NULL) -{ - EXPECT_EQ(0, insertPCNode(&pch, "info is", "SYS_INFO", STR, 0, NULL, false, NULL)); -} -//When pattern, head and data are NULL -TEST(insertPCNode, all_NULL) -{ - EXPECT_EQ(0, insertPCNode(&pch, NULL, NULL, STR, 0, NULL, false, NULL)); -} - -TEST(insertPCNode, regex_not_NULL) -{ - EXPECT_EQ(0, insertPCNode(&pch, NULL, NULL, STR, 0, NULL, false, "[0-9]+")); - EXPECT_EQ(0, insertPCNode(&pch, NULL, NULL, STR, 0, NULL, true, "[0-9]*")); -} - -TEST(COMPAREPATTERN, NP_SP_NULL_CHECK) -{ - pcdata_t *sp = (pcdata_t*)"SEARCH_DATA_LIST"; - pcdata_t *np = (pcdata_t*)"POINTER TO BE SEARCHED"; - EXPECT_EQ(-1, comparePattern(NULL, (gpointer)sp)); - EXPECT_EQ(-1, comparePattern(np, NULL)); -} - -TEST(searchPCNode, list_pattern_NULL) -{ - char* pattern = "Memory Usage is"; - rdkList_t *node1 = NULL; - EXPECT_EQ(NULL, searchPCNode(node1, pattern)); - rdkList_t *node = (rdkList_t*) malloc (sizeof(rdkList_t)); - node->m_pUserData = (void*)"test1"; - node->m_pForward = NULL; - node->m_pBackward = NULL; - EXPECT_EQ(NULL, searchPCNode(node, NULL)); - rdk_list_free_all_nodes(node); -} - -//dcajson.c - -TEST(addtosearchResult, searchresult_NULL) -{ - char* value = "Memory Usage is"; - char* key = "SYS_MEM_INFO"; - addToSearchResult(NULL, NULL); - addToSearchResult(NULL, value); - addToSearchResult(key, NULL); -} - -TEST(clearSearchResultJson, check_json) -{ - cJSON* json = cJSON_CreateObject(); - clearSearchResultJson(&json); -} - //dcaproc.c -TEST(GETPROCUSAGE, GREPRESULTLIST_PROCESS_NULL) +TEST(GETPROCUSAGE, GREPRESULTLIST_NULL) { - EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, false, NULL)); - EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, true, NULL)); - EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, false, "[0-9]")); - EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, true, "[0-9]")); + EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, false, NULL, NULL)); + EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, true, NULL, NULL)); + EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, false, "[0-9]", NULL)); + EXPECT_EQ(-1, getProcUsage("telemetry2_0", NULL, true, "[0-9]", NULL)); } TEST(GETPROCUSAGE, PROCESS_NULL) { Vector* grepResultList = NULL; + char* filename = NULL; + filename = strdup("top_log.txt"); Vector_Create(&grepResultList); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_BOOTUP")); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_MEM")); - EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, false, NULL)); - EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, true, NULL)); - EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, false, "[0-9]")); - EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, true, "[0-9]")); + EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, false, NULL, filename)); + EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, true, NULL, NULL)); + EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, false, "[0-9]", NULL)); + EXPECT_EQ(-1, getProcUsage(NULL, grepResultList, true, "[0-9]", filename)); Vector_Destroy(grepResultList, free); + free(filename); } TEST(GETPROCPIDSTAT, PINFO_NULL) @@ -330,7 +88,20 @@ TEST(GETPROCPIDSTAT, PINFO_NULL) TEST(GETPROCINFO, PMINFO_NULL) { - EXPECT_EQ(0, getProcInfo(NULL)); + char* filename = NULL; + char* processName = NULL; + processName = strdup("telemetry2_0"); + EXPECT_EQ(0, getProcInfo(NULL, filename)); + + filename = strdup("top_log.txt"); + EXPECT_EQ(0, getProcInfo(NULL, filename)); + + procMemCpuInfo pInfo; + memset(&pInfo, '\0', sizeof(procMemCpuInfo)); + memcpy(pInfo.processName, processName, strlen(processName) + 1); + pInfo.total_instance = 0; + EXPECT_EQ(0,getProcInfo(&pInfo, NULL)); + free(filename); } TEST(GETMEMINFO, PMINFO_NULL) @@ -340,7 +111,12 @@ TEST(GETMEMINFO, PMINFO_NULL) TEST(GETCPUINFO, PINFO_NULL) { - EXPECT_EQ(0, getCPUInfo(NULL)); + char* filename = NULL; + EXPECT_EQ(0, getCPUInfo(NULL, filename)); + filename = strdup("top_log.txt"); + EXPECT_EQ(0, getCPUInfo(NULL, filename)); + + } //dcautil.c char* gmarker1 = "SYS_INFO_BOOTUP"; @@ -355,14 +131,23 @@ TEST(GETGREPRESULTS, PROFILENAME_NULL) Vector_PushBack(markerlist, (void*) strdup(dcamarker2)); Vector* grepResultlist = NULL; Vector_Create(&grepResultlist); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + hash_map_put(gsProfile->logFileSeekMap, strdup("t2_log.txt"), (void*)1, free); EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(NULL, markerlist, &grepResultlist, false, false,"/opt/logs")); - EXPECT_EQ(T2ERROR_FAILURE, getGrepResults("RDKB_Profile1", NULL, &grepResultlist, false, false,"/opt/logs")); - EXPECT_EQ(T2ERROR_FAILURE, getGrepResults("RDKB_Profile1", markerlist, NULL, false, false,"/opt/logs")); + EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(&gsProfile, NULL, &grepResultlist, false, false,"/opt/logs")); + EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(&gsProfile, markerlist, NULL, false, false,"/opt/logs")); EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(NULL, markerlist, &grepResultlist, false, true,"/opt/logs")); - EXPECT_EQ(T2ERROR_FAILURE, getGrepResults("RDKB_Profile1", NULL, &grepResultlist, false, true,"/opt/logs")); - EXPECT_EQ(T2ERROR_FAILURE, getGrepResults("RDKB_Profile1", markerlist, NULL, false, true,"/opt/logs")); + EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(&gsProfile, NULL, &grepResultlist, false, true,"/opt/logs")); + EXPECT_EQ(T2ERROR_FAILURE, getGrepResults(&gsProfile, markerlist, NULL, false, true,"/opt/logs")); Vector_Destroy(markerlist, free); Vector_Destroy(grepResultlist, free); + if(gsProfile->logFileSeekMap) + { + hash_map_destroy(gsProfile->logFileSeekMap, free); + } + free(gsProfile); grepResultlist = NULL; markerlist = NULL; } @@ -370,80 +155,32 @@ TEST(GETGREPRESULTS, PROFILENAME_NULL) #ifdef PERSIST_LOG_MON_REF TEST(saveSeekConfigtoFile, profilename_NULL) { - EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile(NULL)); -} -TEST(saveSeekConfigtoFile, profilename_NOT_NULL) -{ - EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile("RDKB_Profile1")); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + hash_map_put(gsProfile->logFileSeekMap, strdup("t2_log.txt"), (void*)1, free); + EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile(NULL, gsProfile)); + EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile("RDKB_Profile1", NULL)); + if(gsProfile->logFileSeekMap) + { + hash_map_destroy(gsProfile->logFileSeekMap, free); + gsProfile->logFileSeekMap = NULL; + } + EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile("RDKB_Profile1", gsProfile)); } TEST(loadSavedSeekConfig, profilename_NULL) { - EXPECT_EQ(T2ERROR_FAILURE, loadSavedSeekConfig(NULL)); -} -TEST(loadSavedSeekConfig, profilename_NOT_NULL) -{ - EXPECT_EQ(T2ERROR_FAILURE, loadSavedSeekConfig("RDKB_Profile1")); -} -#endif - -//legacyutils.c - -TEST(ADDTOPROFILESEEK, PROFILENM_NULL) -{ - EXPECT_EQ(NULL, addToProfileSeekMap(NULL)); -} - -TEST(ADDTOPROFILESEEK, PROFILENM_INVALID) -{ - GrepSeekProfile* gp = NULL; - EXPECT_NE(gp, addToProfileSeekMap("RDKB_INVALID_PROFILE1")); -} - -TEST(GETLOGSEEKMAP, PROFILENM_NULL) -{ - EXPECT_EQ(NULL, getLogSeekMapForProfile(NULL)); -} - -TEST(GETLOGSEEKMAP, PROFILENM_INVALID) -{ - GrepSeekProfile *gp = NULL; - EXPECT_NE(gp, getLogSeekMapForProfile("RDKB_INVALID_PROFILE1")); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + EXPECT_EQ(T2ERROR_FAILURE, loadSavedSeekConfig(NULL, gsProfile)); + hash_map_destroy(gsProfile->logFileSeekMap, free); + free(gsProfile); } -TEST(UPDATELOGSEEK, LOGSEEKMAP_NULL) -{ - char* logFileName = "t2_log.txt"; - EXPECT_EQ(T2ERROR_FAILURE, updateLogSeek(NULL, logFileName)); -} - -TEST(UPDATELOGSEEK, LOGSEEKMAP_NOT_NULL) -{ - char* logFileName = "t2_log.txt"; - hash_map_t* logseekmap = (hash_map_t *)malloc(sizeof(hash_map_t)); - logseekmap = hash_map_create(); - int *temp = (int *) malloc(sizeof(int)); - *temp = 1; - hash_map_put(logseekmap, strdup(logFileName), (void*)temp, free); - *temp = 2; - hash_map_put(logseekmap, strdup("t2_log.txt.0"), (void*)temp, free); - *temp = 3; - hash_map_put(logseekmap, strdup("core_log.txt"), (void*)temp, free); - EXPECT_EQ(T2ERROR_FAILURE, updateLogSeek(logseekmap, NULL)); - EXPECT_EQ(T2ERROR_SUCCESS, updateLogSeek(logseekmap, logFileName)); - hash_map_destroy(logseekmap, free); - logseekmap = NULL; -} +#endif -TEST(GETLOGSEEKMAPFORPROFILE, PROFILE_NULL) -{ - EXPECT_EQ(NULL, getLogSeekMapForProfile(NULL)); -} -TEST(GETLOGSEEKMAPFORPROFILE, PROFILE_NOT_NULL) -{ - GrepSeekProfile *gp = NULL; - EXPECT_EQ(gp, getLogSeekMapForProfile("RDKB_PROFILE")); -} TEST(GETLOADAVG, VECTOR_REGEX_NULL) { @@ -453,93 +190,70 @@ TEST(GETLOADAVG, VECTOR_REGEX_NULL) EXPECT_EQ(0, getLoadAvg(NULL, true, "[0-9]")); } -TEST(GETLOGLINE, NULL_CASES) +TEST(CREATEGREPSEEKPROFILE, SEEKMAPCREATE_CHECK) { - char* logFileName = "t2_log.txt"; - hash_map_t* logseekmap = (hash_map_t *)malloc(sizeof(hash_map_t)); - logseekmap = hash_map_create(); - char *buf = (char*) malloc(512); - char* name = "core_log.txt"; - int *temp = (int *) malloc(sizeof(int)); - *temp = 1; - hash_map_put(logseekmap, strdup(logFileName), (void*)temp, free); - *temp = 2; - hash_map_put(logseekmap, strdup("t2_log.txt.0"), (void*)temp, free); - *temp = 3; - hash_map_put(logseekmap, strdup("core_log.txt"), (void*)temp, free); - int seekFromEOF = 0; - EXPECT_EQ(NULL, getLogLine(NULL, buf, 512, name, &seekFromEOF, false)); - EXPECT_EQ(NULL, getLogLine(logseekmap, NULL, 512, name, &seekFromEOF, false)); - EXPECT_EQ(NULL, getLogLine(logseekmap, buf, 512, NULL, &seekFromEOF, false)); - EXPECT_EQ(NULL, getLogLine(NULL, buf, 512, name, &seekFromEOF, true)); - EXPECT_EQ(NULL, getLogLine(logseekmap, NULL, 512, name, &seekFromEOF, true)); - EXPECT_EQ(NULL, getLogLine(logseekmap, buf, 512, NULL, &seekFromEOF, true)); - - free(buf); - buf = NULL; - hash_map_destroy(logseekmap, free); - logseekmap = NULL; + GrepSeekProfile *gsProfile = createGrepSeekProfile(0); + EXPECT_NE(gsProfile, nullptr); + EXPECT_NE(gsProfile->logFileSeekMap, nullptr); + EXPECT_EQ(gsProfile->execCounter, 0); + hash_map_destroy(gsProfile->logFileSeekMap, free); + free(gsProfile); } -TEST(ISPROPSINITIALIZED, TESTING) +TEST(FREEFREPSEEKPROFILE, SEEKMAPFREE_CHECK) { - EXPECT_EQ(false, isPropsInitialized()); + GrepSeekProfile *gsProfile = createGrepSeekProfile(0); + EXPECT_NE(gsProfile, nullptr); + EXPECT_NE(gsProfile->logFileSeekMap, nullptr); + EXPECT_EQ(gsProfile->execCounter, 0); + + freeGrepSeekProfile(gsProfile); + + // Check if the profile is freed correctly + EXPECT_NE(gsProfile, nullptr); } + //dca.c TEST(PROCESSTOPPATTERN, VECTOR_NULL) { - rdkList_t *rdkListHndl = NULL; - rdkList_t *tlist = (rdkList_t*) malloc (sizeof(rdkList_t)); - tlist->m_pUserData = (void*)"test1"; - tlist->m_pForward = NULL; - tlist->m_pBackward = NULL; - rdk_list_add_node(rdkListHndl, tlist->m_pUserData); - Vector* grepResultlist = NULL; - Vector_Create(&grepResultlist); - Vector_PushBack(grepResultlist, (void*) strdup("SYS_INFO_BOOTUP")); - Vector_PushBack(grepResultlist, (void*) strdup("SYS_INFO_MEM")); - EXPECT_EQ(-1, processTopPattern(NULL, grepResultlist)); - EXPECT_EQ(-1, processTopPattern(rdkListHndl, NULL)); - EXPECT_EQ(-1, processTopPattern(rdkListHndl, grepResultlist)); - rdk_list_free_all_nodes(rdkListHndl); - Vector_Destroy(grepResultlist, free); - grepResultlist = NULL; -} - -TEST(GETERRORCODE, STR_NOT_NULL) -{ - char* str = "telemetry-dcautil"; - EXPECT_EQ(0, getErrorCode(str, NULL)); -} - -TEST(GETERRORCODE, STR_NULL) -{ - EXPECT_EQ(-1, getErrorCode(NULL, NULL)); -} - -TEST(strSplit, STR_NULL) -{ - EXPECT_EQ(NULL, strSplit(NULL, "<#>")); -} - -TEST(getDCAResultsInJson, markerlist_NULL) -{ - char* profileName = "Profile1"; - cJSON* grepResultList = NULL; - EXPECT_EQ(-1, getDCAResultsInJson(profileName, NULL, &grepResultList)); -} - + Vector* topMarkerlist = NULL; + Vector_Create(&topMarkerlist); + Vector_PushBack(topMarkerlist, (void*) strdup("cpu_telemetry2_0")); + Vector_PushBack(topMarkerlist, (void*) strdup("mem_telemetry2_0")); + Vector* outgrepResultlist = NULL; + Vector_Create(&outgrepResultlist); + EXPECT_EQ(-1, processTopPattern("RDK_Profile", topMarkerlist, NULL, 1)); + EXPECT_EQ(-1, processTopPattern(NULL, topMarkerlist, outgrepResultlist, 1)); + EXPECT_EQ(-1, processTopPattern("RDK_Profile",NULL, outgrepResultlist, 1)); + Vector_Destroy(topMarkerlist, free); + Vector_Destroy(outgrepResultlist, free); + topMarkerlist = NULL; + outgrepResultlist = NULL; +} + +//int getDCAResultsInVector(GrepSeekProfile *gSeekProfile, Vector * vecMarkerList, Vector** out_grepResultList, bool check_rotated, char* customLogPath) TEST(getDCAResultsInVector, markerlist_NULL) { - char* profileName = "Profile1"; - Vector* grepResultList = NULL; + + Vector* out_grepResultList = NULL; + Vector_Create(&out_grepResultList); Vector* markerlist = NULL; Vector_Create(&markerlist); Vector_PushBack(markerlist, (void*) strdup("SYS_INFO_BOOTUP")); - EXPECT_EQ(-1, getDCAResultsInVector(profileName, NULL, &grepResultList, true, "/opt/logs")); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + hash_map_put(gsProfile->logFileSeekMap, strdup("t2_log.txt"), (void*)1, free); + EXPECT_EQ(-1, getDCAResultsInVector(NULL, markerlist, &out_grepResultList, true, "/opt/logs/core_log.txt")); + EXPECT_EQ(-1, getDCAResultsInVector(gsProfile, NULL, &out_grepResultList, true, "/opt/logs/core_log.txt")); + EXPECT_EQ(-1, getDCAResultsInVector(gsProfile, markerlist, NULL, true, "/opt/logs/core_log.txt")); + hash_map_destroy(gsProfile->logFileSeekMap, free); + Vector_Destroy(markerlist, free); + Vector_Destroy(out_grepResultList, free); } + class dcaTestFixture : public ::testing::Test { protected: void SetUp() override @@ -589,32 +303,37 @@ TEST_F(dcaTestFixture, dcaFlagReportCompleation1) dcaFlagReportCompleation(); } -#ifdef PERSIST_LOG_MON_RE +#ifdef PERSIST_LOG_MON_REF TEST_F(dcaTestFixture, loadSavedSeekConfig) { + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; FILE* fp = (FILE*)NULL; EXPECT_CALL(*g_fileIOMock, fopen(_,_)) .Times(1) .WillOnce(Return(fp)); - EXPECT_EQ(T2ERROR_FAILURE, loadSavedSeekConfig("RDK_Profile")); - + EXPECT_EQ(T2ERROR_FAILURE, loadSavedSeekConfig("RDK_Profile", gsProfile)); + hash_map_destroy(gsProfile->logFileSeekMap, free); + free(gsProfile); } TEST_F(dcaTestFixture, loadSavedSeekConfig1) { + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; FILE* fp = (FILE*)0xffffffff; EXPECT_CALL(*g_fileIOMock, fopen(_,_)) .Times(1) .WillOnce(Return(fp)); EXPECT_CALL(*g_fileIOMock, fseek(_,_,_)) - .Times(1) + .Times(2) + .WillOnce(Return(0)) .WillOnce(Return(0)); EXPECT_CALL(*g_fileIOMock, ftell(_)) .Times(1) .WillOnce(Return(0)); - EXPECT_CALL(*g_fileIOMock, fseek(_,_,_)) - .Times(1) - .WillOnce(Return(0)); EXPECT_CALL(*g_fileIOMock, fread(_,_,_,_)) .Times(1) .WillOnce(Return(0)); @@ -622,14 +341,38 @@ TEST_F(dcaTestFixture, loadSavedSeekConfig1) .Times(1) .WillOnce(Return(0)); - EXPECT_EQ(T2ERROR_SUCCESS, loadSavedSeekConfig("RDK_Profile")); + EXPECT_EQ(T2ERROR_SUCCESS, loadSavedSeekConfig("RDK_Profile", gsProfile)); + hash_map_destroy(gsProfile->logFileSeekMap,free); + free(gsProfile); +} + +TEST_F(dcaTestFixture, saveSeekConfigtoFile) +{ + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + long *tempnum; + double val = 123456; + tempnum = (long *)malloc(sizeof(long)); + *tempnum = (long)val; + hash_map_put(gsProfile->logFileSeekMap, strdup("t2_log.txt"), (void*)tempnum, free); + FILE* fp = (FILE*)NULL; + EXPECT_CALL(*g_fileIOMock, fopen(_,_)) + .Times(1) + .WillOnce(Return(fp)); + EXPECT_EQ(T2ERROR_FAILURE, saveSeekConfigtoFile("RDK_Profile", gsProfile)); + hash_map_destroy(gsProfile->logFileSeekMap, free); + gsProfile->logFileSeekMap = NULL; + free(gsProfile); } #endif -//dcautil.c +//dcaproc.c TEST_F(dcaTestFixture, getProcUsage) { Vector* grepResultList = NULL; + char* filename = NULL; + filename = strdup("top_log.txt"); Vector_Create(&grepResultList); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_BOOTUP")); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_MEM")); @@ -643,8 +386,10 @@ TEST_F(dcaTestFixture, getProcUsage) .Times(1) .WillOnce(Return(fp)); #endif - EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]")); -} + EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]", filename)); + Vector_Destroy(grepResultList, free); + free(filename); +} TEST_F(dcaTestFixture, getProcUsage1) { @@ -653,6 +398,8 @@ TEST_F(dcaTestFixture, getProcUsage1) Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_BOOTUP")); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_MEM")); FILE* fp = (FILE*)0xffffffff; + char* filename = NULL; + filename = strdup("top_log.txt"); #ifdef LIBSYSWRAPPER_BUILD EXPECT_CALL(*g_fileIOMock, v_secure_popen(_,_)) .Times(1) @@ -674,7 +421,9 @@ TEST_F(dcaTestFixture, getProcUsage1) .Times(1) .WillOnce(Return(-1)); #endif - EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]")); + EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]", filename)); + Vector_Destroy(grepResultList, free); + free(filename); } TEST_F(dcaTestFixture, getProcUsage2) @@ -684,6 +433,8 @@ TEST_F(dcaTestFixture, getProcUsage2) Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_BOOTUP")); Vector_PushBack(grepResultList, (void*) strdup("SYS_INFO_MEM")); FILE* fp = (FILE*)0xffffffff; + char* filename = NULL; + filename = strdup("top_log.txt"); #ifdef LIBSYSWRAPPER_BUILD EXPECT_CALL(*g_fileIOMock, v_secure_popen(_,_)) .Times(1) @@ -708,7 +459,9 @@ TEST_F(dcaTestFixture, getProcUsage2) .Times(1) .WillOnce(Return(-1)); #endif - EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]")); + EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]",filename)); + Vector_Destroy(grepResultList, free); + free(filename); } TEST_F(dcaTestFixture, getProcPidStat) @@ -743,7 +496,20 @@ TEST_F(dcaTestFixture, saveTopOutput) EXPECT_CALL(*g_systemMock, access(_,_)) .Times(1) .WillOnce(Return(0)); - saveTopOutput(); + #ifdef LIBSYSWRAPPER_BUILD + EXPECT_CALL(*g_systemMock, v_secure_system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(-1)) + .WillOnce(Return(-1)); + #else + EXPECT_CALL(*g_systemMock, system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(-1)) + .WillOnce(Return(-1)); + #endif + EXPECT_EQ(NULL, saveTopOutput("RDK_Profile")); } TEST_F(dcaTestFixture, saveTopOutput1) @@ -762,11 +528,13 @@ TEST_F(dcaTestFixture, saveTopOutput1) .WillOnce(Return(-1)) .WillOnce(Return(-1)); #endif - saveTopOutput(); + EXPECT_EQ(NULL, saveTopOutput("RDK_Profile")); } TEST_F(dcaTestFixture, saveTopOutput2) { + char* filename = NULL; + filename = "/tmp/t2toplog_RDK_Profile"; EXPECT_CALL(*g_systemMock, access(_,_)) .Times(1) .WillOnce(Return(-1)); @@ -774,18 +542,20 @@ TEST_F(dcaTestFixture, saveTopOutput2) EXPECT_CALL(*g_systemMock, v_secure_system(_)) .Times(2) .WillOnce(Return(0)) - .WillOnce(Return(-1)); + .WillOnce(Return(0)); #else EXPECT_CALL(*g_systemMock, system(_)) .Times(2) .WillOnce(Return(0)) - .WillOnce(Return(-1)); + .WillOnce(Return(0)); #endif - saveTopOutput(); + EXPECT_STREQ(filename, saveTopOutput("RDK_Profile")); } TEST_F(dcaTestFixture, removeTopOutput) { + char* filename = NULL; + filename = strdup("/tmp/t2toplog_RDK_Profile"); #ifdef LIBSYSWRAPPER_BUILD EXPECT_CALL(*g_systemMock, v_secure_system(_)) .Times(1) @@ -795,11 +565,13 @@ TEST_F(dcaTestFixture, removeTopOutput) .Times(1) .WillOnce(Return(-1)); #endif - removeTopOutput(); + removeTopOutput(filename); } TEST_F(dcaTestFixture, removeTopOutput1) { + char* filename = NULL; + filename = strdup("/tmp/t2toplog_RDK_Profile"); #ifdef LIBSYSWRAPPER_BUILD EXPECT_CALL(*g_systemMock, v_secure_system(_)) .Times(1) @@ -809,7 +581,7 @@ TEST_F(dcaTestFixture, removeTopOutput1) .Times(1) .WillOnce(Return(0)); #endif - removeTopOutput(); + removeTopOutput(filename); } #else @@ -839,7 +611,7 @@ TEST_F(dcaTestFixture, getTotalCpuTimes1) EXPECT_EQ(1, getTotalCpuTimes(totaltime)); } #endif - +/* TEST_F(dcaTestFixture, getProcUsage4) { Vector* gresulist = NULL; @@ -918,6 +690,8 @@ TEST_F(dcaTestFixture, getProcUsage5) #endif EXPECT_EQ(0, getProcUsage("telemetry2_0", grepResultList, true, "[0-9]+")); } +*/ +//legacyutils.c TEST_F(dcaTestFixture, getLoadAvg) { @@ -954,7 +728,7 @@ TEST_F(dcaTestFixture, getLoadAvg1) .Times(1) .WillOnce(Return(0)); EXPECT_EQ(0, getLoadAvg(grepResultList, false, NULL)); - Vector_Destroy(grepResultList, free); + Vector_Destroy(grepResultList, freeGResult); } TEST_F(dcaTestFixture, getLoadAvg2) @@ -978,5 +752,179 @@ TEST_F(dcaTestFixture, getLoadAvg2) .Times(1) .WillOnce(Return(0)); EXPECT_EQ(1, getLoadAvg(grepResultList, true, "[0-9]+")); - Vector_Destroy(grepResultList, free); + Vector_Destroy(grepResultList, freeGResult); } + +TEST_F(dcaTestFixture, initProperties) +{ + char* logpath = NULL; + long int pagesize = 0; + char* perspath = NULL; + FILE* fp = (FILE*)0xffffffff; + EXPECT_CALL(*g_fileIOMock, fopen(_,_)) + .Times(2) + .WillOnce(Return(fp)) + .WillOnce(Return(fp)); + EXPECT_CALL(*g_fileIOMock, fscanf(_,_,_)) + .Times(10) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(EOF)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(EOF)); + EXPECT_CALL(*g_fileIOMock, fclose(_)) + .Times(2) + .WillOnce(Return(0)) + .WillOnce(Return(0)); + initProperties(&logpath, &perspath, &pagesize); +} + +//dca.c +TEST_F(dcaTestFixture, processTopPattern) +{ + Vector* topMarkerlist = NULL; + Vector_Create(&topMarkerlist); + GrepMarker* topMarker = (GrepMarker*) malloc(sizeof(GrepMarker)); + topMarker->markerName = strdup("cpu_telemetry2_0"); + topMarker->searchString = strdup("telemetry2_0"); + topMarker->trimParam = false; + topMarker->regexParam = NULL; + topMarker->logFile = strdup("top_log.txt"); + topMarker->skipFreq = 0; + + Vector_PushBack(topMarkerlist, (void*) topMarker); + Vector* outgrepResultlist = NULL; + Vector_Create(&outgrepResultlist); + EXPECT_CALL(*g_systemMock, access(_,_)) + .Times(1) + .WillOnce(Return(-1)); + #ifdef LIBSYSWRAPPER_BUILD + EXPECT_CALL(*g_systemMock, v_secure_system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(-1)); + #else + EXPECT_CALL(*g_systemMock, system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(-1)); + #endif + FILE* fp = (FILE*)NULL; + #ifdef LIBSYSWRAPPER_BUILD + EXPECT_CALL(*g_fileIOMock, v_secure_popen(_,_)) + .Times(1) + .WillOnce(Return(fp)); + #else + EXPECT_CALL(*g_fileIOMock, popen(_,_)) + .Times(1) + .WillOnce(Return(fp)); + #endif + EXPECT_EQ(0, processTopPattern("RDK_Profile", topMarkerlist, outgrepResultlist, 1)); + Vector_Destroy(topMarkerlist, freeGMarker); + Vector_Destroy(outgrepResultlist, free); +} + +TEST_F(dcaTestFixture, processTopPattern1) +{ + Vector* topMarkerlist = NULL; + Vector_Create(&topMarkerlist); + GrepMarker* topMarker = (GrepMarker*) malloc(sizeof(GrepMarker)); + topMarker->markerName = strdup("cpu_telemetry2_0"); + topMarker->searchString = strdup("telemetry2_0"); + topMarker->trimParam = true; + topMarker->regexParam = strdup("[0-9]+"); + topMarker->logFile = strdup("top_log.txt"); + topMarker->skipFreq = 0; + GrepMarker* topMarker1 = (GrepMarker*) malloc(sizeof(GrepMarker)); + topMarker1->markerName = strdup("Load_Average"); + topMarker1->searchString = strdup("telemetry2_0"); + topMarker1->trimParam = true; + topMarker1->regexParam = strdup("[0-9]+"); + topMarker1->logFile = strdup("top_log.txt"); + topMarker1->skipFreq = 0; + Vector_PushBack(topMarkerlist, (void*) topMarker1); + Vector_PushBack(topMarkerlist, (void*) topMarker); + Vector* outgrepResultlist = NULL; + Vector_Create(&outgrepResultlist); + EXPECT_CALL(*g_systemMock, access(_,_)) + .Times(1) + .WillOnce(Return(-1)); + #ifdef LIBSYSWRAPPER_BUILD + EXPECT_CALL(*g_systemMock, v_secure_system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(-1)); + #else + EXPECT_CALL(*g_systemMock, system(_)) + .Times(3) + .WillOnce(Return(0)) + .WillOnce(Return(0)) + .WillOnce(Return(-1)); + #endif + FILE* fp = (FILE*)NULL; + #ifdef LIBSYSWRAPPER_BUILD + EXPECT_CALL(*g_fileIOMock, v_secure_popen(_,_)) + .Times(1) + .WillOnce(Return(fp)); + #else + EXPECT_CALL(*g_fileIOMock, popen(_,_)) + .Times(1) + .WillOnce(Return(fp)); + #endif + FILE* fs= (FILE*)0xffffffff; + EXPECT_CALL(*g_fileIOMock, fopen(_,_)) + .Times(1) + .WillOnce(Return(fs)); + EXPECT_CALL(*g_fileIOMock, fread(_,_,_,_)) + .Times(1) + .WillOnce(Return(14)); + EXPECT_CALL(*g_fileIOMock, fclose(_)) + .Times(1) + .WillOnce(Return(0)); + + EXPECT_EQ(0, processTopPattern("RDK_Profile", topMarkerlist, outgrepResultlist, 1)); + Vector_Destroy(topMarkerlist, freeGMarker); + Vector_Destroy(outgrepResultlist, free); +} + +TEST_F(dcaTestFixture, getDCAResultsInVector) +{ + + Vector* out_grepResultList = NULL; + Vector_Create(&out_grepResultList); + GrepSeekProfile *gsProfile = (GrepSeekProfile *)malloc(sizeof(GrepSeekProfile)); + gsProfile->logFileSeekMap = hash_map_create(); + gsProfile->execCounter = 0; + long *tempnum; + double val = 1234; + tempnum = (long *)malloc(sizeof(long)); + *tempnum = (long)val; + hash_map_put(gsProfile->logFileSeekMap, strdup("t2_log.txt"), (void*)tempnum, free); + Vector* vecMarkerList = NULL; + Vector_Create(&vecMarkerList); + GrepMarker* marker = (GrepMarker*) malloc(sizeof(GrepMarker)); + marker->markerName = strdup("SYS_INFO_TEST"); + marker->searchString = strdup("Test Marker"); + marker->trimParam = true; + marker->regexParam = strdup("[0-9]+"); + marker->logFile = strdup("Consolelog.txt.0"); + marker->skipFreq = 0; + Vector_PushBack(vecMarkerList, (void*) marker); + EXPECT_CALL(*g_fileIOMock, open(_,_)) + .Times(1) + .WillOnce(Return(-1)); + EXPECT_EQ(0, getDCAResultsInVector(gsProfile, vecMarkerList, &out_grepResultList, true, "/opt/logs")); + hash_map_destroy(gsProfile->logFileSeekMap, free); + gsProfile->logFileSeekMap = NULL; + free(gsProfile); + Vector_Destroy(vecMarkerList, freeGMarker); + Vector_Destroy(out_grepResultList, free); +} \ No newline at end of file diff --git a/source/test/t2parser/Makefile.am b/source/test/t2parser/Makefile.am index 39e70edb..ff66b885 100644 --- a/source/test/t2parser/Makefile.am +++ b/source/test/t2parser/Makefile.am @@ -31,7 +31,7 @@ bin_PROGRAMS = t2parser_gtest.bin t2parser_gtest_bin_CPPFLAGS = -I$(PKG_CONFIG_SYSROOT_DIR)$(includedir)/gtest -I$(PKG_CONFIG_SYSROOT_DIR)$(includedir)/glib-2.0 -I$(PKG_CONFIG_SYSROOT_DIR)/usr/local/lib -I${top_srcdir}/source/include -I${top_srcdir}/source/xconf-client -I${top_srcdir}/source/dcautil -I${top_srcdir}/source/t2parser -I${top_srcdir}/source/bulkdata -I${top_srcdir}/source/utils -I${top_srcdir}/source/ccspinterface -I${top_srcdir}/source -I${top_srcdir}/include -I${top_srcdir}/source/reportgen -I$(PKG_CONFIG_SYSROOT_DIR)/usr/lib -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/cjson -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/gmock -I$(PKG_CONFIG_SYSROOT_DIR)/usr/src/googletest/googlemock/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/lib/glib-2.0/include -I$(PKG_CONFIG_SYSROOT_DIR)/usr/include/glib-2.0 -I${RDK_PROJECT_ROOT_PATH}/$(GLIB_CFLAGS) -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/glib-2.0 -I${PKG_CONFIG_SYSROOT_DIR}$(libdir)/glib-2.0/include -t2parser_gtest_bin_SOURCES = gtest_main.cpp t2parserMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp t2parserTest.cpp ../../utils/vector.c ../../utils/t2common.c ../../utils/t2log_wrapper.c ../../t2parser/t2parserxconf.c ../../t2parser/t2parser.c +t2parser_gtest_bin_SOURCES = gtest_main.cpp t2parserMock.cpp ../mocks/rdklogMock.cpp ../mocks/rbusMock.cpp t2parserTest.cpp ../../utils/vector.c ../../utils/t2common.c ../../utils/t2log_wrapper.c ../../t2parser/t2parserxconf.c ../../t2parser/t2parser.c ../../dcautil/legacyutils.c ../../utils/t2collection.c t2parser_gtest_bin_LDFLAGS = -lgtest -lgcov -L/src/googletest/googlemock/lib -L/usr/src/googletest/googlemock/lib/.libs -lgmock -lcjson -lcurl -lmsgpackc -L/usr/include/glib-2.0 -lglib-2.0 diff --git a/source/utils/t2common.c b/source/utils/t2common.c index 819a9e9a..b125d0f7 100644 --- a/source/utils/t2common.c +++ b/source/utils/t2common.c @@ -300,4 +300,4 @@ void initWhoamiSupport(void) bool isWhoAmiEnabled(void) { return whoami_support; -} \ No newline at end of file +} diff --git a/source/utils/t2common.h b/source/utils/t2common.h index e0239045..a9731855 100644 --- a/source/utils/t2common.h +++ b/source/utils/t2common.h @@ -53,6 +53,7 @@ typedef struct _Param const char* alias; bool trimParam; char* regexParam; + int skipFreq; } Param; typedef struct _StaticParam diff --git a/source/xconf-client/Makefile.am b/source/xconf-client/Makefile.am index ebb6b33b..64c7f1e6 100644 --- a/source/xconf-client/Makefile.am +++ b/source/xconf-client/Makefile.am @@ -36,6 +36,7 @@ libxconfclient_la_CPPFLAGS = -fPIC -I${PKG_CONFIG_SYSROOT_DIR}$(includedir)/dbus -I${top_srcdir}/source/reportgen \ -I${top_srcdir}/source/ccspinterface \ -I${top_srcdir}/source/t2parser \ + -I${top_srcdir}/source/dcautil \ -I${top_srcdir}/source/utils libxconfclient_la_DEPENDENCIES = ${top_builddir}/source/ccspinterface/libccspinterface.la ${top_builddir}/source/t2parser/libt2parser.la