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"
17 * The workstation_VM2013 struct includes the workstation_CLM03 struct in
18 * its first member. The workstation_VM2013_t struct inherites all
19 * characteristics of the workstation_CLM03 struct. So, we can treat a
20 * workstation_VM2013 object as a workstation_CLM03 if necessary.
22 typedef struct workstation_VM2013 {
23 s_workstation_CLM03_t ws; /* a VM is a ''v''host */
25 /* The workstation object of the lower layer */
26 workstation_CLM03_t sub_ws; // Pointer to the ''host'' OS
27 e_msg_vm_state_t current_state; // See include/msg/datatypes.h
28 } s_workstation_VM2013_t, *workstation_VM2013_t;
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
31 "Logging specific to the SURF VM workstation module");
36 surf_model_t surf_vm_workstation_model = NULL;
38 static void vm_ws_create(const char *name, void *ind_phys_workstation)
40 workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
42 // //// WORKSTATION RELATED STUFF ////
43 __init_workstation_CLM03(&vm_ws->ws, name, surf_vm_workstation_model);
45 // Override the model with the current VM one.
46 // vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
49 // //// CPU RELATED STUFF ////
50 // This can be done directly during the creation of the surf_vm_worsktation model if
51 // we consider that there will be only one CPU model for one layer of virtualization.
52 // However, if you want to provide several layers (i.e. a VM inside a VM hosted by a PM)
53 // we should add a kind of table that stores the different CPU model.
54 // vm_ws->ws->generic_resource.model.extension.cpu=cpu_model_cas01(VM level ? );
56 // //// NET RELATED STUFF ////
57 // Bind virtual net_elm to the host
58 // TODO rebind each time you migrate a VM
59 // TODO check how network requests are scheduled between distinct processes competing for the same card.
60 vm_ws->ws.net_elm=xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
61 xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
63 // //// STORAGE RELATED STUFF ////
65 // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
66 vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
67 vm_ws->current_state=msg_vm_state_created,
70 /* If you want to get a workstation_VM2013 object from host_lib, see
71 * ws->generic_resouce.model->type first. If it is
72 * SURF_MODEL_TYPE_VM_WORKSTATION, cast ws to vm_ws. */
73 xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, &vm_ws->ws);
77 * Update the physical host of the given VM
79 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
81 /* ind_phys_workstation equals to smx_host_t */
82 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
87 vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
91 * A physical host does not disapper in the current SimGrid code, but a VM may
92 * disapper during a simulation.
94 static void vm_ws_destroy(void *ind_vm_workstation)
96 /* ind_phys_workstation equals to smx_host_t */
97 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
99 xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
101 const char *name = vm_ws->ws.generic_resource.name;
102 /* this will call surf_resource_free() */
103 xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
105 xbt_free(vm_ws->ws.generic_resource.name);
109 static int vm_ws_get_state(void *ind_vm_ws){
110 return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
113 static void vm_ws_set_state(void *ind_vm_ws, int state){
114 ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
118 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
120 // Can be obsolete if you right can ensure that ws_model has been code previously
121 // invoke ws_share_resources on the physical_lyer: sub_ws->ws_share_resources()
123 unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
124 unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
125 xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
126 /* we have to make sure that the share_resource() callback of the model of the lower layer must be called in advance. */
129 // assign the corresponding value to X1, X2, ....
131 // invoke cpu and net share resources on layer (1)
138 * A surf level object will be useless in the upper layer. Returing the name
139 * will be simple and suffcient.
141 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
143 workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
144 return vm_ws->sub_ws->generic_resource.name;
147 static void surf_vm_workstation_model_init_internal(void)
149 surf_vm_workstation_model = surf_model_init();
151 surf_vm_workstation_model->name = "Virtual Workstation";
152 surf_vm_workstation_model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
154 surf_vm_workstation_model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
156 surf_vm_workstation_model->extension.vm_workstation.create = vm_ws_create;
157 surf_vm_workstation_model->extension.vm_workstation.set_state = vm_ws_set_state;
158 surf_vm_workstation_model->extension.vm_workstation.get_state = vm_ws_get_state;
159 surf_vm_workstation_model->extension.vm_workstation.migrate = vm_ws_migrate;
160 surf_vm_workstation_model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
161 surf_vm_workstation_model->extension.vm_workstation.destroy = vm_ws_destroy;
163 surf_vm_workstation_model->model_private->share_resources = vm_ws_share_resources;
166 void surf_vm_workstation_model_init()
168 surf_vm_workstation_model_init_internal();
169 xbt_dynar_push(model_list, &surf_vm_workstation_model);
170 xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);