Logo AND Algorithmique Numérique Distribuée

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