Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e7292f19735242d45b053e125c02b92394cee9b7
[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 #include "xbt/log.h"
10 #include "xbt/str.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network, surf,
13                                 "Logging specific to the SURF network module");
14
15 surf_model_t surf_network_model = NULL;
16 static lmm_system_t network_maxmin_system = NULL;
17 static void (*network_solve) (lmm_system_t) = NULL;
18
19 double latency_factor = 1.0;    /* default value */
20 double bandwidth_factor = 1.0;  /* default value */
21 double weight_S_parameter = 0.0;        /* default value */
22
23 double sg_tcp_gamma = 0.0;
24
25
26 static link_CM02_t link_new(char *name,
27                             double bw_initial,
28                             tmgr_trace_t bw_trace,
29                             double lat_initial,
30                             tmgr_trace_t lat_trace,
31                             e_surf_resource_state_t
32                             state_initial,
33                             tmgr_trace_t state_trace,
34                             e_surf_link_sharing_policy_t
35                             policy, xbt_dict_t properties)
36 {
37   link_CM02_t nw_link = (link_CM02_t)
38     surf_resource_lmm_new(sizeof(s_link_CM02_t),
39         surf_network_model,name,properties,
40         network_maxmin_system, bandwidth_factor * bw_initial,
41         history,
42         state_initial,state_trace,
43         bw_initial,bw_trace);
44
45   xbt_assert1(!xbt_dict_get_or_null(surf_network_model->resource_set, name),
46               "Link '%s' declared several times in the platform file.", name);
47
48   nw_link->lat_current = lat_initial;
49   if (lat_trace)
50     nw_link->lat_event =
51       tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
52
53   if (policy == SURF_LINK_FATPIPE)
54     lmm_constraint_shared(nw_link->lmm_resource.constraint);
55
56   xbt_dict_set(surf_network_model->resource_set, name, nw_link, surf_resource_free);
57
58   return nw_link;
59 }
60
61 static void parse_link_init(void)
62 {
63   char *name_link;
64   double bw_initial;
65   tmgr_trace_t bw_trace;
66   double lat_initial;
67   tmgr_trace_t lat_trace;
68   e_surf_resource_state_t state_initial_link = SURF_RESOURCE_ON;
69   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
70   tmgr_trace_t state_trace;
71
72   name_link = xbt_strdup(A_surfxml_link_id);
73   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
74   bw_trace = tmgr_trace_new(A_surfxml_link_bandwidth_file);
75   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
76   lat_trace = tmgr_trace_new(A_surfxml_link_latency_file);
77
78   xbt_assert0((A_surfxml_link_state == A_surfxml_link_state_ON)
79               || (A_surfxml_link_state ==
80                   A_surfxml_link_state_OFF), "Invalid state");
81   if (A_surfxml_link_state == A_surfxml_link_state_ON)
82     state_initial_link = SURF_RESOURCE_ON;
83   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
84     state_initial_link = SURF_RESOURCE_OFF;
85
86   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
87     policy_initial_link = SURF_LINK_SHARED;
88   else if (A_surfxml_link_sharing_policy ==
89            A_surfxml_link_sharing_policy_FATPIPE)
90     policy_initial_link = SURF_LINK_FATPIPE;
91
92   state_trace = tmgr_trace_new(A_surfxml_link_state_file);
93
94   link_new(name_link, bw_initial, bw_trace,
95            lat_initial, lat_trace, state_initial_link, state_trace,
96            policy_initial_link, xbt_dict_new());
97
98 }
99
100 static void add_traces(void)
101 {
102   xbt_dict_cursor_t cursor = NULL;
103   char *trace_name, *elm;
104
105   static int called = 0;
106   if (called)
107     return;
108   called = 1;
109
110   /* connect all traces relative to network */
111   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
112     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
113     link_CM02_t link = xbt_dict_get_or_null(surf_network_model->resource_set, elm);
114
115     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
116                 trace_name, elm);
117     xbt_assert2(trace, "Cannot connect trace %s to link %s: trace undefined",
118                 trace_name, elm);
119
120     link->lmm_resource.state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
121   }
122
123   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
124     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
125     link_CM02_t link = xbt_dict_get_or_null(surf_network_model->resource_set, elm);
126
127     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
128                 trace_name, elm);
129     xbt_assert2(trace, "Cannot connect trace %s to link %s: trace undefined",
130                 trace_name, elm);
131
132     link->lmm_resource.power.event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
133   }
134
135   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
136     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
137     link_CM02_t link = xbt_dict_get_or_null(surf_network_model->resource_set, elm);
138
139     xbt_assert2(link, "Cannot connect trace %s to link %s: link undefined",
140                 trace_name, elm);
141     xbt_assert2(trace, "Cannot connect trace %s to link %s: trace undefined",
142                 trace_name, elm);
143
144     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
145   }
146 }
147
148 static void define_callbacks(const char *file)
149 {
150   /* Figuring out the network links */
151   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
152   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
153 }
154
155 static int resource_used(void *resource_id)
156 {
157   return lmm_constraint_used(network_maxmin_system,
158                              ((surf_resource_lmm_t) resource_id)->constraint);
159 }
160
161 static int action_unref(surf_action_t action)
162 {
163   action->refcount--;
164   if (!action->refcount) {
165     xbt_swag_remove(action, action->state_set);
166     if (((surf_action_network_CM02_t) action)->variable)
167       lmm_variable_free(network_maxmin_system,
168                         ((surf_action_network_CM02_t) action)->variable);
169     free(action);
170     return 1;
171   }
172   return 0;
173 }
174
175 static void action_cancel(surf_action_t action)
176 {
177   return;
178 }
179
180 static void action_recycle(surf_action_t action)
181 {
182   return;
183 }
184
185 static double share_resources(double now)
186 {
187   s_surf_action_network_CM02_t s_action;
188   surf_action_network_CM02_t action = NULL;
189   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
190   double min;
191
192   min = generic_maxmin_share_resources(running_actions,
193                                        xbt_swag_offset(s_action,
194                                                        variable),
195                                        network_maxmin_system, network_solve);
196
197 #define VARIABLE(action) (*((lmm_variable_t*)(((char *) (action)) + xbt_swag_offset(s_action, variable)  )))
198
199   xbt_swag_foreach(action, running_actions) {
200     if (action->latency > 0) {
201       if (min < 0)
202         min = action->latency;
203       else if (action->latency < min)
204         min = action->latency;
205     }
206   }
207
208   return min;
209 }
210
211 static void update_actions_state(double now, double delta)
212 {
213   double deltap = 0.0;
214   surf_action_network_CM02_t action = NULL;
215   surf_action_network_CM02_t next_action = NULL;
216   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
217   /*
218      xbt_swag_t failed_actions =
219      surf_network_model->states.failed_action_set;
220    */
221
222   xbt_swag_foreach_safe(action, next_action, running_actions) {
223     deltap = delta;
224     if (action->latency > 0) {
225       if (action->latency > deltap) {
226         double_update(&(action->latency), deltap);
227         deltap = 0.0;
228       } else {
229         double_update(&(deltap), action->latency);
230         action->latency = 0.0;
231       }
232       if ((action->latency == 0.0) && !(action->suspended))
233         lmm_update_variable_weight(network_maxmin_system, action->variable,
234                                    action->weight);
235     }
236     double_update(&(action->generic_action.remains),
237                   lmm_variable_getvalue(action->variable) * deltap);
238     if (action->generic_action.max_duration != NO_MAX_DURATION)
239       double_update(&(action->generic_action.max_duration), delta);
240
241     if ((action->generic_action.remains <= 0) &&
242         (lmm_get_variable_weight(action->variable) > 0)) {
243       action->generic_action.finish = surf_get_clock();
244       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
245     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
246                (action->generic_action.max_duration <= 0)) {
247       action->generic_action.finish = surf_get_clock();
248       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
249     }
250   }
251
252   return;
253 }
254
255 static void update_resource_state(void *id,
256                                   tmgr_trace_event_t event_type,
257                                   double value, double date)
258 {
259   link_CM02_t nw_link = id;
260   /*   printf("[" "%lg" "] Asking to update network card \"%s\" with value " */
261   /*     "%lg" " for event %p\n", surf_get_clock(), nw_link->name, */
262   /*     value, event_type); */
263
264   if (event_type == nw_link->lmm_resource.power.event) {
265     double delta =
266       weight_S_parameter / value - weight_S_parameter /
267           (nw_link->lmm_resource.power.peak * nw_link->lmm_resource.power.scale);
268     lmm_variable_t var = NULL;
269     lmm_element_t elem = NULL;
270     surf_action_network_CM02_t action = NULL;
271
272     nw_link->lmm_resource.power.peak = value;
273     lmm_update_constraint_bound(network_maxmin_system, nw_link->lmm_resource.constraint,
274                                 bandwidth_factor * (nw_link->lmm_resource.power.peak * nw_link->lmm_resource.power.scale));
275     if (weight_S_parameter > 0) {
276       while ((var = lmm_get_var_from_cnst
277               (network_maxmin_system, nw_link->lmm_resource.constraint, &elem))) {
278         action = lmm_variable_id(var);
279         action->weight += delta;
280         if (!(action->suspended))
281           lmm_update_variable_weight(network_maxmin_system, action->variable,
282                                      action->weight);
283       }
284     }
285   } else if (event_type == nw_link->lat_event) {
286     double delta = value - nw_link->lat_current;
287     lmm_variable_t var = NULL;
288     lmm_element_t elem = NULL;
289     surf_action_network_CM02_t action = NULL;
290
291     nw_link->lat_current = value;
292     while ((var = lmm_get_var_from_cnst
293             (network_maxmin_system, nw_link->lmm_resource.constraint, &elem))) {
294       action = lmm_variable_id(var);
295       action->lat_current += delta;
296       action->weight += delta;
297       if (action->rate < 0)
298         lmm_update_variable_bound(network_maxmin_system, action->variable,
299                                   sg_tcp_gamma / (2.0 * action->lat_current));
300       else
301         lmm_update_variable_bound(network_maxmin_system, action->variable,
302                                   min(action->rate,
303                                       sg_tcp_gamma / (2.0 *
304                                                       action->lat_current)));
305       if (!(action->suspended))
306         lmm_update_variable_weight(network_maxmin_system, action->variable,
307                                    action->weight);
308
309     }
310   } else if (event_type == nw_link->lmm_resource.state_event) {
311     if (value > 0)
312       nw_link->lmm_resource.state_current = SURF_RESOURCE_ON;
313     else {
314       lmm_constraint_t cnst = nw_link->lmm_resource.constraint;
315       lmm_variable_t var = NULL;
316       lmm_element_t elem = NULL;
317
318       nw_link->lmm_resource.state_current = SURF_RESOURCE_OFF;
319       while ((var = lmm_get_var_from_cnst
320               (network_maxmin_system, cnst, &elem))) {
321         surf_action_t action = lmm_variable_id(var);
322
323         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
324             surf_action_state_get(action) == SURF_ACTION_READY) {
325           action->finish = date;
326           surf_network_model->action_state_set(action, SURF_ACTION_FAILED);
327         }
328       }
329     }
330   } else {
331     CRITICAL0("Unknown event ! \n");
332     xbt_abort();
333   }
334
335   return;
336 }
337
338 static surf_action_t communicate(const char *src_name, const char *dst_name,int src, int dst, double size,
339                                  double rate)
340 {
341   surf_action_network_CM02_t action = NULL;
342   /* LARGE PLATFORMS HACK:
343      Add a link_CM02_t *link and a int link_nb to network_card_CM02_t. It will represent local links for this node
344      Use the cluster_id for ->id */
345   xbt_dynar_t route = used_routing->get_route(src, dst);
346   /* LARGE PLATFORMS HACK:
347      total_route_size = route_size + src->link_nb + dst->nb */
348   unsigned int i;
349
350   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
351   /* LARGE PLATFORMS HACK:
352      assert on total_route_size */
353   xbt_assert2(xbt_dynar_length(route),
354               "You're trying to send data from %s to %s but there is no connection between these two hosts.",
355               src_name, dst_name);
356
357   link_CM02_t link;
358   int failed=0;
359   xbt_dynar_foreach(route,i,link) {
360     if (link->lmm_resource.state_current == SURF_RESOURCE_OFF) {
361       failed = 1;
362       break;
363     }
364   }
365   action = surf_action_new(sizeof(s_surf_action_network_CM02_t),size,surf_network_model,failed);
366
367   xbt_swag_insert(action, action->generic_action.state_set);
368   action->rate = rate;
369
370   action->latency = 0.0;
371   action->weight = 0.0;
372   xbt_dynar_foreach(route,i,link) {
373     action->latency += link->lat_current;
374     action->weight +=
375       link->lat_current +
376       weight_S_parameter /
377         (link->lmm_resource.power.peak * link->lmm_resource.power.scale);
378   }
379   /* LARGE PLATFORMS HACK:
380      Add src->link and dst->link latencies */
381   action->lat_current = action->latency;
382   action->latency *= latency_factor;
383
384   /* LARGE PLATFORMS HACK:
385      lmm_variable_new(..., total_route_size) */
386   if (action->latency > 0)
387     action->variable =
388       lmm_variable_new(network_maxmin_system, action, 0.0, -1.0, xbt_dynar_length(route));
389   else
390     action->variable =
391       lmm_variable_new(network_maxmin_system, action, 1.0, -1.0, xbt_dynar_length(route));
392
393   if (action->rate < 0) {
394     if (action->lat_current > 0)
395       lmm_update_variable_bound(network_maxmin_system, action->variable,
396                                 sg_tcp_gamma / (2.0 * action->lat_current));
397     else
398       lmm_update_variable_bound(network_maxmin_system, action->variable,
399                                 -1.0);
400   } else {
401     if (action->lat_current > 0)
402       lmm_update_variable_bound(network_maxmin_system, action->variable,
403                                 min(action->rate,
404                                     sg_tcp_gamma / (2.0 *
405                                                     action->lat_current)));
406     else
407       lmm_update_variable_bound(network_maxmin_system, action->variable,
408                                 action->rate);
409   }
410
411   xbt_dynar_foreach(route,i,link) {
412     lmm_expand(network_maxmin_system, link->lmm_resource.constraint,
413                action->variable, 1.0);
414   }
415   /* LARGE PLATFORMS HACK:
416      expand also with src->link and dst->link */
417
418   XBT_OUT;
419
420   return (surf_action_t) action;
421 }
422
423 static double get_link_bandwidth(const void *link)
424 {
425   surf_resource_lmm_t lmm = (surf_resource_lmm_t)link;
426   return lmm->power.peak * lmm->power.scale;
427 }
428
429 static double get_link_latency(const void *link)
430 {
431   return ((link_CM02_t) link)->lat_current;
432 }
433
434 static int link_shared(const void *link)
435 {
436   return lmm_constraint_is_shared(((surf_resource_lmm_t) link)->constraint);
437 }
438
439 static void action_suspend(surf_action_t action)
440 {
441   ((surf_action_network_CM02_t) action)->suspended = 1;
442   lmm_update_variable_weight(network_maxmin_system,
443                              ((surf_action_network_CM02_t) action)->variable,
444                              0.0);
445 }
446
447 static void action_resume(surf_action_t action)
448 {
449   if (((surf_action_network_CM02_t) action)->suspended) {
450     lmm_update_variable_weight(network_maxmin_system,
451                                ((surf_action_network_CM02_t)
452                                 action)->variable,
453                                ((surf_action_network_CM02_t) action)->weight);
454     ((surf_action_network_CM02_t) action)->suspended = 0;
455   }
456 }
457
458 static int action_is_suspended(surf_action_t action)
459 {
460   return ((surf_action_network_CM02_t) action)->suspended;
461 }
462
463 static void action_set_max_duration(surf_action_t action, double duration)
464 {
465   action->max_duration = duration;
466 }
467
468 static void finalize(void)
469 {
470   surf_model_exit(surf_network_model);
471   surf_network_model = NULL;
472
473   used_routing->finalize();
474   lmm_system_free(network_maxmin_system);
475   network_maxmin_system = NULL;
476 }
477
478 static void surf_network_model_init_internal(void)
479 {
480   surf_network_model = surf_model_init();
481
482   surf_network_model->name = "network";
483   surf_network_model->action_unref = action_unref;
484   surf_network_model->action_cancel = action_cancel;
485   surf_network_model->action_recycle = action_recycle;
486
487   surf_network_model->model_private->resource_used = resource_used;
488   surf_network_model->model_private->share_resources = share_resources;
489   surf_network_model->model_private->update_actions_state =
490     update_actions_state;
491   surf_network_model->model_private->update_resource_state =
492     update_resource_state;
493   surf_network_model->model_private->finalize = finalize;
494
495   surf_network_model->suspend = action_suspend;
496   surf_network_model->resume = action_resume;
497   surf_network_model->is_suspended = action_is_suspended;
498   surf_cpu_model->set_max_duration = action_set_max_duration;
499
500   surf_network_model->extension.network.communicate = communicate;
501   surf_network_model->extension.network.get_link_bandwidth =
502     get_link_bandwidth;
503   surf_network_model->extension.network.get_link_latency = get_link_latency;
504   surf_network_model->extension.network.link_shared = link_shared;
505
506   if (!network_maxmin_system)
507     network_maxmin_system = lmm_system_new();
508
509   routing_model_create(sizeof(link_CM02_t),
510       link_new(xbt_strdup("__loopback__"),
511           498000000, NULL, 0.000015, NULL,
512           SURF_RESOURCE_ON, NULL, SURF_LINK_FATPIPE, NULL));
513 }
514
515 /************************************************************************/
516 /* New model based on optimizations discussed during this thesis        */
517 /************************************************************************/
518 void surf_network_model_init_LegrandVelho(const char *filename)
519 {
520
521   if (surf_network_model)
522     return;
523   surf_network_model_init_internal();
524   define_callbacks(filename);
525   xbt_dynar_push(model_list, &surf_network_model);
526   network_solve = lmm_solve;
527
528   latency_factor = 10.4;
529   bandwidth_factor = 0.92;
530   weight_S_parameter = 8775;
531
532   update_model_description(surf_network_model_description,
533                            "LegrandVelho", surf_network_model);
534 }
535
536 /***************************************************************************/
537 /* The nice TCP sharing model designed by Loris Marchal and Henri Casanova */
538 /***************************************************************************/
539 /* @TechReport{      rr-lip2002-40, */
540 /*   author        = {Henri Casanova and Loris Marchal}, */
541 /*   institution   = {LIP}, */
542 /*   title         = {A Network Model for Simulation of Grid Application}, */
543 /*   number        = {2002-40}, */
544 /*   month         = {oct}, */
545 /*   year          = {2002} */
546 /* } */
547 void surf_network_model_init_CM02(const char *filename)
548 {
549
550   if (surf_network_model)
551     return;
552   surf_network_model_init_internal();
553   define_callbacks(filename);
554   xbt_dynar_push(model_list, &surf_network_model);
555   network_solve = lmm_solve;
556
557   update_model_description(surf_network_model_description,
558                            "CM02", surf_network_model);
559 }
560
561 void surf_network_model_init_Reno(const char *filename)
562 {
563   if (surf_network_model)
564     return;
565   surf_network_model_init_internal();
566   define_callbacks(filename);
567
568   xbt_dynar_push(model_list, &surf_network_model);
569   lmm_set_default_protocol_function(func_reno_f, func_reno_fp, func_reno_fpi);
570   network_solve = lagrange_solve;
571
572   latency_factor = 10.4;
573   bandwidth_factor = 0.92;
574   weight_S_parameter = 8775;
575
576   update_model_description(surf_network_model_description,
577                            "Reno", surf_network_model);
578 }
579
580
581 void surf_network_model_init_Reno2(const char *filename)
582 {
583   if (surf_network_model)
584     return;
585   surf_network_model_init_internal();
586   define_callbacks(filename);
587
588   xbt_dynar_push(model_list, &surf_network_model);
589   lmm_set_default_protocol_function(func_reno2_f, func_reno2_fp,
590                                     func_reno2_fpi);
591   network_solve = lagrange_solve;
592
593   latency_factor = 10.4;
594   bandwidth_factor = 0.92;
595   weight_S_parameter = 8775;
596
597   update_model_description(surf_network_model_description,
598                            "Reno2", surf_network_model);
599 }
600
601 void surf_network_model_init_Vegas(const char *filename)
602 {
603   if (surf_network_model)
604     return;
605   surf_network_model_init_internal();
606   define_callbacks(filename);
607
608   xbt_dynar_push(model_list, &surf_network_model);
609   lmm_set_default_protocol_function(func_vegas_f, func_vegas_fp,
610                                     func_vegas_fpi);
611   network_solve = lagrange_solve;
612
613   latency_factor = 10.4;
614   bandwidth_factor = 0.92;
615   weight_S_parameter = 8775;
616
617   update_model_description(surf_network_model_description,
618                            "Vegas", surf_network_model);
619 }