1 /* Copyright (c) 2017-2022. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include <simgrid/plugins/file_system.h>
7 #include <simgrid/s4u.hpp>
8 #include <xbt/replay.hpp>
11 #include <boost/algorithm/string/join.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_io, "Messages specific for this example");
14 namespace sg4 = simgrid::s4u;
16 #define ACT_DEBUG(...) \
17 if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) { \
18 std::string NAME = boost::algorithm::join(action, " "); \
19 XBT_DEBUG(__VA_ARGS__); \
24 static std::unordered_map<std::string, sg4::File*> opened_files;
26 static void log_action(const simgrid::xbt::ReplayAction& action, double date)
28 if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {
29 std::string s = boost::algorithm::join(action, " ");
30 XBT_VERB("%s %f", s.c_str(), date);
34 static sg4::File* get_file_descriptor(const std::string& file_name)
36 std::string full_name = sg4::this_actor::get_name() + ":" + file_name;
37 return opened_files.at(full_name);
41 explicit Replayer(std::vector<std::string> args)
43 const char* actor_name = args[0].c_str();
44 if (args.size() > 1) { // split mode, the trace file was provided in the deployment file
45 const char* trace_filename = args[1].c_str();
46 simgrid::xbt::replay_runner(actor_name, trace_filename);
47 } else { // Merged mode
48 simgrid::xbt::replay_runner(actor_name);
52 void operator()() const
58 static void open(simgrid::xbt::ReplayAction& action)
60 std::string file_name = action[2];
61 double clock = sg4::Engine::get_clock();
62 std::string full_name = sg4::this_actor::get_name() + ":" + file_name;
64 ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
65 auto* file = sg4::File::open(file_name, nullptr);
66 opened_files.try_emplace(full_name, file);
68 log_action(action, sg4::Engine::get_clock() - clock);
71 static void read(simgrid::xbt::ReplayAction& action)
73 std::string file_name = action[2];
74 sg_size_t size = std::stoul(action[3]);
75 double clock = sg4::Engine::get_clock();
77 sg4::File* file = get_file_descriptor(file_name);
79 ACT_DEBUG("Entering Read: %s (size: %llu)", NAME.c_str(), size);
82 log_action(action, sg4::Engine::get_clock() - clock);
85 static void close(simgrid::xbt::ReplayAction& action)
87 std::string file_name = action[2];
88 std::string full_name = sg4::this_actor::get_name() + ":" + file_name;
89 double clock = sg4::Engine::get_clock();
91 ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
92 auto entry = opened_files.find(full_name);
93 xbt_assert(entry != opened_files.end(), "File not found in opened files: %s", full_name.c_str());
94 entry->second->close();
95 opened_files.erase(entry);
96 log_action(action, sg4::Engine::get_clock() - clock);
100 std::unordered_map<std::string, sg4::File*> Replayer::opened_files;
102 int main(int argc, char* argv[])
104 sg4::Engine e(&argc, argv);
105 sg_storage_file_system_init();
108 "Usage: %s platform_file deployment_file [action_files]\n"
109 "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
110 "\t# if actions are in separate files, specified in deployment\n"
111 "\texample: %s platform.xml deployment.xml",
112 argv[0], argv[0], argv[0]);
114 e.load_platform(argv[1]);
115 e.register_actor<Replayer>("p0");
116 e.load_deployment(argv[2]);
118 if (argv[3] != nullptr)
119 xbt_replay_set_tracefile(argv[3]);
121 /* Action registration */
122 xbt_replay_action_register("open", Replayer::open);
123 xbt_replay_action_register("read", Replayer::read);
124 xbt_replay_action_register("close", Replayer::close);
128 XBT_INFO("Simulation time %g", sg4::Engine::get_clock());