Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f878ad61dff93162fa585ee20b4b054d0d587766
[simgrid.git] / src / surf / network_dassf.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 Henri Casanova. 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_dassf_private.h"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
11
12 /* surf_network_resource_t surf_network_resource = NULL; */
13 /*xbt_dict_t network_link_set = NULL;*/
14 /* xbt_dict_t network_card_set = NULL; */
15
16 static int card_number = 0;
17 static network_link_DASSF_t **routing_table = NULL;
18 static int *routing_table_size = NULL;
19
20 #define ROUTE(i,j) routing_table[(i)+(j)*card_number]
21 #define ROUTE_SIZE(i,j) routing_table_size[(i)+(j)*card_number]
22
23 static void create_routing_table(void)
24 {
25   routing_table = xbt_new0(network_link_DASSF_t *, card_number * card_number);
26   routing_table_size = xbt_new0(int, card_number * card_number);
27 }
28
29 static void network_link_free(void *nw_link)
30 {
31   free(((network_link_DASSF_t)nw_link)->name);
32   free(nw_link);
33 }
34
35 static network_link_DASSF_t network_link_new(char *name,
36                                        double bw_initial,
37                                        tmgr_trace_t bw_trace,
38                                        double lat_initial,
39                                        tmgr_trace_t lat_trace,
40                                        e_surf_network_link_state_t
41                                        state_initial,
42                                        tmgr_trace_t state_trace)
43 {
44   network_link_DASSF_t nw_link = xbt_new0(s_network_link_DASSF_t, 1);
45
46
47   nw_link->resource = (surf_resource_t) surf_network_resource;
48   nw_link->name = name;
49   nw_link->bw_current = bw_initial;
50   if (bw_trace)
51     nw_link->bw_event =
52         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
53   nw_link->lat_current = lat_initial;
54   if (lat_trace)
55     nw_link->lat_event =
56         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
57   nw_link->state_current = state_initial;
58   if (state_trace)
59     nw_link->state_event =
60         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
61
62   nw_link->constraint =
63       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
64
65   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
66
67   return nw_link;
68 }
69
70 static void network_card_free(void *nw_card)
71 {
72   free(((network_card_DASSF_t)nw_card)->name);
73   free(nw_card);
74 }
75
76 static int network_card_new(const char *card_name)
77 {
78   network_card_DASSF_t card = xbt_dict_get_or_null(network_card_set, card_name);
79
80   if (!card) {
81     card = xbt_new0(s_network_card_DASSF_t, 1);
82     card->name = xbt_strdup(card_name);
83     card->id = card_number++;
84     xbt_dict_set(network_card_set, card_name, card, network_card_free);
85   }
86   return card->id;
87 }
88
89 static void route_new(int src_id, int dst_id, network_link_DASSF_t *link_list, int nb_link)
90 {
91   ROUTE_SIZE(src_id, dst_id) = nb_link;
92   ROUTE(src_id, dst_id) = link_list = xbt_realloc(link_list, sizeof(network_link_DASSF_t) * nb_link);
93 }
94
95 static void parse_network_link(void)
96 {
97   char *name;
98   double bw_initial;
99   tmgr_trace_t bw_trace;
100   double lat_initial;
101   tmgr_trace_t lat_trace;
102   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
103   tmgr_trace_t state_trace;
104
105   name = xbt_strdup(A_surfxml_network_link_name);
106   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
107   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
108   surf_parse_get_double(&lat_initial,A_surfxml_network_link_latency);
109   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
110
111   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
112               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
113               "Invalid state");
114   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
115     state_initial = SURF_NETWORK_LINK_ON;
116   if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
117     state_initial = SURF_NETWORK_LINK_OFF;
118   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
119
120   network_link_new(name, bw_initial, bw_trace,
121                    lat_initial, lat_trace, state_initial, state_trace);
122 }
123
124 static int nb_link;
125 static int link_list_capacity;
126 static network_link_DASSF_t *link_list = NULL;
127 static int src_id = -1;
128 static int dst_id = -1;
129
130 static void parse_route_set_endpoints(void)
131 {
132   src_id = network_card_new(A_surfxml_route_src);
133   dst_id = network_card_new(A_surfxml_route_dst);
134   nb_link = 0;
135   link_list_capacity = 20;
136   link_list = xbt_new(network_link_DASSF_t, link_list_capacity);
137 }
138
139 static void parse_route_elem(void)
140 {
141   if (nb_link == link_list_capacity) {
142     link_list_capacity *= 2;
143     link_list = xbt_realloc(link_list, link_list_capacity * sizeof(network_link_DASSF_t));
144   }
145   link_list[nb_link++] = xbt_dict_get_or_null(network_link_set, A_surfxml_route_element_name);
146 }
147
148 static void parse_route_set_route(void)
149 {
150   route_new(src_id, dst_id, link_list, nb_link);
151 }
152
153 static void parse_file(const char *file)
154 {
155   /* Figuring out the network links */
156   surf_parse_reset_parser();
157   ETag_surfxml_network_link_fun=parse_network_link;
158   surf_parse_open(file);
159   xbt_assert1((!surf_parse()),"Parse error in %s",file);
160   surf_parse_close();
161
162   /* Figuring out the network cards used */
163   surf_parse_reset_parser();
164   STag_surfxml_route_fun=parse_route_set_endpoints;
165   surf_parse_open(file);
166   xbt_assert1((!surf_parse()),"Parse error in %s",file);
167   surf_parse_close();
168
169   create_routing_table();
170
171   /* Building the routes */
172   surf_parse_reset_parser();
173   STag_surfxml_route_fun=parse_route_set_endpoints;
174   ETag_surfxml_route_element_fun=parse_route_elem;
175   ETag_surfxml_route_fun=parse_route_set_route;
176   surf_parse_open(file);
177   xbt_assert1((!surf_parse()),"Parse error in %s",file);
178   surf_parse_close();
179 }
180
181 static void *name_service(const char *name)
182 {
183   return xbt_dict_get_or_null(network_card_set, name);
184 }
185
186 static const char *get_resource_name(void *resource_id)
187 {
188   return ((network_card_DASSF_t) resource_id)->name;
189 }
190
191 static int resource_used(void *resource_id)
192 {
193   return lmm_constraint_used(maxmin_system,
194                              ((network_link_DASSF_t) resource_id)->constraint);
195 }
196
197 static int action_free(surf_action_t action)
198 {
199   action->using--;
200   if(!action->using) {
201     xbt_swag_remove(action, action->state_set);
202     if(((surf_action_network_DASSF_t)action)->variable)
203       lmm_variable_free(maxmin_system, ((surf_action_network_DASSF_t)action)->variable);
204     free(action);
205   }
206   return 1;
207 }
208
209 static void action_cancel(surf_action_t action)
210 {
211   return;
212 }
213
214 static void action_recycle(surf_action_t action)
215 {
216   return;
217 }
218
219 static void action_change_state(surf_action_t action,
220                                 e_surf_action_state_t state)
221 {
222 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
223 /*     if(((surf_action_network_DASSF_t)action)->variable) { */
224 /*       lmm_variable_disable(maxmin_system, ((surf_action_network_DASSF_t)action)->variable); */
225 /*       ((surf_action_network_DASSF_t)action)->variable = NULL; */
226 /*     } */
227
228   surf_action_change_state(action, state);
229   return;
230 }
231
232 static double share_resources(double now)
233 {
234   s_surf_action_network_DASSF_t s_action;
235   surf_action_network_DASSF_t action = NULL;
236   xbt_swag_t running_actions = surf_network_resource->common_public->states.running_action_set;
237   double min = generic_maxmin_share_resources(running_actions,
238                                               xbt_swag_offset(s_action, variable));
239
240   xbt_swag_foreach(action, running_actions) {
241     if(action->latency>0) {
242       if(min<0) min = action->latency;
243       else if (action->latency<min) min = action->latency;
244     }
245   }
246
247   return min;
248 }
249
250 static void update_actions_state(double now, double delta)
251 {
252   double deltap = 0.0;
253   surf_action_network_DASSF_t action = NULL;
254   surf_action_network_DASSF_t next_action = NULL;
255   xbt_swag_t running_actions =
256       surf_network_resource->common_public->states.running_action_set;
257   /* KILLME: unused
258   xbt_swag_t failed_actions =
259       surf_network_resource->common_public->states.failed_action_set;
260   */
261
262   xbt_swag_foreach_safe(action, next_action, running_actions) {
263     deltap = delta;
264     if (action->latency > 0) {
265       if (action->latency > deltap) {
266         surf_double_update(&(action->latency),deltap);
267         deltap = 0.0;
268       } else {
269         surf_double_update(&(deltap), action->latency);
270         action->latency = 0.0;
271       }
272       if ((action->latency == 0.0) && !(action->suspended)) 
273         lmm_update_variable_weight(maxmin_system, action->variable, 1.0);
274     }
275     surf_double_update(&(action->generic_action.remains),
276         lmm_variable_getvalue(action->variable) * deltap);
277     if (action->generic_action.max_duration != NO_MAX_DURATION)
278       surf_double_update(&(action->generic_action.max_duration), delta);
279
280     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
281
282     if (action->generic_action.remains <= 0) {
283       action->generic_action.finish = surf_get_clock();
284       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
285     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
286                (action->generic_action.max_duration <= 0)) {
287       action->generic_action.finish = surf_get_clock();
288       action_change_state((surf_action_t) action, SURF_ACTION_DONE);
289     } else {                    /* Need to check that none of the resource has failed */
290       lmm_constraint_t cnst = NULL;
291       int i = 0;
292       network_link_DASSF_t nw_link = NULL;
293
294       while ((cnst =
295               lmm_get_cnst_from_var(maxmin_system, action->variable,
296                                     i++))) {
297         nw_link = lmm_constraint_id(cnst);
298         if (nw_link->state_current == SURF_NETWORK_LINK_OFF) {
299           action->generic_action.finish = surf_get_clock();
300           action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
301           break;
302         }
303       }
304     }
305   }
306
307   return;
308 }
309
310 static void update_resource_state(void *id,
311                                   tmgr_trace_event_t event_type,
312                                   double value)
313 {
314   network_link_DASSF_t nw_link = id;
315   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
316   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
317   /*     value, event_type); */
318
319   if (event_type == nw_link->bw_event) {
320     nw_link->bw_current = value;
321     lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
322                                 nw_link->bw_current);
323   } else if (event_type == nw_link->lat_event) {
324     double delta = value - nw_link->lat_current;
325     lmm_variable_t var = NULL;
326     surf_action_network_DASSF_t action = NULL;
327
328     nw_link->lat_current = value;
329     while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
330       action = lmm_variable_id(var);
331       action->lat_current += delta;
332       if(action->rate<0)
333         lmm_update_variable_bound(maxmin_system, action->variable,
334                                   SG_TCP_CTE_GAMMA / action->lat_current);
335       else 
336         lmm_update_variable_bound(maxmin_system, action->variable,
337                                   min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
338     }
339   } else if (event_type == nw_link->state_event) {
340     if (value > 0)
341       nw_link->state_current = SURF_NETWORK_LINK_ON;
342     else
343       nw_link->state_current = SURF_NETWORK_LINK_OFF;
344   } else {
345     CRITICAL0("Unknown event ! \n");
346     xbt_abort();
347   }
348
349   return;
350 }
351
352 static surf_action_t communicate(void *src, void *dst, double size, double rate)
353 {
354   surf_action_network_DASSF_t action = NULL;
355   network_card_DASSF_t card_src = src;
356   network_card_DASSF_t card_dst = dst;
357   int route_size = ROUTE_SIZE(card_src->id, card_dst->id);
358   network_link_DASSF_t *route = ROUTE(card_src->id, card_dst->id);
359   int i;
360
361   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);
362
363   action = xbt_new0(s_surf_action_network_DASSF_t, 1);
364
365   action->generic_action.using = 1;
366   action->generic_action.cost = size;
367   action->generic_action.remains = size;
368   action->generic_action.max_duration = NO_MAX_DURATION;
369   action->generic_action.start = -1.0;
370   action->generic_action.finish = -1.0;
371   action->generic_action.resource_type =
372       (surf_resource_t) surf_network_resource;
373
374   action->generic_action.state_set =
375       surf_network_resource->common_public->states.running_action_set;
376
377   xbt_swag_insert(action, action->generic_action.state_set);
378   action->rate = rate;
379
380   action->latency = 0.0;
381   for (i = 0; i < route_size; i++)
382     action->latency += route[i]->lat_current;
383   action->lat_current = action->latency;
384
385   if(action->latency>0)
386     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
387                                         route_size);
388   else 
389     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
390                                         route_size);
391
392   if(action->rate<0)
393     lmm_update_variable_bound(maxmin_system, action->variable,
394                               SG_TCP_CTE_GAMMA / action->lat_current);
395   else 
396     lmm_update_variable_bound(maxmin_system, action->variable,
397                               min(action->rate,SG_TCP_CTE_GAMMA / action->lat_current));
398
399   if(route_size == 0) {
400     action_change_state((surf_action_t) action, SURF_ACTION_DONE);
401   }
402
403   for (i = 0; i < route_size; i++)
404     lmm_expand(maxmin_system, route[i]->constraint, action->variable, 1.0);
405
406   return (surf_action_t) action;
407 }
408
409 /* returns a NULL-terminated array of network_link_DASSF_t */
410 static const void** get_route(void *src, void *dst) {
411   network_card_DASSF_t card_src = src;
412   network_card_DASSF_t card_dst = dst;
413   return (const void**) ROUTE(card_src->id, card_dst->id);
414 }
415
416 static int get_route_size(void *src, void *dst) {
417   network_card_DASSF_t card_src = src;
418   network_card_DASSF_t card_dst = dst;
419   return ROUTE_SIZE(card_src->id, card_dst->id);
420 }
421
422 static const char *get_link_name(const void *link) {
423   return ((network_link_DASSF_t) link)->name;
424 }
425
426 static double get_link_bandwidth(const void *link) {
427   return ((network_link_DASSF_t) link)->bw_current;
428 }
429
430 static double get_link_latency(const void *link) {
431   return ((network_link_DASSF_t) link)->lat_current;  
432 }
433
434 static void action_suspend(surf_action_t action)
435 {
436   ((surf_action_network_DASSF_t) action)->suspended = 1;
437   lmm_update_variable_weight(maxmin_system,
438                              ((surf_action_network_DASSF_t) action)->variable, 0.0);
439 }
440
441 static void action_resume(surf_action_t action)
442 {
443   lmm_update_variable_weight(maxmin_system,
444                              ((surf_action_network_DASSF_t) action)->variable, 1.0);
445   ((surf_action_network_DASSF_t) action)->suspended = 0;
446 }
447
448 static int action_is_suspended(surf_action_t action)
449 {
450   return ((surf_action_network_DASSF_t) action)->suspended;
451 }
452
453 static void finalize(void)
454 {
455   int i,j;
456
457   xbt_dict_free(&network_card_set);
458   xbt_dict_free(&network_link_set);
459   xbt_swag_free(surf_network_resource->common_public->states.
460                 ready_action_set);
461   xbt_swag_free(surf_network_resource->common_public->states.
462                 running_action_set);
463   xbt_swag_free(surf_network_resource->common_public->states.
464                 failed_action_set);
465   xbt_swag_free(surf_network_resource->common_public->states.
466                 done_action_set);
467   free(surf_network_resource->common_public);
468   free(surf_network_resource->common_private);
469   free(surf_network_resource->extension_public);
470
471   free(surf_network_resource);
472   surf_network_resource = NULL;
473
474   for (i = 0; i < card_number; i++) 
475     for (j = 0; j < card_number; j++) 
476       free(ROUTE(i,j));
477   free(routing_table);
478   routing_table = NULL;
479   free(routing_table_size);
480   routing_table_size = NULL;
481   card_number = 0;
482 }
483
484 static void surf_network_resource_init_internal(void)
485 {
486   s_surf_action_t action;
487
488   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
489
490   surf_network_resource->common_private =
491       xbt_new0(s_surf_resource_private_t, 1);
492   surf_network_resource->common_public =
493       xbt_new0(s_surf_resource_public_t, 1);
494   surf_network_resource->extension_public =
495       xbt_new0(s_surf_network_resource_extension_public_t, 1);
496
497   surf_network_resource->common_public->states.ready_action_set =
498       xbt_swag_new(xbt_swag_offset(action, state_hookup));
499   surf_network_resource->common_public->states.running_action_set =
500       xbt_swag_new(xbt_swag_offset(action, state_hookup));
501   surf_network_resource->common_public->states.failed_action_set =
502       xbt_swag_new(xbt_swag_offset(action, state_hookup));
503   surf_network_resource->common_public->states.done_action_set =
504       xbt_swag_new(xbt_swag_offset(action, state_hookup));
505
506   surf_network_resource->common_public->name_service = name_service;
507   surf_network_resource->common_public->get_resource_name =
508       get_resource_name;
509   surf_network_resource->common_public->action_get_state =
510       surf_action_get_state;
511   surf_network_resource->common_public->action_get_start_time =
512       surf_action_get_start_time;
513   surf_network_resource->common_public->action_get_finish_time =
514       surf_action_get_finish_time;
515   surf_network_resource->common_public->action_free = action_free;
516   surf_network_resource->common_public->action_cancel = action_cancel;
517   surf_network_resource->common_public->action_recycle = action_recycle;
518   surf_network_resource->common_public->action_change_state =
519       action_change_state;
520   surf_network_resource->common_public->action_set_data = surf_action_set_data;
521   surf_network_resource->common_public->name = "network";
522
523   surf_network_resource->common_private->resource_used = resource_used;
524   surf_network_resource->common_private->share_resources = share_resources;
525   surf_network_resource->common_private->update_actions_state =
526       update_actions_state;
527   surf_network_resource->common_private->update_resource_state =
528       update_resource_state;
529   surf_network_resource->common_private->finalize = finalize;
530
531   surf_network_resource->common_public->suspend = action_suspend;
532   surf_network_resource->common_public->resume = action_resume;
533   surf_network_resource->common_public->is_suspended = action_is_suspended;
534
535   surf_network_resource->extension_public->communicate = communicate;
536   surf_network_resource->extension_public->get_route = get_route;
537   surf_network_resource->extension_public->get_route_size = get_route_size;
538   surf_network_resource->extension_public->get_link_name = get_link_name;
539   surf_network_resource->extension_public->get_link_bandwidth = get_link_bandwidth;
540   surf_network_resource->extension_public->get_link_latency = get_link_latency;
541
542   network_link_set = xbt_dict_new();
543   network_card_set = xbt_dict_new();
544
545   xbt_assert0(maxmin_system, "surf_init has to be called first!");
546 }
547
548 /***************************************************************************/
549 /* The nice Hack by Henri to use DASSF from within SURF !                  */
550 /***************************************************************************/
551 /* I put this prototype here for now but it will have to go in 
552    src/include/surf.h when it will be functionnal. */
553 void surf_network_resource_init_DASSF(const char *filename);
554
555 void surf_network_resource_init_DASSF(const char *filename)
556 {
557   if (surf_network_resource)
558     return;
559   surf_network_resource_init_internal();
560   parse_file(filename);
561   xbt_dynar_push(resource_list, &surf_network_resource);
562 }