#include "src/plugins/vm/VirtualMachineImpl.hpp"
#include "simgrid/s4u/VirtualMachine.hpp"
+#include "src/simix/ActorImpl.hpp"
+#include "src/simix/smx_host_private.h"
#include <xbt/signal.hpp>
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
-simgrid::surf::VMModel* surf_vm_model = nullptr;
+simgrid::vm::VMModel* surf_vm_model = nullptr;
void surf_vm_model_init_HL13()
{
if (surf_cpu_model_vm) {
- surf_vm_model = new simgrid::surf::VMModel();
+ surf_vm_model = new simgrid::vm::VMModel();
all_existing_models->push_back(surf_vm_model);
}
}
namespace simgrid {
-namespace surf {
+namespace vm {
/*************
* Callbacks *
*************/
-simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmCreation;
-simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmDestruction;
-simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmStateChange;
+simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmCreation;
+simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmDestruction;
+simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmStateChange;
/*********
* Model *
*********/
-std::deque<VirtualMachineImpl*> VirtualMachineImpl::allVms_;
+std::deque<s4u::VirtualMachine*> VirtualMachineImpl::allVms_;
/* In the real world, processes on the guest operating system will be somewhat degraded due to virtualization overhead.
* The total CPU share these processes get is smaller than that of the VM process gets on a host operating system. */
**/
/* iterate for all virtual machines */
- for (VirtualMachineImpl* ws_vm : VirtualMachineImpl::allVms_) {
- Cpu* cpu = ws_vm->piface_->pimpl_cpu;
+ for (s4u::VirtualMachine* ws_vm : VirtualMachineImpl::allVms_) {
+ surf::Cpu* cpu = ws_vm->pimpl_cpu;
xbt_assert(cpu, "cpu-less host");
- double solved_value = ws_vm->action_->getVariable()->value;
- XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->piface_->name().c_str(),
- ws_vm->getPm()->name().c_str());
+ double solved_value = ws_vm->pimpl_vm_->action_->getVariable()->value;
+ XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->cname(), ws_vm->pimpl_vm_->getPm()->cname());
// TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
// cpu_cas01->constraint->bound = solved_value;
* Resource *
************/
-VirtualMachineImpl::VirtualMachineImpl(simgrid::s4u::Host* piface, simgrid::s4u::Host* host_PM)
+VirtualMachineImpl::VirtualMachineImpl(simgrid::s4u::VirtualMachine* piface, simgrid::s4u::Host* host_PM)
: HostImpl(piface, nullptr /*storage*/), hostPM_(host_PM)
{
/* Register this VM to the list of all VMs */
- allVms_.push_back(this);
-
- /* Currently, a VM uses the network resource of its physical host. In
- * host_lib, this network resource object is referred from two different keys.
- * When deregistering the reference that points the network resource object
- * from the VM name, we have to make sure that the system does not call the
- * free callback for the network resource object. The network resource object
- * is still used by the physical machine. */
- piface_->pimpl_netcard = host_PM->pimpl_netcard;
-
- // //// CPU RELATED STUFF ////
- // Roughly, create a vcpu resource by using the values of the sub_cpu one.
- CpuCas01* sub_cpu = dynamic_cast<CpuCas01*>(host_PM->pimpl_cpu);
-
- piface_->pimpl_cpu = surf_cpu_model_vm->createCpu(piface_, sub_cpu->getSpeedPeakList(), 1 /*cores*/);
- if (sub_cpu->getPState() != 0)
- piface_->pimpl_cpu->setPState(sub_cpu->getPState());
+ allVms_.push_back(piface);
/* We create cpu_action corresponding to a VM process on the host operating system. */
/* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
- action_ = sub_cpu->execution_start(0);
+ action_ = host_PM->pimpl_cpu->execution_start(0);
/* Initialize the VM parameters */
params_.ramsize = 0;
- XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", piface->name().c_str(), hostPM_->name().c_str(),
- xbt_dynar_length(storage_));
+ XBT_VERB("Create VM(%s)@PM(%s)", piface->cname(), hostPM_->cname());
}
extern "C" int
VirtualMachineImpl::~VirtualMachineImpl()
{
onVmDestruction(this);
- allVms_.erase(find(allVms_.begin(), allVms_.end(), this));
+ allVms_.erase(find(allVms_.begin(), allVms_.end(), piface_));
/* dirty page tracking */
unsigned int size = xbt_dict_size(dp_objs);
/* Free the cpu_action of the VM. */
XBT_ATTRIB_UNUSED int ret = action_->unref();
xbt_assert(ret == 1, "Bug: some resource still remains");
-
- delete piface_->pimpl_cpu;
}
e_surf_vm_state_t VirtualMachineImpl::getState()
vmState_ = SURF_VM_STATE_RUNNING;
}
-void VirtualMachineImpl::save()
+/**
+ * @brief Function to save a VM.
+ * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved in memory.
+ * We can later restore it again.
+ *
+ * @param vm the vm host to save (a sg_host_t)
+ */
+void VirtualMachineImpl::save(smx_actor_t issuer)
{
+ if (isMigrating)
+ THROWF(vm_error, 0, "Cannot save VM %s: it is migrating.", piface_->cname());
+
+ if (getState() != SURF_VM_STATE_RUNNING)
+ THROWF(vm_error, 0, "Cannot save VM %s: it is not running.", piface_->cname());
+
+ xbt_swag_t process_list = piface_->extension<simgrid::simix::Host>()->process_list;
+
+ XBT_DEBUG("Save VM %s, where %d processes exist", piface_->cname(), xbt_swag_size(process_list));
+
vmState_ = SURF_VM_STATE_SAVING;
action_->suspend();
vmState_ = SURF_VM_STATE_SAVED;
+
+ smx_actor_t smx_process, smx_process_safe;
+ xbt_swag_foreach_safe(smx_process, smx_process_safe, process_list) {
+ XBT_DEBUG("suspend %s", smx_process->cname());
+ SIMIX_process_suspend(smx_process, issuer);
+ }
}
void VirtualMachineImpl::restore()
{
+ if (getState() != SURF_VM_STATE_SAVED)
+ THROWF(vm_error, 0, "Cannot restore VM %s: it was not saved", piface_->cname());
+
+ xbt_swag_t process_list = piface_->extension<simgrid::simix::Host>()->process_list;
+ XBT_DEBUG("Restore VM %s, where %d processes exist", piface_->cname(), xbt_swag_size(process_list));
+
vmState_ = SURF_VM_STATE_RESTORING;
action_->resume();
vmState_ = SURF_VM_STATE_RUNNING;
+
+ smx_actor_t smx_process, smx_process_safe;
+ xbt_swag_foreach_safe(smx_process, smx_process_safe, process_list) {
+ XBT_DEBUG("resume %s", smx_process->cname());
+ SIMIX_process_resume(smx_process);
+ }
}
/** @brief returns the physical machine on which the VM is running **/
/* Update the physical host of the given VM */
void VirtualMachineImpl::migrate(s4u::Host* host_dest)
{
- const char* vm_name = piface_->name().c_str();
- const char* pm_name_src = hostPM_->name().c_str();
- const char* pm_name_dst = host_dest->name().c_str();
+ const char* vm_name = piface_->cname();
+ const char* pm_name_src = hostPM_->cname();
+ const char* pm_name_dst = host_dest->cname();
/* update net_elm with that of the destination physical host */
piface_->pimpl_netcard = host_dest->pimpl_netcard;
/* Update vcpu's action for the new pm */
/* create a cpu action bound to the pm model at the destination. */
- CpuAction* new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
+ surf::CpuAction* new_cpu_action = static_cast<surf::CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
- Action::State state = action_->getState();
- if (state != Action::State::done)
+ surf::Action::State state = action_->getState();
+ if (state != surf::Action::State::done)
XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
if (action_->getRemainsNoUpdate() > 0)
XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
}
+sg_size_t VirtualMachineImpl::getRamsize()
+{
+ return params_.ramsize;
+}
+
void VirtualMachineImpl::setBound(double bound)
{
action_->setBound(bound);