blob: a31df1da3baa2b35d10df5436d503948dcca0d0f [file] [log] [blame]
Austin Schuh5af45eb2019-09-16 20:54:18 -07001#include "aos/ipc_lib/latency_lib.h"
2
Stephan Pleines682928d2024-05-31 20:43:48 -07003#include <algorithm>
Austin Schuh5af45eb2019-09-16 20:54:18 -07004#include <chrono>
Stephan Pleines682928d2024-05-31 20:43:48 -07005#include <compare>
Austin Schuh5af45eb2019-09-16 20:54:18 -07006#include <random>
Austin Schuh5af45eb2019-09-16 20:54:18 -07007
8#include "aos/logging/logging.h"
9#include "aos/realtime.h"
10#include "aos/time/time.h"
11
12namespace aos {
13
14namespace chrono = std::chrono;
15
16void 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