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