Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
routing table goes public
[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
10 #define SG_TCP_CTE_GAMMA 20000.0
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(network, surf,
13                                 "Logging specific to the SURF network module");
14
15 surf_network_resource_t surf_network_resource = NULL;
16
17 static xbt_dict_t network_link_set = NULL;
18 xbt_dict_t network_card_set = NULL;
19
20 int card_number = 0;
21 network_link_CM02_t **routing_table = NULL;
22 int *routing_table_size = NULL;
23
24 static void create_routing_table(void)
25 {
26   routing_table = xbt_new0(network_link_CM02_t *, card_number * card_number);
27   routing_table_size = xbt_new0(int, card_number * card_number);
28 }
29
30 static void network_link_free(void *nw_link)
31 {
32   free(((network_link_CM02_t)nw_link)->name);
33   free(nw_link);
34 }
35
36 static network_link_CM02_t network_link_new(char *name,
37                                        double bw_initial,
38                                        tmgr_trace_t bw_trace,
39                                        double lat_initial,
40                                        tmgr_trace_t lat_trace,
41                                        e_surf_network_link_state_t
42                                        state_initial,
43                                        tmgr_trace_t state_trace)
44 {
45   network_link_CM02_t nw_link = xbt_new0(s_network_link_CM02_t, 1);
46
47
48   nw_link->resource = (surf_resource_t) surf_network_resource;
49   nw_link->name = name;
50   nw_link->bw_current = bw_initial;
51   if (bw_trace)
52     nw_link->bw_event =
53         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
54   nw_link->lat_current = lat_initial;
55   if (lat_trace)
56     nw_link->lat_event =
57         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
58   nw_link->state_current = state_initial;
59   if (state_trace)
60     nw_link->state_event =
61         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
62
63   nw_link->constraint =
64       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
65
66   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
67
68   return nw_link;
69 }
70
71 static void network_card_free(void *nw_card)
72 {
73   free(((network_card_CM02_t)nw_card)->name);
74   free(nw_card);
75 }
76
77 static int network_card_new(const char *card_name)
78 {
79   network_card_CM02_t card = NULL;
80
81   xbt_dict_get(network_card_set, card_name, (void *) &card);
82
83   if (!card) {
84     card = xbt_new0(s_network_card_CM02_t, 1);
85     card->name = xbt_strdup(card_name);
86     card->id = card_number++;
87     xbt_dict_set(network_card_set, card_name, card, network_card_free);
88   }
89   return card->id;
90 }
91
92 static void route_new(int src_id, int dst_id, char **links, int nb_link)
93 {
94   network_link_CM02_t *link_list = NULL;
95   int i;
96
97   ROUTE_SIZE(src_id, dst_id) = nb_link;
98   link_list = (ROUTE(src_id, dst_id) = xbt_new0(network_link_CM02_t, nb_link));
99   for (i = 0; i < nb_link; i++) {
100     xbt_dict_get(network_link_set, links[i], (void *) &(link_list[i]));
101     free(links[i]);
102   }
103   free(links);
104 }
105
106 static void parse_network_link(void)
107 {
108   char *name;
109   double bw_initial;
110   tmgr_trace_t bw_trace;
111   double lat_initial;
112   tmgr_trace_t lat_trace;
113   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
114   tmgr_trace_t state_trace;
115
116   name = xbt_strdup(A_network_link_name);
117   surf_parse_get_double(&bw_initial,A_network_link_bandwidth);
118   surf_parse_get_trace(&bw_trace, A_network_link_bandwidth_file);
119   surf_parse_get_double(&lat_initial,A_network_link_latency);
120   surf_parse_get_trace(&lat_trace, A_network_link_latency_file);
121
122   xbt_assert0((A_network_link_state==A_network_link_state_ON)||
123               (A_network_link_state==A_network_link_state_OFF),
124               "Invalid state")
125   if (A_network_link_state==A_network_link_state_ON) 
126     state_initial = SURF_NETWORK_LINK_ON;
127   if (A_network_link_state==A_network_link_state_OFF) 
128     state_initial = SURF_NETWORK_LINK_OFF;
129   surf_parse_get_trace(&state_trace,A_network_link_state_file);
130
131   network_link_new(name, bw_initial, bw_trace,
132                    lat_initial, lat_trace, state_initial, state_trace);
133 }
134
135 static int nb_link = 0;
136 static char **link_name = NULL;
137 static int src_id = -1;
138 static int dst_id = -1;
139
140 static void parse_route_set_endpoints(void)
141 {
142   src_id = network_card_new(A_route_src);
143   dst_id = network_card_new(A_route_dst);
144   nb_link = 0;
145   link_name = NULL;
146 }
147
148 static void parse_route_elem(void)
149 {
150   nb_link++;
151   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
152   link_name[(nb_link) - 1] = xbt_strdup(A_route_element_name);
153 }
154
155 static void parse_route_set_route(void)
156 {
157   route_new(src_id, dst_id, link_name, nb_link);
158 }
159
160 static void parse_file(const char *file)
161 {
162   /* Figuring out the network links */
163   surf_parse_reset_parser();
164   ETag_network_link_fun=parse_network_link;
165   surf_parse_open(file);
166   xbt_assert1((!surf_parse()),"Parse error in %s",file);
167   surf_parse_close();
168
169   /* Figuring out the network cards used */
170   surf_parse_reset_parser();
171   STag_route_fun=parse_route_set_endpoints;
172   surf_parse_open(file);
173   xbt_assert1((!surf_parse()),"Parse error in %s",file);
174   surf_parse_close();
175
176   create_routing_table();
177
178   /* Building the routes */
179   surf_parse_reset_parser();
180   STag_route_fun=parse_route_set_endpoints;
181   ETag_route_element_fun=parse_route_elem;
182   ETag_route_fun=parse_route_set_route;
183   surf_parse_open(file);
184   xbt_assert1((!surf_parse()),"Parse error in %s",file);
185   surf_parse_close();
186 }
187
188 static void *name_service(const char *name)
189 {
190   network_card_CM02_t card = NULL;
191
192   xbt_dict_get(network_card_set, name, (void *) &card);
193
194   return card;
195 }
196
197 static const char *get_resource_name(void *resource_id)
198 {
199   return ((network_card_CM02_t) resource_id)->name;
200 }
201
202 static int resource_used(void *resource_id)
203 {
204   return lmm_constraint_used(maxmin_system,
205                              ((network_link_CM02_t) resource_id)->constraint);
206 }
207
208 static int action_free(surf_action_t action)
209 {
210   action->using--;
211   if(!action->using) {
212     xbt_swag_remove(action, action->state_set);
213     if(((surf_action_network_CM02_t)action)->variable)
214       lmm_variable_free(maxmin_system, ((surf_action_network_CM02_t)action)->variable);
215     free(action);
216     return 1;
217   }
218   return 0;
219 }
220
221 static void action_use(surf_action_t action)
222 {
223   action->using++;
224 }
225
226 static void action_cancel(surf_action_t action)
227 {
228   return;
229 }
230
231 static void action_recycle(surf_action_t action)
232 {
233   return;
234 }
235
236 static void action_change_state(surf_action_t action,
237                                 e_surf_action_state_t state)
238 {
239 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
240 /*     if(((surf_action_network_CM02_t)action)->variable) { */
241 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_CM02_t)action)->variable); */
242 /*       ((surf_action_network_CM02_t)action)->variable = NULL; */
243 /*     } */
244
245   surf_action_change_state(action, state);
246   return;
247 }
248
249 static double share_resources(double now)
250 {
251   s_surf_action_network_CM02_t s_action;
252   surf_action_network_CM02_t action = NULL;
253   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
254   double min = generic_maxmin_share_resources(running_actions,
255                                               xbt_swag_offset(s_action, variable));
256
257   xbt_swag_foreach(action, running_actions) {
258     if(action->latency>0) {
259       if(min<0) min = action->latency;
260       else if (action->latency<min) min = action->latency;
261     }
262   }
263
264   return min;
265 }
266
267 static void update_actions_state(double now, double delta)
268 {
269   double deltap = 0.0;
270   surf_action_network_CM02_t action = NULL;
271   surf_action_network_CM02_t next_action = NULL;
272   xbt_swag_t running_actions =
273       surf_network_resource->common_public->states.running_action_set;
274   xbt_swag_t failed_actions =
275       surf_network_resource->common_public->states.failed_action_set;
276
277   xbt_swag_foreach_safe(action, next_action, running_actions) {
278     deltap = delta;
279     if (action->latency > 0) {
280       if (action->latency > deltap) {
281         surf_double_update(&(action->latency),deltap);
282         deltap = 0.0;
283       } else {
284         surf_double_update(&(deltap), action->latency);
285         action->latency = 0.0;
286       }
287       if ((action->latency == 0.0) && !(action->suspended)) 
288         lmm_update_variable_weight(maxmin_system, action->variable, 1.0);
289     }
290     surf_double_update(&(action->generic_action.remains),
291         lmm_variable_getvalue(action->variable) * deltap);
292     if (action->generic_action.max_duration != NO_MAX_DURATION)
293       surf_double_update(&(action->generic_action.max_duration), delta);
294
295     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
296
297     if (action->generic_action.remains <= 0) {
298       action->generic_action.finish = surf_get_clock();
299       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
300     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
301                (action->generic_action.max_duration <= 0)) {
302       action->generic_action.finish = surf_get_clock();
303       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
304     } else {                    /* Need to check that none of the resource has failed */
305       lmm_constraint_t cnst = NULL;
306       int i = 0;
307       network_link_CM02_t nw_link = NULL;
308
309       while ((cnst =
310               lmm_get_cnst_from_var(maxmin_system, action->variable,
311                                     i++))) {
312         nw_link = lmm_constraint_id(cnst);
313         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
314           action->generic_action.finish = surf_get_clock();
315           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
316           break;
317         }
318       }
319     }
320   }
321
322   return;
323 }
324
325 static void update_resource_state(void *id,
326                                   tmgr_trace_event_t event_type,
327                                   double value)
328 {
329   network_link_CM02_t nw_link = id;
330   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
331   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
332   /*     value, event_type); */
333
334   if (event_type == nw_link->bw_event) {
335     nw_link->bw_current = value;
336     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
337                                 nw_link->bw_current);
338   } else if (event_type == nw_link->lat_event) {
339     double delta = value - nw_link->lat_current;
340     lmm_variable_t var = NULL;
341     surf_action_network_CM02_t action = NULL;
342
343     nw_link->lat_current = value;
344     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
345       action = lmm_variable_id(var);
346       action->lat_current += delta;
347       if(action->rate<0)
348         lmm_update_variable_bound(maxmin_system, action->variable,
349                                   SG_TCP_CTE_GAMMA / action->lat_current);
350       else 
351         lmm_update_variable_bound(maxmin_system, action->variable,
352                                   min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
353     }
354   } else if (event_type == nw_link->state_event) {
355     if (value > 0)
356       nw_link->state_current = SURF_NETWORK_LINK_ON;
357     else
358       nw_link->state_current = SURF_NETWORK_LINK_OFF;
359   } else {
360     CRITICAL0("Unknown event ! \n");
361     xbt_abort();
362   }
363
364   return;
365 }
366
367 static surf_action_t communicate(void *src, void *dst, double size, double rate)
368 {
369   surf_action_network_CM02_t action = NULL;
370   network_card_CM02_t card_src = src;
371   network_card_CM02_t card_dst = dst;
372   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
373   network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
374   int i;
375
376   action = xbt_new0(s_surf_action_network_CM02_t, 1);
377
378   action->generic_action.using = 1;
379   action->generic_action.cost = size;
380   action->generic_action.remains = size;
381   action->generic_action.max_duration = NO_MAX_DURATION;
382   action->generic_action.start = -1.0;
383   action->generic_action.finish = -1.0;
384   action->generic_action.resource_type =
385       (surf_resource_t) surf_network_resource;
386   action->suspended = 0;  /* Should be useless because of the 
387                              calloc but it seems to help valgrind... */
388   action->generic_action.state_set =
389       surf_network_resource->common_public->states.running_action_set;
390
391   xbt_swag_insert(action, action->generic_action.state_set);
392   action->rate = rate;
393
394   action->latency = 0.0;
395   for (i = 0; i < route_size; i++)
396     action->latency += route[i]->lat_current;
397   action->lat_current = action->latency;
398
399   if(action->latency>0)
400     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
401                                         route_size);
402   else 
403     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
404                                         route_size);
405
406   if(action->rate<0)
407     lmm_update_variable_bound(maxmin_system, action->variable,
408                               SG_TCP_CTE_GAMMA / action->lat_current);
409   else 
410     lmm_update_variable_bound(maxmin_system, action->variable,
411                               min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
412
413   if(route_size == 0) {
414     action_change_state((surf_action_t) action, SURF_ACTION_DONE);
415   }
416
417   for (i = 0; i < route_size; i++)
418     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
419
420   return (surf_action_t) action;
421 }
422
423 static void action_suspend(surf_action_t action)
424 {
425   ((surf_action_network_CM02_t) action)->suspended = 1;
426   lmm_update_variable_weight(maxmin_system,
427                              ((surf_action_network_CM02_t) action)->variable, 0.0);
428 }
429
430 static void action_resume(surf_action_t action)
431 {
432   lmm_update_variable_weight(maxmin_system,
433                              ((surf_action_network_CM02_t) action)->variable, 1.0);
434   ((surf_action_network_CM02_t) action)->suspended = 0;
435 }
436
437 static int action_is_suspended(surf_action_t action)
438 {
439   return ((surf_action_network_CM02_t) action)->suspended;
440 }
441
442 static void action_set_max_duration(surf_action_t action, double duration)
443 {
444   action->max_duration = duration;
445 }
446
447 static void finalize(void)
448 {
449   int i,j;
450
451   xbt_dict_free(&network_card_set);
452   xbt_dict_free(&network_link_set);
453   xbt_swag_free(surf_network_resource->common_public->states.
454                 ready_action_set);
455   xbt_swag_free(surf_network_resource->common_public->states.
456                 running_action_set);
457   xbt_swag_free(surf_network_resource->common_public->states.
458                 failed_action_set);
459   xbt_swag_free(surf_network_resource->common_public->states.
460                 done_action_set);
461   free(surf_network_resource->common_public);
462   free(surf_network_resource->common_private);
463   free(surf_network_resource->extension_public);
464
465   free(surf_network_resource);
466   surf_network_resource = NULL;
467
468   for (i = 0; i < card_number; i++) 
469     for (j = 0; j < card_number; j++) 
470       free(ROUTE(i,j));
471   free(routing_table);
472   routing_table = NULL;
473   free(routing_table_size);
474   routing_table_size = NULL;
475   card_number = 0;
476 }
477
478 static void surf_network_resource_init_internal(void)
479 {
480   s_surf_action_t action;
481
482   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
483
484   surf_network_resource->common_private =
485       xbt_new0(s_surf_resource_private_t, 1);
486   surf_network_resource->common_public =
487       xbt_new0(s_surf_resource_public_t, 1);
488   surf_network_resource->extension_public =
489       xbt_new0(s_surf_network_resource_extension_public_t, 1);
490
491   surf_network_resource->common_public->states.ready_action_set =
492       xbt_swag_new(xbt_swag_offset(action, state_hookup));
493   surf_network_resource->common_public->states.running_action_set =
494       xbt_swag_new(xbt_swag_offset(action, state_hookup));
495   surf_network_resource->common_public->states.failed_action_set =
496       xbt_swag_new(xbt_swag_offset(action, state_hookup));
497   surf_network_resource->common_public->states.done_action_set =
498       xbt_swag_new(xbt_swag_offset(action, state_hookup));
499
500   surf_network_resource->common_public->name_service = name_service;
501   surf_network_resource->common_public->get_resource_name =
502       get_resource_name;
503   surf_network_resource->common_public->action_get_state =
504       surf_action_get_state;
505   surf_network_resource->common_public->action_free = action_free;
506   surf_network_resource->common_public->action_use = action_use;
507   surf_network_resource->common_public->action_cancel = action_cancel;
508   surf_network_resource->common_public->action_recycle = action_recycle;
509   surf_network_resource->common_public->action_change_state =
510       action_change_state;
511   surf_network_resource->common_public->action_set_data = surf_action_set_data;
512   surf_network_resource->common_public->name = "network";
513
514   surf_network_resource->common_private->resource_used = resource_used;
515   surf_network_resource->common_private->share_resources = share_resources;
516   surf_network_resource->common_private->update_actions_state =
517       update_actions_state;
518   surf_network_resource->common_private->update_resource_state =
519       update_resource_state;
520   surf_network_resource->common_private->finalize = finalize;
521
522   surf_network_resource->common_public->suspend = action_suspend;
523   surf_network_resource->common_public->resume = action_resume;
524   surf_network_resource->common_public->is_suspended = action_is_suspended;
525   surf_cpu_resource->common_public->set_max_duration = action_set_max_duration;
526
527   surf_network_resource->extension_public->communicate = communicate;
528
529   network_link_set = xbt_dict_new();
530   network_card_set = xbt_dict_new();
531
532   xbt_assert0(maxmin_system, "surf_init has to be called first!");
533 }
534
535 /***************************************************************************/
536 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
537 /***************************************************************************/
538 /* @TechReport{      rr-lip2002-40, */
539 /*   author        = {Henri Casanova and Loris Marchal}, */
540 /*   institution   = {LIP}, */
541 /*   title         = {A Network Model for Simulation of Grid Application}, */
542 /*   number        = {2002-40}, */
543 /*   month         = {oct}, */
544 /*   year          = {2002} */
545 /* } */
546 void surf_network_resource_init_CM02(const char *filename)
547 {
548   if (surf_network_resource)
549     return;
550   surf_network_resource_init_internal();
551   parse_file(filename);
552   xbt_dynar_push(resource_list, &surf_network_resource);
553 }