blob: 7c3511da89a3bd023ca33a372ad5dfe9aa41feb3 [file] [log] [blame]
Stephan Massaltf84cf812019-12-31 14:14:50 -08001#include <iomanip>
2#include <iostream>
3
Austin Schuhc99e1392021-03-30 22:59:24 -07004#include "absl/strings/str_format.h"
Austin Schuhb06f03b2021-02-17 22:00:37 -08005#include "aos/events/logging/log_reader.h"
Stephan Massaltf84cf812019-12-31 14:14:50 -08006#include "aos/events/simulated_event_loop.h"
7#include "aos/init.h"
8#include "aos/json_to_flatbuffer.h"
9#include "aos/time/time.h"
10#include "gflags/gflags.h"
11
Stephan Massaltf84cf812019-12-31 14:14:50 -080012DEFINE_string(
13 name, "",
14 "Name to match for printing out channels. Empty means no name filter.");
15
Austin Schuh6f3babe2020-01-26 20:34:50 -080016DEFINE_string(node, "", "Node to print stats out for.");
17
milind upadhyay38fe3cd2021-03-27 15:30:53 -070018DEFINE_bool(excessive_size_only, false,
19 "Only print channels that have a set max message size that is more "
20 "than double of the max message size.");
21
Austin Schuhc99e1392021-03-30 22:59:24 -070022// This class implements a histogram for tracking message period percentiles.
23class Histogram {
24 public:
25 Histogram(size_t buckets = 1024)
26 : max_value_bucket_(0.01), values_(buckets, 0.0), counts_(buckets, 0) {}
27
28 // Adds a new sample to the histogram, potentially downsampling the existing
29 // data.
30 void Add(double value) {
31 if (value < max_value_bucket_) {
32 const ssize_t bucket = static_cast<size_t>(
33 std::floor(value * values_.size() / max_value_bucket_));
34 CHECK_GE(bucket, 0);
35 CHECK_LT(bucket, static_cast<ssize_t>(values_.size()));
36 values_[bucket] += value;
37 if (all_counts_ == 0 || value > max_value_) {
38 max_value_ = value;
39 }
40 if (all_counts_ == 0 || value < min_value_) {
41 min_value_ = value;
42 }
43 ++counts_[bucket];
44 ++all_counts_;
45 } else {
46 // Double all the bucket sizes by merging adjacent buckets and doubling
47 // the max value. If this isn't enough, we'll recurse inside Add and
48 // do it again until it fits.
49 max_value_bucket_ *= 2.0;
50 for (size_t bucket = 0; bucket < values_.size() / 2; ++bucket) {
51 values_[bucket] = values_[bucket * 2] + values_[bucket * 2 + 1];
52 counts_[bucket] = counts_[bucket * 2] + counts_[bucket * 2 + 1];
53 }
54 for (size_t bucket = values_.size() / 2; bucket < values_.size();
55 ++bucket) {
56 values_[bucket] = 0.0;
57 counts_[bucket] = 0;
58 }
59 Add(value);
60 }
61 }
62
63 // Prints out the percentiles for a couple of critical numbers.
64 std::string Percentile() const {
65 const size_t percentile5 = all_counts_ / 20;
66 double percentile5_value = 0.0;
67 const size_t percentile50 = all_counts_ / 2;
68 double percentile50_value = 0.0;
69 const size_t percentile95 = all_counts_ - percentile5;
70 double percentile95_value = 0.0;
71
72 size_t count = 0;
73 for (size_t i = 0; i < values_.size(); ++i) {
74 if (count < percentile5 && count + counts_[i] >= percentile5) {
75 percentile5_value = values_[i] / counts_[i];
76 }
77 if (count < percentile50 && count + counts_[i] >= percentile50) {
78 percentile50_value = values_[i] / counts_[i];
79 }
80 if (count < percentile95 && count + counts_[i] >= percentile95) {
81 percentile95_value = values_[i] / counts_[i];
82 }
83 count += counts_[i];
84 }
85
86 // Assume here that these are periods in seconds. Convert to ms for
87 // readability. This isn't super generic, but that's fine for now.
88 return absl::StrFormat(
89 "[max %.3fms 95%%:%.3fms 50%%:%.3fms 5%%:%.3fms min %.3fms]",
90 max_value_ * 1000., percentile95_value * 1000.,
91 percentile50_value * 1000., percentile5_value * 1000.,
92 min_value_ * 1000.);
93 }
94
95 private:
96 // The size of the largest bucket. Used to figure out which bucket something
97 // goes into.
98 double max_value_bucket_;
99 // Max and min values overall we have seen.
100 double max_value_ = 0;
101 double min_value_ = 0;
102 // A list of the sum of values and counts for those per bucket.
103 std::vector<double> values_;
104 std::vector<size_t> counts_;
105 // Total number of samples.
106 size_t all_counts_ = 0;
107};
108
109class ChannelStats {
110 public:
111 ChannelStats(const aos::Channel *channel) : channel_(channel) {}
112
113 // Adds a sample to the statistics.
114 void Add(const aos::Context &context) {
115 max_message_size_ = std::max(max_message_size_, context.size);
116 total_message_size_ += context.size;
117 total_num_messages_++;
118 channel_end_time_ = context.realtime_event_time;
119 first_message_time_ =
120 std::min(first_message_time_, context.monotonic_event_time);
121 if (current_message_time_ != aos::monotonic_clock::min_time) {
122 histogram_.Add(std::chrono::duration<double>(
123 context.monotonic_event_time - current_message_time_)
124 .count());
125 }
126 current_message_time_ = context.monotonic_event_time;
127 }
128
129 std::string Percentile() const { return histogram_.Percentile(); }
130
131 double SecondsActive() const {
132 return aos::time::DurationInSeconds(current_message_time_ -
133 first_message_time_);
134 }
135
136 size_t max_message_size() const { return max_message_size_; }
137 size_t total_num_messages() const { return total_num_messages_; }
138
139 double avg_messages_per_sec() const {
140 return total_num_messages_ / SecondsActive();
141 }
142 size_t avg_message_size() const {
143 return total_message_size_ / total_num_messages_;
144 }
James Kuszmaul6abc7442021-10-24 13:46:04 -0700145 size_t avg_message_bandwidth() const {
146 return total_message_size_ / SecondsActive();
147 }
Austin Schuhc99e1392021-03-30 22:59:24 -0700148
149 aos::realtime_clock::time_point channel_end_time() const {
150 return channel_end_time_;
151 }
152
153 const aos::Channel *channel() const { return channel_; }
154
155 private:
Stephan Massaltf84cf812019-12-31 14:14:50 -0800156 // pointer to the channel for which stats are collected
Austin Schuhc99e1392021-03-30 22:59:24 -0700157 const aos::Channel *channel_;
158 aos::realtime_clock::time_point channel_end_time_ =
Stephan Massaltf84cf812019-12-31 14:14:50 -0800159 aos::realtime_clock::min_time;
Austin Schuhc99e1392021-03-30 22:59:24 -0700160 aos::monotonic_clock::time_point first_message_time_ =
Stephan Massaltf84cf812019-12-31 14:14:50 -0800161 // needs to be higher than time in the logfile!
162 aos::monotonic_clock::max_time;
Austin Schuhc99e1392021-03-30 22:59:24 -0700163 aos::monotonic_clock::time_point current_message_time_ =
Stephan Massaltf84cf812019-12-31 14:14:50 -0800164 aos::monotonic_clock::min_time;
Austin Schuhc99e1392021-03-30 22:59:24 -0700165
Stephan Massaltf84cf812019-12-31 14:14:50 -0800166 // channel stats to collect per channel
Austin Schuhc99e1392021-03-30 22:59:24 -0700167 int total_num_messages_ = 0;
168 size_t max_message_size_ = 0;
169 size_t total_message_size_ = 0;
170
171 Histogram histogram_;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800172};
173
174struct LogfileStats {
175 // All relevant stats on to logfile level
176 size_t logfile_length = 0;
177 int total_log_messages = 0;
178 aos::realtime_clock::time_point logfile_end_time =
179 aos::realtime_clock::min_time;
180};
181
182int main(int argc, char **argv) {
183 gflags::SetUsageMessage(
Ravago Jones8bab1842020-12-12 17:36:39 -0800184 "Usage: \n"
185 " log_stats [args] logfile1 logfile2 ...\n"
Stephan Massaltf84cf812019-12-31 14:14:50 -0800186 "This program provides statistics on a given log file. Supported "
187 "statistics are:\n"
188 " - Logfile start time;\n"
189 " - Total messages per channel/type;\n"
190 " - Max message size per channel/type;\n"
191 " - Frequency of messages per second;\n"
192 " - Total logfile size and number of messages.\n"
193 "Use --logfile flag to select a logfile (path/filename) and use --name "
194 "flag to specify a channel to listen on.");
195
196 aos::InitGoogle(&argc, &argv);
197
Ravago Jones8bab1842020-12-12 17:36:39 -0800198 if (argc < 2) {
199 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
200 }
201
202 // find logfiles
203 std::vector<std::string> unsorted_logfiles =
204 aos::logger::FindLogs(argc, argv);
205
206 // sort logfiles
207 const std::vector<aos::logger::LogFile> logfiles =
208 aos::logger::SortParts(unsorted_logfiles);
209
210 // open logfiles
211 aos::logger::LogReader reader(logfiles);
212
Stephan Massaltf84cf812019-12-31 14:14:50 -0800213 LogfileStats logfile_stats;
214 std::vector<ChannelStats> channel_stats;
215
Stephan Massaltf84cf812019-12-31 14:14:50 -0800216 aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration());
217 reader.Register(&log_reader_factory);
218
Austin Schuh6f3babe2020-01-26 20:34:50 -0800219 const aos::Node *node = nullptr;
220
221 if (aos::configuration::MultiNode(reader.configuration())) {
222 if (FLAGS_node.empty()) {
223 LOG(INFO) << "Need a --node specified. The log file has:";
Austin Schuh07676622021-01-21 18:59:17 -0800224 for (const aos::Node *node : reader.LoggedNodes()) {
Austin Schuh6f3babe2020-01-26 20:34:50 -0800225 LOG(INFO) << " " << node->name()->string_view();
226 }
Austin Schuh8c7f14b2021-01-21 19:01:54 -0800227 reader.Deregister();
Austin Schuh6f3babe2020-01-26 20:34:50 -0800228 return 1;
229 } else {
230 node = aos::configuration::GetNode(reader.configuration(), FLAGS_node);
231 }
232 }
233
Stephan Massaltf84cf812019-12-31 14:14:50 -0800234 // Make an eventloop for retrieving stats
235 std::unique_ptr<aos::EventLoop> stats_event_loop =
Austin Schuh6f3babe2020-01-26 20:34:50 -0800236 log_reader_factory.MakeEventLoop("logstats", node);
Stephan Massaltf84cf812019-12-31 14:14:50 -0800237 stats_event_loop->SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800238 stats_event_loop->SkipAosLog();
Stephan Massaltf84cf812019-12-31 14:14:50 -0800239
240 // Read channel info and store in vector
241 bool found_channel = false;
242 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
243 reader.configuration()->channels();
244
245 int it = 0; // iterate through the channel_stats
246 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
247 const aos::Channel *channel = channels->Get(i);
Austin Schuh76db3fa2020-03-07 17:02:44 -0800248 if (!aos::configuration::ChannelIsReadableOnNode(
249 channel, stats_event_loop->node())) {
250 continue;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800251 }
Austin Schuh76db3fa2020-03-07 17:02:44 -0800252
253 if (channel->name()->string_view().find(FLAGS_name) == std::string::npos) {
254 continue;
255 }
256
257 // Add a record to the stats vector.
258 channel_stats.push_back({channel});
259 // Lambda to read messages and parse for information
Brian Silvermanad1bce02020-03-05 14:23:01 -0800260 stats_event_loop->MakeRawNoArgWatcher(
261 channel,
262 [&logfile_stats, &channel_stats, it](const aos::Context &context) {
Austin Schuhc99e1392021-03-30 22:59:24 -0700263 channel_stats[it].Add(context);
264
265 // Update the overall logfile statistics
Brian Silvermanad1bce02020-03-05 14:23:01 -0800266 logfile_stats.logfile_length += context.size;
267 });
Austin Schuh76db3fa2020-03-07 17:02:44 -0800268 it++;
269 // TODO (Stephan): Frequency of messages per second
270 // - Sliding window
271 // - Max / Deviation
272 found_channel = true;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800273 }
274 if (!found_channel) {
275 LOG(FATAL) << "Could not find any channels";
276 }
277
278 log_reader_factory.Run();
279
milind upadhyay38fe3cd2021-03-27 15:30:53 -0700280 std::cout << std::endl;
Austin Schuhc99e1392021-03-30 22:59:24 -0700281
Stephan Massaltf84cf812019-12-31 14:14:50 -0800282 // Print out the stats per channel and for the logfile
283 for (size_t i = 0; i != channel_stats.size(); i++) {
Austin Schuhc99e1392021-03-30 22:59:24 -0700284 if (!FLAGS_excessive_size_only ||
285 (channel_stats[i].max_message_size() * 2) <
286 static_cast<size_t>(channel_stats[i].channel()->max_size())) {
287 if (channel_stats[i].total_num_messages() > 0) {
288 std::cout << channel_stats[i].channel()->name()->string_view() << " "
289 << channel_stats[i].channel()->type()->string_view() << "\n";
milind upadhyay38fe3cd2021-03-27 15:30:53 -0700290
Austin Schuhc99e1392021-03-30 22:59:24 -0700291 logfile_stats.total_log_messages +=
292 channel_stats[i].total_num_messages();
293 logfile_stats.logfile_end_time =
294 std::max(logfile_stats.logfile_end_time,
295 channel_stats[i].channel_end_time());
296
milind upadhyay38fe3cd2021-03-27 15:30:53 -0700297 if (!FLAGS_excessive_size_only) {
Austin Schuhc99e1392021-03-30 22:59:24 -0700298 std::cout << " " << channel_stats[i].total_num_messages()
299 << " msgs, " << channel_stats[i].avg_messages_per_sec()
300 << "hz avg, " << channel_stats[i].channel()->frequency()
301 << "hz max";
milind upadhyay38fe3cd2021-03-27 15:30:53 -0700302 }
Austin Schuhc99e1392021-03-30 22:59:24 -0700303 std::cout << " " << channel_stats[i].avg_message_size()
Austin Schuh60e77942022-05-16 17:48:24 -0700304 << " bytes avg, " << channel_stats[i].avg_message_bandwidth()
James Kuszmaul6abc7442021-10-24 13:46:04 -0700305 << " bytes/sec avg, " << channel_stats[i].max_message_size()
Austin Schuhc99e1392021-03-30 22:59:24 -0700306 << " bytes max / " << channel_stats[i].channel()->max_size()
307 << "bytes " << channel_stats[i].Percentile();
milind upadhyay38fe3cd2021-03-27 15:30:53 -0700308 std::cout << std::endl;
309 }
Stephan Massaltf84cf812019-12-31 14:14:50 -0800310 }
311 }
312 std::cout << std::setfill('-') << std::setw(80) << "-"
Ravago Jones8bab1842020-12-12 17:36:39 -0800313 << "\nLogfile statistics:\n"
Austin Schuh76db3fa2020-03-07 17:02:44 -0800314 << "Log starts at:\t" << reader.realtime_start_time(node) << "\n"
Stephan Massaltf84cf812019-12-31 14:14:50 -0800315 << "Log ends at:\t" << logfile_stats.logfile_end_time << "\n"
316 << "Log file size:\t" << logfile_stats.logfile_length << "\n"
317 << "Total messages:\t" << logfile_stats.total_log_messages << "\n";
318
319 // Cleanup the created processes
320 reader.Deregister();
Austin Schuhae87e312020-08-01 16:15:01 -0700321
Stephan Massaltf84cf812019-12-31 14:14:50 -0800322 return 0;
323}