Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
(hopefully) finalize pluginifaction of pseudo file system
[simgrid.git] / examples / s4u / actions-storage / s4u-actions-storage.cpp
1 /* Copyright (c) 2017. 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/s4u.hpp"
7 #include "src/plugins/file_system/FileSystem.hpp"
8 #include <boost/algorithm/string/join.hpp>
9 #include <simgrid/plugins/file_system.h>
10 #include <xbt/replay.hpp>
11 #include <xbt/str.h>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "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(storage_actions, xbt_log_priority_verbose)) {                                                  \
19     char* NAME = xbt_str_join_array(action, " ");                                                                      \
20     XBT_DEBUG(__VA_ARGS__);                                                                                            \
21     xbt_free(NAME);                                                                                                    \
22   } else                                                                                                               \
23   ((void)0)
24
25 static void log_action(const char* const* action, double date)
26 {
27   if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
28     char* name = xbt_str_join_array(action, " ");
29     XBT_VERB("%s %f", name, date);
30     xbt_free(name);
31   }
32 }
33
34 static simgrid::s4u::File* get_file_descriptor(std::string file_name)
35 {
36   std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
37
38   return opened_files.at(full_name);
39 }
40
41 class Replayer {
42 public:
43   explicit Replayer(std::vector<std::string> args)
44   {
45     int argc;
46     char* argv[2];
47     argv[0] = &args.at(0)[0];
48     if (args.size() == 1) {
49       argc = 1;
50     } else {
51       argc    = 2;
52       argv[1] = &args.at(1)[0];
53     }
54     simgrid::xbt::replay_runner(argc, argv);
55   }
56
57   void operator()()
58   {
59     // Nothing to do here
60   }
61
62   /* My actions */
63   static void open(const char* const* action)
64   {
65     std::string file_name = action[2];
66     double clock          = simgrid::s4u::Engine::getClock();
67     std::string full_name = simgrid::s4u::this_actor::getName() + ":" + file_name;
68
69     ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name.c_str());
70     simgrid::s4u::File* file = new simgrid::s4u::File(file_name, NULL);
71
72     opened_files.insert({full_name, file});
73
74     log_action(action, simgrid::s4u::Engine::getClock() - clock);
75   }
76
77   static void read(const char* const* action)
78   {
79     std::string file_name = action[2];
80     sg_size_t size        = std::stoul(action[3]);
81     double clock          = simgrid::s4u::Engine::getClock();
82
83     simgrid::s4u::File* file = get_file_descriptor(file_name);
84
85     ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
86     file->read(size);
87
88     log_action(action, simgrid::s4u::Engine::getClock() - clock);
89   }
90
91   static void close(const char* const* action)
92   {
93     std::string file_name = action[2];
94     double clock          = simgrid::s4u::Engine::getClock();
95
96     simgrid::s4u::File* file = get_file_descriptor(file_name);
97
98     ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name.c_str());
99     delete file;
100
101     log_action(action, simgrid::s4u::Engine::getClock() - clock);
102   }
103 };
104
105 int main(int argc, char* argv[])
106 {
107   simgrid::s4u::Engine e(&argc, argv);
108   sg_storage_file_system_init();
109
110   xbt_assert(argc > 3, "Usage: %s platform_file deployment_file [action_files]\n"
111                        "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
112                        "\t# if actions are in separate files, specified in deployment\n"
113                        "\texample: %s platform.xml deployment.xml",
114              argv[0], argv[0], argv[0]);
115
116   e.loadPlatform(argv[1]);
117   e.registerDefault(&simgrid::xbt::replay_runner);
118   e.registerFunction<Replayer>("p0");
119   e.loadDeployment(argv[2]);
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   if (argv[3]) {
127     simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
128   }
129
130   e.run();
131
132   if (argv[3]) {
133     delete simgrid::xbt::action_fs;
134     simgrid::xbt::action_fs = nullptr;
135   }
136
137   XBT_INFO("Simulation time %g", e.getClock());
138
139   return 0;
140 }