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) { |
Austin Schuh | 95460cc | 2023-06-26 11:53:10 -0700 | [diff] [blame] | 21 | const LogFilesContainer log_files(SortParts(FindLogs(argc, argv))); |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 22 | const Configuration *config = log_files.config(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 23 | |
| 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 Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 31 | auto node_name = MaybeNodeName(node); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 32 | // Confirm that all the parts are from the same boot if there are enough |
| 33 | // parts to not be from the same boot. |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 34 | if (!log_files.ContainsPartsForNode(node_name)) { |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 35 | // Filter the parts relevant to each node when building the mapper. |
| 36 | mappers.emplace_back( |
Alexei Strots | 1f51ac7 | 2023-05-15 10:14:54 -0700 | [diff] [blame] | 37 | std::make_unique<TimestampMapper>(node_name, log_files)); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 38 | } 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 Strots | 5801740 | 2023-05-03 22:05:06 -0700 | [diff] [blame] | 45 | config, config, log_files.boots(), FLAGS_skip_order_validation, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 46 | 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 59 | |
Austin Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 60 | { |
| 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 68 | // 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 Schuh | e639ea1 | 2021-01-25 13:00:22 -0800 | [diff] [blame] | 91 | multinode_estimator.CheckGraph(); |
| 92 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 93 | // 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 Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 108 | 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 Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 114 | std::optional<const std::tuple<distributed_clock::time_point, |
| 115 | std::vector<BootTimestamp>> *> |
| 116 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 117 | 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 Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 122 | << std::get<1>(*next_timestamp.value())[node_index].time; |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 125 | std::vector<monotonic_clock::time_point> just_monotonic( |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 126 | std::get<1>(*next_timestamp.value()).size()); |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 127 | for (size_t i = 0; i < just_monotonic.size(); ++i) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 128 | CHECK_EQ(std::get<1>(*next_timestamp.value())[i].boot, 0u); |
| 129 | just_monotonic[i] = std::get<1>(*next_timestamp.value())[i].time; |
Austin Schuh | 6616884 | 2021-08-17 19:42:21 -0700 | [diff] [blame] | 130 | } |
| 131 | multinode_estimator.Start(just_monotonic); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 132 | |
| 133 | // As we pull off all the timestamps, the time problem is continually solved, |
| 134 | // filling in the CSV files. |
| 135 | while (true) { |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 136 | std::optional<const std::tuple<distributed_clock::time_point, |
| 137 | std::vector<BootTimestamp>> *> |
| 138 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 139 | if (!next_timestamp) { |
| 140 | break; |
| 141 | } |
Austin Schuh | e589b06 | 2022-06-26 21:50:19 -0700 | [diff] [blame] | 142 | multinode_estimator.ObserveTimePassed(std::get<0>(*next_timestamp.value())); |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Austin Schuh | dee8ea7 | 2021-08-20 19:13:41 -0700 | [diff] [blame] | 145 | LOG(INFO) << "Done"; |
| 146 | |
Austin Schuh | ba20ea7 | 2021-01-21 16:47:01 -0800 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | } // namespace aos::logger |
| 151 | |
| 152 | int 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 | } |