Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle action whose latency is 0.
[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       xbt_dict_get_or_null(network_card_set, 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(network_card_set, card_name, card, network_card_free);
50   }
51   return card->id;
52 }
53
54 static int src_id = -1;
55 static int dst_id = -1;
56
57 static void parse_route_set_endpoints(void)
58 {
59   src_id = network_card_new(A_surfxml_route_src);
60   dst_id = network_card_new(A_surfxml_route_dst);
61   route_action = A_surfxml_route_action;
62   route_link_list = xbt_dynar_new(sizeof(char *), &free_string);
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_prop_cb_list, &parse_properties);
85   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
86   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
87   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
88   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
89   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
90   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
91   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
92   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
93   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
94   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
95   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
96   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
97 }
98
99 static void *name_service(const char *name)
100 {
101   network_card_Constant_t card = xbt_dict_get_or_null(network_card_set, name);
102   return card;
103 }
104
105 static const char *get_resource_name(void *resource_id)
106 {
107   return ((network_card_Constant_t) resource_id)->name;
108 }
109
110 static int resource_used(void *resource_id)
111 {
112   return 0;
113 }
114
115 static int action_free(surf_action_t action)
116 {
117   action->using--;
118   if (!action->using) {
119     xbt_swag_remove(action, action->state_set);
120     free(action);
121     return 1;
122   }
123   return 0;
124 }
125
126 static void action_use(surf_action_t action)
127 {
128   action->using++;
129 }
130
131 static void action_cancel(surf_action_t action)
132 {
133   return;
134 }
135
136 static void action_recycle(surf_action_t action)
137 {
138   return;
139 }
140
141 static void action_change_state(surf_action_t action,
142                                 e_surf_action_state_t state)
143 {
144   surf_action_change_state(action, state);
145   return;
146 }
147
148 static double share_resources(double now)
149 {
150   surf_action_network_Constant_t action = NULL;
151   xbt_swag_t running_actions =
152       surf_network_model->common_public->states.running_action_set;
153   double min = -1.0;
154
155   xbt_swag_foreach(action, running_actions) {
156     if (action->latency > 0) {
157       if (min < 0)
158         min = action->latency;
159       else if (action->latency < min)
160         min = action->latency;
161     }
162   }
163
164   return min;
165 }
166
167 static void update_actions_state(double now, double delta)
168 {
169   surf_action_network_Constant_t action = NULL;
170   surf_action_network_Constant_t next_action = NULL;
171   xbt_swag_t running_actions =
172       surf_network_model->common_public->states.running_action_set;
173
174   xbt_swag_foreach_safe(action, next_action, running_actions) {
175     if (action->latency > 0) {
176       if (action->latency > delta) {
177         double_update(&(action->latency), delta);
178       } else {
179         action->latency = 0.0;
180       }
181     }
182     double_update(&(action->generic_action.remains),
183                   action->generic_action.cost * delta/action->lat_init);
184     if (action->generic_action.max_duration != NO_MAX_DURATION)
185       double_update(&(action->generic_action.max_duration), delta);
186
187     if (action->generic_action.remains <= 0) {
188       action->generic_action.finish = surf_get_clock();
189       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
190     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
191                (action->generic_action.max_duration <= 0)) {
192       action->generic_action.finish = surf_get_clock();
193       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
194     } 
195   }
196
197   return;
198 }
199
200 static void update_resource_state(void *id,
201                                   tmgr_trace_event_t event_type,
202                                   double value)
203 {
204   DIE_IMPOSSIBLE;
205 }
206
207 static surf_action_t communicate(void *src, void *dst, double size,
208                                  double rate)
209 {
210   surf_action_network_Constant_t action = NULL;
211   network_card_Constant_t card_src = src;
212   network_card_Constant_t card_dst = dst;
213
214   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
215
216   action = xbt_new0(s_surf_action_network_Constant_t, 1);
217
218   action->generic_action.using = 1;
219   action->generic_action.cost = size;
220   action->generic_action.remains = size;
221   action->generic_action.max_duration = NO_MAX_DURATION;
222   action->generic_action.start = surf_get_clock();
223   action->generic_action.finish = -1.0;
224   action->generic_action.model_type =
225       (surf_model_t) surf_network_model;
226   action->suspended = 0;
227
228   action->latency = random_generate(random_latency);
229   action->lat_init = action->latency;
230
231   if(action->latency<=0.0)
232     action->generic_action.state_set =
233       surf_network_model->common_public->states.done_action_set;
234   else
235     action->generic_action.state_set =
236       surf_network_model->common_public->states.running_action_set;
237
238   xbt_swag_insert(action, action->generic_action.state_set);
239
240
241   XBT_OUT;
242
243   return (surf_action_t) action;
244 }
245
246 /* returns an array of link_Constant_t */
247 static const void **get_route(void *src, void *dst)
248 {
249   xbt_assert0(0, "Calling this function does not make any sense");
250   return (const void **) NULL;
251 }
252
253 static int get_route_size(void *src, void *dst)
254 {
255   xbt_assert0(0, "Calling this function does not make any sense");
256   return 0;
257 }
258
259 static const char *get_link_name(const void *link)
260 {
261   DIE_IMPOSSIBLE;
262 }
263
264 static double get_link_bandwidth(const void *link)
265 {
266   DIE_IMPOSSIBLE;
267 }
268
269 static double get_link_latency(const void *link)
270 {
271   DIE_IMPOSSIBLE;
272 }
273
274 static xbt_dict_t get_properties(void *link)
275 {
276   DIE_IMPOSSIBLE;
277 }
278
279 static void action_suspend(surf_action_t action)
280 {
281   ((surf_action_network_Constant_t) action)->suspended = 1;
282 }
283
284 static void action_resume(surf_action_t action)
285 {
286   if (((surf_action_network_Constant_t) action)->suspended)
287     ((surf_action_network_Constant_t) action)->suspended = 0;
288 }
289
290 static int action_is_suspended(surf_action_t action)
291 {
292   return ((surf_action_network_Constant_t) action)->suspended;
293 }
294
295 static void action_set_max_duration(surf_action_t action, double duration)
296 {
297   action->max_duration = duration;
298 }
299
300 static void finalize(void)
301 {
302   xbt_dict_free(&network_card_set);
303   xbt_swag_free(surf_network_model->common_public->states.
304                 ready_action_set);
305   xbt_swag_free(surf_network_model->common_public->states.
306                 running_action_set);
307   xbt_swag_free(surf_network_model->common_public->states.
308                 failed_action_set);
309   xbt_swag_free(surf_network_model->common_public->states.
310                 done_action_set);
311   free(surf_network_model->common_public);
312   free(surf_network_model->common_private);
313   free(surf_network_model->extension_public);
314
315   free(surf_network_model);
316   surf_network_model = NULL;
317
318   card_number = 0;
319 }
320
321 static void surf_network_model_init_internal(void)
322 {
323   s_surf_action_t action;
324
325   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
326
327   surf_network_model->common_private =
328       xbt_new0(s_surf_model_private_t, 1);
329   surf_network_model->common_public =
330       xbt_new0(s_surf_model_public_t, 1);
331   surf_network_model->extension_public =
332       xbt_new0(s_surf_network_model_extension_public_t, 1);
333
334   surf_network_model->common_public->states.ready_action_set =
335       xbt_swag_new(xbt_swag_offset(action, state_hookup));
336   surf_network_model->common_public->states.running_action_set =
337       xbt_swag_new(xbt_swag_offset(action, state_hookup));
338   surf_network_model->common_public->states.failed_action_set =
339       xbt_swag_new(xbt_swag_offset(action, state_hookup));
340   surf_network_model->common_public->states.done_action_set =
341       xbt_swag_new(xbt_swag_offset(action, state_hookup));
342
343   surf_network_model->common_public->name_service = name_service;
344   surf_network_model->common_public->get_resource_name =
345       get_resource_name;
346   surf_network_model->common_public->action_get_state =
347       surf_action_get_state;
348   surf_network_model->common_public->action_get_start_time =
349       surf_action_get_start_time;
350   surf_network_model->common_public->action_get_finish_time =
351       surf_action_get_finish_time;
352   surf_network_model->common_public->action_free = action_free;
353   surf_network_model->common_public->action_use = action_use;
354   surf_network_model->common_public->action_cancel = action_cancel;
355   surf_network_model->common_public->action_recycle = action_recycle;
356   surf_network_model->common_public->action_change_state =
357       action_change_state;
358   surf_network_model->common_public->action_set_data =
359       surf_action_set_data;
360   surf_network_model->common_public->name = "network";
361
362   surf_network_model->common_private->resource_used = resource_used;
363   surf_network_model->common_private->share_resources = share_resources;
364   surf_network_model->common_private->update_actions_state =
365       update_actions_state;
366   surf_network_model->common_private->update_resource_state =
367       update_resource_state;
368   surf_network_model->common_private->finalize = finalize;
369
370   surf_network_model->common_public->suspend = action_suspend;
371   surf_network_model->common_public->resume = action_resume;
372   surf_network_model->common_public->is_suspended = action_is_suspended;
373   surf_cpu_model->common_public->set_max_duration =
374       action_set_max_duration;
375
376   surf_network_model->extension_public->communicate = communicate;
377   surf_network_model->extension_public->get_route = get_route;
378   surf_network_model->extension_public->get_route_size = get_route_size;
379   surf_network_model->extension_public->get_link_name = get_link_name;
380   surf_network_model->extension_public->get_link_bandwidth =
381       get_link_bandwidth;
382   surf_network_model->extension_public->get_link_latency =
383       get_link_latency;
384
385   surf_network_model->common_public->get_properties =  get_properties;
386
387   network_card_set = xbt_dict_new();
388
389   if(!random_latency) 
390     random_latency = random_new(RAND, 0.0, 1.0, .125, .034);
391 }
392
393 void surf_network_model_init_Constant(const char *filename)
394 {
395
396   if (surf_network_model)
397     return;
398   surf_network_model_init_internal();
399   define_callbacks(filename);
400   xbt_dynar_push(model_list, &surf_network_model);
401
402   update_model_description(surf_network_model_description,
403                               surf_network_model_description_size,
404                               "Constant",
405                               (surf_model_t) surf_network_model);
406 }