blob: 72f9de87bd8a322e983a683475eff758ab0d889a [file] [log] [blame]
Austin Schuhba20ea72021-01-21 16:47:01 -08001#include <iostream>
2#include <string>
3#include <vector>
4
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "gflags/gflags.h"
6
Austin Schuhba20ea72021-01-21 16:47:01 -08007#include "aos/events/logging/logfile_sorting.h"
8#include "aos/events/logging/logfile_utils.h"
9#include "aos/init.h"
10#include "aos/network/multinode_timestamp_filter.h"
Austin Schuhba20ea72021-01-21 16:47:01 -080011
12DECLARE_bool(timestamps_to_csv);
13DEFINE_bool(skip_order_validation, false,
14 "If true, ignore any out of orderness in replay");
15
16namespace aos::logger {
17
18namespace chrono = std::chrono;
19
Austin Schuhba20ea72021-01-21 16:47:01 -080020int Main(int argc, char **argv) {
21 const std::vector<std::string> unsorted_logfiles = FindLogs(argc, argv);
Alexei Strots1f51ac72023-05-15 10:14:54 -070022 const LogFilesContainer log_files(SortParts(unsorted_logfiles));
23 const Configuration *config = log_files.config();
Austin Schuhba20ea72021-01-21 16:47:01 -080024
25 CHECK(configuration::MultiNode(config))
26 << ": Timestamps only make sense in a multi-node world.";
27
28 // Now, build up all the TimestampMapper classes to read and sort the data.
29 std::vector<std::unique_ptr<TimestampMapper>> mappers;
30
31 for (const Node *node : configuration::GetNodes(config)) {
Alexei Strots1f51ac72023-05-15 10:14:54 -070032 auto node_name = MaybeNodeName(node);
Austin Schuhba20ea72021-01-21 16:47:01 -080033 // Confirm that all the parts are from the same boot if there are enough
34 // parts to not be from the same boot.
Alexei Strots1f51ac72023-05-15 10:14:54 -070035 if (!log_files.ContainsPartsForNode(node_name)) {
Austin Schuhba20ea72021-01-21 16:47:01 -080036 // Filter the parts relevant to each node when building the mapper.
37 mappers.emplace_back(
Alexei Strots1f51ac72023-05-15 10:14:54 -070038 std::make_unique<TimestampMapper>(node_name, log_files));
Austin Schuhba20ea72021-01-21 16:47:01 -080039 } else {
40 mappers.emplace_back(nullptr);
41 }
42 }
43
44 // Now, build up the estimator used to solve for time.
45 message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator(
Alexei Strots58017402023-05-03 22:05:06 -070046 config, config, log_files.boots(), FLAGS_skip_order_validation,
Austin Schuh58646e22021-08-23 23:51:46 -070047 chrono::seconds(0));
48 multinode_estimator.set_reboot_found(
49 [config](distributed_clock::time_point reboot_time,
50 const std::vector<logger::BootTimestamp> &node_times) {
51 LOG(INFO) << "Rebooted at distributed " << reboot_time;
52 size_t node_index = 0;
53 for (const logger::BootTimestamp &time : node_times) {
54 LOG(INFO) << " "
55 << config->nodes()->Get(node_index)->name()->string_view()
56 << " " << time;
57 ++node_index;
58 }
59 });
Austin Schuhba20ea72021-01-21 16:47:01 -080060
Austin Schuhe639ea12021-01-25 13:00:22 -080061 {
62 std::vector<TimestampMapper *> timestamp_mappers;
63 for (std::unique_ptr<TimestampMapper> &mapper : mappers) {
64 timestamp_mappers.emplace_back(mapper.get());
65 }
66 multinode_estimator.SetTimestampMappers(std::move(timestamp_mappers));
67 }
68
Austin Schuhba20ea72021-01-21 16:47:01 -080069 // To make things more like the logger and faster, cache the node + channel ->
70 // filter mapping in a set of vectors.
71 std::vector<std::vector<message_bridge::NoncausalOffsetEstimator *>> filters;
72 filters.resize(configuration::NodesCount(config));
73
74 for (const Node *node : configuration::GetNodes(config)) {
75 const size_t node_index = configuration::GetNodeIndex(config, node);
76 filters[node_index].resize(config->channels()->size(), nullptr);
77 for (size_t channel_index = 0; channel_index < config->channels()->size();
78 ++channel_index) {
79 const Channel *channel = config->channels()->Get(channel_index);
80
81 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
82 configuration::ChannelIsReadableOnNode(channel, node)) {
83 // We've got a message which is being forwarded to this node.
84 const Node *source_node = configuration::GetNode(
85 config, channel->source_node()->string_view());
86 filters[node_index][channel_index] =
87 multinode_estimator.GetFilter(node, source_node);
88 }
89 }
90 }
91
Austin Schuhe639ea12021-01-25 13:00:22 -080092 multinode_estimator.CheckGraph();
93
Austin Schuhba20ea72021-01-21 16:47:01 -080094 // Now, read all the timestamps for each node. This is simpler than the
95 // logger on purpose. It loads in *all* the timestamps in 1 go per node,
96 // ignoring memory usage.
97 for (const Node *node : configuration::GetNodes(config)) {
98 LOG(INFO) << "Reading all data for " << node->name()->string_view();
99 const size_t node_index = configuration::GetNodeIndex(config, node);
100 TimestampMapper *timestamp_mapper = mappers[node_index].get();
101 if (timestamp_mapper == nullptr) {
102 continue;
103 }
104 while (true) {
105 TimestampedMessage *m = timestamp_mapper->Front();
106 if (m == nullptr) {
107 break;
108 }
Austin Schuhba20ea72021-01-21 16:47:01 -0800109 timestamp_mapper->PopFront();
110 }
111 }
112
113 // Don't get clever. Use the first time as the start time. Note: this is
114 // different than how log_cat and others work.
Austin Schuhdee8ea72021-08-20 19:13:41 -0700115 std::optional<const std::tuple<distributed_clock::time_point,
116 std::vector<BootTimestamp>> *>
117 next_timestamp = multinode_estimator.QueueNextTimestamp();
Austin Schuhba20ea72021-01-21 16:47:01 -0800118 CHECK(next_timestamp);
119 LOG(INFO) << "Starting at:";
120 for (const Node *node : configuration::GetNodes(config)) {
121 const size_t node_index = configuration::GetNodeIndex(config, node);
122 LOG(INFO) << " " << node->name()->string_view() << " -> "
Austin Schuhdee8ea72021-08-20 19:13:41 -0700123 << std::get<1>(*next_timestamp.value())[node_index].time;
Austin Schuhba20ea72021-01-21 16:47:01 -0800124 }
125
Austin Schuh66168842021-08-17 19:42:21 -0700126 std::vector<monotonic_clock::time_point> just_monotonic(
Austin Schuhdee8ea72021-08-20 19:13:41 -0700127 std::get<1>(*next_timestamp.value()).size());
Austin Schuh66168842021-08-17 19:42:21 -0700128 for (size_t i = 0; i < just_monotonic.size(); ++i) {
Austin Schuhdee8ea72021-08-20 19:13:41 -0700129 CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u);
130 just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time;
Austin Schuh66168842021-08-17 19:42:21 -0700131 }
132 multinode_estimator.Start(just_monotonic);
Austin Schuhba20ea72021-01-21 16:47:01 -0800133
134 // As we pull off all the timestamps, the time problem is continually solved,
135 // filling in the CSV files.
136 while (true) {
Austin Schuhdee8ea72021-08-20 19:13:41 -0700137 std::optional<const std::tuple<distributed_clock::time_point,
138 std::vector<BootTimestamp>> *>
139 next_timestamp = multinode_estimator.QueueNextTimestamp();
Austin Schuhba20ea72021-01-21 16:47:01 -0800140 if (!next_timestamp) {
141 break;
142 }
Austin Schuhe589b062022-06-26 21:50:19 -0700143 multinode_estimator.ObserveTimePassed(std::get<0>(*next_timestamp.value()));
Austin Schuhba20ea72021-01-21 16:47:01 -0800144 }
145
Austin Schuhdee8ea72021-08-20 19:13:41 -0700146 LOG(INFO) << "Done";
147
Austin Schuhba20ea72021-01-21 16:47:01 -0800148 return 0;
149}
150
151} // namespace aos::logger
152
153int main(int argc, char **argv) {
154 FLAGS_timestamps_to_csv = true;
155 gflags::SetUsageMessage(
156 "Usage:\n"
157 " timestamp_extractor [args] logfile1 logfile2 ...\n\nThis program "
158 "dumps out all the timestamps from a set of log files for plotting. Use "
159 "--skip_order_validation to skip any time estimation problems we find.");
160 aos::InitGoogle(&argc, &argv);
161
162 return aos::logger::Main(argc, argv);
163}