Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Concatenate nested namespaces (sonar).
[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/surf/surf_interface.hpp"
12
13 #include <numeric>
14
15 #ifndef NETWORK_INTERFACE_CPP_
16 #define NETWORK_INTERFACE_CPP_
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(res_network, ker_resource, "Network resources, that fuel communications");
19
20 /*********
21  * Model *
22  *********/
23
24 namespace simgrid::kernel::resource {
25
26 /** @brief Command-line option 'network/TCP-gamma' -- see @ref options_model_network_gamma */
27 config::Flag<double> NetworkModel::cfg_tcp_gamma(
28     "network/TCP-gamma",
29     "Size of the biggest TCP window (cat /proc/sys/net/ipv4/tcp_[rw]mem for recv/send window; "
30     "Use the last given value, which is the max window size)",
31     4194304.0);
32
33 /** @brief Command-line option 'network/crosstraffic' -- see @ref options_model_network_crosstraffic */
34 config::Flag<bool> NetworkModel::cfg_crosstraffic(
35     "network/crosstraffic",
36     "Activate the interferences between uploads and downloads for fluid max-min models (LV08, CM02)", "yes");
37
38 NetworkModel::~NetworkModel() = default;
39
40 double NetworkModel::next_occurring_event_full(double now)
41 {
42   double minRes = Model::next_occurring_event_full(now);
43
44   for (Action const& action : *get_started_action_set()) {
45     const auto& net_action = static_cast<const NetworkAction&>(action);
46     if (net_action.latency_ > 0)
47       minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
48   }
49
50   XBT_DEBUG("Min of share resources %f", minRes);
51
52   return minRes;
53 }
54
55 /**********
56  * Action *
57  **********/
58
59 void NetworkAction::set_state(Action::State state)
60 {
61   Action::State previous = get_state();
62   if (previous != state) { // Trigger only if the state changed
63     Action::set_state(state);
64     s4u::Link::on_communication_state_change(*this, previous);
65   }
66 }
67
68 /** @brief returns a list of all Links that this action is using */
69 std::list<StandardLinkImpl*> NetworkAction::get_links() const
70 {
71   std::list<StandardLinkImpl*> retlist;
72   int llen = get_variable()->get_number_of_constraint();
73
74   for (int i = 0; i < llen; i++) {
75     /* Beware of composite actions: ptasks put links and cpus together */
76     if (auto* link = dynamic_cast<StandardLinkImpl*>(get_variable()->get_constraint(i)->get_id()))
77       retlist.push_back(link);
78   }
79
80   return retlist;
81 }
82
83 static void add_latency(const std::vector<StandardLinkImpl*>& links, double* latency)
84 {
85   if (latency)
86     *latency = std::accumulate(begin(links), end(links), *latency,
87                                [](double lat, const auto* link) { return lat + link->get_latency(); });
88 }
89
90 void add_link_latency(std::vector<StandardLinkImpl*>& result, StandardLinkImpl* link, double* latency)
91 {
92   result.push_back(link);
93   if (latency)
94     *latency += link->get_latency();
95 }
96
97 void add_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
98                       double* latency)
99 {
100   result.insert(result.end(), begin(links), end(links));
101   add_latency(links, latency);
102 }
103
104 void insert_link_latency(std::vector<StandardLinkImpl*>& result, const std::vector<StandardLinkImpl*>& links,
105                          double* latency)
106 {
107   result.insert(result.begin(), rbegin(links), rend(links));
108   add_latency(links, latency);
109 }
110
111 } // namespace simgrid::kernel::resource
112
113 #endif /* NETWORK_INTERFACE_CPP_ */