Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / replay-io / s4u-replay-io.cpp
1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <simgrid/plugins/file_system.h>
7 #include <simgrid/s4u.hpp>
8 #include <xbt/replay.hpp>
9 #include <xbt/str.h>
10
11 #include <boost/algorithm/string/join.hpp>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_io, "Messages specific for this example");
14 namespace sg4 = simgrid::s4u;
15
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__);                                                                                            \
20   } else                                                                                                               \
21     ((void)0)
22
23 class Replayer {
24   static std::unordered_map<std::string, sg4::File*> opened_files;
25
26   static void log_action(const simgrid::xbt::ReplayAction& action, double date)
27   {
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);
31     }
32   }
33
34   static sg4::File* get_file_descriptor(const std::string& file_name)
35   {
36     std::string full_name = sg4::this_actor::get_name() + ":" + file_name;
37     return opened_files.at(full_name);
38   }
39
40 public:
41   explicit Replayer(std::vector<std::string> args)
42   {
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);
49     }
50   }
51
52   void operator()() const
53   {
54     // Nothing to do here
55   }
56
57   /* My actions */
58   static void open(simgrid::xbt::ReplayAction& action)
59   {
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;
63
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);
67
68     log_action(action, sg4::Engine::get_clock() - clock);
69   }
70
71   static void read(simgrid::xbt::ReplayAction& action)
72   {
73     std::string file_name = action[2];
74     sg_size_t size        = std::stoul(action[3]);
75     double clock          = sg4::Engine::get_clock();
76
77     sg4::File* file = get_file_descriptor(file_name);
78
79     ACT_DEBUG("Entering Read: %s (size: %llu)", NAME.c_str(), size);
80     file->read(size);
81
82     log_action(action, sg4::Engine::get_clock() - clock);
83   }
84
85   static void close(simgrid::xbt::ReplayAction& action)
86   {
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();
90
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);
97   }
98 };
99
100 std::unordered_map<std::string, sg4::File*> Replayer::opened_files;
101
102 int main(int argc, char* argv[])
103 {
104   sg4::Engine e(&argc, argv);
105   sg_storage_file_system_init();
106
107   xbt_assert(argc > 3,
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]);
113
114   e.load_platform(argv[1]);
115   e.register_actor<Replayer>("p0");
116   e.load_deployment(argv[2]);
117
118   if (argv[3] != nullptr)
119     xbt_replay_set_tracefile(argv[3]);
120
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);
125
126   e.run();
127
128   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
129
130   return 0;
131 }