Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3b35bf3d1403148a90f11147af61bf71b2a117a8
[simgrid.git] / src / surf / virtual_machine.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <xbt/signal.hpp>
8
9 #include "cpu_cas01.hpp"
10 #include "virtual_machine.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
13
14 simgrid::surf::VMModel *surf_vm_model = nullptr;
15
16 namespace simgrid {
17 namespace surf {
18
19 /*************
20  * Callbacks *
21  *************/
22
23 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmCreation;
24 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmDestruction;
25 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> onVmStateChange;
26
27 /*********
28  * Model *
29  *********/
30
31 std::deque<VirtualMachine*> VirtualMachine::allVms_;
32
33 /************
34  * Resource *
35  ************/
36
37 VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u::Host *hostPM)
38 : HostImpl(model, name, nullptr, nullptr, nullptr)
39 , hostPM_(hostPM)
40 {
41   allVms_.push_back(this);
42   piface_ = simgrid::s4u::Host::by_name_or_create(name);
43   piface_->extension_set<simgrid::surf::HostImpl>(this);
44 }
45
46 /*
47  * A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation.
48  */
49 VirtualMachine::~VirtualMachine()
50 {
51   onVmDestruction(this);
52   allVms_.erase( find(allVms_.begin(), allVms_.end(), this) );
53
54   /* Free the cpu_action of the VM. */
55   XBT_ATTRIB_UNUSED int ret = action_->unref();
56   xbt_assert(ret == 1, "Bug: some resource still remains");
57 }
58
59 e_surf_vm_state_t VirtualMachine::getState() {
60   return vmState_;
61 }
62
63 void VirtualMachine::setState(e_surf_vm_state_t state) {
64   vmState_ = state;
65 }
66 void VirtualMachine::turnOn() {
67   if (isOff()) {
68     Resource::turnOn();
69     onVmStateChange(this);
70   }
71 }
72 void VirtualMachine::turnOff() {
73   if (isOn()) {
74     Resource::turnOff();
75     onVmStateChange(this);
76   }
77 }
78 void VirtualMachine::suspend()
79 {
80   action_->suspend();
81   vmState_ = SURF_VM_STATE_SUSPENDED;
82 }
83
84 void VirtualMachine::resume()
85 {
86   action_->resume();
87   vmState_ = SURF_VM_STATE_RUNNING;
88 }
89
90 void VirtualMachine::save()
91 {
92   vmState_ = SURF_VM_STATE_SAVING;
93
94   /* FIXME: do something here */
95   action_->suspend();
96   vmState_ = SURF_VM_STATE_SAVED;
97 }
98
99 void VirtualMachine::restore()
100 {
101   vmState_ = SURF_VM_STATE_RESTORING;
102
103   /* FIXME: do something here */
104   action_->resume();
105   vmState_ = SURF_VM_STATE_RUNNING;
106 }
107
108 /** @brief returns the physical machine on which the VM is running **/
109 sg_host_t VirtualMachine::getPm() {
110   return hostPM_;
111 }
112
113 }
114 }