/* 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 <xbt/signal.hpp>
+
#include "cpu_cas01.hpp"
#include "virtual_machine.hpp"
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf,
- "Logging specific to the SURF VM module");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
-simgrid::surf::VMModel *surf_vm_model = NULL;
+simgrid::surf::VMModel *surf_vm_model = nullptr;
namespace simgrid {
namespace surf {
* Callbacks *
*************/
-surf_callback(void, simgrid::surf::VirtualMachine*) VMCreatedCallbacks;
-surf_callback(void, simgrid::surf::VirtualMachine*) VMDestructedCallbacks;
-surf_callback(void, simgrid::surf::VirtualMachine*) VMStateChangedCallbacks;
+simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmCreation;
+simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmDestruction;
+simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmStateChange;
/*********
* Model *
*********/
-VMModel::vm_list_t VMModel::ws_vms;
+std::deque<VirtualMachine*> VirtualMachine::allVms_;
/************
* Resource *
************/
-VirtualMachine::VirtualMachine(Model *model, const char *name, xbt_dict_t props,
- RoutingEdge *netElm, Cpu *cpu)
-: Host(model, name, props, NULL, netElm, cpu)
+VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u::Host *hostPM)
+: HostImpl(model, name, nullptr, nullptr, nullptr)
+, hostPM_(hostPM)
{
- VMModel::ws_vms.push_back(*this);
- simgrid::Host::by_name_or_create(name)->set_facet<simgrid::surf::Host>(this);
+ allVms_.push_back(this);
+ piface_ = simgrid::s4u::Host::by_name_or_create(name);
+ piface_->extension_set<simgrid::surf::HostImpl>(this);
}
/*
- * A physical host does not disappear in the current SimGrid code, but a VM may
- * disappear during a simulation.
+ * A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation.
*/
VirtualMachine::~VirtualMachine()
{
- surf_callback_emit(VMDestructedCallbacks, this);
- VMModel::ws_vms.erase(VMModel::vm_list_t::s_iterator_to(*this));
+ onVmDestruction(this);
+ allVms_.erase( find(allVms_.begin(), allVms_.end(), this) );
+
+ /* Free the cpu_action of the VM. */
+ XBT_ATTRIB_UNUSED int ret = action_->unref();
+ xbt_assert(ret == 1, "Bug: some resource still remains");
}
-void VirtualMachine::setState(e_surf_resource_state_t state){
- Resource::setState(state);
- surf_callback_emit(VMStateChangedCallbacks, this);
+e_surf_vm_state_t VirtualMachine::getState() {
+ return vmState_;
}
-/*
- * A surf level object will be useless in the upper layer. Returning the
- * dict_elm of the host.
- **/
-sg_host_t VirtualMachine::getPm()
+void VirtualMachine::setState(e_surf_vm_state_t state) {
+ vmState_ = state;
+}
+void VirtualMachine::turnOn() {
+ if (isOff()) {
+ Resource::turnOn();
+ onVmStateChange(this);
+ }
+}
+void VirtualMachine::turnOff() {
+ if (isOn()) {
+ Resource::turnOff();
+ onVmStateChange(this);
+ }
+}
+void VirtualMachine::suspend()
{
- return simgrid::Host::by_name_or_null(p_subWs->getName());
+ action_->suspend();
+ vmState_ = SURF_VM_STATE_SUSPENDED;
}
-/**********
- * Action *
- **********/
+void VirtualMachine::resume()
+{
+ action_->resume();
+ vmState_ = SURF_VM_STATE_RUNNING;
+}
-//FIME:: handle action cancel
+void VirtualMachine::save()
+{
+ vmState_ = SURF_VM_STATE_SAVING;
+
+ /* FIXME: do something here */
+ action_->suspend();
+ vmState_ = SURF_VM_STATE_SAVED;
+}
+
+void VirtualMachine::restore()
+{
+ vmState_ = SURF_VM_STATE_RESTORING;
+
+ /* FIXME: do something here */
+ action_->resume();
+ vmState_ = SURF_VM_STATE_RUNNING;
+}
+
+/** @brief returns the physical machine on which the VM is running **/
+sg_host_t VirtualMachine::getPm() {
+ return hostPM_;
+}
}
}