Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
Stephan Pleines | 6191f1d | 2024-05-30 20:44:45 -0700 | [diff] [blame] | 3 | #include <iomanip> |
| 4 | #include <iostream> // IWYU pragma: keep |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 6 | #include "absl/flags/declare.h" |
| 7 | #include "absl/flags/flag.h" |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 8 | |
| 9 | #include "aos/aos_cli_utils.h" |
| 10 | #include "aos/configuration.h" |
| 11 | #include "aos/ftrace.h" |
| 12 | #include "aos/init.h" |
| 13 | #include "aos/json_to_flatbuffer.h" |
| 14 | #include "aos/realtime.h" |
| 15 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 16 | ABSL_FLAG(int32_t, priority, -1, "If set, the RT priority to run at."); |
| 17 | ABSL_FLAG(double, max_jitter, 0.01, |
| 18 | "The max time in milliseconds between messages before marking it " |
| 19 | "as too late."); |
| 20 | ABSL_FLAG(bool, print_jitter, true, |
| 21 | "If true, print jitter events. These will impact RT performance."); |
| 22 | ABSL_DECLARE_FLAG(bool, enable_ftrace); |
| 23 | ABSL_FLAG(bool, print_latency_stats, false, |
| 24 | "If true, print latency stats. These will impact RT performance."); |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 25 | |
| 26 | namespace aos { |
| 27 | |
| 28 | class State { |
| 29 | public: |
| 30 | State(Ftrace *ftrace, aos::EventLoop *event_loop, const Channel *channel) |
| 31 | : ftrace_(ftrace), |
| 32 | channel_(channel), |
| 33 | channel_name_(aos::configuration::StrippedChannelToString(channel_)) { |
| 34 | LOG(INFO) << "Watching for jitter on " << channel_name_; |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 35 | |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 36 | event_loop->MakeRawWatcher( |
| 37 | channel_, [this](const aos::Context &context, const void *message) { |
| 38 | HandleMessage(context, message); |
| 39 | }); |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 40 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 41 | if (absl::GetFlag(FLAGS_print_latency_stats)) { |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 42 | timer_handle_ = event_loop->AddTimer([this]() { PrintLatencyStats(); }); |
| 43 | timer_handle_->set_name("jitter"); |
| 44 | event_loop->OnRun([this, event_loop]() { |
| 45 | timer_handle_->Schedule(event_loop->monotonic_now(), |
Filip Kujawa | 2b56ff3 | 2024-03-16 16:52:17 -0700 | [diff] [blame] | 46 | std::chrono::milliseconds(1000)); |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 47 | }); |
| 48 | } |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void HandleMessage(const aos::Context &context, const void * /*message*/) { |
| 52 | if (last_time_ != aos::monotonic_clock::min_time) { |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 53 | latency_.push_back(std::chrono::duration<double>( |
| 54 | context.monotonic_event_time - last_time_) |
| 55 | .count()); |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 56 | if (context.monotonic_event_time > |
| 57 | last_time_ + std::chrono::duration_cast<std::chrono::nanoseconds>( |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 58 | std::chrono::duration<double>( |
| 59 | absl::GetFlag(FLAGS_max_jitter)))) { |
| 60 | if (absl::GetFlag(FLAGS_enable_ftrace)) { |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 61 | ftrace_->FormatMessage( |
| 62 | "Got high latency event on %s -> %.9f between messages", |
| 63 | channel_name_.c_str(), |
| 64 | std::chrono::duration<double>(context.monotonic_event_time - |
| 65 | last_time_) |
| 66 | .count()); |
| 67 | ftrace_->TurnOffOrDie(); |
| 68 | } |
| 69 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 70 | if (absl::GetFlag(FLAGS_print_jitter)) { |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 71 | // Printing isn't realtime, but if someone wants to run as RT, they |
| 72 | // should know this. Bypass the warning. |
| 73 | ScopedNotRealtime nrt; |
| 74 | |
| 75 | LOG(INFO) << "Got a high latency event on " |
| 76 | << aos::configuration::StrippedChannelToString(channel_) |
| 77 | << " -> " << std::fixed << std::setprecision(9) |
| 78 | << std::chrono::duration<double>( |
| 79 | context.monotonic_event_time - last_time_) |
| 80 | .count() |
| 81 | << " between messages."; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | last_time_ = context.monotonic_event_time; |
| 87 | } |
| 88 | |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 89 | void PrintLatencyStats() { |
| 90 | std::sort(latency_.begin(), latency_.end()); |
| 91 | if (latency_.size() >= 100) { |
| 92 | LOG(INFO) << "Percentiles 25th: " << latency_[latency_.size() * 0.25] |
| 93 | << " 50th: " << latency_[latency_.size() * 0.5] |
| 94 | << " 75th: " << latency_[latency_.size() * 0.75] |
| 95 | << " 90th: " << latency_[latency_.size() * 0.9] |
| 96 | << " 95th: " << latency_[latency_.size() * 0.95] |
| 97 | << " 99th: " << latency_[latency_.size() * 0.99]; |
| 98 | LOG(INFO) << "Max: " << latency_.back() << " Min: " << latency_.front() |
| 99 | << " Mean: " |
| 100 | << std::accumulate(latency_.begin(), latency_.end(), 0.0) / |
| 101 | latency_.size(); |
| 102 | } |
| 103 | } |
| 104 | |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 105 | private: |
| 106 | Ftrace *ftrace_; |
| 107 | const Channel *channel_; |
| 108 | |
| 109 | std::string channel_name_; |
| 110 | |
| 111 | aos::monotonic_clock::time_point last_time_ = aos::monotonic_clock::min_time; |
Filip Kujawa | 23f2165 | 2024-03-16 15:24:14 -0700 | [diff] [blame] | 112 | |
| 113 | std::vector<double> latency_; |
| 114 | |
| 115 | aos::TimerHandler *timer_handle_; |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace aos |
| 119 | |
| 120 | int main(int argc, char **argv) { |
| 121 | aos::InitGoogle(&argc, &argv); |
| 122 | |
| 123 | aos::CliUtilInfo cli_info; |
| 124 | if (cli_info.Initialize( |
| 125 | &argc, &argv, |
| 126 | [&cli_info](const aos::Channel *channel) { |
| 127 | return aos::configuration::ChannelIsReadableOnNode( |
| 128 | channel, cli_info.event_loop->node()); |
| 129 | }, |
| 130 | "channel is readeable on node", true)) { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | aos::Ftrace ftrace; |
| 135 | |
| 136 | std::vector<std::unique_ptr<aos::State>> states; |
| 137 | |
| 138 | for (const aos::Channel *channel : cli_info.found_channels) { |
| 139 | states.emplace_back(std::make_unique<aos::State>( |
| 140 | &ftrace, &(cli_info.event_loop.value()), channel)); |
| 141 | } |
| 142 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 143 | if (absl::GetFlag(FLAGS_priority) > 0) { |
| 144 | cli_info.event_loop->SetRuntimeRealtimePriority( |
| 145 | absl::GetFlag(FLAGS_priority)); |
Austin Schuh | 9900460 | 2024-03-16 11:59:24 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | cli_info.event_loop->Run(); |
| 149 | |
| 150 | return 0; |
| 151 | } |