Logo AND Algorithmique Numérique Distribuée

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