Logo AND Algorithmique Numérique Distribuée

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