Logo AND Algorithmique Numérique Distribuée

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