Logo AND Algorithmique Numérique Distribuée

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