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 void MSG_vm_set_ramsize(msg_vm_t vm, size_t size)
44 size_t MSG_vm_get_ramsize(msg_vm_t vm)
46 return vm->getRamsize();
49 /* **** Check state of a VM **** */
50 void MSG_vm_set_bound(msg_vm_t vm, double bound)
54 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
56 return vm->pimpl_vm_ != nullptr && vm->getState() == state;
59 /** @brief Returns whether the given VM has just created, not running.
62 int MSG_vm_is_created(msg_vm_t vm)
64 return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
67 /** @brief Returns whether the given VM is currently running
70 int MSG_vm_is_running(msg_vm_t vm)
72 return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
75 /** @brief Returns whether the given VM is currently migrating
78 int MSG_vm_is_migrating(msg_vm_t vm)
80 return vm->isMigrating();
83 /** @brief Returns whether the given VM is currently suspended, not running.
86 int MSG_vm_is_suspended(msg_vm_t vm)
88 return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
91 /* **** ******** MSG vm actions ********* **** */
92 /** @brief Create a new VM with specified parameters.
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.
101 msg_vm_t MSG_vm_create(msg_host_t pm, const char* name, int coreAmount, int ramsize, int mig_netspeed, int dp_intensity)
103 simgrid::vm::VmHostExt::ensureVmExtInstalled();
105 /* For the moment, intensity_rate is the percentage against the migration bandwidth */
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");
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);
116 XBT_DEBUG("migspeed : %f intensity mem : %d", mig_netspeed * 1024 * 1024.0, dp_intensity);
122 /** @brief Create a new VM object with the default parameters
125 * A VM is treated as a host. The name of the VM must be unique among all hosts.
127 msg_vm_t MSG_vm_create_core(msg_host_t pm, const char* name)
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);
132 msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, 1);
135 /** @brief Create a new VM object with the default parameters, but with a specified amount of cores
138 * A VM is treated as a host. The name of the VM must be unique among all hosts.
140 msg_vm_t MSG_vm_create_multicore(msg_host_t pm, const char* name, int coreAmount)
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);
145 msg_vm_t vm = new simgrid::s4u::VirtualMachine(name, pm, coreAmount);
149 /** @brief Start a vm (i.e., boot the guest operating system)
152 * If the VM cannot be started (because of memory over-provisioning), an exception is generated.
154 void MSG_vm_start(msg_vm_t vm)
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");
164 /** @brief Immediately suspend the execution of all processes within the given VM.
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.
170 * No suspension cost occurs.
172 void MSG_vm_suspend(msg_vm_t vm)
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");
182 /** @brief Resume the execution of the VM. All processes on the VM run again.
185 * No resume cost occurs.
187 void MSG_vm_resume(msg_vm_t vm)
190 if (TRACE_msg_vm_is_enabled())
191 simgrid::instr::Container::byName(vm->getName())->getState("MSG_VM_STATE")->popEvent();
194 /** @brief Immediately kills all processes within the given VM.
197 * Any memory that they allocated will be leaked, unless you used #MSG_process_on_exit().
199 * No extra delay occurs. If you want to simulate this too, you want to use a #MSG_process_sleep().
201 void MSG_vm_shutdown(msg_vm_t vm)
206 /** @brief Destroy a VM. Destroy the VM object from the simulation.
209 void MSG_vm_destroy(msg_vm_t vm)
211 if (vm->isMigrating())
212 THROWF(vm_error, 0, "Cannot destroy VM '%s', which is migrating.", vm->getCname());
214 /* First, terminate all processes on the VM if necessary */
217 /* Then, destroy the VM object */
220 if (TRACE_msg_vm_is_enabled()) {
221 container_t container = simgrid::instr::Container::byName(vm->getName());
222 container->removeFromParent();