Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into adrien
[simgrid.git] / src / plugins / vm / VirtualMachineImpl.hpp
1 /* Copyright (c) 2004-2020. 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   friend simgrid::s4u::VirtualMachine;
32
33 public:
34   /** @brief Callbacks fired after VM creation. Signature: `void(VirtualMachineImpl&)` */
35   static xbt::signal<void(simgrid::vm::VirtualMachineImpl&)> on_creation;
36   /** @brief Callbacks fired after VM destruction. Signature: `void(VirtualMachineImpl const&)` */
37   static xbt::signal<void(simgrid::vm::VirtualMachineImpl const&)> on_destruction;
38
39   static std::deque<s4u::VirtualMachine*> allVms_;
40
41   explicit VirtualMachineImpl(s4u::VirtualMachine* piface, s4u::Host* host, int core_amount, size_t ramsize);
42   ~VirtualMachineImpl();
43
44   virtual void suspend(kernel::actor::ActorImpl* issuer);
45   virtual void resume();
46   virtual void shutdown(kernel::actor::ActorImpl* issuer);
47
48   /** @brief Change the physical host on which the given VM is running */
49   virtual void set_physical_host(s4u::Host* dest);
50   /** @brief Get the physical host on which the given VM is running */
51   s4u::Host* get_physical_host() const { return physical_host_; }
52
53   sg_size_t get_ramsize() const { return ramsize_; }
54   void set_ramsize(sg_size_t ramsize) { ramsize_ = ramsize; }
55
56   s4u::VirtualMachine::state get_state() const { return vm_state_; }
57   void set_state(s4u::VirtualMachine::state state) { vm_state_ = state; }
58
59   unsigned int get_core_amount() const { return core_amount_; }
60   kernel::resource::Action* get_action() const { return action_; }
61
62   virtual void set_bound(double bound);
63
64   void update_action_weight();
65
66   void add_active_exec() { active_execs_++; }
67   void remove_active_exec() { active_execs_--; }
68
69   void start_migration() { is_migrating_ = true; }
70   void end_migration() { is_migrating_ = false; }
71   bool is_migrating() const { return is_migrating_; }
72
73 private:
74   kernel::resource::Action* action_ = nullptr;
75   unsigned int active_execs_        = 0;
76   s4u::Host* physical_host_;
77   unsigned int core_amount_;
78   double user_bound_                   = std::numeric_limits<double>::max();
79   size_t ramsize_                      = 0;
80   s4u::VirtualMachine::state vm_state_ = s4u::VirtualMachine::state::CREATED;
81   bool is_migrating_                   = false;
82 };
83
84 /*********
85  * Model *
86  *********/
87 /** @ingroup SURF_vm_interface
88  * @brief SURF VM model interface class
89  * @details A model is an object which handle the interactions between its Resources and its Actions
90  */
91 class XBT_PRIVATE VMModel : public surf::HostModel {
92 public:
93   VMModel();
94
95   double next_occurring_event(double now) override;
96   void update_actions_state(double /*now*/, double /*delta*/) override{};
97   kernel::resource::Action* execute_parallel(const std::vector<s4u::Host*>& host_list, const double* flops_amount,
98                                              const double* bytes_amount, double rate) override
99   {
100     return nullptr;
101   };
102 };
103 }
104 }
105
106 XBT_PUBLIC_DATA simgrid::vm::VMModel* surf_vm_model;
107
108 #endif /* VM_INTERFACE_HPP_ */