Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fec83e5071dea91d0b475aca0c7da564570cc422
[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                                        xbt_maxmin_float_t bw_initial,
40                                        tmgr_trace_t bw_trace,
41                                        xbt_maxmin_float_t 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       float      string    float      string     ON/OFF     string
108 */
109 static void parse_network_link(void)
110 {
111   e_surf_token_t token;
112   char *name;
113   xbt_maxmin_float_t bw_initial;
114   tmgr_trace_t bw_trace;
115   xbt_maxmin_float_t 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_float(&bw_initial);
123   surf_parse_trace(&bw_trace);
124   surf_parse_float(&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", 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", 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", 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", 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", 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", 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", 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", 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 xbt_heap_float_t share_resources(xbt_heap_float_t 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(xbt_heap_float_t now,
305                                  xbt_heap_float_t delta)
306 {
307   xbt_heap_float_t deltap = 0.0;
308   surf_action_network_t action = NULL;
309   surf_action_network_t next_action = NULL;
310   xbt_swag_t running_actions =
311       surf_network_resource->common_public->states.running_action_set;
312   xbt_swag_t failed_actions =
313       surf_network_resource->common_public->states.failed_action_set;
314
315   xbt_swag_foreach_safe(action, next_action, running_actions) {
316     deltap = delta;
317     if (action->latency > 0) {
318       if (action->latency > deltap) {
319         action->latency -= deltap;
320         deltap = 0.0;
321       } else {
322         deltap -= action->latency;
323         action->latency = 0.0;
324       }
325     }
326     action->generic_action.remains -=
327         lmm_variable_getvalue(action->variable) * deltap;
328     if (action->generic_action.max_duration != NO_MAX_DURATION)
329       action->generic_action.max_duration -= delta;
330
331 /*     if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
332
333     if (action->generic_action.remains <= 0) {
334       action->generic_action.finish = surf_get_clock();
335       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
336     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
337                (action->generic_action.max_duration <= 0)) {
338       action->generic_action.finish = surf_get_clock();
339       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
340     } else {                    /* Need to check that none of the resource has failed */
341       lmm_constraint_t cnst = NULL;
342       int i = 0;
343       network_link_t nw_link = NULL;
344
345       while ((cnst =
346               lmm_get_cnst_from_var(maxmin_system, action->variable,
347                                     i++))) {
348         nw_link = lmm_constraint_id(cnst);
349         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
350           action->generic_action.finish = surf_get_clock();
351           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
352           break;
353         }
354       }
355     }
356   }
357
358   xbt_swag_foreach_safe(action, next_action, failed_actions) {
359     lmm_variable_disable(maxmin_system, action->variable);
360   }
361
362   return;
363 }
364
365 static void update_resource_state(void *id,
366                                   tmgr_trace_event_t event_type,
367                                   xbt_maxmin_float_t value)
368 {
369   network_link_t nw_link = id;
370
371 /*   printf("[" XBT_HEAP_FLOAT_T "] Asking to update network card \"%s\" with value " */
372 /*       XBT_MAXMIN_FLOAT_T " for event %p\n", surf_get_clock(), nw_link->name, */
373 /*       value, event_type); */
374
375   if (event_type == nw_link->bw_event) {
376     nw_link->bw_current = value;
377     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
378                                 nw_link->bw_current);
379   } else if (event_type == nw_link->lat_event) {
380     xbt_maxmin_float_t delta = value - nw_link->lat_current;
381     lmm_variable_t var = NULL;
382     surf_action_network_t action = NULL;
383
384     nw_link->lat_current = value;
385     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
386       action = lmm_variable_id(var);
387       action->lat_current += delta;
388       lmm_update_variable_bound(maxmin_system, var,
389                                 1 / (action->lat_current));
390     }
391   } else if (event_type == nw_link->state_event) {
392     if (value > 0)
393       nw_link->state_current = SURF_NETWORK_LINK_ON;
394     else
395       nw_link->state_current = SURF_NETWORK_LINK_OFF;
396   } else {
397     CRITICAL0("Unknown event ! \n");
398     xbt_abort();
399   }
400
401   return;
402 }
403
404 static surf_action_t communicate(void *src, void *dst,
405                                  xbt_maxmin_float_t size)
406 {
407   surf_action_network_t action = NULL;
408   network_card_t card_src = src;
409   network_card_t card_dst = dst;
410   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
411   network_link_t *route = ROUTE(card_src->id, card_dst->id);
412   int i;
413
414   action = xbt_new0(s_surf_action_network_t, 1);
415
416   action->generic_action.cost = size;
417   action->generic_action.remains = size;
418   action->generic_action.max_duration = NO_MAX_DURATION;
419   action->generic_action.start = -1.0;
420   action->generic_action.finish = -1.0;
421   action->generic_action.callback = NULL;
422   action->generic_action.resource_type =
423       (surf_resource_t) surf_network_resource;
424
425   action->generic_action.state_set =
426       surf_network_resource->common_public->states.running_action_set;
427
428   xbt_swag_insert(action, action->generic_action.state_set);
429
430   action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
431                                       route_size);
432   for (i = 0; i < route_size; i++)
433     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
434
435   action->latency = 0.0;
436   for (i = 0; i < route_size; i++)
437     action->latency += route[i]->lat_current;
438   action->lat_current = action->latency;
439   lmm_update_variable_bound(maxmin_system, action->variable,
440                             SG_TCP_CTE_GAMMA / action->lat_current);
441
442   return (surf_action_t) action;
443 }
444
445 static void finalize(void)
446 {
447   xbt_dict_free(&network_card_set);
448   xbt_dict_free(&network_link_set);
449   xbt_swag_free(surf_network_resource->common_public->states.
450                 ready_action_set);
451   xbt_swag_free(surf_network_resource->common_public->states.
452                 running_action_set);
453   xbt_swag_free(surf_network_resource->common_public->states.
454                 failed_action_set);
455   xbt_swag_free(surf_network_resource->common_public->states.
456                 done_action_set);
457   xbt_free(surf_network_resource->common_public);
458   xbt_free(surf_network_resource->common_private);
459   xbt_free(surf_network_resource->extension_public);
460
461   xbt_free(surf_network_resource);
462   surf_network_resource = NULL;
463 }
464
465 static void surf_network_resource_init_internal(void)
466 {
467   s_surf_action_t action;
468
469   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
470
471   surf_network_resource->common_private =
472       xbt_new0(s_surf_resource_private_t, 1);
473   surf_network_resource->common_public =
474       xbt_new0(s_surf_resource_public_t, 1);
475 /*   surf_network_resource->extension_private = xbt_new0(s_surf_network_resource_extension_private_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->name = "network";
499
500   surf_network_resource->common_private->resource_used = resource_used;
501   surf_network_resource->common_private->share_resources = share_resources;
502   surf_network_resource->common_private->update_actions_state =
503       update_actions_state;
504   surf_network_resource->common_private->update_resource_state =
505       update_resource_state;
506   surf_network_resource->common_private->finalize = finalize;
507
508   surf_network_resource->extension_public->communicate = communicate;
509
510   network_link_set = xbt_dict_new();
511   network_card_set = xbt_dict_new();
512
513   xbt_assert0(maxmin_system, "surf_init has to be called first!");
514 }
515
516 void surf_network_resource_init(const char *filename)
517 {
518   if (surf_network_resource)
519     return;
520   surf_network_resource_init_internal();
521   parse_file(filename);
522   xbt_dynar_push(resource_list, &surf_network_resource);
523 }