Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in kernel/resource
[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   std::string name_;
26   Model* model_;
27   bool is_on_ = true;
28
29   lmm::Constraint* const constraint_;
30
31 protected:
32   struct Metric {
33     double peak;           /**< The peak of the metric, ie its max value */
34     double scale;          /**< Current availability of the metric according to the profiles, in [0,1] */
35     profile::Event* event; /**< The associated profile event associated to the metric */
36   };
37
38 public:
39   /**
40    * @brief Constructor of LMM Resources
41    *
42    * @param model Model associated to this Resource
43    * @param name The name of the Resource
44    * @param constraint The lmm constraint associated to this Resource if it is part of a LMM component
45    */
46   Resource(Model* model, const std::string& name, lmm::Constraint* constraint)
47       : name_(name), model_(model), constraint_(constraint)
48   {
49   }
50
51   virtual ~Resource() = default;
52
53   /** @brief Get the Model of the current Resource */
54   Model* get_model() const { return model_; }
55
56   /** @brief Get the name of the current Resource */
57   const std::string& get_name() const { return name_; }
58   /** @brief Get the name of the current Resource */
59   const char* get_cname() const { return name_.c_str(); }
60
61   bool operator==(const Resource& other) const { return name_ == other.name_; }
62
63   /** @brief Apply an event of external load event to that resource */
64   virtual void apply_event(profile::Event* event, double value) = 0;
65
66   /** @brief Check if the current Resource is used (if it currently serves an action) */
67   virtual bool is_used() = 0;
68
69   /** @brief returns the current load due to activities (in flops per second, byte per second or similar)
70    *
71    * The load due to external usages modeled by profile files is ignored.*/
72   virtual double get_load() const;
73
74   /** @brief Check if the current Resource is active */
75   virtual bool is_on() const { return is_on_; }
76   /** @brief Turn on the current Resource */
77   virtual void turn_on() { is_on_ = true; }
78   /** @brief Turn off the current Resource */
79   virtual void turn_off() { is_on_ = false; }
80   /** @brief setup the profile file with states events (ON or OFF). The profile must contain boolean values. */
81   virtual void set_state_profile(profile::Profile* profile);
82
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   profile::Event* state_event_ = nullptr;
87 };
88 } // namespace resource
89 } // namespace kernel
90 } // namespace simgrid
91
92 namespace std {
93 template <> class hash<simgrid::kernel::resource::Resource> {
94 public:
95   std::size_t operator()(const simgrid::kernel::resource::Resource& r) const
96   {
97     return (std::size_t)xbt_str_hash(r.get_cname());
98   }
99 };
100 } // namespace std
101
102 #endif