Logo AND Algorithmique Numérique Distribuée

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