Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix out of tree builds
[simgrid.git] / src / surf / vm_hl13.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 <algorithm>
8
9 #include <simgrid/host.h>
10
11 #include "cpu_cas01.hpp"
12 #include "vm_hl13.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
15
16 void surf_vm_model_init_HL13(void){
17   if (surf_cpu_model_vm) {
18     surf_vm_model = new simgrid::surf::VMHL13Model();
19     simgrid::surf::Model *model = surf_vm_model;
20     xbt_dynar_push(all_existing_models, &model);
21   }
22 }
23
24 namespace simgrid {
25 namespace surf {
26
27 /*********
28  * Model *
29  *********/
30
31 VMHL13Model::VMHL13Model() : VMModel() {}
32
33 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
34
35 /* ind means ''indirect'' that this is a reference on the whole dict_elm
36  * structure (i.e not on the surf_resource_private infos) */
37
38 VirtualMachine *VMHL13Model::createVM(const char *name, sg_host_t host_PM)
39 {
40   VirtualMachine* vm = new VMHL13(this, name, NULL, host_PM);
41   VMCreatedCallbacks(vm);
42   return vm;
43 }
44
45 /* In the real world, processes on the guest operating system will be somewhat
46  * degraded due to virtualization overhead. The total CPU share that these
47  * processes get is smaller than that of the VM process gets on a host
48  * operating system. */
49 // const double virt_overhead = 0.95;
50 const double virt_overhead = 1;
51
52 double VMHL13Model::next_occuring_event(double now)
53 {
54   /* TODO: update action's cost with the total cost of processes on the VM. */
55
56   /* 1. Now we know how many resource should be assigned to each virtual
57    * machine. We update constraints of the virtual machine layer.
58    *
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
80   /* iterate for all virtual machines */
81   for (VMModel::vm_list_t::iterator iter =
82          VMModel::ws_vms.begin();
83        iter !=  VMModel::ws_vms.end(); ++iter) {
84
85     VirtualMachine *ws_vm = &*iter;
86     Cpu *cpu = ws_vm->p_cpu;
87     xbt_assert(cpu, "cpu-less host");
88
89     double solved_value = ws_vm->p_action->getVariable()->value;
90     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
91         ws_vm->getName(), ws_vm->p_hostPM->name().c_str());
92
93     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
94     // cpu_cas01->constraint->bound = solved_value;
95     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
96     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
97     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
98   }
99
100
101   /* 2. Calculate resource share at the virtual machine layer. */
102   adjustWeightOfDummyCpuActions();
103
104   /* 3. Ready. Get the next occuring event */
105   return surf_cpu_model_vm->next_occuring_event(now);
106 }
107
108 /************
109  * Resource *
110  ************/
111
112 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props, sg_host_t host_PM)
113  : VirtualMachine(model, name, props, host_PM)
114 {
115   /* Currently, we assume a VM has no storage. */
116   p_storage = NULL;
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 = sg_host_by_name_or_create(name);
125   host_VM->pimpl_netcard = host_PM->pimpl_netcard;
126
127   p_vm_state = 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 = static_cast<CpuCas01*>(host_PM->pimpl_cpu);
132
133   p_cpu = surf_cpu_model_vm->createCpu(host_VM, // the machine hosting the VM
134       sub_cpu->getSpeedPeakList(),        // host->power_peak,
135       NULL,                       // host->power_trace,
136       1,                          // host->core_amount,
137       NULL);                      // host->state_trace,
138   if (sub_cpu->getPState() != 0)
139     p_cpu->setPState(sub_cpu->getPState());
140
141   /* We create cpu_action corresponding to a VM process on the host operating system. */
142   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
143   p_action = sub_cpu->execution_start(0);
144
145   XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks",
146     name, p_hostPM->name().c_str(), xbt_dynar_length(p_storage));
147 }
148
149 void VMHL13::suspend()
150 {
151   p_action->suspend();
152   p_vm_state = SURF_VM_STATE_SUSPENDED;
153 }
154
155 void VMHL13::resume()
156 {
157   p_action->resume();
158   p_vm_state = SURF_VM_STATE_RUNNING;
159 }
160
161 void VMHL13::save()
162 {
163   p_vm_state = SURF_VM_STATE_SAVING;
164
165   /* FIXME: do something here */
166   p_action->suspend();
167   p_vm_state = SURF_VM_STATE_SAVED;
168 }
169
170 void VMHL13::restore()
171 {
172   p_vm_state = SURF_VM_STATE_RESTORING;
173
174   /* FIXME: do something here */
175   p_action->resume();
176   p_vm_state = SURF_VM_STATE_RUNNING;
177 }
178
179 /*
180  * Update the physical host of the given VM
181  */
182 void VMHL13::migrate(sg_host_t host_dest)
183 {
184    HostImpl *surfHost_dst = host_dest->extension<HostImpl>();
185    const char *vm_name = getName();
186    const char *pm_name_src = p_hostPM->name().c_str();
187    const char *pm_name_dst = surfHost_dst->getName();
188
189    /* update net_elm with that of the destination physical host */
190    sg_host_by_name(vm_name)->pimpl_netcard = sg_host_by_name(pm_name_dst)->pimpl_netcard;
191
192    p_hostPM = host_dest;
193
194    /* Update vcpu's action for the new pm */
195    {
196      /* create a cpu action bound to the pm model at the destination. */
197      CpuAction *new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
198
199      e_surf_action_state_t state = p_action->getState();
200      if (state != SURF_ACTION_DONE)
201        XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
202      if (p_action->getRemainsNoUpdate() > 0)
203        XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
204
205      /* keep the bound value of the cpu action of the VM. */
206      double old_bound = p_action->getBound();
207      if (old_bound != 0) {
208        XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
209        new_cpu_action->setBound(old_bound);
210      }
211
212      XBT_ATTRIB_UNUSED int ret = p_action->unref();
213      xbt_assert(ret == 1, "Bug: some resource still remains");
214
215      p_action = new_cpu_action;
216    }
217
218    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
219 }
220
221 void VMHL13::setBound(double bound){
222  p_action->setBound(bound);
223 }
224
225 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
226  p_action->setAffinity(cpu, mask);
227 }
228
229
230 }
231 }