|
7 | 7 |
|
8 | 8 | #include <chrono>
|
9 | 9 | #include <thread>
|
10 |
| - |
11 | 10 | typedef std::chrono::steady_clock Time;
|
12 | 11 | constexpr std::chrono::microseconds MIN_SLEEP(100);
|
13 | 12 |
|
@@ -37,6 +36,12 @@ class real_time_timer::impl
|
37 | 36 | const auto newHash = std::hash<real_time_config>()(*config_);
|
38 | 37 | if (newHash != configHashValue_) {
|
39 | 38 | start(currentTime);
|
| 39 | + const auto sampling_period = config_->sampling_period_to_monitor.load(); |
| 40 | + if (sampling_period.count() > 0) { |
| 41 | + sampling_period_to_monitor_ = sampling_period; |
| 42 | + } else { |
| 43 | + sampling_period_to_monitor_ = std::nullopt; |
| 44 | + } |
40 | 45 | configHashValue_ = newHash;
|
41 | 46 | }
|
42 | 47 | double rtfTarget = config_->real_time_factor_target.load();
|
@@ -72,21 +77,39 @@ class real_time_timer::impl
|
72 | 77 | std::shared_ptr<real_time_config> config_;
|
73 | 78 | size_t configHashValue_;
|
74 | 79 | std::shared_ptr<real_time_metrics> metrics_;
|
| 80 | + std::optional<std::chrono::milliseconds> sampling_period_to_monitor_ = std::nullopt; |
| 81 | + |
| 82 | + template<typename Rep, typename Period> |
| 83 | + void update_rolling_average_real_time_factor( |
| 84 | + const Time::time_point& currentTime, |
| 85 | + const time_point& currentSimulationTime, |
| 86 | + const std::chrono::duration<Rep, Period>& elapsedRealTime) |
| 87 | + { |
| 88 | + const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_; |
| 89 | + |
| 90 | + metrics_->rolling_average_real_time_factor = |
| 91 | + std::chrono::duration_cast<cosim::duration>(elapsedSimTime).count() / (1.0 * std::chrono::duration_cast<cosim::duration>(elapsedRealTime).count()); |
| 92 | + rtStartTime_ = currentTime; |
| 93 | + rtSimulationStartTime_ = currentSimulationTime; |
| 94 | + rtCounter_ = 0L; |
| 95 | + } |
75 | 96 |
|
76 | 97 | void update_real_time_factor(Time::time_point currentTime, time_point currentSimulationTime)
|
77 | 98 | {
|
78 |
| - const auto relativeSimTime = currentSimulationTime - simulationStartTime_; |
79 |
| - const auto relativeRealTime = currentTime - startTime_; |
| 99 | + const auto relativeSimTime = std::chrono::duration_cast<cosim::duration>(currentSimulationTime - simulationStartTime_); |
| 100 | + const auto relativeRealTime = std::chrono::duration_cast<cosim::duration>(currentTime - startTime_); |
80 | 101 | metrics_->total_average_real_time_factor = relativeSimTime.count() / (1.0 * relativeRealTime.count());
|
81 | 102 |
|
82 |
| - if (rtCounter_ >= config_->steps_to_monitor.load()) { |
83 |
| - const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_; |
| 103 | + if (sampling_period_to_monitor_.has_value()) { |
84 | 104 | const auto elapsedRealTime = currentTime - rtStartTime_;
|
85 | 105 |
|
86 |
| - metrics_->rolling_average_real_time_factor = elapsedSimTime.count() / (1.0 * elapsedRealTime.count()); |
87 |
| - rtStartTime_ = currentTime; |
88 |
| - rtSimulationStartTime_ = currentSimulationTime; |
89 |
| - rtCounter_ = 0L; |
| 106 | + if (elapsedRealTime > sampling_period_to_monitor_.value()) { |
| 107 | + const auto elapsedSimTime = currentSimulationTime - rtSimulationStartTime_; |
| 108 | + update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime); |
| 109 | + } |
| 110 | + } else if (rtCounter_ >= config_->steps_to_monitor.load()) { |
| 111 | + const auto elapsedRealTime = currentTime - rtStartTime_; |
| 112 | + update_rolling_average_real_time_factor(currentTime, currentSimulationTime, elapsedRealTime); |
90 | 113 | }
|
91 | 114 | rtCounter_++;
|
92 | 115 | }
|
|
0 commit comments