Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cecea13f2675c0c0b6277d8a26764978ffc3c6de
[simgrid.git] / examples / cpp / trace-process-migration / s4u-trace-process-migration.cpp
1 /* Copyright (c) 2010-2023. 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 namespace sg4 = simgrid::s4u;
16
17 /* The guy we will move from host to host. It move alone and then is moved by policeman back  */
18 static void emigrant()
19 {
20   auto mailbox = sg4::Mailbox::by_name("master_mailbox");
21
22   sg4::this_actor::sleep_for(2);
23
24   while (true) { // I am an eternal emigrant
25     auto destination = mailbox->get_unique<std::string>();
26     if (destination->empty())
27       break; // there is no destination, die
28     sg4::this_actor::set_host(sg4::Host::by_name(*destination));
29     sg4::this_actor::sleep_for(2); // I am tired, have to sleep for 2 seconds
30   }
31 }
32
33 static void policeman()
34 {
35   // I am the master of emigrant actor,
36   // I tell it where it must emigrate to.
37   auto destinations = {"Tremblay", "Jupiter", "Fafard", "Ginette", "Bourassa", "Fafard", "Tremblay", "Ginette", ""};
38   auto mailbox      = sg4::Mailbox::by_name("master_mailbox");
39
40   for (auto const& destination : destinations) {
41     mailbox->put_init(new std::string(destination), 0)->set_tracing_category("migration_order")->wait();
42   }
43 }
44
45 int main(int argc, char* argv[])
46 {
47   sg4::Engine e(&argc, argv);
48   xbt_assert(argc > 1, "Usage: %s platform_file\n \tExample: %s small_platform.xml\n", argv[0], argv[0]);
49
50   e.load_platform(argv[1]);
51
52   simgrid::instr::declare_tracing_category("migration_order");
53
54   sg4::Actor::create("emigrant", e.host_by_name("Fafard"), emigrant);
55   sg4::Actor::create("policeman", e.host_by_name("Tremblay"), policeman);
56
57   e.run();
58   return 0;
59 }