Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use typed Mailbox::get<>() instead of using static_cast everywhere.
[simgrid.git] / examples / s4u / trace-process-migration / s4u-trace-process-migration.cpp
1 /* Copyright (c) 2010-2020. 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 /* This source code simply loads the platform. This is only useful to play
7  * with the tracing module. See the tesh file to see how to generate the
8  * traces.
9  */
10
11 #include "simgrid/instr.h"
12 #include "simgrid/s4u.hpp"
13 #include <memory>
14
15 /* The guy we will move from host to host. It move alone and then is moved by policeman back  */
16 static void emigrant()
17 {
18   auto mailbox = simgrid::s4u::Mailbox::by_name("master_mailbox");
19
20   simgrid::s4u::this_actor::sleep_for(2);
21
22   while (true) { // I am an eternal emigrant
23     auto destination = std::unique_ptr<std::string>(mailbox->get<std::string>());
24     if (destination->empty())
25       break; // there is no destination, die
26     simgrid::s4u::this_actor::set_host(simgrid::s4u::Host::by_name(*destination));
27     simgrid::s4u::this_actor::sleep_for(2); // I am tired, have to sleep for 2 seconds
28   }
29 }
30
31 static void policeman()
32 {
33   // I am the master of emigrant process,
34   // I tell it where it must emigrate to.
35   auto destinations = {"Tremblay", "Jupiter", "Fafard", "Ginette", "Bourassa", "Fafard", "Tremblay", "Ginette", ""};
36   auto mailbox      = simgrid::s4u::Mailbox::by_name("master_mailbox");
37
38   for (auto const& destination : destinations) {
39     mailbox->put_init(new std::string(destination), 0)->set_tracing_category("migration_order")->wait();
40   }
41 }
42
43 int main(int argc, char* argv[])
44 {
45   simgrid::s4u::Engine e(&argc, argv);
46   xbt_assert(argc > 1, "Usage: %s platform_file\n \tExample: %s small_platform.xml\n", argv[0], argv[0]);
47
48   e.load_platform(argv[1]);
49
50   TRACE_category("migration_order");
51
52   simgrid::s4u::Actor::create("emigrant", simgrid::s4u::Host::by_name("Fafard"), emigrant);
53   simgrid::s4u::Actor::create("policeman", simgrid::s4u::Host::by_name("Tremblay"), policeman);
54
55   e.run();
56   return 0;
57 }