Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <string.h> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 3 | #include <sys/eventfd.h> |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 4 | #include <unistd.h> |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 5 | |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 6 | #include <algorithm> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 7 | #include <chrono> |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 8 | #include <compare> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 9 | #include <random> |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 10 | #include <ratio> |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 11 | #include <thread> |
| 12 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 13 | #include "absl/flags/flag.h" |
| 14 | #include "absl/log/check.h" |
| 15 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 16 | |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 17 | #include "aos/events/epoll.h" |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 18 | #include "aos/init.h" |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 19 | #include "aos/ipc_lib/latency_lib.h" |
| 20 | #include "aos/logging/implementations.h" |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 21 | #include "aos/realtime.h" |
| 22 | #include "aos/time/time.h" |
| 23 | |
| 24 | // This is a demo program which uses named pipes to communicate. |
| 25 | // It measures both latency of a random timer thread, and latency of the |
| 26 | // pipe. |
| 27 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 28 | ABSL_FLAG(int32_t, seconds, 10, "Duration of the test to run"); |
| 29 | ABSL_FLAG( |
| 30 | int32_t, latency_threshold, 1000, |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 31 | "Disable tracing when anything takes more than this many microseoncds"); |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 32 | ABSL_FLAG(int32_t, core, 7, "Core to pin to"); |
| 33 | ABSL_FLAG(int32_t, sender_priority, 53, "RT priority to send at"); |
| 34 | ABSL_FLAG(int32_t, receiver_priority, 52, "RT priority to receive at"); |
| 35 | ABSL_FLAG(int32_t, timer_priority, 51, "RT priority to spin the timer at"); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 36 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 37 | ABSL_FLAG(bool, log_latency, false, "If true, log the latency"); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 38 | |
| 39 | namespace chrono = ::std::chrono; |
| 40 | |
| 41 | namespace aos { |
| 42 | |
| 43 | void SenderThread(int fd) { |
| 44 | const monotonic_clock::time_point end_time = |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 45 | monotonic_clock::now() + chrono::seconds(absl::GetFlag(FLAGS_seconds)); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 46 | // Standard mersenne_twister_engine seeded with 0 |
| 47 | ::std::mt19937 generator(0); |
| 48 | |
| 49 | // Sleep between 1 and 15 ms. |
| 50 | ::std::uniform_int_distribution<> distribution(1000, 15000); |
| 51 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 52 | SetCurrentThreadAffinity(MakeCpusetFromCpus({absl::GetFlag(FLAGS_core)})); |
| 53 | SetCurrentThreadRealtimePriority(absl::GetFlag(FLAGS_sender_priority)); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 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 | |
| 79 | void 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 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 113 | if (wakeup_latency > |
| 114 | chrono::microseconds(absl::GetFlag(FLAGS_latency_threshold))) { |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 115 | t.Stop(); |
| 116 | AOS_LOG(INFO, "Stopped tracing, latency %" PRId64 "\n", |
| 117 | static_cast<int64_t>(wakeup_latency.count())); |
| 118 | } |
| 119 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 120 | if (absl::GetFlag(FLAGS_log_latency)) { |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 121 | AOS_LOG(INFO, "dt: %8d.%03d\n", |
| 122 | static_cast<int>(wakeup_latency.count() / 1000), |
| 123 | static_cast<int>(wakeup_latency.count() % 1000)); |
| 124 | } |
| 125 | }); |
| 126 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 127 | SetCurrentThreadAffinity(MakeCpusetFromCpus({absl::GetFlag(FLAGS_core)})); |
| 128 | SetCurrentThreadRealtimePriority(absl::GetFlag(FLAGS_receiver_priority)); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 129 | epoll.Run(); |
| 130 | UnsetCurrentThreadRealtimePriority(); |
| 131 | epoll.DeleteFd(fd); |
| 132 | |
| 133 | const chrono::nanoseconds average_latency = sum_latency / latency_count; |
| 134 | |
| 135 | AOS_LOG(INFO, |
| 136 | "Max eventfd 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 | |
| 144 | int Main(int /*argc*/, char ** /*argv*/) { |
| 145 | AOS_LOG(INFO, "Main!\n"); |
| 146 | ::std::thread t([]() { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 147 | TimerThread( |
| 148 | monotonic_clock::now() + chrono::seconds(absl::GetFlag(FLAGS_seconds)), |
| 149 | absl::GetFlag(FLAGS_timer_priority)); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 150 | }); |
| 151 | |
| 152 | int fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); |
| 153 | PCHECK(fd); |
| 154 | |
| 155 | ::std::thread st([&fd]() { SenderThread(fd); }); |
| 156 | |
| 157 | ReceiverThread(fd); |
| 158 | st.join(); |
| 159 | |
| 160 | PCHECK(close(fd)); |
| 161 | |
| 162 | t.join(); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | } // namespace aos |
| 167 | |
| 168 | int main(int argc, char **argv) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 169 | aos::InitGoogle(&argc, &argv); |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 170 | |
Austin Schuh | 5af45eb | 2019-09-16 20:54:18 -0700 | [diff] [blame] | 171 | return ::aos::Main(argc, argv); |
| 172 | } |