Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make s_lmm_constraint_t a class with its methods.
[simgrid.git] / src / surf / network_interface.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <algorithm>
8
9 #include "maxmin_private.hpp"
10 #include "network_interface.hpp"
11 #include "simgrid/sg_config.h"
12
13 #ifndef NETWORK_INTERFACE_CPP_
14 #define NETWORK_INTERFACE_CPP_
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf, "Logging specific to the SURF network module");
17
18 namespace simgrid {
19   namespace surf {
20
21   /* List of links */
22   std::unordered_map<std::string, LinkImpl*>* LinkImpl::links = new std::unordered_map<std::string, LinkImpl*>();
23
24   LinkImpl* LinkImpl::byName(std::string name)
25   {
26     auto link = links->find(name);
27     return link == links->end() ? nullptr : link->second;
28   }
29   /** @brief Returns the amount of links in the platform */
30   int LinkImpl::linksCount()
31   {
32     return links->size();
33   }
34   void LinkImpl::linksList(std::vector<s4u::Link*>* linkList)
35   {
36     for (auto const& kv : *links) {
37       linkList->push_back(&kv.second->piface_);
38     }
39   }
40
41   /** @brief Returns a list of all existing links */
42   LinkImpl** LinkImpl::linksList()
43   {
44     LinkImpl** res = xbt_new(LinkImpl*, (int)links->size());
45     int i          = 0;
46     for (auto const& kv : *links) {
47       res[i] = kv.second;
48       i++;
49     }
50     return res;
51   }
52   /** @brief destructor of the static data */
53   void LinkImpl::linksExit()
54   {
55     for (auto const& kv : *links)
56       (kv.second)->destroy();
57     delete links;
58   }
59   }
60 }
61
62 /*********
63  * Model *
64  *********/
65
66 simgrid::surf::NetworkModel *surf_network_model = nullptr;
67
68 namespace simgrid {
69   namespace surf {
70
71     NetworkModel::~NetworkModel()
72     {
73       delete maxminSystem_;
74       delete modifiedSet_;
75     }
76
77     double NetworkModel::latencyFactor(double /*size*/) {
78       return sg_latency_factor;
79     }
80
81     double NetworkModel::bandwidthFactor(double /*size*/) {
82       return sg_bandwidth_factor;
83     }
84
85     double NetworkModel::bandwidthConstraint(double rate, double /*bound*/, double /*size*/) {
86       return rate;
87     }
88
89     double NetworkModel::nextOccuringEventFull(double now)
90     {
91       double minRes = Model::nextOccuringEventFull(now);
92
93       for (Action const& action : *getRunningActionSet()) {
94         const NetworkAction& net_action = static_cast<const NetworkAction&>(action);
95         if (net_action.latency_ > 0)
96           minRes = (minRes < 0) ? net_action.latency_ : std::min(minRes, net_action.latency_);
97       }
98
99       XBT_DEBUG("Min of share resources %f", minRes);
100
101       return minRes;
102     }
103
104     /************
105      * Resource *
106      ************/
107
108     LinkImpl::LinkImpl(simgrid::surf::NetworkModel* model, const std::string& name, lmm_constraint_t constraint)
109         : Resource(model, name, constraint), piface_(this)
110     {
111
112       if (name != "__loopback__")
113         xbt_assert(not LinkImpl::byName(name), "Link '%s' declared several times in the platform.", name.c_str());
114
115       latency_.scale   = 1;
116       bandwidth_.scale = 1;
117
118       links->insert({name, this});
119       XBT_DEBUG("Create link '%s'", name.c_str());
120     }
121
122     /** @brief use destroy() instead of this destructor */
123     LinkImpl::~LinkImpl()
124     {
125       xbt_assert(currentlyDestroying_, "Don't delete Links directly. Call destroy() instead.");
126     }
127     /** @brief Fire the required callbacks and destroy the object
128      *
129      * Don't delete directly a Link, call l->destroy() instead.
130      */
131     void LinkImpl::destroy()
132     {
133       if (not currentlyDestroying_) {
134         currentlyDestroying_ = true;
135         s4u::Link::onDestruction(this->piface_);
136         delete this;
137       }
138     }
139
140     bool LinkImpl::isUsed()
141     {
142       return model()->getMaxminSystem()->constraint_used(constraint());
143     }
144
145     double LinkImpl::latency()
146     {
147       return latency_.peak * latency_.scale;
148     }
149
150     double LinkImpl::bandwidth()
151     {
152       return bandwidth_.peak * bandwidth_.scale;
153     }
154
155     int LinkImpl::sharingPolicy()
156     {
157       return constraint()->get_sharing_policy();
158     }
159
160     void LinkImpl::turnOn()
161     {
162       if (isOff()) {
163         Resource::turnOn();
164         s4u::Link::onStateChange(this->piface_);
165       }
166     }
167     void LinkImpl::turnOff()
168     {
169       if (isOn()) {
170         Resource::turnOff();
171         s4u::Link::onStateChange(this->piface_);
172       }
173     }
174     void LinkImpl::setStateTrace(tmgr_trace_t trace)
175     {
176       xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Link %s", getCname());
177       stateEvent_ = future_evt_set->add_trace(trace, this);
178     }
179     void LinkImpl::setBandwidthTrace(tmgr_trace_t trace)
180     {
181       xbt_assert(bandwidth_.event == nullptr, "Cannot set a second bandwidth trace to Link %s", getCname());
182       bandwidth_.event = future_evt_set->add_trace(trace, this);
183     }
184     void LinkImpl::setLatencyTrace(tmgr_trace_t trace)
185     {
186       xbt_assert(latency_.event == nullptr, "Cannot set a second latency trace to Link %s", getCname());
187       latency_.event = future_evt_set->add_trace(trace, this);
188     }
189
190
191     /**********
192      * Action *
193      **********/
194
195     void NetworkAction::setState(Action::State state)
196     {
197       Action::setState(state);
198       s4u::Link::onCommunicationStateChange(this);
199     }
200
201     /** @brief returns a list of all Links that this action is using */
202     std::list<LinkImpl*> NetworkAction::links()
203     {
204       std::list<LinkImpl*> retlist;
205       lmm_system_t sys = getModel()->getMaxminSystem();
206       int llen         = lmm_get_number_of_cnst_from_var(sys, getVariable());
207
208       for (int i = 0; i < llen; i++) {
209         /* Beware of composite actions: ptasks put links and cpus together */
210         // extra pb: we cannot dynamic_cast from void*...
211         Resource* resource = static_cast<Resource*>(lmm_get_cnst_from_var(sys, getVariable(), i)->get_id());
212         LinkImpl* link     = dynamic_cast<LinkImpl*>(resource);
213         if (link != nullptr)
214           retlist.push_back(link);
215       }
216
217       return retlist;
218     }
219   }
220 }
221
222 #endif /* NETWORK_INTERFACE_CPP_ */