Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move surf_presolve, surf_solve, and surf_get_clock to EngineImpl
[simgrid.git] / src / surf / LinkImpl.cpp
1 /* Copyright (c) 2013-2021. 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 "src/kernel/EngineImpl.hpp"
9 #include "src/surf/LinkImpl.hpp"
10
11 #include <numeric>
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_network);
14
15 /*********
16  * Model *
17  *********/
18
19 namespace simgrid {
20 namespace kernel {
21 namespace resource {
22
23 LinkImpl::LinkImpl(const std::string& name) : LinkImplIntf(name), piface_(this)
24 {
25   if (name != "__loopback__")
26     xbt_assert(not s4u::Link::by_name_or_null(name), "Link '%s' declared several times in the platform.", name.c_str());
27
28   s4u::Engine::get_instance()->link_register(name, &piface_);
29   XBT_DEBUG("Create link '%s'", name.c_str());
30 }
31
32 /** @brief Fire the required callbacks and destroy the object
33  *
34  * Don't delete directly a Link, call l->destroy() instead.
35  */
36 void LinkImpl::destroy()
37 {
38   s4u::Link::on_destruction(piface_);
39   s4u::Engine::get_instance()->link_unregister(get_name());
40   delete this;
41 }
42
43 constexpr kernel::lmm::Constraint::SharingPolicy to_maxmin_policy(s4u::Link::SharingPolicy policy)
44 {
45   switch (policy) {
46     case s4u::Link::SharingPolicy::NONLINEAR:
47       return kernel::lmm::Constraint::SharingPolicy::NONLINEAR;
48     case s4u::Link::SharingPolicy::FATPIPE:
49       return kernel::lmm::Constraint::SharingPolicy::FATPIPE;
50     default:
51       return kernel::lmm::Constraint::SharingPolicy::SHARED;
52   }
53 }
54
55 void LinkImpl::set_sharing_policy(s4u::Link::SharingPolicy policy, const s4u::NonLinearResourceCb& cb)
56 {
57   get_constraint()->set_sharing_policy(to_maxmin_policy(policy), cb);
58   sharing_policy_ = policy;
59 }
60
61 void LinkImpl::latency_check(double latency) const
62 {
63   static double last_warned_latency = sg_surf_precision;
64   if (latency != 0.0 && latency < last_warned_latency) {
65     XBT_WARN("Latency for link %s is smaller than surf/precision (%g < %g)."
66              " For more accuracy, consider setting \"--cfg=surf/precision:%g\".",
67              get_cname(), latency, sg_surf_precision, latency);
68     last_warned_latency = latency;
69   }
70 }
71
72 void LinkImpl::turn_on()
73 {
74   if (not is_on()) {
75     Resource::turn_on();
76     s4u::Link::on_state_change(piface_);
77   }
78 }
79
80 void LinkImpl::turn_off()
81 {
82   if (is_on()) {
83     Resource::turn_off();
84     s4u::Link::on_state_change(piface_);
85
86     const kernel::lmm::Element* elem = nullptr;
87     double now                       = EngineImpl::get_clock();
88     while (const auto* var = get_constraint()->get_variable(&elem)) {
89       Action* action = var->get_id();
90       if (action->get_state() == Action::State::INITED || action->get_state() == Action::State::STARTED) {
91         action->set_finish_time(now);
92         action->set_state(Action::State::FAILED);
93       }
94     }
95   }
96 }
97
98 void LinkImpl::seal()
99 {
100   if (is_sealed())
101     return;
102
103   xbt_assert(this->get_model(), "Cannot seal Link(%s) without setting the Network model first", this->get_cname());
104   Resource::seal();
105 }
106
107 void LinkImpl::on_bandwidth_change() const
108 {
109   s4u::Link::on_bandwidth_change(piface_);
110 }
111
112 void LinkImpl::set_bandwidth_profile(profile::Profile* profile)
113 {
114   if (profile) {
115     xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth profile to Link %s", get_cname());
116     bandwidth_.event = profile->schedule(&profile::future_evt_set, this);
117   }
118 }
119
120 void LinkImpl::set_latency_profile(profile::Profile* profile)
121 {
122   if (profile) {
123     xbt_assert(latency_.event == nullptr, "Cannot set a second latency profile to Link %s", get_cname());
124     latency_.event = profile->schedule(&profile::future_evt_set, this);
125   }
126 }
127
128 void LinkImpl::set_concurrency_limit(int limit) const
129 {
130   if (limit != -1) {
131     get_constraint()->reset_concurrency_maximum();
132   }
133   get_constraint()->set_concurrency_limit(limit);
134 }
135
136 } // namespace resource
137 } // namespace kernel
138 } // namespace simgrid