1 /* Copyright (c) 2017. 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/s4u.hpp"
7 #include <boost/algorithm/string/join.hpp>
8 #include <xbt/replay.hpp>
11 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
13 static std::unordered_map<std::string, simgrid::s4u::File*> opened_files;
15 #define ACT_DEBUG(...) \
16 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) { \
17 char* NAME = xbt_str_join_array(action, " "); \
18 XBT_DEBUG(__VA_ARGS__); \
23 static void log_action(const char* const* action, double date)
25 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
26 char* name = xbt_str_join_array(action, " ");
27 XBT_VERB("%s %f", name, date);
32 static simgrid::s4u::File* get_file_descriptor(std::string file_name)
34 std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
36 return opened_files.at(full_name);
41 explicit Replayer(std::vector<std::string> args)
45 argv[0] = &args.at(0)[0];
46 if (args.size() == 1) {
50 argv[1] = &args.at(1)[0];
52 simgrid::xbt::replay_runner(argc, argv);
61 static void open(const char* const* action)
63 std::string file_name = action[2];
64 double clock = simgrid::s4u::Engine::getClock();
65 std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
67 ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name.c_str());
68 simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL);
70 opened_files.insert({full_name, file});
72 log_action(action, simgrid::s4u::Engine::getClock() - clock);
75 static void read(const char* const* action)
77 std::string file_name = action[2];
78 sg_size_t size = std::stoul(action[3]);
79 double clock = simgrid::s4u::Engine::getClock();
81 simgrid::s4u::File* file = get_file_descriptor(file_name);
83 ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
86 log_action(action, simgrid::s4u::Engine::getClock() - clock);
89 static void close(const char* const* action)
91 std::string file_name = action[2];
92 double clock = simgrid::s4u::Engine::getClock();
94 simgrid::s4u::File* file = get_file_descriptor(file_name);
96 ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name.c_str());
99 log_action(action, simgrid::s4u::Engine::getClock() - clock);
103 int main(int argc, char* argv[])
105 simgrid::s4u::Engine e(&argc, argv);
107 xbt_assert(argc > 3, "Usage: %s platform_file deployment_file [action_files]\n"
108 "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
109 "\t# if actions are in separate files, specified in deployment\n"
110 "\texample: %s platform.xml deployment.xml",
111 argv[0], argv[0], argv[0]);
113 e.loadPlatform(argv[1]);
114 e.registerDefault(&simgrid::xbt::replay_runner);
115 e.registerFunction<Replayer>("p0");
116 e.loadDeployment(argv[2]);
118 /* Action registration */
119 xbt_replay_action_register("open", Replayer::open);
120 xbt_replay_action_register("read", Replayer::read);
121 xbt_replay_action_register("close", Replayer::close);
124 simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
130 delete simgrid::xbt::action_fs;
131 simgrid::xbt::action_fs = nullptr;
134 XBT_INFO("Simulation time %g", e.getClock());