blob: 5ec702589e0a10291287c46b76c263e2566b1edf [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;
21 for (const auto f : log_files) {
22 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) {
36 if (config == nullptr) {
37 config = log_file.config.get();
38 } else {
39 CHECK_EQ(config, log_file.config.get());
40 }
41 }
42
43 CHECK(configuration::MultiNode(config))
44 << ": Timestamps only make sense in a multi-node world.";
45
46 // Now, build up all the TimestampMapper classes to read and sort the data.
47 std::vector<std::unique_ptr<TimestampMapper>> mappers;
48
49 for (const Node *node : configuration::GetNodes(config)) {
50 std::vector<LogParts> filtered_parts =
51 FilterPartsForNode(log_files, node->name()->string_view());
52
53 // Confirm that all the parts are from the same boot if there are enough
54 // parts to not be from the same boot.
55 if (!filtered_parts.empty()) {
56 for (size_t i = 1; i < filtered_parts.size(); ++i) {
57 CHECK_EQ(filtered_parts[i].source_boot_uuid,
58 filtered_parts[0].source_boot_uuid)
59 << ": Found parts from different boots "
60 << LogFileVectorToString(log_files);
61 }
62
63 // Filter the parts relevant to each node when building the mapper.
64 mappers.emplace_back(
65 std::make_unique<TimestampMapper>(std::move(filtered_parts)));
66 } else {
67 mappers.emplace_back(nullptr);
68 }
69 }
70
71 // Now, build up the estimator used to solve for time.
72 message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator(
73 config, config, FLAGS_skip_order_validation, chrono::seconds(0));
74
75 // To make things more like the logger and faster, cache the node + channel ->
76 // filter mapping in a set of vectors.
77 std::vector<std::vector<message_bridge::NoncausalOffsetEstimator *>> filters;
78 filters.resize(configuration::NodesCount(config));
79
80 for (const Node *node : configuration::GetNodes(config)) {
81 const size_t node_index = configuration::GetNodeIndex(config, node);
82 filters[node_index].resize(config->channels()->size(), nullptr);
83 for (size_t channel_index = 0; channel_index < config->channels()->size();
84 ++channel_index) {
85 const Channel *channel = config->channels()->Get(channel_index);
86
87 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
88 configuration::ChannelIsReadableOnNode(channel, node)) {
89 // We've got a message which is being forwarded to this node.
90 const Node *source_node = configuration::GetNode(
91 config, channel->source_node()->string_view());
92 filters[node_index][channel_index] =
93 multinode_estimator.GetFilter(node, source_node);
94 }
95 }
96 }
97
98 // Now, read all the timestamps for each node. This is simpler than the
99 // logger on purpose. It loads in *all* the timestamps in 1 go per node,
100 // ignoring memory usage.
101 for (const Node *node : configuration::GetNodes(config)) {
102 LOG(INFO) << "Reading all data for " << node->name()->string_view();
103 const size_t node_index = configuration::GetNodeIndex(config, node);
104 TimestampMapper *timestamp_mapper = mappers[node_index].get();
105 if (timestamp_mapper == nullptr) {
106 continue;
107 }
108 while (true) {
109 TimestampedMessage *m = timestamp_mapper->Front();
110 if (m == nullptr) {
111 break;
112 }
113
114 if (m->monotonic_remote_time != monotonic_clock::min_time) {
115 // Got a forwarding timestamp!
116 message_bridge::NoncausalOffsetEstimator *filter =
117 filters[node_index][m->channel_index];
118 CHECK(filter != nullptr);
119
120 filter->Sample(node, m->monotonic_event_time, m->monotonic_remote_time);
121
122 // Call the correct method depending on if we are the forward or
123 // reverse direction here.
124 if (m->monotonic_timestamp_time != monotonic_clock::min_time) {
125 // TODO(austin): This assumes that this timestamp is only logged on
126 // the node which sent the data. That is correct for now, but should
127 // be explicitly checked somewhere.
128 filter->ReverseSample(node, m->monotonic_event_time,
129 m->monotonic_timestamp_time);
130 }
131 }
132 timestamp_mapper->PopFront();
133 }
134 }
135
136 // Don't get clever. Use the first time as the start time. Note: this is
137 // different than how log_cat and others work.
138 std::optional<std::tuple<distributed_clock::time_point,
139 std::vector<monotonic_clock::time_point>>>
140 next_timestamp = multinode_estimator.NextTimestamp();
141 CHECK(next_timestamp);
142 LOG(INFO) << "Starting at:";
143 for (const Node *node : configuration::GetNodes(config)) {
144 const size_t node_index = configuration::GetNodeIndex(config, node);
145 LOG(INFO) << " " << node->name()->string_view() << " -> "
146 << std::get<1>(*next_timestamp)[node_index];
147 }
148
149 multinode_estimator.Start(std::get<1>(*next_timestamp));
150
151 // As we pull off all the timestamps, the time problem is continually solved,
152 // filling in the CSV files.
153 while (true) {
154 std::optional<std::tuple<distributed_clock::time_point,
155 std::vector<monotonic_clock::time_point>>>
156 next_timestamp = multinode_estimator.NextTimestamp();
157 if (!next_timestamp) {
158 break;
159 }
160 }
161
162 return 0;
163}
164
165} // namespace aos::logger
166
167int main(int argc, char **argv) {
168 FLAGS_timestamps_to_csv = true;
169 gflags::SetUsageMessage(
170 "Usage:\n"
171 " timestamp_extractor [args] logfile1 logfile2 ...\n\nThis program "
172 "dumps out all the timestamps from a set of log files for plotting. Use "
173 "--skip_order_validation to skip any time estimation problems we find.");
174 aos::InitGoogle(&argc, &argv);
175
176 return aos::logger::Main(argc, argv);
177}