Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
inline 4 more VM simcalls
[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 static long host_get_ramsize(sg_host_t vm, int *overcommit)
16 {
17   s_vm_params_t params;
18   static_cast<simgrid::s4u::VirtualMachine*>(vm)->parameters(&params);
19
20   if (overcommit)
21     *overcommit = params.overcommit;
22
23   return params.ramsize;
24 }
25
26 /**
27  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
28  * VM. All the processes on this VM will pause. The state of the VM is
29  * preserved on memory. We can later resume it again.
30  *
31  * @param vm the vm host to suspend (a sg_host_t)
32  */
33 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
34 {
35   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
36     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
37
38   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
39
40   /* jump to vm_ws_suspend. The state will be set. */
41   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend();
42
43   smx_actor_t smx_process, smx_process_safe;
44   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
45     XBT_DEBUG("suspend %s", smx_process->name.c_str());
46     SIMIX_process_suspend(smx_process, issuer);
47   }
48
49   XBT_DEBUG("suspend all processes on the VM done done");
50 }
51
52 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
53 {
54   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
55
56   SIMIX_vm_suspend(vm, simcall->issuer);
57
58   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
59 }
60
61
62 /**
63  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
64  * VM. All the processes on this VM will run again.
65  *
66  * @param vm the vm host to resume (a sg_host_t)
67  */
68 void SIMIX_vm_resume(sg_host_t vm)
69 {
70   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_SUSPENDED)
71     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
72
73   XBT_DEBUG("resume VM(%s), where %d processes exist",
74       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
75
76   /* jump to vm_ws_resume() */
77   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
78
79   smx_actor_t smx_process, smx_process_safe;
80   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
81     XBT_DEBUG("resume %s", smx_process->name.c_str());
82     SIMIX_process_resume(smx_process);
83   }
84 }
85
86 /**
87  * @brief Function to save a SIMIX VM host.
88  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
89  * We can later restore it again.
90  *
91  * @param vm the vm host to save (a sg_host_t)
92  */
93 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
94 {
95   const char *name = sg_host_get_name(vm);
96
97   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
98     THROWF(vm_error, 0, "VM(%s) is not running", name);
99
100   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
101
102   /* jump to vm_ws_save() */
103   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->save();
104
105   smx_actor_t smx_process, smx_process_safe;
106   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
107     XBT_DEBUG("suspend %s", smx_process->name.c_str());
108     SIMIX_process_suspend(smx_process, issuer);
109   }
110 }
111
112 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
113 {
114   SIMIX_vm_save(vm, simcall->issuer);
115 }
116
117 /**
118  * @brief Function to shutdown a SIMIX VM host. This function powers off the
119  * VM. All the processes on this VM will be killed. But, the state of the VM is
120  * preserved on memory. We can later start it again.
121  *
122  * @param vm the VM to shutdown (a sg_host_t)
123  */
124 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
125 {
126   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
127     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
128
129   XBT_DEBUG("shutdown VM %s, that contains %d processes",
130       vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
131
132   smx_actor_t smx_process, smx_process_safe;
133   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
134     XBT_DEBUG("kill %s", smx_process->name.c_str());
135     SIMIX_process_kill(smx_process, issuer);
136   }
137
138   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
139   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_CREATED);
140 }
141
142 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
143 {
144   SIMIX_vm_shutdown(vm, simcall->issuer);
145 }