Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Develop fix realtime lockup - rev2 #787

Merged
merged 9 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions gldcore/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2499,34 +2499,39 @@ STATUS GldExec::exec_start(void)
if ( global_run_realtime > 0 && iteration_counter > 0 )
{
double metric=0;
#ifdef WIN32
struct timeb tv;
ftime(&tv);
if ( 1000-tv.millitm >= 0 )
{
IN_MYCONTEXT output_verbose("waiting %d msec", 1000-tv.millitm);
Sleep(1000-tv.millitm );
metric = (1000-tv.millitm)/1000.0;
}
else
output_error("simulation failed to keep up with real time");
#else
struct timeval tv;
gettimeofday(&tv, NULL);
if ( 1000000-tv.tv_usec >= 0 )
unsigned int udiff = 1000000 - tv.tv_usec;
if ( global_run_realtime == 1 ) // lock onto system clock
{
IN_MYCONTEXT output_verbose("waiting %d usec", 1000000-tv.tv_usec);
usleep(1000000-tv.tv_usec);
metric = (1000000-tv.tv_usec)/1000000.0;
int diff = global_clock - tv.tv_sec;
if ( diff < 0 )
{
IN_MYCONTEXT output_verbose("skipping %d seconds to catch up", -diff);
global_realtime_metric = 0.0;
}
else if ( diff > 0 )
{
global_realtime_metric = 1.0;
IN_MYCONTEXT output_verbose("sleeping %d seconds to catch up", diff);
sleep(diff);
gettimeofday(&tv, NULL);
udiff = 1000000 - tv.tv_usec;
}
global_clock = tv.tv_sec + 1;
}
else
{
output_error("simulation failed to keep up with real time");
global_clock++;
metric = (udiff)/1000000.0;
global_realtime_metric = global_realtime_metric*realtime_metric_decay + metric*(1-realtime_metric_decay);
}
if ( udiff >= 0 )
{
IN_MYCONTEXT output_verbose("waiting %d usec to synchronize", udiff);
usleep(udiff);
}
#endif
wlock_sync();
global_clock += global_run_realtime;
global_realtime_metric = global_realtime_metric*realtime_metric_decay + metric*(1-realtime_metric_decay);
sync_reset(NULL);
sync_set(NULL,global_clock,false);
IN_MYCONTEXT output_verbose("realtime clock advancing to %d", (int)global_clock);
Expand Down
13 changes: 11 additions & 2 deletions gldcore/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ TIMESTAMP _object_sync(OBJECT *obj, /**< the object to synchronize */
sync_time = plc_time;

/* compute valid_to time */
if(sync_time>TS_MAX)
if ( sync_time > TS_MAX )
obj->valid_to = TS_NEVER;
else
obj->valid_to = sync_time; // NOTE, this can be negative
Expand Down Expand Up @@ -1996,10 +1996,19 @@ TIMESTAMP object_sync(OBJECT *obj, /**< the object to synchronize */
char *event = NULL;
if ( obj->oclass->sync != NULL )
{
unsigned int iter = 0;
TIMESTAMP tt;
do {
/* don't call sync beyond valid horizon */
t2 = _object_sync(obj,(ts<(obj->valid_to>0?obj->valid_to:TS_NEVER)?ts:obj->valid_to),pass);
} while (t2>0 && ts>(t2<0?-t2:t2) && t2<TS_NEVER);
if ( iter++ > global_iteration_limit )
{
output_error("maximum iteration limit reached on valid_to sync update");
return TS_INVALID;
}
tt = ( t2 < 0 ? -t2 : t2 );
} while ( tt > global_clock && tt < ts );
//} while (t2>0 && ts>(t2<0?-t2:t2) && t2<TS_NEVER );
}

/* event handler */
Expand Down
2 changes: 0 additions & 2 deletions market/autotest/dutycycle_CPP_player.player
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,4 @@
2000-01-01 21:58:00 PST,+6.98471e-009
2000-01-01 21:59:00 PST,+4.88923e-008
2000-01-01 22:00:00 PST,+2737.53
2000-01-01 23:30:00 PST,+1502.27
2000-01-01 23:35:00 PST,+2737.53
2000-01-01 23:36:00 PST,+1502.27
40 changes: 24 additions & 16 deletions residential/zipload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ TIMESTAMP ZIPload::sync(TIMESTAMP t0, TIMESTAMP t1)
dt = int(1/R * 3600);

next_time = t1 + dt;
// char ts[64] = "???";
// gl_printtime(next_time,ts,sizeof(ts));
// fprintf(stdout,"DR_MODE: duty_cycle=%g, period=%g, phase=%g, next_time --> %s\n", duty_cycle, period, phase,ts);
// fflush(stdout);
}

// We're in duty cycle mode
Expand Down Expand Up @@ -425,14 +429,18 @@ TIMESTAMP ZIPload::sync(TIMESTAMP t0, TIMESTAMP t1)
next_time = t1 + (period * 3600) * (1 - phase) + 1;
}
}
// char ts[64] = "???";
// gl_printtime(next_time,ts,sizeof(ts));
// fprintf(stdout,"OV_NORMAL: duty_cycle=%g, period=%g, phase=%g, next_time --> %s\n", duty_cycle, period, phase,ts);
// fflush(stdout);
last_duty_cycle = duty_cycle;
}
else if (this->re_override == OV_OFF) // After release or recovery time
{
if (phase <= 1 && t1>=next_time)
{
this->re_override = OV_NORMAL;
next_time = t1;
next_time = t1 + (period * 3600) * (1 - fmod(phase,1)) + 1;
}
else if (t1 >= next_time || next_time == TS_NEVER) // we just came from override ON
{
Expand All @@ -445,28 +453,18 @@ TIMESTAMP ZIPload::sync(TIMESTAMP t0, TIMESTAMP t1)
phase -= 1 * recovery_duty_cycle / duty_cycle * (1 - fmod(phase,1)); // Track everything by the original duty cycle
else
phase = 0; //Start over

/*if (phase < 0)
{
next_time = t1 + (period * 3600) * (recovery_duty_cycle - 0) + 1;
phase = 0;
}*/
}
else // ON->OFF
{
//if (multiplier == 1) // we just transitioned
//{
//if (phase >= 1 * recovery_duty_cycle / duty_cycle)
// phase -= 1 * recovery_duty_cycle / duty_cycle; // Track everything by the original duty cycle
//else if (phase < 1)
// this->re_override = OV_NORMAL;
//}
multiplier = 0;
next_time = t1 + (period * 3600) * (1 - fmod(phase,1)) + 1;
}
}
last_duty_cycle = duty_cycle;

// char ts[64] = "???";
// gl_printtime(next_time,ts,sizeof(ts));
// fprintf(stdout,"OV_OFF: duty_cycle=%g, period=%g, phase=%g, next_time --> %s\n", duty_cycle, period, phase,ts);
// fflush(stdout);
}
else // override is ON, so no power
{
Expand All @@ -488,6 +486,10 @@ TIMESTAMP ZIPload::sync(TIMESTAMP t0, TIMESTAMP t1)
last_duty_cycle = 0;
}
}
// char ts[64] = "???";
// gl_printtime(next_time,ts,sizeof(ts));
// fprintf(stdout,"OV_ON: duty_cycle=%g, period=%g, phase=%g, next_time --> %s\n", duty_cycle, period, phase,ts);
// fflush(stdout);
}
// last_duty_cycle = duty_cycle;
}
Expand Down Expand Up @@ -579,8 +581,14 @@ TIMESTAMP ZIPload::sync(TIMESTAMP t0, TIMESTAMP t1)
load.power_factor = 0.0;
}

if (next_time < t2 && next_time > 0)
if ( next_time < t2 && next_time > 0 )
t2 = next_time;
// char ts0[64], ts1[64], ts2[64];
// gl_printtime(t0,ts0,sizeof(ts0));
// gl_printtime(t1,ts1,sizeof(ts1));
// gl_printtime(t2,ts2,sizeof(ts2));
// fprintf(stdout,"ZIPload::sync(t0=%s,t1=%s) --> t2 = %s\n\n", ts0,ts1,ts2);
// fflush(stdout);
return t2;
}

Expand Down
2 changes: 1 addition & 1 deletion tape/group_recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ TIMESTAMP group_recorder::postsync(TIMESTAMP t0, TIMESTAMP t1){
}
//Default else -- Normal mode, just do what was done before

return next_write;
return next_write > gl_globalclock ? next_write : gl_globalclock+1;
} else {
// on-change intervals simply short-circuit
return TS_NEVER;
Expand Down