Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
when parallel ctests are performed, using the default tracing filename may cause...
[simgrid.git] / src / surf / network_constant.c
1 /* Copyright (c) 2008-2013. 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 "surf_private.h"
8 #include "surf/random_mgr.h"
9 #include "xbt/dict.h"
10 #include "xbt/str.h"
11 #include "xbt/log.h"
12
13 typedef struct surf_action_network_Constant {
14   s_surf_action_t generic_action;
15   double latency;
16   double lat_init;
17   int suspended;
18 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
21 static int host_number_int = 0;
22
23 static void netcste_count_hosts(sg_platf_host_cbarg_t h) {
24   host_number_int++;
25 }
26
27 static void netcste_define_callbacks(void) {
28   sg_platf_host_add_cb(netcste_count_hosts);
29 }
30
31 static int netcste_resource_used(void *resource_id)
32 {
33   return 0;
34 }
35
36 static int netcste_action_unref(surf_action_t action)
37 {
38   action->refcount--;
39   if (!action->refcount) {
40     xbt_swag_remove(action, action->state_set);
41     surf_action_free(&action);
42     return 1;
43   }
44   return 0;
45 }
46
47 static void netcste_action_cancel(surf_action_t action)
48 {
49   return;
50 }
51
52 static double netcste_share_resources(double now)
53 {
54   surf_action_network_Constant_t action = NULL;
55   xbt_swag_t running_actions =
56       surf_network_model->states.running_action_set;
57   double min = -1.0;
58
59   xbt_swag_foreach(action, running_actions) {
60     if (action->latency > 0) {
61       if (min < 0)
62         min = action->latency;
63       else if (action->latency < min)
64         min = action->latency;
65     }
66   }
67
68   return min;
69 }
70
71 static void netcste_update_actions_state(double now, double delta)
72 {
73   surf_action_network_Constant_t action = NULL;
74   surf_action_network_Constant_t next_action = NULL;
75   xbt_swag_t running_actions =
76       surf_network_model->states.running_action_set;
77
78   xbt_swag_foreach_safe(action, next_action, running_actions) {
79     if (action->latency > 0) {
80       if (action->latency > delta) {
81         double_update(&(action->latency), delta);
82       } else {
83         action->latency = 0.0;
84       }
85     }
86     double_update(&(action->generic_action.remains),
87                   action->generic_action.cost * delta / action->lat_init);
88     if (action->generic_action.max_duration != NO_MAX_DURATION)
89       double_update(&(action->generic_action.max_duration), delta);
90
91     if (action->generic_action.remains <= 0) {
92       action->generic_action.finish = surf_get_clock();
93       surf_network_model->action_state_set((surf_action_t) action,
94                                            SURF_ACTION_DONE);
95     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
96                && (action->generic_action.max_duration <= 0)) {
97       action->generic_action.finish = surf_get_clock();
98       surf_network_model->action_state_set((surf_action_t) action,
99                                            SURF_ACTION_DONE);
100     }
101   }
102 }
103
104 static void netcste_update_resource_state(void *id,
105                                           tmgr_trace_event_t event_type,
106                                           double value, double time)
107 {
108   DIE_IMPOSSIBLE;
109 }
110
111 static surf_action_t netcste_communicate(sg_routing_edge_t src,
112                                          sg_routing_edge_t dst,
113                                          double size, double rate)
114 {
115   surf_action_network_Constant_t action = NULL;
116
117   char *src_name = src->name;
118   char *dst_name = dst->name;
119
120   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
121
122   action =
123       surf_action_new(sizeof(s_surf_action_network_Constant_t), size,
124                       surf_network_model, 0);
125
126   action->suspended = 0;
127
128   action->latency = sg_latency_factor;
129   action->lat_init = action->latency;
130
131   if (action->latency <= 0.0) {
132     action->generic_action.state_set =
133         surf_network_model->states.done_action_set;
134     xbt_swag_insert(action, action->generic_action.state_set);
135   }
136
137   XBT_OUT();
138
139   return (surf_action_t) action;
140 }
141
142 #ifdef HAVE_TRACING
143 static void netcste_action_set_category(surf_action_t action, const char *category)
144 {
145   //ignore completely the categories in constant model, they are not traced
146 }
147 #endif
148
149 static double netcste_get_link_bandwidth(const void *link)
150 {
151   DIE_IMPOSSIBLE;
152   return -1.0; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
153 }
154
155 static double netcste_get_link_latency(const void *link)
156 {
157   DIE_IMPOSSIBLE;
158   return -1.0; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
159 }
160
161 static int link_shared(const void *link)
162 {
163   DIE_IMPOSSIBLE;
164   return -1; /* useless since DIE actually abort(), but eclipse prefer to have a useless and harmless return */
165 }
166
167 static void netcste_action_suspend(surf_action_t action)
168 {
169   ((surf_action_network_Constant_t) action)->suspended = 1;
170 }
171
172 static void netcste_action_resume(surf_action_t action)
173 {
174   if (((surf_action_network_Constant_t) action)->suspended)
175     ((surf_action_network_Constant_t) action)->suspended = 0;
176 }
177
178 static int netcste_action_is_suspended(surf_action_t action)
179 {
180   return ((surf_action_network_Constant_t) action)->suspended;
181 }
182
183 static void netcste_finalize(void)
184 {
185   surf_model_exit(surf_network_model);
186   surf_network_model = NULL;
187 }
188
189
190
191 void surf_network_model_init_Constant()
192 {
193   xbt_assert(surf_network_model == NULL);
194   if (surf_network_model)
195     return;
196   surf_network_model = surf_model_init();
197
198   surf_network_model->name = "constant time network";
199   surf_network_model->action_unref = netcste_action_unref;
200   surf_network_model->action_cancel = netcste_action_cancel;
201   surf_network_model->action_recycle = net_action_recycle;
202   surf_network_model->get_remains = surf_action_get_remains;
203 #ifdef HAVE_LATENCY_BOUND_TRACKING
204   surf_network_model->get_latency_limited = net_get_link_latency_limited;
205 #endif
206
207   surf_network_model->model_private->resource_used = netcste_resource_used;
208   surf_network_model->model_private->share_resources =
209       netcste_share_resources;
210   surf_network_model->model_private->update_actions_state =
211       netcste_update_actions_state;
212   surf_network_model->model_private->update_resource_state =
213       netcste_update_resource_state;
214   surf_network_model->model_private->finalize = netcste_finalize;
215
216   surf_network_model->suspend = netcste_action_suspend;
217   surf_network_model->resume = netcste_action_resume;
218   surf_network_model->is_suspended = netcste_action_is_suspended;
219   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
220
221   surf_network_model->extension.network.communicate = netcste_communicate;
222   surf_network_model->extension.network.get_link_bandwidth =
223       netcste_get_link_bandwidth;
224   surf_network_model->extension.network.get_link_latency =
225       netcste_get_link_latency;
226   surf_network_model->extension.network.link_shared = link_shared;
227 #ifdef HAVE_TRACING
228   surf_network_model->set_category = netcste_action_set_category;
229 #endif
230
231   netcste_define_callbacks();
232   xbt_dynar_push(model_list, &surf_network_model);
233
234   routing_model_create(NULL);
235 }