From a743f3e5eb85e4d1d4ad1fc2f51b8743841c3819 Mon Sep 17 00:00:00 2001 From: Andreas Heuermann Date: Fri, 8 Nov 2019 16:02:44 +0100 Subject: [PATCH] [CRuntime] Add time specific logging Added a new runtime flag `-lv_time`to allow logging only in a specific time interval set by the user. This will disable all set logging streams except for STDOUT, ASSERT and SUCCESS outside of given time intreval. This is usefull for giant logs that are otherwise to big to handle. Usage: `-lv_time=start,stop` with start, stop beeing doubles for time values. --- .../c/simulation/simulation_runtime.cpp | 56 +++++++++++++++++ .../solver/perform_simulation.c.inc | 13 ++++ .../SimulationRuntime/c/simulation_data.h | 3 + .../SimulationRuntime/c/util/omc_error.c | 63 ++++++++++++++++++- .../SimulationRuntime/c/util/omc_error.h | 2 + .../c/util/simulation_options.c | 7 +++ .../c/util/simulation_options.h | 1 + 7 files changed, 144 insertions(+), 1 deletion(-) diff --git a/OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp b/OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp index 3aa1073ec08..296d1d9b58c 100644 --- a/OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp +++ b/OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp @@ -227,6 +227,51 @@ void setGlobalVerboseLevel(int argc, char**argv) delete flags; } + +/* Read value of flag lv_time to set time interval in which loggin is active */ +void setGlobalLoggingTime(int argc, char**argv, SIMULATION_INFO *simulationInfo) +{ + const char *flagStr = omc_flagValue[FLAG_LV_TIME]; + const string *flags = flagStr ? new string(flagStr) : NULL; + char *endptr; + const char *secondPart; + double loggingStartTime, loggingStopTime; + + /* Check if lv_time flag is given */ + if (flagStr==NULL || *flagStr=='\0') + { + /* default activated --> Log everything*/ + simulationInfo->useLoggingTime = 0; + return; + } + + /* Parse flagStr */ + loggingStartTime = strtod(flagStr, &endptr); + endptr = endptr+1; + secondPart = endptr; + loggingStopTime = strtod(secondPart, &endptr); + if (*endptr) + { + throwStreamPrint(NULL, "Simulation flag %s expects two real numbers, seperated by a comata. Got: %s", FLAG_NAME[FLAG_LV_TIME], flagStr); + } + + /* Check flag input */ + if (loggingStartTime > loggingStopTime) + { + throwStreamPrint(NULL, "Simulation flag %s expects first number to be smaller then second number. Got: %s", FLAG_NAME[FLAG_LV_TIME], flagStr); + } + + /* Save logging time */ + simulationInfo->useLoggingTime = 1; + simulationInfo->loggingTimeRecord[0] = loggingStartTime; + simulationInfo->loggingTimeRecord[1] = loggingStopTime; + infoStreamPrint(LOG_STDOUT, 0, "Time dependent logging enabled. Activate loggin in intervall [%f, %f]", simulationInfo->loggingTimeRecord[0], simulationInfo->loggingTimeRecord[1]); + + /* Deactivate Logging */ + deactivateLogging(); +} + + static void readFlag(int *flag, int max, const char *value, const char *flagName, const char **names, const char **desc) { int i; @@ -512,8 +557,18 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data, threadData_t outputVariablesAtEnd = omc_flagValue[FLAG_OUTPUT]; } + /* Check if logging should be enabled */ + if ((data->simulationInfo->useLoggingTime == 1) && (data->simulationInfo->startTime >= data->simulationInfo->loggingTimeRecord[0])) { + reactivateLogging(); + } + retVal = callSolver(data, threadData, init_initMethod, init_file, init_time, outputVariablesAtEnd, cpuTime, argv[0]); + /* Check if logging should be disabled */ + if (data->simulationInfo->useLoggingTime == 1) { + deactivateLogging(); + } + if (omc_flag[FLAG_ALARM]) { alarm(0); } @@ -820,6 +875,7 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr } setGlobalVerboseLevel(argc, argv); + setGlobalLoggingTime(argc, argv, data->simulationInfo); initializeDataStruc(data, threadData); if(!data) { diff --git a/OMCompiler/SimulationRuntime/c/simulation/solver/perform_simulation.c.inc b/OMCompiler/SimulationRuntime/c/simulation/solver/perform_simulation.c.inc index 1e78b3fa89a..c9c5cdbef59 100644 --- a/OMCompiler/SimulationRuntime/c/simulation/solver/perform_simulation.c.inc +++ b/OMCompiler/SimulationRuntime/c/simulation/solver/perform_simulation.c.inc @@ -390,6 +390,19 @@ int prefixedName_performSimulation(DATA* data, threadData_t *threadData, SOLVER_ int success = 0; threadData->currentErrorStage = ERROR_SIMULATION; + /* Check if loggin should be activated or deactivated */ + if ((simInfo->useLoggingTime == 1) && + (solverInfo->currentTime >= simInfo->loggingTimeRecord[0] || solverInfo->currentTime + solverInfo->currentStepSize >= simInfo->loggingTimeRecord[0]) && + (solverInfo->currentTime + solverInfo->currentStepSize < simInfo->loggingTimeRecord[1])) + { + reactivateLogging(); + } + if ((simInfo->useLoggingTime == 1) && + (solverInfo->currentTime > simInfo->loggingTimeRecord[1])) + { + deactivateLogging(); + } + #ifdef USE_DEBUG_TRACE if(useStream[LOG_TRACE]) { printf("TRACE: push loop step=%u, time=%.12g\n", __currStepNo, solverInfo->currentTime); diff --git a/OMCompiler/SimulationRuntime/c/simulation_data.h b/OMCompiler/SimulationRuntime/c/simulation_data.h index 8cb6a3e5d7f..35292851157 100644 --- a/OMCompiler/SimulationRuntime/c/simulation_data.h +++ b/OMCompiler/SimulationRuntime/c/simulation_data.h @@ -609,6 +609,9 @@ typedef struct SIMULATION_INFO const char *outputFormat; const char *variableFilter; + double loggingTimeRecord[2]; /* Time interval in which logging is active. Only used if useLoggingTime=1 */ + int useLoggingTime; /* 0 if logging is currently disabled, 1 if enabled */ + int lsMethod; /* linear solver */ int lssMethod; /* linear sparse solver */ int mixedMethod; /* mixed solver */ diff --git a/OMCompiler/SimulationRuntime/c/util/omc_error.c b/OMCompiler/SimulationRuntime/c/util/omc_error.c index cb5bccd745b..be4c7159f60 100644 --- a/OMCompiler/SimulationRuntime/c/util/omc_error.c +++ b/OMCompiler/SimulationRuntime/c/util/omc_error.c @@ -139,11 +139,13 @@ const char *LOG_TYPE_DESC[LOG_TYPE_MAX] = { "debug" }; -int useStream[SIM_LOG_MAX]; +int useStream[SIM_LOG_MAX]; /* 1 if LOG is enabled, otherwise 0 */ +int backupUseStream[SIM_LOG_MAX]; /* Backup of useStream */ int level[SIM_LOG_MAX]; int lastType[SIM_LOG_MAX]; int lastStream = LOG_UNKNOWN; int showAllWarnings = 0; +int streamsActive = 1; /* 1 if info streams from useStream are active, 0 if deactivated */ #ifdef USE_DEBUG_TRACE int DEBUG_TRACE_PUSH_HELPER(const char* pFnc, const char* pFile, const long ln){if(useStream[LOG_TRACE]) printf("TRACE: push %s (%s:%d)\n", pFnc, pFile, ln); return 0;} @@ -166,6 +168,65 @@ void initDumpSystem() useStream[LOG_SUCCESS] = 1; } +/* Deactivates streams for logging except for stdout, assert and success. */ +void deactivateLogging() +{ + int i; + + if (streamsActive == 0) + { + return; /* Do nothing if allready actinactiveive */ + } + + for(i=0; i