Logo AND Algorithmique Numérique Distribuée

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