Logo AND Algorithmique Numérique Distribuée

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