blob: 47f6528285c549d88c12facec07cf571b34081bd [file] [log] [blame]
Austin Schuh5af45eb2019-09-16 20:54:18 -07001#include "gflags/gflags.h"
2
3#include <sys/eventfd.h>
4#include <sys/stat.h>
5#include <sys/types.h>
6#include <chrono>
7#include <random>
8#include <thread>
9
10#include "aos/events/epoll.h"
11#include "aos/init.h"
12#include "aos/ipc_lib/latency_lib.h"
13#include "aos/logging/implementations.h"
14#include "aos/logging/logging.h"
15#include "aos/realtime.h"
16#include "aos/time/time.h"
17
18// This is a demo program which uses named pipes to communicate.
19// It measures both latency of a random timer thread, and latency of the
20// pipe.
21
22DEFINE_int32(seconds, 10, "Duration of the test to run");
23DEFINE_int32(
24 latency_threshold, 1000,
25 "Disable tracing when anything takes more than this many microseoncds");
26DEFINE_int32(core, 7, "Core to pin to");
27DEFINE_int32(sender_priority, 53, "RT priority to send at");
28DEFINE_int32(receiver_priority, 52, "RT priority to receive at");
29DEFINE_int32(timer_priority, 51, "RT priority to spin the timer at");
30
31DEFINE_bool(log_latency, false, "If true, log the latency");
32
33namespace chrono = ::std::chrono;
34
35namespace aos {
36
37void SenderThread(int fd) {
38 const monotonic_clock::time_point end_time =
39 monotonic_clock::now() + chrono::seconds(FLAGS_seconds);
40 // Standard mersenne_twister_engine seeded with 0
41 ::std::mt19937 generator(0);
42
43 // Sleep between 1 and 15 ms.
44 ::std::uniform_int_distribution<> distribution(1000, 15000);
45
Austin Schuh094d09b2020-11-20 23:26:52 -080046 {
47 cpu_set_t cpuset;
48 CPU_ZERO(&cpuset);
49 CPU_SET(FLAGS_core, &cpuset);
50
51 SetCurrentThreadAffinity(cpuset);
52 }
Austin Schuh5af45eb2019-09-16 20:54:18 -070053 SetCurrentThreadRealtimePriority(FLAGS_sender_priority);
54 while (true) {
55 const monotonic_clock::time_point wakeup_time =
56 monotonic_clock::now() + chrono::microseconds(distribution(generator));
57
58 ::std::this_thread::sleep_until(wakeup_time);
59 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
60 char sent_time_buffer[8];
61 memcpy(sent_time_buffer, &monotonic_now, sizeof(sent_time_buffer));
62 PCHECK(write(fd, sent_time_buffer, sizeof(sent_time_buffer)));
63
64 if (monotonic_now > end_time) {
65 break;
66 }
67 }
68
69 {
70 ::std::this_thread::sleep_for(chrono::milliseconds(100));
71 const monotonic_clock::time_point stop_time(chrono::nanoseconds(1));
72 char sent_time_buffer[8];
73 memcpy(sent_time_buffer, &stop_time, sizeof(sent_time_buffer));
74 PCHECK(write(fd, sent_time_buffer, sizeof(sent_time_buffer)));
75 }
76 UnsetCurrentThreadRealtimePriority();
77}
78
79void ReceiverThread(int fd) {
80 Tracing t;
81 t.Start();
82
83 chrono::nanoseconds max_wakeup_latency = chrono::nanoseconds(0);
84
85 chrono::nanoseconds sum_latency = chrono::nanoseconds(0);
86 int latency_count = 0;
87
88 internal::EPoll epoll;
89
90 epoll.OnReadable(fd, [&t, &epoll, &max_wakeup_latency, &sum_latency,
91 &latency_count, fd]() {
92 char sent_time_buffer[8];
93 const int ret = read(fd, static_cast<void *>(sent_time_buffer),
94 sizeof(sent_time_buffer));
95 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
96 CHECK_EQ(ret, 8);
97
98 monotonic_clock::time_point sent_time;
99 memcpy(&sent_time, sent_time_buffer, sizeof(sent_time_buffer));
100
101 if (sent_time == monotonic_clock::time_point(chrono::nanoseconds(1))) {
102 epoll.Quit();
103 return;
104 }
105
106 const chrono::nanoseconds wakeup_latency = monotonic_now - sent_time;
107
108 sum_latency += wakeup_latency;
109 ++latency_count;
110
111 max_wakeup_latency = ::std::max(wakeup_latency, max_wakeup_latency);
112
113 if (wakeup_latency > chrono::microseconds(FLAGS_latency_threshold)) {
114 t.Stop();
115 AOS_LOG(INFO, "Stopped tracing, latency %" PRId64 "\n",
116 static_cast<int64_t>(wakeup_latency.count()));
117 }
118
119 if (FLAGS_log_latency) {
120 AOS_LOG(INFO, "dt: %8d.%03d\n",
121 static_cast<int>(wakeup_latency.count() / 1000),
122 static_cast<int>(wakeup_latency.count() % 1000));
123 }
124 });
125
Austin Schuh094d09b2020-11-20 23:26:52 -0800126 {
127 cpu_set_t cpuset;
128 CPU_ZERO(&cpuset);
129 CPU_SET(FLAGS_core, &cpuset);
130
131 SetCurrentThreadAffinity(cpuset);
132 }
Austin Schuh5af45eb2019-09-16 20:54:18 -0700133 SetCurrentThreadRealtimePriority(FLAGS_receiver_priority);
134 epoll.Run();
135 UnsetCurrentThreadRealtimePriority();
136 epoll.DeleteFd(fd);
137
138 const chrono::nanoseconds average_latency = sum_latency / latency_count;
139
140 AOS_LOG(INFO,
141 "Max eventfd wakeup latency: %d.%03d microseconds, average: %d.%03d "
142 "microseconds\n",
143 static_cast<int>(max_wakeup_latency.count() / 1000),
144 static_cast<int>(max_wakeup_latency.count() % 1000),
145 static_cast<int>(average_latency.count() / 1000),
146 static_cast<int>(average_latency.count() % 1000));
147}
148
149int Main(int /*argc*/, char ** /*argv*/) {
150 AOS_LOG(INFO, "Main!\n");
151 ::std::thread t([]() {
152 TimerThread(monotonic_clock::now() + chrono::seconds(FLAGS_seconds),
153 FLAGS_timer_priority);
154 });
155
156 int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
157 PCHECK(fd);
158
159 ::std::thread st([&fd]() { SenderThread(fd); });
160
161 ReceiverThread(fd);
162 st.join();
163
164 PCHECK(close(fd));
165
166 t.join();
167 return 0;
168}
169
170} // namespace aos
171
172int main(int argc, char **argv) {
173 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
174
Austin Schuh5af45eb2019-09-16 20:54:18 -0700175 return ::aos::Main(argc, argv);
176}