1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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. */
10 #include "surf_private.h"
11 #include "surf/surf_resource.h"
12 #include "simgrid/sg_config.h"
13 #include "vm_workstation_private.h"
14 #include "cpu_cas01_private.h"
15 #include "maxmin_private.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
18 "Logging specific to the SURF VM workstation module");
21 surf_model_t surf_vm_workstation_model = NULL;
23 /* ind means ''indirect'' that this is a reference on the whole dict_elm
24 * structure (i.e not on the surf_resource_private infos) */
26 static void vm_ws_create(const char *name, void *ind_phys_workstation)
28 workstation_CLM03_t sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
29 const char *sub_ws_name = sub_ws->generic_resource.name;
31 /* The workstation_VM2013 struct inherits the workstation_CLM03 struct. We
32 * create a physical workstation resource, but specifying the size of
33 * s_workstation_VM2013_t and the vm workstation model object. */
34 workstation_CLM03_t ws = (workstation_CLM03_t) surf_resource_new(sizeof(s_workstation_VM2013_t),
35 surf_vm_workstation_model, name, NULL, NULL);
37 /* Currently, we assume a VM has no storage. */
40 /* Currently, a VM uses the network resource of its physical host. In
41 * host_lib, this network resource object is refered from two different keys.
42 * When deregistering the reference that points the network resource object
43 * from the VM name, we have to make sure that the system does not call the
44 * free callback for the network resource object. The network resource object
45 * is still used by the physical machine. */
46 ws->net_elm = xbt_lib_get_or_null(host_lib, sub_ws_name, ROUTING_HOST_LEVEL);
47 xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, ws->net_elm);
49 /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
50 * note workstation_VM2013 objects, inheriting the workstation_CLM03
51 * structure, are also saved there.
53 * If you want to get a workstation_VM2013 object from host_lib, see
54 * ws->generic_resouce.model->type first. If it is
55 * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
56 XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws_name, xbt_dynar_length(ws->storage));
57 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, ws);
60 /* We initialize the VM-specific members. */
61 workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
62 vm_ws->sub_ws = sub_ws;
63 vm_ws->current_state = SURF_VM_STATE_CREATED;
67 // //// CPU RELATED STUFF ////
68 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
69 cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
71 /* We can assume one core and cas01 cpu for the first step.
72 * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
73 cpu_cas01_create_resource(name, // name
74 sub_cpu->power_peak_list, // host->power_peak,
76 1, // host->power_scale,
77 NULL, // host->power_trace,
78 1, // host->core_amount,
79 SURF_RESOURCE_ON, // host->initial_state,
80 NULL, // host->state_trace,
81 NULL, // host->properties,
86 /* We create cpu_action corresponding to a VM process on the host operating system. */
87 /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
88 // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
89 vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0);
93 * - check how network requests are scheduled between distinct processes competing for the same card.
98 * Update the physical host of the given VM
100 static void vm_ws_migrate(void *ind_vm, void *ind_dst_pm)
102 /* ind_phys_workstation equals to smx_host_t */
103 workstation_VM2013_t ws_vm2013 = surf_workstation_resource_priv(ind_vm);
104 workstation_CLM03_t ws_clm03_dst = surf_workstation_resource_priv(ind_dst_pm);
105 const char *vm_name = ws_vm2013->ws.generic_resource.name;
106 const char *pm_name_src = ws_vm2013->sub_ws->generic_resource.name;
107 const char *pm_name_dst = ws_clm03_dst->generic_resource.name;
109 xbt_assert(ws_vm2013);
110 xbt_assert(ws_clm03_dst);
114 /* update net_elm with that of the destination physical host */
115 void *old_net_elm = ws_vm2013->ws.net_elm;
116 void *new_net_elm = xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL);
117 xbt_assert(new_net_elm);
119 /* Unregister the current net_elm from host_lib. Do not call the free callback. */
120 xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
122 /* Then, resister the new one. */
123 ws_vm2013->ws.net_elm = new_net_elm;
124 xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, ws_vm2013->ws.net_elm);
126 ws_vm2013->sub_ws = ws_clm03_dst;
128 /* Update vcpu's action for the new pm */
131 XBT_INFO("cpu_action->remains %g", ws_vm2013->cpu_action->remains);
132 XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
133 ws_vm2013->cpu_action->remains,
134 ws_vm2013->cpu_action->start,
135 ws_vm2013->cpu_action->finish
137 XBT_INFO("cpu_action state %d", surf_action_state_get(ws_vm2013->cpu_action));
140 /* create a cpu action bound to the pm model at the destination. */
141 surf_action_t new_cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_dst_pm, 0);
143 e_surf_action_state_t state = surf_action_state_get(ws_vm2013->cpu_action);
144 if (state != SURF_ACTION_DONE)
145 XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
146 if (ws_vm2013->cpu_action->remains > 0)
147 XBT_CRITICAL("FIXME: need copy the state(?), %f", ws_vm2013->cpu_action->remains);
149 int ret = surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
150 xbt_assert(ret == 1, "Bug: some resource still remains");
152 ws_vm2013->cpu_action = new_cpu_action;
155 XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
156 XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
160 * A physical host does not disapper in the current SimGrid code, but a VM may
161 * disapper during a simulation.
163 static void vm_ws_destroy(void *ind_vm_workstation)
165 /* ind_phys_workstation equals to smx_host_t */
167 /* Before clearing the entries in host_lib, we have to pick up resources. */
168 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
169 cpu_Cas01_t cpu = surf_cpu_resource_priv(ind_vm_workstation);
170 const char *name = vm_ws->ws.generic_resource.name;
173 xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
176 /* We deregister objects from host_lib, without invoking the freeing callback
179 * Do not call xbt_lib_remove() here. It deletes all levels of the key,
180 * including MSG_HOST_LEVEL and others. We should unregister only what we know.
182 xbt_lib_unset(host_lib, name, SURF_CPU_LEVEL, 0);
183 xbt_lib_unset(host_lib, name, ROUTING_HOST_LEVEL, 0);
184 xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL, 0);
186 /* TODO: comment out when VM stroage is implemented. */
187 // xbt_lib_unset(host_lib, name, SURF_STORAGE_LEVEL, 0);
190 /* Free the cpu_action of the VM. */
191 int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
192 xbt_assert(ret == 1, "Bug: some resource still remains");
194 /* Free the cpu resource of the VM. If using power_trace, we will have to
195 * free other objects than lmm_constraint. */
196 surf_model_t cpu_model = cpu->generic_resource.model;
197 lmm_constraint_free(cpu_model->model_private->maxmin_system, cpu->constraint);
200 for (i = 0; i < cpu->core; i++) {
201 void *cnst_id = cpu->constraint_core[i]->id;
202 lmm_constraint_free(cpu_model->model_private->maxmin_system, cpu->constraint_core[i]);
206 xbt_free(cpu->constraint_core);
209 surf_resource_free(cpu);
211 /* Free the network resource of the VM. */
212 // Nothing has to be done, because net_elmts is just a pointer on the physical one
214 /* Free the storage resource of the VM. */
217 /* Free the workstation resource of the VM. */
218 surf_resource_free(vm_ws);
221 static int vm_ws_get_state(void *ind_vm_ws)
223 return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
226 static void vm_ws_set_state(void *ind_vm_ws, int state)
228 ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state = state;
231 static void vm_ws_suspend(void *ind_vm_ws)
233 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
235 surf_action_suspend(vm_ws->cpu_action);
237 vm_ws->current_state = SURF_VM_STATE_SUSPENDED;
240 static void vm_ws_resume(void *ind_vm_ws)
242 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
244 surf_action_resume(vm_ws->cpu_action);
246 vm_ws->current_state = SURF_VM_STATE_RUNNING;
249 static void vm_ws_save(void *ind_vm_ws)
251 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
253 vm_ws->current_state = SURF_VM_STATE_SAVING;
255 /* FIXME: do something here */
256 surf_action_suspend(vm_ws->cpu_action);
258 vm_ws->current_state = SURF_VM_STATE_SAVED;
261 static void vm_ws_restore(void *ind_vm_ws)
263 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
265 vm_ws->current_state = SURF_VM_STATE_RESTORING;
267 /* FIXME: do something here */
268 surf_action_resume(vm_ws->cpu_action);
270 vm_ws->current_state = SURF_VM_STATE_RUNNING;
273 static double get_solved_value(surf_action_t cpu_action)
275 lmm_variable_t var = ((surf_action_lmm_t) cpu_action)->variable;
280 /* In the real world, processes on the guest operating system will be somewhat
281 * degraded due to virtualization overhead. The total CPU share that these
282 * processes get is smaller than that of the VM process gets on a host
283 * operating system. */
284 // const double virt_overhead = 0.95;
285 const double virt_overhead = 1;
287 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
289 /* TODO: udpate action's cost with the total cost of processes on the VM. */
292 /* 0. Make sure that we already calculated the resource share at the physical
295 unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &surf_workstation_model);
296 unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &surf_vm_workstation_model);
297 xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
299 /* Another option is that we call sub_ws->share_resource() here. The
300 * share_resource() function has no side-effect. We can call it here to
305 /* 1. Now we know how many resource should be assigned to each virtual
306 * machine. We update constraints of the virtual machine layer.
309 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
310 * X1 + X2 = C (Equation 1)
312 * the resource share of VM1: X1
313 * the resource share of VM2: X2
314 * the capacity of PM1: C
316 * Then, if we have two process (P1 and P2) on VM1.
317 * X1_1 + X1_2 = X1 (Equation 2)
319 * the resource share of P1: X1_1
320 * the resource share of P2: X1_2
321 * the capacity of VM1: X1
323 * Equation 1 was solved in the physical machine layer.
324 * Equation 2 is solved in the virtual machine layer (here).
325 * X1 must be passed to the virtual machine laye as a constraint value.
329 /* iterate for all hosts including virtual machines */
330 xbt_lib_cursor_t cursor;
333 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
334 workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
335 cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
339 /* skip if it is not a virtual machine */
340 if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
342 xbt_assert(cpu_cas01, "cpu-less workstation");
344 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
345 workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
347 double solved_value = get_solved_value(ws_vm2013->cpu_action);
348 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
349 ws_clm03->generic_resource.name, ws_vm2013->sub_ws->generic_resource.name);
351 // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
352 // cpu_cas01->constraint->bound = solved_value;
353 surf_model_t cpu_model = cpu_cas01->generic_resource.model;
354 xbt_assert(cpu_model == surf_cpu_model_vm);
355 lmm_system_t vcpu_system = cpu_model->model_private->maxmin_system;
356 lmm_update_constraint_bound(vcpu_system, cpu_cas01->constraint, virt_overhead * solved_value);
360 /* 2. Calculate resource share at the virtual machine layer. */
361 double ret = ws_share_resources(workstation_model, now);
364 /* FIXME: 3. do we have to re-initialize our cpu_action object? */
366 /* iterate for all hosts including virtual machines */
367 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
368 workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
370 /* skip if it is not a virtual machine */
373 if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
376 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
379 workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
380 XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
381 ws_vm2013->cpu_action->remains,
382 ws_vm2013->cpu_action->start,
383 ws_vm2013->cpu_action->finish
387 void *ind_sub_host = xbt_lib_get_elm_or_null(host_lib, ws_vm2013->sub_ws->generic_resource.name);
388 surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
389 /* FIXME: this means busy loop? */
390 // ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, GUESTOS_NOISE);
391 ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, 0);
404 * A surf level object will be useless in the upper layer. Returing the
405 * dict_elm of the host.
407 static void *vm_ws_get_pm(void *ind_vm_ws)
409 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
410 const char *sub_ws_name = vm_ws->sub_ws->generic_resource.name;
412 return xbt_lib_get_elm_or_null(host_lib, sub_ws_name);
416 /* Adding a task to a VM updates the VCPU task on its physical machine. */
417 static surf_action_t vm_ws_execute(void *workstation, double size)
419 surf_resource_t ws = ((surf_resource_t) surf_workstation_resource_priv(workstation));
421 xbt_assert(ws->model->type == SURF_MODEL_TYPE_VM_WORKSTATION);
422 workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
424 double old_cost = vm_ws->cpu_action->cost;
425 double new_cost = old_cost + size;
427 XBT_DEBUG("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
428 ws->name, vm_ws->sub_ws->generic_resource.name,
431 vm_ws->cpu_action->cost = new_cost;
433 return ws_execute(workstation, size);
436 static void vm_ws_action_cancel(surf_action_t action)
438 XBT_CRITICAL("FIXME: Not yet implemented. Reduce dummy action's cost by %f", action->cost);
440 ws_action_cancel(action);
444 /* Now we can set bound for each task by using MSG_task_set_bound. But, it does
445 * not work for the dummy CPU action of a VM. Here, we add the set_bound
446 * function for the dummy CPU action. */
447 static void vm_ws_set_vm_bound(void *vm, double bound)
449 surf_resource_t ws = ((surf_resource_t) surf_workstation_resource_priv(vm));
450 xbt_assert(ws->model->type == SURF_MODEL_TYPE_VM_WORKSTATION);
451 workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
453 surf_action_set_bound(vm_ws->cpu_action, bound);
457 /* set the affinity of a VM to the CPU cores of a PM */
458 static void vm_ws_set_vm_affinity(void *vm, void *pm, unsigned long mask)
460 surf_resource_t ws = ((surf_resource_t) surf_workstation_resource_priv(vm));
461 xbt_assert(ws->model->type == SURF_MODEL_TYPE_VM_WORKSTATION);
462 workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
464 surf_cpu_model_pm->set_affinity(vm_ws->cpu_action, pm, mask);
468 static void surf_vm_workstation_model_init_internal(void)
470 surf_model_t model = surf_model_init();
472 model->name = "Virtual Workstation";
473 model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
475 model->action_unref = ws_action_unref;
476 model->action_cancel = vm_ws_action_cancel;
477 // model->action_state_set = ws_action_state_set;
480 model->model_private->share_resources = vm_ws_share_resources;
481 model->model_private->resource_used = ws_resource_used;
482 model->model_private->update_actions_state = ws_update_actions_state;
483 model->model_private->update_resource_state = ws_update_resource_state;
484 model->model_private->finalize = ws_finalize;
487 /* operation for an action, not for VM it self */
488 model->suspend = ws_action_suspend;
489 model->resume = ws_action_resume;
490 // model->is_suspended = ws_action_is_suspended;
491 // model->set_max_duration = ws_action_set_max_duration;
492 model->set_priority = ws_action_set_priority;
493 model->set_bound = ws_action_set_bound;
494 model->set_affinity = ws_action_set_affinity;
495 // #ifdef HAVE_TRACING
496 // model->set_category = ws_action_set_category;
498 model->get_remains = ws_action_get_remains;
499 // #ifdef HAVE_LATENCY_BOUND_TRACKING
500 // model->get_latency_limited = ws_get_latency_limited;
509 xbt_assert(surf_cpu_model_vm);
510 model->extension.workstation.cpu_model = surf_cpu_model_vm;
512 model->extension.workstation.execute = vm_ws_execute;
513 model->extension.workstation.sleep = ws_action_sleep;
514 model->extension.workstation.get_state = ws_get_state;
515 model->extension.workstation.get_speed = ws_get_speed;
516 // model->extension.workstation.get_available_speed = ws_get_available_speed;
518 // model->extension.workstation.communicate = ws_communicate;
519 // model->extension.workstation.get_route = ws_get_route;
520 // model->extension.workstation.execute_parallel_task = ws_execute_parallel_task;
521 // model->extension.workstation.get_link_bandwidth = ws_get_link_bandwidth;
522 // model->extension.workstation.get_link_latency = ws_get_link_latency;
523 // model->extension.workstation.link_shared = ws_link_shared;
524 // model->extension.workstation.get_properties = ws_get_properties;
526 // model->extension.workstation.open = ws_action_open;
527 // model->extension.workstation.close = ws_action_close;
528 // model->extension.workstation.read = ws_action_read;
529 // model->extension.workstation.write = ws_action_write;
530 // model->extension.workstation.stat = ws_action_stat;
531 // model->extension.workstation.unlink = ws_action_unlink;
532 // model->extension.workstation.ls = ws_action_ls;
535 model->extension.vm_workstation.create = vm_ws_create;
536 model->extension.vm_workstation.set_state = vm_ws_set_state;
537 model->extension.vm_workstation.get_state = vm_ws_get_state;
538 model->extension.vm_workstation.migrate = vm_ws_migrate;
539 model->extension.vm_workstation.destroy = vm_ws_destroy;
540 model->extension.vm_workstation.suspend = vm_ws_suspend;
541 model->extension.vm_workstation.resume = vm_ws_resume;
542 model->extension.vm_workstation.save = vm_ws_save;
543 model->extension.vm_workstation.restore = vm_ws_restore;
544 model->extension.vm_workstation.get_pm = vm_ws_get_pm;
545 model->extension.vm_workstation.set_vm_bound = vm_ws_set_vm_bound;
546 model->extension.vm_workstation.set_vm_affinity = vm_ws_set_vm_affinity;
548 model->extension.workstation.set_params = ws_set_params;
549 model->extension.workstation.get_params = ws_get_params;
551 surf_vm_workstation_model = model;
554 void surf_vm_workstation_model_init(void)
556 surf_vm_workstation_model_init_internal();
557 xbt_dynar_push(model_list, &surf_vm_workstation_model);
558 xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);