Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix the fixme (inline templated struct).
[simgrid.git] / src / surf / HostImpl.hpp
1 /* Copyright (c) 2004-2022. 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 SURF_HOST_INTERFACE_HPP
7 #define SURF_HOST_INTERFACE_HPP
8
9 #include "src/kernel/actor/ActorImpl.hpp"
10 #include "src/kernel/resource/CpuImpl.hpp"
11 #include "src/kernel/resource/DiskImpl.hpp"
12 #include <xbt/PropertyHolder.hpp>
13
14 #include <vector>
15
16 namespace simgrid::kernel::resource {
17 /*********
18  * Model *
19  *********/
20
21 /** @ingroup SURF_host_interface
22  * @brief SURF Host model interface class
23  * @details A model is an object which handle the interactions between its Resources and its Actions
24  */
25 class XBT_PRIVATE HostModel : public Model {
26 public:
27   using Model::Model;
28   virtual Action* execute_thread(const s4u::Host* host, double flops_amount, int thread_count) = 0;
29
30   virtual Action* execute_parallel(const std::vector<s4u::Host*>& host_list, const double* flops_amount,
31                                    const double* bytes_amount, double rate) = 0;
32 };
33
34 /************
35  * Resource *
36  ************/
37 /** @ingroup SURF_host_interface
38  * @brief SURF Host interface class
39  * @details A host represents a machine with an aggregation of a Cpu, a RoutingEdge and Disk(s)
40  */
41 class XBT_PRIVATE HostImpl : public xbt::PropertyHolder {
42   using ActorList =
43       boost::intrusive::list<actor::ActorImpl,
44                              boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
45                                                            &actor::ActorImpl::host_actor_list_hook>>;
46
47   ActorList actor_list_;
48   std::vector<actor::ProcessArg*> actors_at_boot_;
49   s4u::Host piface_;
50   std::map<std::string, DiskImpl*, std::less<>> disks_;
51   std::map<std::string, VirtualMachineImpl*, std::less<>> vms_;
52   xbt::string name_{"noname"};
53   routing::NetZoneImpl* englobing_zone_ = nullptr;
54   bool sealed_ = false;
55
56 protected:
57   virtual ~HostImpl(); // Use destroy() instead of this destructor.
58
59 public:
60   friend VirtualMachineImpl;
61   explicit HostImpl(const std::string& name);
62
63   void destroy(); // Must be called instead of the destructor
64
65   std::vector<s4u::Disk*> get_disks() const;
66   s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
67   s4u::VirtualMachine* create_vm(const std::string& name, int core_amount, size_t ramsize = 1024);
68   s4u::VirtualMachine* create_vm(const std::string& name, s4u::VirtualMachine* vm);
69   void destroy_vm(const std::string& name);
70   void add_disk(const s4u::Disk* disk);
71   void remove_disk(const std::string& name);
72   /** @brief Moves VM from this host to destination. Only sets the vm_ accordingly */
73   void move_vm(VirtualMachineImpl* vm, HostImpl* destination);
74   std::vector<s4u::VirtualMachine*> get_vms() const;
75   VirtualMachineImpl* get_vm_by_name_or_null(const std::string& name) const;
76
77   virtual const s4u::Host* get_iface() const { return &piface_; }
78   virtual s4u::Host* get_iface() { return &piface_; }
79
80   /** Retrieves the name of that host as a C++ string */
81   xbt::string const& get_name() const { return name_; }
82   /** Retrieves the name of that host as a C string */
83   const char* get_cname() const { return name_.c_str(); }
84
85   routing::NetZoneImpl* get_englobing_zone() const { return englobing_zone_; }
86   /** @brief Set the NetZone in which this Host is included */
87   HostImpl* set_englobing_zone(routing::NetZoneImpl* netzone_p);
88
89   void turn_on() const;
90   void turn_off(const actor::ActorImpl* issuer);
91   std::vector<s4u::ActorPtr> get_all_actors();
92   size_t get_actor_count() const;
93   void add_actor(actor::ActorImpl* actor) { actor_list_.push_back(*actor); }
94   void remove_actor(actor::ActorImpl* actor) { xbt::intrusive_erase(actor_list_, *actor); }
95   void add_actor_at_boot(actor::ProcessArg* arg) { actors_at_boot_.emplace_back(arg); }
96
97   virtual void seal();
98
99   template <class F> void foreach_actor(F function)
100   {
101     for (auto& actor : actor_list_)
102       function(actor);
103   }
104 };
105 } // namespace simgrid::kernel::resource
106
107 #endif /* HOST_INTERFACE_HPP */