Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a model object to the arguments of callbacks
[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/surf_resource.h"
11 #include "simgrid/sg_config.h"
12
13 #include "workstation_private.h"
14
15 typedef struct workstation_VM2013 {
16   s_workstation_CLM03_t ws;    /* a VM is a ''v''host */
17
18   workstation_CLM03_t sub_ws;  // Pointer to the ''host'' OS
19   e_msg_vm_state_t current_state;            // See include/msg/datatypes.h
20 } s_workstation_VM2013_t, *workstation_VM2013_t;
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
23                                 "Logging specific to the SURF VM workstation module");
24
25
26
27
28 surf_model_t surf_vm_workstation_model = NULL;
29
30 static void vm_ws_create(const char *name, void *ind_phys_workstation)
31 {
32   workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
33
34   // //// WORKSTATION  RELATED STUFF ////
35   __init_ws(&(vm_ws->ws), name);
36   // Override the model with the current VM one.
37   vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
38
39   // //// CPU  RELATED STUFF ////
40   // This can be done directly during the creation of the surf_vm_worsktation model if
41   // we consider that there will be only one CPU model for one layer of virtualization.
42   // However, if you want to provide several layers (i.e. a VM inside a VM hosted by a PM)
43   // we should add a kind of table that stores the different CPU model.
44   // vm_ws->ws->generic_resource.model.extension.cpu=cpu_model_cas01(VM level ? );
45
46  // //// NET RELATED STUFF ////
47   // Bind virtual net_elm to the host
48   // TODO rebind each time you migrate a VM
49   // TODO check how network requests are scheduled between distinct processes competing for the same card.
50   vm_ws->ws.net_elm=xbt_lib_get_or_null(host_lib,vm_ws->physical_ws->generic_resource.name,ROUTING_HOST_LEVEL);
51   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
52
53   // //// STORAGE RELATED STUFF ////
54
55   // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
56   vm_ws->physical_ws = surf_workstation_resource_priv(ind_phys_workstation);
57   vm_ws->current_state=msg_vm_state_created,
58   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, vm_ws);
59
60 }
61
62 /*
63  * Update the physical host of the given VM
64  */
65 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
66
67    /* ind_phys_workstation equals to smx_host_t */
68    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
69    xbt_assert(vm_ws);
70
71    /* do something */
72
73    vm_ws->physical_workstation = surf_workstation_resource_priv(ind_dest_phys_workstation);
74 }
75
76 /*
77  * A physical host does not disapper in the current SimGrid code, but a VM may
78  * disapper during a simulation.
79  */
80 static void vm_ws_destroy(void *ind_vm_workstation)
81
82         /* ind_phys_workstation equals to smx_host_t */
83         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
84         xbt_assert(vm_ws);
85         xbt_assert(vm_ws->generic_resource.model == surf_vm_workstation_model);
86
87         const char *name = vm_ws->generic_resource.name;
88         /* this will call surf_resource_free() */
89         xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
90
91         xbt_free(vm_ws->generic_resource.name);
92         xbt_free(vm_ws);
93 }
94
95 static int vm_ws_get_state(void *ind_vm_ws){
96         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
97 }
98
99 static void vm_ws_set_state(void *ind_vm_ws, int state){
100          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
101 }
102
103
104 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
105 {
106   // Can be obsolete if you right can ensure that ws_model has been code previously
107   // invoke ws_share_resources on the physical_lyer: sub_ws->ws_share_resources()
108   {
109     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
110     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
111     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
112     /* we have to make sure that the share_resource() callback of the model of the lower layer must be called in advance. */
113   }
114
115         // assign the corresponding value to X1, X2, ....
116
117    // invoke cpu and net share resources on layer (1)
118         // return min;
119   return -1.0;
120 }
121
122
123 /*
124  * A surf level object will be useless in the upper layer. Returing the name
125  * will be simple and suffcient.
126  **/
127 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
128 {
129         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
130         return vm_ws->physical_workstation->name;
131 }
132
133 static void surf_vm_workstation_model_init_internal(void)
134 {
135   surf_vm_workstation_model = surf_model_init();
136
137   surf_vm_workstation_model->name = "Virtual Workstation";
138
139   surf_vm_workstation_model->extension.vm_workstation.create = vm_ws_create;
140   surf_vm_workstation_model->extension.vm_workstation.set_state = vm_ws_set_state;
141   surf_vm_workstation_model->extension.vm_workstation.get_state = vm_ws_get_state;
142   surf_vm_workstation_model->extension.vm_workstation.migrate = vm_ws_migrate;
143   surf_vm_workstation_model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
144   surf_vm_workstation_model->extension.vm_workstation.destroy = vm_ws_destroy;
145
146   surf_vm_workstation_model->model_private->share_resources = ws_share_resources;
147 }
148
149 void surf_vm_workstation_model_init()
150 {
151   surf_vm_workstation_model_init_internal();
152   xbt_dynar_push(model_list, &surf_vm_workstation_model);
153   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
154 }