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)
174 std::unordered_set<const char*> affected_links;
176 std::vector<StandardLinkImpl*> route;
177 src_host_->route_to(dst_host_, route, &lat);
178 latency = std::max(latency, lat);
180 for (auto const& link : route)
181 affected_links.insert(link->get_cname());
183 link_nb = affected_links.size();
186 XBT_DEBUG("Creating a stream io (%p) with %zu disk(s) and %zu unique link(s).", this, disk_nb, link_nb);
189 set_variable(model->get_maxmin_system()->variable_new(this, 1.0, -1.0, disk_nb + link_nb));
192 model->get_maxmin_system()->update_variable_penalty(get_variable(), 0.0);
194 /* 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
195 * communication either */
196 if (src_disk_ != nullptr)
197 model->get_maxmin_system()->expand(src_disk_->get_constraint(), get_variable(), size, true);
198 if (dst_disk_ != nullptr)
199 model->get_maxmin_system()->expand(dst_disk_->get_constraint(), get_variable(), size, true);
202 std::vector<StandardLinkImpl*> route;
203 src_host_->route_to(dst_host_, route, nullptr);
204 for (auto const& link : route)
205 model->get_maxmin_system()->expand(link->get_constraint(), this->get_variable(), size_);
208 if (link_nb + disk_nb == 0) {
210 this->set_remains(0.0);
213 /* finally calculate the initial bound value */
217 DiskAction* HostS22Model::io_stream(s4u::Host* src_host, DiskImpl* src_disk, s4u::Host* dst_host, DiskImpl* dst_disk,
220 return new S22Action(this, src_host, src_disk, dst_host, dst_disk, size);
223 Action* NetworkS22Model::communicate(s4u::Host* src, s4u::Host* dst, double size, double /*rate*/)
225 return hostModel_->io_stream(src, nullptr, dst, nullptr, size);
228 DiskImpl* DiskS22Model::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
230 return (new DiskS22(name, read_bandwidth, write_bandwidth))->set_model(this);
233 StandardLinkImpl* NetworkS22Model::create_link(const std::string& name, const std::vector<double>& bandwidths)
235 xbt_assert(bandwidths.size() == 1, "Non WIFI link must have only 1 bandwidth.");
236 auto link = new LinkS22(name, bandwidths[0], get_maxmin_system());
237 link->set_model(this);
241 StandardLinkImpl* NetworkS22Model::create_wifi_link(const std::string& name, const std::vector<double>& bandwidths)
249 DiskAction* DiskS22::io_start(sg_size_t size, s4u::Io::OpType type)
253 case s4u::Io::OpType::READ:
254 res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), this, get_host(), nullptr, size);
256 case s4u::Io::OpType::WRITE:
257 res = static_cast<DiskS22Model*>(get_model())->hostModel_->io_stream(get_host(), nullptr, get_host(), this, size);
266 void DiskS22::apply_event(kernel::profile::Event* triggered, double value)
268 /* Find out which of my iterators was triggered, and react accordingly */
269 if (triggered == get_read_event()) {
270 set_read_bandwidth(value);
272 } else if (triggered == get_write_event()) {
273 set_write_bandwidth(value);
275 } else if (triggered == get_state_event()) {
282 xbt_die("Unknown event!\n");
286 LinkS22::LinkS22(const std::string& name, double bandwidth, lmm::System* system) : StandardLinkImpl(name)
288 this->set_constraint(system->constraint_new(this, bandwidth));
289 bandwidth_.peak = bandwidth;
292 void LinkS22::apply_event(profile::Event* triggered, double value)
294 XBT_DEBUG("Updating link %s (%p) with value=%f", get_cname(), this, value);
295 if (triggered == bandwidth_.event) {
296 set_bandwidth(value);
297 tmgr_trace_event_unref(&bandwidth_.event);
299 } else if (triggered == latency_.event) {
301 tmgr_trace_event_unref(&latency_.event);
303 } else if (triggered == get_state_event()) {
310 xbt_die("Unknown event ! \n");
314 void LinkS22::set_bandwidth(double value)
316 bandwidth_.peak = value;
317 StandardLinkImpl::on_bandwidth_change();
319 get_model()->get_maxmin_system()->update_constraint_bound(get_constraint(), bandwidth_.peak * bandwidth_.scale);
322 void LinkS22::set_latency(double value)
324 latency_check(value);
325 const lmm::Element* elem = nullptr;
327 latency_.peak = value;
328 while (const auto* var = get_constraint()->get_variable(&elem)) {
329 const auto* action = static_cast<S22Action*>(var->get_id());
330 action->update_bound();
338 double S22Action::calculate_io_read_bound() const
340 double io_read_bound = std::numeric_limits<double>::max();
342 if (src_disk_ == nullptr)
343 return io_read_bound;
346 io_read_bound = std::min(io_read_bound, src_disk_->get_read_bandwidth() / size_);
348 return io_read_bound;
351 double S22Action::calculate_io_write_bound() const
353 double io_write_bound = std::numeric_limits<double>::max();
355 if (dst_disk_ == nullptr)
356 return io_write_bound;
359 io_write_bound = std::min(io_write_bound, dst_disk_->get_write_bandwidth() / size_);
361 return io_write_bound;
364 double S22Action::calculate_network_bound() const
367 double lat_bound = std::numeric_limits<double>::max();
369 if (src_host_ == dst_host_)
373 std::vector<StandardLinkImpl*> route;
374 src_host_->route_to(dst_host_, route, &lat);
378 lat_bound = NetworkModel::cfg_tcp_gamma / (2.0 * lat);
383 void S22Action::update_bound() const
385 double bound = std::min(std::min(calculate_io_read_bound(), calculate_io_write_bound()),
386 calculate_network_bound());
388 XBT_DEBUG("action (%p) : bound = %g", this, bound);
390 /* latency has been paid (or no latency), we can set the appropriate bound for network limit */
391 if ((bound < std::numeric_limits<double>::max()) && (latency_ <= 0.0))
392 get_model()->get_maxmin_system()->update_variable_bound(get_variable(), bound);
395 } // namespace simgrid::kernel::resource