Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename a global to make its purpose clear (hopefully)
[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 "cpu_cas01.hpp"
8 #include "vm_hl13.hpp"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
11
12 void surf_vm_model_init_HL13(void){
13   if (surf_cpu_model_vm) {
14     surf_vm_model = new VMHL13Model();
15     Model *model = surf_vm_model;
16
17     xbt_dynar_push(all_existing_models, &model);
18     xbt_dynar_push(model_list_invoke, &model);
19   }
20 }
21
22 /*********
23  * Model *
24  *********/
25
26 VMHL13Model::VMHL13Model() : VMModel() {}
27
28 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
29
30 /* ind means ''indirect'' that this is a reference on the whole dict_elm
31  * structure (i.e not on the surf_resource_private infos) */
32
33 VirtualMachine *VMHL13Model::createVM(const char *name, surf_resource_t host_PM) {
34   return new VMHL13(this, name, NULL, host_PM);
35 }
36
37 static inline double get_solved_value(CpuAction *cpu_action)
38 {
39   return cpu_action->getVariable()->value;
40 }
41
42 /* In the real world, processes on the guest operating system will be somewhat
43  * degraded due to virtualization overhead. The total CPU share that these
44  * processes get is smaller than that of the VM process gets on a host
45  * operating system. */
46 // const double virt_overhead = 0.95;
47 const double virt_overhead = 1;
48
49 double VMHL13Model::shareResources(double now)
50 {
51   /* TODO: update action's cost with the total cost of processes on the VM. */
52
53
54   /* 0. Make sure that we already calculated the resource share at the physical
55    * machine layer. */
56   {
57     XBT_ATTRIB_UNUSED Model *ws_model = surf_host_model;
58     XBT_ATTRIB_UNUSED Model *vm_ws_model = surf_vm_model;
59     XBT_ATTRIB_UNUSED unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &ws_model);
60     XBT_ATTRIB_UNUSED unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &vm_ws_model);
61     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_host_model comes before");
62
63     /* Another option is that we call sub_ws->share_resource() here. The
64      * share_resource() function has no side-effect. We can call it here to
65      * ensure that. */
66   }
67
68
69   /* 1. Now we know how many resource should be assigned to each virtual
70    * machine. We update constraints of the virtual machine layer.
71    *
72    *
73    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
74    *     X1 + X2 = C       (Equation 1)
75    * where
76    *    the resource share of VM1: X1
77    *    the resource share of VM2: X2
78    *    the capacity of PM1: C
79    *
80    * Then, if we have two process (P1 and P2) on VM1.
81    *     X1_1 + X1_2 = X1  (Equation 2)
82    * where
83    *    the resource share of P1: X1_1
84    *    the resource share of P2: X1_2
85    *    the capacity of VM1: X1
86    *
87    * Equation 1 was solved in the physical machine layer.
88    * Equation 2 is solved in the virtual machine layer (here).
89    * X1 must be passed to the virtual machine laye as a constraint value.
90    *
91    **/
92
93   /* iterate for all virtual machines */
94   for (VMModel::vm_list_t::iterator iter =
95          VMModel::ws_vms.begin();
96        iter !=  VMModel::ws_vms.end(); ++iter) {
97
98     VirtualMachine *ws_vm = &*iter;
99     Cpu *cpu = ws_vm->p_cpu;
100     xbt_assert(cpu, "cpu-less host");
101
102     double solved_value = get_solved_value(ws_vm->p_action);
103     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
104         ws_vm->getName(), ws_vm->p_subWs->getName());
105
106     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
107     // cpu_cas01->constraint->bound = solved_value;
108     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
109     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
110     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
111   }
112
113
114   /* 2. Calculate resource share at the virtual machine layer. */
115   adjustWeightOfDummyCpuActions();
116
117   double min_by_cpu = surf_cpu_model_vm->shareResources(now);
118   double min_by_net = surf_network_model->shareResourcesIsIdempotent() ? surf_network_model->shareResources(now) : -1;
119   // Fixme: take storage into account once it's implemented
120   double min_by_sto = -1;
121
122   XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f, %s min_by_sto %f",
123       this, typeid(surf_cpu_model_pm ).name(), min_by_cpu,
124                 typeid(surf_network_model).name(), min_by_net,
125             typeid(surf_storage_model).name(), min_by_sto);
126
127   double ret = max(max(min_by_cpu, min_by_net), min_by_sto);
128   if (min_by_cpu >= 0.0 && min_by_cpu < ret)
129         ret = min_by_cpu;
130   if (min_by_net >= 0.0 && min_by_net < ret)
131         ret = min_by_net;
132   if (min_by_sto >= 0.0 && min_by_sto < ret)
133         ret = min_by_sto;
134
135   return ret;
136 }
137
138 Action *VMHL13Model::executeParallelTask(int host_nb,
139                                          sg_host_t *host_list,
140                                                                                  double *flops_amount,
141                                                                                  double *bytes_amount,
142                                                                                  double rate){
143 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
144   if ((host_nb == 1)
145       && (cost_or_zero(bytes_amount, 0) == 0.0))
146     return surf_host_execute(host_list[0], flops_amount[0]);
147   else if ((host_nb == 1)
148            && (cost_or_zero(flops_amount, 0) == 0.0))
149     return surf_network_model_communicate(surf_network_model, host_list[0], host_list[0],bytes_amount[0], rate);
150   else if ((host_nb == 2)
151              && (cost_or_zero(flops_amount, 0) == 0.0)
152              && (cost_or_zero(flops_amount, 1) == 0.0)) {
153     int i,nb = 0;
154     double value = 0.0;
155
156     for (i = 0; i < host_nb * host_nb; i++) {
157       if (cost_or_zero(bytes_amount, i) > 0.0) {
158         nb++;
159         value = cost_or_zero(bytes_amount, i);
160       }
161     }
162     if (nb == 1)
163       return surf_network_model_communicate(surf_network_model, host_list[0], host_list[1], value, rate);
164   }
165 #undef cost_or_zero
166
167   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks for more than 2 hosts. */
168   return NULL;
169 }
170
171 /************
172  * Resource *
173  ************/
174
175 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
176                                                    surf_resource_t host_PM)
177  : VirtualMachine(model, name, props, NULL, NULL)
178 {
179   Host *sub_ws = surf_host_resource_priv(host_PM);
180
181   /* Currently, we assume a VM has no storage. */
182   p_storage = NULL;
183
184   /* Currently, a VM uses the network resource of its physical host. In
185    * host_lib, this network resource object is referred from two different keys.
186    * When deregistering the reference that points the network resource object
187    * from the VM name, we have to make sure that the system does not call the
188    * free callback for the network resource object. The network resource object
189    * is still used by the physical machine. */
190   sg_host_t sg_sub_ws = sg_host_by_name_or_create(sub_ws->getName());
191   p_netElm = new RoutingEdgeWrapper(sg_host_edge(sg_sub_ws));
192   sg_host_edge_set(sg_host_by_name_or_create(name), p_netElm);
193
194   p_subWs = sub_ws;
195   p_currentState = SURF_VM_STATE_CREATED;
196
197   // //// CPU  RELATED STUFF ////
198   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
199   CpuCas01 *sub_cpu = static_cast<CpuCas01*>(sg_host_surfcpu(host_PM));
200
201   p_cpu = surf_cpu_model_vm->createCpu(name, // name
202       sub_cpu->getPowerPeakList(),        // host->power_peak,
203       sub_cpu->getPState(),
204       1,                          // host->power_scale,
205       NULL,                       // host->power_trace,
206       1,                          // host->core_amount,
207       SURF_RESOURCE_ON,           // host->initial_state,
208       NULL,                       // host->state_trace,
209       NULL);                       // host->properties,
210
211   /* We create cpu_action corresponding to a VM process on the host operating system. */
212   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
213   // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(host_PM, GUESTOS_NOISE);
214   p_action = sub_cpu->execute(0);
215
216   XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->getName(), xbt_dynar_length(p_storage));
217
218   surf_callback_emit(VMCreatedCallbacks, this);
219 }
220
221 /*
222  * A physical host does not disappear in the current SimGrid code, but a VM may
223  * disappear during a simulation.
224  */
225 VMHL13::~VMHL13()
226 {
227   /* Free the cpu_action of the VM. */
228   XBT_ATTRIB_UNUSED int ret = p_action->unref();
229   xbt_assert(ret == 1, "Bug: some resource still remains");
230 }
231
232 void VMHL13::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/) {
233   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
234 }
235
236 bool VMHL13::isUsed() {
237   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
238   return -1;
239 }
240
241 e_surf_resource_state_t VMHL13::getState()
242 {
243   return (e_surf_resource_state_t) p_currentState;
244 }
245
246 void VMHL13::setState(e_surf_resource_state_t state)
247 {
248   p_currentState = (e_surf_vm_state_t) state;
249 }
250
251 void VMHL13::suspend()
252 {
253   p_action->suspend();
254   p_currentState = SURF_VM_STATE_SUSPENDED;
255 }
256
257 void VMHL13::resume()
258 {
259   p_action->resume();
260   p_currentState = SURF_VM_STATE_RUNNING;
261 }
262
263 void VMHL13::save()
264 {
265   p_currentState = SURF_VM_STATE_SAVING;
266
267   /* FIXME: do something here */
268   p_action->suspend();
269   p_currentState = SURF_VM_STATE_SAVED;
270 }
271
272 void VMHL13::restore()
273 {
274   p_currentState = SURF_VM_STATE_RESTORING;
275
276   /* FIXME: do something here */
277   p_action->resume();
278   p_currentState = SURF_VM_STATE_RUNNING;
279 }
280
281 /*
282  * Update the physical host of the given VM
283  */
284 void VMHL13::migrate(surf_resource_t ind_dst_pm)
285 {
286    /* ind_dst_pm equals to smx_host_t */
287    Host *ws_dst = static_cast<Host*>(surf_host_resource_priv(ind_dst_pm));
288    const char *vm_name = getName();
289    const char *pm_name_src = p_subWs->getName();
290    const char *pm_name_dst = ws_dst->getName();
291
292    xbt_assert(ws_dst);
293
294    /* update net_elm with that of the destination physical host */
295    RoutingEdge *old_net_elm = p_netElm;
296    RoutingEdge *new_net_elm = new RoutingEdgeWrapper(sg_host_edge(sg_host_by_name(pm_name_dst)));
297    xbt_assert(new_net_elm);
298
299    /* Unregister the current net_elm from host_lib. Do not call the free callback. */
300    sg_host_edge_destroy(sg_host_by_name(vm_name), 0);
301
302    /* Then, resister the new one. */
303    p_netElm = new_net_elm;
304    sg_host_edge_set(sg_host_by_name(vm_name), p_netElm);
305
306    p_subWs = ws_dst;
307
308    /* Update vcpu's action for the new pm */
309    {
310      /* create a cpu action bound to the pm model at the destination. */
311      CpuAction *new_cpu_action = static_cast<CpuAction*>(
312                                             static_cast<Cpu*>(sg_host_surfcpu(ind_dst_pm))->execute(0));
313
314      e_surf_action_state_t state = p_action->getState();
315      if (state != SURF_ACTION_DONE)
316        XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
317      if (p_action->getRemainsNoUpdate() > 0)
318        XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
319
320      /* keep the bound value of the cpu action of the VM. */
321      double old_bound = p_action->getBound();
322      if (old_bound != 0) {
323        XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
324        new_cpu_action->setBound(old_bound);
325      }
326
327      XBT_ATTRIB_UNUSED int ret = p_action->unref();
328      xbt_assert(ret == 1, "Bug: some resource still remains");
329
330      p_action = new_cpu_action;
331    }
332
333    XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
334    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
335    delete old_net_elm;
336 }
337
338 void VMHL13::setBound(double bound){
339  p_action->setBound(bound);
340 }
341
342 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
343  p_action->setAffinity(cpu, mask);
344 }
345
346 /*
347  * A surf level object will be useless in the upper layer. Returning the
348  * dict_elm of the host.
349  **/
350 surf_resource_t VMHL13::getPm()
351 {
352   return xbt_lib_get_elm_or_null(host_lib, p_subWs->getName());
353 }
354
355 /* Adding a task to a VM updates the VCPU task on its physical machine. */
356 Action *VMHL13::execute(double size)
357 {
358   double old_cost = p_action->getCost();
359   double new_cost = old_cost + size;
360
361   XBT_DEBUG("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
362       getName(), p_subWs->getName(),
363       old_cost, new_cost);
364
365   p_action->setCost(new_cost);
366
367   return p_cpu->execute(size);
368 }
369
370 Action *VMHL13::sleep(double duration) {
371   return p_cpu->sleep(duration);
372 }