Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak
[simgrid.git] / src / surf / workstation_ptask_L07.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 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 "xbt/ex.h"
9 #include "xbt/str.h"
10 #include "xbt/dict.h"
11 #include "surf_private.h"
12 /* extern lmm_system_t maxmin_system; */
13
14 typedef enum {
15   SURF_WORKSTATION_RESOURCE_CPU,
16   SURF_WORKSTATION_RESOURCE_LINK,
17 } e_surf_workstation_model_type_t;
18
19 /**************************************/
20 /********* cpu object *****************/
21 /**************************************/
22 typedef struct cpu_L07 {
23   surf_model_t model;           /* Do not move this field: must match model_obj_t */
24   xbt_dict_t properties;        /* Do not move this field: must match link_L07_t */
25   e_surf_workstation_model_type_t type; /* Do not move this field: must match link_L07_t */
26   char *name;                   /* Do not move this field: must match link_L07_t */
27   lmm_constraint_t constraint;  /* Do not move this field: must match link_L07_t */
28   double power_scale;
29   double power_current;
30   tmgr_trace_event_t power_event;
31   e_surf_cpu_state_t state_current;
32   tmgr_trace_event_t state_event;
33   int id;                       /* cpu and network card are a single object... */
34 } s_cpu_L07_t, *cpu_L07_t;
35
36 /**************************************/
37 /*********** network object ***********/
38 /**************************************/
39
40 typedef struct link_L07 {
41   surf_model_t model;           /* Do not move this field: must match model_obj_t */
42   xbt_dict_t properties;        /* Do not move this field: must match link_L07_t */
43   e_surf_workstation_model_type_t type; /* Do not move this field: must match cpu_L07_t */
44   char *name;                   /* Do not move this field: must match cpu_L07_t */
45   lmm_constraint_t constraint;  /* Do not move this field: must match cpu_L07_t */
46   double lat_current;
47   tmgr_trace_event_t lat_event;
48   double bw_current;
49   tmgr_trace_event_t bw_event;
50   e_surf_link_state_t state_current;
51   tmgr_trace_event_t state_event;
52 } s_link_L07_t, *link_L07_t;
53
54
55 typedef struct s_route_L07 {
56   link_L07_t *links;
57   int size;
58 } s_route_L07_t, *route_L07_t;
59
60 /**************************************/
61 /*************** actions **************/
62 /**************************************/
63 typedef struct surf_action_workstation_L07 {
64   s_surf_action_t generic_action;
65   lmm_variable_t variable;
66   int workstation_nb;
67   cpu_L07_t *workstation_list;
68   double *computation_amount;
69   double *communication_amount;
70   double latency;
71   double rate;
72   int suspended;
73 } s_surf_action_workstation_L07_t, *surf_action_workstation_L07_t;
74
75
76 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_workstation);
77
78 static int nb_workstation = 0;
79 static s_route_L07_t *routing_table = NULL;
80 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
81 static link_L07_t loopback = NULL;
82 static xbt_dict_t parallel_task_link_set = NULL;
83 lmm_system_t ptask_maxmin_system = NULL;
84
85
86 static void update_action_bound(surf_action_workstation_L07_t action)
87 {
88   int workstation_nb = action->workstation_nb;
89   double lat_current = 0.0;
90   double lat_bound = -1.0;
91   int i, j, k;
92
93   for (i = 0; i < workstation_nb; i++) {
94     for (j = 0; j < workstation_nb; j++) {
95       cpu_L07_t card_src = action->workstation_list[i];
96       cpu_L07_t card_dst = action->workstation_list[j];
97       int route_size = ROUTE(card_src->id, card_dst->id).size;
98       link_L07_t *route = ROUTE(card_src->id, card_dst->id).links;
99       double lat = 0.0;
100
101       if (action->communication_amount[i * workstation_nb + j] > 0) {
102         for (k = 0; k < route_size; k++) {
103           lat += route[k]->lat_current;
104         }
105         lat_current =
106           MAX(lat_current,
107               lat * action->communication_amount[i * workstation_nb + j]);
108       }
109     }
110   }
111   lat_bound = sg_tcp_gamma / (2.0 * lat_current);
112   DEBUG2("action (%p) : lat_bound = %g", action, lat_bound);
113   if ((action->latency == 0.0) && (action->suspended == 0)) {
114     if (action->rate < 0)
115       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
116                                 lat_bound);
117     else
118       lmm_update_variable_bound(ptask_maxmin_system, action->variable,
119                                 min(action->rate, lat_bound));
120   }
121 }
122
123 /**************************************/
124 /******* Resource Public     **********/
125 /**************************************/
126
127 static void *name_service(const char *name)
128 {
129   return xbt_dict_get_or_null(workstation_set, name);
130 }
131
132 static const char *get_resource_name(void *resource_id)
133 {
134   /* We can freely cast as a cpu_L07_t because it has the same
135      prefix as link_L07_t. However, only cpu_L07_t
136      will theoretically be given as an argument here. */
137
138   return ((cpu_L07_t) resource_id)->name;
139 }
140
141 static xbt_dict_t get_properties(void *r)
142 {
143   /* We can freely cast as a cpu_L07_t since it has the same prefix than link_L07_t */
144   return ((cpu_L07_t) r)->properties;
145 }
146
147 /* action_get_state is inherited from the surf module */
148
149 static void action_use(surf_action_t action)
150 {
151   action->refcount++;
152   return;
153 }
154
155 static int action_free(surf_action_t action)
156 {
157   action->refcount--;
158
159   if (!action->refcount) {
160     xbt_swag_remove(action, action->state_set);
161     if (((surf_action_workstation_L07_t) action)->variable)
162       lmm_variable_free(ptask_maxmin_system,
163                         ((surf_action_workstation_L07_t) action)->variable);
164     free(((surf_action_workstation_L07_t) action)->workstation_list);
165     free(((surf_action_workstation_L07_t) action)->communication_amount);
166     free(((surf_action_workstation_L07_t) action)->computation_amount);
167     free(action);
168     return 1;
169   }
170   return 0;
171 }
172
173 static void action_cancel(surf_action_t action)
174 {
175   surf_action_change_state(action, SURF_ACTION_FAILED);
176   return;
177 }
178
179 static void action_recycle(surf_action_t action)
180 {
181   DIE_IMPOSSIBLE;
182   return;
183 }
184
185 /* action_change_state is inherited from the surf module */
186 /* action_set_data is inherited from the surf module */
187
188 static void action_suspend(surf_action_t action)
189 {
190   XBT_IN1("(%p))", action);
191   if (((surf_action_workstation_L07_t) action)->suspended != 2) {
192     ((surf_action_workstation_L07_t) action)->suspended = 1;
193     lmm_update_variable_weight(ptask_maxmin_system,
194                                ((surf_action_workstation_L07_t)
195                                 action)->variable, 0.0);
196   }
197   XBT_OUT;
198 }
199
200 static void action_resume(surf_action_t action)
201 {
202   surf_action_workstation_L07_t act = (surf_action_workstation_L07_t) action;
203
204   XBT_IN1("(%p)", act);
205   if (act->suspended != 2) {
206     lmm_update_variable_weight(ptask_maxmin_system, act->variable, 1.0);
207     act->suspended = 0;
208   }
209   XBT_OUT;
210 }
211
212 static int action_is_suspended(surf_action_t action)
213 {
214   return (((surf_action_workstation_L07_t) action)->suspended == 1);
215 }
216
217 static void action_set_max_duration(surf_action_t action, double duration)
218 {                               /* FIXME: should inherit */
219   XBT_IN2("(%p,%g)", action, duration);
220   action->max_duration = duration;
221   XBT_OUT;
222 }
223
224
225 static void action_set_priority(surf_action_t action, double priority)
226 {                               /* FIXME: should inherit */
227   XBT_IN2("(%p,%g)", action, priority);
228   action->priority = priority;
229   XBT_OUT;
230 }
231
232 /**************************************/
233 /******* Resource Private    **********/
234 /**************************************/
235
236 static int resource_used(void *resource_id)
237 {
238   /* We can freely cast as a link_L07_t because it has
239      the same prefix as cpu_L07_t */
240   return lmm_constraint_used(ptask_maxmin_system,
241                              ((link_L07_t) resource_id)->constraint);
242
243 }
244
245 static double share_resources(double now)
246 {
247   s_surf_action_workstation_L07_t s_action;
248   surf_action_workstation_L07_t action = NULL;
249
250   xbt_swag_t running_actions =
251     surf_workstation_model->common_public->states.running_action_set;
252   double min = generic_maxmin_share_resources(running_actions,
253                                               xbt_swag_offset(s_action,
254                                                               variable),
255                                               ptask_maxmin_system,
256                                               bottleneck_solve);
257
258   xbt_swag_foreach(action, running_actions) {
259     if (action->latency > 0) {
260       if (min < 0) {
261         min = action->latency;
262         DEBUG3("Updating min (value) with %p (start %f): %f", action,
263                action->generic_action.start, min);
264       } else if (action->latency < min) {
265         min = action->latency;
266         DEBUG3("Updating min (latency) with %p (start %f): %f", action,
267                action->generic_action.start, min);
268       }
269     }
270   }
271
272   DEBUG1("min value : %f", min);
273
274   return min;
275 }
276
277 static void update_actions_state(double now, double delta)
278 {
279   double deltap = 0.0;
280   surf_action_workstation_L07_t action = NULL;
281   surf_action_workstation_L07_t next_action = NULL;
282   xbt_swag_t running_actions =
283     surf_workstation_model->common_public->states.running_action_set;
284
285   xbt_swag_foreach_safe(action, next_action, running_actions) {
286     deltap = delta;
287     if (action->latency > 0) {
288       if (action->latency > deltap) {
289         double_update(&(action->latency), deltap);
290         deltap = 0.0;
291       } else {
292         double_update(&(deltap), action->latency);
293         action->latency = 0.0;
294       }
295       if ((action->latency == 0.0) && (action->suspended == 0)) {
296         update_action_bound(action);
297         lmm_update_variable_weight(ptask_maxmin_system, action->variable,
298                                    1.0);
299       }
300     }
301     DEBUG3("Action (%p) : remains (%g) updated by %g.",
302            action, action->generic_action.remains,
303            lmm_variable_getvalue(action->variable) * delta);
304     double_update(&(action->generic_action.remains),
305                   lmm_variable_getvalue(action->variable) * delta);
306
307     if (action->generic_action.max_duration != NO_MAX_DURATION)
308       double_update(&(action->generic_action.max_duration), delta);
309
310     DEBUG2("Action (%p) : remains (%g).",
311            action, action->generic_action.remains);
312     if ((action->generic_action.remains <= 0) &&
313         (lmm_get_variable_weight(action->variable) > 0)) {
314       action->generic_action.finish = surf_get_clock();
315       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
316     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
317                (action->generic_action.max_duration <= 0)) {
318       action->generic_action.finish = surf_get_clock();
319       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
320     } else {
321       /* Need to check that none of the model has failed */
322       lmm_constraint_t cnst = NULL;
323       int i = 0;
324       void *constraint_id = NULL;
325
326       while ((cnst =
327               lmm_get_cnst_from_var(ptask_maxmin_system, action->variable,
328                                     i++))) {
329         constraint_id = lmm_constraint_id(cnst);
330
331 /*      if(((link_L07_t)constraint_id)->type== */
332 /*         SURF_WORKSTATION_RESOURCE_LINK) { */
333 /*        DEBUG2("Checking for link %s (%p)", */
334 /*               ((link_L07_t)constraint_id)->name, */
335 /*               ((link_L07_t)constraint_id)); */
336 /*      } */
337 /*      if(((cpu_L07_t)constraint_id)->type== */
338 /*         SURF_WORKSTATION_RESOURCE_CPU) { */
339 /*        DEBUG3("Checking for cpu %s (%p) : %s", */
340 /*               ((cpu_L07_t)constraint_id)->name, */
341 /*               ((cpu_L07_t)constraint_id), */
342 /*               ((cpu_L07_t)constraint_id)->state_current==SURF_CPU_OFF?"Off":"On"); */
343 /*      } */
344
345         if (((((link_L07_t) constraint_id)->type ==
346               SURF_WORKSTATION_RESOURCE_LINK) &&
347              (((link_L07_t) constraint_id)->state_current ==
348               SURF_LINK_OFF)) ||
349             ((((cpu_L07_t) constraint_id)->type ==
350               SURF_WORKSTATION_RESOURCE_CPU) &&
351              (((cpu_L07_t) constraint_id)->state_current == SURF_CPU_OFF))) {
352           DEBUG1("Action (%p) Failed!!", action);
353           action->generic_action.finish = surf_get_clock();
354           surf_action_change_state((surf_action_t) action,
355                                    SURF_ACTION_FAILED);
356           break;
357         }
358       }
359     }
360   }
361   return;
362 }
363
364 static void update_resource_state(void *id,
365                                   tmgr_trace_event_t event_type,
366                                   double value, double date)
367 {
368   cpu_L07_t cpu = id;
369   link_L07_t nw_link = id;
370
371   if (nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
372     DEBUG2("Updating link %s (%p)", nw_link->name, nw_link);
373     if (event_type == nw_link->bw_event) {
374       nw_link->bw_current = value;
375       lmm_update_constraint_bound(ptask_maxmin_system, nw_link->constraint,
376                                   nw_link->bw_current);
377     } else if (event_type == nw_link->lat_event) {
378       lmm_variable_t var = NULL;
379       surf_action_workstation_L07_t action = NULL;
380       lmm_element_t elem = NULL;
381
382       nw_link->lat_current = value;
383       while ((var = lmm_get_var_from_cnst
384               (ptask_maxmin_system, nw_link->constraint, &elem))) {
385
386
387         action = lmm_variable_id(var);
388         update_action_bound(action);
389       }
390
391     } else if (event_type == nw_link->state_event) {
392       if (value > 0)
393         nw_link->state_current = SURF_LINK_ON;
394       else
395         nw_link->state_current = SURF_LINK_OFF;
396     } else {
397       CRITICAL0("Unknown event ! \n");
398       xbt_abort();
399     }
400     return;
401   } else if (cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
402     DEBUG3("Updating cpu %s (%p) with value %g", cpu->name, cpu, value);
403     if (event_type == cpu->power_event) {
404       cpu->power_current = value;
405       lmm_update_constraint_bound(ptask_maxmin_system, cpu->constraint,
406                                   cpu->power_current * cpu->power_scale);
407     } else if (event_type == cpu->state_event) {
408       if (value > 0)
409         cpu->state_current = SURF_CPU_ON;
410       else
411         cpu->state_current = SURF_CPU_OFF;
412     } else {
413       CRITICAL0("Unknown event ! \n");
414       xbt_abort();
415     }
416     return;
417   } else {
418     DIE_IMPOSSIBLE;
419   }
420   return;
421 }
422
423 static void finalize(void)
424 {
425   int i, j;
426
427   xbt_dict_free(&link_set);
428   xbt_dict_free(&workstation_set);
429   if (parallel_task_link_set != NULL) {
430     xbt_dict_free(&parallel_task_link_set);
431   }
432   xbt_swag_free(surf_workstation_model->common_public->
433                 states.ready_action_set);
434   xbt_swag_free(surf_workstation_model->common_public->
435                 states.running_action_set);
436   xbt_swag_free(surf_workstation_model->common_public->
437                 states.failed_action_set);
438   xbt_swag_free(surf_workstation_model->common_public->
439                 states.done_action_set);
440
441   free(surf_workstation_model->common_public);
442   free(surf_workstation_model->common_private);
443   free(surf_workstation_model->extension_public);
444
445   free(surf_workstation_model);
446   surf_workstation_model = NULL;
447
448   for (i = 0; i < nb_workstation; i++)
449     for (j = 0; j < nb_workstation; j++)
450       free(ROUTE(i, j).links);
451   free(routing_table);
452   routing_table = NULL;
453   nb_workstation = 0;
454
455   if (ptask_maxmin_system) {
456     lmm_system_free(ptask_maxmin_system);
457     ptask_maxmin_system = NULL;
458   }
459 }
460
461 /**************************************/
462 /******* Resource Private    **********/
463 /**************************************/
464
465 static e_surf_cpu_state_t resource_get_state(void *cpu)
466 {
467   return ((cpu_L07_t) cpu)->state_current;
468 }
469
470 static double get_speed(void *cpu, double load)
471 {
472   return load * (((cpu_L07_t) cpu)->power_scale);
473 }
474
475 static double get_available_speed(void *cpu)
476 {
477   return ((cpu_L07_t) cpu)->power_current;
478 }
479
480 static surf_action_t execute_parallel_task(int workstation_nb,
481                                            void **workstation_list,
482                                            double *computation_amount,
483                                            double *communication_amount,
484                                            double amount, double rate)
485 {
486   surf_action_workstation_L07_t action = NULL;
487   int i, j, k;
488   int nb_link = 0;
489   int nb_host = 0;
490   double latency = 0.0;
491
492   if (parallel_task_link_set == NULL)
493     parallel_task_link_set = xbt_dict_new();
494
495   xbt_dict_reset(parallel_task_link_set);
496
497   /* Compute the number of affected resources... */
498   for (i = 0; i < workstation_nb; i++) {
499     for (j = 0; j < workstation_nb; j++) {
500       cpu_L07_t card_src = workstation_list[i];
501       cpu_L07_t card_dst = workstation_list[j];
502       int route_size = ROUTE(card_src->id, card_dst->id).size;
503       link_L07_t *route = ROUTE(card_src->id, card_dst->id).links;
504       double lat = 0.0;
505
506       if (communication_amount[i * workstation_nb + j] > 0)
507         for (k = 0; k < route_size; k++) {
508           lat += route[k]->lat_current;
509           xbt_dict_set(parallel_task_link_set, route[k]->name,
510                        route[k], NULL);
511         }
512       latency = MAX(latency, lat);
513     }
514   }
515
516   nb_link = xbt_dict_length(parallel_task_link_set);
517   xbt_dict_reset(parallel_task_link_set);
518
519   for (i = 0; i < workstation_nb; i++)
520     if (computation_amount[i] > 0)
521       nb_host++;
522
523   action = xbt_new0(s_surf_action_workstation_L07_t, 1);
524   DEBUG3("Creating a parallel task (%p) with %d cpus and %d links.",
525          action, workstation_nb, nb_link);
526   action->generic_action.refcount = 1;
527   action->generic_action.cost = amount;
528   action->generic_action.remains = amount;
529   action->generic_action.max_duration = NO_MAX_DURATION;
530   action->generic_action.start = surf_get_clock();
531   action->generic_action.finish = -1.0;
532   action->generic_action.model_type = (surf_model_t) surf_workstation_model;
533   action->suspended = 0;        /* Should be useless because of the
534                                    calloc but it seems to help valgrind... */
535   action->workstation_nb = workstation_nb;
536   action->workstation_list = (cpu_L07_t *) workstation_list;
537   action->computation_amount = computation_amount;
538   action->communication_amount = communication_amount;
539   action->latency = latency;
540   action->generic_action.state_set =
541     surf_workstation_model->common_public->states.running_action_set;
542
543   xbt_swag_insert(action, action->generic_action.state_set);
544   action->rate = rate;
545
546   action->variable =
547     lmm_variable_new(ptask_maxmin_system, action, 1.0,
548                      (action->rate > 0) ? action->rate : -1.0,
549                      workstation_nb + nb_link);
550
551   if (action->latency > 0)
552     lmm_update_variable_weight(ptask_maxmin_system, action->variable, 0.0);
553
554   for (i = 0; i < workstation_nb; i++)
555     lmm_expand(ptask_maxmin_system,
556                ((cpu_L07_t) workstation_list[i])->constraint,
557                action->variable, computation_amount[i]);
558
559   for (i = 0; i < workstation_nb; i++) {
560     for (j = 0; j < workstation_nb; j++) {
561       cpu_L07_t card_src = workstation_list[i];
562       cpu_L07_t card_dst = workstation_list[j];
563       int route_size = ROUTE(card_src->id, card_dst->id).size;
564       link_L07_t *route = ROUTE(card_src->id, card_dst->id).links;
565
566       if (communication_amount[i * workstation_nb + j] == 0.0)
567         continue;
568       for (k = 0; k < route_size; k++) {
569         lmm_expand_add(ptask_maxmin_system, route[k]->constraint,
570                        action->variable,
571                        communication_amount[i * workstation_nb + j]);
572       }
573     }
574   }
575
576   if (nb_link + nb_host == 0) {
577     action->generic_action.cost = 1.0;
578     action->generic_action.remains = 0.0;
579   }
580
581   return (surf_action_t) action;
582 }
583
584 static surf_action_t execute(void *cpu, double size)
585 {
586   void **workstation_list = xbt_new0(void *, 1);
587   double *computation_amount = xbt_new0(double, 1);
588   double *communication_amount = xbt_new0(double, 1);
589
590   workstation_list[0] = cpu;
591   communication_amount[0] = 0.0;
592   computation_amount[0] = size;
593
594   return execute_parallel_task(1, workstation_list, computation_amount,
595                                communication_amount, 1, -1);
596 }
597
598 static surf_action_t communicate(void *src, void *dst, double size,
599                                  double rate)
600 {
601   void **workstation_list = xbt_new0(void *, 2);
602   double *computation_amount = xbt_new0(double, 2);
603   double *communication_amount = xbt_new0(double, 4);
604   surf_action_t res = NULL;
605
606   workstation_list[0] = src;
607   workstation_list[1] = dst;
608   communication_amount[1] = size;
609
610   res = execute_parallel_task(2, workstation_list,
611                               computation_amount, communication_amount,
612                               1, rate);
613
614   return res;
615 }
616
617 static surf_action_t action_sleep(void *cpu, double duration)
618 {
619   surf_action_workstation_L07_t action = NULL;
620
621   XBT_IN2("(%s,%g)", ((cpu_L07_t) cpu)->name, duration);
622
623   action = (surf_action_workstation_L07_t) execute(cpu, 1.0);
624   action->generic_action.max_duration = duration;
625   action->suspended = 2;
626   lmm_update_variable_weight(ptask_maxmin_system, action->variable, 0.0);
627
628   XBT_OUT;
629   return (surf_action_t) action;
630 }
631
632 /* returns an array of link_L07_t */
633 static const void **get_route(void *src, void *dst)
634 {
635   cpu_L07_t card_src = src;
636   cpu_L07_t card_dst = dst;
637   route_L07_t route = &(ROUTE(card_src->id, card_dst->id));
638
639   return (const void **) route->links;
640 }
641
642 static int get_route_size(void *src, void *dst)
643 {
644   cpu_L07_t card_src = src;
645   cpu_L07_t card_dst = dst;
646   route_L07_t route = &(ROUTE(card_src->id, card_dst->id));
647   return route->size;
648 }
649
650 static const char *get_link_name(const void *link)
651 {
652   return ((link_L07_t) link)->name;
653 }
654
655 static double get_link_bandwidth(const void *link)
656 {
657   return ((link_L07_t) link)->bw_current;
658 }
659
660 static double get_link_latency(const void *link)
661 {
662   return ((link_L07_t) link)->lat_current;
663 }
664
665 static int link_shared(const void *link)
666 {
667   return lmm_constraint_is_shared(((link_L07_t) link)->constraint);
668 }
669
670 /**************************************/
671 /*** Resource Creation & Destruction **/
672 /**************************************/
673
674 static void cpu_free(void *cpu)
675 {
676   free(((cpu_L07_t) cpu)->name);
677   xbt_dict_free(&(((cpu_L07_t) cpu)->properties));
678   free(cpu);
679 }
680
681 static cpu_L07_t cpu_new(const char *name, double power_scale,
682                          double power_initial,
683                          tmgr_trace_t power_trace,
684                          e_surf_cpu_state_t state_initial,
685                          tmgr_trace_t state_trace, xbt_dict_t cpu_properties)
686 {
687   cpu_L07_t cpu = xbt_new0(s_cpu_L07_t, 1);
688   xbt_assert1(!xbt_dict_get_or_null(workstation_set, name),
689               "Host '%s' declared several times in the platform file.", name);
690
691   cpu->model = (surf_model_t) surf_workstation_model;
692   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
693   cpu->name = xbt_strdup(name);
694   cpu->id = nb_workstation++;
695
696   cpu->power_scale = power_scale;
697   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
698
699   cpu->power_current = power_initial;
700   if (power_trace)
701     cpu->power_event =
702       tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
703
704   cpu->state_current = state_initial;
705   if (state_trace)
706     cpu->state_event =
707       tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
708
709   cpu->constraint =
710     lmm_constraint_new(ptask_maxmin_system, cpu,
711                        cpu->power_current * cpu->power_scale);
712
713   /*add the property set */
714   cpu->properties = current_property_set;
715
716   xbt_dict_set(workstation_set, name, cpu, cpu_free);
717
718   return cpu;
719 }
720
721 static void create_routing_table(void)
722 {
723   routing_table = xbt_new0(s_route_L07_t, nb_workstation * nb_workstation);
724 }
725
726 static void parse_cpu_init(void)
727 {
728   double power_scale = 0.0;
729   double power_initial = 0.0;
730   tmgr_trace_t power_trace = NULL;
731   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
732   tmgr_trace_t state_trace = NULL;
733
734   power_scale = get_cpu_power(A_surfxml_host_power);
735   surf_parse_get_double(&power_initial, A_surfxml_host_availability);
736   surf_parse_get_trace(&power_trace, A_surfxml_host_availability_file);
737
738   xbt_assert0((A_surfxml_host_state == A_surfxml_host_state_ON) ||
739               (A_surfxml_host_state == A_surfxml_host_state_OFF),
740               "Invalid state");
741   if (A_surfxml_host_state == A_surfxml_host_state_ON)
742     state_initial = SURF_CPU_ON;
743   if (A_surfxml_host_state == A_surfxml_host_state_OFF)
744     state_initial = SURF_CPU_OFF;
745   surf_parse_get_trace(&state_trace, A_surfxml_host_state_file);
746
747   current_property_set = xbt_dict_new();
748   cpu_new(A_surfxml_host_id, power_scale, power_initial, power_trace,
749           state_initial, state_trace, current_property_set);
750 }
751
752 static void link_free(void *nw_link)
753 {
754   free(((link_L07_t) nw_link)->name);
755   xbt_dict_free(&(((link_L07_t) nw_link)->properties));
756   free(nw_link);
757 }
758
759 static link_L07_t link_new(char *name,
760                            double bw_initial,
761                            tmgr_trace_t bw_trace,
762                            double lat_initial,
763                            tmgr_trace_t lat_trace,
764                            e_surf_link_state_t
765                            state_initial,
766                            tmgr_trace_t state_trace,
767                            e_surf_link_sharing_policy_t
768                            policy, xbt_dict_t properties)
769 {
770   link_L07_t nw_link = xbt_new0(s_link_L07_t, 1);
771   xbt_assert1(!xbt_dict_get_or_null(link_set, name),
772               "Link '%s' declared several times in the platform file.", name);
773
774   nw_link->model = (surf_model_t) surf_workstation_model;
775   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
776   nw_link->name = name;
777   nw_link->bw_current = bw_initial;
778   if (bw_trace)
779     nw_link->bw_event =
780       tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
781   nw_link->state_current = state_initial;
782   nw_link->lat_current = lat_initial;
783   if (lat_trace)
784     nw_link->lat_event =
785       tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
786   if (state_trace)
787     nw_link->state_event =
788       tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
789
790   nw_link->constraint =
791     lmm_constraint_new(ptask_maxmin_system, nw_link, nw_link->bw_current);
792
793   if (policy == SURF_LINK_FATPIPE)
794     lmm_constraint_shared(nw_link->constraint);
795
796   nw_link->properties = properties;
797
798   xbt_dict_set(link_set, name, nw_link, link_free);
799
800   return nw_link;
801 }
802
803 static void parse_link_init(void)
804 {
805   char *name_link;
806   double bw_initial;
807   tmgr_trace_t bw_trace;
808   double lat_initial;
809   tmgr_trace_t lat_trace;
810   e_surf_link_state_t state_initial_link = SURF_LINK_ON;
811   e_surf_link_sharing_policy_t policy_initial_link = SURF_LINK_SHARED;
812   tmgr_trace_t state_trace;
813
814   name_link = xbt_strdup(A_surfxml_link_id);
815   surf_parse_get_double(&bw_initial, A_surfxml_link_bandwidth);
816   surf_parse_get_trace(&bw_trace, A_surfxml_link_bandwidth_file);
817   surf_parse_get_double(&lat_initial, A_surfxml_link_latency);
818   surf_parse_get_trace(&lat_trace, A_surfxml_link_latency_file);
819
820   xbt_assert0((A_surfxml_link_state == A_surfxml_link_state_ON)
821               || (A_surfxml_link_state ==
822                   A_surfxml_link_state_OFF), "Invalid state");
823   if (A_surfxml_link_state == A_surfxml_link_state_ON)
824     state_initial_link = SURF_LINK_ON;
825   else if (A_surfxml_link_state == A_surfxml_link_state_OFF)
826     state_initial_link = SURF_LINK_OFF;
827
828   if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
829     policy_initial_link = SURF_LINK_SHARED;
830   else if (A_surfxml_link_sharing_policy ==
831            A_surfxml_link_sharing_policy_FATPIPE)
832     policy_initial_link = SURF_LINK_FATPIPE;
833
834   surf_parse_get_trace(&state_trace, A_surfxml_link_state_file);
835
836   current_property_set = xbt_dict_new();
837   link_new(name_link, bw_initial, bw_trace, lat_initial, lat_trace,
838            state_initial_link, state_trace, policy_initial_link,
839            current_property_set);
840 }
841
842 static void route_new(int src_id, int dst_id,
843                       link_L07_t * link_list, int nb_link)
844 {
845   route_L07_t route = &(ROUTE(src_id, dst_id));
846
847   route->size = nb_link;
848   route->links = link_list =
849     xbt_realloc(link_list, sizeof(link_L07_t) * nb_link);
850 }
851
852
853 static int src_id = -1;
854 static int dst_id = -1;
855
856 static void parse_route_set_endpoints(void)
857 {
858   cpu_L07_t cpu_tmp = NULL;
859
860   cpu_tmp = (cpu_L07_t) name_service(A_surfxml_route_src);
861   xbt_assert1(cpu_tmp, "Invalid cpu %s", A_surfxml_route_src);
862   if (cpu_tmp != NULL)
863     src_id = cpu_tmp->id;
864
865   cpu_tmp = (cpu_L07_t) name_service(A_surfxml_route_dst);
866   xbt_assert1(cpu_tmp, "Invalid cpu %s", A_surfxml_route_dst);
867   if (cpu_tmp != NULL)
868     dst_id = cpu_tmp->id;
869
870   route_action = A_surfxml_route_action;
871 }
872
873 static void parse_route_set_route(void)
874 {
875   char *name;
876   if (src_id != -1 && dst_id != -1) {
877     name = bprintf("%x#%x", src_id, dst_id);
878     manage_route(route_table, name, route_action, 0);
879     free(name);
880   }
881 }
882
883 static void add_loopback(void)
884 {
885   int i;
886
887   /* Adding loopback if needed */
888   for (i = 0; i < nb_workstation; i++)
889     if (!ROUTE(i, i).size) {
890       if (!loopback)
891         loopback = link_new(xbt_strdup("__MSG_loopback__"),
892                             498000000, NULL, 0.000015, NULL,
893                             SURF_LINK_ON, NULL, SURF_LINK_FATPIPE, NULL);
894
895       ROUTE(i, i).size = 1;
896       ROUTE(i, i).links = xbt_new0(link_L07_t, 1);
897       ROUTE(i, i).links[0] = loopback;
898     }
899 }
900
901 static void add_route(void)
902 {
903   xbt_ex_t e;
904   int nb_link = 0;
905   unsigned int cpt = 0;
906   int link_list_capacity = 0;
907   link_L07_t *link_list = NULL;
908   xbt_dict_cursor_t cursor = NULL;
909   char *key, *data, *end;
910   const char *sep = "#";
911   xbt_dynar_t links, keys;
912   char *link = NULL;
913
914   if (routing_table == NULL)
915     create_routing_table();
916
917   xbt_dict_foreach(route_table, cursor, key, data) {
918     nb_link = 0;
919     links = (xbt_dynar_t) data;
920     keys = xbt_str_split_str(key, sep);
921
922     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
923     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
924     xbt_dynar_free(&keys);
925
926     link_list_capacity = xbt_dynar_length(links);
927     link_list = xbt_new(link_L07_t, link_list_capacity);
928
929
930     xbt_dynar_foreach(links, cpt, link) {
931       TRY {
932         link_list[nb_link++] = xbt_dict_get(link_set, link);
933       }
934       CATCH(e) {
935         RETHROW1("Link %s not found (dict raised this exception: %s)", link);
936       }
937     }
938     route_new(src_id, dst_id, link_list, nb_link);
939   }
940 }
941
942 static void add_traces(void)
943 {
944   xbt_dict_cursor_t cursor = NULL;
945   char *trace_name, *elm;
946
947   if (!trace_connect_list_host_avail)
948     return;
949
950   /* Connect traces relative to cpu */
951   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
952     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
953     cpu_L07_t host = xbt_dict_get_or_null(workstation_set, elm);
954
955     xbt_assert1(host, "Host %s undefined", elm);
956     xbt_assert1(trace, "Trace %s undefined", trace_name);
957
958     host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
959   }
960
961   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
962     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
963     cpu_L07_t host = xbt_dict_get_or_null(workstation_set, elm);
964
965     xbt_assert1(host, "Host %s undefined", elm);
966     xbt_assert1(trace, "Trace %s undefined", trace_name);
967
968     host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host);
969   }
970
971   /* Connect traces relative to network */
972   xbt_dict_foreach(trace_connect_list_link_avail, cursor, trace_name, elm) {
973     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
974     link_L07_t link = xbt_dict_get_or_null(link_set, elm);
975
976     xbt_assert1(link, "Link %s undefined", elm);
977     xbt_assert1(trace, "Trace %s undefined", trace_name);
978
979     link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
980   }
981
982   xbt_dict_foreach(trace_connect_list_bandwidth, cursor, trace_name, elm) {
983     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
984     link_L07_t link = xbt_dict_get_or_null(link_set, elm);
985
986     xbt_assert1(link, "Link %s undefined", elm);
987     xbt_assert1(trace, "Trace %s undefined", trace_name);
988
989     link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
990   }
991
992   xbt_dict_foreach(trace_connect_list_latency, cursor, trace_name, elm) {
993     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
994     link_L07_t link = xbt_dict_get_or_null(link_set, elm);
995
996     xbt_assert1(link, "Link %s undefined", elm);
997     xbt_assert1(trace, "Trace %s undefined", trace_name);
998
999     link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link);
1000   }
1001 /*
1002
1003    xbt_dynar_foreach (traces_connect_list, cpt, value) {
1004      trace_connect = xbt_str_split_str(value, "#");
1005      trace_id        = xbt_dynar_get_as(trace_connect, 0, char*);
1006      connect_element = atoi(xbt_dynar_get_as(trace_connect, 1, char*));
1007      connect_kind    = atoi(xbt_dynar_get_as(trace_connect, 2, char*));
1008      connector_id    = xbt_dynar_get_as(trace_connect, 3, char*);
1009
1010      xbt_assert1((trace = xbt_dict_get_or_null(traces_set_list, trace_id)), "Trace %s undefined", trace_id);
1011
1012      if (connect_element == A_surfxml_trace_c_connect_element_HOST) {
1013         xbt_assert1((host = xbt_dict_get_or_null(workstation_set, connector_id)), "Host %s undefined", connector_id);
1014         switch (connect_kind) {
1015            case A_surfxml_trace_c_connect_kind_AVAILABILITY: host->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
1016            case A_surfxml_trace_c_connect_kind_POWER: host->power_event = tmgr_history_add_trace(history, trace, 0.0, 0, host); break;
1017         }
1018      }
1019      else {
1020         xbt_assert1((link = xbt_dict_get_or_null(link_set, connector_id)), "Link %s undefined", connector_id);
1021         switch (connect_kind) {
1022            case A_surfxml_trace_c_connect_kind_AVAILABILITY: link->state_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1023            case A_surfxml_trace_c_connect_kind_BANDWIDTH: link->bw_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1024            case A_surfxml_trace_c_connect_kind_LATENCY: link->lat_event = tmgr_history_add_trace(history, trace, 0.0, 0, link); break;
1025         }
1026      }
1027    }
1028 */
1029 }
1030
1031 static void define_callbacks(const char *file)
1032 {
1033   /* Adding callback functions */
1034   surf_parse_reset_parser();
1035   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_cpu_init);
1036   surfxml_add_callback(STag_surfxml_link_cb_list, &parse_link_init);
1037   surfxml_add_callback(STag_surfxml_route_cb_list,
1038                        &parse_route_set_endpoints);
1039   surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_route_set_route);
1040   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_route);
1041   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_loopback);
1042   surfxml_add_callback(ETag_surfxml_platform_cb_list, &add_traces);
1043 }
1044
1045
1046 /**************************************/
1047 /********* Module  creation ***********/
1048 /**************************************/
1049
1050 static void model_init_internal(void)
1051 {
1052   s_surf_action_t action;
1053
1054   surf_workstation_model = xbt_new0(s_surf_workstation_model_t, 1);
1055
1056   surf_workstation_model->common_private =
1057     xbt_new0(s_surf_model_private_t, 1);
1058   surf_workstation_model->common_public = xbt_new0(s_surf_model_public_t, 1);
1059   surf_workstation_model->extension_public =
1060     xbt_new0(s_surf_workstation_model_extension_public_t, 1);
1061
1062   surf_workstation_model->common_public->states.ready_action_set =
1063     xbt_swag_new(xbt_swag_offset(action, state_hookup));
1064   surf_workstation_model->common_public->states.running_action_set =
1065     xbt_swag_new(xbt_swag_offset(action, state_hookup));
1066   surf_workstation_model->common_public->states.failed_action_set =
1067     xbt_swag_new(xbt_swag_offset(action, state_hookup));
1068   surf_workstation_model->common_public->states.done_action_set =
1069     xbt_swag_new(xbt_swag_offset(action, state_hookup));
1070
1071   surf_workstation_model->common_public->name_service = name_service;
1072   surf_workstation_model->common_public->get_resource_name =
1073     get_resource_name;
1074   surf_workstation_model->common_public->action_get_state =
1075     surf_action_get_state;
1076   surf_workstation_model->common_public->action_get_start_time =
1077     surf_action_get_start_time;
1078   surf_workstation_model->common_public->action_get_finish_time =
1079     surf_action_get_finish_time;
1080   surf_workstation_model->common_public->action_use = action_use;
1081   surf_workstation_model->common_public->action_free = action_free;
1082   surf_workstation_model->common_public->action_cancel = action_cancel;
1083   surf_workstation_model->common_public->action_recycle = action_recycle;
1084   surf_workstation_model->common_public->action_change_state =
1085     surf_action_change_state;
1086   surf_workstation_model->common_public->action_set_data =
1087     surf_action_set_data;
1088   surf_workstation_model->common_public->suspend = action_suspend;
1089   surf_workstation_model->common_public->resume = action_resume;
1090   surf_workstation_model->common_public->is_suspended = action_is_suspended;
1091   surf_workstation_model->common_public->set_max_duration =
1092     action_set_max_duration;
1093   surf_workstation_model->common_public->set_priority = action_set_priority;
1094   surf_workstation_model->common_public->name = "Workstation ptask_L07";
1095
1096   surf_workstation_model->common_private->resource_used = resource_used;
1097   surf_workstation_model->common_private->share_resources = share_resources;
1098   surf_workstation_model->common_private->update_actions_state =
1099     update_actions_state;
1100   surf_workstation_model->common_private->update_resource_state =
1101     update_resource_state;
1102   surf_workstation_model->common_private->finalize = finalize;
1103
1104   surf_workstation_model->extension_public->execute = execute;
1105   surf_workstation_model->extension_public->sleep = action_sleep;
1106   surf_workstation_model->extension_public->get_state = resource_get_state;
1107   surf_workstation_model->extension_public->get_speed = get_speed;
1108   surf_workstation_model->extension_public->get_available_speed =
1109     get_available_speed;
1110   surf_workstation_model->extension_public->communicate = communicate;
1111   surf_workstation_model->extension_public->execute_parallel_task =
1112     execute_parallel_task;
1113   surf_workstation_model->extension_public->get_route = get_route;
1114   surf_workstation_model->extension_public->get_route_size = get_route_size;
1115   surf_workstation_model->extension_public->get_link_name = get_link_name;
1116   surf_workstation_model->extension_public->get_link_bandwidth =
1117     get_link_bandwidth;
1118   surf_workstation_model->extension_public->get_link_latency =
1119     get_link_latency;
1120   surf_workstation_model->extension_public->link_shared = link_shared;
1121
1122   surf_workstation_model->common_public->get_properties = get_properties;
1123
1124   workstation_set = xbt_dict_new();
1125   link_set = xbt_dict_new();
1126
1127   if (!ptask_maxmin_system)
1128     ptask_maxmin_system = lmm_system_new();
1129 }
1130
1131 /**************************************/
1132 /*************** Generic **************/
1133 /**************************************/
1134 void surf_workstation_model_init_ptask_L07(const char *filename)
1135 {
1136   xbt_assert0(!surf_cpu_model, "CPU model type already defined");
1137   xbt_assert0(!surf_network_model, "network model type already defined");
1138   model_init_internal();
1139   define_callbacks(filename);
1140
1141   update_model_description(surf_workstation_model_description,
1142                            "ptask_L07",
1143                            (surf_model_t) surf_workstation_model);
1144   xbt_dynar_push(model_list, &surf_workstation_model);
1145 }