Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add option smpi/privatize-lib to add extra library to privatization.
[simgrid.git] / examples / s4u / replay-storage / s4u-replay-storage.cpp
1 /* Copyright (c) 2017-2018. 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(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     int argc;
44     char* argv[2];
45     argv[0] = &args.at(0)[0];
46     if (args.size() == 1) {
47       argc = 1;
48     } else {
49       argc    = 2;
50       argv[1] = &args.at(1)[0];
51     }
52     simgrid::xbt::replay_runner(argc, argv);
53   }
54
55   void operator()()
56   {
57     // Nothing to do here
58   }
59
60   /* My actions */
61   static void open(simgrid::xbt::ReplayAction& action)
62   {
63     std::string file_name = action[2];
64     double clock          = simgrid::s4u::Engine::get_clock();
65     std::string full_name = simgrid::s4u::this_actor::get_name() + ":" + file_name;
66
67     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME.c_str(), file_name.c_str());
68     simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL);
69
70     opened_files.insert({full_name, file});
71
72     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
73   }
74
75   static void read(simgrid::xbt::ReplayAction& action)
76   {
77     std::string file_name = action[2];
78     sg_size_t size        = std::stoul(action[3]);
79     double clock          = simgrid::s4u::Engine::get_clock();
80
81     simgrid::s4u::File* file = get_file_descriptor(file_name);
82
83     ACT_DEBUG("Entering Read: %s (size: %llu)", NAME.c_str(), size);
84     file->read(size);
85
86     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
87   }
88
89   static void close(simgrid::xbt::ReplayAction& action)
90   {
91     std::string file_name = action[2];
92     double clock          = simgrid::s4u::Engine::get_clock();
93
94     simgrid::s4u::File* file = get_file_descriptor(file_name);
95
96     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME.c_str(), file_name.c_str());
97     delete file;
98
99     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
100   }
101 };
102
103 int main(int argc, char* argv[])
104 {
105   simgrid::s4u::Engine e(&argc, argv);
106   sg_storage_file_system_init();
107
108   xbt_assert(argc > 3, "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_default(&simgrid::xbt::replay_runner);
116   e.register_actor<Replayer>("p0");
117   e.load_deployment(argv[2]);
118
119   /*   Action registration */
120   xbt_replay_action_register("open", Replayer::open);
121   xbt_replay_action_register("read", Replayer::read);
122   xbt_replay_action_register("close", Replayer::close);
123
124   if (argv[3]) {
125     simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
126   }
127
128   e.run();
129
130   if (argv[3]) {
131     delete simgrid::xbt::action_fs;
132     simgrid::xbt::action_fs = nullptr;
133   }
134
135   XBT_INFO("Simulation time %g", e.get_clock());
136
137   return 0;
138 }