blob: 0f7444a2223c27277727314ceb1a199d86aa8622 [file] [log] [blame]
Naman Guptae7fe3552022-11-23 13:52:03 -08001#include <stdlib.h>
2
3#include <iostream>
4#include <optional>
5#include <ostream>
6#include <sstream>
7#include <string_view>
8#include <vector>
9
10#include "aos/configuration_generated.h"
11#include "aos/events/event_loop.h"
12#include "aos/events/logging/log_reader.h"
13#include "aos/events/logging/log_reader_utils.h"
14#include "aos/events/logging/log_replayer_config_generated.h"
15#include "aos/events/logging/logfile_sorting.h"
16#include "aos/events/logging/logfile_utils.h"
17#include "aos/events/logging/replay_timing_generated.h"
18#include "aos/events/logging/replay_timing_schema.h"
19#include "aos/events/shm_event_loop.h"
20#include "aos/flatbuffer_merge.h"
21#include "aos/init.h"
22#include "aos/json_to_flatbuffer.h"
23#include "aos/util/file.h"
24#include "flatbuffers/flatbuffers.h"
25#include "gflags/gflags.h"
26#include "glog/logging.h"
27
28DEFINE_string(config, "", "If specified, overrides logged configuration.");
29DEFINE_bool(
30 plot_timing, true,
31 "If set, generates a plot of the replay timing--namely, the errors between "
32 "when we "
33 "should've sent messages and when we actually sent replayed messages.");
34DEFINE_bool(skip_sender_channels, true,
35 "If set, skips replay of the channels applications replay on");
36DEFINE_bool(skip_replay, false,
37 "If set, skips actually running the replay. Useful for writing a "
38 "config without running replay");
39DEFINE_bool(
40 print_config, false,
41 "If set, prints the config that will be used for replay to stdout as json");
42DEFINE_string(
43 replay_config, "",
44 "Path to the configuration used for log replay which includes items such "
45 "as channels to remap, and applications to target for replay. If not set, "
46 "log_reader will run on shm event loop. ");
47DEFINE_string(merge_with_config, "",
48 "A valid json string to be merged with config. This is used to "
49 "add extra applications needed to run only for log_replayer");
50
51namespace aos::logger {
52
53int Main(int argc, char *argv[]) {
54 const std::vector<std::string> unsorted_logfiles =
55 aos::logger::FindLogs(argc, argv);
56
57 const std::vector<aos::logger::LogFile> logfiles =
58 aos::logger::SortParts(unsorted_logfiles);
59
60 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
61 FLAGS_config.empty()
62 ? aos::FlatbufferDetachedBuffer<aos::Configuration>::Empty()
63 : aos::configuration::ReadConfig(FLAGS_config);
64
65 if (FLAGS_plot_timing) {
66 aos::logger::LogReader config_reader(logfiles);
67
68 // Go through the effort to add a ReplayTiming channel to ensure that we
69 // can capture timing information from the replay.
70 const aos::Configuration *raw_config = FLAGS_config.empty()
71 ? config_reader.configuration()
72 : &config.message();
James Kuszmaul741a4d02023-01-05 14:59:21 -080073 aos::ChannelT channel_overrides;
74 channel_overrides.max_size = 10000;
75 channel_overrides.frequency = 10000;
76 config = aos::configuration::AddChannelToConfiguration(
77 raw_config, "/timing",
78 aos::FlatbufferSpan<reflection::Schema>(
79 aos::timing::ReplayTimingSchema()),
80 aos::configuration::GetMyNode(raw_config), channel_overrides);
Naman Guptae7fe3552022-11-23 13:52:03 -080081 }
82
83 if (!FLAGS_merge_with_config.empty()) {
84 config = aos::configuration::MergeWithConfig(&config.message(),
85 FLAGS_merge_with_config);
86 }
87
88 std::optional<aos::FlatbufferDetachedBuffer<ReplayConfig>> replay_config =
89 FLAGS_replay_config.empty()
90 ? std::nullopt
91 : std::make_optional(aos::JsonToFlatbuffer<ReplayConfig>(
92 aos::util::ReadFileToStringOrDie(FLAGS_replay_config.data())));
93
94 std::vector<std::pair<std::string_view, std::string_view>> message_filter;
95 if (FLAGS_skip_sender_channels && replay_config.has_value()) {
96 CHECK(replay_config.value().message().has_active_nodes());
97 std::vector<const Node *> active_nodes;
98 for (const auto &node : *replay_config.value().message().active_nodes()) {
99 active_nodes.emplace_back(configuration::GetNode(
100 &config.message(), node->name()->string_view()));
101 }
102
103 std::vector<std::string> applications;
104 for (const auto &application :
105 *replay_config.value().message().applications()) {
106 if (application->name()->string_view() != "camera_message_interceptor") {
107 applications.emplace_back(application->name()->string_view());
108 }
109 }
110
111 aos::logger::ChannelsInLogResult channels =
112 ChannelsInLog(logfiles, active_nodes, applications);
113 for (auto const &channel :
114 channels.watchers_and_fetchers_without_senders.value()) {
115 message_filter.emplace_back(std::make_pair(channel.name, channel.type));
116 }
117 }
118
119 aos::logger::LogReader reader(
120 logfiles, &config.message(),
121 message_filter.empty() ? nullptr : &message_filter);
122
123 if (replay_config.has_value()) {
124 for (auto const &remap_channel :
125 *replay_config.value().message().remap_channels()) {
126 auto const &channel = remap_channel->channel();
127 std::string_view new_type = remap_channel->has_new_type()
128 ? remap_channel->new_type()->string_view()
129 : channel->type()->string_view();
130 reader.RemapLoggedChannel(
131 channel->name()->string_view(), channel->type()->string_view(),
132 remap_channel->prefix()->string_view(), new_type);
133 }
134 }
135
136 if (FLAGS_print_config) {
137 // TODO(Naman): Replace with config writer if it will be cleaner
138 std::cout << FlatbufferToJson(reader.configuration()) << std::endl;
139 }
140
141 if (!FLAGS_skip_replay) {
142 aos::ShmEventLoop event_loop(reader.configuration());
143
144 event_loop.SkipAosLog();
145 event_loop.SkipTimingReport();
146
147 reader.Register(&event_loop);
148 reader.OnEnd(event_loop.node(), [&event_loop]() { event_loop.Exit(); });
149
150 if (FLAGS_plot_timing) {
151 aos::Sender<aos::timing::ReplayTiming> replay_timing_sender =
152 event_loop.MakeSender<aos::timing::ReplayTiming>("/timing");
153 reader.set_timing_accuracy_sender(event_loop.node(),
154 std::move(replay_timing_sender));
155 }
156
157 event_loop.Run();
158
159 reader.Deregister();
160 }
161
162 return EXIT_SUCCESS;
163}
164
165} // namespace aos::logger
166
167int main(int argc, char *argv[]) {
168 gflags::SetUsageMessage(
169 R"message(Binary to replay the full contents of a logfile into shared memory.
170 #replay_config should be set in order to replay a set of nodes, applications and channels
171 #print config and skip replay, if you only want to print the config and not do log replay
172 Use case #1: log_replayer <log_dir> --print_config --replay_config=<path_to_config> --skip_replay
173 Use case #2: log_replayer <log_dir> --nofatal_sent_too_fast --replay_config=<path_to_config>
174 )message");
175
176 aos::InitGoogle(&argc, &argv);
177 return aos::logger::Main(argc, argv);
178}