Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More consistency in resource creation/destruction
[simgrid.git] / src / s4u / s4u_Disk.cpp
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 #include "simgrid/s4u/Disk.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "simgrid/s4u/Io.hpp"
10 #include "simgrid/simix.hpp"
11 #include "src/kernel/resource/DiskImpl.hpp"
12
13 namespace simgrid {
14
15 template class xbt::Extendable<s4u::Disk>;
16
17 namespace s4u {
18
19 xbt::signal<void(Disk&)> Disk::on_creation;
20 xbt::signal<void(Disk const&)> Disk::on_destruction;
21 xbt::signal<void(Disk const&)> Disk::on_state_change;
22
23 const std::string& Disk::get_name() const
24 {
25   return pimpl_->get_name();
26 }
27
28 const char* Disk::get_cname() const
29 {
30   return pimpl_->get_cname();
31 }
32
33 Disk* Disk::set_read_bandwidth(double read_bw)
34 {
35   kernel::actor::simcall([this, read_bw] { pimpl_->set_read_bandwidth(read_bw); });
36   return this;
37 }
38
39 Disk* Disk::set_write_bandwidth(double write_bw)
40 {
41   kernel::actor::simcall([this, write_bw] { pimpl_->set_write_bandwidth(write_bw); });
42   return this;
43 }
44
45 double Disk::get_read_bandwidth() const
46 {
47   return pimpl_->get_read_bandwidth();
48 }
49
50 Disk* Disk::set_readwrite_bandwidth(double bw)
51 {
52   kernel::actor::simcall([this, bw] { pimpl_->set_readwrite_bandwidth(bw); });
53   return this;
54 }
55
56 double Disk::get_write_bandwidth() const
57 {
58   return pimpl_->get_write_bandwidth();
59 }
60
61 Disk* Disk::set_host(Host* host)
62 {
63   pimpl_->set_host(host);
64   return this;
65 }
66
67 Host* Disk::get_host() const
68 {
69   return pimpl_->get_host();
70 }
71
72 const std::unordered_map<std::string, std::string>* Disk::get_properties() const
73 {
74   return pimpl_->get_properties();
75 }
76
77 const char* Disk::get_property(const std::string& key) const
78 {
79   return pimpl_->get_property(key);
80 }
81
82 Disk* Disk::set_property(const std::string& key, const std::string& value)
83 {
84   kernel::actor::simcall([this, &key, &value] { this->pimpl_->set_property(key, value); });
85   return this;
86 }
87
88 Disk* Disk::set_properties(const std::unordered_map<std::string, std::string>& properties)
89 {
90   kernel::actor::simcall([this, properties] { this->pimpl_->set_properties(properties); });
91   return this;
92 }
93
94 Disk* Disk::set_state_profile(kernel::profile::Profile* profile)
95 {
96   xbt_assert(not pimpl_->is_sealed(), "Cannot set a state profile once the Disk is sealed");
97   kernel::actor::simcall([this, profile]() { this->pimpl_->set_state_profile(profile); });
98   return this;
99 }
100
101 Disk* Disk::set_read_bandwidth_profile(kernel::profile::Profile* profile)
102 {
103   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Disk is sealed");
104   kernel::actor::simcall([this, profile]() { this->pimpl_->set_read_bandwidth_profile(profile); });
105   return this;
106 }
107
108 Disk* Disk::set_write_bandwidth_profile(kernel::profile::Profile* profile)
109 {
110   xbt_assert(not pimpl_->is_sealed(), "Cannot set a bandwidth profile once the Disk is sealed");
111   kernel::actor::simcall([this, profile]() { this->pimpl_->set_write_bandwidth_profile(profile); });
112   return this;
113 }
114
115 IoPtr Disk::io_init(sg_size_t size, Io::OpType type) const
116 {
117   return Io::init()->set_disk(this)->set_size(size)->set_op_type(type);
118 }
119
120 IoPtr Disk::read_async(sg_size_t size) const
121 {
122   return IoPtr(io_init(size, Io::OpType::READ))->vetoable_start();
123 }
124
125 sg_size_t Disk::read(sg_size_t size) const
126 {
127   return IoPtr(io_init(size, Io::OpType::READ))->vetoable_start()->wait()->get_performed_ioops();
128 }
129
130 IoPtr Disk::write_async(sg_size_t size) const
131 {
132   return IoPtr(io_init(size, Io::OpType::WRITE)->vetoable_start());
133 }
134
135 sg_size_t Disk::write(sg_size_t size) const
136 {
137   return IoPtr(io_init(size, Io::OpType::WRITE))->vetoable_start()->wait()->get_performed_ioops();
138 }
139
140 Disk* Disk::set_sharing_policy(Disk::Operation op, Disk::SharingPolicy policy, const NonLinearResourceCb& cb)
141 {
142   kernel::actor::simcall([this, op, policy, &cb] { pimpl_->set_sharing_policy(op, policy, cb); });
143   return this;
144 }
145
146 Disk::SharingPolicy Disk::get_sharing_policy(Operation op) const
147 {
148   return this->pimpl_->get_sharing_policy(op);
149 }
150
151 Disk* Disk::set_factor_cb(const std::function<IoFactorCb>& cb)
152 {
153   kernel::actor::simcall([this, &cb] { pimpl_->set_factor_cb(cb); });
154   return this;
155 }
156
157 Disk* Disk::seal()
158 {
159   kernel::actor::simcall([this]{ pimpl_->seal(); });
160   Disk::on_creation(*this); // notify the signal
161   return this;
162 }
163 } // namespace s4u
164 } // namespace simgrid
165
166 /* **************************** Public C interface *************************** */
167
168 const char* sg_disk_get_name(const_sg_disk_t disk)
169 {
170   return disk->get_cname();
171 }
172
173 sg_host_t sg_disk_get_host(const_sg_disk_t disk)
174 {
175   return disk->get_host();
176 }
177
178 double sg_disk_read_bandwidth(const_sg_disk_t disk)
179 {
180   return disk->get_read_bandwidth();
181 }
182
183 double sg_disk_write_bandwidth(const_sg_disk_t disk)
184 {
185   return disk->get_write_bandwidth();
186 }
187
188 sg_size_t sg_disk_read(const_sg_disk_t disk, sg_size_t size)
189 {
190   return disk->read(size);
191 }
192 sg_size_t sg_disk_write(const_sg_disk_t disk, sg_size_t size)
193 {
194   return disk->write(size);
195 }
196
197 void* sg_disk_get_data(const_sg_disk_t disk)
198 {
199   return disk->get_data();
200 }
201
202 void sg_disk_set_data(sg_disk_t disk, void* data)
203 {
204   disk->set_data(data);
205 }