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"
17 /* FIXME: Where should the VM state be defined?
18 * At now, this must be synchronized with e_msg_vm_state_t */
20 /* created, but not yet started */
21 surf_vm_state_created,
23 surf_vm_state_running,
24 surf_vm_state_migrating,
26 /* Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
27 surf_vm_state_suspended,
29 /* Save/restore involves disk I/O, so there should be transition states. */
32 surf_vm_state_restoring,
40 * The workstation_VM2013 struct includes the workstation_CLM03 struct in
41 * its first member. The workstation_VM2013_t struct inherites all
42 * characteristics of the workstation_CLM03 struct. So, we can treat a
43 * workstation_VM2013 object as a workstation_CLM03 if necessary.
45 typedef struct workstation_VM2013 {
46 s_workstation_CLM03_t ws; /* a VM is a ''v''host */
48 /* The workstation object of the lower layer */
49 workstation_CLM03_t sub_ws; // Pointer to the ''host'' OS
51 e_surf_vm_state_t current_state;
52 } s_workstation_VM2013_t, *workstation_VM2013_t;
54 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
55 "Logging specific to the SURF VM workstation module");
60 surf_model_t surf_vm_workstation_model = NULL;
62 static void vm_ws_create(const char *name, void *ind_phys_workstation)
64 workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
66 // //// WORKSTATION RELATED STUFF ////
67 __init_workstation_CLM03(&vm_ws->ws, name, surf_vm_workstation_model);
69 // Override the model with the current VM one.
70 // vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
73 // //// CPU RELATED STUFF ////
74 // This can be done directly during the creation of the surf_vm_worsktation model if
75 // we consider that there will be only one CPU model for one layer of virtualization.
76 // However, if you want to provide several layers (i.e. a VM inside a VM hosted by a PM)
77 // we should add a kind of table that stores the different CPU model.
78 // vm_ws->ws->generic_resource.model.extension.cpu=cpu_model_cas01(VM level ? );
80 // //// NET RELATED STUFF ////
81 // Bind virtual net_elm to the host
82 // TODO rebind each time you migrate a VM
83 // TODO check how network requests are scheduled between distinct processes competing for the same card.
84 vm_ws->ws.net_elm=xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
85 xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
87 // //// STORAGE RELATED STUFF ////
89 // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
90 vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
91 vm_ws->current_state = surf_vm_state_created;
94 /* If you want to get a workstation_VM2013 object from host_lib, see
95 * ws->generic_resouce.model->type first. If it is
96 * SURF_MODEL_TYPE_VM_WORKSTATION, cast ws to vm_ws. */
97 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, &vm_ws->ws);
101 cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
103 /* We can assume one core and cas01 cpu for the first step.
104 * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource.
106 cpu_cas01_create_resource(name, // name
107 sub_cpu->power_peak, // host->power_peak,
108 1, // host->power_scale,
109 NULL, // host->power_trace,
110 1, // host->core_amount,
111 SURF_RESOURCE_ON, // host->initial_state,
112 NULL, // host->state_trace,
113 NULL, // host->properties,
116 // void *ind_host = xbt_lib_get_elm_or_null(host_lib, name);
120 * Update the physical host of the given VM
122 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
124 /* ind_phys_workstation equals to smx_host_t */
125 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
130 vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
134 * A physical host does not disapper in the current SimGrid code, but a VM may
135 * disapper during a simulation.
137 static void vm_ws_destroy(void *ind_vm_workstation)
139 /* ind_phys_workstation equals to smx_host_t */
140 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
142 xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
144 const char *name = vm_ws->ws.generic_resource.name;
145 /* this will call surf_resource_free() */
146 xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
148 xbt_free(vm_ws->ws.generic_resource.name);
152 static int vm_ws_get_state(void *ind_vm_ws){
153 return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
156 static void vm_ws_set_state(void *ind_vm_ws, int state){
157 ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
161 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
163 // Can be obsolete if you right can ensure that ws_model has been code previously
164 // invoke ws_share_resources on the physical_lyer: sub_ws->ws_share_resources()
166 unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
167 unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
168 xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
169 /* we have to make sure that the share_resource() callback of the model of the lower layer must be called in advance. */
172 // assign the corresponding value to X1, X2, ....
174 // invoke cpu and net share resources on layer (1)
181 * A surf level object will be useless in the upper layer. Returing the name
182 * will be simple and suffcient.
184 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
186 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
187 return vm_ws->sub_ws->generic_resource.name;
190 static void surf_vm_workstation_model_init_internal(void)
192 surf_model_t model = surf_model_init();
194 model->name = "Virtual Workstation";
195 model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
197 model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
199 model->extension.vm_workstation.create = vm_ws_create;
200 model->extension.vm_workstation.set_state = vm_ws_set_state;
201 model->extension.vm_workstation.get_state = vm_ws_get_state;
202 model->extension.vm_workstation.migrate = vm_ws_migrate;
203 model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
204 model->extension.vm_workstation.destroy = vm_ws_destroy;
206 model->model_private->share_resources = vm_ws_share_resources;
207 model->model_private->resource_used = ws_resource_used;
208 model->model_private->update_actions_state = ws_update_actions_state;
209 model->model_private->update_resource_state = ws_update_resource_state;
210 model->model_private->finalize = ws_finalize;
212 surf_vm_workstation_model = model;
215 void surf_vm_workstation_model_init()
217 surf_vm_workstation_model_init_internal();
218 xbt_dynar_push(model_list, &surf_vm_workstation_model);
219 xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);