Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add user friendly wrapper to set priorities on I/Os
[simgrid.git] / include / simgrid / s4u / Disk.hpp
1 /* Copyright (c) 2019-2021. 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   IoPtr io_init(sg_size_t size, s4u::Io::OpType type) const;
84
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;
88
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;
92
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 };
96
97   /**
98    * @brief Describes how the disk is shared between activities for each operation
99    *
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
104    *
105    * Note that the NONLINEAR callback is in the critical path of the solver, so it should be fast.
106    *
107    * @param op Operation type
108    * @param policy Sharing policy
109    * @param cb Callback for NONLINEAR policies
110    */
111   Disk* set_sharing_policy(Operation op, SharingPolicy policy, const s4u::NonLinearResourceCb& cb = {});
112   SharingPolicy get_sharing_policy(Operation op) const;
113   /**
114    * @brief Callback to set IO factors
115    *
116    * This callback offers a flexible way to create variability in I/O operations
117    *
118    * @param size I/O operation size in bytes
119    * @param op I/O operation type: read or write
120    * @return Multiply factor
121    */
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);
125
126   Disk* seal();
127
128   /* The signals */
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;
135 };
136
137 } // namespace s4u
138 } // namespace simgrid
139
140 #endif /* INCLUDE_SIMGRID_S4U_DISK_HPP_ */