1 /* Copyright (c) 2013-2015. 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. */
7 #include "VirtualMachineImpl.hpp"
9 #include <xbt/signal.hpp>
11 #include "cpu_cas01.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
15 simgrid::surf::VMModel* surf_vm_model = nullptr;
17 void surf_vm_model_init_HL13()
19 if (surf_cpu_model_vm) {
20 surf_vm_model = new simgrid::surf::VMModel();
21 all_existing_models->push_back(surf_vm_model);
32 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmCreation;
33 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmDestruction;
34 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmStateChange;
40 std::deque<VirtualMachineImpl*> VirtualMachineImpl::allVms_;
42 s4u::Host* VMModel::createVM(const char* name, sg_host_t host_PM)
44 VirtualMachineImpl* vm = new VirtualMachineImpl(name, host_PM);
48 /* In the real world, processes on the guest operating system will be somewhat degraded due to virtualization overhead.
49 * The total CPU share these processes get is smaller than that of the VM process gets on a host operating system. */
50 // const double virt_overhead = 0.95;
51 const double virt_overhead = 1;
53 double VMModel::nextOccuringEvent(double now)
55 /* TODO: update action's cost with the total cost of processes on the VM. */
57 /* 1. Now we know how many resource should be assigned to each virtual
58 * machine. We update constraints of the virtual machine layer.
60 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
61 * X1 + X2 = C (Equation 1)
63 * the resource share of VM1: X1
64 * the resource share of VM2: X2
65 * the capacity of PM1: C
67 * Then, if we have two process (P1 and P2) on VM1.
68 * X1_1 + X1_2 = X1 (Equation 2)
70 * the resource share of P1: X1_1
71 * the resource share of P2: X1_2
72 * the capacity of VM1: X1
74 * Equation 1 was solved in the physical machine layer.
75 * Equation 2 is solved in the virtual machine layer (here).
76 * X1 must be passed to the virtual machine laye as a constraint value.
79 /* iterate for all virtual machines */
80 for (VirtualMachineImpl* ws_vm : VirtualMachineImpl::allVms_) {
81 Cpu* cpu = ws_vm->piface_->pimpl_cpu;
82 xbt_assert(cpu, "cpu-less host");
84 double solved_value = ws_vm->action_->getVariable()->value;
85 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->piface_->name().c_str(),
86 ws_vm->getPm()->name().c_str());
88 // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
89 // cpu_cas01->constraint->bound = solved_value;
90 xbt_assert(cpu->getModel() == surf_cpu_model_vm);
91 lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
92 lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
95 /* 2. Calculate resource share at the virtual machine layer. */
96 adjustWeightOfDummyCpuActions();
98 /* 3. Ready. Get the next occuring event */
99 return surf_cpu_model_vm->nextOccuringEvent(now);
106 VirtualMachineImpl::VirtualMachineImpl(const char* name, simgrid::s4u::Host* host_PM)
107 : HostImpl(new simgrid::s4u::Host(name), nullptr /*storage*/), hostPM_(host_PM)
109 /* Register this VM to the list of all VMs */
110 allVms_.push_back(this);
112 /* Currently, a VM uses the network resource of its physical host. In
113 * host_lib, this network resource object is referred from two different keys.
114 * When deregistering the reference that points the network resource object
115 * from the VM name, we have to make sure that the system does not call the
116 * free callback for the network resource object. The network resource object
117 * is still used by the physical machine. */
118 piface_->pimpl_netcard = host_PM->pimpl_netcard;
120 // //// CPU RELATED STUFF ////
121 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
122 CpuCas01* sub_cpu = dynamic_cast<CpuCas01*>(host_PM->pimpl_cpu);
124 piface_->pimpl_cpu = surf_cpu_model_vm->createCpu(piface_, sub_cpu->getSpeedPeakList(), 1 /*cores*/);
125 if (sub_cpu->getPState() != 0)
126 piface_->pimpl_cpu->setPState(sub_cpu->getPState());
128 /* We create cpu_action corresponding to a VM process on the host operating system. */
129 /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
130 action_ = sub_cpu->execution_start(0);
132 XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", name, hostPM_->name().c_str(), xbt_dynar_length(storage_));
136 * A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation.
138 VirtualMachineImpl::~VirtualMachineImpl()
140 onVmDestruction(this);
141 allVms_.erase(find(allVms_.begin(), allVms_.end(), this));
143 /* Free the cpu_action of the VM. */
144 XBT_ATTRIB_UNUSED int ret = action_->unref();
145 xbt_assert(ret == 1, "Bug: some resource still remains");
148 e_surf_vm_state_t VirtualMachineImpl::getState()
153 void VirtualMachineImpl::setState(e_surf_vm_state_t state)
157 void VirtualMachineImpl::suspend()
160 vmState_ = SURF_VM_STATE_SUSPENDED;
163 void VirtualMachineImpl::resume()
166 vmState_ = SURF_VM_STATE_RUNNING;
169 void VirtualMachineImpl::save()
171 vmState_ = SURF_VM_STATE_SAVING;
173 vmState_ = SURF_VM_STATE_SAVED;
176 void VirtualMachineImpl::restore()
178 vmState_ = SURF_VM_STATE_RESTORING;
180 vmState_ = SURF_VM_STATE_RUNNING;
183 /** @brief returns the physical machine on which the VM is running **/
184 sg_host_t VirtualMachineImpl::getPm()
189 /* Update the physical host of the given VM */
190 void VirtualMachineImpl::migrate(sg_host_t host_dest)
192 const char* vm_name = piface_->name().c_str();
193 const char* pm_name_src = hostPM_->name().c_str();
194 const char* pm_name_dst = host_dest->name().c_str();
196 /* update net_elm with that of the destination physical host */
197 piface_->pimpl_netcard = host_dest->pimpl_netcard;
201 /* Update vcpu's action for the new pm */
202 /* create a cpu action bound to the pm model at the destination. */
203 CpuAction* new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
205 Action::State state = action_->getState();
206 if (state != Action::State::done)
207 XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
208 if (action_->getRemainsNoUpdate() > 0)
209 XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
211 /* keep the bound value of the cpu action of the VM. */
212 double old_bound = action_->getBound();
213 if (old_bound != 0) {
214 XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
215 new_cpu_action->setBound(old_bound);
218 XBT_ATTRIB_UNUSED int ret = action_->unref();
219 xbt_assert(ret == 1, "Bug: some resource still remains");
221 action_ = new_cpu_action;
223 XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
226 void VirtualMachineImpl::setBound(double bound)
228 action_->setBound(bound);