1 /* Copyright (c) 2019-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef INCLUDE_SIMGRID_S4U_DISK_HPP_
7 #define INCLUDE_SIMGRID_S4U_DISK_HPP_
9 #include <simgrid/disk.h>
10 #include <simgrid/forward.h>
11 #include <simgrid/s4u/Io.hpp>
12 #include <xbt/Extendable.hpp>
14 #include <xbt/signal.hpp>
18 #include <unordered_map>
22 extern template class XBT_PUBLIC xbt::Extendable<s4u::Disk>;
26 /** Disk represent the disk resources associated to a host
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.
32 class XBT_PUBLIC Disk : public xbt::Extendable<Disk> {
36 friend kernel::resource::DiskImpl;
39 explicit Disk(kernel::resource::DiskImpl* pimpl) : pimpl_(pimpl) {}
40 virtual ~Disk() = default;
42 // The private implementation, that never changes
43 kernel::resource::DiskImpl* const pimpl_;
47 kernel::resource::DiskImpl* get_impl() const { return pimpl_; }
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;
54 Disk* set_read_bandwidth(double read_bw);
55 double get_read_bandwidth() const;
57 Disk* set_write_bandwidth(double write_bw);
58 double get_write_bandwidth() const;
61 * @brief Set limit for read/write operations.
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
67 * @param bw New bandwidth for the disk
69 Disk* set_readwrite_bandwidth(double bw);
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);
76 Disk* set_host(Host* host);
77 Host* get_host() const;
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);
83 * @brief Set the max amount of operations (either read or write) that can take place on this disk at the same time
85 * Use -1 to set no limit.
87 Disk* set_concurrency_limit(int limit);
88 int get_concurrency_limit() const;
90 IoPtr io_init(sg_size_t size, s4u::Io::OpType type) const;
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;
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;
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 };
105 * @brief Describes how the disk is shared between activities for each operation
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
112 * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
114 * @param op Operation type
115 * @param policy Sharing policy
116 * @param cb Callback for NONLINEAR policies
118 Disk* set_sharing_policy(Operation op, SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
119 SharingPolicy get_sharing_policy(Operation op) const;
121 * @brief Callback to set IO factors
123 * This callback offers a flexible way to create variability in I/O operations
125 * @param size I/O operation size in bytes
126 * @param op I/O operation type: read or write
127 * @return Multiply factor
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);
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)
145 on_onoff.connect(cb);
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)
150 on_this_onoff.connect(cb);
153 XBT_ATTRIB_DEPRECATED_v338("Please use on_onoff_cb() instead") static void on_state_change_cb(
154 const std::function<void(Disk const&)>& cb)
156 on_onoff.connect(cb);
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;
168 } // namespace simgrid
170 #endif /* INCLUDE_SIMGRID_S4U_DISK_HPP_ */