Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f0ba1e694a6d5c0d64d6f3a22edb6a9a258bb217
[simgrid.git] / src / surf / workstation_KCCFLN05.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2005 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/dict.h"
10 #include "workstation_KCCFLN05_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(workstation_KCCFLN05, surf,
13                                 "Logging specific to the SURF workstation module (KCCFLN05)");
14
15 static int nb_workstation = 0;
16 static s_route_KCCFLN05_t *routing_table = NULL;
17 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
18 static network_link_KCCFLN05_t loopback = NULL;
19
20 static xbt_dict_t network_link_set = NULL;
21
22 /* convenient function */
23 static void __update_cpu_usage(cpu_KCCFLN05_t cpu)
24 {
25   int cpt;
26   surf_action_workstation_KCCFLN05_t action = NULL;
27   if ((!xbt_dynar_length(cpu->incomming_communications)) &&
28       (!xbt_dynar_length(cpu->outgoing_communications))) {
29     /* No communications */
30     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
31                                 cpu->power_current * cpu->power_scale);
32   } else if ((!xbt_dynar_length(cpu->incomming_communications))
33              && (xbt_dynar_length(cpu->outgoing_communications))) {
34     /* Emission */
35     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
36                                 cpu->power_current * cpu->power_scale *
37                                 cpu->interference_send);
38     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
39       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
40                          cpu->power_current * cpu->power_scale * 
41                          ROUTE(action->src->id, action->dst->id).impact_on_src
42                          );
43   } else if ((xbt_dynar_length(cpu->incomming_communications)) &&
44              (!xbt_dynar_length(cpu->outgoing_communications))) {
45     /* Reception */
46     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
47                                 cpu->power_current * cpu->power_scale *
48                                 cpu->interference_recv);
49     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
50       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
51                          cpu->power_current * cpu->power_scale * 
52                          ROUTE(action->src->id, action->dst->id).impact_on_dst
53                          );
54   } else {
55     /* Emission & Reception */
56     lmm_update_constraint_bound(maxmin_system, cpu->constraint,
57                                 cpu->power_current * cpu->power_scale *
58                                 cpu->interference_send_recv);
59     xbt_dynar_foreach(cpu->outgoing_communications, cpt, action)
60       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
61                          cpu->power_current * cpu->power_scale * 
62                          ROUTE(action->src->id, action->dst->id).impact_on_src_with_other_recv
63                          );
64     xbt_dynar_foreach(cpu->incomming_communications, cpt, action)
65       lmm_elem_set_value(maxmin_system,cpu->constraint,action->variable,
66                          cpu->power_current * cpu->power_scale * 
67                          ROUTE(action->src->id, action->dst->id).impact_on_dst_with_other_send
68                          );
69   }
70 }
71
72 /**************************************/
73 /******* Resource Public     **********/
74 /**************************************/
75
76 static void *name_service(const char *name)
77 {
78   return xbt_dict_get_or_null(workstation_set, name);
79 }
80
81 static const char *get_resource_name(void *resource_id)
82 {
83   /* We can freely cast as a cpu_KCCFLN05_t because it has the same
84      prefix as network_link_KCCFLN05_t. However, only cpu_KCCFLN05_t
85      will theoretically be given as an argument here. */
86   return ((cpu_KCCFLN05_t) resource_id)->name;
87 }
88
89 /* action_get_state is inherited from the surf module */
90
91 static void action_use(surf_action_t action)
92 {
93   action->using++;
94   return;
95 }
96
97 static int action_free(surf_action_t action)
98 {
99   int cpt;
100   surf_action_t act = NULL;
101   cpu_KCCFLN05_t src = ((surf_action_workstation_KCCFLN05_t) action)->src;
102   cpu_KCCFLN05_t dst = ((surf_action_workstation_KCCFLN05_t) action)->dst;
103
104   action->using--;
105   if (!action->using) {
106     xbt_swag_remove(action, action->state_set);
107     if (((surf_action_workstation_KCCFLN05_t) action)->variable)
108       lmm_variable_free(maxmin_system,
109                         ((surf_action_workstation_KCCFLN05_t) action)->
110                         variable);
111     if(src)
112       xbt_dynar_foreach(src->outgoing_communications, cpt, act)
113         if (act == action) {
114           xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
115           break;
116         }    
117
118     if(dst)
119       xbt_dynar_foreach(dst->incomming_communications, cpt, act)
120         if (act == action) {
121           xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
122           break;
123         }
124
125     if(src && (!xbt_dynar_length(src->outgoing_communications)))
126       __update_cpu_usage(src);
127     if(dst && (!xbt_dynar_length(dst->incomming_communications)))
128       __update_cpu_usage(dst);
129
130     free(action);
131     return 1;
132   }
133   return 0;
134 }
135
136 static void action_cancel(surf_action_t action)
137 {
138   surf_action_change_state(action, SURF_ACTION_FAILED);
139   return;
140 }
141
142 static void action_recycle(surf_action_t action)
143 {
144   DIE_IMPOSSIBLE;
145   return;
146 }
147
148 /* action_change_state is inherited from the surf module */
149 /* action_set_data is inherited from the surf module */
150
151 static void action_suspend(surf_action_t action)
152 {
153   XBT_IN1("(%p))",action);
154   ((surf_action_workstation_KCCFLN05_t) action)->suspended = 1;
155   lmm_update_variable_weight(maxmin_system,
156                              ((surf_action_workstation_KCCFLN05_t)
157                               action)->variable, 0.0);
158   XBT_OUT;
159 }
160
161 static void action_resume(surf_action_t action)
162 {
163   XBT_IN1("(%p)",action);
164   if(((surf_action_workstation_KCCFLN05_t)action)->lat_current==0.0)
165     lmm_update_variable_weight(maxmin_system,
166                                ((surf_action_workstation_KCCFLN05_t)
167                                 action)->variable, 1.0);
168   else
169     lmm_update_variable_weight(maxmin_system,
170                                ((surf_action_workstation_KCCFLN05_t) action)->variable, 
171                                ((surf_action_workstation_KCCFLN05_t) action)->lat_current);
172
173   ((surf_action_workstation_KCCFLN05_t) action)->suspended = 0;
174   XBT_OUT;
175 }
176
177 static int action_is_suspended(surf_action_t action)
178 {
179   return (((surf_action_workstation_KCCFLN05_t) action)->suspended);
180 }
181
182 static void action_set_max_duration(surf_action_t action, double duration)
183 {                               /* FIXME: should inherit */
184   XBT_IN2("(%p,%g)",action,duration);
185   action->max_duration = duration;
186   XBT_OUT;
187 }
188
189
190 static void action_set_priority(surf_action_t action, double priority)
191 {                               /* FIXME: should inherit */
192   XBT_IN2("(%p,%g)",action,priority);
193   action->priority = priority;
194   XBT_OUT;
195 }
196
197 /**************************************/
198 /******* Resource Private    **********/
199 /**************************************/
200
201 static int resource_used(void *resource_id)
202 {
203   /* We can freely cast as a network_link_KCCFLN05_t because it has
204      the same prefix as cpu_KCCFLN05_t */
205   if(((cpu_KCCFLN05_t) resource_id)->type == SURF_WORKSTATION_RESOURCE_CPU)
206     return (lmm_constraint_used(maxmin_system,
207                                ((cpu_KCCFLN05_t) resource_id)->
208                                constraint) || 
209             ((((cpu_KCCFLN05_t) resource_id)->bus)?
210              lmm_constraint_used(maxmin_system,
211                                 ((cpu_KCCFLN05_t) resource_id)->
212                                 bus):0));
213   else 
214     return lmm_constraint_used(maxmin_system,
215                                ((network_link_KCCFLN05_t) resource_id)->
216                                constraint);
217
218 }
219
220 static double share_resources(double now)
221 {
222   s_surf_action_workstation_KCCFLN05_t s_action;
223   surf_action_workstation_KCCFLN05_t action = NULL;
224
225   xbt_swag_t running_actions = surf_workstation_resource->common_public->states.running_action_set;
226   double min = generic_maxmin_share_resources(running_actions,
227                                               xbt_swag_offset(s_action, variable));
228
229   xbt_swag_foreach(action, running_actions) {
230     if(action->latency>0) {
231       if(min<0) min = action->latency;
232       else if (action->latency<min) min = action->latency;
233     }
234   }
235
236   return min;
237 /*   return generic_maxmin_share_resources(surf_workstation_resource->common_public-> */
238 /*                                      states.running_action_set, */
239 /*                                      xbt_swag_offset(action, variable)); */
240 }
241
242 static void update_actions_state(double now, double delta)
243 {
244   double deltap = 0.0;
245   surf_action_workstation_KCCFLN05_t action = NULL;
246   surf_action_workstation_KCCFLN05_t next_action = NULL;
247   xbt_swag_t running_actions =
248       surf_workstation_resource->common_public->states.running_action_set;
249
250   xbt_swag_foreach_safe(action, next_action, running_actions) {
251     deltap = delta;
252     if (action->latency > 0) {
253       if (action->latency > deltap) {
254         surf_double_update(&(action->latency), deltap);
255         deltap = 0.0;
256       } else {
257         surf_double_update(&(deltap), action->latency);
258         action->latency = 0.0;
259       }
260       if ((action->latency == 0.0) && !(action->suspended))
261         lmm_update_variable_weight(maxmin_system, action->variable, 1.0);
262     }
263     surf_double_update(&(action->generic_action.remains),
264                        lmm_variable_getvalue(action->variable) * deltap);
265     if (action->generic_action.max_duration != NO_MAX_DURATION)
266       surf_double_update(&(action->generic_action.max_duration), delta);
267
268     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
269
270     if ((action->generic_action.remains <= 0) && 
271         (lmm_get_variable_weight(action->variable)>0)) {
272       action->generic_action.finish = surf_get_clock();
273       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
274     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
275                (action->generic_action.max_duration <= 0)) {
276       action->generic_action.finish = surf_get_clock();
277       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
278     } else {                    /* Need to check that none of the resource has failed */
279       lmm_constraint_t cnst = NULL;
280       int i = 0;
281       void *constraint_id = NULL;
282
283       while ((cnst =
284               lmm_get_cnst_from_var(maxmin_system, action->variable,
285                                     i++))) {
286         constraint_id = lmm_constraint_id(cnst);
287         if(((((network_link_KCCFLN05_t)constraint_id)->type==
288              SURF_WORKSTATION_RESOURCE_LINK) &&
289             (((network_link_KCCFLN05_t)constraint_id)->state_current==
290              SURF_NETWORK_LINK_OFF)) ||
291            ((((cpu_KCCFLN05_t)constraint_id)->type==
292              SURF_WORKSTATION_RESOURCE_CPU) &&
293             (((cpu_KCCFLN05_t)constraint_id)->state_current==
294              SURF_CPU_OFF))) {
295           action->generic_action.finish = surf_get_clock();
296           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
297           break;
298         }
299       }
300     }
301   }
302   return;
303 }
304
305 static void update_resource_state(void *id,
306                                   tmgr_trace_event_t event_type,
307                                   double value)
308 {
309   cpu_KCCFLN05_t cpu = id;
310   network_link_KCCFLN05_t nw_link = id ;
311
312   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
313     if (event_type == nw_link->bw_event) {
314       nw_link->bw_current = value;
315       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
316                                   nw_link->bw_current);
317     } else if (event_type == nw_link->lat_event) {
318       double delta = value - nw_link->lat_current;
319       lmm_variable_t var = NULL;
320       surf_action_workstation_KCCFLN05_t action = NULL;
321       
322       nw_link->lat_current = value;
323       while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
324         action = lmm_variable_id(var);
325         action->lat_current += delta;
326         if(action->rate<0)
327           lmm_update_variable_bound(maxmin_system, action->variable,
328                                     SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
329         else 
330           lmm_update_variable_bound(maxmin_system, action->variable,
331                                     min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
332         if(!(action->suspended))
333           lmm_update_variable_weight(maxmin_system, action->variable, 
334                                      action->lat_current);
335       }
336     } else if (event_type == nw_link->state_event) {
337       if (value > 0)
338         nw_link->state_current = SURF_NETWORK_LINK_ON;
339       else
340         nw_link->state_current = SURF_NETWORK_LINK_OFF;
341     } else {
342       CRITICAL0("Unknown event ! \n");
343       xbt_abort();
344     }
345     return;
346   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
347     if (event_type == cpu->power_event) {
348       cpu->power_current = value;
349       __update_cpu_usage(cpu);
350     } else if (event_type == cpu->state_event) {
351       if (value > 0)
352         cpu->state_current = SURF_CPU_ON;
353       else
354         cpu->state_current = SURF_CPU_OFF;
355     } else {
356       CRITICAL0("Unknown event ! \n");
357       xbt_abort();
358     }    
359     return;
360   } else {
361     DIE_IMPOSSIBLE;
362   }
363   return;
364 }
365
366 static void finalize(void)
367 {
368   int i,j;
369
370   xbt_dict_free(&network_link_set);
371   xbt_dict_free(&workstation_set);
372   xbt_swag_free(surf_workstation_resource->common_public->states.
373                 ready_action_set);
374   xbt_swag_free(surf_workstation_resource->common_public->states.
375                 running_action_set);
376   xbt_swag_free(surf_workstation_resource->common_public->states.
377                 failed_action_set);
378   xbt_swag_free(surf_workstation_resource->common_public->states.
379                 done_action_set);
380
381   free(surf_workstation_resource->common_public);
382   free(surf_workstation_resource->common_private);
383   free(surf_workstation_resource->extension_public);
384
385   free(surf_workstation_resource);
386   surf_workstation_resource = NULL;
387
388   for (i = 0; i < nb_workstation; i++)
389     for (j = 0; j < nb_workstation; j++)
390       free(ROUTE(i, j).links);
391   free(routing_table);
392   routing_table = NULL;
393   nb_workstation = 0;
394
395   if (maxmin_system) {
396     lmm_system_free(maxmin_system);
397     maxmin_system = NULL;
398   }
399 }
400
401 /**************************************/
402 /******* Resource Private    **********/
403 /**************************************/
404
405 static surf_action_t execute(void *cpu, double size)
406 {
407   surf_action_workstation_KCCFLN05_t action = NULL;
408   cpu_KCCFLN05_t CPU = cpu;
409
410   XBT_IN2("(%s,%g)",CPU->name,size);
411   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
412
413   action->generic_action.using = 1;
414   action->generic_action.cost = size;
415   action->generic_action.remains = size;
416   action->generic_action.priority = 1.0;
417   action->generic_action.max_duration = NO_MAX_DURATION;
418   action->generic_action.start = surf_get_clock();
419   action->generic_action.finish = -1.0;
420   action->generic_action.resource_type =
421       (surf_resource_t) surf_workstation_resource;
422   action->suspended = 0;
423
424   if (CPU->state_current == SURF_CPU_ON)
425     action->generic_action.state_set =
426         surf_workstation_resource->common_public->states.running_action_set;
427   else
428     action->generic_action.state_set =
429         surf_workstation_resource->common_public->states.failed_action_set;
430   xbt_swag_insert(action, action->generic_action.state_set);
431
432   action->variable = lmm_variable_new(maxmin_system, action, 
433                                       action->generic_action.priority, -1.0, 1);
434   lmm_expand(maxmin_system, CPU->constraint, action->variable,
435              1.0);
436   XBT_OUT;
437   return (surf_action_t) action;
438 }
439
440 static surf_action_t action_sleep(void *cpu, double duration)
441 {
442   surf_action_workstation_KCCFLN05_t action = NULL;
443
444   XBT_IN2("(%s,%g)",((cpu_KCCFLN05_t)cpu)->name,duration);
445
446   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
447   action->generic_action.max_duration = duration;
448   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
449
450   XBT_OUT;
451   return (surf_action_t) action;
452 }
453
454 static e_surf_cpu_state_t resource_get_state(void *cpu)
455 {
456   return ((cpu_KCCFLN05_t) cpu)->state_current;
457 }
458
459 static double get_speed(void *cpu, double load)
460 {
461   return load*(((cpu_KCCFLN05_t) cpu)->power_scale);
462 }
463
464 static surf_action_t communicate(void *src, void *dst, double size, double rate)
465 {
466   surf_action_workstation_KCCFLN05_t action = NULL;
467   cpu_KCCFLN05_t card_src = src;
468   cpu_KCCFLN05_t card_dst = dst;
469   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
470   int route_size = route->size;
471   int i;
472
473   XBT_IN4("(%s,%s,%g,%g)",card_src->name,card_dst->name,size,rate);
474   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);
475
476   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
477
478   action->generic_action.using = 1;
479   action->generic_action.cost = size;
480   action->generic_action.remains = size;
481   action->generic_action.max_duration = NO_MAX_DURATION;
482   action->generic_action.start = -1.0;
483   action->generic_action.finish = -1.0;
484   action->src = src;
485   action->dst = dst;
486   action->generic_action.resource_type =
487     (surf_resource_t) surf_workstation_resource;
488   action->suspended = 0;  /* Should be useless because of the 
489                              calloc but it seems to help valgrind... */
490   action->generic_action.state_set =
491       surf_workstation_resource->common_public->states.running_action_set;
492
493   xbt_dynar_push(card_src->outgoing_communications, &action);
494   xbt_dynar_push(card_dst->incomming_communications, &action);
495
496   xbt_swag_insert(action, action->generic_action.state_set);
497   action->rate = rate;
498
499   action->latency = 0.0;
500   for (i = 0; i < route_size; i++)
501     action->latency += route->links[i]->lat_current;
502   action->lat_current = action->latency;
503
504   if(action->latency>0)
505     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
506                                         route_size+4); /* +1 for the src bus
507                                                           +1 for the dst bus
508                                                           +1 for the src cpu
509                                                           +1 for the dst cpu */
510   else
511     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
512                                         route_size+4);
513
514   if(action->rate<0) {
515     if(action->lat_current>0)
516       lmm_update_variable_bound(maxmin_system, action->variable,
517                                 SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
518     else
519       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
520   } else {
521     if(action->lat_current>0)
522       lmm_update_variable_bound(maxmin_system, action->variable,
523                                 min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
524     else
525       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
526   }
527
528   for (i = 0; i < route_size; i++)
529     lmm_expand(maxmin_system, route->links[i]->constraint, action->variable, 1.0);
530   if (card_src->bus)
531     lmm_expand(maxmin_system, card_src->bus, action->variable, 1.0);
532   if (card_dst->bus)
533     lmm_expand(maxmin_system, card_dst->bus, action->variable, 1.0);
534   lmm_expand(maxmin_system, card_src->constraint, action->variable, 0.0);
535   lmm_expand(maxmin_system, card_dst->constraint, action->variable, 0.0);
536
537   XBT_OUT;
538   return (surf_action_t) action;
539 }
540
541 /* FIXME: execute_parallel_task */
542 static surf_action_t execute_parallel_task(int cpu_nb,
543                                            void **cpu_list, 
544                                            double *computation_amount, 
545                                            double *communication_amount,
546                                            double amount,
547                                            double rate)
548 {
549   DIE_IMPOSSIBLE;
550   return NULL;
551 }
552
553
554 /**************************************/
555 /*** Resource Creation & Destruction **/
556 /**************************************/
557
558 static void cpu_free(void *cpu)
559 {
560   free(((cpu_KCCFLN05_t) cpu)->name);
561   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
562   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
563   free(cpu);
564 }
565
566 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
567                               double power_initial,
568                               tmgr_trace_t power_trace,
569                               e_surf_cpu_state_t state_initial,
570                               tmgr_trace_t state_trace,
571                               double interference_send,
572                               double interference_recv,
573                               double interference_send_recv,
574                               double max_outgoing_rate)
575 {
576   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
577
578   cpu->resource = (surf_resource_t) surf_workstation_resource;
579   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
580   cpu->name = xbt_strdup(name);
581   cpu->id = nb_workstation++;
582
583   cpu->power_scale = power_scale;
584   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
585
586   cpu->power_current = power_initial;
587   if (power_trace)
588     cpu->power_event =
589         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
590
591   cpu->state_current = state_initial;
592   if (state_trace)
593     cpu->state_event =
594         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
595
596   cpu->interference_send = interference_send;
597   cpu->interference_recv = interference_recv;
598   cpu->interference_send_recv = interference_send_recv;
599
600   cpu->constraint =
601       lmm_constraint_new(maxmin_system, cpu,
602                          cpu->power_current * cpu->power_scale);
603   if (max_outgoing_rate > 0)
604     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
605
606   cpu->incomming_communications =
607       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
608   cpu->outgoing_communications =
609       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
610
611   xbt_dict_set(workstation_set, name, cpu, cpu_free);
612
613   return cpu;
614 }
615
616 static void parse_cpu(void)
617 {
618   double power_scale = 0.0;
619   double power_initial = 0.0;
620   tmgr_trace_t power_trace = NULL;
621   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
622   tmgr_trace_t state_trace = NULL;
623   double interference_send = 0.0;
624   double interference_recv = 0.0;
625   double interference_send_recv = 0.0;
626   double max_outgoing_rate = -1.0;
627
628   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
629   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
630   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
631
632   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
633               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
634               "Invalid state");
635   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
636     state_initial = SURF_CPU_ON;
637   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
638     state_initial = SURF_CPU_OFF;
639   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
640
641   surf_parse_get_double(&interference_send,
642                         A_surfxml_cpu_interference_send);
643   surf_parse_get_double(&interference_recv,
644                         A_surfxml_cpu_interference_recv);
645   surf_parse_get_double(&interference_send_recv,
646                         A_surfxml_cpu_interference_send_recv);
647   surf_parse_get_double(&max_outgoing_rate,
648                         A_surfxml_cpu_max_outgoing_rate);
649
650   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
651           state_initial, state_trace, interference_send, interference_recv,
652           interference_send_recv, max_outgoing_rate);
653 }
654
655 static void create_routing_table(void)
656 {
657   routing_table =
658       xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
659 }
660
661 static void network_link_free(void *nw_link)
662 {
663   free(((network_link_KCCFLN05_t) nw_link)->name);
664   free(nw_link);
665 }
666
667 static network_link_KCCFLN05_t network_link_new(char *name,
668                                                 double bw_initial,
669                                                 tmgr_trace_t bw_trace,
670                                                 double lat_initial,
671                                                 tmgr_trace_t lat_trace,
672                                                 e_surf_network_link_state_t
673                                                 state_initial,
674                                                 tmgr_trace_t state_trace,
675                                                 e_surf_network_link_sharing_policy_t policy)
676 {
677   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
678
679
680   nw_link->resource = (surf_resource_t) surf_workstation_resource;
681   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
682   nw_link->name = name;
683   nw_link->bw_current = bw_initial;
684   if (bw_trace)
685     nw_link->bw_event =
686         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
687   nw_link->state_current = state_initial;
688   nw_link->lat_current = lat_initial;
689   if (lat_trace)
690     nw_link->lat_event =
691         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
692   if (state_trace)
693     nw_link->state_event =
694         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
695
696   nw_link->constraint =
697       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
698
699   if(policy == SURF_NETWORK_LINK_FATPIPE)
700     lmm_constraint_shared(nw_link->constraint);
701
702   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
703
704   return nw_link;
705 }
706
707 static void parse_network_link(void)
708 {
709   char *name;
710   double bw_initial;
711   tmgr_trace_t bw_trace;
712   double lat_initial;
713   tmgr_trace_t lat_trace;
714   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
715   e_surf_network_link_sharing_policy_t policy_initial = SURF_NETWORK_LINK_SHARED;
716   tmgr_trace_t state_trace;
717
718   name = xbt_strdup(A_surfxml_network_link_name);
719   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
720   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
721   surf_parse_get_double(&lat_initial,A_surfxml_network_link_latency);
722   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
723
724   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
725               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
726               "Invalid state");
727   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
728     state_initial = SURF_NETWORK_LINK_ON;
729   else if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
730     state_initial = SURF_NETWORK_LINK_OFF;
731
732   if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_SHARED) 
733     policy_initial = SURF_NETWORK_LINK_SHARED;
734   else if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_FATPIPE) 
735     policy_initial = SURF_NETWORK_LINK_FATPIPE;
736
737   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
738
739   network_link_new(name, bw_initial, bw_trace,
740                    lat_initial, lat_trace, state_initial, state_trace,
741                    policy_initial);
742 }
743
744 static void route_new(int src_id, int dst_id, char **links, int nb_link,
745                       double impact_on_src, double impact_on_dst,
746                       double impact_on_src_with_other_recv,
747                       double impact_on_dst_with_other_send)
748 {
749   network_link_KCCFLN05_t *link_list = NULL;
750   int i;
751   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
752
753   route->size = nb_link;
754   link_list = route->links = xbt_new0(network_link_KCCFLN05_t, nb_link);
755   for (i = 0; i < nb_link; i++) {
756     link_list[i] = xbt_dict_get_or_null(network_link_set, links[i]);
757     free(links[i]);
758   }
759   free(links);
760   route->impact_on_src = impact_on_src;
761   route->impact_on_dst = impact_on_src;
762   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
763   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
764 }
765
766 static int nb_link = 0;
767 static char **link_name = NULL;
768 static int src_id = -1;
769 static int dst_id = -1;
770 static double impact_on_src;
771 static double impact_on_dst;
772 static double impact_on_src_with_other_recv;
773 static double impact_on_dst_with_other_send;
774
775 static void parse_route_set_endpoints(void)
776 {
777   src_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_src))->id;
778   dst_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_dst))->id;
779   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
780   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
781   surf_parse_get_double(&impact_on_src_with_other_recv,
782                         A_surfxml_route_impact_on_src_with_other_recv);
783   surf_parse_get_double(&impact_on_dst_with_other_send,
784                         A_surfxml_route_impact_on_dst_with_other_send);
785
786   nb_link = 0;
787   link_name = NULL;
788 }
789
790 static void parse_route_elem(void)
791 {
792   nb_link++;
793   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
794   link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
795 }
796
797 static void parse_route_set_route(void)
798 {
799   route_new(src_id, dst_id, link_name, nb_link, impact_on_src,
800             impact_on_dst, impact_on_src_with_other_recv,
801             impact_on_dst_with_other_send);
802 }
803
804 static void parse_file(const char *file)
805 {
806   int i ;
807
808   /* Figuring out the cpus */
809   surf_parse_reset_parser();
810   ETag_surfxml_cpu_fun = parse_cpu;
811   surf_parse_open(file);
812   xbt_assert1((!surf_parse()), "Parse error in %s", file);
813   surf_parse_close();
814
815   create_routing_table();
816
817   /* Figuring out the network links */
818   surf_parse_reset_parser();
819   ETag_surfxml_network_link_fun = parse_network_link;
820   surf_parse_open(file);
821   xbt_assert1((!surf_parse()), "Parse error in %s", file);
822   surf_parse_close();
823
824   /* Building the routes */
825   surf_parse_reset_parser();
826   STag_surfxml_route_fun = parse_route_set_endpoints;
827   ETag_surfxml_route_element_fun = parse_route_elem;
828   ETag_surfxml_route_fun = parse_route_set_route;
829   surf_parse_open(file);
830   xbt_assert1((!surf_parse()), "Parse error in %s", file);
831   surf_parse_close();
832
833   /* Adding loopback if needed */    
834   for (i = 0; i < nb_workstation; i++) 
835     if(!ROUTE(i,i).size) {
836       if(!loopback)
837         loopback = network_link_new(xbt_strdup("__MSG_loopback__"), 
838                                    498.00, NULL, 0.000015, NULL, 
839                                    SURF_NETWORK_LINK_ON, NULL,
840                                    SURF_NETWORK_LINK_FATPIPE);
841       ROUTE(i,i).size=1;
842       ROUTE(i,i).links = xbt_new0(network_link_KCCFLN05_t, 1);
843       ROUTE(i,i).links[0] = loopback;
844     }
845
846 }
847
848 /**************************************/
849 /********* Module  creation ***********/
850 /**************************************/
851
852 static void resource_init_internal(void)
853 {
854   s_surf_action_t action;
855
856   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
857
858   surf_workstation_resource->common_private =
859       xbt_new0(s_surf_resource_private_t, 1);
860   surf_workstation_resource->common_public =
861       xbt_new0(s_surf_resource_public_t, 1);
862   surf_workstation_resource->extension_public =
863       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
864
865   surf_workstation_resource->common_public->states.ready_action_set =
866       xbt_swag_new(xbt_swag_offset(action, state_hookup));
867   surf_workstation_resource->common_public->states.running_action_set =
868       xbt_swag_new(xbt_swag_offset(action, state_hookup));
869   surf_workstation_resource->common_public->states.failed_action_set =
870       xbt_swag_new(xbt_swag_offset(action, state_hookup));
871   surf_workstation_resource->common_public->states.done_action_set =
872       xbt_swag_new(xbt_swag_offset(action, state_hookup));
873
874   surf_workstation_resource->common_public->name_service = name_service;
875   surf_workstation_resource->common_public->get_resource_name = get_resource_name;
876   surf_workstation_resource->common_public->action_get_state = surf_action_get_state;
877   surf_workstation_resource->common_public->action_use = action_use;
878   surf_workstation_resource->common_public->action_free = action_free;
879   surf_workstation_resource->common_public->action_cancel = action_cancel;
880   surf_workstation_resource->common_public->action_recycle = action_recycle;
881   surf_workstation_resource->common_public->action_change_state = surf_action_change_state;
882   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
883   surf_workstation_resource->common_public->suspend = action_suspend;
884   surf_workstation_resource->common_public->resume = action_resume;
885   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
886   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
887   surf_workstation_resource->common_public->set_priority = action_set_priority;
888   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
889
890   surf_workstation_resource->common_private->resource_used = resource_used;
891   surf_workstation_resource->common_private->share_resources = share_resources;
892   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
893   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
894   surf_workstation_resource->common_private->finalize = finalize;
895
896   surf_workstation_resource->extension_public->execute = execute;
897   surf_workstation_resource->extension_public->sleep = action_sleep;
898   surf_workstation_resource->extension_public->get_state = resource_get_state;
899   surf_workstation_resource->extension_public->communicate = communicate;
900   surf_workstation_resource->extension_public->execute_parallel_task = execute_parallel_task;
901
902   workstation_set = xbt_dict_new();
903   network_link_set = xbt_dict_new();
904
905   xbt_assert0(maxmin_system, "surf_init has to be called first!");
906 }
907
908 /**************************************/
909 /*************** Generic **************/
910 /**************************************/
911 void surf_workstation_resource_init_KCCFLN05(const char *filename)
912 {
913   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
914   xbt_assert0(!surf_network_resource, "network resource type already defined");
915   resource_init_internal();
916   parse_file(filename);
917
918   xbt_dynar_push(resource_list, &surf_workstation_resource);
919 }