Logo AND Algorithmique Numérique Distribuée

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