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