Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 1 | #include <iomanip> |
| 2 | #include <iostream> |
| 3 | |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 4 | #include "absl/strings/str_format.h" |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 5 | #include "aos/events/logging/log_reader.h" |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 6 | #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 Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 12 | DEFINE_string( |
| 13 | name, "", |
| 14 | "Name to match for printing out channels. Empty means no name filter."); |
| 15 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 16 | DEFINE_string(node, "", "Node to print stats out for."); |
| 17 | |
milind upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 18 | DEFINE_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 Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 22 | // This class implements a histogram for tracking message period percentiles. |
| 23 | class 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 | |
| 109 | class 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 Kuszmaul | 6abc744 | 2021-10-24 13:46:04 -0700 | [diff] [blame] | 145 | size_t avg_message_bandwidth() const { |
| 146 | return total_message_size_ / SecondsActive(); |
| 147 | } |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 148 | |
| 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 Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 156 | // pointer to the channel for which stats are collected |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 157 | const aos::Channel *channel_; |
| 158 | aos::realtime_clock::time_point channel_end_time_ = |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 159 | aos::realtime_clock::min_time; |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 160 | aos::monotonic_clock::time_point first_message_time_ = |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 161 | // needs to be higher than time in the logfile! |
| 162 | aos::monotonic_clock::max_time; |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 163 | aos::monotonic_clock::time_point current_message_time_ = |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 164 | aos::monotonic_clock::min_time; |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 165 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 166 | // channel stats to collect per channel |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 167 | int total_num_messages_ = 0; |
| 168 | size_t max_message_size_ = 0; |
| 169 | size_t total_message_size_ = 0; |
| 170 | |
| 171 | Histogram histogram_; |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | struct 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 | |
| 182 | int main(int argc, char **argv) { |
| 183 | gflags::SetUsageMessage( |
Ravago Jones | 8bab184 | 2020-12-12 17:36:39 -0800 | [diff] [blame] | 184 | "Usage: \n" |
| 185 | " log_stats [args] logfile1 logfile2 ...\n" |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 186 | "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 Jones | 8bab184 | 2020-12-12 17:36:39 -0800 | [diff] [blame] | 198 | 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 Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 213 | LogfileStats logfile_stats; |
| 214 | std::vector<ChannelStats> channel_stats; |
| 215 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 216 | aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
| 217 | reader.Register(&log_reader_factory); |
| 218 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 219 | 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 Schuh | 0767662 | 2021-01-21 18:59:17 -0800 | [diff] [blame] | 224 | for (const aos::Node *node : reader.LoggedNodes()) { |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 225 | LOG(INFO) << " " << node->name()->string_view(); |
| 226 | } |
Austin Schuh | 8c7f14b | 2021-01-21 19:01:54 -0800 | [diff] [blame] | 227 | reader.Deregister(); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 228 | return 1; |
| 229 | } else { |
| 230 | node = aos::configuration::GetNode(reader.configuration(), FLAGS_node); |
| 231 | } |
| 232 | } |
| 233 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 234 | // Make an eventloop for retrieving stats |
| 235 | std::unique_ptr<aos::EventLoop> stats_event_loop = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 236 | log_reader_factory.MakeEventLoop("logstats", node); |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 237 | stats_event_loop->SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 238 | stats_event_loop->SkipAosLog(); |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 239 | |
| 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 Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame] | 248 | if (!aos::configuration::ChannelIsReadableOnNode( |
| 249 | channel, stats_event_loop->node())) { |
| 250 | continue; |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 251 | } |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame] | 252 | |
| 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 Silverman | ad1bce0 | 2020-03-05 14:23:01 -0800 | [diff] [blame] | 260 | stats_event_loop->MakeRawNoArgWatcher( |
| 261 | channel, |
| 262 | [&logfile_stats, &channel_stats, it](const aos::Context &context) { |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 263 | channel_stats[it].Add(context); |
| 264 | |
| 265 | // Update the overall logfile statistics |
Brian Silverman | ad1bce0 | 2020-03-05 14:23:01 -0800 | [diff] [blame] | 266 | logfile_stats.logfile_length += context.size; |
| 267 | }); |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame] | 268 | it++; |
| 269 | // TODO (Stephan): Frequency of messages per second |
| 270 | // - Sliding window |
| 271 | // - Max / Deviation |
| 272 | found_channel = true; |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 273 | } |
| 274 | if (!found_channel) { |
| 275 | LOG(FATAL) << "Could not find any channels"; |
| 276 | } |
| 277 | |
| 278 | log_reader_factory.Run(); |
| 279 | |
milind upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 280 | std::cout << std::endl; |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 281 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 282 | // Print out the stats per channel and for the logfile |
| 283 | for (size_t i = 0; i != channel_stats.size(); i++) { |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 284 | 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 upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 290 | |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 291 | 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 upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 297 | if (!FLAGS_excessive_size_only) { |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 298 | 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 upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 302 | } |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 303 | std::cout << " " << channel_stats[i].avg_message_size() |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame^] | 304 | << " bytes avg, " << channel_stats[i].avg_message_bandwidth() |
James Kuszmaul | 6abc744 | 2021-10-24 13:46:04 -0700 | [diff] [blame] | 305 | << " bytes/sec avg, " << channel_stats[i].max_message_size() |
Austin Schuh | c99e139 | 2021-03-30 22:59:24 -0700 | [diff] [blame] | 306 | << " bytes max / " << channel_stats[i].channel()->max_size() |
| 307 | << "bytes " << channel_stats[i].Percentile(); |
milind upadhyay | 38fe3cd | 2021-03-27 15:30:53 -0700 | [diff] [blame] | 308 | std::cout << std::endl; |
| 309 | } |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | std::cout << std::setfill('-') << std::setw(80) << "-" |
Ravago Jones | 8bab184 | 2020-12-12 17:36:39 -0800 | [diff] [blame] | 313 | << "\nLogfile statistics:\n" |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame] | 314 | << "Log starts at:\t" << reader.realtime_start_time(node) << "\n" |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 315 | << "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 Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 321 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 322 | return 0; |
| 323 | } |