Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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 void MSG_vm_set_ramsize(msg_vm_t vm, size_t size)
41 {
42   vm->setRamsize(size);
43 }
44 size_t MSG_vm_get_ramsize(msg_vm_t vm)
45 {
46   return vm->getRamsize();
47 }
48
49 /* **** Check state of a VM **** */
50 void MSG_vm_set_bound(msg_vm_t vm, double bound)
51 {
52   vm->setBound(bound);
53 }
54 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
55 {
56   return vm->pimpl_vm_ != nullptr && vm->getState() == state;
57 }
58
59 /** @brief Returns whether the given VM has just created, not running.
60  *  @ingroup msg_VMs
61  */
62 int MSG_vm_is_created(msg_vm_t vm)
63 {
64   return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
65 }
66
67 /** @brief Returns whether the given VM is currently running
68  *  @ingroup msg_VMs
69  */
70 int MSG_vm_is_running(msg_vm_t vm)
71 {
72   return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
73 }
74
75 /** @brief Returns whether the given VM is currently migrating
76  *  @ingroup msg_VMs
77  */
78 int MSG_vm_is_migrating(msg_vm_t vm)
79 {
80   return vm->isMigrating();
81 }
82
83 /** @brief Returns whether the given VM is currently suspended, not running.
84  *  @ingroup msg_VMs
85  */
86 int MSG_vm_is_suspended(msg_vm_t vm)
87 {
88   return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
89 }
90
91 /* **** ******** MSG vm actions ********* **** */
92 /** @brief Create a new VM with specified parameters.
93  *  @ingroup msg_VMs*
94  *  @param pm        Physical machine that will host the VM
95  *  @param name      Must be unique
96  *  @param coreAmount Must be >= 1
97  *  @param ramsize   [TODO]
98  *  @param mig_netspeed Amount of Mbyte/s allocated to the migration (cannot be larger than net_cap). Use 0 if unsure.
99  *  @param dp_intensity Dirty page percentage according to migNetSpeed, [0-100]. Use 0 if unsure.
100  */
101 msg_vm_t MSG_vm_create(msg_host_t pm, const char* name, int coreAmount, int ramsize, int mig_netspeed, int dp_intensity)
102 {
103   simgrid::vm::VmHostExt::ensureVmExtInstalled();
104
105   /* For the moment, intensity_rate is the percentage against the migration bandwidth */
106
107   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount, static_cast<sg_size_t>(ramsize) * 1024 * 1024);
108   if (not sg_vm_is_migratable(vm)) {
109     if (mig_netspeed != 0 || dp_intensity != 0)
110       XBT_WARN("The live migration is not enabled. dp_intensity and mig_netspeed can't be used");
111   } else {
112     sg_vm_set_dirty_page_intensity(vm, dp_intensity / 100.0);
113     sg_vm_set_working_set_memory(vm, vm->getRamsize() * 0.9); // assume working set memory is 90% of ramsize
114     sg_vm_set_migration_speed(vm, mig_netspeed * 1024 * 1024.0);
115
116     XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
117   }
118
119   return vm;
120 }
121
122 /** @brief Create a new VM object with the default parameters
123  *  @ingroup msg_VMs*
124  *
125  * A VM is treated as a host. The name of the VM must be unique among all hosts.
126  */
127 msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name)
128 {
129   xbt_assert(sg_host_by_name(name) == nullptr,
130              "Cannot create a VM named %s: this name is already used by an host or a VM", name);
131
132   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, 1);
133   return vm;
134 }
135 /** @brief Create a new VM object with the default parameters, but with a specified amount of cores
136  *  @ingroup msg_VMs*
137  *
138  * A VM is treated as a host. The name of the VM must be unique among all hosts.
139  */
140 msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount)
141 {
142   xbt_assert(sg_host_by_name(name) == nullptr,
143              "Cannot create a VM named %s: this name is already used by an host or a VM", name);
144
145   msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount);
146   return vm;
147 }
148
149 /** @brief Start a vm (i.e., boot the guest operating system)
150  *  @ingroup msg_VMs
151  *
152  *  If the VM cannot be started (because of memory over-provisioning), an exception is generated.
153  */
154 void MSG_vm_start(msg_vm_t vm)
155 {
156   vm->start();
157   if (TRACE_msg_vm_is_enabled()) {
158     simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
159     state->addEntityValue("start", "0 0 1"); // start is blue
160     state->pushEvent("start");
161   }
162 }
163
164 /** @brief Immediately suspend the execution of all processes within the given VM.
165  *  @ingroup msg_VMs
166  *
167  * This function stops the execution of the VM. All the processes on this VM
168  * will pause. The state of the VM is preserved. We can later resume it again.
169  *
170  * No suspension cost occurs.
171  */
172 void MSG_vm_suspend(msg_vm_t vm)
173 {
174   vm->suspend();
175   if (TRACE_msg_vm_is_enabled()) {
176     simgrid::instr::StateType* state = simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE");
177     state->addEntityValue("suspend", "1 0 0"); // suspend is red
178     state->pushEvent("suspend");
179   }
180 }
181
182 /** @brief Resume the execution of the VM. All processes on the VM run again.
183  *  @ingroup msg_VMs
184  *
185  * No resume cost occurs.
186  */
187 void MSG_vm_resume(msg_vm_t vm)
188 {
189   vm->resume();
190   if (TRACE_msg_vm_is_enabled())
191     simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
192 }
193
194 /** @brief Immediately kills all processes within the given VM.
195  *  @ingroup msg_VMs
196  *
197  * Any memory that they allocated will be leaked, unless you used #MSG_process_on_exit().
198  *
199  * No extra delay occurs. If you want to simulate this too, you want to use a #MSG_process_sleep().
200  */
201 void MSG_vm_shutdown(msg_vm_t vm)
202 {
203   vm->shutdown();
204 }
205
206 /** @brief Destroy a VM. Destroy the VM object from the simulation.
207  *  @ingroup msg_VMs
208  */
209 void MSG_vm_destroy(msg_vm_t vm)
210 {
211   if (vm->isMigrating())
212     THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
213
214   /* First, terminate all processes on the VM if necessary */
215   vm->shutdown();
216
217   /* Then, destroy the VM object */
218   vm->destroy();
219
220   if (TRACE_msg_vm_is_enabled()) {
221     container_t container = simgrid::instr::Container::byName(vm->getName());
222     container->removeFromParent();
223     delete container;
224   }
225 }
226
227 }