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. */
7 #include "cpu_cas01.hpp"
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
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;
17 xbt_dynar_push(all_existing_models, &model);
25 VMHL13Model::VMHL13Model() : VMModel() {}
27 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
29 /* ind means ''indirect'' that this is a reference on the whole dict_elm
30 * structure (i.e not on the surf_resource_private infos) */
32 VirtualMachine *VMHL13Model::createVM(const char *name, surf_resource_t host_PM) {
33 return new VMHL13(this, name, NULL, host_PM);
36 static inline double get_solved_value(CpuAction *cpu_action)
38 return cpu_action->getVariable()->value;
41 /* In the real world, processes on the guest operating system will be somewhat
42 * degraded due to virtualization overhead. The total CPU share that these
43 * processes get is smaller than that of the VM process gets on a host
44 * operating system. */
45 // const double virt_overhead = 0.95;
46 const double virt_overhead = 1;
48 double VMHL13Model::shareResources(double now)
50 /* TODO: update action's cost with the total cost of processes on the VM. */
52 /* 1. Now we know how many resource should be assigned to each virtual
53 * machine. We update constraints of the virtual machine layer.
56 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
57 * X1 + X2 = C (Equation 1)
59 * the resource share of VM1: X1
60 * the resource share of VM2: X2
61 * the capacity of PM1: C
63 * Then, if we have two process (P1 and P2) on VM1.
64 * X1_1 + X1_2 = X1 (Equation 2)
66 * the resource share of P1: X1_1
67 * the resource share of P2: X1_2
68 * the capacity of VM1: X1
70 * Equation 1 was solved in the physical machine layer.
71 * Equation 2 is solved in the virtual machine layer (here).
72 * X1 must be passed to the virtual machine laye as a constraint value.
76 /* iterate for all virtual machines */
77 for (VMModel::vm_list_t::iterator iter =
78 VMModel::ws_vms.begin();
79 iter != VMModel::ws_vms.end(); ++iter) {
81 VirtualMachine *ws_vm = &*iter;
82 Cpu *cpu = ws_vm->p_cpu;
83 xbt_assert(cpu, "cpu-less host");
85 double solved_value = get_solved_value(ws_vm->p_action);
86 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
87 ws_vm->getName(), ws_vm->p_subWs->getName());
89 // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
90 // cpu_cas01->constraint->bound = solved_value;
91 xbt_assert(cpu->getModel() == surf_cpu_model_vm);
92 lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
93 lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
97 /* 2. Calculate resource share at the virtual machine layer. */
98 adjustWeightOfDummyCpuActions();
100 double min_by_cpu = surf_cpu_model_vm->shareResources(now);
101 double min_by_net = surf_network_model->shareResourcesIsIdempotent() ? surf_network_model->shareResources(now) : -1;
102 // Fixme: take storage into account once it's implemented
103 double min_by_sto = -1;
105 XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f, %s min_by_sto %f",
106 this, typeid(surf_cpu_model_pm ).name(), min_by_cpu,
107 typeid(surf_network_model).name(), min_by_net,
108 typeid(surf_storage_model).name(), min_by_sto);
110 double ret = max(max(min_by_cpu, min_by_net), min_by_sto);
111 if (min_by_cpu >= 0.0 && min_by_cpu < ret)
113 if (min_by_net >= 0.0 && min_by_net < ret)
115 if (min_by_sto >= 0.0 && min_by_sto < ret)
121 Action *VMHL13Model::executeParallelTask(int host_nb,
122 sg_host_t *host_list,
123 double *flops_amount,
124 double *bytes_amount,
126 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
128 && (cost_or_zero(bytes_amount, 0) == 0.0))
129 return surf_host_execute(host_list[0], flops_amount[0]);
130 else if ((host_nb == 1)
131 && (cost_or_zero(flops_amount, 0) == 0.0))
132 return surf_network_model_communicate(surf_network_model, host_list[0], host_list[0],bytes_amount[0], rate);
133 else if ((host_nb == 2)
134 && (cost_or_zero(flops_amount, 0) == 0.0)
135 && (cost_or_zero(flops_amount, 1) == 0.0)) {
139 for (i = 0; i < host_nb * host_nb; i++) {
140 if (cost_or_zero(bytes_amount, i) > 0.0) {
142 value = cost_or_zero(bytes_amount, i);
146 return surf_network_model_communicate(surf_network_model, host_list[0], host_list[1], value, rate);
150 THROW_UNIMPLEMENTED; /* This model does not implement parallel tasks for more than 2 hosts. */
158 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
159 surf_resource_t host_PM)
160 : VirtualMachine(model, name, props, NULL, NULL)
162 Host *sub_ws = surf_host_resource_priv(host_PM);
164 /* Currently, we assume a VM has no storage. */
167 /* Currently, a VM uses the network resource of its physical host. In
168 * host_lib, this network resource object is referred from two different keys.
169 * When deregistering the reference that points the network resource object
170 * from the VM name, we have to make sure that the system does not call the
171 * free callback for the network resource object. The network resource object
172 * is still used by the physical machine. */
173 sg_host_t sg_sub_ws = sg_host_by_name_or_create(sub_ws->getName());
174 p_netElm = new RoutingEdgeWrapper(sg_host_edge(sg_sub_ws));
175 sg_host_edge_set(sg_host_by_name_or_create(name), p_netElm);
178 p_currentState = SURF_VM_STATE_CREATED;
180 // //// CPU RELATED STUFF ////
181 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
182 CpuCas01 *sub_cpu = static_cast<CpuCas01*>(sg_host_surfcpu(host_PM));
184 p_cpu = surf_cpu_model_vm->createCpu(name, // name
185 sub_cpu->getPowerPeakList(), // host->power_peak,
186 sub_cpu->getPState(),
187 1, // host->power_scale,
188 NULL, // host->power_trace,
189 1, // host->core_amount,
190 SURF_RESOURCE_ON, // host->initial_state,
191 NULL, // host->state_trace,
192 NULL); // host->properties,
194 /* We create cpu_action corresponding to a VM process on the host operating system. */
195 /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
196 // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(host_PM, GUESTOS_NOISE);
197 p_action = sub_cpu->execute(0);
199 XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->getName(), xbt_dynar_length(p_storage));
201 surf_callback_emit(VMCreatedCallbacks, this);
205 * A physical host does not disappear in the current SimGrid code, but a VM may
206 * disappear during a simulation.
210 /* Free the cpu_action of the VM. */
211 XBT_ATTRIB_UNUSED int ret = p_action->unref();
212 xbt_assert(ret == 1, "Bug: some resource still remains");
215 void VMHL13::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/) {
216 THROW_IMPOSSIBLE; /* This model does not implement parallel tasks */
219 bool VMHL13::isUsed() {
220 THROW_IMPOSSIBLE; /* This model does not implement parallel tasks */
224 e_surf_resource_state_t VMHL13::getState()
226 return (e_surf_resource_state_t) p_currentState;
229 void VMHL13::setState(e_surf_resource_state_t state)
231 p_currentState = (e_surf_vm_state_t) state;
234 void VMHL13::suspend()
237 p_currentState = SURF_VM_STATE_SUSPENDED;
240 void VMHL13::resume()
243 p_currentState = SURF_VM_STATE_RUNNING;
248 p_currentState = SURF_VM_STATE_SAVING;
250 /* FIXME: do something here */
252 p_currentState = SURF_VM_STATE_SAVED;
255 void VMHL13::restore()
257 p_currentState = SURF_VM_STATE_RESTORING;
259 /* FIXME: do something here */
261 p_currentState = SURF_VM_STATE_RUNNING;
265 * Update the physical host of the given VM
267 void VMHL13::migrate(surf_resource_t ind_dst_pm)
269 /* ind_dst_pm equals to smx_host_t */
270 Host *ws_dst = static_cast<Host*>(surf_host_resource_priv(ind_dst_pm));
271 const char *vm_name = getName();
272 const char *pm_name_src = p_subWs->getName();
273 const char *pm_name_dst = ws_dst->getName();
277 /* update net_elm with that of the destination physical host */
278 RoutingEdge *old_net_elm = p_netElm;
279 RoutingEdge *new_net_elm = new RoutingEdgeWrapper(sg_host_edge(sg_host_by_name(pm_name_dst)));
280 xbt_assert(new_net_elm);
282 /* Unregister the current net_elm from host_lib. Do not call the free callback. */
283 sg_host_edge_destroy(sg_host_by_name(vm_name), 0);
285 /* Then, resister the new one. */
286 p_netElm = new_net_elm;
287 sg_host_edge_set(sg_host_by_name(vm_name), p_netElm);
291 /* Update vcpu's action for the new pm */
293 /* create a cpu action bound to the pm model at the destination. */
294 CpuAction *new_cpu_action = static_cast<CpuAction*>(
295 static_cast<Cpu*>(sg_host_surfcpu(ind_dst_pm))->execute(0));
297 e_surf_action_state_t state = p_action->getState();
298 if (state != SURF_ACTION_DONE)
299 XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
300 if (p_action->getRemainsNoUpdate() > 0)
301 XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
303 /* keep the bound value of the cpu action of the VM. */
304 double old_bound = p_action->getBound();
305 if (old_bound != 0) {
306 XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
307 new_cpu_action->setBound(old_bound);
310 XBT_ATTRIB_UNUSED int ret = p_action->unref();
311 xbt_assert(ret == 1, "Bug: some resource still remains");
313 p_action = new_cpu_action;
316 XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
317 XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
321 void VMHL13::setBound(double bound){
322 p_action->setBound(bound);
325 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
326 p_action->setAffinity(cpu, mask);
330 * A surf level object will be useless in the upper layer. Returning the
331 * dict_elm of the host.
333 surf_resource_t VMHL13::getPm()
335 return xbt_lib_get_elm_or_null(host_lib, p_subWs->getName());
338 /* Adding a task to a VM updates the VCPU task on its physical machine. */
339 Action *VMHL13::execute(double size)
341 double old_cost = p_action->getCost();
342 double new_cost = old_cost + size;
344 XBT_DEBUG("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
345 getName(), p_subWs->getName(),
348 p_action->setCost(new_cost);
350 return p_cpu->execute(size);
353 Action *VMHL13::sleep(double duration) {
354 return p_cpu->sleep(duration);