Skip to content

Commit

Permalink
Repurpose the "Sim/Real Time" field on the sim control panel #1626 (#…
Browse files Browse the repository at this point in the history
…1627)

* Repurpose the "Sim/Real Time" field on the sim control panel #1626

Added calculations in the realtime sync monitor job to calculate the
average sim/realtime ratio for the past 100 frames.  This is saved to
a new variable, actual_run_ratio.  Changed the sim control panel to
display the actual_run_ratio value.

* Repurpose the "Sim/Real Time" field on the sim control panel #1626

how does this compile locally but not in CI?  Didn't include cmath.
  • Loading branch information
alexlin0 authored Jan 23, 2024
1 parent dad8e3b commit 7547d36
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/trick/RealtimeSync.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ namespace Trick {
/** The clock time when the sim ended. Used for total actual time calculation.\n */
long long sim_end_time ; /**< trick_units(--) */

/** The actual simulation time/wall clock time ratio\n */
double actual_run_ratio ; /**< trick_units(--) */

/**
@brief This is the constructor of the RealtimeSync class. It starts the RealtimeSync as
disabled and sets the maximum overrun parameters to basically infinity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ private void scheduleGetSimState() {

status_vars = "trick.var_add(\"trick_sys.sched.time_tics\") \n" +
"trick.var_add(\"trick_sys.sched.mode\") \n" +
"trick.var_add(\"trick_real_time.gtod_clock.rt_clock_ratio\") \n" +
"trick.var_add(\"trick_real_time.rt_sync.actual_run_ratio\") \n" +
"trick.var_add(\"trick_real_time.rt_sync.active\") \n";

if ( debug_present != 0 ) {
Expand Down Expand Up @@ -1673,7 +1673,7 @@ protected Void doInBackground() {
ii++ ;
}

// "real_time.gtod_clock.rt_clock_ratio"
// "real_time.rt_sync.actual_run_ratio"
if (results.length > ii && results[ii] != null && results[ii] != "") {
simState.setSimRealtimeRatio(Float.parseFloat(results[ii]));
ii++ ;
Expand Down
37 changes: 36 additions & 1 deletion trick_source/sim_services/RealtimeSync/RealtimeSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include "trick/RealtimeSync.hh"
#include "trick/exec_proto.h"
#include "trick/sim_mode.h"
Expand Down Expand Up @@ -48,6 +49,8 @@ Trick::RealtimeSync::RealtimeSync( Trick::Clock * in_clock , Trick::Timer * in_t
sim_end_init_time = 0 ;
sim_end_time = 0 ;

actual_run_ratio = 0.0;

the_rts = this ;

}
Expand Down Expand Up @@ -250,6 +253,33 @@ int Trick::RealtimeSync::start_realtime(double in_frame_time , long long ref_tim
return(0) ;
}

template <size_t N>
class Run_Ratio {
public:
Run_Ratio() : num_samples(0) {}
Run_Ratio& operator()(long long sample, double in_rt_ratio)
{
samples[num_samples++ % N] = sample;
rt_ratio = in_rt_ratio;
return *this;
}

operator double() const {
if ( num_samples <= 1 ) {
return 0.0 ;
} else if ( num_samples < N ) {
return round(exec_get_software_frame() * (num_samples-1) * exec_get_time_tic_value() * rt_ratio / (double)(samples[num_samples - 1] - samples[0])*100.0)/100.0 ;
} else {
return round(exec_get_software_frame() * (N-1) * exec_get_time_tic_value() * rt_ratio / (double)(samples[(num_samples - 1) % N] - samples[num_samples % N])*100.0)/100.0 ;
}
}

private:
long long samples[N];
size_t num_samples;
double rt_ratio;
};

/**
@details
-# If real-time is not active:
Expand Down Expand Up @@ -282,6 +312,7 @@ int Trick::RealtimeSync::rt_monitor(long long sim_time_tics) {

long long curr_clock_time ;
char buf[512];
static Run_Ratio<100> run_ratio ;

/* calculate the current underrun/overrun */
curr_clock_time = rt_clock->clock_time() ;
Expand All @@ -301,6 +332,8 @@ int Trick::RealtimeSync::rt_monitor(long long sim_time_tics) {
if ( disable_flag ) {
disable_flag = false ;
}
/* calculate run ratio in non-realtime mode */
actual_run_ratio = run_ratio(curr_clock_time, 1.0);
return(0) ;
}
if ( enable_flag ) {
Expand Down Expand Up @@ -367,6 +400,8 @@ int Trick::RealtimeSync::rt_monitor(long long sim_time_tics) {
/* If the timer requires to be reset at the end of each frame, reset it here. */
sleep_timer->reset(exec_get_software_frame() / rt_clock->get_rt_clock_ratio()) ;

/* Calculate the run ratio after sleeping */
actual_run_ratio = run_ratio(curr_clock_time, rt_clock->get_rt_clock_ratio());
}

return(0) ;
Expand Down Expand Up @@ -508,4 +543,4 @@ int Trick::RealtimeSync::shutdown() {

bool Trick::RealtimeSync::is_active() {
return active;
}
}

0 comments on commit 7547d36

Please sign in to comment.