Logo AND Algorithmique Numérique Distribuée

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