Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sonar smells.
[simgrid.git] / examples / cpp / replay-io / s4u-replay-io.cpp
1 /* Copyright (c) 2017-2021. 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, 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);
37   }
38
39 public:
40   explicit Replayer(std::vector<std::string> args)
41   {
42     const char* actor_name = args[0].c_str();
43     if (args.size() > 1) { // split mode, the trace file was provided in the deployment file
44       const char* trace_filename = args[1].c_str();
45       simgrid::xbt::replay_runner(actor_name, trace_filename);
46     } else { // Merged mode
47       simgrid::xbt::replay_runner(actor_name);
48     }
49   }
50
51   void operator()() const
52   {
53     // Nothing to do here
54   }
55
56   /* My actions */
57   static void open(simgrid::xbt::ReplayAction& action)
58   {
59     std::string file_name = action[2];
60     double clock          = simgrid::s4u::Engine::get_clock();
61     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
62
63     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
64     opened_files.emplace(std::piecewise_construct, std::forward_as_tuple(full_name),
65                          std::forward_as_tuple(file_name, nullptr));
66
67     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
68   }
69
70   static void read(simgrid::xbt::ReplayAction& action)
71   {
72     std::string file_name = action[2];
73     sg_size_t size        = std::stoul(action[3]);
74     double clock          = simgrid::s4u::Engine::get_clock();
75
76     simgrid::s4u::File* file = get_file_descriptor(file_name);
77
78     ACT_DEBUG("Entering Read: %s (size: %llu)", NAME.c_str(), size);
79     file->read(size);
80
81     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
82   }
83
84   static void close(simgrid::xbt::ReplayAction& action)
85   {
86     std::string file_name = action[2];
87     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
88     double clock          = simgrid::s4u::Engine::get_clock();
89
90     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
91     xbt_assert(opened_files.erase(full_name) == 1, "File not found in opened files: %s", full_name.c_str());
92
93     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
94   }
95 };
96
97 std::unordered_map<std::string, simgrid::s4u::File> Replayer::opened_files;
98
99 int main(int argc, char* argv[])
100 {
101   simgrid::s4u::Engine e(&argc, argv);
102   sg_storage_file_system_init();
103
104   xbt_assert(argc > 3,
105              "Usage: %s platform_file deployment_file [action_files]\n"
106              "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
107              "\t# if actions are in separate files, specified in deployment\n"
108              "\texample: %s platform.xml deployment.xml",
109              argv[0], argv[0], argv[0]);
110
111   e.load_platform(argv[1]);
112   e.register_actor<Replayer>("p0");
113   e.load_deployment(argv[2]);
114
115   if (argv[3] != nullptr)
116     xbt_replay_set_tracefile(argv[3]);
117
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);
122
123   e.run();
124
125   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
126
127   return 0;
128 }