Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/latency_lib.h" |
| 2 | |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 3 | #include <algorithm> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 4 | #include <chrono> |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 5 | #include <compare> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 6 | #include <random> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 7 | |
| 8 | #include "aos/logging/logging.h" |
| 9 | #include "aos/realtime.h" |
| 10 | #include "aos/time/time.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
| 16 | void TimerThread(const monotonic_clock::time_point end_time, |
| 17 | int timer_priority) { |
| 18 | // Standard mersenne_twister_engine seeded with 0 |
| 19 | ::std::mt19937 generator(0); |
| 20 | |
| 21 | // Sleep between 1 and 15 ms. |
| 22 | ::std::uniform_int_distribution<> distribution(1000, 15000); |
| 23 | |
| 24 | chrono::nanoseconds max_wakeup_latency = chrono::nanoseconds(0); |
| 25 | |
| 26 | SetCurrentThreadRealtimePriority(timer_priority); |
| 27 | while (true) { |
| 28 | const monotonic_clock::time_point wakeup_time = |
| 29 | monotonic_clock::now() + chrono::microseconds(distribution(generator)); |
| 30 | |
| 31 | ::std::this_thread::sleep_until(wakeup_time); |
| 32 | const monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 33 | const chrono::nanoseconds wakeup_latency = monotonic_now - wakeup_time; |
| 34 | |
| 35 | max_wakeup_latency = ::std::max(wakeup_latency, max_wakeup_latency); |
| 36 | |
| 37 | if (monotonic_now > end_time) { |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | AOS_LOG(INFO, "Max wakeup latency: %d.%d microseconds\n", |
| 42 | static_cast<int>(max_wakeup_latency.count() / 1000), |
| 43 | static_cast<int>(max_wakeup_latency.count() % 1000)); |
| 44 | } |
| 45 | |
| 46 | } // namespace aos |