Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9a59eaf1b1f8083230273b79f9d4d53f85b31429
[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   surf_action_network_t Action = (surf_action_network_t) action;
214
215   xbt_swag_remove(action, action->state_set);
216   lmm_variable_free(maxmin_system, Action->variable);
217   xbt_free(action);
218
219   return;
220 }
221
222 static void action_cancel(surf_action_t action)
223 {
224   return;
225 }
226
227 static void action_recycle(surf_action_t action)
228 {
229   return;
230 }
231
232 static void action_change_state(surf_action_t action,
233                                 e_surf_action_state_t state)
234 {
235   surf_action_change_state(action, state);
236   return;
237 }
238
239 static double share_resources(double now)
240 {
241   s_surf_action_network_t s_action;
242   surf_action_network_t action = NULL;
243   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
244   double min = generic_maxmin_share_resources(running_actions,
245                                               xbt_swag_offset(s_action, variable));
246
247   xbt_swag_foreach(action, running_actions) {
248     if(action->latency>0) {
249       if(min<0) min = action->latency;
250       else if (action->latency<min) min = action->latency;
251     }
252   }
253
254   return min;
255 }
256
257
258 static void update_actions_state(double now, double delta)
259 {
260   double deltap = 0.0;
261   surf_action_network_t action = NULL;
262   surf_action_network_t next_action = NULL;
263   xbt_swag_t running_actions =
264       surf_network_resource->common_public->states.running_action_set;
265   xbt_swag_t failed_actions =
266       surf_network_resource->common_public->states.failed_action_set;
267
268   xbt_swag_foreach_safe(action, next_action, running_actions) {
269     deltap = delta;
270     if (action->latency > 0) {
271       if (action->latency > deltap) {
272         surf_double_update(&(action->latency),deltap);
273         deltap = 0.0;
274       } else {
275         surf_double_update(&(deltap), action->latency);
276         action->latency = 0.0;
277       }
278       if ((action->latency == 0.0) && !(action->suspended)) 
279         lmm_update_variable_weight(maxmin_system, action->variable, 1.0);
280     }
281     surf_double_update(&(action->generic_action.remains),
282         lmm_variable_getvalue(action->variable) * deltap);
283     if (action->generic_action.max_duration != NO_MAX_DURATION)
284       surf_double_update(&(action->generic_action.max_duration), delta);
285
286     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
287
288     if (action->generic_action.remains <= 0) {
289       action->generic_action.finish = surf_get_clock();
290       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
291     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
292                (action->generic_action.max_duration <= 0)) {
293       action->generic_action.finish = surf_get_clock();
294       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
295     } else {                    /* Need to check that none of the resource has failed */
296       lmm_constraint_t cnst = NULL;
297       int i = 0;
298       network_link_t nw_link = NULL;
299
300       while ((cnst =
301               lmm_get_cnst_from_var(maxmin_system, action->variable,
302                                     i++))) {
303         nw_link = lmm_constraint_id(cnst);
304         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
305           action->generic_action.finish = surf_get_clock();
306           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
307           break;
308         }
309       }
310     }
311   }
312
313   xbt_swag_foreach_safe(action, next_action, failed_actions) {
314     lmm_variable_disable(maxmin_system, action->variable);
315   }
316
317   return;
318 }
319
320 static void update_resource_state(void *id,
321                                   tmgr_trace_event_t event_type,
322                                   double value)
323 {
324   network_link_t nw_link = id;
325   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
326   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
327   /*     value, event_type); */
328
329   if (event_type == nw_link->bw_event) {
330     nw_link->bw_current = value;
331     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
332                                 nw_link->bw_current);
333   } else if (event_type == nw_link->lat_event) {
334     double delta = value - nw_link->lat_current;
335     lmm_variable_t var = NULL;
336     surf_action_network_t action = NULL;
337
338     nw_link->lat_current = value;
339     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
340       action = lmm_variable_id(var);
341       action->lat_current += delta;
342       if(action->rate<0)
343         lmm_update_variable_bound(maxmin_system, action->variable,
344                                   SG_TCP_CTE_GAMMA / action->lat_current);
345       else 
346         lmm_update_variable_bound(maxmin_system, action->variable,
347                                   min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
348     }
349   } else if (event_type == nw_link->state_event) {
350     if (value > 0)
351       nw_link->state_current = SURF_NETWORK_LINK_ON;
352     else
353       nw_link->state_current = SURF_NETWORK_LINK_OFF;
354   } else {
355     CRITICAL0("Unknown event ! \n");
356     xbt_abort();
357   }
358
359   return;
360 }
361
362 static surf_action_t communicate(void *src, void *dst, double size, double rate)
363 {
364   surf_action_network_t action = NULL;
365   network_card_t card_src = src;
366   network_card_t card_dst = dst;
367   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
368   network_link_t *route = ROUTE(card_src->id, card_dst->id);
369   int i;
370
371   action = xbt_new0(s_surf_action_network_t, 1);
372
373   action->generic_action.cost = size;
374   action->generic_action.remains = size;
375   action->generic_action.max_duration = NO_MAX_DURATION;
376   action->generic_action.start = -1.0;
377   action->generic_action.finish = -1.0;
378   action->generic_action.resource_type =
379       (surf_resource_t) surf_network_resource;
380
381   action->generic_action.state_set =
382       surf_network_resource->common_public->states.running_action_set;
383
384   xbt_swag_insert(action, action->generic_action.state_set);
385   action->rate = rate;
386
387   action->latency = 0.0;
388   for (i = 0; i < route_size; i++)
389     action->latency += route[i]->lat_current;
390   action->lat_current = action->latency;
391
392   if(action->latency>0)
393     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
394                                         route_size);
395   else 
396     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
397                                         route_size);
398
399   if(action->rate<0)
400     lmm_update_variable_bound(maxmin_system, action->variable,
401                               SG_TCP_CTE_GAMMA / action->lat_current);
402   else 
403     lmm_update_variable_bound(maxmin_system, action->variable,
404                               min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
405
406   if(route_size == 0) {
407     surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
408   }
409
410   for (i = 0; i < route_size; i++)
411     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
412
413   return (surf_action_t) action;
414 }
415
416 static void action_suspend(surf_action_t action)
417 {
418   ((surf_action_network_t) action)->suspended = 1;
419   lmm_update_variable_weight(maxmin_system,
420                              ((surf_action_network_t) action)->variable, 0.0);
421 }
422
423 static void action_resume(surf_action_t action)
424 {
425   lmm_update_variable_weight(maxmin_system,
426                              ((surf_action_network_t) action)->variable, 1.0);
427   ((surf_action_network_t) action)->suspended = 0;
428 }
429
430 static int action_is_suspended(surf_action_t action)
431 {
432   return ((surf_action_network_t) action)->suspended;
433 }
434
435 static void finalize(void)
436 {
437   int i,j;
438
439   xbt_dict_free(&network_card_set);
440   xbt_dict_free(&network_link_set);
441   xbt_swag_free(surf_network_resource->common_public->states.
442                 ready_action_set);
443   xbt_swag_free(surf_network_resource->common_public->states.
444                 running_action_set);
445   xbt_swag_free(surf_network_resource->common_public->states.
446                 failed_action_set);
447   xbt_swag_free(surf_network_resource->common_public->states.
448                 done_action_set);
449   xbt_free(surf_network_resource->common_public);
450   xbt_free(surf_network_resource->common_private);
451   xbt_free(surf_network_resource->extension_public);
452
453   xbt_free(surf_network_resource);
454   surf_network_resource = NULL;
455
456   for (i = 0; i < card_number; i++) 
457     for (j = 0; j < card_number; j++) 
458       xbt_free(ROUTE(i,j));
459   xbt_free(routing_table);
460   routing_table = NULL;
461   xbt_free(routing_table_size);
462   routing_table_size = NULL;
463   card_number = 0;
464 }
465
466 static void surf_network_resource_init_internal(void)
467 {
468   s_surf_action_t action;
469
470   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
471
472   surf_network_resource->common_private =
473       xbt_new0(s_surf_resource_private_t, 1);
474   surf_network_resource->common_public =
475       xbt_new0(s_surf_resource_public_t, 1);
476   surf_network_resource->extension_public =
477       xbt_new0(s_surf_network_resource_extension_public_t, 1);
478
479   surf_network_resource->common_public->states.ready_action_set =
480       xbt_swag_new(xbt_swag_offset(action, state_hookup));
481   surf_network_resource->common_public->states.running_action_set =
482       xbt_swag_new(xbt_swag_offset(action, state_hookup));
483   surf_network_resource->common_public->states.failed_action_set =
484       xbt_swag_new(xbt_swag_offset(action, state_hookup));
485   surf_network_resource->common_public->states.done_action_set =
486       xbt_swag_new(xbt_swag_offset(action, state_hookup));
487
488   surf_network_resource->common_public->name_service = name_service;
489   surf_network_resource->common_public->get_resource_name =
490       get_resource_name;
491   surf_network_resource->common_public->action_get_state =
492       surf_action_get_state;
493   surf_network_resource->common_public->action_free = action_free;
494   surf_network_resource->common_public->action_cancel = action_cancel;
495   surf_network_resource->common_public->action_recycle = action_recycle;
496   surf_network_resource->common_public->action_change_state =
497       action_change_state;
498   surf_network_resource->common_public->action_set_data = surf_action_set_data;
499   surf_network_resource->common_public->name = "network";
500
501   surf_network_resource->common_private->resource_used = resource_used;
502   surf_network_resource->common_private->share_resources = share_resources;
503   surf_network_resource->common_private->update_actions_state =
504       update_actions_state;
505   surf_network_resource->common_private->update_resource_state =
506       update_resource_state;
507   surf_network_resource->common_private->finalize = finalize;
508
509   surf_network_resource->common_public->suspend = action_suspend;
510   surf_network_resource->common_public->resume = action_resume;
511   surf_network_resource->common_public->is_suspended = action_is_suspended;
512
513   surf_network_resource->extension_public->communicate = communicate;
514
515   network_link_set = xbt_dict_new();
516   network_card_set = xbt_dict_new();
517
518   xbt_assert0(maxmin_system, "surf_init has to be called first!");
519 }
520
521 /***************************************************************************/
522 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
523 /***************************************************************************/
524 /* @TechReport{      rr-lip2002-40, */
525 /*   author        = {Henri Casanova and Loris Marchal}, */
526 /*   institution   = {LIP}, */
527 /*   title         = {A Network Model for Simulation of Grid Application}, */
528 /*   number        = {2002-40}, */
529 /*   month         = {oct}, */
530 /*   year          = {2002} */
531 /* } */
532 void surf_network_resource_init_CM02(const char *filename)
533 {
534   if (surf_network_resource)
535     return;
536   surf_network_resource_init_internal();
537   parse_file(filename);
538   xbt_dynar_push(resource_list, &surf_network_resource);
539 }