Logo AND Algorithmique Numérique Distribuée

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