Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill (unindent) an unamed block in a function
[simgrid.git] / src / surf / virtual_machine.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include <xbt/signal.hpp>
8
9 #include "cpu_cas01.hpp"
10 #include "virtual_machine.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
13
14 simgrid::surf::VMModel *surf_vm_model = nullptr;
15
16 void surf_vm_model_init_HL13(){
17   if (surf_cpu_model_vm) {
18     surf_vm_model = new simgrid::surf::VMModel();
19     all_existing_models->push_back(surf_vm_model);
20   }
21 }
22
23 namespace simgrid {
24 namespace surf {
25
26
27 /*************
28  * Callbacks *
29  *************/
30
31 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmCreation;
32 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmDestruction;
33 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmStateChange;
34
35 /*********
36  * Model *
37  *********/
38
39 std::deque<VirtualMachine*> VirtualMachine::allVms_;
40
41 s4u::Host *VMModel::createVM(const char *name, sg_host_t host_PM)
42 {
43   VirtualMachine* vm = new VirtualMachine(this, name, host_PM);
44   onVmCreation(vm);
45   return vm->piface_;
46 }
47
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;
52
53 double VMModel::next_occuring_event(double now)
54 {
55   /* TODO: update action's cost with the total cost of processes on the VM. */
56
57   /* 1. Now we know how many resource should be assigned to each virtual
58    * machine. We update constraints of the virtual machine layer.
59    *
60    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
61    *     X1 + X2 = C       (Equation 1)
62    * where
63    *    the resource share of VM1: X1
64    *    the resource share of VM2: X2
65    *    the capacity of PM1: C
66    *
67    * Then, if we have two process (P1 and P2) on VM1.
68    *     X1_1 + X1_2 = X1  (Equation 2)
69    * where
70    *    the resource share of P1: X1_1
71    *    the resource share of P2: X1_2
72    *    the capacity of VM1: X1
73    *
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.
77    **/
78
79   /* iterate for all virtual machines */
80   for (VirtualMachine *ws_vm : VirtualMachine::allVms_) {
81     Cpu *cpu = ws_vm->cpu_;
82     xbt_assert(cpu, "cpu-less host");
83
84     double solved_value = ws_vm->action_->getVariable()->value;
85     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->getName(), ws_vm->getPm()->name().c_str());
86
87     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
88     // cpu_cas01->constraint->bound = solved_value;
89     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
90     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
91     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
92   }
93
94   /* 2. Calculate resource share at the virtual machine layer. */
95   adjustWeightOfDummyCpuActions();
96
97   /* 3. Ready. Get the next occuring event */
98   return surf_cpu_model_vm->next_occuring_event(now);
99 }
100
101
102 /************
103  * Resource *
104  ************/
105
106 VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u::Host *host_PM)
107 : HostImpl(model, name, nullptr, nullptr, nullptr)
108 , hostPM_(host_PM)
109 {
110   /* Register this VM to the list of all VMs */
111   allVms_.push_back(this);
112   piface_ = simgrid::s4u::Host::by_name_or_create(name);
113   piface_->extension_set<simgrid::surf::HostImpl>(this);
114
115   /* Currently, we assume a VM has no storage. */
116   storage_ = nullptr;
117
118   /* Currently, a VM uses the network resource of its physical host. In
119    * host_lib, this network resource object is referred from two different keys.
120    * When deregistering the reference that points the network resource object
121    * from the VM name, we have to make sure that the system does not call the
122    * free callback for the network resource object. The network resource object
123    * is still used by the physical machine. */
124   sg_host_t host_VM = simgrid::s4u::Host::by_name_or_create(name);
125   host_VM->pimpl_netcard = host_PM->pimpl_netcard;
126
127   vmState_ = SURF_VM_STATE_CREATED;
128
129   // //// CPU  RELATED STUFF ////
130   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
131   CpuCas01 *sub_cpu = dynamic_cast<CpuCas01*>(host_PM->pimpl_cpu);
132
133   cpu_ = surf_cpu_model_vm->createCpu(host_VM, // the machine hosting the VM
134       sub_cpu->getSpeedPeakList(), 1 /*cores*/);
135   if (sub_cpu->getPState() != 0)
136     cpu_->setPState(sub_cpu->getPState());
137
138   /* We create cpu_action corresponding to a VM process on the host operating system. */
139   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
140   action_ = sub_cpu->execution_start(0);
141
142   XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", name, hostPM_->name().c_str(), xbt_dynar_length(storage_));
143 }
144
145 /*
146  * A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation.
147  */
148 VirtualMachine::~VirtualMachine()
149 {
150   onVmDestruction(this);
151   allVms_.erase( find(allVms_.begin(), allVms_.end(), this) );
152
153   /* Free the cpu_action of the VM. */
154   XBT_ATTRIB_UNUSED int ret = action_->unref();
155   xbt_assert(ret == 1, "Bug: some resource still remains");
156
157   delete cpu_;
158 }
159
160 e_surf_vm_state_t VirtualMachine::getState() {
161   return vmState_;
162 }
163
164 void VirtualMachine::setState(e_surf_vm_state_t state) {
165   vmState_ = state;
166 }
167 void VirtualMachine::turnOn() {
168   if (isOff()) {
169     Resource::turnOn();
170     onVmStateChange(this);
171   }
172 }
173 void VirtualMachine::turnOff() {
174   if (isOn()) {
175     Resource::turnOff();
176     onVmStateChange(this);
177   }
178 }
179 void VirtualMachine::suspend()
180 {
181   action_->suspend();
182   vmState_ = SURF_VM_STATE_SUSPENDED;
183 }
184
185 void VirtualMachine::resume()
186 {
187   action_->resume();
188   vmState_ = SURF_VM_STATE_RUNNING;
189 }
190
191 void VirtualMachine::save()
192 {
193   vmState_ = SURF_VM_STATE_SAVING;
194   action_->suspend();
195   vmState_ = SURF_VM_STATE_SAVED;
196 }
197
198 void VirtualMachine::restore()
199 {
200   vmState_ = SURF_VM_STATE_RESTORING;
201   action_->resume();
202   vmState_ = SURF_VM_STATE_RUNNING;
203 }
204
205 /** @brief returns the physical machine on which the VM is running **/
206 sg_host_t VirtualMachine::getPm() {
207   return hostPM_;
208 }
209
210 /* Update the physical host of the given VM */
211 void VirtualMachine::migrate(sg_host_t host_dest)
212 {
213    HostImpl *surfHost_dst = host_dest->extension<HostImpl>();
214    const char *vm_name = getName();
215    const char *pm_name_src = hostPM_->name().c_str();
216    const char *pm_name_dst = surfHost_dst->getName();
217
218    /* update net_elm with that of the destination physical host */
219    sg_host_by_name(vm_name)->pimpl_netcard = sg_host_by_name(pm_name_dst)->pimpl_netcard;
220
221    hostPM_ = host_dest;
222
223    /* Update vcpu's action for the new pm */
224    /* create a cpu action bound to the pm model at the destination. */
225    CpuAction *new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
226
227    Action::State state = action_->getState();
228    if (state != Action::State::done)
229      XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
230    if (action_->getRemainsNoUpdate() > 0)
231      XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
232
233    /* keep the bound value of the cpu action of the VM. */
234    double old_bound = action_->getBound();
235    if (old_bound != 0) {
236      XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
237      new_cpu_action->setBound(old_bound);
238    }
239
240    XBT_ATTRIB_UNUSED int ret = action_->unref();
241    xbt_assert(ret == 1, "Bug: some resource still remains");
242
243    action_ = new_cpu_action;
244
245    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
246 }
247
248 void VirtualMachine::setBound(double bound){
249  action_->setBound(bound);
250 }
251
252
253 }
254 }