Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Hide cfg_latency_factor and bw, and change them into smpi::utils::FactorSet
[simgrid.git] / src / kernel / resource / NetworkModel.cpp
1 /* Copyright (c) 2013-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 <simgrid/s4u/Engine.hpp>
7
8 #include "simgrid/sg_config.hpp"
9 #include "src/kernel/resource/NetworkModel.hpp"
10 #include "src/kernel/resource/profile/Profile.hpp"
11 #include "src/smpi/include/smpi_utils.hpp"
12 #include "src/surf/surf_interface.hpp"
13
14 #include <numeric>
15
16 #ifndef NETWORK_INTERFACE_CPP_
17 #define NETWORK_INTERFACE_CPP_
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, that fuel communications");
20
21 /*********
22  * Model *
23  *********/
24
25 namespace simgrid::kernel::resource {
26 static smpi::utils::FactorSet cfg_latency_factor("network/latency-factor");
27 static smpi::utils::FactorSet cfg_bandwidth_factor("network/bandwidth-factor");
28
29 config::Flag<std::string> cfg_latency_factor_str(
30     "network/latency-factor",
31     "Correction factor to apply to the provided latency (default value overridden by network model)", "1.0");
32 static config::Flag<std::string> cfg_bandwidth_factor_str(
33     "network/bandwidth-factor",
34     "Correction factor to apply to the provided bandwidth (default value overridden by network model)", "1.0");
35
36 double NetworkModel::get_latency_factor(double size)
37 {
38   if (not cfg_latency_factor.is_initialized()) // lazy initiaization to avoid initialization fiasco
39     cfg_latency_factor.parse(cfg_latency_factor_str.get());
40
41   return cfg_latency_factor(size);
42 }
43 double NetworkModel::get_bandwidth_factor(double size)
44 {
45   if (not cfg_bandwidth_factor.is_initialized())
46     cfg_bandwidth_factor.parse(cfg_bandwidth_factor_str.get());
47
48   return cfg_bandwidth_factor(size);
49 }
50
51 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
52 config::Flag<double> NetworkModel::cfg_tcp_gamma(
53     "network/TCP-gamma",
54     "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
55     "Use the last given value, which is the max window size)",
56     4194304.0);
57
58 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
59 config::Flag<bool> NetworkModel::cfg_crosstraffic(
60     "network/crosstraffic",
61     "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
62
63 config::Flag<double> NetworkModel::cfg_weight_S_parameter(
64     "network/weight-S",
65     "Correction factor to apply to the weight of competing streams (default value set by network model)", 0.0);
66
67 NetworkModel::~NetworkModel() = default;
68
69 double NetworkModel::next_occurring_event_full(double now)
70 {
71   double minRes = Model::next_occurring_event_full(now);
72
73   for (Action const& action : *get_started_action_set()) {
74     const auto& net_action = static_cast<const NetworkAction&>(action);
75     if (net_action.latency_ > 0)
76       minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
77   }
78
79   XBT_DEBUG("Min of share resources %f", minRes);
80
81   return minRes;
82 }
83
84 /**********
85  * Action *
86  **********/
87
88 void NetworkAction::set_state(Action::State state)
89 {
90   Action::State previous = get_state();
91   if (previous != state) { // Trigger only if the state changed
92     Action::set_state(state);
93     s4u::Link::on_communication_state_change(*this, previous);
94   }
95 }
96
97 /** @brief returns a list of all Links that this action is using */
98 std::list<StandardLinkImpl*> NetworkAction::get_links() const
99 {
100   std::list<StandardLinkImpl*> retlist;
101   int llen = get_variable()->get_number_of_constraint();
102
103   for (int i = 0; i < llen; i++) {
104     /* Beware of composite actions: ptasks put links and cpus together */
105     if (auto* link = dynamic_cast<StandardLinkImpl*>(get_variable()->get_constraint(i)->get_id()))
106       retlist.push_back(link);
107   }
108
109   return retlist;
110 }
111
112 static void add_latency(const std::vector<StandardLinkImpl*>& links, double* latency)
113 {
114   if (latency)
115     *latency = std::accumulate(begin(links), end(links), *latency,
116                                [](double lat, const auto* link) { return lat + link->get_latency(); });
117 }
118
119 void add_link_latency(std::vector<StandardLinkImpl*>& result, StandardLinkImpl* link, double* latency)
120 {
121   result.push_back(link);
122   if (latency)
123     *latency += link->get_latency();
124 }
125
126 void add_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
127                       double* latency)
128 {
129   result.insert(result.end(), begin(links), end(links));
130   add_latency(links, latency);
131 }
132
133 void insert_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
134                          double* latency)
135 {
136   result.insert(result.begin(), rbegin(links), rend(links));
137   add_latency(links, latency);
138 }
139
140 } // namespace simgrid::kernel::resource
141
142 #endif /* NETWORK_INTERFACE_CPP_ */