blob: f7f5a404f728812220c289dade5cc6f43aa43da7 [file] [log] [blame]
Austin Schuhba20ea72021-01-21 16:47:01 -08001#include <iostream>
2#include <string>
3#include <vector>
4
5#include "aos/events/logging/logfile_sorting.h"
6#include "aos/events/logging/logfile_utils.h"
7#include "aos/init.h"
8#include "aos/network/multinode_timestamp_filter.h"
9#include "gflags/gflags.h"
10
11DECLARE_bool(timestamps_to_csv);
12DEFINE_bool(skip_order_validation, false,
13 "If true, ignore any out of orderness in replay");
14
15namespace aos::logger {
16
17namespace chrono = std::chrono;
18
19std::string LogFileVectorToString(std::vector<logger::LogFile> log_files) {
20 std::stringstream ss;
Austin Schuh05dad8b2021-01-27 22:47:23 -080021 for (const auto &f : log_files) {
Austin Schuhba20ea72021-01-21 16:47:01 -080022 ss << f << "\n";
23 }
24 return ss.str();
25}
26
27int Main(int argc, char **argv) {
28 const std::vector<std::string> unsorted_logfiles = FindLogs(argc, argv);
29 const std::vector<LogFile> log_files = SortParts(unsorted_logfiles);
30
31 CHECK_GT(log_files.size(), 0u);
32 // Validate that we have the same config everwhere. This will be true if
33 // all the parts were sorted together and the configs match.
34 const Configuration *config = nullptr;
35 for (const LogFile &log_file : log_files) {
Austin Schuh3e20c692021-11-16 20:43:16 -080036 VLOG(1) << log_file;
Austin Schuhba20ea72021-01-21 16:47:01 -080037 if (config == nullptr) {
38 config = log_file.config.get();
39 } else {
40 CHECK_EQ(config, log_file.config.get());
41 }
42 }
43
44 CHECK(configuration::MultiNode(config))
45 << ": Timestamps only make sense in a multi-node world.";
46
47 // Now, build up all the TimestampMapper classes to read and sort the data.
48 std::vector<std::unique_ptr<TimestampMapper>> mappers;
49
50 for (const Node *node : configuration::GetNodes(config)) {
51 std::vector<LogParts> filtered_parts =
52 FilterPartsForNode(log_files, node->name()->string_view());
53
54 // Confirm that all the parts are from the same boot if there are enough
55 // parts to not be from the same boot.
56 if (!filtered_parts.empty()) {
Austin Schuhba20ea72021-01-21 16:47:01 -080057 // Filter the parts relevant to each node when building the mapper.
58 mappers.emplace_back(
59 std::make_unique<TimestampMapper>(std::move(filtered_parts)));
60 } else {
61 mappers.emplace_back(nullptr);
62 }
63 }
64
65 // Now, build up the estimator used to solve for time.
66 message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator(
Austin Schuh58646e22021-08-23 23:51:46 -070067 config, config, log_files[0].boots, FLAGS_skip_order_validation,
68 chrono::seconds(0));
69 multinode_estimator.set_reboot_found(
70 [config](distributed_clock::time_point reboot_time,
71 const std::vector<logger::BootTimestamp> &node_times) {
72 LOG(INFO) << "Rebooted at distributed " << reboot_time;
73 size_t node_index = 0;
74 for (const logger::BootTimestamp &time : node_times) {
75 LOG(INFO) << " "
76 << config->nodes()->Get(node_index)->name()->string_view()
77 << " " << time;
78 ++node_index;
79 }
80 });
Austin Schuhba20ea72021-01-21 16:47:01 -080081
Austin Schuhe639ea12021-01-25 13:00:22 -080082 {
83 std::vector<TimestampMapper *> timestamp_mappers;
84 for (std::unique_ptr<TimestampMapper> &mapper : mappers) {
85 timestamp_mappers.emplace_back(mapper.get());
86 }
87 multinode_estimator.SetTimestampMappers(std::move(timestamp_mappers));
88 }
89
Austin Schuhba20ea72021-01-21 16:47:01 -080090 // To make things more like the logger and faster, cache the node + channel ->
91 // filter mapping in a set of vectors.
92 std::vector<std::vector<message_bridge::NoncausalOffsetEstimator *>> filters;
93 filters.resize(configuration::NodesCount(config));
94
95 for (const Node *node : configuration::GetNodes(config)) {
96 const size_t node_index = configuration::GetNodeIndex(config, node);
97 filters[node_index].resize(config->channels()->size(), nullptr);
98 for (size_t channel_index = 0; channel_index < config->channels()->size();
99 ++channel_index) {
100 const Channel *channel = config->channels()->Get(channel_index);
101
102 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
103 configuration::ChannelIsReadableOnNode(channel, node)) {
104 // We've got a message which is being forwarded to this node.
105 const Node *source_node = configuration::GetNode(
106 config, channel->source_node()->string_view());
107 filters[node_index][channel_index] =
108 multinode_estimator.GetFilter(node, source_node);
109 }
110 }
111 }
112
Austin Schuhe639ea12021-01-25 13:00:22 -0800113 multinode_estimator.CheckGraph();
114
Austin Schuhba20ea72021-01-21 16:47:01 -0800115 // Now, read all the timestamps for each node. This is simpler than the
116 // logger on purpose. It loads in *all* the timestamps in 1 go per node,
117 // ignoring memory usage.
118 for (const Node *node : configuration::GetNodes(config)) {
119 LOG(INFO) << "Reading all data for " << node->name()->string_view();
120 const size_t node_index = configuration::GetNodeIndex(config, node);
121 TimestampMapper *timestamp_mapper = mappers[node_index].get();
122 if (timestamp_mapper == nullptr) {
123 continue;
124 }
125 while (true) {
126 TimestampedMessage *m = timestamp_mapper->Front();
127 if (m == nullptr) {
128 break;
129 }
Austin Schuhba20ea72021-01-21 16:47:01 -0800130 timestamp_mapper->PopFront();
131 }
132 }
133
134 // Don't get clever. Use the first time as the start time. Note: this is
135 // different than how log_cat and others work.
Austin Schuhdee8ea72021-08-20 19:13:41 -0700136 std::optional<const std::tuple<distributed_clock::time_point,
137 std::vector<BootTimestamp>> *>
138 next_timestamp = multinode_estimator.QueueNextTimestamp();
Austin Schuhba20ea72021-01-21 16:47:01 -0800139 CHECK(next_timestamp);
140 LOG(INFO) << "Starting at:";
141 for (const Node *node : configuration::GetNodes(config)) {
142 const size_t node_index = configuration::GetNodeIndex(config, node);
143 LOG(INFO) << " " << node->name()->string_view() << " -> "
Austin Schuhdee8ea72021-08-20 19:13:41 -0700144 << std::get<1>(*next_timestamp.value())[node_index].time;
Austin Schuhba20ea72021-01-21 16:47:01 -0800145 }
146
Austin Schuh66168842021-08-17 19:42:21 -0700147 std::vector<monotonic_clock::time_point> just_monotonic(
Austin Schuhdee8ea72021-08-20 19:13:41 -0700148 std::get<1>(*next_timestamp.value()).size());
Austin Schuh66168842021-08-17 19:42:21 -0700149 for (size_t i = 0; i < just_monotonic.size(); ++i) {
Austin Schuhdee8ea72021-08-20 19:13:41 -0700150 CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u);
151 just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time;
Austin Schuh66168842021-08-17 19:42:21 -0700152 }
153 multinode_estimator.Start(just_monotonic);
Austin Schuhba20ea72021-01-21 16:47:01 -0800154
155 // As we pull off all the timestamps, the time problem is continually solved,
156 // filling in the CSV files.
157 while (true) {
Austin Schuhdee8ea72021-08-20 19:13:41 -0700158 std::optional<const std::tuple<distributed_clock::time_point,
159 std::vector<BootTimestamp>> *>
160 next_timestamp = multinode_estimator.QueueNextTimestamp();
Austin Schuhba20ea72021-01-21 16:47:01 -0800161 if (!next_timestamp) {
162 break;
163 }
164 }
165
Austin Schuhdee8ea72021-08-20 19:13:41 -0700166 LOG(INFO) << "Done";
167
Austin Schuhba20ea72021-01-21 16:47:01 -0800168 return 0;
169}
170
171} // namespace aos::logger
172
173int main(int argc, char **argv) {
174 FLAGS_timestamps_to_csv = true;
175 gflags::SetUsageMessage(
176 "Usage:\n"
177 " timestamp_extractor [args] logfile1 logfile2 ...\n\nThis program "
178 "dumps out all the timestamps from a set of log files for plotting. Use "
179 "--skip_order_validation to skip any time estimation problems we find.");
180 aos::InitGoogle(&argc, &argv);
181
182 return aos::logger::Main(argc, argv);
183}