Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[surf] Move network_constant host callback from sg_platf_host_cb to hostCreatedCallbacks
[simgrid.git] / src / surf / network_constant.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 "network_constant.hpp"
8 #include "surf/random_mgr.h"
9
10 #include "host_interface.hpp"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
13 static int host_number_int = 0;
14
15 static void netcste_parse_nolink(sg_platf_link_cbarg_t link){
16         xbt_die("There is no link in the Constant network model. "
17                         "Please remove any link from your platform (and switch to routing='None')");
18 }
19
20 /*********
21  * Model *
22  *********/
23 void surf_network_model_init_Constant()
24 {
25   xbt_assert(surf_network_model == NULL);
26   surf_network_model = new NetworkConstantModel();
27
28   routing_model_create(NULL);
29
30   hostCreatedCallbacks.connect([](Host*) {
31     host_number_int++;
32   });
33   sg_platf_link_add_cb(netcste_parse_nolink);
34
35   Model *model = surf_network_model;
36   xbt_dynar_push(all_existing_models, &model);
37 }
38
39 double NetworkConstantModel::shareResources(double /*now*/)
40 {
41   NetworkConstantAction *action = NULL;
42   double min = -1.0;
43
44   ActionList *actionSet = getRunningActionSet();
45   for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
46          ; it != itend ; ++it) {
47         action = static_cast<NetworkConstantAction*>(&*it);
48         if (action->m_latency > 0 && (min < 0 || action->m_latency < min))
49             min = action->m_latency;
50   }
51
52   return min;
53 }
54
55 void NetworkConstantModel::updateActionsState(double /*now*/, double delta)
56 {
57   NetworkConstantAction *action = NULL;
58   ActionList *actionSet = getRunningActionSet();
59   for(ActionList::iterator it(actionSet->begin()), itNext=it, itend(actionSet->end())
60      ; it != itend ; it=itNext) {
61     ++itNext;
62         action = static_cast<NetworkConstantAction*>(&*it);
63     if (action->m_latency > 0) {
64       if (action->m_latency > delta) {
65         double_update(&(action->m_latency), delta, sg_surf_precision);
66       } else {
67         action->m_latency = 0.0;
68       }
69     }
70     action->updateRemains(action->getCost() * delta / action->m_latInit);
71     if (action->getMaxDuration() != NO_MAX_DURATION)
72       action->updateMaxDuration(delta);
73
74     if (action->getRemainsNoUpdate() <= 0) {
75       action->finish();
76       action->setState(SURF_ACTION_DONE);
77     } else if ((action->getMaxDuration() != NO_MAX_DURATION)
78                && (action->getMaxDuration() <= 0)) {
79       action->finish();
80       action->setState(SURF_ACTION_DONE);
81     }
82   }
83 }
84
85 Action *NetworkConstantModel::communicate(RoutingEdge *src, RoutingEdge *dst,
86                                          double size, double rate)
87 {
88   char *src_name = src->getName();
89   char *dst_name = dst->getName();
90
91   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
92   NetworkConstantAction *action = new NetworkConstantAction(this, size, sg_latency_factor);
93   XBT_OUT();
94
95   surf_callback_emit(networkCommunicateCallbacks, action, src, dst, size, rate);
96   return action;
97 }
98
99 /**********
100  * Action *
101  **********/
102
103 int NetworkConstantAction::unref()
104 {
105   m_refcount--;
106   if (!m_refcount) {
107         if (action_hook.is_linked())
108           p_stateSet->erase(p_stateSet->iterator_to(*this));
109     delete this;
110   return 1;
111   }
112   return 0;
113 }
114
115 void NetworkConstantAction::cancel()
116 {
117   return;
118 }
119
120 void NetworkConstantAction::setCategory(const char * /*category*/)
121 {
122   //ignore completely the categories in constant model, they are not traced
123 }
124
125 void NetworkConstantAction::suspend()
126 {
127   m_suspended = true;
128 }
129
130 void NetworkConstantAction::resume()
131 {
132   if (m_suspended)
133         m_suspended = false;
134 }
135
136 bool NetworkConstantAction::isSuspended()
137 {
138   return m_suspended;
139 }
140