4 * Created on: Nov 12, 2013
7 #include "vm_workstation.hpp"
8 #include "cpu_cas01.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
12 "Logging specific to the SURF VM workstation module");
15 WorkstationVMModelPtr surf_vm_workstation_model = NULL;
17 void surf_vm_workstation_model_init_current_default(void){
18 if (surf_cpu_model_vm) {
19 surf_vm_workstation_model = new WorkstationVMModel();
20 ModelPtr model = static_cast<ModelPtr>(surf_vm_workstation_model);
22 xbt_dynar_push(model_list, &model);
23 xbt_dynar_push(model_list_invoke, &model);
31 WorkstationVMModel::WorkstationVMModel() : WorkstationModel("Virtual Workstation") {
32 p_cpuModel = surf_cpu_model_vm;
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 void WorkstationVMModel::createResource(const char *name, void *ind_phys_workstation)
40 WorkstationVM2013LmmPtr ws = new WorkstationVM2013Lmm(this, name, NULL, static_cast<surf_resource_t>(ind_phys_workstation));
42 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, static_cast<ResourcePtr>(ws));
45 * - check how network requests are scheduled between distinct processes competing for the same card.
49 static inline double get_solved_value(CpuActionLmmPtr cpu_action)
51 return cpu_action->p_variable->value;
54 /* In the real world, processes on the guest operating system will be somewhat
55 * degraded due to virtualization overhead. The total CPU share that these
56 * processes get is smaller than that of the VM process gets on a host
57 * operating system. */
58 // const double virt_overhead = 0.95;
59 const double virt_overhead = 1;
61 double WorkstationVMModel::shareResources(double now)
63 /* TODO: udpate action's cost with the total cost of processes on the VM. */
66 /* 0. Make sure that we already calculated the resource share at the physical
69 ModelPtr ws_model = static_cast<ModelPtr>(surf_workstation_model);
70 ModelPtr vm_ws_model = static_cast<ModelPtr>(surf_vm_workstation_model);
71 unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &ws_model);
72 unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &vm_ws_model);
73 xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
75 /* Another option is that we call sub_ws->share_resource() here. The
76 * share_resource() function has no side-effect. We can call it here to
81 /* 1. Now we know how many resource should be assigned to each virtual
82 * machine. We update constraints of the virtual machine layer.
85 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
86 * X1 + X2 = C (Equation 1)
88 * the resource share of VM1: X1
89 * the resource share of VM2: X2
90 * the capacity of PM1: C
92 * Then, if we have two process (P1 and P2) on VM1.
93 * X1_1 + X1_2 = X1 (Equation 2)
95 * the resource share of P1: X1_1
96 * the resource share of P2: X1_2
97 * the capacity of VM1: X1
99 * Equation 1 was solved in the physical machine layer.
100 * Equation 2 is solved in the virtual machine layer (here).
101 * X1 must be passed to the virtual machine laye as a constraint value.
105 /* iterate for all hosts including virtual machines */
106 xbt_lib_cursor_t cursor;
109 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
110 WorkstationCLM03Ptr ws_clm03 = dynamic_cast<WorkstationCLM03Ptr>(
111 static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
112 CpuCas01LmmPtr cpu_cas01 = dynamic_cast<CpuCas01LmmPtr>(
113 static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
117 /* skip if it is not a virtual machine */
118 if (ws_clm03->p_model != static_cast<ModelPtr>(surf_vm_workstation_model))
120 xbt_assert(cpu_cas01, "cpu-less workstation");
122 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
123 WorkstationVM2013Ptr ws_vm2013 = dynamic_cast<WorkstationVM2013Ptr>(ws_clm03);
125 double solved_value = get_solved_value(ws_vm2013->p_action);
126 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
127 ws_clm03->m_name, ws_vm2013->p_subWs->m_name);
129 // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
130 // cpu_cas01->constraint->bound = solved_value;
131 xbt_assert(cpu_cas01->p_model == static_cast<ModelPtr>(surf_cpu_model_vm));
132 lmm_system_t vcpu_system = cpu_cas01->p_model->p_maxminSystem;
133 lmm_update_constraint_bound(vcpu_system, cpu_cas01->p_constraint, virt_overhead * solved_value);
137 /* 2. Calculate resource share at the virtual machine layer. */
138 double ret = WorkstationModel::shareResources(now);
141 /* FIXME: 3. do we have to re-initialize our cpu_action object? */
143 /* iterate for all hosts including virtual machines */
144 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
145 WorkstationCLM03Ptr ws_clm03 = ind_host[SURF_WKS_LEVEL];
147 /* skip if it is not a virtual machine */
150 if (ws_clm03->p_model != surf_vm_workstation_model)
153 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
156 WorkstationVM2013Ptr ws_vm2013 = (workstation_VM2013_t) ws_clm03;
157 XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
158 ws_vm2013->cpu_action->remains,
159 ws_vm2013->cpu_action->start,
160 ws_vm2013->cpu_action->finish
164 void *ind_sub_host = xbt_lib_get_elm_or_null(host_lib, ws_vm2013->sub_ws->generic_resource.name);
165 surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
166 /* FIXME: this means busy loop? */
167 // ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, GUESTOS_NOISE);
168 ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, 0);
182 WorkstationVM2013Lmm::WorkstationVM2013Lmm(WorkstationVMModelPtr model, const char* name, xbt_dict_t props,
183 surf_resource_t ind_phys_workstation)
184 : Resource(model, name, props),
185 WorkstationCLM03(model, name, props, NULL, NULL, NULL),
186 WorkstationVM2013(model, name, props, NULL, NULL),
187 WorkstationCLM03Lmm(model, name, props, NULL, NULL, NULL) {
188 WorkstationCLM03Ptr sub_ws = dynamic_cast<WorkstationCLM03Ptr>(
189 static_cast<ResourcePtr>(
190 surf_workstation_resource_priv(ind_phys_workstation)));
192 /* Currently, we assume a VM has no storage. */
195 /* Currently, a VM uses the network resource of its physical host. In
196 * host_lib, this network resource object is refered from two different keys.
197 * When deregistering the reference that points the network resource object
198 * from the VM name, we have to make sure that the system does not call the
199 * free callback for the network resource object. The network resource object
200 * is still used by the physical machine. */
201 p_netElm = static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, sub_ws->m_name, ROUTING_HOST_LEVEL));
202 xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, p_netElm);
205 p_currentState = SURF_VM_STATE_CREATED;
207 // //// CPU RELATED STUFF ////
208 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
209 CpuCas01LmmPtr sub_cpu = dynamic_cast<CpuCas01LmmPtr>(
210 static_cast<ResourcePtr>(
211 surf_cpu_resource_priv(ind_phys_workstation)));
213 /* We can assume one core and cas01 cpu for the first step.
214 * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
216 p_cpu = static_cast<CpuCas01ModelPtr>(surf_cpu_model_vm)->createResource(name, // name
217 sub_cpu->p_powerPeakList, // host->power_peak,
219 1, // host->power_scale,
220 NULL, // host->power_trace,
221 1, // host->core_amount,
222 SURF_RESOURCE_ON, // host->initial_state,
223 NULL, // host->state_trace,
224 NULL); // host->properties,
226 /* We create cpu_action corresponding to a VM process on the host operating system. */
227 /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
228 // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
229 p_action = dynamic_cast<CpuActionLmmPtr>(sub_cpu->execute(0));
231 /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
232 * note workstation_VM2013 objects, inheriting the workstation_CLM03
233 * structure, are also saved there.
235 * If you want to get a workstation_VM2013 object from host_lib, see
236 * ws->generic_resouce.model->type first. If it is
237 * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
238 XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->m_name, xbt_dynar_length(p_storage));
242 * A physical host does not disapper in the current SimGrid code, but a VM may
243 * disapper during a simulation.
245 WorkstationVM2013Lmm::~WorkstationVM2013Lmm()
247 /* ind_phys_workstation equals to smx_host_t */
248 surf_resource_t ind_vm_workstation = xbt_lib_get_elm_or_null(host_lib, m_name);
250 /* Before clearing the entries in host_lib, we have to pick up resources. */
251 CpuCas01LmmPtr cpu = dynamic_cast<CpuCas01LmmPtr>(
252 static_cast<ResourcePtr>(
253 surf_cpu_resource_priv(ind_vm_workstation)));
255 /* We deregister objects from host_lib, without invoking the freeing callback
258 * Do not call xbt_lib_remove() here. It deletes all levels of the key,
259 * including MSG_HOST_LEVEL and others. We should unregister only what we know.
261 xbt_lib_unset(host_lib, m_name, SURF_CPU_LEVEL, 0);
262 xbt_lib_unset(host_lib, m_name, ROUTING_HOST_LEVEL, 0);
263 xbt_lib_unset(host_lib, m_name, SURF_WKS_LEVEL, 0);
265 /* TODO: comment out when VM stroage is implemented. */
266 // xbt_lib_unset(host_lib, name, SURF_STORAGE_LEVEL, 0);
269 /* Free the cpu_action of the VM. */
270 int ret = p_action->unref();
271 xbt_assert(ret == 1, "Bug: some resource still remains");
273 /* Free the cpu resource of the VM. If using power_trace, we will have to */
276 /* Free the network resource of the VM. */
277 // Nothing has to be done, because net_elmts is just a pointer on the physical one
279 /* Free the storage resource of the VM. */
282 /* Free the workstation resource of the VM. */
285 e_surf_resource_state_t WorkstationVM2013Lmm::getState()
287 return (e_surf_resource_state_t) p_currentState;
290 void WorkstationVM2013Lmm::setState(e_surf_resource_state_t state)
292 p_currentState = (e_surf_vm_state_t) state;
295 void WorkstationVM2013Lmm::suspend()
298 p_currentState = SURF_VM_STATE_SUSPENDED;
301 void WorkstationVM2013Lmm::resume()
304 p_currentState = SURF_VM_STATE_RUNNING;
307 void WorkstationVM2013Lmm::save()
309 p_currentState = SURF_VM_STATE_SAVING;
311 /* FIXME: do something here */
313 p_currentState = SURF_VM_STATE_SAVED;
316 void WorkstationVM2013Lmm::restore()
318 p_currentState = SURF_VM_STATE_RESTORING;
320 /* FIXME: do something here */
322 p_currentState = SURF_VM_STATE_RUNNING;
326 * Update the physical host of the given VM
328 void WorkstationVM2013Lmm::migrate(surf_resource_t ind_dst_pm)
330 /* ind_phys_workstation equals to smx_host_t */
331 WorkstationCLM03Ptr ws_clm03_dst = dynamic_cast<WorkstationCLM03Ptr>(
332 static_cast<ResourcePtr>(
333 surf_workstation_resource_priv(ind_dst_pm)));
334 const char *vm_name = m_name;
335 const char *pm_name_src = p_subWs->m_name;
336 const char *pm_name_dst = ws_clm03_dst->m_name;
338 xbt_assert(ws_clm03_dst);
342 /* update net_elm with that of the destination physical host */
343 RoutingEdgePtr old_net_elm = p_netElm;
344 RoutingEdgePtr new_net_elm = static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL));
345 xbt_assert(new_net_elm);
347 /* Unregister the current net_elm from host_lib. Do not call the free callback. */
348 xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
350 /* Then, resister the new one. */
351 p_netElm = new_net_elm;
352 xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, p_netElm);
354 p_subWs = ws_clm03_dst;
356 /* Update vcpu's action for the new pm */
359 XBT_INFO("cpu_action->remains %g", p_action->remains);
360 XBT_INFO("cost %f remains %f start %f finish %f", p_action->cost,
365 XBT_INFO("cpu_action state %d", surf_action_get_state(p_action));
368 /* create a cpu action bound to the pm model at the destination. */
369 CpuActionLmmPtr new_cpu_action = dynamic_cast<CpuActionLmmPtr>(
370 dynamic_cast<CpuCas01LmmPtr>(
371 static_cast<ResourcePtr>(
372 surf_cpu_resource_priv(ind_dst_pm)))->execute(0));
374 e_surf_action_state_t state = surf_action_get_state(p_action);
375 if (state != SURF_ACTION_DONE)
376 XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
377 if (p_action->m_remains > 0)
378 XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->m_remains);
380 int ret = p_action->unref();
381 xbt_assert(ret == 1, "Bug: some resource still remains");
383 p_action = new_cpu_action;
386 XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
387 XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
390 void WorkstationVM2013Lmm::setBound(double bound){
391 p_action->setBound(bound);
394 void WorkstationVM2013Lmm::setAffinity(CpuLmmPtr cpu, unsigned long mask){
395 p_action->setAffinity(cpu, mask);
399 * A surf level object will be useless in the upper layer. Returing the
400 * dict_elm of the host.
402 surf_resource_t WorkstationVM2013Lmm::getPm()
404 return xbt_lib_get_elm_or_null(host_lib, p_subWs->m_name);
407 /* Adding a task to a VM updates the VCPU task on its physical machine. */
408 ActionPtr WorkstationVM2013Lmm::execute(double size)
410 double old_cost = p_action->m_cost;
411 double new_cost = old_cost + size;
413 XBT_DEBUG("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
414 m_name, p_subWs->m_name,
417 p_action->m_cost = new_cost;
419 return WorkstationCLM03::execute(size);
426 //FIME:: handle action cancel