Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the cpu model object in the workstation model object
[simgrid.git] / src / surf / vm_workstation.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11 #include "surf/surf_resource.h"
12 #include "simgrid/sg_config.h"
13 #include "workstation_private.h"
14
15
16 /* NOTE:
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.
21  **/
22 typedef struct workstation_VM2013 {
23   s_workstation_CLM03_t ws;    /* a VM is a ''v''host */
24
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;
29
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
31                                 "Logging specific to the SURF VM workstation module");
32
33
34
35
36 surf_model_t surf_vm_workstation_model = NULL;
37
38 static void vm_ws_create(const char *name, void *ind_phys_workstation)
39 {
40   workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
41
42   // //// WORKSTATION  RELATED STUFF ////
43   __init_workstation_CLM03(&vm_ws->ws, name, surf_vm_workstation_model);
44
45   // Override the model with the current VM one.
46   // vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
47
48
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 ? );
55
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);
62
63   // //// STORAGE RELATED STUFF ////
64
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,
68
69
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);
74 }
75
76 /*
77  * Update the physical host of the given VM
78  */
79 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
80
81    /* ind_phys_workstation equals to smx_host_t */
82    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
83    xbt_assert(vm_ws);
84
85    /* do something */
86
87    vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
88 }
89
90 /*
91  * A physical host does not disapper in the current SimGrid code, but a VM may
92  * disapper during a simulation.
93  */
94 static void vm_ws_destroy(void *ind_vm_workstation)
95
96         /* ind_phys_workstation equals to smx_host_t */
97         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
98         xbt_assert(vm_ws);
99         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
100
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);
104
105         xbt_free(vm_ws->ws.generic_resource.name);
106         xbt_free(vm_ws);
107 }
108
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;
111 }
112
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;
115 }
116
117
118 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
119 {
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()
122   {
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. */
127   }
128
129         // assign the corresponding value to X1, X2, ....
130
131    // invoke cpu and net share resources on layer (1)
132         // return min;
133   return -1.0;
134 }
135
136
137 /*
138  * A surf level object will be useless in the upper layer. Returing the name
139  * will be simple and suffcient.
140  **/
141 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
142 {
143         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
144         return vm_ws->sub_ws->generic_resource.name;
145 }
146
147 static void surf_vm_workstation_model_init_internal(void)
148 {
149   surf_vm_workstation_model = surf_model_init();
150
151   surf_vm_workstation_model->name = "Virtual Workstation";
152   surf_vm_workstation_model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
153
154   surf_vm_workstation_model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
155
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;
162
163   surf_vm_workstation_model->model_private->share_resources = vm_ws_share_resources;
164 }
165
166 void surf_vm_workstation_model_init()
167 {
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);
171 }