Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
task->data (formerly callback) could not be set by end-users.
[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(nw_link);
36 }
37
38 static network_link_t network_link_new(const char *name,
39                                        double bw_initial,
40                                        tmgr_trace_t bw_trace,
41                                        double lat_initial,
42                                        tmgr_trace_t lat_trace,
43                                        e_surf_network_link_state_t
44                                        state_initial,
45                                        tmgr_trace_t state_trace)
46 {
47   network_link_t nw_link = xbt_new0(s_network_link_t, 1);
48
49
50   nw_link->resource = (surf_resource_t) surf_network_resource;
51   nw_link->name = name;
52   nw_link->bw_current = bw_initial;
53   if (bw_trace)
54     nw_link->bw_event =
55         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
56   nw_link->lat_current = lat_initial;
57   if (lat_trace)
58     nw_link->lat_event =
59         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
60   nw_link->state_current = state_initial;
61   if (state_trace)
62     nw_link->state_event =
63         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
64
65   nw_link->constraint =
66       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
67
68   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
69
70   return nw_link;
71 }
72
73 static int network_card_new(const char *card_name)
74 {
75   network_card_t card = NULL;
76
77   xbt_dict_get(network_card_set, card_name, (void *) &card);
78
79   if (!card) {
80     card = xbt_new0(s_network_card_t, 1);
81     card->name = xbt_strdup(card_name);
82     card->id = card_number++;
83     xbt_dict_set(network_card_set, card_name, card, NULL);
84   }
85   return card->id;
86 }
87
88 static void route_new(int src_id, int dst_id, char **links, int nb_link)
89 {
90   network_link_t *link_list = NULL;
91   int i;
92
93   ROUTE_SIZE(src_id, dst_id) = nb_link;
94   link_list = (ROUTE(src_id, dst_id) = xbt_new0(network_link_t, nb_link));
95   for (i = 0; i < nb_link; i++) {
96     xbt_dict_get(network_link_set, links[i], (void *) &(link_list[i]));
97     xbt_free(links[i]);
98   }
99   xbt_free(links);
100 }
101
102 /*  
103    Semantic:  name       initial   bandwidth   initial   latency    initial     state
104                         bandwidth    trace     latency    trace      state      trace
105    
106    Token:   TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD TOKEN_WORD
107    Type:     string      double      string    double     string     ON/OFF     string
108 */
109 static void parse_network_link(void)
110 {
111   e_surf_token_t token;
112   char *name;
113   double bw_initial;
114   tmgr_trace_t bw_trace;
115   double lat_initial;
116   tmgr_trace_t lat_trace;
117   e_surf_network_link_state_t state_initial;
118   tmgr_trace_t state_trace;
119
120   name = xbt_strdup(surf_parse_text);
121
122   surf_parse_double(&bw_initial);
123   surf_parse_trace(&bw_trace);
124   surf_parse_double(&lat_initial);
125   surf_parse_trace(&lat_trace);
126
127   token = surf_parse();         /* state_initial */
128   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
129   if (strcmp(surf_parse_text, "ON") == 0)
130     state_initial = SURF_NETWORK_LINK_ON;
131   else if (strcmp(surf_parse_text, "OFF") == 0)
132     state_initial = SURF_NETWORK_LINK_OFF;
133   else {
134     CRITICAL2("Invalid cpu state (line %d): %s neq ON or OFF\n", surf_line_pos,
135               surf_parse_text);
136     xbt_abort();
137   }
138
139   surf_parse_trace(&state_trace);
140
141   network_link_new(name, bw_initial, bw_trace,
142                    lat_initial, lat_trace, state_initial, state_trace);
143 }
144
145 /*  
146    Semantic:  source   destination           network
147                                               links
148    
149    Token:   TOKEN_WORD TOKEN_WORD TOKEN_LP TOKEN_WORD* TOKEN_RP
150    Type:     string       string             string
151 */
152 static void parse_route(int fake)
153 {
154   int src_id = -1;
155   int dst_id = -1;
156   int nb_link = 0;
157   char **link_name = NULL;
158   e_surf_token_t token;
159
160   src_id = network_card_new(surf_parse_text);
161
162   token = surf_parse();
163   xbt_assert1((token == TOKEN_WORD), "Parse error line %d", surf_line_pos);
164   dst_id = network_card_new(surf_parse_text);
165
166   token = surf_parse();
167   xbt_assert1((token == TOKEN_LP), "Parse error line %d", surf_line_pos);
168
169   while ((token = surf_parse()) == TOKEN_WORD) {
170     if (!fake) {
171       nb_link++;
172       link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
173       link_name[(nb_link) - 1] = xbt_strdup(surf_parse_text);
174     }
175   }
176   xbt_assert1((token == TOKEN_RP), "Parse error line %d", surf_line_pos);
177
178   if (!fake)
179     route_new(src_id, dst_id, link_name, nb_link);
180 }
181
182 static void parse_file(const char *file)
183 {
184   e_surf_token_t token;
185
186   /* Figuring out the network links in the system */
187   find_section(file, "NETWORK");
188   while (1) {
189     token = surf_parse();
190
191     if (token == TOKEN_END_SECTION)
192       break;
193     if (token == TOKEN_NEWLINE)
194       continue;
195
196     if (token == TOKEN_WORD)
197       parse_network_link();
198     else {
199       CRITICAL1("Parse error line %d\n", surf_line_pos);
200       xbt_abort();
201     }
202   }
203   close_section("NETWORK");
204
205   /* Figuring out the network cards used */
206   find_section(file, "ROUTE");
207   while (1) {
208     token = surf_parse();
209
210     if (token == TOKEN_END_SECTION)
211       break;
212     if (token == TOKEN_NEWLINE)
213       continue;
214
215     if (token == TOKEN_WORD)
216       parse_route(1);
217     else {
218       CRITICAL1("Parse error line %d\n", surf_line_pos);
219       xbt_abort();
220     }
221   }
222   close_section("ROUTE");
223
224   create_routing_table();
225
226   /* Building the routes */
227   find_section(file, "ROUTE");
228   while (1) {
229     token = surf_parse();
230
231     if (token == TOKEN_END_SECTION)
232       break;
233     if (token == TOKEN_NEWLINE)
234       continue;
235
236     if (token == TOKEN_WORD)
237       parse_route(0);
238     else {
239       CRITICAL1("Parse error line %d\n", surf_line_pos);
240       xbt_abort();
241     }
242   }
243   close_section("ROUTE");
244 }
245
246 static void *name_service(const char *name)
247 {
248   network_card_t card = NULL;
249
250   xbt_dict_get(network_card_set, name, (void *) &card);
251
252   return card;
253 }
254
255 static const char *get_resource_name(void *resource_id)
256 {
257   return ((network_card_t) resource_id)->name;
258 }
259
260 static int resource_used(void *resource_id)
261 {
262   return lmm_constraint_used(maxmin_system,
263                              ((network_link_t) resource_id)->constraint);
264 }
265
266 static void action_free(surf_action_t action)
267 {
268   surf_action_network_t Action = (surf_action_network_t) action;
269
270   xbt_swag_remove(action, action->state_set);
271   lmm_variable_free(maxmin_system, Action->variable);
272   xbt_free(action);
273
274   return;
275 }
276
277 static void action_cancel(surf_action_t action)
278 {
279   return;
280 }
281
282 static void action_recycle(surf_action_t action)
283 {
284   return;
285 }
286
287 static void action_change_state(surf_action_t action,
288                                 e_surf_action_state_t state)
289 {
290   surf_action_change_state(action, state);
291   return;
292 }
293
294 static double share_resources(double now)
295 {
296   s_surf_action_network_t action;
297   return generic_maxmin_share_resources(surf_network_resource->
298                                         common_public->states.
299                                         running_action_set,
300                                         xbt_swag_offset(action, variable));
301 }
302
303
304 static void update_actions_state(double now, double delta)
305 {
306   double deltap = 0.0;
307   surf_action_network_t action = NULL;
308   surf_action_network_t next_action = NULL;
309   xbt_swag_t running_actions =
310       surf_network_resource->common_public->states.running_action_set;
311   xbt_swag_t failed_actions =
312       surf_network_resource->common_public->states.failed_action_set;
313
314   xbt_swag_foreach_safe(action, next_action, running_actions) {
315     deltap = delta;
316     if (action->latency > 0) {
317       if (action->latency > deltap) {
318         action->latency -= deltap;
319         deltap = 0.0;
320       } else {
321         deltap -= action->latency;
322         action->latency = 0.0;
323       }
324     }
325     action->generic_action.remains -=
326         lmm_variable_getvalue(action->variable) * deltap;
327     if (action->generic_action.max_duration != NO_MAX_DURATION)
328       action->generic_action.max_duration -= delta;
329
330 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
331
332     if (action->generic_action.remains <= 0) {
333       action->generic_action.finish = surf_get_clock();
334       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
335     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
336                (action->generic_action.max_duration <= 0)) {
337       action->generic_action.finish = surf_get_clock();
338       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
339     } else {                    /* Need to check that none of the resource has failed */
340       lmm_constraint_t cnst = NULL;
341       int i = 0;
342       network_link_t nw_link = NULL;
343
344       while ((cnst =
345               lmm_get_cnst_from_var(maxmin_system, action->variable,
346                                     i++))) {
347         nw_link = lmm_constraint_id(cnst);
348         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
349           action->generic_action.finish = surf_get_clock();
350           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
351           break;
352         }
353       }
354     }
355   }
356
357   xbt_swag_foreach_safe(action, next_action, failed_actions) {
358     lmm_variable_disable(maxmin_system, action->variable);
359   }
360
361   return;
362 }
363
364 static void update_resource_state(void *id,
365                                   tmgr_trace_event_t event_type,
366                                   double value)
367 {
368   network_link_t nw_link = id;
369
370 /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
371 /*       "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
372 /*       value, event_type); */
373
374   if (event_type == nw_link->bw_event) {
375     nw_link->bw_current = value;
376     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
377                                 nw_link->bw_current);
378   } else if (event_type == nw_link->lat_event) {
379     double delta = value - nw_link->lat_current;
380     lmm_variable_t var = NULL;
381     surf_action_network_t action = NULL;
382
383     nw_link->lat_current = value;
384     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
385       action = lmm_variable_id(var);
386       action->lat_current += delta;
387       lmm_update_variable_bound(maxmin_system, var,
388                                 1 / (action->lat_current));
389     }
390   } else if (event_type == nw_link->state_event) {
391     if (value > 0)
392       nw_link->state_current = SURF_NETWORK_LINK_ON;
393     else
394       nw_link->state_current = SURF_NETWORK_LINK_OFF;
395   } else {
396     CRITICAL0("Unknown event ! \n");
397     xbt_abort();
398   }
399
400   return;
401 }
402
403 static surf_action_t communicate(void *src, void *dst, double size)
404 {
405   surf_action_network_t action = NULL;
406   network_card_t card_src = src;
407   network_card_t card_dst = dst;
408   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
409   network_link_t *route = ROUTE(card_src->id, card_dst->id);
410   int i;
411
412   action = xbt_new0(s_surf_action_network_t, 1);
413
414   action->generic_action.cost = size;
415   action->generic_action.remains = size;
416   action->generic_action.max_duration = NO_MAX_DURATION;
417   action->generic_action.start = -1.0;
418   action->generic_action.finish = -1.0;
419   action->generic_action.resource_type =
420       (surf_resource_t) surf_network_resource;
421
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
427   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
428                                       route_size);
429   for (i = 0; i < route_size; i++)
430     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
431
432   action->latency = 0.0;
433   for (i = 0; i < route_size; i++)
434     action->latency += route[i]->lat_current;
435   action->lat_current = action->latency;
436   lmm_update_variable_bound(maxmin_system, action->variable,
437                             SG_TCP_CTE_GAMMA / action->lat_current);
438
439   return (surf_action_t) action;
440 }
441
442 static void finalize(void)
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
462 static void surf_network_resource_init_internal(void)
463 {
464   s_surf_action_t action;
465
466   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
467
468   surf_network_resource->common_private =
469       xbt_new0(s_surf_resource_private_t, 1);
470   surf_network_resource->common_public =
471       xbt_new0(s_surf_resource_public_t, 1);
472 /*   surf_network_resource->extension_private = xbt_new0(s_surf_network_resource_extension_private_t,1); */
473   surf_network_resource->extension_public =
474       xbt_new0(s_surf_network_resource_extension_public_t, 1);
475
476   surf_network_resource->common_public->states.ready_action_set =
477       xbt_swag_new(xbt_swag_offset(action, state_hookup));
478   surf_network_resource->common_public->states.running_action_set =
479       xbt_swag_new(xbt_swag_offset(action, state_hookup));
480   surf_network_resource->common_public->states.failed_action_set =
481       xbt_swag_new(xbt_swag_offset(action, state_hookup));
482   surf_network_resource->common_public->states.done_action_set =
483       xbt_swag_new(xbt_swag_offset(action, state_hookup));
484
485   surf_network_resource->common_public->name_service = name_service;
486   surf_network_resource->common_public->get_resource_name =
487       get_resource_name;
488   surf_network_resource->common_public->action_get_state =
489       surf_action_get_state;
490   surf_network_resource->common_public->action_free = action_free;
491   surf_network_resource->common_public->action_cancel = action_cancel;
492   surf_network_resource->common_public->action_recycle = action_recycle;
493   surf_network_resource->common_public->action_change_state =
494       action_change_state;
495   surf_network_resource->common_public->action_set_data = surf_action_set_data;
496   surf_network_resource->common_public->name = "network";
497
498   surf_network_resource->common_private->resource_used = resource_used;
499   surf_network_resource->common_private->share_resources = share_resources;
500   surf_network_resource->common_private->update_actions_state =
501       update_actions_state;
502   surf_network_resource->common_private->update_resource_state =
503       update_resource_state;
504   surf_network_resource->common_private->finalize = finalize;
505
506   surf_network_resource->extension_public->communicate = communicate;
507
508   network_link_set = xbt_dict_new();
509   network_card_set = xbt_dict_new();
510
511   xbt_assert0(maxmin_system, "surf_init has to be called first!");
512 }
513
514 void surf_network_resource_init(const char *filename)
515 {
516   if (surf_network_resource)
517     return;
518   surf_network_resource_init_internal();
519   parse_file(filename);
520   xbt_dynar_push(resource_list, &surf_network_resource);
521 }