Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / kernel / resource / DiskImpl.cpp
1 /* Copyright (c) 2019-2022. 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
15 namespace simgrid {
16 namespace kernel {
17 namespace resource {
18
19 xbt::signal<void(DiskAction const&, Action::State, Action::State)> DiskAction::on_state_change;
20
21 /*********
22  * Model *
23  *********/
24
25 DiskModel::DiskModel(const std::string& name) : Model(name)
26 {
27   set_maxmin_system(new lmm::System(true /* selective update */));
28 }
29
30 /************
31  * Resource *
32  ************/
33 DiskImpl::DiskImpl(const std::string& name, double read_bandwidth, double write_bandwidth)
34     : Resource_T(name), piface_(this)
35 {
36   read_bw_.peak   = read_bandwidth;
37   read_bw_.scale  = 1.0;
38   write_bw_.peak  = write_bandwidth;
39   write_bw_.scale = 1.0;
40 }
41
42 DiskImpl* DiskImpl::set_host(s4u::Host* host)
43 {
44   xbt_assert(host, "Cannot set host, none given");
45   host_ = host;
46   return this;
47 }
48
49 DiskImpl* DiskImpl::set_read_constraint(lmm::Constraint* constraint_read)
50 {
51   constraint_read_ = constraint_read;
52   return this;
53 }
54
55 DiskImpl* DiskImpl::set_write_constraint(lmm::Constraint* constraint_write)
56 {
57   constraint_write_ = constraint_write;
58   return this;
59 }
60
61 /** @brief Fire the required callbacks and destroy the object
62  *
63  * Don't delete directly a Disk, call d->destroy() instead.
64  */
65 void DiskImpl::destroy()
66 {
67   s4u::Disk::on_destruction(piface_);
68   delete this;
69 }
70
71 void DiskImpl::turn_on()
72 {
73   if (not is_on()) {
74     Resource::turn_on();
75     s4u::Disk::on_state_change(piface_);
76   }
77 }
78 void DiskImpl::turn_off()
79 {
80   if (is_on()) {
81     Resource::turn_off();
82     s4u::Disk::on_state_change(piface_);
83   }
84 }
85
86 DiskImpl* DiskImpl::set_read_bandwidth_profile(profile::Profile* profile)
87 {
88   if (profile) {
89     xbt_assert(read_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
90     read_bw_.event = profile->schedule(&profile::future_evt_set, this);
91   }
92   return this;
93 }
94
95 DiskImpl* DiskImpl::set_write_bandwidth_profile(profile::Profile* profile)
96 {
97   if (profile) {
98     xbt_assert(write_bw_.event == nullptr, "Cannot set a second read bandwidth profile to Disk %s", get_cname());
99     write_bw_.event = profile->schedule(&profile::future_evt_set, this);
100   }
101   return this;
102 }
103
104 void DiskImpl::seal()
105 {
106   if (is_sealed())
107     return;
108
109   xbt_assert(this->get_model(), "Cannot seal Disk (%s) without setting the model first", get_cname());
110   lmm::System* maxmin_system = get_model()->get_maxmin_system();
111   /* set readwrite constraint if not configured by user */
112   if (readwrite_bw_ == -1) {
113     readwrite_bw_ = std::max(read_bw_.peak, write_bw_.peak);
114   }
115   this->set_read_constraint(maxmin_system->constraint_new(this, read_bw_.peak * read_bw_.scale))
116       ->set_write_constraint(maxmin_system->constraint_new(this, write_bw_.peak * write_bw_.scale))
117       ->set_constraint(maxmin_system->constraint_new(this, readwrite_bw_));
118   apply_sharing_policy_cfg();
119   XBT_DEBUG("Create resource with read_bw '%f' write_bw '%f'", read_bw_.peak, write_bw_.peak);
120   Resource::seal();
121   turn_on();
122 }
123
124 constexpr kernel::lmm::Constraint::SharingPolicy to_maxmin_policy(s4u::Disk::SharingPolicy policy)
125 {
126   kernel::lmm::Constraint::SharingPolicy lmm_policy = kernel::lmm::Constraint::SharingPolicy::SHARED;
127   if (policy == s4u::Disk::SharingPolicy::NONLINEAR)
128     lmm_policy = kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
129   return lmm_policy;
130 }
131
132 void DiskImpl::set_sharing_policy(s4u::Disk::Operation op, s4u::Disk::SharingPolicy policy,
133                                   const s4u::NonLinearResourceCb& cb)
134 {
135   sharing_policy_[op]    = policy;
136   sharing_policy_cb_[op] = cb;
137   apply_sharing_policy_cfg();
138 }
139
140 s4u::Disk::SharingPolicy DiskImpl::get_sharing_policy(s4u::Disk::Operation op) const
141 {
142   return sharing_policy_.at(op);
143 }
144
145 void DiskImpl::apply_sharing_policy_cfg()
146 {
147   if (get_constraint())
148     get_constraint()->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READWRITE]),
149                                          sharing_policy_cb_[s4u::Disk::Operation::READWRITE]);
150   if (constraint_read_)
151     constraint_read_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::READ]),
152                                          sharing_policy_cb_[s4u::Disk::Operation::READ]);
153   if (constraint_write_)
154     constraint_write_->set_sharing_policy(to_maxmin_policy(sharing_policy_[s4u::Disk::Operation::WRITE]),
155                                           sharing_policy_cb_[s4u::Disk::Operation::WRITE]);
156 }
157
158 void DiskImpl::set_factor_cb(const std::function<s4u::Disk::IoFactorCb>& cb)
159 {
160   xbt_assert(not is_sealed(), "Cannot set I/O factor callback in an already sealed disk(%s)", get_cname());
161   factor_cb_ = cb;
162 }
163
164 /**********
165  * Action *
166  **********/
167 void DiskAction::set_state(Action::State new_state)
168 {
169   Action::State previous_state = get_state();
170   if (new_state != previous_state) { // Trigger only if the state changed
171     Action::set_state(new_state);
172     on_state_change(*this, previous_state, new_state);
173   }
174 }
175 } // namespace resource
176 } // namespace kernel
177 } // namespace simgrid