Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve the doc of MSG_task_*_bounded
[simgrid.git] / src / msg / msg_vm.cpp
1 /* Copyright (c) 2012-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* TODO:
8  * 1. add the support of trace
9  * 2. use parallel tasks to simulate CPU overhead and remove the experimental code generating micro computation tasks
10  */
11
12 #include <xbt/ex.hpp>
13
14 #include "simgrid/plugins/live_migration.h"
15 #include "src/instr/instr_private.hpp"
16 #include "src/plugins/vm/VirtualMachineImpl.hpp"
17 #include "src/plugins/vm/VmHostExt.hpp"
18
19 #include "simgrid/host.h"
20 #include "simgrid/simix.hpp"
21 #include "xbt/string.hpp"
22
23 extern "C" {
24
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg, "Cloud-oriented parts of the MSG API");
26
27 const char* MSG_vm_get_name(msg_vm_t vm)
28 {
29   return vm->getCname();
30 }
31
32 /** @brief Get the physical host of a given VM.
33  *  @ingroup msg_VMs
34  */
35 msg_host_t MSG_vm_get_pm(msg_vm_t vm)
36 {
37   return vm->getPm();
38 }
39
40 /** \ingroup m_vm_management
41  * \brief Set the parameters of a given host
42  *
43  * \param vm a vm
44  * \param params a parameter object
45  */
46 void MSG_vm_set_params(msg_vm_t vm, vm_params_t params)
47 {
48   vm->setParameters(params);
49 }
50
51 /** \ingroup m_vm_management
52  * \brief Get the parameters of a given host
53  *
54  * \param vm the vm you are interested into
55  * \param params a prameter object
56  */
57 void MSG_vm_get_params(msg_vm_t vm, vm_params_t params)
58 {
59   vm->getParameters(params);
60 }
61
62 void MSG_vm_set_ramsize(msg_vm_t vm, size_t size)
63 {
64   vm->setRamsize(size);
65 }
66 size_t MSG_vm_get_ramsize(msg_vm_t vm)
67 {
68   return vm->getRamsize();
69 }
70
71 /* **** Check state of a VM **** */
72 void MSG_vm_set_bound(msg_vm_t vm, double bound)
73 {
74   vm->setBound(bound);
75 }
76 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
77 {
78   return vm->pimpl_vm_ != nullptr && vm->getState() == state;
79 }
80
81 /** @brief Returns whether the given VM has just created, not running.
82  *  @ingroup msg_VMs
83  */
84 int MSG_vm_is_created(msg_vm_t vm)
85 {
86   return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
87 }
88
89 /** @brief Returns whether the given VM is currently running
90  *  @ingroup msg_VMs
91  */
92 int MSG_vm_is_running(msg_vm_t vm)
93 {
94   return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
95 }
96
97 /** @brief Returns whether the given VM is currently migrating
98  *  @ingroup msg_VMs
99  */
100 int MSG_vm_is_migrating(msg_vm_t vm)
101 {
102   return vm->isMigrating();
103 }
104
105 /** @brief Returns whether the given VM is currently suspended, not running.
106  *  @ingroup msg_VMs
107  */
108 int MSG_vm_is_suspended(msg_vm_t vm)
109 {
110   return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
111 }
112
113 /* **** ******** MSG vm actions ********* **** */
114 /** @brief Create a new VM with specified parameters.
115  *  @ingroup msg_VMs*
116  *  @param pm        Physical machine that will host the VM
117  *  @param name      Must be unique
118  *  @param coreAmount Must be >= 1
119  *  @param ramsize   [TODO]
120  *  @param mig_netspeed Amount of Mbyte/s allocated to the migration (cannot be larger than net_cap). Use 0 if unsure.
121  *  @param dp_intensity Dirty page percentage according to migNetSpeed, [0-100]. Use 0 if unsure.
122  */
123 msg_vm_t MSG_vm_create(msg_host_t pm, const char* name, int coreAmount, int ramsize, int mig_netspeed, int dp_intensity)
124 {
125   simgrid::vm::VmHostExt::ensureVmExtInstalled();
126
127   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
128
129   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
130   s_vm_params_t params;
131   params.max_downtime = 0.03;
132   params.mig_speed    = static_cast<double>(mig_netspeed) * 1024 * 1024; // mig_speed
133   params.dp_intensity = static_cast<double>(dp_intensity) / 100;
134   params.dp_cap       = vm->getRamsize() * 0.9; // assume working set memory is 90% of ramsize
135
136   XBT_DEBUG("migspeed : %f intensity mem : %d", params.mig_speed, dp_intensity);
137   vm->setParameters(&params);
138
139   return vm;
140 }
141
142 /** @brief Create a new VM object with the default parameters
143  *  @ingroup msg_VMs*
144  *
145  * A VM is treated as a host. The name of the VM must be unique among all hosts.
146  */
147 msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name)
148 {
149   xbt_assert(sg_host_by_name(name) == nullptr,
150              "Cannot create a VM named %s: this name is already used by an host or a VM", name);
151
152   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, 1);
153   s_vm_params_t params;
154   memset(&params, 0, sizeof(params));
155   vm->setParameters(&params);
156   return vm;
157 }
158 /** @brief Create a new VM object with the default parameters, but with a specified amount of cores
159  *  @ingroup msg_VMs*
160  *
161  * A VM is treated as a host. The name of the VM must be unique among all hosts.
162  */
163 msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount)
164 {
165   xbt_assert(sg_host_by_name(name) == nullptr,
166              "Cannot create a VM named %s: this name is already used by an host or a VM", name);
167
168   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount);
169   s_vm_params_t params;
170   memset(&params, 0, sizeof(params));
171   vm->setParameters(&params);
172   return vm;
173 }
174
175 /** @brief Start a vm (i.e., boot the guest operating system)
176  *  @ingroup msg_VMs
177  *
178  *  If the VM cannot be started (because of memory over-provisioning), an exception is generated.
179  */
180 void MSG_vm_start(msg_vm_t vm)
181 {
182   vm->start();
183   if (TRACE_msg_vm_is_enabled()) {
184     simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
185     state->addEntityValue("start", "0 0 1"); // start is blue
186     state->pushEvent("start");
187   }
188 }
189
190 /** @brief Immediately suspend the execution of all processes within the given VM.
191  *  @ingroup msg_VMs
192  *
193  * This function stops the execution of the VM. All the processes on this VM
194  * will pause. The state of the VM is preserved. We can later resume it again.
195  *
196  * No suspension cost occurs.
197  */
198 void MSG_vm_suspend(msg_vm_t vm)
199 {
200   vm->suspend();
201   if (TRACE_msg_vm_is_enabled()) {
202     simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
203     state->addEntityValue("suspend", "1 0 0"); // suspend is red
204     state->pushEvent("suspend");
205   }
206 }
207
208 /** @brief Resume the execution of the VM. All processes on the VM run again.
209  *  @ingroup msg_VMs
210  *
211  * No resume cost occurs.
212  */
213 void MSG_vm_resume(msg_vm_t vm)
214 {
215   vm->resume();
216   if (TRACE_msg_vm_is_enabled())
217     simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
218 }
219
220 /** @brief Immediately kills all processes within the given VM.
221  *  @ingroup msg_VMs
222  *
223  * Any memory that they allocated will be leaked, unless you used #MSG_process_on_exit().
224  *
225  * No extra delay occurs. If you want to simulate this too, you want to use a #MSG_process_sleep().
226  */
227 void MSG_vm_shutdown(msg_vm_t vm)
228 {
229   vm->shutdown();
230 }
231
232 /** @brief Destroy a VM. Destroy the VM object from the simulation.
233  *  @ingroup msg_VMs
234  */
235 void MSG_vm_destroy(msg_vm_t vm)
236 {
237   if (vm->isMigrating())
238     THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
239
240   /* First, terminate all processes on the VM if necessary */
241   vm->shutdown();
242
243   /* Then, destroy the VM object */
244   vm->destroy();
245
246   if (TRACE_msg_vm_is_enabled()) {
247     container_t container = simgrid::instr::Container::byName(vm->getName());
248     container->removeFromParent();
249     delete container;
250   }
251 }
252
253 }