Logo AND Algorithmique Numérique Distribuée

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