Logo AND Algorithmique Numérique Distribuée

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