Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
store the VMs by their interface, not by their implementation
[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 /* works for VMs and PMs */
16 static long host_get_ramsize(sg_host_t vm, int *overcommit)
17 {
18   s_vm_params_t params;
19   static_cast<simgrid::s4u::VirtualMachine*>(vm)->parameters(&params);
20
21   if (overcommit)
22     *overcommit = params.overcommit;
23
24   return params.ramsize;
25 }
26
27 /* **** start a VM **** */
28 void SIMIX_vm_start(sg_host_t vm)
29 {
30   sg_host_t pm = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getPm();
31
32   simgrid::vm::VmHostExt::ensureVmExtInstalled();
33   if (pm->extension<simgrid::vm::VmHostExt>() == nullptr)
34     pm->extension_set(new simgrid::vm::VmHostExt());
35
36   long pm_ramsize = pm->extension<simgrid::vm::VmHostExt>()->ramsize;
37   int pm_overcommit = pm->extension<simgrid::vm::VmHostExt>()->overcommit;
38   long vm_ramsize = host_get_ramsize(vm, nullptr);
39
40   if (pm_ramsize && !pm_overcommit) { /* Only verify that we don't overcommit on need */
41     /* Retrieve the memory occupied by the VMs on that host. Yep, we have to traverse all VMs of all hosts for that */
42     long total_ramsize_of_vms = 0;
43     for (simgrid::s4u::VirtualMachine* ws_vm : simgrid::surf::VirtualMachineImpl::allVms_)
44       if (pm == ws_vm->pimpl_vm_->getPm())
45         total_ramsize_of_vms += ws_vm->pimpl_vm_->getRamsize();
46
47     if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
48       XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
49                sg_host_get_name(vm), sg_host_get_name(pm), vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
50       THROWF(vm_error, 0, "The VM %s cannot be started", vm->name().c_str());
51     }
52   }
53
54   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_RUNNING);
55 }
56
57 /**
58  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
59  * VM. All the processes on this VM will pause. The state of the VM is
60  * preserved on memory. We can later resume it again.
61  *
62  * @param vm the vm host to suspend (a sg_host_t)
63  */
64 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
65 {
66   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
67     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
68
69   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
70
71   /* jump to vm_ws_suspend. The state will be set. */
72   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend();
73
74   smx_actor_t smx_process, smx_process_safe;
75   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
76     XBT_DEBUG("suspend %s", smx_process->name.c_str());
77     SIMIX_process_suspend(smx_process, issuer);
78   }
79
80   XBT_DEBUG("suspend all processes on the VM done done");
81 }
82
83 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
84 {
85   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
86
87   SIMIX_vm_suspend(vm, simcall->issuer);
88
89   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
90 }
91
92
93 /**
94  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
95  * VM. All the processes on this VM will run again.
96  *
97  * @param vm the vm host to resume (a sg_host_t)
98  */
99 void SIMIX_vm_resume(sg_host_t vm)
100 {
101   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_SUSPENDED)
102     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
103
104   XBT_DEBUG("resume VM(%s), where %d processes exist",
105       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
106
107   /* jump to vm_ws_resume() */
108   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
109
110   smx_actor_t smx_process, smx_process_safe;
111   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
112     XBT_DEBUG("resume %s", smx_process->name.c_str());
113     SIMIX_process_resume(smx_process);
114   }
115 }
116
117 /**
118  * @brief Function to save a SIMIX VM host.
119  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
120  * We can later restore it again.
121  *
122  * @param vm the vm host to save (a sg_host_t)
123  */
124 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
125 {
126   const char *name = sg_host_get_name(vm);
127
128   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
129     THROWF(vm_error, 0, "VM(%s) is not running", name);
130
131   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
132
133   /* jump to vm_ws_save() */
134   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->save();
135
136   smx_actor_t smx_process, smx_process_safe;
137   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
138     XBT_DEBUG("suspend %s", smx_process->name.c_str());
139     SIMIX_process_suspend(smx_process, issuer);
140   }
141 }
142
143 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
144 {
145   SIMIX_vm_save(vm, simcall->issuer);
146 }
147
148 /**
149  * @brief Function to shutdown a SIMIX VM host. This function powers off the
150  * VM. All the processes on this VM will be killed. But, the state of the VM is
151  * preserved on memory. We can later start it again.
152  *
153  * @param vm the VM to shutdown (a sg_host_t)
154  */
155 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
156 {
157   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
158     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
159
160   XBT_DEBUG("shutdown VM %s, that contains %d processes",
161       vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
162
163   smx_actor_t smx_process, smx_process_safe;
164   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
165     XBT_DEBUG("kill %s", smx_process->name.c_str());
166     SIMIX_process_kill(smx_process, issuer);
167   }
168
169   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
170   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_CREATED);
171 }
172
173 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
174 {
175   SIMIX_vm_shutdown(vm, simcall->issuer);
176 }