blob: a2d818406d2123711f97ca453e9f5eb63b5fdad7 [file] [log] [blame]
Stephan Massaltf84cf812019-12-31 14:14:50 -08001#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
Stephan Massaltf84cf812019-12-31 14:14:50 -080011DEFINE_string(
12 name, "",
13 "Name to match for printing out channels. Empty means no name filter.");
14
Austin Schuh6f3babe2020-01-26 20:34:50 -080015DEFINE_string(node, "", "Node to print stats out for.");
16
Stephan Massaltf84cf812019-12-31 14:14:50 -080017// define struct to hold all information
18struct ChannelStats {
19 // pointer to the channel for which stats are collected
20 const aos::Channel *channel;
21 aos::realtime_clock::time_point channel_end_time =
22 aos::realtime_clock::min_time;
23 aos::monotonic_clock::time_point first_message_time =
24 // needs to be higher than time in the logfile!
25 aos::monotonic_clock::max_time;
26 aos::monotonic_clock::time_point current_message_time =
27 aos::monotonic_clock::min_time;
28 // channel stats to collect per channel
29 int total_num_messages = 0;
30 size_t max_message_size = 0;
31 size_t total_message_size = 0;
32 double avg_messages_sec = 0.0; // TODO in Lambda, now in stats overview.
33 double max_messages_sec = 0.0; // TODO in Lambda
34};
35
36struct LogfileStats {
37 // All relevant stats on to logfile level
38 size_t logfile_length = 0;
39 int total_log_messages = 0;
40 aos::realtime_clock::time_point logfile_end_time =
41 aos::realtime_clock::min_time;
42};
43
44int main(int argc, char **argv) {
45 gflags::SetUsageMessage(
Ravago Jones8bab1842020-12-12 17:36:39 -080046 "Usage: \n"
47 " log_stats [args] logfile1 logfile2 ...\n"
Stephan Massaltf84cf812019-12-31 14:14:50 -080048 "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
Ravago Jones8bab1842020-12-12 17:36:39 -080060 if (argc < 2) {
61 LOG(FATAL) << "Expected at least 1 logfile as an argument.";
62 }
63
64 // find logfiles
65 std::vector<std::string> unsorted_logfiles =
66 aos::logger::FindLogs(argc, argv);
67
68 // sort logfiles
69 const std::vector<aos::logger::LogFile> logfiles =
70 aos::logger::SortParts(unsorted_logfiles);
71
72 // open logfiles
73 aos::logger::LogReader reader(logfiles);
74
Stephan Massaltf84cf812019-12-31 14:14:50 -080075 LogfileStats logfile_stats;
76 std::vector<ChannelStats> channel_stats;
77
Stephan Massaltf84cf812019-12-31 14:14:50 -080078 aos::SimulatedEventLoopFactory log_reader_factory(reader.configuration());
79 reader.Register(&log_reader_factory);
80
Austin Schuh6f3babe2020-01-26 20:34:50 -080081 const aos::Node *node = nullptr;
82
83 if (aos::configuration::MultiNode(reader.configuration())) {
84 if (FLAGS_node.empty()) {
85 LOG(INFO) << "Need a --node specified. The log file has:";
86 for (const aos::Node *node : reader.Nodes()) {
87 LOG(INFO) << " " << node->name()->string_view();
88 }
Austin Schuh8c7f14b2021-01-21 19:01:54 -080089 reader.Deregister();
Austin Schuh6f3babe2020-01-26 20:34:50 -080090 return 1;
91 } else {
92 node = aos::configuration::GetNode(reader.configuration(), FLAGS_node);
93 }
94 }
95
Stephan Massaltf84cf812019-12-31 14:14:50 -080096 // Make an eventloop for retrieving stats
97 std::unique_ptr<aos::EventLoop> stats_event_loop =
Austin Schuh6f3babe2020-01-26 20:34:50 -080098 log_reader_factory.MakeEventLoop("logstats", node);
Stephan Massaltf84cf812019-12-31 14:14:50 -080099 stats_event_loop->SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800100 stats_event_loop->SkipAosLog();
Stephan Massaltf84cf812019-12-31 14:14:50 -0800101
102 // Read channel info and store in vector
103 bool found_channel = false;
104 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
105 reader.configuration()->channels();
106
107 int it = 0; // iterate through the channel_stats
108 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
109 const aos::Channel *channel = channels->Get(i);
Austin Schuh76db3fa2020-03-07 17:02:44 -0800110 if (!aos::configuration::ChannelIsReadableOnNode(
111 channel, stats_event_loop->node())) {
112 continue;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800113 }
Austin Schuh76db3fa2020-03-07 17:02:44 -0800114
115 if (channel->name()->string_view().find(FLAGS_name) == std::string::npos) {
116 continue;
117 }
118
119 // Add a record to the stats vector.
120 channel_stats.push_back({channel});
121 // Lambda to read messages and parse for information
Brian Silvermanad1bce02020-03-05 14:23:01 -0800122 stats_event_loop->MakeRawNoArgWatcher(
123 channel,
124 [&logfile_stats, &channel_stats, it](const aos::Context &context) {
125 channel_stats[it].max_message_size =
126 std::max(channel_stats[it].max_message_size, context.size);
127 channel_stats[it].total_message_size += context.size;
128 channel_stats[it].total_num_messages++;
129 // asume messages are send in sequence per channel
130 channel_stats[it].channel_end_time = context.realtime_event_time;
131 channel_stats[it].first_message_time =
132 std::min(channel_stats[it].first_message_time,
133 context.monotonic_event_time);
134 channel_stats[it].current_message_time = context.monotonic_event_time;
135 // update the overall logfile statistics
136 logfile_stats.logfile_length += context.size;
137 });
Austin Schuh76db3fa2020-03-07 17:02:44 -0800138 it++;
139 // TODO (Stephan): Frequency of messages per second
140 // - Sliding window
141 // - Max / Deviation
142 found_channel = true;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800143 }
144 if (!found_channel) {
145 LOG(FATAL) << "Could not find any channels";
146 }
147
148 log_reader_factory.Run();
149
150 // Print out the stats per channel and for the logfile
151 for (size_t i = 0; i != channel_stats.size(); i++) {
152 if (channel_stats[i].total_num_messages > 0) {
153 double sec_active =
154 aos::time::DurationInSeconds(channel_stats[i].current_message_time -
155 channel_stats[i].first_message_time);
156 channel_stats[i].avg_messages_sec =
157 (channel_stats[i].total_num_messages / sec_active);
158 logfile_stats.total_log_messages += channel_stats[i].total_num_messages;
159 logfile_stats.logfile_end_time = std::max(
160 logfile_stats.logfile_end_time, channel_stats[i].channel_end_time);
161 std::cout << "Channel name: "
162 << channel_stats[i].channel->name()->string_view()
163 << "\tMsg type: "
164 << channel_stats[i].channel->type()->string_view() << "\n"
165 << "Number of msg: " << channel_stats[i].total_num_messages
166 << std::setprecision(3) << std::fixed
167 << "\tAvg msg per sec: " << channel_stats[i].avg_messages_sec
168 << "\tSet max msg frequency: "
169 << channel_stats[i].channel->frequency() << "\n"
170 << "Avg msg size: "
171 << (channel_stats[i].total_message_size /
172 channel_stats[i].total_num_messages)
173 << "\tMax msg size: " << channel_stats[i].max_message_size
174 << "\tSet max msg size: "
175 << channel_stats[i].channel->max_size() << "\n"
176 << "First msg time: " << channel_stats[i].first_message_time
177 << "\tLast msg time: " << channel_stats[i].current_message_time
178 << "\tSeconds active: " << sec_active << "sec\n";
179 } else {
180 std::cout << "Channel name: "
181 << channel_stats[i].channel->name()->string_view() << "\t"
182 << "Msg type: "
183 << channel_stats[i].channel->type()->string_view() << "\n"
184 << "Set max msg frequency: "
185 << channel_stats[i].channel->frequency() << "\t"
186 << "Set max msg size: " << channel_stats[i].channel->max_size()
187 << "\n--- No messages in channel ---"
188 << "\n";
189 }
190 }
191 std::cout << std::setfill('-') << std::setw(80) << "-"
Ravago Jones8bab1842020-12-12 17:36:39 -0800192 << "\nLogfile statistics:\n"
Austin Schuh76db3fa2020-03-07 17:02:44 -0800193 << "Log starts at:\t" << reader.realtime_start_time(node) << "\n"
Stephan Massaltf84cf812019-12-31 14:14:50 -0800194 << "Log ends at:\t" << logfile_stats.logfile_end_time << "\n"
195 << "Log file size:\t" << logfile_stats.logfile_length << "\n"
196 << "Total messages:\t" << logfile_stats.total_log_messages << "\n";
197
198 // Cleanup the created processes
199 reader.Deregister();
Austin Schuhae87e312020-08-01 16:15:01 -0700200
Stephan Massaltf84cf812019-12-31 14:14:50 -0800201 return 0;
202}