Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <vector> |
| 4 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "gflags/gflags.h" |
| 6 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 7 | #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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 11 | |
| 12 | DECLARE_bool(timestamps_to_csv); |
| 13 | DEFINE_bool(skip_order_validation, false, |
| 14 | "If true, ignore any out of orderness in replay"); |
| 15 | |
| 16 | namespace aos::logger { |
| 17 | |
| 18 | namespace chrono = std::chrono; |
| 19 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 20 | int Main(int argc, char **argv) { |
| 21 | const std::vector<std::string> unsorted_logfiles = FindLogs(argc, argv); |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 22 | const LogFilesContainer log_files(SortParts(unsorted_logfiles)); |
| 23 | const Configuration *config = log_files.config(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 24 | |
| 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 Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 32 | auto node_name = MaybeNodeName(node); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 33 | // Confirm that all the parts are from the same boot if there are enough |
| 34 | // parts to not be from the same boot. |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 35 | if (!log_files.ContainsPartsForNode(node_name)) { |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 36 | // Filter the parts relevant to each node when building the mapper. |
| 37 | mappers.emplace_back( |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 38 | std::make_unique<TimestampMapper>(node_name, log_files)); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 39 | } 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 Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame] | 46 | config, config, log_files.boots(), FLAGS_skip_order_validation, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 47 | 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 60 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 61 | { |
| 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 69 | // 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 Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 92 | multinode_estimator.CheckGraph(); |
| 93 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 94 | // 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 109 | 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 Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 115 | std::optional<const std::tuple<distributed_clock::time_point, |
| 116 | std::vector<BootTimestamp>> *> |
| 117 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 118 | 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 Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 123 | << std::get<1>(*next_timestamp.value())[node_index].time; |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 126 | std::vector<monotonic_clock::time_point> just_monotonic( |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 127 | std::get<1>(*next_timestamp.value()).size()); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 128 | for (size_t i = 0; i < just_monotonic.size(); ++i) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 129 | CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u); |
| 130 | just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time; |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 131 | } |
| 132 | multinode_estimator.Start(just_monotonic); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 133 | |
| 134 | // As we pull off all the timestamps, the time problem is continually solved, |
| 135 | // filling in the CSV files. |
| 136 | while (true) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 137 | std::optional<const std::tuple<distributed_clock::time_point, |
| 138 | std::vector<BootTimestamp>> *> |
| 139 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 140 | if (!next_timestamp) { |
| 141 | break; |
| 142 | } |
Austin Schuh | e589b06 | 2022-06-26 21:50:19 -0700 | [diff] [blame] | 143 | multinode_estimator.ObserveTimePassed(std::get<0>(*next_timestamp.value())); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 146 | LOG(INFO) << "Done"; |
| 147 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | } // namespace aos::logger |
| 152 | |
| 153 | int 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 | } |