Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Completely rework the properties of netzones
[simgrid.git] / src / simix / smx_vm.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mc/mc.h"
7 #include "simgrid/s4u/VirtualMachine.hpp"
8 #include "smx_private.h"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include "src/plugins/vm/VmHostExt.hpp"
11 #include "src/surf/HostImpl.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX Virtual Machines");
14
15 /**
16  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
17  * VM. All the processes on this VM will pause. The state of the VM is
18  * preserved on memory. We can later resume it again.
19  *
20  * @param vm the vm host to suspend (a sg_host_t)
21  */
22 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
23 {
24   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
25     THROWF(vm_error, 0, "VM(%s) is not running", vm->cname());
26
27   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->cname(), xbt_swag_size(sg_host_simix(vm)->process_list));
28
29   /* jump to vm_ws_suspend. The state will be set. */
30   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend();
31
32   smx_actor_t smx_process, smx_process_safe;
33   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
34     XBT_DEBUG("suspend %s", smx_process->name.c_str());
35     SIMIX_process_suspend(smx_process, issuer);
36   }
37
38   XBT_DEBUG("suspend all processes on the VM done done");
39 }
40
41 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
42 {
43   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
44
45   SIMIX_vm_suspend(vm, simcall->issuer);
46
47   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
48 }
49
50
51 /**
52  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
53  * VM. All the processes on this VM will run again.
54  *
55  * @param vm the vm host to resume (a sg_host_t)
56  */
57 void SIMIX_vm_resume(sg_host_t vm)
58 {
59   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_SUSPENDED)
60     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->cname());
61
62   XBT_DEBUG("resume VM(%s), where %d processes exist", vm->cname(), xbt_swag_size(sg_host_simix(vm)->process_list));
63
64   /* jump to vm_ws_resume() */
65   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
66
67   smx_actor_t smx_process, smx_process_safe;
68   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
69     XBT_DEBUG("resume %s", smx_process->name.c_str());
70     SIMIX_process_resume(smx_process);
71   }
72 }
73
74 /**
75  * @brief Function to save a SIMIX VM host.
76  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
77  * We can later restore it again.
78  *
79  * @param vm the vm host to save (a sg_host_t)
80  */
81 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
82 {
83   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
84     THROWF(vm_error, 0, "VM(%s) is not running", vm->cname());
85
86   XBT_DEBUG("save VM(%s), where %d processes exist", vm->cname(), xbt_swag_size(sg_host_simix(vm)->process_list));
87
88   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->save();
89
90   smx_actor_t smx_process, smx_process_safe;
91   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
92     XBT_DEBUG("suspend %s", smx_process->cname());
93     SIMIX_process_suspend(smx_process, issuer);
94   }
95 }
96
97 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
98 {
99   SIMIX_vm_save(vm, simcall->issuer);
100 }
101
102 /**
103  * @brief Function to shutdown a SIMIX VM host. This function powers off the
104  * VM. All the processes on this VM will be killed. But, the state of the VM is
105  * preserved on memory. We can later start it again.
106  *
107  * @param vm the VM to shutdown (a sg_host_t)
108  */
109 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
110 {
111   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
112     THROWF(vm_error, 0, "VM(%s) is not running", vm->cname());
113
114   XBT_DEBUG("shutdown VM %s, that contains %d processes", vm->cname(), xbt_swag_size(sg_host_simix(vm)->process_list));
115
116   smx_actor_t smx_process, smx_process_safe;
117   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
118     XBT_DEBUG("kill %s", smx_process->name.c_str());
119     SIMIX_process_kill(smx_process, issuer);
120   }
121
122   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
123   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_CREATED);
124 }
125
126 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
127 {
128   SIMIX_vm_shutdown(vm, simcall->issuer);
129 }