Logo AND Algorithmique Numérique Distribuée

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