Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 1 | #include <iomanip> |
| 2 | #include <iostream> |
| 3 | |
| 4 | #include "aos/events/logging/logger.h" |
| 5 | #include "aos/events/simulated_event_loop.h" |
| 6 | #include "aos/init.h" |
| 7 | #include "aos/json_to_flatbuffer.h" |
| 8 | #include "aos/time/time.h" |
| 9 | #include "gflags/gflags.h" |
| 10 | |
| 11 | DEFINE_string(logfile, "/tmp/logfile.bfbs", |
| 12 | "Name and path of the logfile to read from."); |
| 13 | DEFINE_string( |
| 14 | name, "", |
| 15 | "Name to match for printing out channels. Empty means no name filter."); |
| 16 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 17 | DEFINE_string(node, "", "Node to print stats out for."); |
| 18 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 19 | // define struct to hold all information |
| 20 | struct ChannelStats { |
| 21 | // pointer to the channel for which stats are collected |
| 22 | const aos::Channel *channel; |
| 23 | aos::realtime_clock::time_point channel_end_time = |
| 24 | aos::realtime_clock::min_time; |
| 25 | aos::monotonic_clock::time_point first_message_time = |
| 26 | // needs to be higher than time in the logfile! |
| 27 | aos::monotonic_clock::max_time; |
| 28 | aos::monotonic_clock::time_point current_message_time = |
| 29 | aos::monotonic_clock::min_time; |
| 30 | // channel stats to collect per channel |
| 31 | int total_num_messages = 0; |
| 32 | size_t max_message_size = 0; |
| 33 | size_t total_message_size = 0; |
| 34 | double avg_messages_sec = 0.0; // TODO in Lambda, now in stats overview. |
| 35 | double max_messages_sec = 0.0; // TODO in Lambda |
| 36 | }; |
| 37 | |
| 38 | struct LogfileStats { |
| 39 | // All relevant stats on to logfile level |
| 40 | size_t logfile_length = 0; |
| 41 | int total_log_messages = 0; |
| 42 | aos::realtime_clock::time_point logfile_end_time = |
| 43 | aos::realtime_clock::min_time; |
| 44 | }; |
| 45 | |
| 46 | int main(int argc, char **argv) { |
| 47 | gflags::SetUsageMessage( |
| 48 | "This program provides statistics on a given log file. Supported " |
| 49 | "statistics are:\n" |
| 50 | " - Logfile start time;\n" |
| 51 | " - Total messages per channel/type;\n" |
| 52 | " - Max message size per channel/type;\n" |
| 53 | " - Frequency of messages per second;\n" |
| 54 | " - Total logfile size and number of messages.\n" |
| 55 | "Use --logfile flag to select a logfile (path/filename) and use --name " |
| 56 | "flag to specify a channel to listen on."); |
| 57 | |
| 58 | aos::InitGoogle(&argc, &argv); |
| 59 | |
| 60 | LogfileStats logfile_stats; |
| 61 | std::vector<ChannelStats> channel_stats; |
| 62 | |
| 63 | // Open LogFile |
| 64 | aos::logger::LogReader reader(FLAGS_logfile); |
| 65 | aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
| 66 | reader.Register(&log_reader_factory); |
| 67 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 68 | const aos::Node *node = nullptr; |
| 69 | |
| 70 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 71 | if (FLAGS_node.empty()) { |
| 72 | LOG(INFO) << "Need a --node specified. The log file has:"; |
| 73 | for (const aos::Node *node : reader.Nodes()) { |
| 74 | LOG(INFO) << " " << node->name()->string_view(); |
| 75 | } |
| 76 | return 1; |
| 77 | } else { |
| 78 | node = aos::configuration::GetNode(reader.configuration(), FLAGS_node); |
| 79 | } |
| 80 | } |
| 81 | |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 82 | // Make an eventloop for retrieving stats |
| 83 | std::unique_ptr<aos::EventLoop> stats_event_loop = |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 84 | log_reader_factory.MakeEventLoop("logstats", node); |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 85 | stats_event_loop->SkipTimingReport(); |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 86 | stats_event_loop->SkipAosLog(); |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 87 | |
| 88 | // Read channel info and store in vector |
| 89 | bool found_channel = false; |
| 90 | const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels = |
| 91 | reader.configuration()->channels(); |
| 92 | |
| 93 | int it = 0; // iterate through the channel_stats |
| 94 | for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) { |
| 95 | const aos::Channel *channel = channels->Get(i); |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame^] | 96 | if (!aos::configuration::ChannelIsReadableOnNode( |
| 97 | channel, stats_event_loop->node())) { |
| 98 | continue; |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 99 | } |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame^] | 100 | |
| 101 | if (channel->name()->string_view().find(FLAGS_name) == std::string::npos) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | // Add a record to the stats vector. |
| 106 | channel_stats.push_back({channel}); |
| 107 | // Lambda to read messages and parse for information |
| 108 | stats_event_loop->MakeRawWatcher(channel, [&logfile_stats, &channel_stats, |
| 109 | it](const aos::Context &context, |
| 110 | const void * /* message */) { |
| 111 | channel_stats[it].max_message_size = |
| 112 | std::max(channel_stats[it].max_message_size, context.size); |
| 113 | channel_stats[it].total_message_size += context.size; |
| 114 | channel_stats[it].total_num_messages++; |
| 115 | // asume messages are send in sequence per channel |
| 116 | channel_stats[it].channel_end_time = context.realtime_event_time; |
| 117 | channel_stats[it].first_message_time = std::min( |
| 118 | channel_stats[it].first_message_time, context.monotonic_event_time); |
| 119 | channel_stats[it].current_message_time = context.monotonic_event_time; |
| 120 | // update the overall logfile statistics |
| 121 | logfile_stats.logfile_length += context.size; |
| 122 | }); |
| 123 | it++; |
| 124 | // TODO (Stephan): Frequency of messages per second |
| 125 | // - Sliding window |
| 126 | // - Max / Deviation |
| 127 | found_channel = true; |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 128 | } |
| 129 | if (!found_channel) { |
| 130 | LOG(FATAL) << "Could not find any channels"; |
| 131 | } |
| 132 | |
| 133 | log_reader_factory.Run(); |
| 134 | |
| 135 | // Print out the stats per channel and for the logfile |
| 136 | for (size_t i = 0; i != channel_stats.size(); i++) { |
| 137 | if (channel_stats[i].total_num_messages > 0) { |
| 138 | double sec_active = |
| 139 | aos::time::DurationInSeconds(channel_stats[i].current_message_time - |
| 140 | channel_stats[i].first_message_time); |
| 141 | channel_stats[i].avg_messages_sec = |
| 142 | (channel_stats[i].total_num_messages / sec_active); |
| 143 | logfile_stats.total_log_messages += channel_stats[i].total_num_messages; |
| 144 | logfile_stats.logfile_end_time = std::max( |
| 145 | logfile_stats.logfile_end_time, channel_stats[i].channel_end_time); |
| 146 | std::cout << "Channel name: " |
| 147 | << channel_stats[i].channel->name()->string_view() |
| 148 | << "\tMsg type: " |
| 149 | << channel_stats[i].channel->type()->string_view() << "\n" |
| 150 | << "Number of msg: " << channel_stats[i].total_num_messages |
| 151 | << std::setprecision(3) << std::fixed |
| 152 | << "\tAvg msg per sec: " << channel_stats[i].avg_messages_sec |
| 153 | << "\tSet max msg frequency: " |
| 154 | << channel_stats[i].channel->frequency() << "\n" |
| 155 | << "Avg msg size: " |
| 156 | << (channel_stats[i].total_message_size / |
| 157 | channel_stats[i].total_num_messages) |
| 158 | << "\tMax msg size: " << channel_stats[i].max_message_size |
| 159 | << "\tSet max msg size: " |
| 160 | << channel_stats[i].channel->max_size() << "\n" |
| 161 | << "First msg time: " << channel_stats[i].first_message_time |
| 162 | << "\tLast msg time: " << channel_stats[i].current_message_time |
| 163 | << "\tSeconds active: " << sec_active << "sec\n"; |
| 164 | } else { |
| 165 | std::cout << "Channel name: " |
| 166 | << channel_stats[i].channel->name()->string_view() << "\t" |
| 167 | << "Msg type: " |
| 168 | << channel_stats[i].channel->type()->string_view() << "\n" |
| 169 | << "Set max msg frequency: " |
| 170 | << channel_stats[i].channel->frequency() << "\t" |
| 171 | << "Set max msg size: " << channel_stats[i].channel->max_size() |
| 172 | << "\n--- No messages in channel ---" |
| 173 | << "\n"; |
| 174 | } |
| 175 | } |
| 176 | std::cout << std::setfill('-') << std::setw(80) << "-" |
| 177 | << "\nLogfile statistics for: " << FLAGS_logfile << "\n" |
Austin Schuh | 76db3fa | 2020-03-07 17:02:44 -0800 | [diff] [blame^] | 178 | << "Log starts at:\t" << reader.realtime_start_time(node) << "\n" |
Stephan Massalt | f84cf81 | 2019-12-31 14:14:50 -0800 | [diff] [blame] | 179 | << "Log ends at:\t" << logfile_stats.logfile_end_time << "\n" |
| 180 | << "Log file size:\t" << logfile_stats.logfile_length << "\n" |
| 181 | << "Total messages:\t" << logfile_stats.total_log_messages << "\n"; |
| 182 | |
| 183 | // Cleanup the created processes |
| 184 | reader.Deregister(); |
| 185 | aos::Cleanup(); |
| 186 | return 0; |
| 187 | } |