blob: 1ec3fc147e2b52ab69331ff52ff919895573030a [file] [log] [blame]
Austin Schuh5af45eb2019-09-16 20:54:18 -07001#include <fcntl.h>
2#include <sys/stat.h>
3#include <sys/types.h>
Austin Schuh60e77942022-05-16 17:48:24 -07004
Austin Schuh5af45eb2019-09-16 20:54:18 -07005#include <chrono>
6#include <random>
7#include <thread>
8
9#include "aos/condition.h"
10#include "aos/init.h"
11#include "aos/ipc_lib/latency_lib.h"
12#include "aos/logging/implementations.h"
13#include "aos/logging/logging.h"
14#include "aos/mutex/mutex.h"
15#include "aos/realtime.h"
16#include "aos/time/time.h"
Austin Schuh60e77942022-05-16 17:48:24 -070017#include "gflags/gflags.h"
Austin Schuh5af45eb2019-09-16 20:54:18 -070018
19DEFINE_int32(seconds, 10, "Duration of the test to run");
20DEFINE_int32(
21 latency_threshold, 1000,
22 "Disable tracing when anything takes more than this many microseoncds");
23DEFINE_int32(core, 7, "Core to pin to");
24DEFINE_int32(sender_priority, 53, "RT priority to send at");
25DEFINE_int32(receiver_priority, 52, "RT priority to receive at");
26DEFINE_int32(timer_priority, 51, "RT priority to spin the timer at");
27
28DEFINE_bool(log_latency, false, "If true, log the latency");
29
30const uint32_t kSignalNumber = SIGRTMIN + 1;
31const uint32_t kQuitSignalNumber = SIGRTMIN + 2;
32
33namespace chrono = ::std::chrono;
34
35namespace aos {
36
37struct WakeupData {
38 Mutex mutex;
39 Condition condition;
40
41 WakeupData() : condition(&mutex) {}
42
43 monotonic_clock::time_point wakeup_time = monotonic_clock::epoch();
44
45 bool done = false;
46};
47
48void SenderThread(WakeupData *data) {
49 const monotonic_clock::time_point end_time =
50 monotonic_clock::now() + chrono::seconds(FLAGS_seconds);
51 // Standard mersenne_twister_engine seeded with 0
52 ::std::mt19937 generator(0);
53
54 // Sleep between 1 and 15 ms.
55 ::std::uniform_int_distribution<> distribution(1000, 15000);
56
Austin Schuh9014e3b2020-11-21 14:26:07 -080057 SetCurrentThreadAffinity(MakeCpusetFromCpus({FLAGS_core}));
Austin Schuh5af45eb2019-09-16 20:54:18 -070058 SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
59 while (true) {
60 const monotonic_clock::time_point wakeup_time =
61 monotonic_clock::now() + chrono::microseconds(distribution(generator));
62
63 ::std::this_thread::sleep_until(wakeup_time);
64 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
65
66 {
67 MutexLocker locker(&data->mutex);
68 data->wakeup_time = monotonic_now;
69 data->condition.Broadcast();
70
71 if (monotonic_now > end_time) {
72 break;
73 }
74 }
75 }
76
77 {
78 MutexLocker locker(&data->mutex);
79 data->done = true;
80 data->condition.Broadcast();
81 }
82
83 UnsetCurrentThreadRealtimePriority();
84}
85
86void ReceiverThread(WakeupData *data) {
87 Tracing t;
88 t.Start();
89
90 chrono::nanoseconds max_wakeup_latency = chrono::nanoseconds(0);
91 chrono::nanoseconds sum_latency = chrono::nanoseconds(0);
92 int latency_count = 0;
93
Austin Schuh9014e3b2020-11-21 14:26:07 -080094 SetCurrentThreadAffinity(MakeCpusetFromCpus({FLAGS_core}));
Austin Schuh5af45eb2019-09-16 20:54:18 -070095 SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
96 while (true) {
97 chrono::nanoseconds wakeup_latency;
98 {
99 MutexLocker locker(&data->mutex);
100 while (data->wakeup_time == monotonic_clock::epoch() && !data->done) {
101 CHECK(!data->condition.Wait());
102 }
103
104 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
105
106 if (data->done) {
107 break;
108 }
109
110 wakeup_latency = monotonic_now - data->wakeup_time;
111 data->wakeup_time = monotonic_clock::epoch();
112 }
113
114 sum_latency += wakeup_latency;
115 ++latency_count;
116
117 max_wakeup_latency = ::std::max(wakeup_latency, max_wakeup_latency);
118
119 if (wakeup_latency > chrono::microseconds(FLAGS_latency_threshold)) {
120 t.Stop();
121 AOS_LOG(INFO, "Stopped tracing, latency %" PRId64 "\n",
122 static_cast<int64_t>(wakeup_latency.count()));
123 }
124
125 if (FLAGS_log_latency) {
126 AOS_LOG(INFO, "dt: %8d.%03d\n",
127 static_cast<int>(wakeup_latency.count() / 1000),
128 static_cast<int>(wakeup_latency.count() % 1000));
129 }
130 }
131 UnsetCurrentThreadRealtimePriority();
132
133 const chrono::nanoseconds average_latency = sum_latency / latency_count;
134
135 AOS_LOG(INFO,
136 "Max futex wakeup latency: %d.%03d microseconds, average: %d.%03d "
137 "microseconds\n",
138 static_cast<int>(max_wakeup_latency.count() / 1000),
139 static_cast<int>(max_wakeup_latency.count() % 1000),
140 static_cast<int>(average_latency.count() / 1000),
141 static_cast<int>(average_latency.count() % 1000));
142}
143
144int Main(int /*argc*/, char ** /*argv*/) {
145 WakeupData data;
146
147 AOS_LOG(INFO, "Main!\n");
148 ::std::thread t([]() {
149 TimerThread(monotonic_clock::now() + chrono::seconds(FLAGS_seconds),
150 FLAGS_timer_priority);
151 });
152
153 ::std::thread st([&data]() { SenderThread(&data); });
154
155 ReceiverThread(&data);
156
157 st.join();
158 t.join();
159 return 0;
160}
161
162} // namespace aos
163
164int main(int argc, char **argv) {
165 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
166
Austin Schuh5af45eb2019-09-16 20:54:18 -0700167 return ::aos::Main(argc, argv);
168}