Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
For sonar, remove explicit new/delete.
[simgrid.git] / examples / s4u / replay-io / s4u-replay-io.cpp
1 /* Copyright (c) 2017-2020. 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
15 #define ACT_DEBUG(...)                                                                                                 \
16   if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {                                                        \
17     std::string NAME = boost::algorithm::join(action, " ");                                                            \
18     XBT_DEBUG(__VA_ARGS__);                                                                                            \
19   } else                                                                                                               \
20     ((void)0)
21
22 class Replayer {
23   static std::unordered_map<std::string, std::unique_ptr<simgrid::s4u::File>> opened_files;
24
25   static void log_action(const simgrid::xbt::ReplayAction& action, double date)
26   {
27     if (XBT_LOG_ISENABLED(replay_io, xbt_log_priority_verbose)) {
28       std::string s = boost::algorithm::join(action, " ");
29       XBT_VERB("%s %f", s.c_str(), date);
30     }
31   }
32
33   static simgrid::s4u::File* get_file_descriptor(const std::string& file_name)
34   {
35     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
36     return opened_files.at(full_name).get();
37   }
38
39 public:
40   explicit Replayer(std::vector<std::string> args)
41   {
42     const char* actor_name = args[0].c_str();
43     simgrid::xbt::replay_runner(actor_name, nullptr);
44   }
45
46   void operator()() const
47   {
48     // Nothing to do here
49   }
50
51   /* My actions */
52   static void open(simgrid::xbt::ReplayAction& action)
53   {
54     std::string file_name = action[2];
55     double clock          = simgrid::s4u::Engine::get_clock();
56     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
57
58     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
59     auto file = std::make_unique<simgrid::s4u::File>(file_name, nullptr);
60
61     opened_files.insert({full_name, std::move(file)});
62
63     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
64   }
65
66   static void read(simgrid::xbt::ReplayAction& action)
67   {
68     std::string file_name = action[2];
69     sg_size_t size        = std::stoul(action[3]);
70     double clock          = simgrid::s4u::Engine::get_clock();
71
72     simgrid::s4u::File* file = get_file_descriptor(file_name);
73
74     ACT_DEBUG("Entering Read: %s (size: %llu)", NAME.c_str(), size);
75     file->read(size);
76
77     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
78   }
79
80   static void close(simgrid::xbt::ReplayAction& action)
81   {
82     std::string file_name = action[2];
83     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
84     double clock          = simgrid::s4u::Engine::get_clock();
85
86     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
87     XBT_ATTRIB_UNUSED auto count = opened_files.erase(full_name);
88     xbt_assert(count == 1, "File not found in opened files: %s", full_name.c_str());
89
90     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
91   }
92 };
93
94 std::unordered_map<std::string, std::unique_ptr<simgrid::s4u::File>> Replayer::opened_files;
95
96 int main(int argc, char* argv[])
97 {
98   simgrid::s4u::Engine e(&argc, argv);
99   sg_storage_file_system_init();
100
101   xbt_assert(argc > 3,
102              "Usage: %s platform_file deployment_file [action_files]\n"
103              "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
104              "\t# if actions are in separate files, specified in deployment\n"
105              "\texample: %s platform.xml deployment.xml",
106              argv[0], argv[0], argv[0]);
107
108   e.load_platform(argv[1]);
109   e.register_actor<Replayer>("p0");
110   e.load_deployment(argv[2]);
111
112   /*   Action registration */
113   xbt_replay_action_register("open", Replayer::open);
114   xbt_replay_action_register("read", Replayer::read);
115   xbt_replay_action_register("close", Replayer::close);
116
117   std::ifstream ifs;
118   if (argv[3]) {
119     ifs.open(argv[3], std::ifstream::in);
120     simgrid::xbt::action_fs = &ifs;
121   }
122
123   e.run();
124
125   simgrid::xbt::action_fs = nullptr;
126
127   XBT_INFO("Simulation time %g", e.get_clock());
128
129   return 0;
130 }