blob: 47f3c5c9111db9301237148808636d3f6e5a9f7f [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 }
89 return 1;
90 } else {
91 node = aos::configuration::GetNode(reader.configuration(), FLAGS_node);
92 }
93 }
94
Stephan Massaltf84cf812019-12-31 14:14:50 -080095 // Make an eventloop for retrieving stats
96 std::unique_ptr<aos::EventLoop> stats_event_loop =
Austin Schuh6f3babe2020-01-26 20:34:50 -080097 log_reader_factory.MakeEventLoop("logstats", node);
Stephan Massaltf84cf812019-12-31 14:14:50 -080098 stats_event_loop->SkipTimingReport();
Tyler Chatow67ddb032020-01-12 14:30:04 -080099 stats_event_loop->SkipAosLog();
Stephan Massaltf84cf812019-12-31 14:14:50 -0800100
101 // Read channel info and store in vector
102 bool found_channel = false;
103 const flatbuffers::Vector<flatbuffers::Offset<aos::Channel>> *channels =
104 reader.configuration()->channels();
105
106 int it = 0; // iterate through the channel_stats
107 for (flatbuffers::uoffset_t i = 0; i < channels->size(); i++) {
108 const aos::Channel *channel = channels->Get(i);
Austin Schuh76db3fa2020-03-07 17:02:44 -0800109 if (!aos::configuration::ChannelIsReadableOnNode(
110 channel, stats_event_loop->node())) {
111 continue;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800112 }
Austin Schuh76db3fa2020-03-07 17:02:44 -0800113
114 if (channel->name()->string_view().find(FLAGS_name) == std::string::npos) {
115 continue;
116 }
117
118 // Add a record to the stats vector.
119 channel_stats.push_back({channel});
120 // Lambda to read messages and parse for information
Brian Silvermanad1bce02020-03-05 14:23:01 -0800121 stats_event_loop->MakeRawNoArgWatcher(
122 channel,
123 [&logfile_stats, &channel_stats, it](const aos::Context &context) {
124 channel_stats[it].max_message_size =
125 std::max(channel_stats[it].max_message_size, context.size);
126 channel_stats[it].total_message_size += context.size;
127 channel_stats[it].total_num_messages++;
128 // asume messages are send in sequence per channel
129 channel_stats[it].channel_end_time = context.realtime_event_time;
130 channel_stats[it].first_message_time =
131 std::min(channel_stats[it].first_message_time,
132 context.monotonic_event_time);
133 channel_stats[it].current_message_time = context.monotonic_event_time;
134 // update the overall logfile statistics
135 logfile_stats.logfile_length += context.size;
136 });
Austin Schuh76db3fa2020-03-07 17:02:44 -0800137 it++;
138 // TODO (Stephan): Frequency of messages per second
139 // - Sliding window
140 // - Max / Deviation
141 found_channel = true;
Stephan Massaltf84cf812019-12-31 14:14:50 -0800142 }
143 if (!found_channel) {
144 LOG(FATAL) << "Could not find any channels";
145 }
146
147 log_reader_factory.Run();
148
149 // Print out the stats per channel and for the logfile
150 for (size_t i = 0; i != channel_stats.size(); i++) {
151 if (channel_stats[i].total_num_messages > 0) {
152 double sec_active =
153 aos::time::DurationInSeconds(channel_stats[i].current_message_time -
154 channel_stats[i].first_message_time);
155 channel_stats[i].avg_messages_sec =
156 (channel_stats[i].total_num_messages / sec_active);
157 logfile_stats.total_log_messages += channel_stats[i].total_num_messages;
158 logfile_stats.logfile_end_time = std::max(
159 logfile_stats.logfile_end_time, channel_stats[i].channel_end_time);
160 std::cout << "Channel name: "
161 << channel_stats[i].channel->name()->string_view()
162 << "\tMsg type: "
163 << channel_stats[i].channel->type()->string_view() << "\n"
164 << "Number of msg: " << channel_stats[i].total_num_messages
165 << std::setprecision(3) << std::fixed
166 << "\tAvg msg per sec: " << channel_stats[i].avg_messages_sec
167 << "\tSet max msg frequency: "
168 << channel_stats[i].channel->frequency() << "\n"
169 << "Avg msg size: "
170 << (channel_stats[i].total_message_size /
171 channel_stats[i].total_num_messages)
172 << "\tMax msg size: " << channel_stats[i].max_message_size
173 << "\tSet max msg size: "
174 << channel_stats[i].channel->max_size() << "\n"
175 << "First msg time: " << channel_stats[i].first_message_time
176 << "\tLast msg time: " << channel_stats[i].current_message_time
177 << "\tSeconds active: " << sec_active << "sec\n";
178 } else {
179 std::cout << "Channel name: "
180 << channel_stats[i].channel->name()->string_view() << "\t"
181 << "Msg type: "
182 << channel_stats[i].channel->type()->string_view() << "\n"
183 << "Set max msg frequency: "
184 << channel_stats[i].channel->frequency() << "\t"
185 << "Set max msg size: " << channel_stats[i].channel->max_size()
186 << "\n--- No messages in channel ---"
187 << "\n";
188 }
189 }
190 std::cout << std::setfill('-') << std::setw(80) << "-"
Ravago Jones8bab1842020-12-12 17:36:39 -0800191 << "\nLogfile statistics:\n"
Austin Schuh76db3fa2020-03-07 17:02:44 -0800192 << "Log starts at:\t" << reader.realtime_start_time(node) << "\n"
Stephan Massaltf84cf812019-12-31 14:14:50 -0800193 << "Log ends at:\t" << logfile_stats.logfile_end_time << "\n"
194 << "Log file size:\t" << logfile_stats.logfile_length << "\n"
195 << "Total messages:\t" << logfile_stats.total_log_messages << "\n";
196
197 // Cleanup the created processes
198 reader.Deregister();
Austin Schuhae87e312020-08-01 16:15:01 -0700199
Stephan Massaltf84cf812019-12-31 14:14:50 -0800200 return 0;
201}