1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/instr/instr_private.hpp"
7 #include "src/plugins/vm/VirtualMachineImpl.hpp"
8 #include "src/plugins/vm/VmHostExt.hpp"
9 #include "src/simix/smx_host_private.hpp"
10 #include "src/surf/cpu_cas01.hpp"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_vm, "S4U virtual machines");
17 VirtualMachine::VirtualMachine(const char* name, s4u::Host* pm, int coreAmount)
18 : Host(name), pimpl_vm_(new vm::VirtualMachineImpl(this, pm, coreAmount))
20 XBT_DEBUG("Create VM %s", name);
22 /* Currently, a VM uses the network resource of its physical host */
23 pimpl_netpoint = pm->pimpl_netpoint;
24 // Create a VCPU for this VM
25 surf::CpuCas01* sub_cpu = dynamic_cast<surf::CpuCas01*>(pm->pimpl_cpu);
27 pimpl_cpu = surf_cpu_model_vm->createCpu(this, sub_cpu->getSpeedPeakList(), coreAmount);
28 if (sub_cpu->getPState() != 0)
29 pimpl_cpu->setPState(sub_cpu->getPState());
31 /* Make a process container */
32 extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
34 if (TRACE_msg_vm_is_enabled()) {
35 container_t host_container = simgrid::instr::Container::byName(pm->getName());
36 new simgrid::instr::Container(name, simgrid::instr::INSTR_MSG_VM, host_container);
40 VirtualMachine::~VirtualMachine()
44 XBT_DEBUG("destroy %s", getCname());
46 /* FIXME: this is really strange that everything fails if the next line is removed.
47 * This is as if we shared these data with the PM, which definitely should not be the case...
49 * We need to test that suspending a VM does not suspends the processes running on its PM, for example.
50 * Or we need to simplify this code enough to make it actually readable (but this sounds harder than testing)
52 extension_set<simgrid::simix::Host>(nullptr);
54 /* Don't free these things twice: they are the ones of my physical host */
55 pimpl_netpoint = nullptr;
58 void VirtualMachine::start()
60 simgrid::simix::kernelImmediate([this]() {
61 simgrid::vm::VmHostExt::ensureVmExtInstalled();
63 simgrid::s4u::Host* pm = this->pimpl_vm_->getPm();
64 if (pm->extension<simgrid::vm::VmHostExt>() == nullptr)
65 pm->extension_set(new simgrid::vm::VmHostExt());
67 long pm_ramsize = pm->extension<simgrid::vm::VmHostExt>()->ramsize;
68 int pm_overcommit = pm->extension<simgrid::vm::VmHostExt>()->overcommit;
69 long vm_ramsize = this->getRamsize();
71 if (pm_ramsize && not pm_overcommit) { /* Only verify that we don't overcommit on need */
72 /* Retrieve the memory occupied by the VMs on that host. Yep, we have to traverse all VMs of all hosts for that */
73 long total_ramsize_of_vms = 0;
74 for (simgrid::s4u::VirtualMachine* const& ws_vm : simgrid::vm::VirtualMachineImpl::allVms_)
75 if (pm == ws_vm->pimpl_vm_->getPm())
76 total_ramsize_of_vms += ws_vm->pimpl_vm_->getRamsize();
78 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
79 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
80 this->getCname(), pm->getCname(), vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
81 THROWF(vm_error, 0, "Memory shortage on host '%s', VM '%s' cannot be started", pm->getCname(),
86 this->pimpl_vm_->setState(SURF_VM_STATE_RUNNING);
90 bool VirtualMachine::isMigrating()
92 return pimpl_vm_ && pimpl_vm_->isMigrating;
94 double VirtualMachine::getRamsize()
96 return pimpl_vm_->params_.ramsize;
98 simgrid::s4u::Host* VirtualMachine::getPm()
100 return pimpl_vm_->getPm();
102 e_surf_vm_state_t VirtualMachine::getState()
104 return pimpl_vm_->getState();
107 /** @brief Retrieve a copy of the parameters of that VM/PM
108 * @details The ramsize and overcommit fields are used on the PM too */
109 void VirtualMachine::getParameters(vm_params_t params)
111 pimpl_vm_->getParams(params);
113 /** @brief Sets the params of that VM/PM */
114 void VirtualMachine::setParameters(vm_params_t params)
116 simgrid::simix::kernelImmediate([this, params] { pimpl_vm_->setParams(params); });
119 } // namespace simgrid