Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bf67c6a42f8d90bde1b6202717721e42a00f1d32
[simgrid.git] / src / kernel / resource / Resource.hpp
1 /* Copyright (c) 2004-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_KERNEL_RESOURCE_RESOURCE_HPP
7 #define SIMGRID_KERNEL_RESOURCE_RESOURCE_HPP
8
9 #include "simgrid/forward.h"
10 #include "src/kernel/actor/Simcall.hpp"
11 #include "src/kernel/lmm/maxmin.hpp" // Constraint
12 #include "src/kernel/resource/profile/Event.hpp"
13 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
14 #include "src/kernel/resource/profile/Profile.hpp"
15 #include "xbt/signal.hpp"
16 #include "xbt/str.h"
17 #include "xbt/utility.hpp"
18
19 #include <string>
20
21 namespace simgrid::kernel::resource {
22
23 /** @ingroup Model_interface
24  * @brief Resource interface class
25  * @details This is the ancestor class of every resources in SimGrid, such as links, CPU or disk
26  */
27 class XBT_PUBLIC Resource : public actor::ObjectAccessSimcallItem {
28   std::string name_            = "unnamed";
29   bool is_on_                  = true;
30   bool sealed_                 = false;
31   profile::Event* state_event_ = nullptr;
32
33 protected:
34   struct Metric {
35     double peak;           /**< The peak of the metric, ie its max value */
36     double scale;          /**< Current availability of the metric according to the profiles, in [0,1] */
37     profile::Event* event; /**< The associated profile event associated to the metric */
38   };
39
40   virtual profile::Event* get_state_event() const { return state_event_; }
41   virtual void set_state_event(profile::Event* evt) { state_event_ = evt; }
42   virtual void unref_state_event() { tmgr_trace_event_unref(&state_event_); }
43
44 public:
45   explicit Resource(const std::string& name) : name_(name){};
46   virtual ~Resource() = default;
47   virtual void seal() { sealed_ = true; }
48
49   /** @brief Get the name of the current Resource */
50   const std::string& get_name() const { return name_; }
51   /** @brief Get the name of the current Resource */
52   const char* get_cname() const { return name_.c_str(); }
53
54   bool operator==(const Resource& other) const { return name_ == other.name_; }
55
56   /** @brief Apply an event of external load event to that resource */
57   virtual void apply_event(profile::Event* event, double value) = 0;
58
59   /** @brief Check if the current Resource is active */
60   virtual bool is_on() const { return is_on_; }
61   virtual bool is_sealed() const { return sealed_; }
62
63   /** @brief Check if the current Resource is used (if it currently serves an action) */
64   virtual bool is_used() const = 0;
65   /** @brief Turn on the current Resource */
66   virtual void turn_on() { is_on_ = true; }
67   /** @brief Turn off the current Resource */
68   virtual void turn_off() { is_on_ = false; }
69 };
70
71 template <class AnyResource> class Resource_T : public Resource {
72   Model* model_                = nullptr;
73   lmm::Constraint* constraint_ = nullptr;
74
75 public:
76   using Resource::Resource;
77   /** @brief setup the profile file with states events (ON or OFF). The profile must contain boolean values. */
78   AnyResource* set_state_profile(profile::Profile* profile)
79   {
80     if (profile) {
81       xbt_assert(get_state_event() == nullptr, "Cannot set a second state profile to %s", get_cname());
82       set_state_event(profile->schedule(&profile::future_evt_set, this));
83     }
84
85     return static_cast<AnyResource*>(this);
86   }
87
88   AnyResource* set_model(Model* model)
89   {
90     model_ = model;
91     return static_cast<AnyResource*>(this);
92   }
93
94   Model* get_model() const { return model_; }
95
96   AnyResource* set_constraint(lmm::Constraint* constraint)
97   {
98     constraint_ = constraint;
99     return static_cast<AnyResource*>(this);
100   }
101
102   lmm::Constraint* get_constraint() const { return constraint_; }
103
104   /** @brief returns the current load due to activities (in flops per second, byte per second or similar)
105    *
106    * The load due to external usages modeled by profile files is ignored.*/
107   virtual double get_load() const { return constraint_->get_usage(); }
108
109   bool is_used() const override { return model_->get_maxmin_system()->constraint_used(constraint_); }
110 };
111
112 } // namespace simgrid::kernel::resource
113
114 namespace std {
115 template <> class hash<simgrid::kernel::resource::Resource> {
116 public:
117   std::size_t operator()(const simgrid::kernel::resource::Resource& r) const
118   {
119     return (std::size_t)xbt_str_hash(r.get_cname());
120   }
121 };
122 } // namespace std
123
124 #endif