Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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 task 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   s_payload* 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 s_payload* 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 a task on a PM");
79   launch_computation_worker(pm0);
80   simgrid::s4u::this_actor::sleep_for(2);
81
82   XBT_INFO("### Put two tasks 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 a task 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("## Test 2 (started): check impact of running a task inside a VM (there is no degradation for the moment)");
95
96   XBT_INFO("### Put a VM on a PM, and put a task to the VM");
97   simgrid::s4u::VirtualMachine* vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
98   vm0->start();
99   launch_computation_worker(vm0);
100   simgrid::s4u::this_actor::sleep_for(2);
101   vm0->destroy();
102
103   XBT_INFO("## Test 2 (ended)");
104
105   XBT_INFO(
106       "## Test 3 (started): check impact of running a task collocated with a VM (there is no VM noise for the moment)");
107
108   XBT_INFO("### Put a VM on a PM, and put a task to the PM");
109   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
110   vm0->start();
111   launch_computation_worker(pm0);
112   simgrid::s4u::this_actor::sleep_for(2);
113   vm0->destroy();
114   XBT_INFO("## Test 3 (ended)");
115
116   XBT_INFO("## Test 4 (started): compare the cost of running two tasks inside two different VMs collocated or not (for"
117            " the moment, there is no degradation for the VMs. Hence, the time should be equals to the time of test 1");
118
119   XBT_INFO("### Put two VMs on a PM, and put a task to each VM");
120   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
121   vm0->start();
122   simgrid::s4u::VirtualMachine* vm1 = new simgrid::s4u::VirtualMachine("VM1", pm0, 1);
123   launch_computation_worker(vm0);
124   launch_computation_worker(vm1);
125   simgrid::s4u::this_actor::sleep_for(2);
126   vm0->destroy();
127   vm1->destroy();
128
129   XBT_INFO("### Put a VM on each PM, and put a task to each VM");
130   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
131   vm1 = new simgrid::s4u::VirtualMachine("VM1", pm1, 1);
132   vm0->start();
133   vm1->start();
134   launch_computation_worker(vm0);
135   launch_computation_worker(vm1);
136   simgrid::s4u::this_actor::sleep_for(2);
137   vm0->destroy();
138   vm1->destroy();
139   XBT_INFO("## Test 4 (ended)");
140
141   XBT_INFO("## Test 5  (started): Analyse network impact");
142   XBT_INFO("### Make a connection between PM0 and PM1");
143   launch_communication_worker(pm0, pm1);
144   simgrid::s4u::this_actor::sleep_for(5);
145
146   XBT_INFO("### Make two connection between PM0 and PM1");
147   launch_communication_worker(pm0, pm1);
148   launch_communication_worker(pm0, pm1);
149   simgrid::s4u::this_actor::sleep_for(5);
150
151   XBT_INFO("### Make a connection between PM0 and VM0@PM0");
152   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
153   vm0->start();
154   launch_communication_worker(pm0, vm0);
155   simgrid::s4u::this_actor::sleep_for(5);
156   vm0->destroy();
157
158   XBT_INFO("### Make a connection between PM0 and VM0@PM1");
159   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
160   launch_communication_worker(pm0, vm0);
161   simgrid::s4u::this_actor::sleep_for(5);
162   vm0->destroy();
163
164   XBT_INFO("### Make two connections between PM0 and VM0@PM1");
165   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
166   vm0->start();
167   launch_communication_worker(pm0, vm0);
168   launch_communication_worker(pm0, vm0);
169   simgrid::s4u::this_actor::sleep_for(5);
170   vm0->destroy();
171
172   XBT_INFO("### Make a connection between PM0 and VM0@PM1, and also make a connection between PM0 and PM1");
173   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm1, 1);
174   vm0->start();
175   launch_communication_worker(pm0, vm0);
176   launch_communication_worker(pm0, pm1);
177   simgrid::s4u::this_actor::sleep_for(5);
178   vm0->destroy();
179
180   XBT_INFO("### Make a connection between VM0@PM0 and PM1@PM1, and also make a connection between VM0@PM0 and VM1@PM1");
181   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
182   vm1 = new simgrid::s4u::VirtualMachine("VM1", pm1, 1);
183   vm0->start();
184   vm1->start();
185   launch_communication_worker(vm0, vm1);
186   launch_communication_worker(vm0, vm1);
187   simgrid::s4u::this_actor::sleep_for(5);
188   vm0->destroy();
189   vm1->destroy();
190
191   XBT_INFO("## Test 5 (ended)");
192   XBT_INFO("## Test 6 (started): Check migration impact (not yet implemented neither on the CPU resource nor on the"
193            " network one");
194   XBT_INFO("### Relocate VM0 between PM0 and PM1");
195   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
196   vm0->set_ramsize(1L * 1024 * 1024 * 1024); // 1GiB
197
198   vm0->start();
199   launch_communication_worker(vm0, pm2);
200   simgrid::s4u::this_actor::sleep_for(0.01);
201   sg_vm_migrate(vm0, pm1);
202   simgrid::s4u::this_actor::sleep_for(0.01);
203   sg_vm_migrate(vm0, pm0);
204   simgrid::s4u::this_actor::sleep_for(5);
205   vm0->destroy();
206   XBT_INFO("## Test 6 (ended)");
207 }
208
209 int main(int argc, char* argv[])
210 {
211   simgrid::s4u::Engine e(&argc, argv);
212   sg_vm_live_migration_plugin_init();
213   e.load_platform(argv[1]); /* - Load the platform description */
214
215   simgrid::s4u::Actor::create("master_", simgrid::s4u::Host::by_name("Fafard"), master_main);
216
217   e.run();
218
219   XBT_INFO("Simulation time %g", e.get_clock());
220
221   return 0;
222 }