Logo AND Algorithmique Numérique Distribuée

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