1 /* Copyright (c) 2019-2021. 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 IoPtr io_init(sg_size_t size, s4u::Io::OpType type) const;
85 IoPtr read_async(sg_size_t size) const;
86 sg_size_t read(sg_size_t size) const;
87 sg_size_t read(sg_size_t size, double priority) const;
89 IoPtr write_async(sg_size_t size) const;
90 sg_size_t write(sg_size_t size) const;
91 sg_size_t write(sg_size_t size, double priority) const;
93 /** @brief Policy for sharing the disk among activities */
94 enum class SharingPolicy { NONLINEAR = 1, LINEAR = 0 };
95 enum class Operation { READ = 2, WRITE = 1, READWRITE = 0 };
98 * @brief Describes how the disk is shared between activities for each operation
100 * Disks have different bandwidths for read and write operations, that can have different policies:
101 * - Read: resource sharing for read operation
102 * - Write: resource sharing for write
103 * - ReadWrite: global sharing for read and write operations
105 * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
107 * @param op Operation type
108 * @param policy Sharing policy
109 * @param cb Callback for NONLINEAR policies
111 Disk* set_sharing_policy(Operation op, SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
112 SharingPolicy get_sharing_policy(Operation op) const;
114 * @brief Callback to set IO factors
116 * This callback offers a flexible way to create variability in I/O operations
118 * @param size I/O operation size in bytes
119 * @param op I/O operation type: read or write
120 * @return Multiply factor
122 using IoFactorCb = double(sg_size_t size, Io::OpType op);
123 /** @brief Configure the factor callback */
124 Disk* set_factor_cb(const std::function<IoFactorCb>& cb);
129 /** @brief Callback signal fired when a new Disk is created */
130 static xbt::signal<void(Disk&)> on_creation;
131 /** @brief Callback signal fired when a Disk is destroyed */
132 static xbt::signal<void(Disk const&)> on_destruction;
133 /** @brief Callback signal fired when a Disk's state changes */
134 static xbt::signal<void(Disk const&)> on_state_change;
138 } // namespace simgrid
140 #endif /* INCLUDE_SIMGRID_S4U_DISK_HPP_ */