Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7020c267ed04da27ca1808fa77a106195bf196e2
[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   auto payload     = mbox->get_unique<struct s_payload>();
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
57 static void launch_communication_worker(s4u_Host* tx_host, s4u_Host* rx_host)
58 {
59   std::string mbox_name = std::string("MBOX:") + tx_host->get_cname() + "-" + rx_host->get_cname();
60   std::vector<std::string> args;
61   args.push_back(mbox_name);
62
63   simgrid::s4u::Actor::create("comm_tx", tx_host, communication_tx_fun, args);
64
65   simgrid::s4u::Actor::create("comm_rx", rx_host, communication_rx_fun, args);
66 }
67
68 static void master_main()
69 {
70   s4u_Host* pm0 = simgrid::s4u::Host::by_name("Fafard");
71   s4u_Host* pm1 = simgrid::s4u::Host::by_name("Tremblay");
72   s4u_Host* pm2 = simgrid::s4u::Host::by_name("Bourassa");
73
74   XBT_INFO("## Test 1 (started): check computation on normal PMs");
75
76   XBT_INFO("### Put an activity on a PM");
77   launch_computation_worker(pm0);
78   simgrid::s4u::this_actor::sleep_for(2);
79
80   XBT_INFO("### Put two activities on a PM");
81   launch_computation_worker(pm0);
82   launch_computation_worker(pm0);
83   simgrid::s4u::this_actor::sleep_for(2);
84
85   XBT_INFO("### Put an activity on each PM");
86   launch_computation_worker(pm0);
87   launch_computation_worker(pm1);
88   simgrid::s4u::this_actor::sleep_for(2);
89
90   XBT_INFO("## Test 1 (ended)");
91
92   XBT_INFO(
93       "## Test 2 (started): check impact of running an activity inside a VM (there is no degradation for the moment)");
94
95   XBT_INFO("### Put a VM on a PM, and put an activity to the VM");
96   auto* vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
97   vm0->start();
98   launch_computation_worker(vm0);
99   simgrid::s4u::this_actor::sleep_for(2);
100   vm0->destroy();
101
102   XBT_INFO("## Test 2 (ended)");
103
104   XBT_INFO("## Test 3 (started): check impact of running an activity collocated with a VM (there is no VM noise for "
105            "the moment)");
106
107   XBT_INFO("### Put a VM on a PM, and put an activity to the PM");
108   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
109   vm0->start();
110   launch_computation_worker(pm0);
111   simgrid::s4u::this_actor::sleep_for(2);
112   vm0->destroy();
113   XBT_INFO("## Test 3 (ended)");
114
115   XBT_INFO(
116       "## Test 4 (started): compare the cost of running two activities 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 an activity to each VM");
120   vm0 = new simgrid::s4u::VirtualMachine("VM0", pm0, 1);
121   vm0->start();
122   auto* 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 an activity 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 }