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 "src/plugins/file_system/FileSystem.hpp"
8 #include <boost/algorithm/string/join.hpp>
9 #include <simgrid/plugins/file_system.h>
10 #include <xbt/replay.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
15 static std::unordered_map<std::string, simgrid::s4u::File*> opened_files;
17 #define ACT_DEBUG(...) \
18 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) { \
19 char* NAME = xbt_str_join_array(action, " "); \
20 XBT_DEBUG(__VA_ARGS__); \
25 static void log_action(const char* const* action, double date)
27 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
28 char* name = xbt_str_join_array(action, " ");
29 XBT_VERB("%s %f", name, date);
34 static simgrid::s4u::File* get_file_descriptor(std::string file_name)
36 std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
38 return opened_files.at(full_name);
43 explicit Replayer(std::vector<std::string> args)
47 argv[0] = &args.at(0)[0];
48 if (args.size() == 1) {
52 argv[1] = &args.at(1)[0];
54 simgrid::xbt::replay_runner(argc, argv);
63 static void open(const char* const* action)
65 std::string file_name = action[2];
66 double clock = simgrid::s4u::Engine::getClock();
67 std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
69 ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name.c_str());
70 simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL);
72 opened_files.insert({full_name, file});
74 log_action(action, simgrid::s4u::Engine::getClock() - clock);
77 static void read(const char* const* action)
79 std::string file_name = action[2];
80 sg_size_t size = std::stoul(action[3]);
81 double clock = simgrid::s4u::Engine::getClock();
83 simgrid::s4u::File* file = get_file_descriptor(file_name);
85 ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
88 log_action(action, simgrid::s4u::Engine::getClock() - clock);
91 static void close(const char* const* action)
93 std::string file_name = action[2];
94 double clock = simgrid::s4u::Engine::getClock();
96 simgrid::s4u::File* file = get_file_descriptor(file_name);
98 ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name.c_str());
101 log_action(action, simgrid::s4u::Engine::getClock() - clock);
105 int main(int argc, char* argv[])
107 simgrid::s4u::Engine e(&argc, argv);
108 sg_storage_file_system_init();
110 xbt_assert(argc > 3, "Usage: %s platform_file deployment_file [action_files]\n"
111 "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
112 "\t# if actions are in separate files, specified in deployment\n"
113 "\texample: %s platform.xml deployment.xml",
114 argv[0], argv[0], argv[0]);
116 e.loadPlatform(argv[1]);
117 e.registerDefault(&simgrid::xbt::replay_runner);
118 e.registerFunction<Replayer>("p0");
119 e.loadDeployment(argv[2]);
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);
127 simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
133 delete simgrid::xbt::action_fs;
134 simgrid::xbt::action_fs = nullptr;
137 XBT_INFO("Simulation time %g", e.getClock());