Logo AND Algorithmique Numérique Distribuée

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