Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various doc cleanups
[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 "cpu_cas01.hpp"
8 #include "virtual_machine.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
11
12 simgrid::surf::VMModel *surf_vm_model = NULL;
13
14 namespace simgrid {
15 namespace surf {
16
17 /*************
18  * Callbacks *
19  *************/
20
21 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> VMCreatedCallbacks;
22 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> VMDestructedCallbacks;
23 simgrid::xbt::signal<void(simgrid::surf::VirtualMachine*)> VMStateChangedCallbacks;
24
25 /*********
26  * Model *
27  *********/
28
29 VMModel::vm_list_t VMModel::ws_vms;
30
31 /************
32  * Resource *
33  ************/
34
35 VirtualMachine::VirtualMachine(HostModel *model, const char *name, simgrid::s4u::Host *hostPM)
36 : HostImpl(model, name, NULL, NULL, NULL)
37 , hostPM_(hostPM)
38 {
39   VMModel::ws_vms.push_back(*this);
40   simgrid::s4u::Host::by_name_or_create(name)->extension_set<simgrid::surf::HostImpl>(this);
41 }
42
43 /*
44  * A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation.
45  */
46 VirtualMachine::~VirtualMachine()
47 {
48   VMDestructedCallbacks(this);
49   VMModel::ws_vms.erase(VMModel::vm_list_t::s_iterator_to(*this));
50   /* Free the cpu_action of the VM. */
51   XBT_ATTRIB_UNUSED int ret = action_->unref();
52   xbt_assert(ret == 1, "Bug: some resource still remains");
53 }
54
55 e_surf_vm_state_t VirtualMachine::getState() {
56   return p_vm_state;
57 }
58
59 void VirtualMachine::setState(e_surf_vm_state_t state) {
60   p_vm_state = state;
61 }
62 void VirtualMachine::turnOn() {
63   if (isOff()) {
64     Resource::turnOn();
65     VMStateChangedCallbacks(this);
66   }
67 }
68 void VirtualMachine::turnOff() {
69   if (isOn()) {
70     Resource::turnOff();
71     VMStateChangedCallbacks(this);
72   }
73 }
74
75 /** @brief returns the physical machine on which the VM is running **/
76 sg_host_t VirtualMachine::getPm() {
77   return hostPM_;
78 }
79
80 }
81 }