James Kuszmaul | beaa3c8 | 2023-09-07 11:11:27 -0700 | [diff] [blame^] | 1 | |
| 2 | #include "aos/events/logging/logfile_validator.h" |
| 3 | |
| 4 | #include "aos/events/logging/logfile_sorting.h" |
| 5 | #include "aos/events/logging/logfile_utils.h" |
| 6 | #include "aos/events/logging/logfile_validator.h" |
| 7 | #include "aos/network/multinode_timestamp_filter.h" |
| 8 | |
| 9 | namespace aos::logger { |
| 10 | bool MultiNodeLogIsReadable(const LogFilesContainer &log_files, |
| 11 | bool skip_order_validation) { |
| 12 | const Configuration *config = log_files.config().get(); |
| 13 | |
| 14 | CHECK(configuration::MultiNode(config)) |
| 15 | << ": Timestamps only make sense in a multi-node world."; |
| 16 | |
| 17 | // Now, build up all the TimestampMapper classes to read and sort the data. |
| 18 | std::vector<std::unique_ptr<TimestampMapper>> mappers; |
| 19 | |
| 20 | for (const Node *node : configuration::GetNodes(config)) { |
| 21 | auto node_name = MaybeNodeName(node); |
| 22 | // Confirm that all the parts are from the same boot if there are enough |
| 23 | // parts to not be from the same boot. |
| 24 | if (log_files.ContainsPartsForNode(node_name)) { |
| 25 | // Filter the parts relevant to each node when building the mapper. |
| 26 | mappers.emplace_back(std::make_unique<TimestampMapper>( |
| 27 | node_name, log_files, TimestampQueueStrategy::kQueueTogether)); |
| 28 | } else { |
| 29 | mappers.emplace_back(nullptr); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Now, build up the estimator used to solve for time. |
| 34 | message_bridge::MultiNodeNoncausalOffsetEstimator multinode_estimator( |
| 35 | config, config, log_files.boots(), skip_order_validation, |
| 36 | std::chrono::seconds(0)); |
| 37 | multinode_estimator.set_reboot_found( |
| 38 | [config](distributed_clock::time_point reboot_time, |
| 39 | const std::vector<logger::BootTimestamp> &node_times) { |
| 40 | LOG(INFO) << "Rebooted at distributed " << reboot_time; |
| 41 | size_t node_index = 0; |
| 42 | for (const logger::BootTimestamp &time : node_times) { |
| 43 | LOG(INFO) << " " |
| 44 | << config->nodes()->Get(node_index)->name()->string_view() |
| 45 | << " " << time; |
| 46 | ++node_index; |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | // Because RAII doesn't let us do non-fatal/non-exception things, use this |
| 51 | // when returning to handle certain cleanup-related checks that would normally |
| 52 | // happen fatally in the estimator destrictor. |
| 53 | auto preempt_destructor = [&multinode_estimator](bool success) { |
| 54 | if (!multinode_estimator.RunDestructorChecks()) { |
| 55 | return false; |
| 56 | } |
| 57 | return success; |
| 58 | }; |
| 59 | |
| 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 | |
| 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 | |
| 91 | multinode_estimator.CheckGraph(); |
| 92 | |
| 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 | } |
| 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. |
| 114 | std::optional<std::optional<const std::tuple<distributed_clock::time_point, |
| 115 | std::vector<BootTimestamp>> *>> |
| 116 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
| 117 | if (!next_timestamp.has_value() || !next_timestamp.value().has_value()) { |
| 118 | return preempt_destructor(false); |
| 119 | } |
| 120 | LOG(INFO) << "Starting at:"; |
| 121 | for (const Node *node : configuration::GetNodes(config)) { |
| 122 | const size_t node_index = configuration::GetNodeIndex(config, node); |
| 123 | LOG(INFO) << " " << node->name()->string_view() << " -> " |
| 124 | << std::get<1>(*next_timestamp.value().value())[node_index].time; |
| 125 | } |
| 126 | |
| 127 | std::vector<monotonic_clock::time_point> just_monotonic( |
| 128 | std::get<1>(*next_timestamp.value().value()).size()); |
| 129 | for (size_t i = 0; i < just_monotonic.size(); ++i) { |
| 130 | CHECK_EQ(std::get<1>(*next_timestamp.value().value())[i].boot, 0u); |
| 131 | just_monotonic[i] = std::get<1>(*next_timestamp.value().value())[i].time; |
| 132 | } |
| 133 | multinode_estimator.Start(just_monotonic); |
| 134 | |
| 135 | // As we pull off all the timestamps, the time problem is continually solved, |
| 136 | // filling in the CSV files. |
| 137 | while (true) { |
| 138 | std::optional<std::optional<const std::tuple<distributed_clock::time_point, |
| 139 | std::vector<BootTimestamp>> *>> |
| 140 | next_timestamp = multinode_estimator.QueueNextTimestamp(); |
| 141 | if (!next_timestamp.has_value()) { |
| 142 | return preempt_destructor(false); |
| 143 | } |
| 144 | if (!next_timestamp.value().has_value()) { |
| 145 | break; |
| 146 | } |
| 147 | multinode_estimator.ObserveTimePassed( |
| 148 | std::get<0>(*next_timestamp.value().value())); |
| 149 | } |
| 150 | |
| 151 | LOG(INFO) << "Done"; |
| 152 | |
| 153 | return preempt_destructor(true); |
| 154 | } |
| 155 | |
| 156 | bool LogIsReadableIfMultiNode(const LogFilesContainer &log_files) { |
| 157 | if (aos::configuration::NodesCount(log_files.config().get()) == 1u) { |
| 158 | return true; |
| 159 | } |
| 160 | return MultiNodeLogIsReadable(log_files); |
| 161 | } |
| 162 | } // namespace aos::logger |