Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
68bd7c3b0c4ff16bc41a8cdef479a28179baebe8
[simgrid.git] / src / plugins / vm / VirtualMachineImpl.hpp
1 /* Copyright (c) 2004-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/s4u/VirtualMachine.hpp"
7 #include "src/kernel/actor/ActorImpl.hpp"
8 #include "src/surf/HostImpl.hpp"
9 #include <algorithm>
10 #include <deque>
11 #include <unordered_map>
12
13 #ifndef VM_INTERFACE_HPP_
14 #define VM_INTERFACE_HPP_
15
16 namespace simgrid {
17
18 extern template class XBT_PUBLIC xbt::Extendable<vm::VirtualMachineImpl>;
19
20 namespace vm {
21
22 /************
23  * Resource *
24  ************/
25
26 /** @ingroup SURF_vm_interface
27  * @brief SURF VM interface class
28  * @details A VM represent a virtual machine
29  */
30 class XBT_PUBLIC VirtualMachineImpl : public surf::HostImpl, public simgrid::xbt::Extendable<VirtualMachineImpl> {
31 #ifndef DOXYGEN
32   friend simgrid::s4u::VirtualMachine;
33 #endif
34
35 public:
36   /** @brief Callbacks fired after VM creation. Signature: `void(VirtualMachineImpl&)` */
37   static xbt::signal<void(simgrid::vm::VirtualMachineImpl&)> on_creation;
38   /** @brief Callbacks fired after VM destruction. Signature: `void(VirtualMachineImpl const&)` */
39   static xbt::signal<void(simgrid::vm::VirtualMachineImpl const&)> on_destruction;
40
41   static std::deque<s4u::VirtualMachine*> allVms_;
42
43   explicit VirtualMachineImpl(const std::string& name, s4u::VirtualMachine* piface, s4u::Host* host, int core_amount,
44                               size_t ramsize);
45   ~VirtualMachineImpl() override;
46
47   void suspend(kernel::actor::ActorImpl* issuer);
48   void resume();
49   void shutdown(kernel::actor::ActorImpl* issuer);
50
51   /** @brief Change the physical host on which the given VM is running */
52   void set_physical_host(s4u::Host* dest);
53   /** @brief Get the physical host on which the given VM is running */
54   s4u::Host* get_physical_host() const { return physical_host_; }
55
56   sg_size_t get_ramsize() const { return ramsize_; }
57   void set_ramsize(sg_size_t ramsize) { ramsize_ = ramsize; }
58
59   s4u::VirtualMachine::State get_state() const { return vm_state_; }
60   void set_state(s4u::VirtualMachine::State state) { vm_state_ = state; }
61
62   unsigned int get_core_amount() const { return core_amount_; }
63   kernel::resource::Action* get_action() const { return action_; }
64
65   const s4u::VirtualMachine* get_iface() const override { return piface_; }
66   s4u::VirtualMachine* get_iface() override { return piface_; }
67
68   void set_bound(double bound);
69
70   void update_action_weight();
71
72   void add_active_exec() { active_execs_++; }
73   void remove_active_exec() { active_execs_--; }
74
75   void start_migration() { is_migrating_ = true; }
76   void end_migration() { is_migrating_ = false; }
77   bool is_migrating() const { return is_migrating_; }
78
79 private:
80   s4u::VirtualMachine* piface_;
81   kernel::resource::Action* action_ = nullptr;
82   unsigned int active_execs_        = 0;
83   s4u::Host* physical_host_;
84   unsigned int core_amount_;
85   double user_bound_                   = std::numeric_limits<double>::max();
86   size_t ramsize_                      = 0;
87   s4u::VirtualMachine::State vm_state_ = s4u::VirtualMachine::State::CREATED;
88   bool is_migrating_                   = false;
89 };
90
91 /*********
92  * Model *
93  *********/
94 /** @ingroup SURF_vm_interface
95  * @brief SURF VM model interface class
96  * @details A model is an object which handle the interactions between its Resources and its Actions
97  */
98 class XBT_PRIVATE VMModel : public surf::HostModel {
99 public:
100   explicit VMModel(const std::string& name);
101
102   double next_occurring_event(double now) override;
103   void update_actions_state(double /*now*/, double /*delta*/) override{};
104   kernel::resource::Action* execute_parallel(const std::vector<s4u::Host*>& host_list, const double* flops_amount,
105                                              const double* bytes_amount, double rate) override
106   {
107     return nullptr;
108   };
109 };
110 } // namespace vm
111 } // namespace simgrid
112
113 #endif /* VM_INTERFACE_HPP_ */