Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'odpor-implementation' into 'master'
[simgrid.git] / include / simgrid / s4u / Disk.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 INCLUDE_SIMGRID_S4U_DISK_HPP_
7 #define INCLUDE_SIMGRID_S4U_DISK_HPP_
8
9 #include <simgrid/disk.h>
10 #include <simgrid/forward.h>
11 #include <simgrid/s4u/Io.hpp>
12 #include <xbt/Extendable.hpp>
13 #include <xbt/base.h>
14 #include <xbt/signal.hpp>
15
16 #include <map>
17 #include <string>
18 #include <unordered_map>
19
20 namespace simgrid {
21
22 extern template class XBT_PUBLIC xbt::Extendable<s4u::Disk>;
23
24 namespace s4u {
25
26 /** Disk represent the disk resources associated to a host
27  *
28  * By default, SimGrid does not keep track of the actual data being written but
29  * only computes the time taken by the corresponding data movement.
30  */
31
32 class XBT_PUBLIC Disk : public xbt::Extendable<Disk> {
33 #ifndef DOXYGEN
34   friend Engine;
35   friend Io;
36   friend kernel::resource::DiskImpl;
37 #endif
38
39   explicit Disk(kernel::resource::DiskImpl* pimpl) : pimpl_(pimpl) {}
40   virtual ~Disk() = default;
41
42   // The private implementation, that never changes
43   kernel::resource::DiskImpl* const pimpl_;
44
45 public:
46 #ifndef DOXYGEN
47   kernel::resource::DiskImpl* get_impl() const { return pimpl_; }
48 #endif
49
50   std::string const& get_name() const;
51   /** @brief Retrieves the name of that disk as a C string */
52   const char* get_cname() const;
53
54   Disk* set_read_bandwidth(double read_bw);
55   double get_read_bandwidth() const;
56
57   Disk* set_write_bandwidth(double write_bw);
58   double get_write_bandwidth() const;
59
60   /**
61    * @brief Set limit for read/write operations.
62    *
63    * This determines the limit for read and write operation in the same disk.
64    * Usually, it's configured to max(read_bw, write_bw).
65    * You can change this behavior using this method
66    *
67    * @param bw New bandwidth for the disk
68    */
69   Disk* set_readwrite_bandwidth(double bw);
70
71   const std::unordered_map<std::string, std::string>* get_properties() const;
72   const char* get_property(const std::string& key) const;
73   Disk* set_property(const std::string&, const std::string& value);
74   Disk* set_properties(const std::unordered_map<std::string, std::string>& properties);
75
76   Disk* set_host(Host* host);
77   Host* get_host() const;
78
79   Disk* set_state_profile(kernel::profile::Profile* profile);
80   Disk* set_read_bandwidth_profile(kernel::profile::Profile* profile);
81   Disk* set_write_bandwidth_profile(kernel::profile::Profile* profile);
82   /**
83    * @brief Set the max amount of operations (either read or write) that can take place on this disk at the same time
84    *
85    * Use -1 to set no limit.
86    */
87   Disk* set_concurrency_limit(int limit);
88   int get_concurrency_limit() const;
89
90   IoPtr io_init(sg_size_t size, s4u::Io::OpType type) const;
91
92   IoPtr read_async(sg_size_t size) const;
93   sg_size_t read(sg_size_t size) const;
94   sg_size_t read(sg_size_t size, double priority) const;
95
96   IoPtr write_async(sg_size_t size) const;
97   sg_size_t write(sg_size_t size) const;
98   sg_size_t write(sg_size_t size, double priority) const;
99
100   /** @brief Policy for sharing the disk among activities */
101   enum class SharingPolicy { NONLINEAR = 1, LINEAR = 0 };
102   enum class Operation { READ = 2, WRITE = 1, READWRITE = 0 };
103
104   /**
105    * @brief Describes how the disk is shared between activities for each operation
106    *
107    * Disks have different bandwidths for read and write operations, that can have different policies:
108    * - Read: resource sharing for read operation
109    * - Write: resource sharing for write
110    * - ReadWrite: global sharing for read and write operations
111    *
112    * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
113    *
114    * @param op Operation type
115    * @param policy Sharing policy
116    * @param cb Callback for NONLINEAR policies
117    */
118   Disk* set_sharing_policy(Operation op, SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
119   SharingPolicy get_sharing_policy(Operation op) const;
120   /**
121    * @brief Callback to set IO factors
122    *
123    * This callback offers a flexible way to create variability in I/O operations
124    *
125    * @param size I/O operation size in bytes
126    * @param op I/O operation type: read or write
127    * @return Multiply factor
128    */
129   using IoFactorCb = double(sg_size_t size, Io::OpType op);
130   /** @brief Configure the factor callback */
131   Disk* set_factor_cb(const std::function<IoFactorCb>& cb);
132
133   Disk* seal();
134
135   /* The signals */
136   /** @brief \static Add a callback fired when a new Disk is created */
137   static void on_creation_cb(const std::function<void(Disk&)>& cb) { on_creation.connect(cb); }
138   /** @brief \static Add a callback fired when any Disk is destroyed */
139   static void on_destruction_cb(const std::function<void(Disk const&)>& cb) { on_destruction.connect(cb); }
140   /** @brief Add a callback fired when this specific Disk is destroyed */
141   void on_this_destruction_cb(const std::function<void(Disk const&)>& cb) { on_this_destruction.connect(cb); }
142   /** @brief \static Add a callback fired when any Disk is turned on or off */
143   static void on_onoff_cb(const std::function<void(Disk const&)>& cb)
144   {
145     on_onoff.connect(cb);
146   }
147   /** @brief Add a callback fired when this specific Disk is turned on or off */
148   void on_this_onoff_cb(const std::function<void(Disk const&)>& cb)
149   {
150     on_this_onoff.connect(cb);
151   }
152
153   XBT_ATTRIB_DEPRECATED_v337("Please use on_onoff_cb() instead") static void on_state_change_cb(
154       const std::function<void(Disk const&)>& cb)
155   {
156     on_onoff.connect(cb);
157   }
158
159 private:
160   static xbt::signal<void(Disk&)> on_creation;
161   static xbt::signal<void(Disk const&)> on_destruction;
162   xbt::signal<void(Disk const&)> on_this_destruction;
163   static xbt::signal<void(Disk const&)> on_onoff;
164   xbt::signal<void(Disk const&)> on_this_onoff;
165 };
166
167 } // namespace s4u
168 } // namespace simgrid
169
170 #endif /* INCLUDE_SIMGRID_S4U_DISK_HPP_ */