Skip to content

Commit

Permalink
[CRuntime] Add time specific logging
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AnHeuermann authored and lochel committed Dec 4, 2019
1 parent 9c5134d commit a743f3e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
56 changes: 56 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/SimulationRuntime/c/simulation_data.h
Expand Up @@ -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 */
Expand Down
63 changes: 62 additions & 1 deletion OMCompiler/SimulationRuntime/c/util/omc_error.c
Expand Up @@ -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;}
Expand All @@ -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<SIM_LOG_MAX; ++i)
{
if (i != LOG_STDOUT && i != LOG_ASSERT && i != LOG_SUCCESS)
{
backupUseStream[i] = useStream[i];
/*
if (useStream[i] != 0) {
printf("Stream %s deactivated\n",LOG_STREAM_NAME[i]);
}
*/
useStream[i] = 0;
}
}

useStream[LOG_STDOUT] = 1;
useStream[LOG_ASSERT] = 1;
useStream[LOG_SUCCESS] = 1;

streamsActive = 0; /* Deactivate info streams */
//infoStreamPrint(LOG_STDOUT,0,"Deactivated logging");
}

/* Resets streams to backup after deactivateLogging() was used. */
void reactivateLogging()
{
int i;

if (streamsActive == 1)
{
return; /* Do nothing if allready active */
}

for(i=0; i<SIM_LOG_MAX; ++i)
{
if (i != LOG_STDOUT && i != LOG_ASSERT && i != LOG_SUCCESS)
{
useStream[i] = backupUseStream[i];
/*
if (useStream[i] != 0) {
printf("Stream %s reactivated\n",LOG_STREAM_NAME[i]);
}
*/
}
}

streamsActive = 1; /* Activate info streams */
//infoStreamPrint(LOG_STDOUT,0,"Reactivated logging");
}

void printInfo(FILE *stream, FILE_INFO info)
{
fprintf(stream, "[%s:%d:%d-%d:%d:%s]", info.filename, info.lineStart, info.colStart, info.lineEnd, info.colEnd, info.readonly ? "readonly" : "writable");
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/SimulationRuntime/c/util/omc_error.h
Expand Up @@ -65,6 +65,8 @@ DLLExport extern void (*omc_assert_withEquationIndexes)(threadData_t*,FILE_INFO,
DLLExport extern void (*omc_assert_warning_withEquationIndexes)(FILE_INFO, const int*, const char*, ...);

void initDumpSystem();
void deactivateLogging();
void reactivateLogging();
void omc_assert_function(threadData_t*,FILE_INFO info, const char *msg, ...) __attribute__ ((noreturn));
void omc_assert_warning_function(FILE_INFO info, const char *msg, ...);
void omc_terminate_function(FILE_INFO info, const char *msg, ...);
Expand Down
7 changes: 7 additions & 0 deletions OMCompiler/SimulationRuntime/c/util/simulation_options.c
Expand Up @@ -98,6 +98,7 @@ const char *FLAG_NAME[FLAG_MAX+1] = {
/* FLAG_LSS_MAX_DENSITY */ "lssMaxDensity",
/* FLAG_LSS_MIN_SIZE */ "lssMinSize",
/* FLAG_LV */ "lv",
/* FLAG_LV_TIME */ "lv_time",
/* FLAG_MAX_BISECTION_ITERATIONS */ "mbi",
/* FLAG_MAX_EVENT_ITERATIONS */ "mei",
/* FLAG_MAX_ORDER */ "maxIntegrationOrder",
Expand Down Expand Up @@ -212,6 +213,7 @@ const char *FLAG_DESC[FLAG_MAX+1] = {
/* FLAG_LSS_MAX_DENSITY */ "[double (default 0.2)] value specifies the maximum density for using a linear sparse solver",
/* FLAG_LSS_MIN_SIZE */ "[int (default 4001)] value specifies the minimum system size for using a linear sparse solver",
/* FLAG_LV */ "[string list] value specifies the logging level",
/* FLAG_LV_TIME */ "[double list] specifying time interval to allow loging in",
/* FLAG_MAX_BISECTION_ITERATIONS */ "[int (default 0)] value specifies the maximum number of bisection iterations for state event detection or zero for default behavior",
/* FLAG_MAX_EVENT_ITERATIONS */ "[int (default 20)] value specifies the maximum number of event iterations",
/* FLAG_MAX_ORDER */ "value specifies maximum integration order for supported solver",
Expand Down Expand Up @@ -415,6 +417,10 @@ const char *FLAG_DETAILED_DESC[FLAG_MAX+1] = {
/* FLAG_LV */
" Value (a comma-separated String list) specifies which logging levels to\n"
" enable. Multiple options can be enabled at the same time.",
/* FLAG_LV_TIME */
" Interval (a comma-separated Double list with two elements) specifies in which\n"
" time interval logging is active. Doesn't affect LOG_STDOUT, LOG_ASSERT, and\n"
" LOG_SUCCESS, LOG_STATS, LOG_STATS_V.",
/* FLAG_MAX_BISECTION_ITERATIONS */
" Value specifies the maximum number of bisection iterations for state event\n"
" detection or zero for default behavior",
Expand Down Expand Up @@ -601,6 +607,7 @@ const int FLAG_TYPE[FLAG_MAX] = {
/* FLAG_LSS_MAX_DENSITY */ FLAG_TYPE_OPTION,
/* FLAG_LSS_MIN_SIZE */ FLAG_TYPE_OPTION,
/* FLAG_LV */ FLAG_TYPE_OPTION,
/* FLAG_LV_TIME */ FLAG_TYPE_OPTION,
/* FLAG_MAX_BISECTION_ITERATIONS */ FLAG_TYPE_OPTION,
/* FLAG_MAX_EVENT_ITERATIONS */ FLAG_TYPE_OPTION,
/* FLAG_MAX_ORDER */ FLAG_TYPE_OPTION,
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/util/simulation_options.h
Expand Up @@ -106,6 +106,7 @@ enum _FLAG
FLAG_LSS_MAX_DENSITY,
FLAG_LSS_MIN_SIZE,
FLAG_LV,
FLAG_LV_TIME,
FLAG_MAX_BISECTION_ITERATIONS,
FLAG_MAX_EVENT_ITERATIONS,
FLAG_MAX_ORDER,
Expand Down

0 comments on commit a743f3e

Please sign in to comment.