Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MailboxPtr looks like a smart pointer, but it's not. Kill it.
[simgrid.git] / examples / s4u / replay-comm / s4u-replay-comm.cpp
1 /* Copyright (c) 2009-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/s4u.hpp"
7 #include "xbt/replay.hpp"
8 #include "xbt/str.h"
9 #include <boost/algorithm/string/join.hpp>
10 #include <string>
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_comm, "Messages specific for this msg example");
13
14 #define ACT_DEBUG(...)                                                                                                 \
15   if (XBT_LOG_ISENABLED(replay_comm, xbt_log_priority_verbose)) {                                                   \
16     std::string NAME = boost::algorithm::join(action, " ");                                                            \
17     XBT_DEBUG(__VA_ARGS__);                                                                                            \
18   } else                                                                                                               \
19   ((void)0)
20
21 static void log_action(simgrid::xbt::ReplayAction& action, double date)
22 {
23   if (XBT_LOG_ISENABLED(replay_comm, xbt_log_priority_verbose)) {
24     std::string s = boost::algorithm::join(action, " ");
25     XBT_VERB("%s %f", s.c_str(), date);
26   }
27 }
28
29 class Replayer {
30 public:
31   explicit Replayer(std::vector<std::string> args)
32   {
33     const char* actor_name     = args.at(0).c_str();
34     const char* trace_filename = args.size() > 1 ? args[1].c_str() : nullptr;
35     simgrid::xbt::replay_runner(actor_name, trace_filename);
36   }
37
38   void operator()()
39   {
40     // Nothing to do here
41   }
42
43   /* My actions */
44   static void compute(simgrid::xbt::ReplayAction& action)
45   {
46     double amount = std::stod(action[2]);
47     double clock  = simgrid::s4u::Engine::get_clock();
48     ACT_DEBUG("Entering %s", NAME.c_str());
49     simgrid::s4u::this_actor::execute(amount);
50     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
51   }
52
53   static void send(simgrid::xbt::ReplayAction& action)
54   {
55     double size                 = std::stod(action[3]);
56     std::string* payload        = new std::string(action[3]);
57     double clock                = simgrid::s4u::Engine::get_clock();
58     simgrid::s4u::Mailbox* to = simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_name() + "_" + action[2]);
59     ACT_DEBUG("Entering Send: %s (size: %g) -- Actor %s on mailbox %s", NAME.c_str(), size,
60               simgrid::s4u::this_actor::get_cname(), to->get_cname());
61     to->put(payload, size);
62     delete payload;
63
64     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
65   }
66
67   static void recv(simgrid::xbt::ReplayAction& action)
68   {
69     double clock = simgrid::s4u::Engine::get_clock();
70     simgrid::s4u::Mailbox* from =
71         simgrid::s4u::Mailbox::by_name(std::string(action[2]) + "_" + simgrid::s4u::this_actor::get_name());
72
73     ACT_DEBUG("Receiving: %s -- Actor %s on mailbox %s", NAME.c_str(), simgrid::s4u::this_actor::get_cname(),
74               from->get_cname());
75     from->get();
76     log_action(action, simgrid::s4u::Engine::get_clock() - clock);
77   }
78 };
79
80 int main(int argc, char* argv[])
81 {
82   simgrid::s4u::Engine e(&argc, argv);
83
84   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file [action_files]\n"
85                        "\t# if all actions are in the same file\n"
86                        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
87                        "\t# if actions are in separate files, specified in deployment\n"
88                        "\tExample: %s msg_platform.xml msg_deployment.xml ",
89              argv[0], argv[0], argv[0]);
90
91   e.load_platform(argv[1]);
92   e.register_actor<Replayer>("p0");
93   e.register_actor<Replayer>("p1");
94   e.load_deployment(argv[2]);
95
96   /*   Action registration */
97   xbt_replay_action_register("compute", Replayer::compute);
98   xbt_replay_action_register("send", Replayer::send);
99   xbt_replay_action_register("recv", Replayer::recv);
100
101   if (argv[3]) {
102     simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
103   }
104
105   e.run();
106
107   if (argv[3]) {
108     delete simgrid::xbt::action_fs;
109     simgrid::xbt::action_fs = nullptr;
110   }
111
112   XBT_INFO("Simulation time %g", e.get_clock());
113
114   return 0;
115 }