1 /* Copyright (c) 2022. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <xbt/config.hpp>
10 #include "simgrid/config.h"
11 #include "src/kernel/EngineImpl.hpp"
12 #if SIMGRID_HAVE_EIGEN3
13 #include "src/kernel/lmm/bmf.hpp"
15 #include "src/kernel/resource/profile/Event.hpp"
16 #include "src/surf/sio_S22.hpp"
18 #include <unordered_set>
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
21 XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
26 static simgrid::config::Flag<std::string> cfg_sio_solver("host/sio_solver",
27 "Set linear equations solver used by sio model",
29 &simgrid::kernel::lmm::System::validate_solver);
31 /**************************************/
32 /*** Resource Creation & Destruction **/
33 /**************************************/
34 void surf_host_model_init_sio_S22()
36 XBT_CINFO(xbt_cfg, "Switching to the S22 model to handle streaming I/Os.");
37 xbt_assert(cfg_sio_solver != "maxmin", "Invalid configuration. Cannot use maxmin solver with streaming I/Os.");
39 auto* system = simgrid::kernel::lmm::System::build(cfg_sio_solver.get(), true /* selective update */);
40 auto host_model = std::make_shared<simgrid::kernel::resource::HostS22Model>("Host_Sio", system);
41 auto* engine = simgrid::kernel::EngineImpl::get_instance();
42 engine->add_model(host_model);
43 engine->get_netzone_root()->set_host_model(host_model);
46 namespace simgrid::kernel::resource {
48 HostS22Model::HostS22Model(const std::string& name, lmm::System* sys) : HostModel(name)
50 set_maxmin_system(sys);
52 auto net_model = std::make_shared<NetworkS22Model>("Network_Sio", this, sys);
53 auto engine = EngineImpl::get_instance();
54 engine->add_model(net_model);
55 engine->get_netzone_root()->set_network_model(net_model);
57 auto disk_model = std::make_shared<DiskS22Model>("Disk_Sio", this, sys);
58 engine->add_model(disk_model);
59 engine->get_netzone_root()->set_disk_model(disk_model);
61 surf_cpu_model_init_Cas01();
64 DiskS22Model::DiskS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
65 : DiskModel(name), hostModel_(hmodel)
67 set_maxmin_system(sys);
70 DiskS22Model::~DiskS22Model()
72 set_maxmin_system(nullptr);
75 NetworkS22Model::NetworkS22Model(const std::string& name, HostS22Model* hmodel, lmm::System* sys)
76 : NetworkModel(name), hostModel_(hmodel)
78 set_maxmin_system(sys);
79 loopback_.reset(create_link("__loopback__", {simgrid::config::get_value<double>("network/loopback-bw")}));
80 loopback_->set_sharing_policy(s4u::Link::SharingPolicy::FATPIPE, {});
81 loopback_->set_latency(simgrid::config::get_value<double>("network/loopback-lat"));
82 loopback_->get_iface()->seal();
85 NetworkS22Model::~NetworkS22Model()
87 set_maxmin_system(nullptr);
90 double HostS22Model::next_occurring_event(double now)
92 double min = HostModel::next_occurring_event_full(now);
93 for (Action const& action : *get_started_action_set()) {
94 const auto& net_action = static_cast<const S22Action&>(action);
95 if (net_action.get_latency() > 0 && (min < 0 || net_action.get_latency() < min)) {
96 min = net_action.get_latency();
97 XBT_DEBUG("Updating min with %p (start %f): %f", &net_action, net_action.get_start_time(), min);
100 XBT_DEBUG("min value: %f", min);
105 void HostS22Model::update_actions_state(double /*now*/, double delta)
107 for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) {
108 auto& action = static_cast<S22Action&>(*it);
109 ++it; // increment iterator here since the following calls to action.finish() may invalidate it
110 if (action.get_latency() > 0) {
111 if (action.get_latency() > delta) {
112 action.update_latency(delta, sg_surf_precision);
114 action.set_latency(0.0);
116 if ((action.get_latency() <= 0.0) && (action.is_suspended() == 0)) {
117 action.update_bound();
118 get_maxmin_system()->update_variable_penalty(action.get_variable(), 1.0);
119 action.set_last_update();
122 XBT_DEBUG("Action (%p) : remains (%g) updated by %g.", &action, action.get_remains(), action.get_rate() * delta);
123 action.update_remains(action.get_rate() * delta);
124 action.update_max_duration(delta);
126 XBT_DEBUG("Action (%p) : remains (%g).", &action, action.get_remains());
128 /* In the next if cascade, the action can be finished either because:
129 * - The amount of remaining work reached 0
130 * - The max duration was reached
131 * If it's not done, it may have failed.
134 if (((action.get_remains() <= 0) && (action.get_variable()->get_penalty() > 0)) ||
135 ((action.get_max_duration() != NO_MAX_DURATION) && (action.get_max_duration() <= 0))) {
136 action.finish(Action::State::FINISHED);
140 /* Need to check that none of the model has failed */
142 const lmm::Constraint* cnst = action.get_variable()->get_constraint(i);
143 while (cnst != nullptr) {
145 if (not cnst->get_id()->is_on()) {
146 XBT_DEBUG("Action (%p) Failed!!", &action);
147 action.finish(Action::State::FAILED);
150 cnst = action.get_variable()->get_constraint(i);
155 S22Action::S22Action(Model* model, s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk, double size)
156 : DiskAction(model, 1, false)
157 , src_host_(src_host)
158 , src_disk_(src_disk)
159 , dst_host_(dst_host)
160 , dst_disk_(dst_disk)
165 double latency = 0.0;
166 this->set_last_update();
168 if (src_disk_ != nullptr)
170 if (dst_disk_ != nullptr)
173 /* there should always be a route between src_host and dst_host (loopback_ for self communication at least) */
175 std::vector<StandardLinkImpl*> route;
176 src_host_->route_to(dst_host_, route, &lat);
177 latency = std::max(latency, lat);
178 link_nb = route.size();
180 XBT_DEBUG("Creating a stream io (%p) with %zu disk(s) and %zu unique link(s).", this, disk_nb, link_nb);
183 set_variable(model->get_maxmin_system()->variable_new(this, 1.0, -1.0, disk_nb + link_nb));
186 model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
188 /* Expand it for the disks even if there is nothing to read/write, to make sure that it gets expended even if there is no
189 * communication either */
190 if (src_disk_ != nullptr)
191 model->get_maxmin_system()->expand(src_disk_->get_constraint(), get_variable(), size, true);
192 if (dst_disk_ != nullptr)
193 model->get_maxmin_system()->expand(dst_disk_->get_constraint(), get_variable(), size, true);
196 std::vector<StandardLinkImpl*> route;
197 src_host_->route_to(dst_host_, route, nullptr);
198 for (auto const& link : route)
199 model->get_maxmin_system()->expand(link->get_constraint(), this->get_variable(), size_);
202 if (link_nb + disk_nb == 0) {
204 this->set_remains(0.0);
207 /* finally calculate the initial bound value */
211 DiskAction* HostS22Model::io_stream(s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk,
214 return new S22Action(this, src_host, src_disk, dst_host, dst_disk, size);
217 Action* NetworkS22Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
219 return hostModel_->io_stream(src, nullptr, dst, nullptr, size);
222 DiskImpl* DiskS22Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
224 return (new DiskS22(name, read_bandwidth, write_bandwidth))->set_model(this);
227 StandardLinkImpl* NetworkS22Model::create_link(const std::string& name, const std::vector<double>& bandwidths)
229 xbt_assert(bandwidths.size() == 1, "Non WIFI link must have only 1 bandwidth.");
230 auto link = new LinkS22(name, bandwidths[0], get_maxmin_system());
231 link->set_model(this);
235 StandardLinkImpl* NetworkS22Model::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
243 DiskAction* DiskS22::io_start(sg_size_t size, s4u::Io::OpType type)
247 case s4u::Io::OpType::READ:
248 res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), this, get_host(), nullptr, size);
250 case s4u::Io::OpType::WRITE:
251 res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), nullptr, get_host(), this, size);
260 void DiskS22::apply_event(kernel::profile::Event* triggered, double value)
262 /* Find out which of my iterators was triggered, and react accordingly */
263 if (triggered == get_read_event()) {
264 set_read_bandwidth(value);
266 } else if (triggered == get_write_event()) {
267 set_write_bandwidth(value);
269 } else if (triggered == get_state_event()) {
276 xbt_die("Unknown event!\n");
280 LinkS22::LinkS22(const std::string& name, double bandwidth, lmm::System* system) : StandardLinkImpl(name)
282 this->set_constraint(system->constraint_new(this, bandwidth));
283 bandwidth_.peak = bandwidth;
286 void LinkS22::apply_event(profile::Event* triggered, double value)
288 XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
289 if (triggered == bandwidth_.event) {
290 set_bandwidth(value);
291 tmgr_trace_event_unref(&bandwidth_.event);
293 } else if (triggered == latency_.event) {
295 tmgr_trace_event_unref(&latency_.event);
297 } else if (triggered == get_state_event()) {
304 xbt_die("Unknown event ! \n");
308 void LinkS22::set_bandwidth(double value)
310 bandwidth_.peak = value;
311 StandardLinkImpl::on_bandwidth_change();
313 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
316 void LinkS22::set_latency(double value)
318 latency_check(value);
319 const lmm::Element* elem = nullptr;
321 latency_.peak = value;
322 while (const auto* var = get_constraint()->get_variable(&elem)) {
323 const auto* action = static_cast<S22Action*>(var->get_id());
324 action->update_bound();
332 double S22Action::calculate_io_read_bound() const
334 double io_read_bound = std::numeric_limits<double>::max();
336 if (src_disk_ == nullptr)
337 return io_read_bound;
340 io_read_bound = std::min(io_read_bound, src_disk_->get_read_bandwidth() / size_);
342 return io_read_bound;
345 double S22Action::calculate_io_write_bound() const
347 double io_write_bound = std::numeric_limits<double>::max();
349 if (dst_disk_ == nullptr)
350 return io_write_bound;
353 io_write_bound = std::min(io_write_bound, dst_disk_->get_write_bandwidth() / size_);
355 return io_write_bound;
358 double S22Action::calculate_network_bound() const
361 double lat_bound = std::numeric_limits<double>::max();
363 if (src_host_ == dst_host_)
367 std::vector<StandardLinkImpl*> route;
368 src_host_->route_to(dst_host_, route, &lat);
372 lat_bound = NetworkModel::cfg_tcp_gamma / (2.0 * lat);
377 void S22Action::update_bound() const
379 double bound = std::min(std::min(calculate_io_read_bound(), calculate_io_write_bound()),
380 calculate_network_bound());
382 XBT_DEBUG("action (%p) : bound = %g", this, bound);
384 /* latency has been paid (or no latency), we can set the appropriate bound for network limit */
385 if ((bound < std::numeric_limits<double>::max()) && (latency_ <= 0.0))
386 get_model()->get_maxmin_system()->update_variable_bound(get_variable(), bound);
389 } // namespace simgrid::kernel::resource