1 /* Copyright (c) 2013-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 "src/kernel/resource/NetworkModelFactors.hpp"
7 #include "simgrid/sg_config.hpp"
8 #include "src/kernel/resource/FactorSet.hpp"
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
16 namespace simgrid::kernel::resource {
18 static FactorSet cfg_latency_factor("network/latency-factor");
19 static FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
21 config::Flag<std::string> cfg_latency_factor_str(
22 "network/latency-factor", std::initializer_list<const char*>{"smpi/lat-factor"},
23 "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
24 static config::Flag<std::string> cfg_bandwidth_factor_str(
25 "network/bandwidth-factor", std::initializer_list<const char*>{"smpi/bw-factor"},
26 "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
28 double NetworkModelFactors::get_latency_factor()
30 xbt_assert(not lat_factor_cb_,
31 "Cannot access the global latency factor since a callback is used. Please go for the advanced API.");
33 if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
34 cfg_latency_factor.parse(cfg_latency_factor_str.get());
36 return cfg_latency_factor(0);
39 double NetworkModelFactors::get_latency_factor(double size, const s4u::Host* src, const s4u::Host* dst,
40 const std::vector<s4u::Link*>& links,
41 const std::unordered_set<s4u::NetZone*>& netzones)
44 return lat_factor_cb_(size, src, dst, links, netzones);
46 if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
47 cfg_latency_factor.parse(cfg_latency_factor_str.get());
49 return cfg_latency_factor(size);
51 double NetworkModelFactors::get_bandwidth_factor()
53 xbt_assert(not bw_factor_cb_,
54 "Cannot access the global bandwidth factor since a callback is used. Please go for the advanced API.");
56 if (not cfg_bandwidth_factor.is_initialized())
57 cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
59 return cfg_bandwidth_factor(0);
62 double NetworkModelFactors::get_bandwidth_factor(double size, const s4u::Host* src, const s4u::Host* dst,
63 const std::vector<s4u::Link*>& links,
64 const std::unordered_set<s4u::NetZone*>& netzones)
67 return bw_factor_cb_(size, src, dst, links, netzones);
69 if (not cfg_bandwidth_factor.is_initialized())
70 cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
72 return cfg_bandwidth_factor(size);
76 void NetworkModelFactors::set_lat_factor_cb(const std::function<NetworkFactorCb>& cb)
79 throw std::invalid_argument("NetworkModelFactors: Invalid callback");
80 if (not simgrid::config::is_default("network/latency-factor"))
81 throw std::invalid_argument("You must choose between network/latency-factor and callback configuration.");
86 void NetworkModelFactors::set_bw_factor_cb(const std::function<NetworkFactorCb>& cb)
89 throw std::invalid_argument("NetworkModelFactors: Invalid callback");
90 if (not simgrid::config::is_default("network/bandwidth-factor"))
91 throw std::invalid_argument("You must choose between network/bandwidth-factor and callback configuration.");
96 } // namespace simgrid::kernel::resource