Logo AND Algorithmique Numérique Distribuée

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