Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update workstation handling in SimDag. Change the prefix of SimDag functions with...
[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(surf_workstation, 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         if((action)->lat_current==0.0)
262           lmm_update_variable_weight(maxmin_system,action->variable, 1.0);
263         else
264           lmm_update_variable_weight(maxmin_system, action->variable, 
265                                      action->lat_current);
266       }
267     }
268     surf_double_update(&(action->generic_action.remains),
269                        lmm_variable_getvalue(action->variable) * deltap);
270     if (action->generic_action.max_duration != NO_MAX_DURATION)
271       surf_double_update(&(action->generic_action.max_duration), delta);
272
273     /*   if(action->generic_action.remains<.00001) action->generic_action.remains=0; */
274
275     if ((action->generic_action.remains <= 0) && 
276         (lmm_get_variable_weight(action->variable)>0)) {
277       action->generic_action.finish = surf_get_clock();
278       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
279     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
280                (action->generic_action.max_duration <= 0)) {
281       action->generic_action.finish = surf_get_clock();
282       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
283     } else {                    /* Need to check that none of the resource has failed */
284       lmm_constraint_t cnst = NULL;
285       int i = 0;
286       void *constraint_id = NULL;
287
288       while ((cnst =
289               lmm_get_cnst_from_var(maxmin_system, action->variable,
290                                     i++))) {
291         constraint_id = lmm_constraint_id(cnst);
292         if(((((network_link_KCCFLN05_t)constraint_id)->type==
293              SURF_WORKSTATION_RESOURCE_LINK) &&
294             (((network_link_KCCFLN05_t)constraint_id)->state_current==
295              SURF_NETWORK_LINK_OFF)) ||
296            ((((cpu_KCCFLN05_t)constraint_id)->type==
297              SURF_WORKSTATION_RESOURCE_CPU) &&
298             (((cpu_KCCFLN05_t)constraint_id)->state_current==
299              SURF_CPU_OFF))) {
300           action->generic_action.finish = surf_get_clock();
301           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
302           break;
303         }
304       }
305     }
306   }
307   return;
308 }
309
310 static void update_resource_state(void *id,
311                                   tmgr_trace_event_t event_type,
312                                   double value)
313 {
314   cpu_KCCFLN05_t cpu = id;
315   network_link_KCCFLN05_t nw_link = id ;
316
317   if(nw_link->type == SURF_WORKSTATION_RESOURCE_LINK) {
318     if (event_type == nw_link->bw_event) {
319       nw_link->bw_current = value;
320       lmm_update_constraint_bound(maxmin_system, nw_link->constraint,
321                                   nw_link->bw_current);
322     } else if (event_type == nw_link->lat_event) {
323       double delta = value - nw_link->lat_current;
324       lmm_variable_t var = NULL;
325       surf_action_workstation_KCCFLN05_t action = NULL;
326       
327       nw_link->lat_current = value;
328       while (lmm_get_var_from_cnst(maxmin_system, nw_link->constraint, &var)) {
329         action = lmm_variable_id(var);
330         action->lat_current += delta;
331         if(action->rate<0)
332           lmm_update_variable_bound(maxmin_system, action->variable,
333                                     SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
334         else 
335           lmm_update_variable_bound(maxmin_system, action->variable,
336                                     min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
337         if(!(action->suspended))
338           lmm_update_variable_weight(maxmin_system, action->variable, 
339                                      action->lat_current);
340       }
341     } else if (event_type == nw_link->state_event) {
342       if (value > 0)
343         nw_link->state_current = SURF_NETWORK_LINK_ON;
344       else
345         nw_link->state_current = SURF_NETWORK_LINK_OFF;
346     } else {
347       CRITICAL0("Unknown event ! \n");
348       xbt_abort();
349     }
350     return;
351   } else if(cpu->type == SURF_WORKSTATION_RESOURCE_CPU) {
352     if (event_type == cpu->power_event) {
353       cpu->power_current = value;
354       __update_cpu_usage(cpu);
355     } else if (event_type == cpu->state_event) {
356       if (value > 0)
357         cpu->state_current = SURF_CPU_ON;
358       else
359         cpu->state_current = SURF_CPU_OFF;
360     } else {
361       CRITICAL0("Unknown event ! \n");
362       xbt_abort();
363     }    
364     return;
365   } else {
366     DIE_IMPOSSIBLE;
367   }
368   return;
369 }
370
371 static void finalize(void)
372 {
373   int i,j;
374
375   xbt_dict_free(&network_link_set);
376   xbt_dict_free(&workstation_set);
377   xbt_swag_free(surf_workstation_resource->common_public->states.
378                 ready_action_set);
379   xbt_swag_free(surf_workstation_resource->common_public->states.
380                 running_action_set);
381   xbt_swag_free(surf_workstation_resource->common_public->states.
382                 failed_action_set);
383   xbt_swag_free(surf_workstation_resource->common_public->states.
384                 done_action_set);
385
386   free(surf_workstation_resource->common_public);
387   free(surf_workstation_resource->common_private);
388   free(surf_workstation_resource->extension_public);
389
390   free(surf_workstation_resource);
391   surf_workstation_resource = NULL;
392
393   for (i = 0; i < nb_workstation; i++)
394     for (j = 0; j < nb_workstation; j++)
395       free(ROUTE(i, j).links);
396   free(routing_table);
397   routing_table = NULL;
398   nb_workstation = 0;
399
400   if (maxmin_system) {
401     lmm_system_free(maxmin_system);
402     maxmin_system = NULL;
403   }
404 }
405
406 /**************************************/
407 /******* Resource Private    **********/
408 /**************************************/
409
410 static surf_action_t execute(void *cpu, double size)
411 {
412   surf_action_workstation_KCCFLN05_t action = NULL;
413   cpu_KCCFLN05_t CPU = cpu;
414
415   XBT_IN2("(%s,%g)",CPU->name,size);
416   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
417
418   action->generic_action.using = 1;
419   action->generic_action.cost = size;
420   action->generic_action.remains = size;
421   action->generic_action.priority = 1.0;
422   action->generic_action.max_duration = NO_MAX_DURATION;
423   action->generic_action.start = surf_get_clock();
424   action->generic_action.finish = -1.0;
425   action->generic_action.resource_type =
426       (surf_resource_t) surf_workstation_resource;
427   action->suspended = 0;
428
429   if (CPU->state_current == SURF_CPU_ON)
430     action->generic_action.state_set =
431         surf_workstation_resource->common_public->states.running_action_set;
432   else
433     action->generic_action.state_set =
434         surf_workstation_resource->common_public->states.failed_action_set;
435   xbt_swag_insert(action, action->generic_action.state_set);
436
437   action->variable = lmm_variable_new(maxmin_system, action, 
438                                       action->generic_action.priority, -1.0, 1);
439   lmm_expand(maxmin_system, CPU->constraint, action->variable,
440              1.0);
441   XBT_OUT;
442   return (surf_action_t) action;
443 }
444
445 static surf_action_t action_sleep(void *cpu, double duration)
446 {
447   surf_action_workstation_KCCFLN05_t action = NULL;
448
449   XBT_IN2("(%s,%g)",((cpu_KCCFLN05_t)cpu)->name,duration);
450
451   action = (surf_action_workstation_KCCFLN05_t) execute(cpu, 1.0);
452   action->generic_action.max_duration = duration;
453   lmm_update_variable_weight(maxmin_system, action->variable, 0.0);
454
455   XBT_OUT;
456   return (surf_action_t) action;
457 }
458
459 static e_surf_cpu_state_t resource_get_state(void *cpu)
460 {
461   return ((cpu_KCCFLN05_t) cpu)->state_current;
462 }
463
464 static double get_speed(void *cpu, double load)
465 {
466   return load*(((cpu_KCCFLN05_t) cpu)->power_scale);
467 }
468
469 static surf_action_t communicate(void *src, void *dst, double size, double rate)
470 {
471   surf_action_workstation_KCCFLN05_t action = NULL;
472   cpu_KCCFLN05_t card_src = src;
473   cpu_KCCFLN05_t card_dst = dst;
474   route_KCCFLN05_t route = &(ROUTE(card_src->id, card_dst->id));
475   int route_size = route->size;
476   int i;
477
478   XBT_IN4("(%s,%s,%g,%g)",card_src->name,card_dst->name,size,rate);
479   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);
480
481   action = xbt_new0(s_surf_action_workstation_KCCFLN05_t, 1);
482
483   action->generic_action.using = 1;
484   action->generic_action.cost = size;
485   action->generic_action.remains = size;
486   action->generic_action.max_duration = NO_MAX_DURATION;
487   action->generic_action.start = -1.0;
488   action->generic_action.finish = -1.0;
489   action->src = src;
490   action->dst = dst;
491   action->generic_action.resource_type =
492     (surf_resource_t) surf_workstation_resource;
493   action->suspended = 0;  /* Should be useless because of the 
494                              calloc but it seems to help valgrind... */
495   action->generic_action.state_set =
496       surf_workstation_resource->common_public->states.running_action_set;
497
498   xbt_dynar_push(card_src->outgoing_communications, &action);
499   xbt_dynar_push(card_dst->incomming_communications, &action);
500
501   xbt_swag_insert(action, action->generic_action.state_set);
502   action->rate = rate;
503
504   action->latency = 0.0;
505   for (i = 0; i < route_size; i++)
506     action->latency += route->links[i]->lat_current;
507   action->lat_current = action->latency;
508
509   if(action->latency>0)
510     action->variable = lmm_variable_new(maxmin_system, action, 0.0, -1.0,
511                                         route_size+4); /* +1 for the src bus
512                                                           +1 for the dst bus
513                                                           +1 for the src cpu
514                                                           +1 for the dst cpu */
515   else
516     action->variable = lmm_variable_new(maxmin_system, action, 1.0, -1.0,
517                                         route_size+4);
518
519   if(action->rate<0) {
520     if(action->lat_current>0)
521       lmm_update_variable_bound(maxmin_system, action->variable,
522                                 SG_TCP_CTE_GAMMA / (2.0 * action->lat_current));
523     else
524       lmm_update_variable_bound(maxmin_system, action->variable, -1.0);
525   } else {
526     if(action->lat_current>0)
527       lmm_update_variable_bound(maxmin_system, action->variable,
528                                 min(action->rate,SG_TCP_CTE_GAMMA / (2.0 * action->lat_current)));
529     else
530       lmm_update_variable_bound(maxmin_system, action->variable, action->rate);
531   }
532
533   for (i = 0; i < route_size; i++)
534     lmm_expand(maxmin_system, route->links[i]->constraint, action->variable, 1.0);
535   if (card_src->bus)
536     lmm_expand(maxmin_system, card_src->bus, action->variable, 1.0);
537   if (card_dst->bus)
538     lmm_expand(maxmin_system, card_dst->bus, action->variable, 1.0);
539   lmm_expand(maxmin_system, card_src->constraint, action->variable, 0.0);
540   lmm_expand(maxmin_system, card_dst->constraint, action->variable, 0.0);
541
542   XBT_OUT;
543   return (surf_action_t) action;
544 }
545
546 /* FIXME: execute_parallel_task */
547 static surf_action_t execute_parallel_task(int cpu_nb,
548                                            void **cpu_list, 
549                                            double *computation_amount, 
550                                            double *communication_amount,
551                                            double amount,
552                                            double rate)
553 {
554   DIE_IMPOSSIBLE;
555   return NULL;
556 }
557
558
559 /**************************************/
560 /*** Resource Creation & Destruction **/
561 /**************************************/
562
563 static void cpu_free(void *cpu)
564 {
565   free(((cpu_KCCFLN05_t) cpu)->name);
566   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->incomming_communications));
567   xbt_dynar_free(&(((cpu_KCCFLN05_t) cpu)->outgoing_communications));
568   free(cpu);
569 }
570
571 static cpu_KCCFLN05_t cpu_new(const char *name, double power_scale,
572                               double power_initial,
573                               tmgr_trace_t power_trace,
574                               e_surf_cpu_state_t state_initial,
575                               tmgr_trace_t state_trace,
576                               double interference_send,
577                               double interference_recv,
578                               double interference_send_recv,
579                               double max_outgoing_rate)
580 {
581   cpu_KCCFLN05_t cpu = xbt_new0(s_cpu_KCCFLN05_t, 1);
582
583   cpu->resource = (surf_resource_t) surf_workstation_resource;
584   cpu->type = SURF_WORKSTATION_RESOURCE_CPU;
585   cpu->name = xbt_strdup(name);
586   cpu->id = nb_workstation++;
587
588   cpu->power_scale = power_scale;
589   xbt_assert0(cpu->power_scale > 0, "Power has to be >0");
590
591   cpu->power_current = power_initial;
592   if (power_trace)
593     cpu->power_event =
594         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
595
596   cpu->state_current = state_initial;
597   if (state_trace)
598     cpu->state_event =
599         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
600
601   cpu->interference_send = interference_send;
602   cpu->interference_recv = interference_recv;
603   cpu->interference_send_recv = interference_send_recv;
604
605   cpu->constraint =
606       lmm_constraint_new(maxmin_system, cpu,
607                          cpu->power_current * cpu->power_scale);
608   if (max_outgoing_rate > 0)
609     cpu->bus = lmm_constraint_new(maxmin_system, cpu, max_outgoing_rate);
610
611   cpu->incomming_communications =
612       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
613   cpu->outgoing_communications =
614       xbt_dynar_new(sizeof(surf_action_workstation_KCCFLN05_t), NULL);
615
616   xbt_dict_set(workstation_set, name, cpu, cpu_free);
617
618   return cpu;
619 }
620
621 static void parse_cpu(void)
622 {
623   double power_scale = 0.0;
624   double power_initial = 0.0;
625   tmgr_trace_t power_trace = NULL;
626   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
627   tmgr_trace_t state_trace = NULL;
628   double interference_send = 0.0;
629   double interference_recv = 0.0;
630   double interference_send_recv = 0.0;
631   double max_outgoing_rate = -1.0;
632
633   surf_parse_get_double(&power_scale, A_surfxml_cpu_power);
634   surf_parse_get_double(&power_initial, A_surfxml_cpu_availability);
635   surf_parse_get_trace(&power_trace, A_surfxml_cpu_availability_file);
636
637   xbt_assert0((A_surfxml_cpu_state == A_surfxml_cpu_state_ON) ||
638               (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF),
639               "Invalid state");
640   if (A_surfxml_cpu_state == A_surfxml_cpu_state_ON)
641     state_initial = SURF_CPU_ON;
642   if (A_surfxml_cpu_state == A_surfxml_cpu_state_OFF)
643     state_initial = SURF_CPU_OFF;
644   surf_parse_get_trace(&state_trace, A_surfxml_cpu_state_file);
645
646   surf_parse_get_double(&interference_send,
647                         A_surfxml_cpu_interference_send);
648   surf_parse_get_double(&interference_recv,
649                         A_surfxml_cpu_interference_recv);
650   surf_parse_get_double(&interference_send_recv,
651                         A_surfxml_cpu_interference_send_recv);
652   surf_parse_get_double(&max_outgoing_rate,
653                         A_surfxml_cpu_max_outgoing_rate);
654
655   cpu_new(A_surfxml_cpu_name, power_scale, power_initial, power_trace,
656           state_initial, state_trace, interference_send, interference_recv,
657           interference_send_recv, max_outgoing_rate);
658 }
659
660 static void create_routing_table(void)
661 {
662   routing_table =
663       xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
664 }
665
666 static void network_link_free(void *nw_link)
667 {
668   free(((network_link_KCCFLN05_t) nw_link)->name);
669   free(nw_link);
670 }
671
672 static network_link_KCCFLN05_t network_link_new(char *name,
673                                                 double bw_initial,
674                                                 tmgr_trace_t bw_trace,
675                                                 double lat_initial,
676                                                 tmgr_trace_t lat_trace,
677                                                 e_surf_network_link_state_t
678                                                 state_initial,
679                                                 tmgr_trace_t state_trace,
680                                                 e_surf_network_link_sharing_policy_t policy)
681 {
682   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
683
684
685   nw_link->resource = (surf_resource_t) surf_workstation_resource;
686   nw_link->type = SURF_WORKSTATION_RESOURCE_LINK;
687   nw_link->name = name;
688   nw_link->bw_current = bw_initial;
689   if (bw_trace)
690     nw_link->bw_event =
691         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
692   nw_link->state_current = state_initial;
693   nw_link->lat_current = lat_initial;
694   if (lat_trace)
695     nw_link->lat_event =
696         tmgr_history_add_trace(history, lat_trace, 0.0, 0, nw_link);
697   if (state_trace)
698     nw_link->state_event =
699         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
700
701   nw_link->constraint =
702       lmm_constraint_new(maxmin_system, nw_link, nw_link->bw_current);
703
704   if(policy == SURF_NETWORK_LINK_FATPIPE)
705     lmm_constraint_shared(nw_link->constraint);
706
707   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
708
709   return nw_link;
710 }
711
712 static void parse_network_link(void)
713 {
714   char *name;
715   double bw_initial;
716   tmgr_trace_t bw_trace;
717   double lat_initial;
718   tmgr_trace_t lat_trace;
719   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
720   e_surf_network_link_sharing_policy_t policy_initial = SURF_NETWORK_LINK_SHARED;
721   tmgr_trace_t state_trace;
722
723   name = xbt_strdup(A_surfxml_network_link_name);
724   surf_parse_get_double(&bw_initial,A_surfxml_network_link_bandwidth);
725   surf_parse_get_trace(&bw_trace, A_surfxml_network_link_bandwidth_file);
726   surf_parse_get_double(&lat_initial,A_surfxml_network_link_latency);
727   surf_parse_get_trace(&lat_trace, A_surfxml_network_link_latency_file);
728
729   xbt_assert0((A_surfxml_network_link_state==A_surfxml_network_link_state_ON)||
730               (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF),
731               "Invalid state");
732   if (A_surfxml_network_link_state==A_surfxml_network_link_state_ON) 
733     state_initial = SURF_NETWORK_LINK_ON;
734   else if (A_surfxml_network_link_state==A_surfxml_network_link_state_OFF) 
735     state_initial = SURF_NETWORK_LINK_OFF;
736
737   if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_SHARED) 
738     policy_initial = SURF_NETWORK_LINK_SHARED;
739   else if (A_surfxml_network_link_sharing_policy==A_surfxml_network_link_sharing_policy_FATPIPE) 
740     policy_initial = SURF_NETWORK_LINK_FATPIPE;
741
742   surf_parse_get_trace(&state_trace,A_surfxml_network_link_state_file);
743
744   network_link_new(name, bw_initial, bw_trace,
745                    lat_initial, lat_trace, state_initial, state_trace,
746                    policy_initial);
747 }
748
749 static void route_new(int src_id, int dst_id, char **links, int nb_link,
750                       double impact_on_src, double impact_on_dst,
751                       double impact_on_src_with_other_recv,
752                       double impact_on_dst_with_other_send)
753 {
754   network_link_KCCFLN05_t *link_list = NULL;
755   int i;
756   route_KCCFLN05_t route = &(ROUTE(src_id, dst_id));
757
758   route->size = nb_link;
759   link_list = route->links = xbt_new0(network_link_KCCFLN05_t, nb_link);
760   for (i = 0; i < nb_link; i++) {
761     link_list[i] = xbt_dict_get_or_null(network_link_set, links[i]);
762     free(links[i]);
763   }
764   free(links);
765   route->impact_on_src = impact_on_src;
766   route->impact_on_dst = impact_on_src;
767   route->impact_on_src_with_other_recv = impact_on_src_with_other_recv;
768   route->impact_on_dst_with_other_send = impact_on_dst_with_other_send;
769 }
770
771 static int nb_link = 0;
772 static char **link_name = NULL;
773 static int src_id = -1;
774 static int dst_id = -1;
775 static double impact_on_src;
776 static double impact_on_dst;
777 static double impact_on_src_with_other_recv;
778 static double impact_on_dst_with_other_send;
779
780 static void parse_route_set_endpoints(void)
781 {
782   src_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_src))->id;
783   dst_id = ((cpu_KCCFLN05_t) name_service(A_surfxml_route_dst))->id;
784   surf_parse_get_double(&impact_on_src, A_surfxml_route_impact_on_src);
785   surf_parse_get_double(&impact_on_dst, A_surfxml_route_impact_on_dst);
786   surf_parse_get_double(&impact_on_src_with_other_recv,
787                         A_surfxml_route_impact_on_src_with_other_recv);
788   surf_parse_get_double(&impact_on_dst_with_other_send,
789                         A_surfxml_route_impact_on_dst_with_other_send);
790
791   nb_link = 0;
792   link_name = NULL;
793 }
794
795 static void parse_route_elem(void)
796 {
797   nb_link++;
798   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
799   link_name[(nb_link) - 1] = xbt_strdup(A_surfxml_route_element_name);
800 }
801
802 static void parse_route_set_route(void)
803 {
804   route_new(src_id, dst_id, link_name, nb_link, impact_on_src,
805             impact_on_dst, impact_on_src_with_other_recv,
806             impact_on_dst_with_other_send);
807 }
808
809 static void parse_file(const char *file)
810 {
811   int i ;
812
813   /* Figuring out the cpus */
814   surf_parse_reset_parser();
815   ETag_surfxml_cpu_fun = parse_cpu;
816   surf_parse_open(file);
817   xbt_assert1((!surf_parse()), "Parse error in %s", file);
818   surf_parse_close();
819
820   create_routing_table();
821
822   /* Figuring out the network links */
823   surf_parse_reset_parser();
824   ETag_surfxml_network_link_fun = parse_network_link;
825   surf_parse_open(file);
826   xbt_assert1((!surf_parse()), "Parse error in %s", file);
827   surf_parse_close();
828
829   /* Building the routes */
830   surf_parse_reset_parser();
831   STag_surfxml_route_fun = parse_route_set_endpoints;
832   ETag_surfxml_route_element_fun = parse_route_elem;
833   ETag_surfxml_route_fun = parse_route_set_route;
834   surf_parse_open(file);
835   xbt_assert1((!surf_parse()), "Parse error in %s", file);
836   surf_parse_close();
837
838   /* Adding loopback if needed */    
839   for (i = 0; i < nb_workstation; i++) 
840     if(!ROUTE(i,i).size) {
841       if(!loopback)
842         loopback = network_link_new(xbt_strdup("__MSG_loopback__"), 
843                                    498.00, NULL, 0.000015, NULL, 
844                                    SURF_NETWORK_LINK_ON, NULL,
845                                    SURF_NETWORK_LINK_FATPIPE);
846       ROUTE(i,i).size=1;
847       ROUTE(i,i).links = xbt_new0(network_link_KCCFLN05_t, 1);
848       ROUTE(i,i).links[0] = loopback;
849     }
850
851 }
852
853 /**************************************/
854 /********* Module  creation ***********/
855 /**************************************/
856
857 static void resource_init_internal(void)
858 {
859   s_surf_action_t action;
860
861   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
862
863   surf_workstation_resource->common_private =
864       xbt_new0(s_surf_resource_private_t, 1);
865   surf_workstation_resource->common_public =
866       xbt_new0(s_surf_resource_public_t, 1);
867   surf_workstation_resource->extension_public =
868       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
869
870   surf_workstation_resource->common_public->states.ready_action_set =
871       xbt_swag_new(xbt_swag_offset(action, state_hookup));
872   surf_workstation_resource->common_public->states.running_action_set =
873       xbt_swag_new(xbt_swag_offset(action, state_hookup));
874   surf_workstation_resource->common_public->states.failed_action_set =
875       xbt_swag_new(xbt_swag_offset(action, state_hookup));
876   surf_workstation_resource->common_public->states.done_action_set =
877       xbt_swag_new(xbt_swag_offset(action, state_hookup));
878
879   surf_workstation_resource->common_public->name_service = name_service;
880   surf_workstation_resource->common_public->get_resource_name = get_resource_name;
881   surf_workstation_resource->common_public->action_get_state = surf_action_get_state;
882   surf_workstation_resource->common_public->action_use = action_use;
883   surf_workstation_resource->common_public->action_free = action_free;
884   surf_workstation_resource->common_public->action_cancel = action_cancel;
885   surf_workstation_resource->common_public->action_recycle = action_recycle;
886   surf_workstation_resource->common_public->action_change_state = surf_action_change_state;
887   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
888   surf_workstation_resource->common_public->suspend = action_suspend;
889   surf_workstation_resource->common_public->resume = action_resume;
890   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
891   surf_workstation_resource->common_public->set_max_duration = action_set_max_duration;
892   surf_workstation_resource->common_public->set_priority = action_set_priority;
893   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
894
895   surf_workstation_resource->common_private->resource_used = resource_used;
896   surf_workstation_resource->common_private->share_resources = share_resources;
897   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
898   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
899   surf_workstation_resource->common_private->finalize = finalize;
900
901   surf_workstation_resource->extension_public->execute = execute;
902   surf_workstation_resource->extension_public->sleep = action_sleep;
903   surf_workstation_resource->extension_public->get_state = resource_get_state;
904   surf_workstation_resource->extension_public->get_speed = get_speed;
905   surf_workstation_resource->extension_public->communicate = communicate;
906   surf_workstation_resource->extension_public->execute_parallel_task = execute_parallel_task;
907
908   workstation_set = xbt_dict_new();
909   network_link_set = xbt_dict_new();
910
911   xbt_assert0(maxmin_system, "surf_init has to be called first!");
912 }
913
914 /**************************************/
915 /*************** Generic **************/
916 /**************************************/
917 void surf_workstation_resource_init_KCCFLN05(const char *filename)
918 {
919   xbt_assert0(!surf_cpu_resource, "CPU resource type already defined");
920   xbt_assert0(!surf_network_resource, "network resource type already defined");
921   resource_init_internal();
922   parse_file(filename);
923
924   xbt_dynar_push(resource_list, &surf_workstation_resource);
925 }