Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'wifi_clean' into 'master'
[simgrid.git] / src / kernel / resource / DiskImpl.hpp
1 /* Copyright (c) 2019-2022. 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 #include "simgrid/kernel/resource/Action.hpp"
7 #include "simgrid/kernel/resource/Model.hpp"
8 #include "simgrid/s4u/Disk.hpp"
9 #include "simgrid/s4u/Io.hpp"
10 #include "src/kernel/resource/Resource.hpp"
11 #include "src/surf/surf_interface.hpp"
12 #include "xbt/PropertyHolder.hpp"
13
14 #include <map>
15
16 #ifndef DISK_IMPL_HPP_
17 #define DISK_IMPL_HPP_
18
19 /*********
20  * Model *
21  *********/
22
23 namespace simgrid::kernel::resource {
24 /***********
25  * Classes *
26  ***********/
27
28 class DiskAction;
29
30 /*********
31  * Model *
32  *********/
33 class DiskModel : public Model {
34 public:
35   explicit DiskModel(const std::string& name);
36   DiskModel(const DiskModel&) = delete;
37   DiskModel& operator=(const DiskModel&) = delete;
38
39   virtual DiskImpl* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth) = 0;
40
41   virtual DiskAction* io_start(const DiskImpl* disk, sg_size_t size, s4u::Io::OpType type) = 0;
42 };
43
44 /************
45  * Resource *
46  ************/
47 class DiskImpl : public Resource_T<DiskImpl>, public xbt::PropertyHolder {
48   s4u::Disk piface_;
49   s4u::Host* host_                   = nullptr;
50   lmm::Constraint* constraint_write_ = nullptr; /* Constraint for maximum write bandwidth*/
51   lmm::Constraint* constraint_read_  = nullptr; /* Constraint for maximum read bandwidth*/
52   std::unordered_map<s4u::Disk::Operation, s4u::Disk::SharingPolicy> sharing_policy_ = {
53       {s4u::Disk::Operation::READ, s4u::Disk::SharingPolicy::LINEAR},
54       {s4u::Disk::Operation::WRITE, s4u::Disk::SharingPolicy::LINEAR},
55       {s4u::Disk::Operation::READWRITE, s4u::Disk::SharingPolicy::LINEAR}};
56   std::unordered_map<s4u::Disk::Operation, s4u::NonLinearResourceCb> sharing_policy_cb_ = {};
57   std::function<s4u::Disk::IoFactorCb> factor_cb_                                       = {};
58
59   Metric read_bw_      = {0.0, 0, nullptr};
60   Metric write_bw_     = {0.0, 0, nullptr};
61   double readwrite_bw_ = -1; /* readwrite constraint bound, usually max(read, write) */
62
63   void apply_sharing_policy_cfg();
64
65 protected:
66   ~DiskImpl() override = default; // Disallow direct deletion. Call destroy() instead.
67
68 public:
69   explicit DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth);
70   DiskImpl(const DiskImpl&) = delete;
71   DiskImpl& operator=(const DiskImpl&) = delete;
72
73   /** @brief Public interface */
74   const s4u::Disk* get_iface() const { return &piface_; }
75   s4u::Disk* get_iface() { return &piface_; }
76   DiskImpl* set_host(s4u::Host* host);
77   s4u::Host* get_host() const { return host_; }
78
79   virtual void set_read_bandwidth(double value) { read_bw_.peak = value; }
80   double get_read_bandwidth() const { return read_bw_.peak * read_bw_.scale; }
81
82   virtual void set_write_bandwidth(double value) { write_bw_.peak = value; }
83   double get_write_bandwidth() const { return write_bw_.peak * write_bw_.scale; }
84
85   virtual void set_readwrite_bandwidth(double value) { readwrite_bw_ = value; }
86   double get_readwrite_bandwidth() const { return readwrite_bw_; }
87
88   DiskImpl* set_read_constraint(lmm::Constraint* constraint_read);
89   lmm::Constraint* get_read_constraint() const { return constraint_read_; }
90
91   DiskImpl* set_write_constraint(lmm::Constraint* constraint_write);
92   lmm::Constraint* get_write_constraint() const { return constraint_write_; }
93
94   profile::Event* get_read_event() const { return read_bw_.event; }
95   void unref_read_event() { tmgr_trace_event_unref(&read_bw_.event); }
96
97   profile::Event* get_write_event() const { return write_bw_.event; }
98   void unref_write_event() { tmgr_trace_event_unref(&write_bw_.event); }
99
100   DiskImpl* set_read_bandwidth_profile(profile::Profile* profile);
101   DiskImpl* set_write_bandwidth_profile(profile::Profile* profile);
102
103   void set_sharing_policy(s4u::Disk::Operation op, s4u::Disk::SharingPolicy policy, const s4u::NonLinearResourceCb& cb);
104   s4u::Disk::SharingPolicy get_sharing_policy(s4u::Disk::Operation op) const;
105
106   void set_factor_cb(const std::function<s4u::Disk::IoFactorCb>& cb);
107   const std::function<s4u::Disk::IoFactorCb>& get_factor_cb() const { return factor_cb_; }
108
109   void turn_on() override;
110   void turn_off() override;
111
112   void seal() override;
113   void destroy(); // Must be called instead of the destructor
114 };
115
116 /**********
117  * Action *
118  **********/
119
120 class DiskAction : public Action {
121 public:
122   static xbt::signal<void(DiskAction const&, Action::State, Action::State)> on_state_change;
123
124   using Action::Action;
125   void set_state(simgrid::kernel::resource::Action::State state) override;
126 };
127
128 } // namespace simgrid::kernel::resource
129 #endif /* DISK_IMPL_HPP_ */