Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tiny cleanups in surf::VM
[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 <simgrid/host.h>
8
9 #include "cpu_cas01.hpp"
10 #include "vm_hl13.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
13
14 void surf_vm_model_init_HL13(){
15   if (surf_cpu_model_vm) {
16     surf_vm_model = new simgrid::surf::VMHL13Model();
17     all_existing_models->push_back(surf_vm_model);
18   }
19 }
20
21 namespace simgrid {
22 namespace surf {
23
24 /*********
25  * Model *
26  *********/
27 VMHL13Model::VMHL13Model() : VMModel() {}
28
29 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
30
31 s4u::Host *VMHL13Model::createVM(const char *name, sg_host_t host_PM)
32 {
33   VirtualMachine* vm = new VMHL13(this, name, host_PM);
34   onVmCreation(vm);
35   return vm->piface_;
36 }
37
38 /* In the real world, processes on the guest operating system will be somewhat degraded due to virtualization overhead.
39  * The total CPU share these processes get is smaller than that of the VM process gets on a host operating system. */
40 // const double virt_overhead = 0.95;
41 const double virt_overhead = 1;
42
43 double VMHL13Model::next_occuring_event(double now)
44 {
45   /* TODO: update action's cost with the total cost of processes on the VM. */
46
47   /* 1. Now we know how many resource should be assigned to each virtual
48    * machine. We update constraints of the virtual machine layer.
49    *
50    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
51    *     X1 + X2 = C       (Equation 1)
52    * where
53    *    the resource share of VM1: X1
54    *    the resource share of VM2: X2
55    *    the capacity of PM1: C
56    *
57    * Then, if we have two process (P1 and P2) on VM1.
58    *     X1_1 + X1_2 = X1  (Equation 2)
59    * where
60    *    the resource share of P1: X1_1
61    *    the resource share of P2: X1_2
62    *    the capacity of VM1: X1
63    *
64    * Equation 1 was solved in the physical machine layer.
65    * Equation 2 is solved in the virtual machine layer (here).
66    * X1 must be passed to the virtual machine laye as a constraint value.
67    **/
68
69   /* iterate for all virtual machines */
70   for (VirtualMachine *ws_vm : VirtualMachine::allVms_) {
71     Cpu *cpu = ws_vm->cpu_;
72     xbt_assert(cpu, "cpu-less host");
73
74     double solved_value = ws_vm->action_->getVariable()->value;
75     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->getName(), ws_vm->getPm()->name().c_str());
76
77     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
78     // cpu_cas01->constraint->bound = solved_value;
79     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
80     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
81     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
82   }
83
84   /* 2. Calculate resource share at the virtual machine layer. */
85   adjustWeightOfDummyCpuActions();
86
87   /* 3. Ready. Get the next occuring event */
88   return surf_cpu_model_vm->next_occuring_event(now);
89 }
90
91 /************
92  * Resource *
93  ************/
94 VMHL13::VMHL13(VMModel *model, const char* name, sg_host_t host_PM)
95  : VirtualMachine(model, name, host_PM)
96 {
97   /* Currently, we assume a VM has no storage. */
98   storage_ = nullptr;
99
100   /* Currently, a VM uses the network resource of its physical host. In
101    * host_lib, this network resource object is referred from two different keys.
102    * When deregistering the reference that points the network resource object
103    * from the VM name, we have to make sure that the system does not call the
104    * free callback for the network resource object. The network resource object
105    * is still used by the physical machine. */
106   sg_host_t host_VM = simgrid::s4u::Host::by_name_or_create(name);
107   host_VM->pimpl_netcard = host_PM->pimpl_netcard;
108
109   vmState_ = SURF_VM_STATE_CREATED;
110
111   // //// CPU  RELATED STUFF ////
112   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
113   CpuCas01 *sub_cpu = dynamic_cast<CpuCas01*>(host_PM->pimpl_cpu);
114
115   cpu_ = surf_cpu_model_vm->createCpu(host_VM, // the machine hosting the VM
116       sub_cpu->getSpeedPeakList(), 1 /*cores*/);
117   if (sub_cpu->getPState() != 0)
118     cpu_->setPState(sub_cpu->getPState());
119
120   /* We create cpu_action corresponding to a VM process on the host operating system. */
121   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
122   action_ = sub_cpu->execution_start(0);
123
124   XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", name, hostPM_->name().c_str(), xbt_dynar_length(storage_));
125 }
126 VMHL13::~VMHL13() {
127   delete cpu_;
128 }
129
130
131 /* Update the physical host of the given VM */
132 void VMHL13::migrate(sg_host_t host_dest)
133 {
134    HostImpl *surfHost_dst = host_dest->extension<HostImpl>();
135    const char *vm_name = getName();
136    const char *pm_name_src = hostPM_->name().c_str();
137    const char *pm_name_dst = surfHost_dst->getName();
138
139    /* update net_elm with that of the destination physical host */
140    sg_host_by_name(vm_name)->pimpl_netcard = sg_host_by_name(pm_name_dst)->pimpl_netcard;
141
142    hostPM_ = host_dest;
143
144    /* Update vcpu's action for the new pm */
145    {
146      /* create a cpu action bound to the pm model at the destination. */
147      CpuAction *new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
148
149      Action::State state = action_->getState();
150      if (state != Action::State::done)
151        XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
152      if (action_->getRemainsNoUpdate() > 0)
153        XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
154
155      /* keep the bound value of the cpu action of the VM. */
156      double old_bound = action_->getBound();
157      if (old_bound != 0) {
158        XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
159        new_cpu_action->setBound(old_bound);
160      }
161
162      XBT_ATTRIB_UNUSED int ret = action_->unref();
163      xbt_assert(ret == 1, "Bug: some resource still remains");
164
165      action_ = new_cpu_action;
166    }
167
168    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
169 }
170
171 void VMHL13::setBound(double bound){
172  action_->setBound(bound);
173 }
174
175 }
176 }