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