1 /* Copyright (c) 2013-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include <simgrid/host.h>
11 #include "cpu_cas01.hpp"
12 #include "vm_hl13.hpp"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
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);
31 VMHL13Model::VMHL13Model() : VMModel() {}
33 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
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) */
38 VirtualMachine *VMHL13Model::createVM(const char *name, sg_host_t host_PM)
40 VirtualMachine* vm = new VMHL13(this, name, NULL, host_PM);
41 VMCreatedCallbacks(vm);
45 static inline double get_solved_value(CpuAction *cpu_action)
47 return cpu_action->getVariable()->value;
50 /* In the real world, processes on the guest operating system will be somewhat
51 * degraded due to virtualization overhead. The total CPU share that these
52 * processes get is smaller than that of the VM process gets on a host
53 * operating system. */
54 // const double virt_overhead = 0.95;
55 const double virt_overhead = 1;
57 double VMHL13Model::shareResources(double now)
59 /* TODO: update action's cost with the total cost of processes on the VM. */
61 /* 1. Now we know how many resource should be assigned to each virtual
62 * machine. We update constraints of the virtual machine layer.
65 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
66 * X1 + X2 = C (Equation 1)
68 * the resource share of VM1: X1
69 * the resource share of VM2: X2
70 * the capacity of PM1: C
72 * Then, if we have two process (P1 and P2) on VM1.
73 * X1_1 + X1_2 = X1 (Equation 2)
75 * the resource share of P1: X1_1
76 * the resource share of P2: X1_2
77 * the capacity of VM1: X1
79 * Equation 1 was solved in the physical machine layer.
80 * Equation 2 is solved in the virtual machine layer (here).
81 * X1 must be passed to the virtual machine laye as a constraint value.
85 /* iterate for all virtual machines */
86 for (VMModel::vm_list_t::iterator iter =
87 VMModel::ws_vms.begin();
88 iter != VMModel::ws_vms.end(); ++iter) {
90 VirtualMachine *ws_vm = &*iter;
91 Cpu *cpu = ws_vm->p_cpu;
92 xbt_assert(cpu, "cpu-less host");
94 double solved_value = get_solved_value(ws_vm->p_action);
95 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
96 ws_vm->getName(), ws_vm->p_hostPM->getName().c_str());
98 // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
99 // cpu_cas01->constraint->bound = solved_value;
100 xbt_assert(cpu->getModel() == surf_cpu_model_vm);
101 lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
102 lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
106 /* 2. Calculate resource share at the virtual machine layer. */
107 adjustWeightOfDummyCpuActions();
109 double min_by_cpu = surf_cpu_model_vm->shareResources(now);
110 double min_by_net = surf_network_model->shareResourcesIsIdempotent() ? surf_network_model->shareResources(now) : -1;
111 // Fixme: take storage into account once it's implemented
112 double min_by_sto = -1;
114 XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f, %s min_by_sto %f",
115 this, typeid(surf_cpu_model_pm ).name(), min_by_cpu,
116 typeid(surf_network_model).name(), min_by_net,
117 typeid(surf_storage_model).name(), min_by_sto);
119 double ret = std::max(std::max(min_by_cpu, min_by_net), min_by_sto);
120 if (min_by_cpu >= 0.0 && min_by_cpu < ret)
122 if (min_by_net >= 0.0 && min_by_net < ret)
124 if (min_by_sto >= 0.0 && min_by_sto < ret)
130 Action *VMHL13Model::executeParallelTask(int host_nb,
131 sg_host_t *host_list,
132 double *flops_amount,
133 double *bytes_amount,
135 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
137 && (cost_or_zero(bytes_amount, 0) == 0.0))
138 return surf_host_execute(host_list[0], flops_amount[0]);
139 else if ((host_nb == 1)
140 && (cost_or_zero(flops_amount, 0) == 0.0))
141 return surf_network_model_communicate(surf_network_model, host_list[0], host_list[0],bytes_amount[0], rate);
142 else if ((host_nb == 2)
143 && (cost_or_zero(flops_amount, 0) == 0.0)
144 && (cost_or_zero(flops_amount, 1) == 0.0)) {
148 for (i = 0; i < host_nb * host_nb; i++) {
149 if (cost_or_zero(bytes_amount, i) > 0.0) {
151 value = cost_or_zero(bytes_amount, i);
155 return surf_network_model_communicate(surf_network_model, host_list[0], host_list[1], value, rate);
159 THROW_UNIMPLEMENTED; /* This model does not implement parallel tasks for more than 2 hosts. */
167 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props, sg_host_t host_PM)
168 : VirtualMachine(model, name, props, host_PM)
170 /* Currently, we assume a VM has no storage. */
173 /* Currently, a VM uses the network resource of its physical host. In
174 * host_lib, this network resource object is referred from two different keys.
175 * When deregistering the reference that points the network resource object
176 * from the VM name, we have to make sure that the system does not call the
177 * free callback for the network resource object. The network resource object
178 * is still used by the physical machine. */
179 p_netElm = new RoutingEdgeWrapper(sg_host_edge(host_PM));
180 sg_host_t host = sg_host_by_name_or_create(name);
181 sg_host_edge_set(host, p_netElm);
183 p_currentState = SURF_VM_STATE_CREATED;
185 // //// CPU RELATED STUFF ////
186 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
187 CpuCas01 *sub_cpu = static_cast<CpuCas01*>(host_PM->p_cpu);
189 p_cpu = surf_cpu_model_vm->createCpu(host, // the machine hosting the VM
190 sub_cpu->getSpeedPeakList(), // host->power_peak,
191 sub_cpu->getPState(),
192 1, // host->power_scale,
193 NULL, // host->power_trace,
194 1, // host->core_amount,
195 SURF_RESOURCE_ON, // host->initial_state,
196 NULL); // host->state_trace,
198 /* We create cpu_action corresponding to a VM process on the host operating system. */
199 /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
200 // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(host_PM, GUESTOS_NOISE);
201 p_action = sub_cpu->execute(0);
203 XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, p_hostPM->getName().c_str(), xbt_dynar_length(p_storage));
207 * A physical host does not disappear in the current SimGrid code, but a VM may
208 * disappear during a simulation.
212 /* Free the cpu_action of the VM. */
213 XBT_ATTRIB_UNUSED int ret = p_action->unref();
214 xbt_assert(ret == 1, "Bug: some resource still remains");
217 void VMHL13::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/) {
218 THROW_IMPOSSIBLE; /* This model does not implement parallel tasks */
221 bool VMHL13::isUsed() {
222 THROW_IMPOSSIBLE; /* This model does not implement parallel tasks */
226 e_surf_resource_state_t VMHL13::getState()
228 return (e_surf_resource_state_t) p_currentState;
231 void VMHL13::setState(e_surf_resource_state_t state)
233 p_currentState = (e_surf_vm_state_t) state;
236 void VMHL13::suspend()
239 p_currentState = SURF_VM_STATE_SUSPENDED;
242 void VMHL13::resume()
245 p_currentState = SURF_VM_STATE_RUNNING;
250 p_currentState = SURF_VM_STATE_SAVING;
252 /* FIXME: do something here */
254 p_currentState = SURF_VM_STATE_SAVED;
257 void VMHL13::restore()
259 p_currentState = SURF_VM_STATE_RESTORING;
261 /* FIXME: do something here */
263 p_currentState = SURF_VM_STATE_RUNNING;
267 * Update the physical host of the given VM
269 void VMHL13::migrate(sg_host_t host_dest)
271 /* ind_dst_pm equals to smx_host_t */
272 Host *surfHost_dst = static_cast<Host*>(surf_host_resource_priv(host_dest));
273 const char *vm_name = getName();
274 const char *pm_name_src = p_hostPM->getName().c_str();
275 const char *pm_name_dst = surfHost_dst->getName();
277 xbt_assert(surfHost_dst);
279 /* update net_elm with that of the destination physical host */
280 NetCard *old_net_elm = p_netElm;
281 NetCard *new_net_elm = new RoutingEdgeWrapper(sg_host_edge(sg_host_by_name(pm_name_dst)));
282 xbt_assert(new_net_elm);
284 /* Unregister the current net_elm from host_lib. Do not call the free callback. */
285 sg_host_edge_destroy(sg_host_by_name(vm_name), 0);
287 /* Then, resister the new one. */
288 p_netElm = new_net_elm;
289 sg_host_edge_set(sg_host_by_name(vm_name), p_netElm);
291 p_hostPM = host_dest;
293 /* Update vcpu's action for the new pm */
295 /* create a cpu action bound to the pm model at the destination. */
296 CpuAction *new_cpu_action = static_cast<CpuAction*>(host_dest->p_cpu->execute(0));
298 e_surf_action_state_t state = p_action->getState();
299 if (state != SURF_ACTION_DONE)
300 XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
301 if (p_action->getRemainsNoUpdate() > 0)
302 XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
304 /* keep the bound value of the cpu action of the VM. */
305 double old_bound = p_action->getBound();
306 if (old_bound != 0) {
307 XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
308 new_cpu_action->setBound(old_bound);
311 XBT_ATTRIB_UNUSED int ret = p_action->unref();
312 xbt_assert(ret == 1, "Bug: some resource still remains");
314 p_action = new_cpu_action;
317 XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
318 XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
322 void VMHL13::setBound(double bound){
323 p_action->setBound(bound);
326 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
327 p_action->setAffinity(cpu, mask);