Logo AND Algorithmique Numérique Distribuée

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