Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename Resource::on_state_change to Resource::on_onoff for sake of clarity
[simgrid.git] / src / kernel / resource / DiskImpl.cpp
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 #include "DiskImpl.hpp"
7
8 #include "simgrid/s4u/Engine.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/lmm/maxmin.hpp"
11 #include "src/kernel/resource/profile/Profile.hpp"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_disk, ker_resource, "Disk resources, that fuel I/O activities");
14 namespace simgrid::kernel::resource {
15
16 xbt::signal<void(DiskAction const&, Action::State, Action::State)> DiskAction::on_state_change;
17
18 /************
19  * Resource *
20  ************/
21 DiskImpl::DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth)
22     : Resource_T(name), piface_(this)
23 {
24   read_bw_.peak   = read_bandwidth;
25   read_bw_.scale  = 1.0;
26   write_bw_.peak  = write_bandwidth;
27   write_bw_.scale = 1.0;
28 }
29
30 DiskImpl* DiskImpl::set_host(s4u::Host* host)
31 {
32   xbt_assert(host, "Cannot set host, none given");
33   host_ = host;
34   return this;
35 }
36
37 DiskImpl* DiskImpl::set_read_constraint(lmm::Constraint* constraint_read)
38 {
39   constraint_read_ = constraint_read;
40   return this;
41 }
42
43 DiskImpl* DiskImpl::set_write_constraint(lmm::Constraint* constraint_write)
44 {
45   constraint_write_ = constraint_write;
46   return this;
47 }
48
49 /** @brief Fire the required callbacks and destroy the object
50  *
51  * Don't delete directly a Disk, call d->destroy() instead.
52  */
53 void DiskImpl::destroy()
54 {
55   s4u::Disk::on_destruction(piface_);
56   piface_.on_this_destruction(piface_);
57   delete this;
58 }
59
60 void DiskImpl::turn_on()
61 {
62   if (not is_on()) {
63     Resource::turn_on();
64     s4u::Disk::on_onoff(piface_);
65     piface_.on_this_onoff(piface_);
66   }
67 }
68 void DiskImpl::turn_off()
69 {
70   if (is_on()) {
71     Resource::turn_off();
72     s4u::Disk::on_onoff(piface_);
73     piface_.on_this_onoff(piface_);
74
75     const kernel::lmm::Element* elem = nullptr;
76     double now                       = EngineImpl::get_clock();
77     while (const auto* var = get_constraint()->get_variable(&elem)) {
78       Action* action = var->get_id();
79       if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
80         action->set_finish_time(now);
81         action->set_state(Action::State::FAILED);
82       }
83     }
84   }
85 }
86
87 DiskImpl* DiskImpl::set_read_bandwidth_profile(profile::Profile* profile)
88 {
89   if (profile) {
90     xbt_assert(read_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
91     read_bw_.event = profile->schedule(&profile::future_evt_set, this);
92   }
93   return this;
94 }
95
96 DiskImpl* DiskImpl::set_write_bandwidth_profile(profile::Profile* profile)
97 {
98   if (profile) {
99     xbt_assert(write_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
100     write_bw_.event = profile->schedule(&profile::future_evt_set, this);
101   }
102   return this;
103 }
104
105 void DiskImpl::seal()
106 {
107   if (is_sealed())
108     return;
109
110   xbt_assert(this->get_model(), "Cannot seal Disk (%s) without setting the model first", get_cname());
111   lmm::System* maxmin_system = get_model()->get_maxmin_system();
112   /* set readwrite constraint if not configured by user */
113   if (readwrite_bw_ == -1) {
114     readwrite_bw_ = std::max(read_bw_.peak, write_bw_.peak);
115   }
116   this->set_read_constraint(maxmin_system->constraint_new(this, read_bw_.peak * read_bw_.scale))
117       ->set_write_constraint(maxmin_system->constraint_new(this, write_bw_.peak * write_bw_.scale))
118       ->set_constraint(maxmin_system->constraint_new(this, readwrite_bw_));
119   apply_sharing_policy_cfg();
120   XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw_.peak, write_bw_.peak);
121   Resource::seal();
122   turn_on();
123 }
124
125 constexpr kernel::lmm::Constraint::SharingPolicy to_maxmin_policy(s4u::Disk::SharingPolicy policy)
126 {
127   kernel::lmm::Constraint::SharingPolicy lmm_policy = kernel::lmm::Constraint::SharingPolicy::SHARED;
128   if (policy == s4u::Disk::SharingPolicy::NONLINEAR)
129     lmm_policy = kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
130   return lmm_policy;
131 }
132
133 void DiskImpl::set_read_bandwidth(double value)
134 {
135   read_bw_.peak = value;
136   if (constraint_read_)
137     get_model()->get_maxmin_system()->update_constraint_bound(constraint_read_, read_bw_.peak * read_bw_.scale);
138 }
139
140 void DiskImpl::set_write_bandwidth(double value)
141 {
142   write_bw_.peak = value;
143   if (constraint_write_) {
144     get_model()->get_maxmin_system()->update_constraint_bound(constraint_write_, write_bw_.peak* write_bw_.scale);
145   }
146 }
147
148 void DiskImpl::set_readwrite_bandwidth(double value)
149 {
150    readwrite_bw_ = value;
151    if (get_constraint()) {
152     get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), readwrite_bw_);
153   }
154 }
155
156 void DiskImpl::set_sharing_policy(s4u::Disk::Operation op, s4u::Disk::SharingPolicy policy,
157                                   const s4u::NonLinearResourceCb& cb)
158 {
159   sharing_policy_[op]    = policy;
160   sharing_policy_cb_[op] = cb;
161   apply_sharing_policy_cfg();
162 }
163
164 s4u::Disk::SharingPolicy DiskImpl::get_sharing_policy(s4u::Disk::Operation op) const
165 {
166   return sharing_policy_.at(op);
167 }
168
169 void DiskImpl::apply_sharing_policy_cfg()
170 {
171   if (get_constraint())
172     get_constraint()->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READWRITE]),
173                                          sharing_policy_cb_[s4u::Disk::Operation::READWRITE]);
174   if (constraint_read_)
175     constraint_read_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READ]),
176                                          sharing_policy_cb_[s4u::Disk::Operation::READ]);
177   if (constraint_write_)
178     constraint_write_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::WRITE]),
179                                           sharing_policy_cb_[s4u::Disk::Operation::WRITE]);
180 }
181
182 void DiskImpl::set_factor_cb(const std::function<s4u::Disk::IoFactorCb>& cb)
183 {
184   xbt_assert(not is_sealed(), "Cannot set I/O factor callback in an already sealed disk(%s)", get_cname());
185   factor_cb_ = cb;
186 }
187
188 /**********
189  * Action *
190  **********/
191 void DiskAction::set_state(Action::State new_state)
192 {
193   Action::State previous_state = get_state();
194   if (new_state != previous_state) { // Trigger only if the state changed
195     Action::set_state(new_state);
196     on_state_change(*this, previous_state, new_state);
197   }
198 }
199
200 void DiskAction::update_remains_lazy(double /*now*/)
201 {
202   THROW_IMPOSSIBLE;
203 }
204 } // namespace simgrid::kernel::resource