Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6733ba3784c2c235ab277d55ae92ed25cfbd9b2c
[simgrid.git] / include / simgrid / s4u / VirtualMachine.hpp
1 /* Copyright (c) 2015-2023. 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 #ifndef SIMGRID_S4U_VM_HPP
7 #define SIMGRID_S4U_VM_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Host.hpp>
11 #include <xbt/utility.hpp>
12
13 namespace simgrid::s4u {
14
15 /** @brief Host extension for the VMs */
16 class VmHostExt {
17 public:
18   static xbt::Extension<s4u::Host, VmHostExt> EXTENSION_ID;
19
20   sg_size_t ramsize = 0;    /* available ramsize (0= not taken into account) */
21   bool overcommit   = true; /* Whether the host allows overcommiting more VM than the avail ramsize allows */
22   static void ensureVmExtInstalled();
23 };
24
25 /** @ingroup s4u_api
26  *
27  * @tableofcontents
28  *
29  * A VM represents a virtual machine (or a container) that hosts actors.
30  * The total computing power that the contained actors can get is constrained to the virtual machine size.
31  *
32  */
33 class XBT_PUBLIC VirtualMachine : public s4u::Host {
34   kernel::resource::VirtualMachineImpl* const pimpl_vm_;
35
36   /* Signals about the life cycle of the VM */
37   static xbt::signal<void(VirtualMachine&)> on_vm_creation;
38   static xbt::signal<void(VirtualMachine const&)> on_start;
39   xbt::signal<void(VirtualMachine const&)> on_this_start;
40   static xbt::signal<void(VirtualMachine const&)> on_started;
41   xbt::signal<void(VirtualMachine const&)> on_this_started;
42   static xbt::signal<void(VirtualMachine const&)> on_shutdown;
43   xbt::signal<void(VirtualMachine const&)> on_this_shutdown;
44   static xbt::signal<void(VirtualMachine const&)> on_suspend;
45   xbt::signal<void(VirtualMachine const&)> on_this_suspend;
46   static xbt::signal<void(VirtualMachine const&)> on_resume;
47   xbt::signal<void(VirtualMachine const&)> on_this_resume;
48   static xbt::signal<void(VirtualMachine const&)> on_migration_start;
49   xbt::signal<void(VirtualMachine const&)> on_this_migration_start;
50   static xbt::signal<void(VirtualMachine const&)> on_migration_end;
51   xbt::signal<void(VirtualMachine const&)> on_this_migration_end;
52   static xbt::signal<void(VirtualMachine const&)> on_vm_destruction;
53   xbt::signal<void(VirtualMachine const&)> on_this_vm_destruction;
54
55 #ifndef DOXYGEN
56   friend kernel::resource::VirtualMachineImpl; // calls signals from Impl
57   friend kernel::resource::HostImpl;           // call private constructor
58   explicit VirtualMachine(kernel::resource::VirtualMachineImpl* impl);
59 #endif
60
61 public:
62   XBT_ATTRIB_DEPRECATED_v336("Please use s4u::Host::create_vm") explicit VirtualMachine(const std::string& name,
63                                                                                         Host* physical_host,
64                                                                                         int core_amount,
65                                                                                         size_t ramsize = 1024);
66
67 #ifndef DOXYGEN
68   // No copy/move
69   VirtualMachine(VirtualMachine const&) = delete;
70   VirtualMachine& operator=(VirtualMachine const&) = delete;
71 #endif
72
73   // enum class State { ... }
74   XBT_DECLARE_ENUM_CLASS(State,
75     CREATED, /**< created, but not yet started */
76     RUNNING,
77     SUSPENDED, /**< Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
78     DESTROYED
79   );
80
81   kernel::resource::VirtualMachineImpl* get_vm_impl() const { return pimpl_vm_; }
82   void start();
83   void suspend();
84   void resume();
85   void shutdown();
86   void destroy() override;
87
88   Host* get_pm() const;
89   VirtualMachine* set_pm(Host* pm);
90   size_t get_ramsize() const;
91   VirtualMachine* set_ramsize(size_t ramsize);
92   VirtualMachine* set_bound(double bound);
93   void start_migration() const;
94   void end_migration() const;
95
96   State get_state() const;
97
98   /* Callbacks on signals */
99   /*! \static Add a callback fired when any VM is created */
100   static void on_creation_cb(const std::function<void(VirtualMachine&)>& cb) { on_vm_creation.connect(cb); }
101   /*! \static Add a callback fired when any VM starts */
102   static void on_start_cb(const std::function<void(VirtualMachine const&)>& cb) { on_start.connect(cb); }
103   /*! Add a callback fired when this specific VM starts */
104   void on_this_start_cb(const std::function<void(VirtualMachine const&)>& cb)
105   {
106     on_this_start.connect(cb);
107   }
108   /*! \static Add a callback fired when any VM is actually started */
109   static void on_started_cb(const std::function<void(VirtualMachine const&)>& cb) { on_started.connect(cb); }
110   /*! Add a callback fired when this specific VM is actually started */
111   void on_this_started_cb(const std::function<void(VirtualMachine const&)>& cb)
112   {
113     on_this_started.connect(cb);
114   }
115   /*! \static Add a callback fired when any VM is shut down */
116   static void on_shutdown_cb(const std::function<void(VirtualMachine const&)>& cb) { on_shutdown.connect(cb); }
117   /*! Add a callback fired when this specific VM is shut down */
118   void on_this_shutdown_cb(const std::function<void(VirtualMachine const&)>& cb)
119   {
120     on_this_shutdown.connect(cb);
121   }
122   /*! \static Add a callback fired when any VM is suspended*/
123   static void on_suspend_cb(const std::function<void(VirtualMachine const&)>& cb) { on_suspend.connect(cb); }
124   /*! Add a callback fired when this specific VM is suspended*/
125   void on_this_suspend_cb(const std::function<void(VirtualMachine const&)>& cb)
126   {
127     on_this_suspend.connect(cb);
128   }
129   /*! \static Add a callback fired when any VM is resumed*/
130   static void on_resume_cb(const std::function<void(VirtualMachine const&)>& cb) { on_resume.connect(cb); }
131   /*! Add a callback fired when this specific VM is resumed*/
132   void on_this_resume_cb(const std::function<void(VirtualMachine const&)>& cb)
133   {
134     on_this_resume.connect(cb);
135   }
136   /*! \static Add a callback fired when any VM is destroyed*/
137   static void on_destruction_cb(const std::function<void(VirtualMachine const&)>& cb) { on_vm_destruction.connect(cb); }
138   /*! Add a callback fired when this specific VM is destroyed*/
139   void on_this_destruction_cb(const std::function<void(VirtualMachine const&)>& cb)
140   {
141     on_this_vm_destruction.connect(cb);
142   }
143   /*! \static Add a callback fired when any VM starts a migration*/
144   static void on_migration_start_cb(const std::function<void(VirtualMachine const&)>& cb)
145   {
146     on_migration_start.connect(cb);
147   }
148   /*! Add a callback fired when this specific VM starts a migration*/
149   void on_this_migration_start_cb(const std::function<void(VirtualMachine const&)>& cb)
150   {
151     on_this_migration_start.connect(cb);
152   }
153   /*! \static Add a callback fired when any VM ends a migration*/
154   static void on_migration_end_cb(const std::function<void(VirtualMachine const&)>& cb)
155   {
156     on_migration_end.connect(cb);
157   }
158   /*! Add a callback fired when this specific VM ends a migration*/
159   void on_this_migration_end_cb(const std::function<void(VirtualMachine const&)>& cb)
160   {
161     on_this_migration_end.connect(cb);
162   }
163 };
164 } // namespace simgrid::s4u
165
166 #endif