Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Moving init_GTNETS function to the right place.
[simgrid.git] / src / surf / network.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 "network_private.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
13                                 "Logging specific to the SURF network module");
14
15 surf_network_model_t surf_network_model = NULL;
16 static lmm_system_t network_maxmin_system = NULL;
17 static void (*network_solve) (lmm_system_t) = NULL;
18 xbt_dict_t link_set = NULL;
19 xbt_dict_t network_card_set = NULL;
20
21 int card_number = 0;
22 int host_number = 0;
23 link_CM02_t **routing_table = NULL;
24 int *routing_table_size = NULL;
25 static link_CM02_t loopback = NULL;
26
27 static void create_routing_table(void)
28 {
29   routing_table =
30       xbt_new0(link_CM02_t *, /*card_number * card_number */ host_number * host_number);
31   routing_table_size = xbt_new0(int, /*card_number * card_number*/ host_number * host_number);
32 }
33
34 static void link_free(void *nw_link)
35 {
36   free(((link_CM02_t) nw_link)->name);
37   free(nw_link);
38 }
39
40 static link_CM02_t link_new(char *name,
41                             double bw_initial,
42                             tmgr_trace_t bw_trace,
43                             double lat_initial,
44                             tmgr_trace_t lat_trace,
45                             e_surf_link_state_t
46                             state_initial,
47                             tmgr_trace_t state_trace,
48                             e_surf_link_sharing_policy_t
49                             policy, xbt_dict_t properties)
50 {
51   link_CM02_t nw_link = xbt_new0(s_link_CM02_t, 1);
52   xbt_assert1(!xbt_dict_get_or_null(link_set, name), 
53               "Link '%s' declared several times in the platform file.", name);   
54
55   nw_link->model = (surf_model_t) surf_network_model;
56   nw_link->name = name;
57   nw_link->bw_current = bw_initial;
58   if (bw_trace)
59     nw_link->bw_event =
60         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
61   nw_link->lat_current = lat_initial;
62   if (lat_trace)
63     nw_link->lat_event =
64         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
65   nw_link->state_current = state_initial;
66   if (state_trace)
67     nw_link->state_event =
68         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
69
70   nw_link->constraint =
71       lmm_constraint_new(network_maxmin_system, nw_link,
72                          nw_link->bw_current);
73
74   if (policy == SURF_LINK_FATPIPE)
75     lmm_constraint_shared(nw_link->constraint);
76
77   nw_link->properties = properties;
78
79   current_property_set = properties;
80
81   xbt_dict_set(link_set, name, nw_link, link_free);
82
83   return nw_link;
84 }
85
86 static void network_card_free(void *nw_card)
87 {
88   free(((network_card_CM02_t) nw_card)->name);
89   free(nw_card);
90 }
91
92 static int network_card_new(const char *card_name)
93 {
94   network_card_CM02_t card =
95       xbt_dict_get_or_null(network_card_set, card_name);
96
97   if (!card) {
98     card = xbt_new0(s_network_card_CM02_t, 1);
99     card->name = xbt_strdup(card_name);
100     card->id = host_number++;
101     xbt_dict_set(network_card_set, card_name, card, network_card_free);
102   }
103   return card->id;
104 }
105
106 static void route_new(int src_id, int dst_id,
107                       link_CM02_t * link_list, int nb_link)
108 {
109   ROUTE_SIZE(src_id, dst_id) = nb_link;
110   ROUTE(src_id, dst_id) = link_list =
111       xbt_realloc(link_list, sizeof(link_CM02_t) * nb_link);
112 }
113
114 static void parse_link_init(void)
115 {
116   char *name_link;
117   double bw_initial;
118   tmgr_trace_t bw_trace;
119   double lat_initial;
120   tmgr_trace_t lat_trace;
121   e_surf_link_state_t state_initial_link = SURF_LINK_ON;
122   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
123   tmgr_trace_t state_trace;
124
125   name_link = xbt_strdup(A_surfxml_link_id);
126   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
127   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
128   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
129   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
130
131   xbt_assert0((A_surfxml_link_state ==
132                A_surfxml_link_state_ON)
133               || (A_surfxml_link_state ==
134                   A_surfxml_link_state_OFF), "Invalid state");
135   if (A_surfxml_link_state == A_surfxml_link_state_ON)
136     state_initial_link = SURF_LINK_ON;
137   else if (A_surfxml_link_state ==
138            A_surfxml_link_state_OFF)
139     state_initial_link = SURF_LINK_OFF;
140
141   if (A_surfxml_link_sharing_policy ==
142       A_surfxml_link_sharing_policy_SHARED)
143     policy_initial_link = SURF_LINK_SHARED;
144   else if (A_surfxml_link_sharing_policy ==
145            A_surfxml_link_sharing_policy_FATPIPE)
146     policy_initial_link = SURF_LINK_FATPIPE;
147
148   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
149
150   link_new(name_link, bw_initial, bw_trace,
151                    lat_initial, lat_trace, state_initial_link, state_trace,
152                    policy_initial_link, xbt_dict_new());
153
154 }
155
156 static int src_id = -1;
157 static int dst_id = -1;
158
159 static void parse_route_set_endpoints(void)
160 {
161   src_id = network_card_new(A_surfxml_route_src);
162   dst_id = network_card_new(A_surfxml_route_dst);
163   route_action = A_surfxml_route_action;
164   route_link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
165 }
166
167 static void parse_route_set_route(void)
168 {
169   char *name;
170   if (src_id != -1 && dst_id != -1) {
171     name = bprintf("%x#%x",src_id, dst_id);
172     manage_route(route_table, name, route_action, 0);
173     free(name);    
174   }
175 }
176
177 static void add_loopback(void)
178 {
179   int i;
180   /* Adding loopback if needed */
181   for (i = 0; i < host_number; i++)
182     if (!ROUTE_SIZE(i, i)) {
183       if (!loopback)
184         loopback = link_new(xbt_strdup("__MSG_loopback__"),
185                                     498000000, NULL, 0.000015, NULL,
186                                     SURF_LINK_ON, NULL,
187                                     SURF_LINK_FATPIPE,NULL);
188       ROUTE_SIZE(i, i) = 1;
189       ROUTE(i, i) = xbt_new0(link_CM02_t, 1);
190       ROUTE(i, i)[0] = loopback;
191     }
192 }
193
194 static void add_route(void)
195 {
196   xbt_ex_t e;
197   int nb_link = 0;
198   unsigned int cpt = 0;    
199   int link_list_capacity = 0;
200   link_CM02_t *link_list = NULL;
201   xbt_dict_cursor_t cursor = NULL;
202   char *key,*data, *end;
203   const char *sep = "#";
204   xbt_dynar_t links, keys;
205
206   if (routing_table == NULL) create_routing_table();
207
208   xbt_dict_foreach(route_table, cursor, key, data) {
209           char* link = NULL;
210     nb_link = 0;
211     links = (xbt_dynar_t)data;
212     keys = xbt_str_split_str(key, sep);
213
214     link_list_capacity = xbt_dynar_length(links);
215     link_list = xbt_new(link_CM02_t, link_list_capacity);
216
217     src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
218     dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
219     xbt_dynar_free(&keys);
220  
221     xbt_dynar_foreach (links, cpt, link) {
222       TRY {
223         link_list[nb_link++] = xbt_dict_get(link_set, link);
224       }
225       CATCH(e) {
226         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
227       }     
228     }
229     route_new(src_id, dst_id, link_list, nb_link);
230    }
231
232    xbt_dict_free(&route_table);
233
234 }
235
236 static void count_hosts(void)
237 {
238    host_number++;
239 }
240
241
242 static void add_traces(void) {
243    xbt_dict_cursor_t cursor=NULL;
244    char *trace_name,*elm;
245    
246    static int called = 0;
247    if (called) return;
248    called = 1;
249
250    /* connect all traces relative to network */
251    xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
252       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
253       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
254       
255       xbt_assert1(link, "Link %s undefined", elm);
256       xbt_assert1(trace, "Trace %s undefined", trace_name);
257       
258       link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
259    }
260
261    xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
262       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
263       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
264       
265       xbt_assert1(link, "Link %s undefined", elm);
266       xbt_assert1(trace, "Trace %s undefined", trace_name);
267       
268       link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
269    }
270    
271    xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
272       tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
273       link_CM02_t link = xbt_dict_get_or_null(link_set, elm);
274       
275       xbt_assert1(link, "Link %s undefined", elm);
276       xbt_assert1(trace, "Trace %s undefined", trace_name);
277       
278       link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
279    }
280
281    xbt_dict_free(&trace_connect_list_host_avail);
282    xbt_dict_free(&trace_connect_list_power);
283    xbt_dict_free(&trace_connect_list_link_avail);
284    xbt_dict_free(&trace_connect_list_bandwidth);
285    xbt_dict_free(&trace_connect_list_latency);
286    
287    xbt_dict_free(&traces_set_list); 
288 }
289
290 static void define_callbacks(const char *file)
291 {
292   /* Figuring out the network links */
293   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
294   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
295   surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties);
296   surfxml_add_callback(STag_surfxml_route_cb_list, &parse_route_set_endpoints);
297   surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_route_elem);
298   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
299   surfxml_add_callback(STag_surfxml_platform_cb_list, &init_data);
300   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
301   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
302   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback);
303   surfxml_add_callback(STag_surfxml_set_cb_list, &parse_sets);
304   surfxml_add_callback(STag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_endpoints);
305   surfxml_add_callback(ETag_surfxml_route_c_multi_cb_list, &parse_route_multi_set_route);
306   surfxml_add_callback(STag_surfxml_foreach_cb_list, &parse_foreach);
307   surfxml_add_callback(STag_surfxml_cluster_cb_list, &parse_cluster);
308   surfxml_add_callback(STag_surfxml_trace_cb_list, &parse_trace_init);
309   surfxml_add_callback(ETag_surfxml_trace_cb_list, &parse_trace_finalize);
310   surfxml_add_callback(STag_surfxml_trace_c_connect_cb_list, &parse_trace_c_connect);
311 }
312
313 static void *name_service(const char *name)
314 {
315   network_card_CM02_t card = xbt_dict_get_or_null(network_card_set, name);
316   return card;
317 }
318
319 static const char *get_resource_name(void *resource_id)
320 {
321   return ((network_card_CM02_t) resource_id)->name;
322 }
323
324 static int resource_used(void *resource_id)
325 {
326   return lmm_constraint_used(network_maxmin_system,
327                              ((link_CM02_t) resource_id)->
328                              constraint);
329 }
330
331 static int action_free(surf_action_t action)
332 {
333   action->using--;
334   if (!action->using) {
335     xbt_swag_remove(action, action->state_set);
336     if (((surf_action_network_CM02_t) action)->variable)
337       lmm_variable_free(network_maxmin_system,
338                         ((surf_action_network_CM02_t) action)->variable);
339     free(action);
340     return 1;
341   }
342   return 0;
343 }
344
345 static void action_use(surf_action_t action)
346 {
347   action->using++;
348 }
349
350 static void action_cancel(surf_action_t action)
351 {
352   return;
353 }
354
355 static void action_recycle(surf_action_t action)
356 {
357   return;
358 }
359
360 static void action_change_state(surf_action_t action,
361                                 e_surf_action_state_t state)
362 {
363 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
364 /*     if(((surf_action_network_CM02_t)action)->variable) { */
365 /*       lmm_variable_disable(network_maxmin_system, ((surf_action_network_CM02_t)action)->variable); */
366 /*       ((surf_action_network_CM02_t)action)->variable = NULL; */
367 /*     } */
368
369   surf_action_change_state(action, state);
370   return;
371 }
372
373 static double share_resources(double now)
374 {
375   s_surf_action_network_CM02_t s_action;
376   surf_action_network_CM02_t action = NULL;
377   xbt_swag_t running_actions =
378       surf_network_model->common_public->states.running_action_set;
379   double min;
380
381   min = generic_maxmin_share_resources(running_actions,
382                                        xbt_swag_offset(s_action,
383                                                        variable),
384                                        network_maxmin_system,
385                                        network_solve);
386
387 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
388   
389   xbt_swag_foreach(action, running_actions) {
390     if (action->latency > 0) {
391       if (min < 0)
392         min = action->latency;
393       else if (action->latency < min)
394         min = action->latency;
395     }
396   }
397
398   return min;
399 }
400
401 static void update_actions_state(double now, double delta)
402 {
403   double deltap = 0.0;
404   surf_action_network_CM02_t action = NULL;
405   surf_action_network_CM02_t next_action = NULL;
406   xbt_swag_t running_actions =
407       surf_network_model->common_public->states.running_action_set;
408   /*
409      xbt_swag_t failed_actions =
410      surf_network_model->common_public->states.failed_action_set;
411    */
412
413   xbt_swag_foreach_safe(action, next_action, running_actions) {
414     deltap = delta;
415     if (action->latency > 0) {
416       if (action->latency > deltap) {
417         double_update(&(action->latency), deltap);
418         deltap = 0.0;
419       } else {
420         double_update(&(deltap), action->latency);
421         action->latency = 0.0;
422       }
423       if ((action->latency == 0.0) && !(action->suspended))
424         lmm_update_variable_weight(network_maxmin_system, action->variable,
425                                    action->lat_current);
426     }
427     double_update(&(action->generic_action.remains),
428                   lmm_variable_getvalue(action->variable) * deltap);
429     if (action->generic_action.max_duration != NO_MAX_DURATION)
430       double_update(&(action->generic_action.max_duration), delta);
431
432     if ((action->generic_action.remains <= 0) &&
433         (lmm_get_variable_weight(action->variable) > 0)) {
434       action->generic_action.finish = surf_get_clock();
435       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
436     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
437                (action->generic_action.max_duration <= 0)) {
438       action->generic_action.finish = surf_get_clock();
439       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
440     }
441   }
442
443   return;
444 }
445
446 static void update_resource_state(void *id,
447                                   tmgr_trace_event_t event_type,
448                                   double value, double date)
449 {
450   link_CM02_t nw_link = id;
451   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
452   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
453   /*     value, event_type); */
454
455   if (event_type == nw_link->bw_event) {
456     nw_link->bw_current = value;
457     lmm_update_constraint_bound(network_maxmin_system, nw_link->constraint,
458                                 nw_link->bw_current);
459   } else if (event_type == nw_link->lat_event) {
460     double delta = value - nw_link->lat_current;
461     lmm_variable_t var = NULL;
462     lmm_element_t elem = NULL;
463     surf_action_network_CM02_t action = NULL;
464
465     nw_link->lat_current = value;
466     while ((var= lmm_get_var_from_cnst
467            (network_maxmin_system, nw_link->constraint, &elem))) {
468       action = lmm_variable_id(var);
469       action->lat_current += delta;
470       if (action->rate < 0)
471         lmm_update_variable_bound(network_maxmin_system, action->variable,
472                                   SG_TCP_CTE_GAMMA / (2.0 *
473                                                       action->
474                                                       lat_current));
475       else
476         lmm_update_variable_bound(network_maxmin_system, action->variable,
477                                   min(action->rate,
478                                       SG_TCP_CTE_GAMMA / (2.0 *
479                                                           action->
480                                                           lat_current)));
481       if (!(action->suspended))
482         lmm_update_variable_weight(network_maxmin_system, action->variable,
483                                    action->lat_current);
484       lmm_update_variable_latency(network_maxmin_system, action->variable,
485                                   delta);
486     }
487   } else if (event_type == nw_link->state_event) {
488     if (value > 0)
489       nw_link->state_current = SURF_LINK_ON;
490     else {
491       lmm_constraint_t cnst = nw_link->constraint;
492       lmm_variable_t var = NULL;
493       lmm_element_t elem = NULL;
494
495       nw_link->state_current = SURF_LINK_OFF;
496       while ((var= lmm_get_var_from_cnst
497               (network_maxmin_system, cnst, &elem))) {
498         surf_action_t action = lmm_variable_id(var) ;
499
500         if(surf_action_get_state(action)==SURF_ACTION_RUNNING ||
501            surf_action_get_state(action)==SURF_ACTION_READY) {
502           action->finish = date;
503           action_change_state( action, SURF_ACTION_FAILED);
504         }
505       }
506     }
507   } else {
508     CRITICAL0("Unknown event ! \n");
509     xbt_abort();
510   }
511
512   return;
513 }
514
515 static surf_action_t communicate(void *src, void *dst, double size,
516                                  double rate)
517 {
518   surf_action_network_CM02_t action = NULL;
519   /* LARGE PLATFORMS HACK:
520      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
521      Use the cluster_id for ->id */
522   network_card_CM02_t card_src = src;
523   network_card_CM02_t card_dst = dst;
524   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
525   link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
526   /* LARGE PLATFORMS HACK:
527      total_route_size = route_size + src->link_nb + dst->nb */
528   int i;
529
530   XBT_IN4("(%s,%s,%g,%g)", card_src->name, card_dst->name, size, rate);
531   /* LARGE PLATFORMS HACK:
532      assert on total_route_size */
533   xbt_assert2(route_size,
534               "You're trying to send data from %s to %s but there is no connexion between these two cards.",
535               card_src->name, card_dst->name);
536
537   action = xbt_new0(s_surf_action_network_CM02_t, 1);
538
539   action->generic_action.using = 1;
540   action->generic_action.cost = size;
541   action->generic_action.remains = size;
542   action->generic_action.max_duration = NO_MAX_DURATION;
543   action->generic_action.start = surf_get_clock();
544   action->generic_action.finish = -1.0;
545   action->generic_action.model_type =
546       (surf_model_t) surf_network_model;
547   action->suspended = 0;        /* Should be useless because of the 
548                                    calloc but it seems to help valgrind... */
549   action->generic_action.state_set =
550       surf_network_model->common_public->states.running_action_set;
551   for (i = 0; i < route_size; i++) 
552     if(route[i]->state_current == SURF_LINK_OFF) {
553       action->generic_action.state_set =
554         surf_network_model->common_public->states.failed_action_set;
555       break;
556     }
557   
558   xbt_swag_insert(action, action->generic_action.state_set);
559   action->rate = rate;
560
561   action->latency = 0.0;
562   for (i = 0; i < route_size; i++)
563     action->latency += route[i]->lat_current;
564   /* LARGE PLATFORMS HACK:
565      Add src->link and dst->link latencies */
566   action->lat_current = action->latency;
567
568   /* LARGE PLATFORMS HACK:
569      lmm_variable_new(..., total_route_size)*/
570   if (action->latency > 0)
571     action->variable =
572         lmm_variable_new(network_maxmin_system, action, 0.0, -1.0,
573                          route_size);
574   else
575     action->variable =
576         lmm_variable_new(network_maxmin_system, action, 1.0, -1.0,
577                          route_size);
578
579   if (action->rate < 0) {
580     if (action->lat_current > 0)
581       lmm_update_variable_bound(network_maxmin_system, action->variable,
582                                 SG_TCP_CTE_GAMMA / (2.0 *
583                                                     action->lat_current));
584     else
585       lmm_update_variable_bound(network_maxmin_system, action->variable,
586                                 -1.0);
587   } else {
588     if (action->lat_current > 0)
589       lmm_update_variable_bound(network_maxmin_system, action->variable,
590                                 min(action->rate,
591                                     SG_TCP_CTE_GAMMA / (2.0 *
592                                                         action->
593                                                         lat_current)));
594     else
595       lmm_update_variable_bound(network_maxmin_system, action->variable,
596                                 action->rate);
597   }
598   lmm_update_variable_latency(network_maxmin_system, action->variable,
599                               action->latency);
600
601   for (i = 0; i < route_size; i++)
602     lmm_expand(network_maxmin_system, route[i]->constraint,
603                action->variable, 1.0);
604   /* LARGE PLATFORMS HACK:
605      expand also with src->link and dst->link */
606
607   XBT_OUT;
608
609   return (surf_action_t) action;
610 }
611
612 /* returns an array of link_CM02_t */
613 static const void **get_route(void *src, void *dst)
614 {
615   network_card_CM02_t card_src = src;
616   network_card_CM02_t card_dst = dst;
617   return (const void **) ROUTE(card_src->id, card_dst->id);
618 }
619
620 static int get_route_size(void *src, void *dst)
621 {
622   network_card_CM02_t card_src = src;
623   network_card_CM02_t card_dst = dst;
624   return ROUTE_SIZE(card_src->id, card_dst->id);
625 }
626
627 static const char *get_link_name(const void *link)
628 {
629   return ((link_CM02_t) link)->name;
630 }
631
632 static double get_link_bandwidth(const void *link)
633 {
634   return ((link_CM02_t) link)->bw_current;
635 }
636
637 static double get_link_latency(const void *link)
638 {
639   return ((link_CM02_t) link)->lat_current;
640 }
641
642 static xbt_dict_t get_properties(void *link)
643 {
644  return ((link_CM02_t) link)->properties;
645 }
646
647 static void action_suspend(surf_action_t action)
648 {
649   ((surf_action_network_CM02_t) action)->suspended = 1;
650   lmm_update_variable_weight(network_maxmin_system,
651                              ((surf_action_network_CM02_t) action)->
652                              variable, 0.0);
653 }
654
655 static void action_resume(surf_action_t action)
656 {
657   if (((surf_action_network_CM02_t) action)->suspended) {
658     lmm_update_variable_weight(network_maxmin_system,
659                                ((surf_action_network_CM02_t) action)->
660                                variable,
661                                ((surf_action_network_CM02_t) action)->
662                                lat_current);
663     ((surf_action_network_CM02_t) action)->suspended = 0;
664   }
665 }
666
667 static int action_is_suspended(surf_action_t action)
668 {
669   return ((surf_action_network_CM02_t) action)->suspended;
670 }
671
672 static void action_set_max_duration(surf_action_t action, double duration)
673 {
674   action->max_duration = duration;
675 }
676
677 static void finalize(void)
678 {
679   int i, j;
680
681   xbt_dict_free(&network_card_set);
682   xbt_dict_free(&link_set);
683   xbt_swag_free(surf_network_model->common_public->states.
684                 ready_action_set);
685   xbt_swag_free(surf_network_model->common_public->states.
686                 running_action_set);
687   xbt_swag_free(surf_network_model->common_public->states.
688                 failed_action_set);
689   xbt_swag_free(surf_network_model->common_public->states.
690                 done_action_set);
691   free(surf_network_model->common_public);
692   free(surf_network_model->common_private);
693   free(surf_network_model->extension_public);
694
695   free(surf_network_model);
696   surf_network_model = NULL;
697
698   loopback = NULL;
699   for (i = 0; i < host_number; i++)
700     for (j = 0; j < host_number; j++)
701       free(ROUTE(i, j));
702   free(routing_table);
703   routing_table = NULL;
704   free(routing_table_size);
705   routing_table_size = NULL;
706   host_number = 0;
707 }
708
709 static void surf_network_model_init_internal(void)
710 {
711   s_surf_action_t action;
712
713   surf_network_model = xbt_new0(s_surf_network_model_t, 1);
714
715   surf_network_model->common_private =
716       xbt_new0(s_surf_model_private_t, 1);
717   surf_network_model->common_public =
718       xbt_new0(s_surf_model_public_t, 1);
719   surf_network_model->extension_public =
720       xbt_new0(s_surf_network_model_extension_public_t, 1);
721
722   surf_network_model->common_public->states.ready_action_set =
723       xbt_swag_new(xbt_swag_offset(action, state_hookup));
724   surf_network_model->common_public->states.running_action_set =
725       xbt_swag_new(xbt_swag_offset(action, state_hookup));
726   surf_network_model->common_public->states.failed_action_set =
727       xbt_swag_new(xbt_swag_offset(action, state_hookup));
728   surf_network_model->common_public->states.done_action_set =
729       xbt_swag_new(xbt_swag_offset(action, state_hookup));
730
731   surf_network_model->common_public->name_service = name_service;
732   surf_network_model->common_public->get_resource_name =
733       get_resource_name;
734   surf_network_model->common_public->action_get_state =
735       surf_action_get_state;
736   surf_network_model->common_public->action_get_start_time =
737       surf_action_get_start_time;
738   surf_network_model->common_public->action_get_finish_time =
739       surf_action_get_finish_time;
740   surf_network_model->common_public->action_free = action_free;
741   surf_network_model->common_public->action_use = action_use;
742   surf_network_model->common_public->action_cancel = action_cancel;
743   surf_network_model->common_public->action_recycle = action_recycle;
744   surf_network_model->common_public->action_change_state =
745       action_change_state;
746   surf_network_model->common_public->action_set_data =
747       surf_action_set_data;
748   surf_network_model->common_public->name = "network";
749
750   surf_network_model->common_private->resource_used = resource_used;
751   surf_network_model->common_private->share_resources = share_resources;
752   surf_network_model->common_private->update_actions_state =
753       update_actions_state;
754   surf_network_model->common_private->update_resource_state =
755       update_resource_state;
756   surf_network_model->common_private->finalize = finalize;
757
758   surf_network_model->common_public->suspend = action_suspend;
759   surf_network_model->common_public->resume = action_resume;
760   surf_network_model->common_public->is_suspended = action_is_suspended;
761   surf_cpu_model->common_public->set_max_duration =
762       action_set_max_duration;
763
764   surf_network_model->extension_public->communicate = communicate;
765   surf_network_model->extension_public->get_route = get_route;
766   surf_network_model->extension_public->get_route_size = get_route_size;
767   surf_network_model->extension_public->get_link_name = get_link_name;
768   surf_network_model->extension_public->get_link_bandwidth =
769       get_link_bandwidth;
770   surf_network_model->extension_public->get_link_latency =
771       get_link_latency;
772
773   surf_network_model->common_public->get_properties =  get_properties;
774
775   link_set = xbt_dict_new();
776   network_card_set = xbt_dict_new();
777
778   if (!network_maxmin_system)
779     network_maxmin_system = lmm_system_new();
780 }
781
782 /***************************************************************************/
783 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
784 /***************************************************************************/
785 /* @TechReport{      rr-lip2002-40, */
786 /*   author        = {Henri Casanova and Loris Marchal}, */
787 /*   institution   = {LIP}, */
788 /*   title         = {A Network Model for Simulation of Grid Application}, */
789 /*   number        = {2002-40}, */
790 /*   month         = {oct}, */
791 /*   year          = {2002} */
792 /* } */
793 void surf_network_model_init_CM02(const char *filename)
794 {
795
796   if (surf_network_model)
797     return;
798   surf_network_model_init_internal();
799   define_callbacks(filename);
800   xbt_dynar_push(model_list, &surf_network_model);
801   network_solve = lmm_solve;
802
803   update_model_description(surf_network_model_description,
804                               surf_network_model_description_size,
805                               "CM02",
806                               (surf_model_t) surf_network_model);
807 }
808
809 void surf_network_model_init_Reno(const char *filename)
810 {
811   if (surf_network_model)
812     return;
813   surf_network_model_init_internal();
814   define_callbacks(filename);
815
816   xbt_dynar_push(model_list, &surf_network_model);
817   lmm_set_default_protocol_function(func_reno_f, func_reno_fp,
818                                     func_reno_fpi);
819   network_solve = lagrange_solve;
820
821   update_model_description(surf_network_model_description,
822                               surf_network_model_description_size,
823                               "Reno",
824                               (surf_model_t) surf_network_model);
825 }
826
827
828 void surf_network_model_init_Reno2(const char *filename)
829 {
830   if (surf_network_model)
831     return;
832   surf_network_model_init_internal();
833   define_callbacks(filename);
834
835   xbt_dynar_push(model_list, &surf_network_model);
836   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
837                                     func_reno2_fpi);
838   network_solve = lagrange_solve;
839
840   update_model_description(surf_network_model_description,
841                               surf_network_model_description_size,
842                               "Reno2",
843                               (surf_model_t) surf_network_model);
844 }
845
846 void surf_network_model_init_Vegas(const char *filename)
847 {
848   if (surf_network_model)
849     return;
850   surf_network_model_init_internal();
851   define_callbacks(filename);
852
853   xbt_dynar_push(model_list, &surf_network_model);
854   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
855                                     func_vegas_fpi);
856   network_solve = lagrange_solve;
857
858   update_model_description(surf_network_model_description,
859                               surf_network_model_description_size,
860                               "Vegas",
861                               (surf_model_t) surf_network_model);
862 }
863
864 #ifdef HAVE_SDP
865 void surf_network_model_init_SDP(const char *filename)
866 {
867   if (surf_network_model)
868     return;
869   surf_network_model_init_internal();
870   define_callbacks(filename);
871
872   xbt_dynar_push(model_list, &surf_network_model);
873   network_solve = sdp_solve;
874
875   update_model_description(surf_network_model_description,
876                               surf_network_model_description_size,
877                               "SDP",
878                               (surf_model_t) surf_network_model);
879 }
880 #endif
881
882 #ifdef HAVE_GTNETS
883 void surf_network_model_init_GTNETS(const char *filename)
884 {
885   if (surf_network_model)
886     return;
887   surf_network_model_init_internal();
888   define_callbacks(filename);
889   xbt_dynar_push(model_list, &surf_network_model);
890
891   update_model_description(surf_network_model_description,
892                               surf_network_model_description_size,
893                               "GTNets",
894                               (surf_model_t) surf_network_model);
895 }
896 #endif