Logo AND Algorithmique Numérique Distribuée

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