Logo AND Algorithmique Numérique Distribuée

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