Logo AND Algorithmique Numérique Distribuée

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