Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove explicit conversion to std::string when it's not required.
[simgrid.git] / examples / cpp / cloud-simple / s4u-cloud-simple.cpp
1 /* Copyright (c) 2007-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 "simgrid/plugins/live_migration.h"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11 namespace sg4 = simgrid::s4u;
12
13 static void computation_fun()
14 {
15   double clock_sta = sg4::Engine::get_clock();
16   sg4::this_actor::execute(1000000);
17   double clock_end = sg4::Engine::get_clock();
18
19   XBT_INFO("%s:%s executed %g", sg4::this_actor::get_host()->get_cname(), sg4::this_actor::get_cname(),
20            clock_end - clock_sta);
21 }
22
23 static void launch_computation_worker(s4u_Host* host)
24 {
25   sg4::Actor::create("compute", host, computation_fun);
26 }
27
28 struct s_payload {
29   s4u_Host* tx_host;
30   const char* tx_actor_name;
31   double clock_sta;
32 };
33
34 static void communication_tx_fun(std::vector<std::string> args)
35 {
36   sg4::Mailbox* mbox            = sg4::Mailbox::by_name(args.at(0));
37   auto* payload                 = new s_payload;
38   payload->tx_actor_name        = sg4::Actor::self()->get_cname();
39   payload->tx_host              = sg4::this_actor::get_host();
40   payload->clock_sta            = sg4::Engine::get_clock();
41
42   mbox->put(payload, 1000000);
43 }
44
45 static void communication_rx_fun(std::vector<std::string> args)
46 {
47   const char* actor_name = sg4::Actor::self()->get_cname();
48   const char* host_name  = sg4::this_actor::get_host()->get_cname();
49   sg4::Mailbox* mbox     = sg4::Mailbox::by_name(args.at(0));
50
51   auto payload     = mbox->get_unique<struct s_payload>();
52   double clock_end = sg4::Engine::get_clock();
53
54   XBT_INFO("%s:%s to %s:%s => %g sec", payload->tx_host->get_cname(), payload->tx_actor_name, host_name, actor_name,
55            clock_end - payload->clock_sta);
56 }
57
58 static void launch_communication_worker(s4u_Host* tx_host, s4u_Host* rx_host)
59 {
60   std::string mbox_name = "MBOX:" + tx_host->get_name() + "-" + rx_host->get_name();
61   std::vector<std::string> args;
62   args.push_back(mbox_name);
63
64   sg4::Actor::create("comm_tx", tx_host, communication_tx_fun, args);
65
66   sg4::Actor::create("comm_rx", rx_host, communication_rx_fun, args);
67 }
68
69 static void master_main()
70 {
71   s4u_Host* pm0 = sg4::Host::by_name("Fafard");
72   s4u_Host* pm1 = sg4::Host::by_name("Tremblay");
73   s4u_Host* pm2 = sg4::Host::by_name("Bourassa");
74
75   XBT_INFO("## Test 1 (started): check computation on normal PMs");
76
77   XBT_INFO("### Put an activity on a PM");
78   launch_computation_worker(pm0);
79   sg4::this_actor::sleep_for(2);
80
81   XBT_INFO("### Put two activities on a PM");
82   launch_computation_worker(pm0);
83   launch_computation_worker(pm0);
84   sg4::this_actor::sleep_for(2);
85
86   XBT_INFO("### Put an activity on each PM");
87   launch_computation_worker(pm0);
88   launch_computation_worker(pm1);
89   sg4::this_actor::sleep_for(2);
90
91   XBT_INFO("## Test 1 (ended)");
92
93   XBT_INFO(
94       "## Test 2 (started): check impact of running an activity inside a VM (there is no degradation for the moment)");
95
96   XBT_INFO("### Put a VM on a PM, and put an activity to the VM");
97   auto* vm0 = pm0->create_vm("VM0", 1);
98   vm0->start();
99   launch_computation_worker(vm0);
100   sg4::this_actor::sleep_for(2);
101   vm0->destroy();
102
103   XBT_INFO("## Test 2 (ended)");
104
105   XBT_INFO("## Test 3 (started): check impact of running an activity collocated with a VM (there is no VM noise for "
106            "the moment)");
107
108   XBT_INFO("### Put a VM on a PM, and put an activity to the PM");
109   vm0 = pm0->create_vm("VM0", 1);
110   vm0->start();
111   launch_computation_worker(pm0);
112   sg4::this_actor::sleep_for(2);
113   vm0->destroy();
114   XBT_INFO("## Test 3 (ended)");
115
116   XBT_INFO(
117       "## Test 4 (started): compare the cost of running two activities inside two different VMs collocated or not (for"
118       " the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
119
120   XBT_INFO("### Put two VMs on a PM, and put an activity to each VM");
121   vm0 = pm0->create_vm("VM0", 1);
122   vm0->start();
123   auto* vm1 = pm0->create_vm("VM1", 1);
124   launch_computation_worker(vm0);
125   launch_computation_worker(vm1);
126   sg4::this_actor::sleep_for(2);
127   vm0->destroy();
128   vm1->destroy();
129
130   XBT_INFO("### Put a VM on each PM, and put an activity to each VM");
131   vm0 = pm0->create_vm("VM0", 1);
132   vm1 = pm1->create_vm("VM1", 1);
133   vm0->start();
134   vm1->start();
135   launch_computation_worker(vm0);
136   launch_computation_worker(vm1);
137   sg4::this_actor::sleep_for(2);
138   vm0->destroy();
139   vm1->destroy();
140   XBT_INFO("## Test 4 (ended)");
141
142   XBT_INFO("## Test 5  (started): Analyse network impact");
143   XBT_INFO("### Make a connection between PM0 and PM1");
144   launch_communication_worker(pm0, pm1);
145   sg4::this_actor::sleep_for(5);
146
147   XBT_INFO("### Make two connection between PM0 and PM1");
148   launch_communication_worker(pm0, pm1);
149   launch_communication_worker(pm0, pm1);
150   sg4::this_actor::sleep_for(5);
151
152   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
153   vm0 = pm0->create_vm("VM0", 1);
154   vm0->start();
155   launch_communication_worker(pm0, vm0);
156   sg4::this_actor::sleep_for(5);
157   vm0->destroy();
158
159   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
160   vm0 = pm1->create_vm("VM0", 1);
161   launch_communication_worker(pm0, vm0);
162   sg4::this_actor::sleep_for(5);
163   vm0->destroy();
164
165   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
166   vm0 = pm1->create_vm("VM0", 1);
167   vm0->start();
168   launch_communication_worker(pm0, vm0);
169   launch_communication_worker(pm0, vm0);
170   sg4::this_actor::sleep_for(5);
171   vm0->destroy();
172
173   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
174   vm0 = pm1->create_vm("VM0", 1);
175   vm0->start();
176   launch_communication_worker(pm0, vm0);
177   launch_communication_worker(pm0, pm1);
178   sg4::this_actor::sleep_for(5);
179   vm0->destroy();
180
181   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
182   vm0 = pm0->create_vm("VM0", 1);
183   vm1 = pm1->create_vm("VM1", 1);
184   vm0->start();
185   vm1->start();
186   launch_communication_worker(vm0, vm1);
187   launch_communication_worker(vm0, vm1);
188   sg4::this_actor::sleep_for(5);
189   vm0->destroy();
190   vm1->destroy();
191
192   XBT_INFO("## Test 5 (ended)");
193   XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the"
194            " network one");
195   XBT_INFO("### Relocate VM0 between PM0 and PM1");
196   vm0 = pm0->create_vm("VM0", 1);
197   vm0->set_ramsize(1L * 1024 * 1024 * 1024); // 1GiB
198
199   vm0->start();
200   launch_communication_worker(vm0, pm2);
201   sg4::this_actor::sleep_for(0.01);
202   sg_vm_migrate(vm0, pm1);
203   sg4::this_actor::sleep_for(0.01);
204   sg_vm_migrate(vm0, pm0);
205   sg4::this_actor::sleep_for(5);
206   vm0->destroy();
207   XBT_INFO("## Test 6 (ended)");
208 }
209
210 int main(int argc, char* argv[])
211 {
212   sg4::Engine e(&argc, argv);
213   sg_vm_live_migration_plugin_init();
214   e.load_platform(argv[1]); /* - Load the platform description */
215
216   sg4::Actor::create("master_", e.host_by_name("Fafard"), master_main);
217
218   e.run();
219
220   XBT_INFO("Simulation time %g", sg4::Engine::get_clock());
221
222   return 0;
223 }