Logo AND Algorithmique Numérique Distribuée

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