blob: 82a79ea81106488b8927478ef892abbf680a73ea [file] [log] [blame]
Austin Schuh99004602024-03-16 11:59:24 -07001#include <unistd.h>
2
Stephan Pleines6191f1d2024-05-30 20:44:45 -07003#include <iomanip>
4#include <iostream> // IWYU pragma: keep
Austin Schuh99004602024-03-16 11:59:24 -07005
Austin Schuh99f7c6a2024-06-25 22:07:44 -07006#include "absl/flags/declare.h"
7#include "absl/flags/flag.h"
Austin Schuh99004602024-03-16 11:59:24 -07008
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 Schuh99f7c6a2024-06-25 22:07:44 -070016ABSL_FLAG(int32_t, priority, -1, "If set, the RT priority to run at.");
17ABSL_FLAG(double, max_jitter, 0.01,
18 "The max time in milliseconds between messages before marking it "
19 "as too late.");
20ABSL_FLAG(bool, print_jitter, true,
21 "If true, print jitter events. These will impact RT performance.");
22ABSL_DECLARE_FLAG(bool, enable_ftrace);
23ABSL_FLAG(bool, print_latency_stats, false,
24 "If true, print latency stats. These will impact RT performance.");
Austin Schuh99004602024-03-16 11:59:24 -070025
26namespace aos {
27
28class 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 Kujawa23f21652024-03-16 15:24:14 -070035
Austin Schuh99004602024-03-16 11:59:24 -070036 event_loop->MakeRawWatcher(
37 channel_, [this](const aos::Context &context, const void *message) {
38 HandleMessage(context, message);
39 });
Filip Kujawa23f21652024-03-16 15:24:14 -070040
Austin Schuh99f7c6a2024-06-25 22:07:44 -070041 if (absl::GetFlag(FLAGS_print_latency_stats)) {
Filip Kujawa23f21652024-03-16 15:24:14 -070042 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 Kujawa2b56ff32024-03-16 16:52:17 -070046 std::chrono::milliseconds(1000));
Filip Kujawa23f21652024-03-16 15:24:14 -070047 });
48 }
Austin Schuh99004602024-03-16 11:59:24 -070049 }
50
51 void HandleMessage(const aos::Context &context, const void * /*message*/) {
52 if (last_time_ != aos::monotonic_clock::min_time) {
Filip Kujawa23f21652024-03-16 15:24:14 -070053 latency_.push_back(std::chrono::duration<double>(
54 context.monotonic_event_time - last_time_)
55 .count());
Austin Schuh99004602024-03-16 11:59:24 -070056 if (context.monotonic_event_time >
57 last_time_ + std::chrono::duration_cast<std::chrono::nanoseconds>(
Austin Schuh99f7c6a2024-06-25 22:07:44 -070058 std::chrono::duration<double>(
59 absl::GetFlag(FLAGS_max_jitter)))) {
60 if (absl::GetFlag(FLAGS_enable_ftrace)) {
Austin Schuh99004602024-03-16 11:59:24 -070061 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 Schuh99f7c6a2024-06-25 22:07:44 -070070 if (absl::GetFlag(FLAGS_print_jitter)) {
Austin Schuh99004602024-03-16 11:59:24 -070071 // 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 Kujawa23f21652024-03-16 15:24:14 -070089 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 Schuh99004602024-03-16 11:59:24 -0700105 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 Kujawa23f21652024-03-16 15:24:14 -0700112
113 std::vector<double> latency_;
114
115 aos::TimerHandler *timer_handle_;
Austin Schuh99004602024-03-16 11:59:24 -0700116};
117
118} // namespace aos
119
120int 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 Schuh99f7c6a2024-06-25 22:07:44 -0700143 if (absl::GetFlag(FLAGS_priority) > 0) {
144 cli_info.event_loop->SetRuntimeRealtimePriority(
145 absl::GetFlag(FLAGS_priority));
Austin Schuh99004602024-03-16 11:59:24 -0700146 }
147
148 cli_info.event_loop->Run();
149
150 return 0;
151}