Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix conflict - Adrien
[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,
15 //                                "Logging specific to SIMIX (vms)");
16
17 /* **** create a VM **** */
18
19 /**
20  * \brief Internal function to create a SIMIX host.
21  * \param name name of the host to create
22  * \param data some user data (may be NULL)
23  */
24 smx_host_t SIMIX_vm_create(const char *name, smx_host_t ind_phys_host)
25 {
26
27   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
28   s_smx_process_t proc;
29
30   // TODO check why we do not have any VM here and why we have the host_proc_hookup  ?
31
32   /* Host structure */
33   smx_host->data = NULL;
34   smx_host->process_list =
35       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
36
37   /* Update global variables */
38   xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
39
40   /* Create surf associated resource */
41   // TODO change phys_host into the right workstation surf model
42   surf_vm_workstation_model->extension.vm_workstation.create(name, ind_phys_host);
43
44   return xbt_lib_get_elm_or_null(host_lib, name);
45 }
46
47
48 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host){
49    return SIMIX_vm_create(name, ind_phys_host);
50 }
51
52
53 /* **** start a VM **** */
54 int __can_be_started(smx_host_t vm){
55         // TODO add checking code related to overcommitment or not.
56         return 1;
57 }
58 void SIMIX_vm_start(smx_host_t ind_vm){
59
60   //TODO only start the VM if you can
61   if (can_be_started(ind_vm))
62           SIMIX_set_vm_state(ind_vm, msg_vm_state_running);
63   else
64           THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
65 }
66
67 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm){
68    SIMIX_vm_start(vm);
69 }
70
71 /* ***** set/get state of a VM ***** */
72 void SIMIX_set_vm_state(smx_host_t ind_vm, int state){
73         surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
74 }
75 void SIMIX_prev_set_vm_state(smx_host_t ind_vm, int state){
76         SIMIX_set_vm_state(ind_vm, state);
77 }
78
79 int SIMIX_get_vm_state(smx_host_t ind_vm){
80  return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
81 }
82 int SIMIX_pre_vm_state(smx_host_t ind_vm){
83         return SIMIX_get_vm_state(ind_vm);
84 }
85
86 /**
87  * \brief Function to suspend a SIMIX VM host. This function powers off the
88  * VM. All the processes on this VM will be killed. But, the state of the VM is
89  * perserved. We can later start it again.
90  *
91  * \param host the vm host to suspend (a smx_host_t)
92  */
93 void SIMIX_vm_suspend(smx_host_t host)
94 {
95   /* TODO: check state */
96
97   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(host)->process_list));
98
99   smx_process_t smx_process, smx_process_safe;
100   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(host)->process_list) {
101          XBT_DEBUG("suspend %s", SIMIX_host_get_name(host));
102          simcall_process_suspend(smx_process);
103   }
104
105   /* TODO: Using the variable of the MSG layer is not clean. */
106   SIMIX_set_vm_state(host, msg_vm_state_suspended);
107 }
108
109 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t vm){
110    SIMIX_vm_suspend(vm);
111 }
112
113 /**
114  * \brief Function to shutdown a SIMIX VM host. This function powers off the
115  * VM. All the processes on this VM will be killed. But, the state of the VM is
116  * perserved. We can later start it again.
117  *
118  * \param host the vm host to shutdown (a smx_host_t)
119  */
120 void SIMIX_vm_shutdown(smx_host_t host)
121 {
122   /* TODO: check state */
123
124   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(host)->process_list));
125
126   smx_process_t smx_process, smx_process_safe;
127   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(host)->process_list) {
128          XBT_DEBUG("kill %s", SIMIX_host_get_name(host));
129          simcall_process_kill(smx_process);
130   }
131
132   /* TODO: Using the variable of the MSG layer is not clean. */
133   SIMIX_set_vm_state(host, msg_vm_state_sleeping);
134 }
135
136 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t vm){
137    SIMIX_vm_shutdown(vm);
138 }
139
140 /**
141  * \brief Function to destroy a SIMIX VM host.
142  *
143  * \param host the vm host to destroy (a smx_host_t)
144  */
145 void SIMIX_vm_destroy(smx_host_t ind_vm)
146 {
147   /* this code basically performs a similar thing like SIMIX_host_destroy() */
148
149   xbt_assert((host != NULL), "Invalid parameters");
150   char *hostname = SIMIX_host_get_name(ind_vm);
151
152   smx_host_priv_t host_priv = SIMIX_host_priv(host);
153
154   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
155   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL);
156
157   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
158   surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
159 }
160
161 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t vm){
162    SIMIX_vm_destroy(vm);
163 }