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 "workstation_private.h"
14 #include "surf/cpu_cas01_private.h"
15 #include "surf/maxmin_private.h"
18 /* FIXME: Where should the VM state be defined?
19 * At now, this must be synchronized with e_msg_vm_state_t */
21 /* created, but not yet started */
27 /* Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
30 /* Save/restore involves disk I/O, so there should be transition states. */
38 * The workstation_VM2013 struct includes the workstation_CLM03 struct in
39 * its first member. The workstation_VM2013_t struct inherites all
40 * characteristics of the workstation_CLM03 struct. So, we can treat a
41 * workstation_VM2013 object as a workstation_CLM03 if necessary.
43 typedef struct workstation_VM2013 {
44 s_workstation_CLM03_t ws; /* a VM is a ''v''host */
46 /* The workstation object of the lower layer */
47 workstation_CLM03_t sub_ws; // Pointer to the ''host'' OS
49 e_surf_vm_state_t current_state;
52 surf_action_t cpu_action;
54 } s_workstation_VM2013_t, *workstation_VM2013_t;
56 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
57 "Logging specific to the SURF VM workstation module");
62 surf_model_t surf_vm_workstation_model = NULL;
64 static void vm_ws_create(const char *name, void *ind_phys_workstation)
66 workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
68 vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
69 vm_ws->current_state = vm_state_created;
72 // //// WORKSTATION RELATED STUFF ////
73 /* Create a workstation_CLM03 resource and register it to the system
74 Please note that the new ws is added into the host_lib. Then,
75 if you want to get a workstation_VM2013 object from host_lib, see
76 ws->generic_resouce.model->type first. If it is SURF_MODEL_TYPE_VM_WORKSTATION,
77 you can cast ws to vm_ws. */
79 __init_workstation_CLM03(&vm_ws->ws, name);
81 // Override the model with the current VM one.
82 vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
85 // //// CPU RELATED STUFF ////
86 // Roughly, create a vcpu resource by using the values of the sub_cpu one.
87 cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
89 /* We can assume one core and cas01 cpu for the first step.
90 * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource.
92 cpu_cas01_create_resource(name, // name
93 sub_cpu->power_peak, // host->power_peak,
94 1, // host->power_scale,
95 NULL, // host->power_trace,
96 1, // host->core_amount,
97 SURF_RESOURCE_ON, // host->initial_state,
98 NULL, // host->state_trace,
99 NULL, // host->properties,
102 vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0); // cost 0 is okay?
104 //// NET RELATED STUFF ////
105 // Bind virtual net_elm to the host
106 // TODO rebind each time you migrate a VM
107 // TODO check how network requests are scheduled between distinct processes competing for the same card.
108 // Please note that the __init_workstation_CLM03 invocation assigned NULL to ws.net_elm since no network elements
109 // were previously created for this hostname. Indeed all network elements are created during the SimGrid initialization phase by considering
110 // the platform file.
111 vm_ws->ws.net_elm = xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
112 xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
114 // //// STORAGE RELATED STUFF ////
115 // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
121 * Update the physical host of the given VM
123 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
125 /* ind_phys_workstation equals to smx_host_t */
126 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
131 vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
135 * A physical host does not disapper in the current SimGrid code, but a VM may
136 * disapper during a simulation.
138 static void vm_ws_destroy(void *ind_vm_workstation)
140 /* ind_phys_workstation equals to smx_host_t */
141 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
143 xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
146 int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
147 xbt_assert(ret == 1, "Bug: some resource still remains");
150 const char *name = vm_ws->ws.generic_resource.name;
151 /* this will call surf_resource_free() */
152 xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
154 xbt_free(vm_ws->ws.generic_resource.name);
158 static int vm_ws_get_state(void *ind_vm_ws){
159 return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
162 static void vm_ws_set_state(void *ind_vm_ws, int state){
163 ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
167 // TODO Please fix it (if found is wrong, nothing is returned)
168 static double get_solved_value(surf_action_t cpu_action)
171 lmm_system_t pm_system = surf_workstation_model->model_private->maxmin_system;
172 lmm_variable_t var = NULL;
174 xbt_swag_foreach(var, &pm_system->variable_set) {
175 XBT_DEBUG("var id %p id_int %d double %f", var->id, var->id_int, var->value);
176 if (var->id == cpu_action) {
185 XBT_CRITICAL("bug: cannot found the solved variable of the action %p", cpu_action);
189 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
191 /* 0. Make sure that we already calculated the resource share at the physical
194 unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
195 unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
196 xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
198 /* Another option is that we call sub_ws->share_resource() here. The
199 * share_resource() function has no side-effect. We can call it here to
204 /* 1. Now we know how many resource should be assigned to each virtual
205 * machine. We update constraints of the virtual machine layer.
208 * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
209 * X1 + X2 = C (Equation 1)
211 * the resource share of VM1: X1
212 * the resource share of VM2: X2
213 * the capacity of PM1: C
215 * Then, if we have two process (P1 and P2) on VM1.
216 * X1_1 + X1_2 = X1 (Equation 2)
218 * the resource share of P1: X1_1
219 * the resource share of P2: X1_2
220 * the capacity of VM1: X1
222 * Equation 1 was solved in the physical machine layer.
223 * Equation 2 is solved in the virtual machine layer (here).
224 * X1 must be passed to the virtual machine laye as a constraint value.
228 /* iterate for all hosts including virtual machines */
229 xbt_lib_cursor_t cursor;
232 xbt_lib_foreach(host_lib, cursor, key, ind_host) {
233 workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
234 cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
236 /* skip if it is not a virtual machine */
239 if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
241 xbt_assert(cpu_cas01, "cpu-less workstation");
243 /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
244 workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
246 double solved_value = get_solved_value(ws_vm2013->cpu_action);
247 XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
248 ws_clm03->generic_resource.name, ws_vm2013->sub_ws->generic_resource.name);
250 cpu_cas01->constraint->bound = solved_value;
254 /* 2. Calculate resource share at the virtual machine layer. */
255 return ws_share_resources(workstation_model, now);
260 * A surf level object will be useless in the upper layer. Returing the name
261 * will be simple and suffcient.
263 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
265 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
266 return vm_ws->sub_ws->generic_resource.name;
269 static void surf_vm_workstation_model_init_internal(void)
271 surf_model_t model = surf_model_init();
273 model->name = "Virtual Workstation";
274 model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
276 model->extension.workstation.cpu_model = surf_cpu_model_vm;
278 model->extension.vm_workstation.create = vm_ws_create;
279 model->extension.vm_workstation.set_state = vm_ws_set_state;
280 model->extension.vm_workstation.get_state = vm_ws_get_state;
281 model->extension.vm_workstation.migrate = vm_ws_migrate;
282 model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
283 model->extension.vm_workstation.destroy = vm_ws_destroy;
285 model->model_private->share_resources = vm_ws_share_resources;
286 model->model_private->resource_used = ws_resource_used;
287 model->model_private->update_actions_state = ws_update_actions_state;
288 model->model_private->update_resource_state = ws_update_resource_state;
289 model->model_private->finalize = ws_finalize;
291 surf_vm_workstation_model = model;
294 void surf_vm_workstation_model_init()
296 surf_vm_workstation_model_init_internal();
297 xbt_dynar_push(model_list, &surf_vm_workstation_model);
298 xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);