Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e74223a10fbaaf9319e1802109514694d509816
[simgrid.git] / src / plugins / vm / s4u_VirtualMachine.cpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
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"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_vm, "S4U virtual machines");
13
14 namespace simgrid {
15 namespace s4u {
16
17 VirtualMachine::VirtualMachine(const char* name, s4u::Host* pm, int coreAmount)
18     : Host(name), pimpl_vm_(new vm::VirtualMachineImpl(this, pm, coreAmount))
19 {
20   XBT_DEBUG("Create VM %s", name);
21
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);
26
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());
30
31   /* Make a process container */
32   extension_set<simgrid::simix::Host>(new simgrid::simix::Host());
33
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);
37   }
38 }
39
40 VirtualMachine::~VirtualMachine()
41 {
42   onDestruction(*this);
43
44   XBT_DEBUG("destroy %s", getCname());
45
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...
48    *
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)
51    */
52   extension_set<simgrid::simix::Host>(nullptr);
53
54   /* Don't free these things twice: they are the ones of my physical host */
55   pimpl_netpoint = nullptr;
56 }
57
58 void VirtualMachine::start()
59 {
60   simgrid::simix::kernelImmediate([this]() {
61     simgrid::vm::VmHostExt::ensureVmExtInstalled();
62
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());
66
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();
70
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();
77
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(),
82                this->getCname());
83       }
84     }
85
86     this->pimpl_vm_->setState(SURF_VM_STATE_RUNNING);
87   });
88 }
89
90 bool VirtualMachine::isMigrating()
91 {
92   return pimpl_vm_ && pimpl_vm_->isMigrating;
93 }
94 double VirtualMachine::getRamsize()
95 {
96   return pimpl_vm_->params_.ramsize;
97 }
98 simgrid::s4u::Host* VirtualMachine::getPm()
99 {
100   return pimpl_vm_->getPm();
101 }
102 e_surf_vm_state_t VirtualMachine::getState()
103 {
104   return pimpl_vm_->getState();
105 }
106
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)
110 {
111   pimpl_vm_->getParams(params);
112 }
113 /** @brief Sets the params of that VM/PM */
114 void VirtualMachine::setParameters(vm_params_t params)
115 {
116   simgrid::simix::kernelImmediate([this, params] { pimpl_vm_->setParams(params); });
117 }
118
119 } // namespace simgrid
120 } // namespace s4u