Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make the prefix of the functions consistent, e.g., vm_{get/set}_state
[simgrid.git] / src / simix / smx_vm.c
1 /* Copyright (c) 2007-2012. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 //If you need to log some stuffs, just uncomment these two lines and uses XBT_DEBUG for instance
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
15
16 /* **** create a VM **** */
17
18 /**
19  * \brief Internal function to create a SIMIX host.
20  * \param name name of the host to create
21  * \param data some user data (may be NULL)
22  */
23 smx_host_t SIMIX_vm_create(const char *name, smx_host_t ind_phys_host)
24 {
25
26   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
27   s_smx_process_t proc;
28
29   // TODO check why we do not have any VM here and why we have the host_proc_hookup  ?
30
31   /* Host structure */
32   smx_host->data = NULL;
33   smx_host->process_list =
34       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
35
36   /* Update global variables */
37   xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
38
39   /* Create surf associated resource */
40   // TODO change phys_host into the right workstation surf model
41   surf_vm_workstation_model->extension.vm_workstation.create(name, ind_phys_host);
42
43   return xbt_lib_get_elm_or_null(host_lib, name);
44 }
45
46
47 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host){
48    return SIMIX_vm_create(name, ind_phys_host);
49 }
50
51
52 /* **** start a VM **** */
53 static int __can_be_started(smx_host_t vm){
54         // TODO add checking code related to overcommitment or not.
55         return 1;
56 }
57 void SIMIX_vm_start(smx_host_t ind_vm){
58
59   //TODO only start the VM if you can
60   if (__can_be_started(ind_vm))
61           SIMIX_vm_set_state(ind_vm, msg_vm_state_running);
62   else
63           THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
64 }
65
66 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm){
67    SIMIX_vm_start(ind_vm);
68 }
69
70 /* ***** set/get state of a VM ***** */
71 void SIMIX_vm_set_state(smx_host_t ind_vm, int state){
72         surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
73 }
74 void SIMIX_pre_vm_set_state(smx_host_t ind_vm, int state){
75         SIMIX_vm_set_state(ind_vm, state);
76 }
77
78 int SIMIX_vm_get_state(smx_host_t ind_vm){
79  return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
80 }
81 int SIMIX_pre_vm_get_state(smx_host_t ind_vm){
82         return SIMIX_vm_get_state(ind_vm);
83 }
84
85 /**
86  * \brief Function to migrate a SIMIX VM host. This function stops the exection of the
87  * VM. All the processes on this VM will pause. The state of the VM is
88  * perserved. We can later resume it again.
89  *
90  * \param host the vm host to migrate (a smx_host_t)
91  */
92 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
93 {
94   /* TODO: check state */
95
96   /* TODO: Using the variable of the MSG layer is not clean. */
97   SIMIX_vm_set_state(ind_vm, msg_vm_state_migrating);
98
99   /* jump to vm_ws_migrate(). this will update the vm location. */
100   surf_vm_workstation_model->extension.vm_workstation.migrate(ind_vm, ind_dst_pm);
101
102   SIMIX_vm_set_state(ind_vm, msg_vm_state_running);
103 }
104
105 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm){
106    SIMIX_vm_migrate(ind_vm, ind_dst_pm);
107 }
108
109 /**
110  * \brief Function to get the physical host of the given the SIMIX VM host.
111  *
112  * \param host the vm host to get_phys_host (a smx_host_t)
113  */
114 const char *SIMIX_vm_get_phys_host(smx_host_t ind_vm)
115 {
116   /* jump to vm_ws_get_phys_host(). this will update the vm location. */
117   return surf_vm_workstation_model->extension.vm_workstation.get_phys_host(ind_vm);
118 }
119
120 const char *SIMIX_pre_vm_get_phys_host(smx_simcall_t simcall, smx_host_t ind_vm){
121   return SIMIX_vm_get_phys_host(ind_vm);
122 }
123
124 /**
125  * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
126  * VM. All the processes on this VM will pause. The state of the VM is
127  * perserved. We can later resume it again.
128  *
129  * \param host the vm host to suspend (a smx_host_t)
130  */
131 void SIMIX_vm_suspend(smx_host_t ind_vm)
132 {
133   /* TODO: check state */
134
135   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
136
137   smx_process_t smx_process, smx_process_safe;
138   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
139          XBT_DEBUG("suspend %s", SIMIX_host_get_name(ind_vm));
140          /* FIXME: calling a simcall from the SIMIX layer is strange. */
141          simcall_process_suspend(smx_process);
142   }
143
144   /* TODO: Using the variable of the MSG layer is not clean. */
145   SIMIX_vm_set_state(ind_vm, msg_vm_state_suspended);
146 }
147
148 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm){
149    SIMIX_vm_suspend(ind_vm);
150 }
151
152 /**
153  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
154  * VM. All the processes on this VM will run again. 
155  *
156  * \param host the vm host to resume (a smx_host_t)
157  */
158 void SIMIX_vm_resume(smx_host_t ind_vm)
159 {
160   /* TODO: check state */
161
162   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
163
164   smx_process_t smx_process, smx_process_safe;
165   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
166          XBT_DEBUG("resume %s", SIMIX_host_get_name(ind_vm));
167          /* FIXME: calling a simcall from the SIMIX layer is strange. */
168          simcall_process_resume(smx_process);
169   }
170
171   /* TODO: Using the variable of the MSG layer is not clean. */
172   SIMIX_vm_set_state(ind_vm, msg_vm_state_running);
173 }
174
175 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm){
176    SIMIX_vm_resume(ind_vm);
177 }
178
179 /**
180  * \brief Function to shutdown a SIMIX VM host. This function powers off the
181  * VM. All the processes on this VM will be killed. But, the state of the VM is
182  * perserved. We can later start it again.
183  *
184  * \param host the vm host to shutdown (a smx_host_t)
185  */
186 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
187 {
188   /* TODO: check state */
189
190   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
191
192   smx_process_t smx_process, smx_process_safe;
193   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
194          XBT_DEBUG("kill %s", SIMIX_host_get_name(ind_vm));
195
196          SIMIX_process_kill(smx_process, issuer);
197   }
198
199   /* TODO: Using the variable of the MSG layer is not clean. */
200   SIMIX_vm_set_state(ind_vm, msg_vm_state_sleeping);
201 }
202
203 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm){
204    SIMIX_vm_shutdown(ind_vm, simcall->issuer);
205 }
206
207 /**
208  * \brief Function to destroy a SIMIX VM host.
209  *
210  * \param host the vm host to destroy (a smx_host_t)
211  */
212 void SIMIX_vm_destroy(smx_host_t ind_vm)
213 {
214   /* this code basically performs a similar thing like SIMIX_host_destroy() */
215
216   xbt_assert((ind_vm != NULL), "Invalid parameters");
217   const char *hostname = SIMIX_host_get_name(ind_vm);
218
219   smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
220
221   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
222   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL);
223
224   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
225   surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
226 }
227
228 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm){
229    SIMIX_vm_destroy(ind_vm);
230 }