1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u.hpp"
7 #include "xbt/replay.hpp"
9 #include <boost/algorithm/string/join.hpp>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(replay_comm, "Messages specific for this msg example");
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__); \
22 static void log_action(const simgrid::xbt::ReplayAction& action, double date)
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);
32 explicit Replayer(std::vector<std::string> args)
34 const char* actor_name = args.at(0).c_str();
35 const char* trace_filename = args.size() > 1 ? args[1].c_str() : nullptr;
36 simgrid::xbt::replay_runner(actor_name, trace_filename);
39 void operator()() const
45 static void compute(simgrid::xbt::ReplayAction& action)
47 double amount = std::stod(action[2]);
48 double clock = simgrid::s4u::Engine::get_clock();
49 ACT_DEBUG("Entering %s", NAME.c_str());
50 simgrid::s4u::this_actor::execute(amount);
51 log_action(action, simgrid::s4u::Engine::get_clock() - clock);
54 static void send(simgrid::xbt::ReplayAction& action)
56 auto size = static_cast<uint64_t>(std::stod(action[3]));
57 auto* payload = new std::string(action[3]);
58 double clock = simgrid::s4u::Engine::get_clock();
59 simgrid::s4u::Mailbox* to = simgrid::s4u::Mailbox::by_name(simgrid::s4u::this_actor::get_name() + "_" + action[2]);
60 ACT_DEBUG("Entering Send: %s (size: %" PRIu64 ") -- Actor %s on mailbox %s", NAME.c_str(), size,
61 simgrid::s4u::this_actor::get_cname(), to->get_cname());
62 to->put(payload, size);
63 log_action(action, simgrid::s4u::Engine::get_clock() - clock);
66 static void recv(simgrid::xbt::ReplayAction& action)
68 double clock = simgrid::s4u::Engine::get_clock();
69 simgrid::s4u::Mailbox* from =
70 simgrid::s4u::Mailbox::by_name(std::string(action[2]) + "_" + simgrid::s4u::this_actor::get_name());
72 ACT_DEBUG("Receiving: %s -- Actor %s on mailbox %s", NAME.c_str(), simgrid::s4u::this_actor::get_cname(),
74 from->get_unique<std::string>();
75 log_action(action, simgrid::s4u::Engine::get_clock() - clock);
79 int main(int argc, char* argv[])
81 simgrid::s4u::Engine e(&argc, argv);
84 "Usage: %s platform_file deployment_file [action_files]\n"
85 "\t# if all actions are in the same file\n"
86 "\tExample: %s platform.xml deployment.xml actions\n"
87 "\t# if actions are in separate files, specified in deployment\n"
88 "\tExample: %s platform.xml deployment.xml ",
89 argv[0], argv[0], argv[0]);
91 e.load_platform(argv[1]);
92 e.register_actor<Replayer>("p0");
93 e.register_actor<Replayer>("p1");
94 e.load_deployment(argv[2]);
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);
103 ifs.open(argv[3], std::ifstream::in);
104 simgrid::xbt::action_fs = &ifs;
109 simgrid::xbt::action_fs = nullptr;
111 XBT_INFO("Simulation time %g", e.get_clock());