1 /* Copyright (c) 2012-2017. The SimGrid Team.
2 * All rights reserved. */
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. */
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
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"
19 #include "simgrid/host.h"
20 #include "simgrid/simix.hpp"
21 #include "xbt/string.hpp"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg, "Cloud-oriented parts of the MSG API");
27 const char* MSG_vm_get_name(msg_vm_t vm)
29 return vm->getCname();
32 /** @brief Get the physical host of a given VM.
35 msg_host_t MSG_vm_get_pm(msg_vm_t vm)
40 /** \ingroup m_vm_management
41 * \brief Set the parameters of a given host
44 * \param params a parameter object
46 void MSG_vm_set_params(msg_vm_t vm, vm_params_t params)
48 vm->setParameters(params);
51 /** \ingroup m_vm_management
52 * \brief Get the parameters of a given host
54 * \param vm the vm you are interested into
55 * \param params a prameter object
57 void MSG_vm_get_params(msg_vm_t vm, vm_params_t params)
59 vm->getParameters(params);
62 void MSG_vm_set_ramsize(msg_vm_t vm, size_t size)
66 size_t MSG_vm_get_ramsize(msg_vm_t vm)
68 return vm->getRamsize();
71 /* **** Check state of a VM **** */
72 void MSG_vm_set_bound(msg_vm_t vm, double bound)
76 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
78 return vm->pimpl_vm_ != nullptr && vm->getState() == state;
81 /** @brief Returns whether the given VM has just created, not running.
84 int MSG_vm_is_created(msg_vm_t vm)
86 return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
89 /** @brief Returns whether the given VM is currently running
92 int MSG_vm_is_running(msg_vm_t vm)
94 return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
97 /** @brief Returns whether the given VM is currently migrating
100 int MSG_vm_is_migrating(msg_vm_t vm)
102 return vm->isMigrating();
105 /** @brief Returns whether the given VM is currently suspended, not running.
108 int MSG_vm_is_suspended(msg_vm_t vm)
110 return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
113 /* **** ******** MSG vm actions ********* **** */
114 /** @brief Create a new VM with specified parameters.
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.
123 msg_vm_t MSG_vm_create(msg_host_t pm, const char* name, int coreAmount, int ramsize, int mig_netspeed, int dp_intensity)
125 simgrid::vm::VmHostExt::ensureVmExtInstalled();
127 /* For the moment, intensity_rate is the percentage against the migration bandwidth */
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
136 XBT_DEBUG("migspeed : %f intensity mem : %d", params.mig_speed, dp_intensity);
137 vm->setParameters(¶ms);
142 /** @brief Create a new VM object with the default parameters
145 * A VM is treated as a host. The name of the VM must be unique among all hosts.
147 msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name)
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);
152 msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, 1);
153 s_vm_params_t params;
154 memset(¶ms, 0, sizeof(params));
155 vm->setParameters(¶ms);
158 /** @brief Create a new VM object with the default parameters, but with a specified amount of cores
161 * A VM is treated as a host. The name of the VM must be unique among all hosts.
163 msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount)
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);
168 msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount);
169 s_vm_params_t params;
170 memset(¶ms, 0, sizeof(params));
171 vm->setParameters(¶ms);
175 /** @brief Start a vm (i.e., boot the guest operating system)
178 * If the VM cannot be started (because of memory over-provisioning), an exception is generated.
180 void MSG_vm_start(msg_vm_t vm)
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");
190 /** @brief Immediately suspend the execution of all processes within the given VM.
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.
196 * No suspension cost occurs.
198 void MSG_vm_suspend(msg_vm_t vm)
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");
208 /** @brief Resume the execution of the VM. All processes on the VM run again.
211 * No resume cost occurs.
213 void MSG_vm_resume(msg_vm_t vm)
216 if (TRACE_msg_vm_is_enabled())
217 simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
220 /** @brief Immediately kills all processes within the given VM.
223 * Any memory that they allocated will be leaked, unless you used #MSG_process_on_exit().
225 * No extra delay occurs. If you want to simulate this too, you want to use a #MSG_process_sleep().
227 void MSG_vm_shutdown(msg_vm_t vm)
232 /** @brief Destroy a VM. Destroy the VM object from the simulation.
235 void MSG_vm_destroy(msg_vm_t vm)
237 if (vm->isMigrating())
238 THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
240 /* First, terminate all processes on the VM if necessary */
243 /* Then, destroy the VM object */
246 if (TRACE_msg_vm_is_enabled()) {
247 container_t container = simgrid::instr::Container::byName(vm->getName());
248 container->removeFromParent();