Logo AND Algorithmique Numérique Distribuée

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