rework zmc delay code to be more readable and use few gettimeofday calls and be more efficient

This commit is contained in:
Isaac Connor 2018-02-15 14:07:05 -05:00
parent ebb0c9eec6
commit e7ec1086ea
1 changed files with 36 additions and 52 deletions

View File

@ -260,7 +260,6 @@ int main(int argc, char *argv[]) {
AnalysisThread **analysis_threads = new AnalysisThread *[n_monitors]; AnalysisThread **analysis_threads = new AnalysisThread *[n_monitors];
long *capture_delays = new long[n_monitors]; long *capture_delays = new long[n_monitors];
long *alarm_capture_delays = new long[n_monitors]; long *alarm_capture_delays = new long[n_monitors];
long *next_delays = new long[n_monitors];
struct timeval * last_capture_times = new struct timeval[n_monitors]; struct timeval * last_capture_times = new struct timeval[n_monitors];
for ( int i = 0; i < n_monitors; i++ ) { for ( int i = 0; i < n_monitors; i++ ) {
last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0; last_capture_times[i].tv_sec = last_capture_times[i].tv_usec = 0;
@ -280,64 +279,50 @@ int main(int argc, char *argv[]) {
struct timeval now; struct timeval now;
struct DeltaTimeval delta_time; struct DeltaTimeval delta_time;
while ( !zm_terminate ) { while ( !zm_terminate ) {
//Debug(2,"blocking");
sigprocmask(SIG_BLOCK, &block_set, 0); sigprocmask(SIG_BLOCK, &block_set, 0);
for ( int i = 0; i < n_monitors; i++ ) { for ( int i = 0; i < n_monitors; i++ ) {
long min_delay = MAXINT;
gettimeofday(&now, NULL);
for ( int j = 0; j < n_monitors; j++ ) {
if ( last_capture_times[j].tv_sec ) {
// We pretty much know this is positive.
DELTA_TIMEVAL(delta_time, now, last_capture_times[j], DT_PREC_3);
// capture_delay is the amount of time we should sleep to achieve the desired framerate.
if ( monitors[i]->GetState() == Monitor::ALARM )
next_delays[j] = alarm_capture_delays[j] - delta_time.delta;
else
next_delays[j] = capture_delays[j] - delta_time.delta;
if ( next_delays[j] < 0 )
next_delays[j] = 0;
}
if ( next_delays[j] <= min_delay ) {
min_delay = next_delays[j];
}
} // end foreach monitor
monitors[i]->CheckAction(); monitors[i]->CheckAction();
if ( next_delays[i] <= min_delay || next_delays[i] <= 0 ) { if ( monitors[i]->PreCapture() < 0 ) {
if ( monitors[i]->PreCapture() < 0 ) { Error("Failed to pre-capture monitor %d %d (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors);
Error("Failed to pre-capture monitor %d %d (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); result = -1;
result = -1; break;
break; }
} if ( monitors[i]->Capture() < 0 ) {
if ( monitors[i]->Capture() < 0 ) { Error("Failed to capture image from monitor %d %s (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors);
Error("Failed to capture image from monitor %d %s (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); result = -1;
result = -1; break;
break; }
} if ( monitors[i]->PostCapture() < 0 ) {
if ( monitors[i]->PostCapture() < 0 ) { Error("Failed to post-capture monitor %d %s (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors);
Error("Failed to post-capture monitor %d %s (%d/%d)", monitors[i]->Id(), monitors[i]->Name(), i+1, n_monitors); result = -1;
result = -1; break;
break; }
}
if ( next_delays[i] > 0 ) { gettimeofday(&now, NULL);
gettimeofday(&now, NULL); // capture_delay is the amount of time we should sleep to achieve the desired framerate.
DELTA_TIMEVAL(delta_time, now, last_capture_times[i], DT_PREC_3); if ( last_capture_times[i].tv_sec ) {
long sleep_time = next_delays[i] - delta_time.delta; long sleep_time;
if ( sleep_time > 0 ) { DELTA_TIMEVAL(delta_time, now, last_capture_times[i], DT_PREC_3);
Debug(2,"usleeping (%d)", sleep_time*(DT_MAXGRAN/DT_PREC_3) ); long delay = monitors[i]->GetState() == Monitor::ALARM ? alarm_capture_delays[i] : capture_delays[i];
usleep(sleep_time*(DT_MAXGRAN/DT_PREC_3));
}
last_capture_times[i] = now;
} else {
gettimeofday(&(last_capture_times[i]), NULL);
}
} // end if next_delay <= min_delay || next_delays[i] <= 0 )
} // end foreach n_monitors sleep_time = delay - delta_time.delta;
Debug(3, "Sleep time is %d from now:%d.%d last:%d.%d delay: %d", sleep_time, now.tv_sec, now.tv_usec, last_capture_times[i].tv_sec, last_capture_times[i].tv_usec, delay );
if ( sleep_time < 0 )
sleep_time = 0;
if ( sleep_time > 0 ) {
Debug(2,"usleeping (%d)", sleep_time*(DT_MAXGRAN/DT_PREC_3) );
usleep(sleep_time*(DT_MAXGRAN/DT_PREC_3));
}
} // end if has a last_capture time
last_capture_times[i] = now;
} // end foreach n_monitors
//Debug(2,"unblocking"); //Debug(2,"unblocking");
sigprocmask(SIG_UNBLOCK, &block_set, 0); sigprocmask(SIG_UNBLOCK, &block_set, 0);
if ( zm_reload ) { if ( zm_reload ) {
@ -356,7 +341,6 @@ int main(int argc, char *argv[]) {
delete [] alarm_capture_delays; delete [] alarm_capture_delays;
delete [] capture_delays; delete [] capture_delays;
delete [] next_delays;
delete [] last_capture_times; delete [] last_capture_times;
// Killoff the analysis threads. Don't need them spinning while we try to reconnect // Killoff the analysis threads. Don't need them spinning while we try to reconnect