Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c6e2e49634efec133b54cfb572aeabc7bf7c8a87
[simgrid.git] / src / surf / network_constant.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "surf_private.h"
9 #include "network_common.h"
10 #include "surf/random_mgr.h"
11 #include "xbt/dict.h"
12 #include "xbt/str.h"
13 #include "xbt/log.h"
14
15 typedef struct network_card_Constant {
16   char *name;
17   int id;
18 } s_network_card_Constant_t, *network_card_Constant_t;
19
20 typedef struct surf_action_network_Constant {
21   s_surf_action_t generic_action;
22   double latency;
23   double lat_init;
24   int suspended;
25   network_card_Constant_t src;
26   network_card_Constant_t dst;
27 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
28
29 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
30 static random_data_t random_latency = NULL;
31 static int card_number = 0;
32 static int host_number = 0;
33
34 static void network_card_free(void *nw_card)
35 {
36   free(((network_card_Constant_t) nw_card)->name);
37   free(nw_card);
38 }
39
40 static int network_card_new(const char *card_name)
41 {
42   network_card_Constant_t card =
43     surf_model_resource_by_name(surf_network_model, card_name);
44
45   if (!card) {
46     card = xbt_new0(s_network_card_Constant_t, 1);
47     card->name = xbt_strdup(card_name);
48     card->id = card_number++;
49     xbt_dict_set(surf_model_resource_set(surf_network_model), card_name, card,
50                  network_card_free);
51   }
52   return card->id;
53 }
54
55 static int src_id = -1;
56 static int dst_id = -1;
57
58 static void parse_route_set_endpoints(void)
59 {
60   src_id = network_card_new(A_surfxml_route_src);
61   dst_id = network_card_new(A_surfxml_route_dst);
62   route_action = A_surfxml_route_action;
63 }
64
65 static void parse_route_set_route(void)
66 {
67   char *name;
68   if (src_id != -1 && dst_id != -1) {
69     name = bprintf("%x#%x", src_id, dst_id);
70     manage_route(route_table, name, route_action, 0);
71     free(name);
72   }
73 }
74
75 static void count_hosts(void)
76 {
77   host_number++;
78 }
79
80 static void define_callbacks(const char *file)
81 {
82   /* Figuring out the network links */
83   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
84   surfxml_add_callback(STag_surfxml_route_cb_list,
85                        &parse_route_set_endpoints);
86   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
87 }
88
89 static const char *get_resource_name(void *resource_id)
90 {
91   return ((network_card_Constant_t) resource_id)->name;
92 }
93
94 static int resource_used(void *resource_id)
95 {
96   return 0;
97 }
98
99 static int action_free(surf_action_t action)
100 {
101   action->refcount--;
102   if (!action->refcount) {
103     xbt_swag_remove(action, action->state_set);
104     free(action);
105     return 1;
106   }
107   return 0;
108 }
109
110 static void action_use(surf_action_t action)
111 {
112   action->refcount++;
113 }
114
115 static void action_cancel(surf_action_t action)
116 {
117   return;
118 }
119
120 static void action_recycle(surf_action_t action)
121 {
122   return;
123 }
124
125 static void action_change_state(surf_action_t action,
126                                 e_surf_action_state_t state)
127 {
128   surf_action_change_state(action, state);
129   return;
130 }
131
132 static double share_resources(double now)
133 {
134   surf_action_network_Constant_t action = NULL;
135   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
136   double min = -1.0;
137
138   xbt_swag_foreach(action, running_actions) {
139     if (action->latency > 0) {
140       if (min < 0)
141         min = action->latency;
142       else if (action->latency < min)
143         min = action->latency;
144     }
145   }
146
147   return min;
148 }
149
150 static void update_actions_state(double now, double delta)
151 {
152   surf_action_network_Constant_t action = NULL;
153   surf_action_network_Constant_t next_action = NULL;
154   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
155
156   xbt_swag_foreach_safe(action, next_action, running_actions) {
157     if (action->latency > 0) {
158       if (action->latency > delta) {
159         double_update(&(action->latency), delta);
160       } else {
161         action->latency = 0.0;
162       }
163     }
164     double_update(&(action->generic_action.remains),
165                   action->generic_action.cost * delta / action->lat_init);
166     if (action->generic_action.max_duration != NO_MAX_DURATION)
167       double_update(&(action->generic_action.max_duration), delta);
168
169     if (action->generic_action.remains <= 0) {
170       action->generic_action.finish = surf_get_clock();
171       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
172     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
173                (action->generic_action.max_duration <= 0)) {
174       action->generic_action.finish = surf_get_clock();
175       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
176     }
177   }
178
179   return;
180 }
181
182 static void update_resource_state(void *id,
183                                   tmgr_trace_event_t event_type,
184                                   double value, double time)
185 {
186   DIE_IMPOSSIBLE;
187 }
188
189 static surf_action_t communicate(void *src, void *dst, double size,
190                                  double rate)
191 {
192   surf_action_network_Constant_t action = NULL;
193   network_card_Constant_t card_src = src;
194   network_card_Constant_t card_dst = dst;
195
196   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
197
198   action = xbt_new0(s_surf_action_network_Constant_t, 1);
199
200   action->generic_action.refcount = 1;
201   action->generic_action.cost = size;
202   action->generic_action.remains = size;
203   action->generic_action.max_duration = NO_MAX_DURATION;
204   action->generic_action.start = surf_get_clock();
205   action->generic_action.finish = -1.0;
206   action->generic_action.model_type = surf_network_model;
207   action->suspended = 0;
208
209   action->latency = random_generate(random_latency);
210   action->lat_init = action->latency;
211
212   if (action->latency <= 0.0)
213     action->generic_action.state_set =
214       surf_network_model->states.done_action_set;
215   else
216     action->generic_action.state_set =
217       surf_network_model->states.running_action_set;
218
219   xbt_swag_insert(action, action->generic_action.state_set);
220
221
222   XBT_OUT;
223
224   return (surf_action_t) action;
225 }
226
227 /* returns an array of link_Constant_t */
228 static const void **get_route(void *src, void *dst)
229 {
230   xbt_assert0(0, "Calling this function does not make any sense");
231   return (const void **) NULL;
232 }
233
234 static int get_route_size(void *src, void *dst)
235 {
236   xbt_assert0(0, "Calling this function does not make any sense");
237   return 0;
238 }
239
240 static const char *get_link_name(const void *link)
241 {
242   DIE_IMPOSSIBLE;
243 }
244
245 static double get_link_bandwidth(const void *link)
246 {
247   DIE_IMPOSSIBLE;
248 }
249
250 static double get_link_latency(const void *link)
251 {
252   DIE_IMPOSSIBLE;
253 }
254
255 static int link_shared(const void *link)
256 {
257   DIE_IMPOSSIBLE;
258 }
259
260 static xbt_dict_t get_properties(void *link)
261 {
262   DIE_IMPOSSIBLE;
263 }
264
265 static void action_suspend(surf_action_t action)
266 {
267   ((surf_action_network_Constant_t) action)->suspended = 1;
268 }
269
270 static void action_resume(surf_action_t action)
271 {
272   if (((surf_action_network_Constant_t) action)->suspended)
273     ((surf_action_network_Constant_t) action)->suspended = 0;
274 }
275
276 static int action_is_suspended(surf_action_t action)
277 {
278   return ((surf_action_network_Constant_t) action)->suspended;
279 }
280
281 static void action_set_max_duration(surf_action_t action, double duration)
282 {
283   action->max_duration = duration;
284 }
285
286 static void finalize(void)
287 {
288   surf_model_exit(surf_network_model);
289
290   free(surf_network_model);
291   surf_network_model = NULL;
292
293   card_number = 0;
294 }
295
296 static void surf_network_model_init_internal(void)
297 {
298   surf_network_model = xbt_new0(s_surf_model_t, 1);
299
300   surf_model_init(surf_network_model);
301
302   surf_network_model->get_resource_name = get_resource_name;
303   surf_network_model->action_get_state = surf_action_get_state;
304   surf_network_model->action_get_start_time = surf_action_get_start_time;
305   surf_network_model->action_get_finish_time = surf_action_get_finish_time;
306   surf_network_model->action_free = action_free;
307   surf_network_model->action_use = action_use;
308   surf_network_model->action_cancel = action_cancel;
309   surf_network_model->action_recycle = action_recycle;
310   surf_network_model->action_change_state = action_change_state;
311   surf_network_model->action_set_data = surf_action_set_data;
312   surf_network_model->name = "network";
313
314   surf_network_model->model_private->resource_used = resource_used;
315   surf_network_model->model_private->share_resources = share_resources;
316   surf_network_model->model_private->update_actions_state =
317     update_actions_state;
318   surf_network_model->model_private->update_resource_state =
319     update_resource_state;
320   surf_network_model->model_private->finalize = finalize;
321
322   surf_network_model->suspend = action_suspend;
323   surf_network_model->resume = action_resume;
324   surf_network_model->is_suspended = action_is_suspended;
325   surf_cpu_model->set_max_duration = action_set_max_duration;
326
327   surf_network_model->extension.network.communicate = communicate;
328   surf_network_model->extension.network.get_route = get_route;
329   surf_network_model->extension.network.get_route_size = get_route_size;
330   surf_network_model->extension.network.get_link_name = get_link_name;
331   surf_network_model->extension.network.get_link_bandwidth =
332     get_link_bandwidth;
333   surf_network_model->extension.network.get_link_latency = get_link_latency;
334   surf_network_model->extension.network.link_shared = link_shared;
335
336   surf_network_model->get_properties = get_properties;
337
338   if (!random_latency)
339     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
340 }
341
342 void surf_network_model_init_Constant(const char *filename)
343 {
344
345   if (surf_network_model)
346     return;
347   surf_network_model_init_internal();
348   define_callbacks(filename);
349   xbt_dynar_push(model_list, &surf_network_model);
350
351   update_model_description(surf_network_model_description,
352                            "Constant", surf_network_model);
353 }