Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove deprecated features for next release (3.36).
[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 #ifndef DOXYGEN
63   // No copy/move
64   VirtualMachine(VirtualMachine const&) = delete;
65   VirtualMachine& operator=(VirtualMachine const&) = delete;
66 #endif
67
68   // enum class State { ... }
69   XBT_DECLARE_ENUM_CLASS(State,
70     CREATED, /**< created, but not yet started */
71     RUNNING,
72     SUSPENDED, /**< Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
73     DESTROYED
74   );
75
76   kernel::resource::VirtualMachineImpl* get_vm_impl() const { return pimpl_vm_; }
77   void start();
78   void suspend();
79   void resume();
80   void shutdown();
81   void destroy() override;
82
83   Host* get_pm() const;
84   VirtualMachine* set_pm(Host* pm);
85   size_t get_ramsize() const;
86   VirtualMachine* set_ramsize(size_t ramsize);
87   VirtualMachine* set_bound(double bound);
88   void start_migration() const;
89   void end_migration() const;
90
91   State get_state() const;
92
93   /* Callbacks on signals */
94   /*! \static Add a callback fired when any VM is created */
95   static void on_creation_cb(const std::function<void(VirtualMachine&)>& cb) { on_vm_creation.connect(cb); }
96   /*! \static Add a callback fired when any VM starts */
97   static void on_start_cb(const std::function<void(VirtualMachine const&)>& cb) { on_start.connect(cb); }
98   /*! Add a callback fired when this specific VM starts */
99   void on_this_start_cb(const std::function<void(VirtualMachine const&)>& cb)
100   {
101     on_this_start.connect(cb);
102   }
103   /*! \static Add a callback fired when any VM is actually started */
104   static void on_started_cb(const std::function<void(VirtualMachine const&)>& cb) { on_started.connect(cb); }
105   /*! Add a callback fired when this specific VM is actually started */
106   void on_this_started_cb(const std::function<void(VirtualMachine const&)>& cb)
107   {
108     on_this_started.connect(cb);
109   }
110   /*! \static Add a callback fired when any VM is shut down */
111   static void on_shutdown_cb(const std::function<void(VirtualMachine const&)>& cb) { on_shutdown.connect(cb); }
112   /*! Add a callback fired when this specific VM is shut down */
113   void on_this_shutdown_cb(const std::function<void(VirtualMachine const&)>& cb)
114   {
115     on_this_shutdown.connect(cb);
116   }
117   /*! \static Add a callback fired when any VM is suspended*/
118   static void on_suspend_cb(const std::function<void(VirtualMachine const&)>& cb) { on_suspend.connect(cb); }
119   /*! Add a callback fired when this specific VM is suspended*/
120   void on_this_suspend_cb(const std::function<void(VirtualMachine const&)>& cb)
121   {
122     on_this_suspend.connect(cb);
123   }
124   /*! \static Add a callback fired when any VM is resumed*/
125   static void on_resume_cb(const std::function<void(VirtualMachine const&)>& cb) { on_resume.connect(cb); }
126   /*! Add a callback fired when this specific VM is resumed*/
127   void on_this_resume_cb(const std::function<void(VirtualMachine const&)>& cb)
128   {
129     on_this_resume.connect(cb);
130   }
131   /*! \static Add a callback fired when any VM is destroyed*/
132   static void on_destruction_cb(const std::function<void(VirtualMachine const&)>& cb) { on_vm_destruction.connect(cb); }
133   /*! Add a callback fired when this specific VM is destroyed*/
134   void on_this_destruction_cb(const std::function<void(VirtualMachine const&)>& cb)
135   {
136     on_this_vm_destruction.connect(cb);
137   }
138   /*! \static Add a callback fired when any VM starts a migration*/
139   static void on_migration_start_cb(const std::function<void(VirtualMachine const&)>& cb)
140   {
141     on_migration_start.connect(cb);
142   }
143   /*! Add a callback fired when this specific VM starts a migration*/
144   void on_this_migration_start_cb(const std::function<void(VirtualMachine const&)>& cb)
145   {
146     on_this_migration_start.connect(cb);
147   }
148   /*! \static Add a callback fired when any VM ends a migration*/
149   static void on_migration_end_cb(const std::function<void(VirtualMachine const&)>& cb)
150   {
151     on_migration_end.connect(cb);
152   }
153   /*! Add a callback fired when this specific VM ends a migration*/
154   void on_this_migration_end_cb(const std::function<void(VirtualMachine const&)>& cb)
155   {
156     on_this_migration_end.connect(cb);
157   }
158 };
159 } // namespace simgrid::s4u
160
161 #endif