Logo AND Algorithmique Numérique Distribuée

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