Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aee9b9927d81581a032d33deabb502ea82434a80
[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 VMHL13Model::VMHL13Model() : VMModel() {}
31
32 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
33
34 VirtualMachine *VMHL13Model::createVM(const char *name, sg_host_t host_PM)
35 {
36   VirtualMachine* vm = new VMHL13(this, name, NULL, host_PM);
37   VMCreatedCallbacks(vm);
38   return vm;
39 }
40
41 /* In the real world, processes on the guest operating system will be somewhat degraded due to virtualization overhead.
42  * The total CPU share these processes get is smaller than that of the VM process gets on a host operating system. */
43 // const double virt_overhead = 0.95;
44 const double virt_overhead = 1;
45
46 double VMHL13Model::next_occuring_event(double now)
47 {
48   /* TODO: update action's cost with the total cost of processes on the VM. */
49
50   /* 1. Now we know how many resource should be assigned to each virtual
51    * machine. We update constraints of the virtual machine layer.
52    *
53    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
54    *     X1 + X2 = C       (Equation 1)
55    * where
56    *    the resource share of VM1: X1
57    *    the resource share of VM2: X2
58    *    the capacity of PM1: C
59    *
60    * Then, if we have two process (P1 and P2) on VM1.
61    *     X1_1 + X1_2 = X1  (Equation 2)
62    * where
63    *    the resource share of P1: X1_1
64    *    the resource share of P2: X1_2
65    *    the capacity of VM1: X1
66    *
67    * Equation 1 was solved in the physical machine layer.
68    * Equation 2 is solved in the virtual machine layer (here).
69    * X1 must be passed to the virtual machine laye as a constraint value.
70    **/
71
72   /* iterate for all virtual machines */
73   for (VMModel::vm_list_t::iterator iter = VMModel::ws_vms.begin(); iter !=  VMModel::ws_vms.end(); ++iter) {
74     VirtualMachine *ws_vm = &*iter;
75     Cpu *cpu = ws_vm->p_cpu;
76     xbt_assert(cpu, "cpu-less host");
77
78     double solved_value = ws_vm->p_action->getVariable()->value;
79     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->getName(), ws_vm->p_hostPM->name().c_str());
80
81     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
82     // cpu_cas01->constraint->bound = solved_value;
83     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
84     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
85     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
86   }
87
88   /* 2. Calculate resource share at the virtual machine layer. */
89   adjustWeightOfDummyCpuActions();
90
91   /* 3. Ready. Get the next occuring event */
92   return surf_cpu_model_vm->next_occuring_event(now);
93 }
94
95 /************
96  * Resource *
97  ************/
98 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props, sg_host_t host_PM)
99  : VirtualMachine(model, name, props, host_PM)
100 {
101   /* Currently, we assume a VM has no storage. */
102   p_storage = NULL;
103
104   /* Currently, a VM uses the network resource of its physical host. In
105    * host_lib, this network resource object is referred from two different keys.
106    * When deregistering the reference that points the network resource object
107    * from the VM name, we have to make sure that the system does not call the
108    * free callback for the network resource object. The network resource object
109    * is still used by the physical machine. */
110   sg_host_t host_VM = sg_host_by_name_or_create(name);
111   host_VM->pimpl_netcard = host_PM->pimpl_netcard;
112
113   p_vm_state = SURF_VM_STATE_CREATED;
114
115   // //// CPU  RELATED STUFF ////
116   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
117   CpuCas01 *sub_cpu = static_cast<CpuCas01*>(host_PM->pimpl_cpu);
118
119   p_cpu = surf_cpu_model_vm->createCpu(host_VM, // the machine hosting the VM
120       sub_cpu->getSpeedPeakList(), 1 /*cores*/);
121   if (sub_cpu->getPState() != 0)
122     p_cpu->setPState(sub_cpu->getPState());
123
124   /* We create cpu_action corresponding to a VM process on the host operating system. */
125   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
126   p_action = sub_cpu->execution_start(0);
127
128   XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", name, p_hostPM->name().c_str(), xbt_dynar_length(p_storage));
129 }
130
131 void VMHL13::suspend()
132 {
133   p_action->suspend();
134   p_vm_state = SURF_VM_STATE_SUSPENDED;
135 }
136
137 void VMHL13::resume()
138 {
139   p_action->resume();
140   p_vm_state = SURF_VM_STATE_RUNNING;
141 }
142
143 void VMHL13::save()
144 {
145   p_vm_state = SURF_VM_STATE_SAVING;
146
147   /* FIXME: do something here */
148   p_action->suspend();
149   p_vm_state = SURF_VM_STATE_SAVED;
150 }
151
152 void VMHL13::restore()
153 {
154   p_vm_state = SURF_VM_STATE_RESTORING;
155
156   /* FIXME: do something here */
157   p_action->resume();
158   p_vm_state = SURF_VM_STATE_RUNNING;
159 }
160
161 /* Update the physical host of the given VM */
162 void VMHL13::migrate(sg_host_t host_dest)
163 {
164    HostImpl *surfHost_dst = host_dest->extension<HostImpl>();
165    const char *vm_name = getName();
166    const char *pm_name_src = p_hostPM->name().c_str();
167    const char *pm_name_dst = surfHost_dst->getName();
168
169    /* update net_elm with that of the destination physical host */
170    sg_host_by_name(vm_name)->pimpl_netcard = sg_host_by_name(pm_name_dst)->pimpl_netcard;
171
172    p_hostPM = host_dest;
173
174    /* Update vcpu's action for the new pm */
175    {
176      /* create a cpu action bound to the pm model at the destination. */
177      CpuAction *new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
178
179      Action::State state = p_action->getState();
180      if (state != Action::State::done)
181        XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
182      if (p_action->getRemainsNoUpdate() > 0)
183        XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
184
185      /* keep the bound value of the cpu action of the VM. */
186      double old_bound = p_action->getBound();
187      if (old_bound != 0) {
188        XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
189        new_cpu_action->setBound(old_bound);
190      }
191
192      XBT_ATTRIB_UNUSED int ret = p_action->unref();
193      xbt_assert(ret == 1, "Bug: some resource still remains");
194
195      p_action = new_cpu_action;
196    }
197
198    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
199 }
200
201 void VMHL13::setBound(double bound){
202  p_action->setBound(bound);
203 }
204
205 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
206  p_action->setAffinity(cpu, mask);
207 }
208
209 }
210 }