Logo AND Algorithmique Numérique Distribuée

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