blob: 0b22f9355c4cdfedf449bcea8a682ecc453d7d0c [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) {
Austin Schuh95460cc2023-06-26 11:53:10 -070021 const LogFilesContainer log_files(SortParts(FindLogs(argc, argv)));
Alexei Strots1f51ac72023-05-15 10:14:54 -070022 const Configuration *config = log_files.config();
Austin Schuhba20ea72021-01-21 16:47:01 -080023
24 CHECK(configuration::MultiNode(config))
25 << ": Timestamps only make sense in a multi-node world.";
26
27 // Now, build up all the TimestampMapper classes to read and sort the data.
28 std::vector<std::unique_ptr<TimestampMapper>> mappers;
29
30 for (const Node *node : configuration::GetNodes(config)) {
Alexei Strots1f51ac72023-05-15 10:14:54 -070031 auto node_name = MaybeNodeName(node);
Austin Schuhba20ea72021-01-21 16:47:01 -080032 // Confirm that all the parts are from the same boot if there are enough
33 // parts to not be from the same boot.
Alexei Strots1f51ac72023-05-15 10:14:54 -070034 if (!log_files.ContainsPartsForNode(node_name)) {
Austin Schuhba20ea72021-01-21 16:47:01 -080035 // Filter the parts relevant to each node when building the mapper.
36 mappers.emplace_back(
Alexei Strots1f51ac72023-05-15 10:14:54 -070037 std::make_unique<TimestampMapper>(node_name, log_files));
Austin Schuhba20ea72021-01-21 16:47:01 -080038 } else {
39 mappers.emplace_back(nullptr);
40 }
41 }
42
43 // Now, build up the estimator used to solve for time.
44 message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator(
Alexei Strots58017402023-05-03 22:05:06 -070045 config, config, log_files.boots(), FLAGS_skip_order_validation,
Austin Schuh58646e22021-08-23 23:51:46 -070046 chrono::seconds(0));
47 multinode_estimator.set_reboot_found(
48 [config](distributed_clock::time_point reboot_time,
49 const std::vector<logger::BootTimestamp> &node_times) {
50 LOG(INFO) << "Rebooted at distributed " << reboot_time;
51 size_t node_index = 0;
52 for (const logger::BootTimestamp &time : node_times) {
53 LOG(INFO) << " "
54 << config->nodes()->Get(node_index)->name()->string_view()
55 << " " << time;
56 ++node_index;
57 }
58 });
Austin Schuhba20ea72021-01-21 16:47:01 -080059
Austin Schuhe639ea12021-01-25 13:00:22 -080060 {
61 std::vector<TimestampMapper *> timestamp_mappers;
62 for (std::unique_ptr<TimestampMapper> &mapper : mappers) {
63 timestamp_mappers.emplace_back(mapper.get());
64 }
65 multinode_estimator.SetTimestampMappers(std::move(timestamp_mappers));
66 }
67
Austin Schuhba20ea72021-01-21 16:47:01 -080068 // To make things more like the logger and faster, cache the node + channel ->
69 // filter mapping in a set of vectors.
70 std::vector<std::vector<message_bridge::NoncausalOffsetEstimator *>> filters;
71 filters.resize(configuration::NodesCount(config));
72
73 for (const Node *node : configuration::GetNodes(config)) {
74 const size_t node_index = configuration::GetNodeIndex(config, node);
75 filters[node_index].resize(config->channels()->size(), nullptr);
76 for (size_t channel_index = 0; channel_index < config->channels()->size();
77 ++channel_index) {
78 const Channel *channel = config->channels()->Get(channel_index);
79
80 if (!configuration::ChannelIsSendableOnNode(channel, node) &&
81 configuration::ChannelIsReadableOnNode(channel, node)) {
82 // We've got a message which is being forwarded to this node.
83 const Node *source_node = configuration::GetNode(
84 config, channel->source_node()->string_view());
85 filters[node_index][channel_index] =
86 multinode_estimator.GetFilter(node, source_node);
87 }
88 }
89 }
90
Austin Schuhe639ea12021-01-25 13:00:22 -080091 multinode_estimator.CheckGraph();
92
Austin Schuhba20ea72021-01-21 16:47:01 -080093 // Now, read all the timestamps for each node. This is simpler than the
94 // logger on purpose. It loads in *all* the timestamps in 1 go per node,
95 // ignoring memory usage.
96 for (const Node *node : configuration::GetNodes(config)) {
97 LOG(INFO) << "Reading all data for " << node->name()->string_view();
98 const size_t node_index = configuration::GetNodeIndex(config, node);
99 TimestampMapper *timestamp_mapper = mappers[node_index].get();
100 if (timestamp_mapper == nullptr) {
101 continue;
102 }
103 while (true) {
104 TimestampedMessage *m = timestamp_mapper->Front();
105 if (m == nullptr) {
106 break;
107 }
Austin Schuhba20ea72021-01-21 16:47:01 -0800108 timestamp_mapper->PopFront();
109 }
110 }
111
112 // Don't get clever. Use the first time as the start time. Note: this is
113 // different than how log_cat and others work.
Austin Schuhdee8ea72021-08-20 19:13:41 -0700114 std::optional<const std::tuple<distributed_clock::time_point,
115 std::vector<BootTimestamp>> *>
116 next_timestamp = multinode_estimator.QueueNextTimestamp();
Austin Schuhba20ea72021-01-21 16:47:01 -0800117 CHECK(next_timestamp);
118 LOG(INFO) << "Starting at:";
119 for (const Node *node : configuration::GetNodes(config)) {
120 const size_t node_index = configuration::GetNodeIndex(config, node);
121 LOG(INFO) << " " << node->name()->string_view() << " -> "
Austin Schuhdee8ea72021-08-20 19:13:41 -0700122 << std::get<1>(*next_timestamp.value())[node_index].time;
Austin Schuhba20ea72021-01-21 16:47:01 -0800123 }
124
Austin Schuh66168842021-08-17 19:42:21 -0700125 std::vector<monotonic_clock::time_point> just_monotonic(
Austin Schuhdee8ea72021-08-20 19:13:41 -0700126 std::get<1>(*next_timestamp.value()).size());
Austin Schuh66168842021-08-17 19:42:21 -0700127 for (size_t i = 0; i < just_monotonic.size(); ++i) {
Austin Schuhdee8ea72021-08-20 19:13:41 -0700128 CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u);
129 just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time;
Austin Schuh66168842021-08-17 19:42:21 -0700130 }
131 multinode_estimator.Start(just_monotonic);
Austin Schuhba20ea72021-01-21 16:47:01 -0800132
133 // As we pull off all the timestamps, the time problem is continually solved,
134 // filling in the CSV files.
135 while (true) {
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 if (!next_timestamp) {
140 break;
141 }
Austin Schuhe589b062022-06-26 21:50:19 -0700142 multinode_estimator.ObserveTimePassed(std::get<0>(*next_timestamp.value()));
Austin Schuhba20ea72021-01-21 16:47:01 -0800143 }
144
Austin Schuhdee8ea72021-08-20 19:13:41 -0700145 LOG(INFO) << "Done";
146
Austin Schuhba20ea72021-01-21 16:47:01 -0800147 return 0;
148}
149
150} // namespace aos::logger
151
152int main(int argc, char **argv) {
153 FLAGS_timestamps_to_csv = true;
154 gflags::SetUsageMessage(
155 "Usage:\n"
156 " timestamp_extractor [args] logfile1 logfile2 ...\n\nThis program "
157 "dumps out all the timestamps from a set of log files for plotting. Use "
158 "--skip_order_validation to skip any time estimation problems we find.");
159 aos::InitGoogle(&argc, &argv);
160
161 return aos::logger::Main(argc, argv);
162}