Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
edb762e6ec7820890f324cc9ba55366b289974e0
[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(surf_network, surf,
12                                 "Logging specific to the SURF network module");
13
14 surf_network_resource_t surf_network_resource = NULL;
15
16 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, 
315                                    action->lat_current);
316     }
317     surf_double_update(&(action->generic_action.remains),
318         lmm_variable_getvalue(action->variable) * deltap);
319     if (action->generic_action.max_duration != NO_MAX_DURATION)
320       surf_double_update(&(action->generic_action.max_duration), delta);
321
322     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
323
324     if ((action->generic_action.remains <= 0) && 
325         (lmm_get_variable_weight(action->variable)>0)) {
326       action->generic_action.finish = surf_get_clock();
327       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
328     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
329                (action->generic_action.max_duration <= 0)) {
330       action->generic_action.finish = surf_get_clock();
331       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
332     } else {                    /* Need to check that none of the resource has failed */
333       lmm_constraint_t cnst = NULL;
334       int i = 0;
335       network_link_CM02_t nw_link = NULL;
336
337       while ((cnst =
338               lmm_get_cnst_from_var(maxmin_system, action->variable,
339                                     i++))) {
340         nw_link = lmm_constraint_id(cnst);
341         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
342           action->generic_action.finish = surf_get_clock();
343           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
344           break;
345         }
346       }
347     }
348   }
349
350   return;
351 }
352
353 static void update_resource_state(void *id,
354                                   tmgr_trace_event_t event_type,
355                                   double value)
356 {
357   network_link_CM02_t nw_link = id;
358   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
359   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
360   /*     value, event_type); */
361
362   if (event_type == nw_link->bw_event) {
363     nw_link->bw_current = value;
364     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
365                                 nw_link->bw_current);
366   } else if (event_type == nw_link->lat_event) {
367     double delta = value - nw_link->lat_current;
368     lmm_variable_t var = NULL;
369     surf_action_network_CM02_t action = NULL;
370
371     nw_link->lat_current = value;
372     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
373       action = lmm_variable_id(var);
374       action->lat_current += delta;
375       if(action->rate<0)
376         lmm_update_variable_bound(maxmin_system, action->variable,
377                                   SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
378       else 
379         lmm_update_variable_bound(maxmin_system, action->variable,
380                                   min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
381       if(!(action->suspended))
382         lmm_update_variable_weight(maxmin_system, action->variable, 
383                                    action->lat_current);
384     }
385   } else if (event_type == nw_link->state_event) {
386     if (value > 0)
387       nw_link->state_current = SURF_NETWORK_LINK_ON;
388     else
389       nw_link->state_current = SURF_NETWORK_LINK_OFF;
390   } else {
391     CRITICAL0("Unknown event ! \n");
392     xbt_abort();
393   }
394
395   return;
396 }
397
398 static surf_action_t communicate(void *src, void *dst, double size, double rate)
399 {
400   surf_action_network_CM02_t action = NULL;
401   network_card_CM02_t card_src = src;
402   network_card_CM02_t card_dst = dst;
403   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
404   network_link_CM02_t *route = ROUTE(card_src->id, card_dst->id);
405   int i;
406
407   XBT_IN4("(%s,%s,%g,%g)",card_src->name,card_dst->name,size,rate);
408   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);
409
410   action = xbt_new0(s_surf_action_network_CM02_t, 1);
411
412   action->generic_action.using = 1;
413   action->generic_action.cost = size;
414   action->generic_action.remains = size;
415   action->generic_action.max_duration = NO_MAX_DURATION;
416   action->generic_action.start = -1.0;
417   action->generic_action.finish = -1.0;
418   action->generic_action.resource_type =
419       (surf_resource_t) surf_network_resource;
420   action->suspended = 0;  /* Should be useless because of the 
421                              calloc but it seems to help valgrind... */
422   action->generic_action.state_set =
423       surf_network_resource->common_public->states.running_action_set;
424
425   xbt_swag_insert(action, action->generic_action.state_set);
426   action->rate = rate;
427
428   action->latency = 0.0;
429   for (i = 0; i < route_size; i++)
430     action->latency += route[i]->lat_current;
431   action->lat_current = action->latency;
432
433   if(action->latency>0)
434     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
435                                         route_size);
436   else 
437     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
438                                         route_size);
439
440   if(action->rate<0) {
441     if(action->lat_current>0)
442       lmm_update_variable_bound(maxmin_system, action->variable,
443                                 SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
444     else
445       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
446   } else {
447     if(action->lat_current>0)
448       lmm_update_variable_bound(maxmin_system, action->variable,
449                                 min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
450     else
451       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
452   }
453
454   for (i = 0; i < route_size; i++)
455     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
456   XBT_OUT;
457
458   return (surf_action_t) action;
459 }
460
461 static void* get_route(void *src, void *dst) {
462   /* TODO */
463
464   return NULL;
465 }
466
467 static void action_suspend(surf_action_t action)
468 {
469   ((surf_action_network_CM02_t) action)->suspended = 1;
470   lmm_update_variable_weight(maxmin_system,
471                              ((surf_action_network_CM02_t) action)->variable, 0.0);
472 }
473
474 static void action_resume(surf_action_t action)
475 {
476   lmm_update_variable_weight(maxmin_system,
477                              ((surf_action_network_CM02_t) action)->variable, 
478                              ((surf_action_network_CM02_t) action)->lat_current);
479   ((surf_action_network_CM02_t) action)->suspended = 0;
480 }
481
482 static int action_is_suspended(surf_action_t action)
483 {
484   return ((surf_action_network_CM02_t) action)->suspended;
485 }
486
487 static void action_set_max_duration(surf_action_t action, double duration)
488 {
489   action->max_duration = duration;
490 }
491
492 static void finalize(void)
493 {
494   int i,j;
495
496   xbt_dict_free(&network_card_set);
497   xbt_dict_free(&network_link_set);
498   xbt_swag_free(surf_network_resource->common_public->states.
499                 ready_action_set);
500   xbt_swag_free(surf_network_resource->common_public->states.
501                 running_action_set);
502   xbt_swag_free(surf_network_resource->common_public->states.
503                 failed_action_set);
504   xbt_swag_free(surf_network_resource->common_public->states.
505                 done_action_set);
506   free(surf_network_resource->common_public);
507   free(surf_network_resource->common_private);
508   free(surf_network_resource->extension_public);
509
510   free(surf_network_resource);
511   surf_network_resource = NULL;
512
513   loopback = NULL;
514   for (i = 0; i < card_number; i++) 
515     for (j = 0; j < card_number; j++) 
516       free(ROUTE(i,j));
517   free(routing_table);
518   routing_table = NULL;
519   free(routing_table_size);
520   routing_table_size = NULL;
521   card_number = 0;
522 }
523
524 static void surf_network_resource_init_internal(void)
525 {
526   s_surf_action_t action;
527
528   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
529
530   surf_network_resource->common_private =
531       xbt_new0(s_surf_resource_private_t, 1);
532   surf_network_resource->common_public =
533       xbt_new0(s_surf_resource_public_t, 1);
534   surf_network_resource->extension_public =
535       xbt_new0(s_surf_network_resource_extension_public_t, 1);
536
537   surf_network_resource->common_public->states.ready_action_set =
538       xbt_swag_new(xbt_swag_offset(action, state_hookup));
539   surf_network_resource->common_public->states.running_action_set =
540       xbt_swag_new(xbt_swag_offset(action, state_hookup));
541   surf_network_resource->common_public->states.failed_action_set =
542       xbt_swag_new(xbt_swag_offset(action, state_hookup));
543   surf_network_resource->common_public->states.done_action_set =
544       xbt_swag_new(xbt_swag_offset(action, state_hookup));
545
546   surf_network_resource->common_public->name_service = name_service;
547   surf_network_resource->common_public->get_resource_name =
548       get_resource_name;
549   surf_network_resource->common_public->action_get_state =
550       surf_action_get_state;
551   surf_network_resource->common_public->action_free = action_free;
552   surf_network_resource->common_public->action_use = action_use;
553   surf_network_resource->common_public->action_cancel = action_cancel;
554   surf_network_resource->common_public->action_recycle = action_recycle;
555   surf_network_resource->common_public->action_change_state =
556       action_change_state;
557   surf_network_resource->common_public->action_set_data = surf_action_set_data;
558   surf_network_resource->common_public->name = "network";
559
560   surf_network_resource->common_private->resource_used = resource_used;
561   surf_network_resource->common_private->share_resources = share_resources;
562   surf_network_resource->common_private->update_actions_state =
563       update_actions_state;
564   surf_network_resource->common_private->update_resource_state =
565       update_resource_state;
566   surf_network_resource->common_private->finalize = finalize;
567
568   surf_network_resource->common_public->suspend = action_suspend;
569   surf_network_resource->common_public->resume = action_resume;
570   surf_network_resource->common_public->is_suspended = action_is_suspended;
571   surf_cpu_resource->common_public->set_max_duration = action_set_max_duration;
572
573   surf_network_resource->extension_public->communicate = communicate;
574   surf_network_resource->extension_public->get_route = get_route;
575
576   network_link_set = xbt_dict_new();
577   network_card_set = xbt_dict_new();
578
579   xbt_assert0(maxmin_system, "surf_init has to be called first!");
580 }
581
582 /***************************************************************************/
583 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
584 /***************************************************************************/
585 /* @TechReport{      rr-lip2002-40, */
586 /*   author        = {Henri Casanova and Loris Marchal}, */
587 /*   institution   = {LIP}, */
588 /*   title         = {A Network Model for Simulation of Grid Application}, */
589 /*   number        = {2002-40}, */
590 /*   month         = {oct}, */
591 /*   year          = {2002} */
592 /* } */
593 void surf_network_resource_init_CM02(const char *filename)
594 {
595   if (surf_network_resource)
596     return;
597   surf_network_resource_init_internal();
598   parse_file(filename);
599   xbt_dynar_push(resource_list, &surf_network_resource);
600 }