1 /* Copyright (c) 2009-2016. 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"
10 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
12 #define ACT_DEBUG(...) \
13 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
14 char *NAME = xbt_str_join_array(action, " "); \
15 XBT_DEBUG(__VA_ARGS__); \
19 static void log_action(const char *const *action, double date)
21 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
22 char *name = xbt_str_join_array(action, " ");
23 XBT_VERB("%s %f", name, date);
30 explicit Replayer(std::vector<std::string> args)
34 argv[0] = (char*)(args.at(0).c_str());
35 if (args.size() == 1) {
39 argv[1] = (char*)(args.at(1).c_str());
41 xbt_replay_action_runner(argc, argv);
47 static void compute(const char* const* action)
49 double amount = std::stod(action[2]);
50 double clock = simgrid::s4u::Engine::instance()->getClock();
51 ACT_DEBUG("Entering %s", NAME);
52 simgrid::s4u::this_actor::execute(amount);
53 log_action(action, simgrid::s4u::Engine::instance()->getClock() - clock);
56 static void send(const char* const* action)
58 double size = std::stod(action[3]);
59 char* payload = xbt_strdup(action[3]);
60 double clock = simgrid::s4u::Engine::instance()->getClock();
61 simgrid::s4u::MailboxPtr to = simgrid::s4u::Mailbox::byName(simgrid::s4u::this_actor::name() + "_" + action[2]);
62 ACT_DEBUG("Entering Send: %s (size: %g) -- Actor %s on mailbox %s", NAME, size,
63 simgrid::s4u::this_actor::name().c_str(), to->name());
64 simgrid::s4u::this_actor::send(to, payload, size);
67 log_action(action, simgrid::s4u::Engine::instance()->getClock() - clock);
70 static void recv(const char* const* action)
72 double clock = simgrid::s4u::Engine::instance()->getClock();
73 simgrid::s4u::MailboxPtr from =
74 simgrid::s4u::Mailbox::byName(std::string(action[2]) + "_" + simgrid::s4u::this_actor::name());
76 ACT_DEBUG("Receiving: %s -- Actor %s on mailbox %s", NAME, simgrid::s4u::this_actor::name().c_str(), from->name());
77 simgrid::s4u::this_actor::recv(from);
78 log_action(action, simgrid::s4u::Engine::instance()->getClock() - clock);
82 int main(int argc, char *argv[])
84 simgrid::s4u::Engine* e = new simgrid::s4u::Engine(&argc, argv);
85 /* Explicit initialization of the action module is required now*/
86 _xbt_replay_action_init();
88 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file [action_files]\n"
89 "\t# if all actions are in the same file\n"
90 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
91 "\t# if actions are in separate files, specified in deployment\n"
92 "\tExample: %s msg_platform.xml msg_deployment.xml ",
93 argv[0], argv[0], argv[0]);
95 e->loadPlatform(argv[1]);
96 e->registerDefault(xbt_replay_action_runner);
97 e->registerFunction<Replayer>("p0");
98 e->registerFunction<Replayer>("p1");
99 e->loadDeployment(argv[2]);
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);
106 /* Actually do the simulation using MSG_action_trace_run */
108 simgrid::xbt::action_fs = new std::ifstream(argv[3], std::ifstream::in);
114 delete simgrid::xbt::action_fs;
115 simgrid::xbt::action_fs = nullptr;
118 XBT_INFO("Simulation time %g", e->getClock());
120 _xbt_replay_action_exit(); /* Explicit finalization of the action module */