-/* Copyright (c) 2015-2022. The SimGrid Team. All rights reserved. */
+/* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
+#include "simgrid/simix.hpp"
#include <simgrid/Exception.hpp>
#include <simgrid/kernel/routing/NetPoint.hpp>
#include <simgrid/vm.h>
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_vm, s4u, "S4U virtual machines");
-namespace simgrid {
-namespace s4u {
-xbt::signal<void(VirtualMachine&)> VirtualMachine::on_creation;
+namespace simgrid::s4u {
+xbt::signal<void(VirtualMachine&)> VirtualMachine::on_vm_creation;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_start;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_started;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_shutdown;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_resume;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_migration_start;
xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_migration_end;
-xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_destruction;
+xbt::signal<void(VirtualMachine const&)> VirtualMachine::on_vm_destruction;
xbt::Extension<Host, VmHostExt> VmHostExt::EXTENSION_ID;
EXTENSION_ID = Host::extension_create<VmHostExt>();
}
-VirtualMachine::VirtualMachine(const std::string& name, s4u::Host* physical_host, int core_amount)
- : VirtualMachine(name, physical_host, core_amount, 1024)
-{
-}
-
VirtualMachine::VirtualMachine(const std::string& name, s4u::Host* physical_host, int core_amount, size_t ramsize)
: Host(new kernel::resource::VirtualMachineImpl(name, this, physical_host, core_amount, ramsize))
, pimpl_vm_(dynamic_cast<kernel::resource::VirtualMachineImpl*>(Host::get_impl()))
{
- XBT_DEBUG("Create VM %s", get_cname());
-
- /* Currently, a VM uses the network resource of its physical host */
- set_netpoint(physical_host->get_netpoint());
-
- // Create a VCPU for this VM
- std::vector<double> speeds;
- for (unsigned long i = 0; i < physical_host->get_pstate_count(); i++)
- speeds.push_back(physical_host->get_pstate_speed(i));
-
- physical_host->get_netpoint()
- ->get_englobing_zone()
- ->get_cpu_vm_model()
- ->create_cpu(this, speeds)
- ->set_core_count(core_amount)
- ->seal();
-
- if (physical_host->get_pstate() != 0)
- set_pstate(physical_host->get_pstate());
+ physical_host->get_impl()->create_vm(name, this);
+}
- seal(); // seal this host
- s4u::VirtualMachine::on_creation(*this);
+VirtualMachine::VirtualMachine(kernel::resource::VirtualMachineImpl* impl)
+ : Host(impl), pimpl_vm_(dynamic_cast<kernel::resource::VirtualMachineImpl*>(Host::get_impl()))
+{
}
void VirtualMachine::start()
{
- on_start(*this);
-
- VmHostExt::ensureVmExtInstalled();
-
- kernel::actor::simcall([this]() {
- Host* pm = this->pimpl_vm_->get_physical_host();
- if (pm->extension<VmHostExt>() == nullptr)
- pm->extension_set(new VmHostExt());
-
- size_t pm_ramsize = pm->extension<VmHostExt>()->ramsize;
- if (pm_ramsize && not pm->extension<VmHostExt>()->overcommit) { /* Need to verify that we don't overcommit */
- /* Retrieve the memory occupied by the VMs on that host. Yep, we have to traverse all VMs of all hosts for that */
- size_t total_ramsize_of_vms = 0;
- for (VirtualMachine* const& ws_vm : kernel::resource::VirtualMachineImpl::allVms_)
- if (pm == ws_vm->get_pm())
- total_ramsize_of_vms += ws_vm->get_ramsize();
-
- if (total_ramsize_of_vms + get_ramsize() > pm_ramsize) {
- XBT_WARN("cannot start %s@%s due to memory shortage: get_ramsize() %zu, free %zu, pm_ramsize %zu (bytes).",
- get_cname(), pm->get_cname(), get_ramsize(), pm_ramsize - total_ramsize_of_vms, pm_ramsize);
- throw VmFailureException(XBT_THROW_POINT,
- xbt::string_printf("Memory shortage on host '%s', VM '%s' cannot be started",
- pm->get_cname(), get_cname()));
- }
- }
- this->pimpl_vm_->set_state(State::RUNNING);
- });
-
- on_started(*this);
+ kernel::actor::simcall_answered([this]() { pimpl_vm_->start(); });
}
void VirtualMachine::suspend()
{
- on_suspend(*this);
- kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
- kernel::actor::simcall([this, issuer]() { pimpl_vm_->suspend(issuer); });
+ const kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
+ kernel::actor::simcall_answered([this, issuer]() { pimpl_vm_->suspend(issuer); });
}
void VirtualMachine::resume()
{
pimpl_vm_->resume();
- on_resume(*this);
}
void VirtualMachine::shutdown()
{
kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
- kernel::actor::simcall([this, issuer]() { pimpl_vm_->shutdown(issuer); });
- on_shutdown(*this);
+ kernel::actor::simcall_answered([this, issuer]() { pimpl_vm_->shutdown(issuer); });
}
void VirtualMachine::destroy()
shutdown();
XBT_DEBUG("destroy %s", get_cname());
-
+ on_vm_destruction(*this);
/* Then, destroy the VM object */
- kernel::actor::simcall([this]() {
- get_vm_impl()->vm_destroy();
- get_impl()->destroy();
-
- /* Don't free these things twice: they are the ones of my physical host */
- set_netpoint(nullptr);
- delete this;
- });
+ kernel::actor::simcall_answered(
+ [this]() { get_vm_impl()->get_physical_host()->get_impl()->destroy_vm(get_name()); });
};
if (not this_actor::is_maestro() && this_actor::get_host() == this) {
XBT_VERB("Launch another actor on physical host %s to destroy my own VM: %s", get_pm()->get_cname(), get_cname());
- simgrid::s4u::Actor::create(get_cname() + std::string("-vm_destroy"), get_pm(), destroy_code);
+ simgrid::s4u::Actor::create(get_name() + "-vm_destroy", get_pm(), destroy_code);
simgrid::s4u::this_actor::yield();
XBT_CRITICAL("I should be dead now!");
DIE_IMPOSSIBLE;
VirtualMachine* VirtualMachine::set_pm(simgrid::s4u::Host* pm)
{
- kernel::actor::simcall([this, pm]() { pimpl_vm_->set_physical_host(pm); });
+ kernel::actor::simcall_answered([this, pm]() { pimpl_vm_->set_physical_host(pm); });
return this;
}
VirtualMachine::State VirtualMachine::get_state() const
{
- return kernel::actor::simcall([this]() { return pimpl_vm_->get_state(); });
+ return kernel::actor::simcall_answered([this]() { return pimpl_vm_->get_state(); });
}
size_t VirtualMachine::get_ramsize() const
/** @brief Set a CPU bound for a given VM.
* @ingroup msg_VMs
*
- * 1. Note that in some cases MSG_task_set_bound() may not intuitively work for VMs.
+ * 1. Note that in some cases sg_exec_set_bound() may not intuitively work for VMs.
*
* For example,
* On PM0, there are Task1 and VM0.
* It should be the sum of all tasks on the VM. But, this solution might be costly, because we have to scan all tasks
* on the VM in share_resource() or we have to trap both the start and end of task execution.
*
- * The current solution is to use setBound(), which allows us to directly set the bound of the dummy CPU action.
+ * The current solution is to use set_bound(), which allows us to directly set the bound of the dummy CPU action.
*
* 2. Note that bound == 0 means no bound (i.e., unlimited). But, if a host has multiple CPU cores, the CPU share of a
* computation task (or a VM) never exceeds the capacity of a CPU core.
*/
VirtualMachine* VirtualMachine::set_bound(double bound)
{
- kernel::actor::simcall([this, bound]() { pimpl_vm_->set_bound(bound); });
+ kernel::actor::simcall_answered([this, bound]() { pimpl_vm_->set_bound(bound); });
return this;
}
-} // namespace s4u
-} // namespace simgrid
+void VirtualMachine::start_migration() const
+{
+ pimpl_vm_->start_migration();
+}
+
+void VirtualMachine::end_migration() const
+{
+ pimpl_vm_->end_migration();
+}
+
+} // namespace simgrid::s4u
/* **************************** Public C interface *************************** */