Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use doxygen's section instead of my poor approximation; link to the list of all exist...
[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/dict.h"
9 #include "workstation_KCCFLN05_private.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(workstation_KCCFLN05, surf,
12                                 "Logging specific to the SURF workstation module (KCCFLN05)");
13
14
15 static lmm_system_t maxmin_system_cpu_KCCFLN05 = NULL;
16 static lmm_system_t maxmin_system_network_KCCFLN05 = NULL;
17 static xbt_dict_t network_link_set = NULL;
18 static int nb_workstation = 0;
19 s_route_KCCFLN05_t *routing_table = NULL;
20 #define ROUTE(i,j) routing_table[(i)+(j)*nb_workstation]
21
22 /**************************************/
23 /************* CPU object *************/
24 /**************************************/
25
26 /************ workstation creation *********/
27 static void workstation_free(void *workstation)
28 {
29   xbt_free(((workstation_KCCFLN05_t)workstation)->name);
30   xbt_dynar_free(&(((workstation_KCCFLN05_t)workstation)->incomming_communications));
31   xbt_dynar_free(&(((workstation_KCCFLN05_t)workstation)->outgoing_communications));
32   xbt_free(workstation);
33 }
34
35 static workstation_KCCFLN05_t workstation_new(const char *name,
36                                               double power_scale,
37                                               double power_initial,
38                                               tmgr_trace_t power_trace,
39                                               e_surf_cpu_state_t state_initial,
40                                               tmgr_trace_t state_trace,
41                                               double interference_send,
42                                               double interference_recv,
43                                               double interference_send_recv,
44                                               double max_outgoing_rate)
45 {
46   workstation_KCCFLN05_t workstation = xbt_new0(s_workstation_KCCFLN05_t, 1);
47
48   workstation->resource = (surf_resource_t) surf_workstation_resource;
49   workstation->name = xbt_strdup(name);
50   workstation->id = nb_workstation++;
51
52   workstation->power_scale = power_scale;
53   xbt_assert0(workstation->power_scale>0,"Power has to be >0");
54
55   workstation->power_current = power_initial;
56   if (power_trace)
57     workstation->power_event =
58         tmgr_history_add_trace(history, power_trace, 0.0, 0, workstation);
59
60   workstation->state_current = state_initial;
61   if (state_trace)
62     workstation->state_event =
63         tmgr_history_add_trace(history, state_trace, 0.0, 0, workstation);
64
65   workstation->interference_send=interference_send;
66   workstation->interference_recv=interference_recv;
67   workstation->interference_send_recv=interference_send_recv;
68
69   workstation->constraint =
70       lmm_constraint_new(maxmin_system_cpu_KCCFLN05, workstation,
71                          workstation->power_current * workstation->power_scale);
72   if(max_outgoing_rate>0) 
73     workstation->bus=
74       lmm_constraint_new(maxmin_system_cpu_KCCFLN05, workstation,
75                          max_outgoing_rate);
76
77   workstation->incomming_communications = 
78     xbt_dynar_new(sizeof(surf_action_network_KCCFLN05_t),NULL);
79   workstation->outgoing_communications = 
80     xbt_dynar_new(sizeof(surf_action_network_KCCFLN05_t),NULL);
81
82   xbt_dict_set(workstation_set, name, workstation, workstation_free);
83
84   return workstation;
85 }
86
87 static void parse_workstation(void)
88 {
89   double power_scale = 0.0;
90   double power_initial = 0.0;
91   tmgr_trace_t power_trace = NULL;
92   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
93   tmgr_trace_t state_trace = NULL;
94   double interference_send = 0.0;
95   double interference_recv = 0.0;
96   double interference_send_recv = 0.0;
97   double max_outgoing_rate = -1.0;
98
99   surf_parse_get_double(&power_scale,A_cpu_power);
100   surf_parse_get_double(&power_initial,A_cpu_availability);
101   surf_parse_get_trace(&power_trace,A_cpu_availability_file);
102
103   xbt_assert0((A_cpu_state==A_cpu_state_ON)||
104               (A_cpu_state==A_cpu_state_OFF),
105               "Invalid state")
106   if (A_cpu_state==A_cpu_state_ON) state_initial = SURF_CPU_ON;
107   if (A_cpu_state==A_cpu_state_OFF) state_initial = SURF_CPU_OFF;
108   surf_parse_get_trace(&state_trace,A_cpu_state_file);
109
110   surf_parse_get_double(&interference_send,A_cpu_interference_send);
111   surf_parse_get_double(&interference_recv,A_cpu_interference_recv);
112   surf_parse_get_double(&interference_send_recv,A_cpu_interference_send_recv);
113   surf_parse_get_double(&max_outgoing_rate,A_cpu_max_outgoing_rate);
114
115   workstation_new(A_cpu_name, power_scale, power_initial, power_trace, state_initial,
116                   state_trace, interference_send, interference_recv,
117                   interference_send_recv, max_outgoing_rate);
118 }
119
120 /*********** resource management ***********/
121
122 static void *name_service(const char *name)
123 {
124   void *workstation = NULL;
125
126   xbt_dict_get(workstation_set, name, &workstation);
127
128   return workstation;
129 }
130
131 static const char *get_resource_name(void *resource_id)
132 {
133   return ((workstation_KCCFLN05_t) resource_id)->name;
134 }
135
136 static int cpu_used(void *resource_id)
137 {
138   return lmm_constraint_used(maxmin_system_cpu_KCCFLN05,
139                              ((workstation_KCCFLN05_t) resource_id)->constraint);
140 }
141
142 static int link_used(void *resource_id)
143 {
144   return lmm_constraint_used(maxmin_system_network_KCCFLN05,
145                              ((network_link_KCCFLN05_t) resource_id)->constraint);
146 }
147
148 static e_surf_cpu_state_t get_state(void *cpu)
149 {
150   return ((workstation_KCCFLN05_t) cpu)->state_current;
151 }
152
153 static void update_cpu_KCCFLN05_state(void *id,
154                                       tmgr_trace_event_t event_type,
155                                       double value)
156 {
157   workstation_KCCFLN05_t cpu = id;
158
159   if (event_type == cpu->power_event) {
160     cpu->power_current = value;
161     /*** The bound is updated in share_cpu_KCCFLN05_resources ***/
162     /*     lmm_update_constraint_bound(maxmin_system_cpu_KCCFLN05, cpu->constraint, */
163     /*                          cpu->power_current * cpu->power_scale); */
164   } else if (event_type == cpu->state_event) {
165     if (value > 0)
166       cpu->state_current = SURF_CPU_ON;
167     else
168       cpu->state_current = SURF_CPU_OFF;
169   } else {
170     CRITICAL0("Unknown event ! \n");
171     xbt_abort();
172   }
173
174   return;
175 }
176
177 /**************************************/
178 /*********** network object ***********/
179 /**************************************/
180
181 static void create_routing_table(void)
182 {
183   routing_table = xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
184 }
185
186 static void network_link_free(void *nw_link)
187 {
188   xbt_free(((network_link_KCCFLN05_t)nw_link)->name);
189   xbt_free(nw_link);
190 }
191
192 static network_link_KCCFLN05_t network_link_new(char *name,
193                                                  double bw_initial,
194                                                  tmgr_trace_t bw_trace,
195                                                  e_surf_network_link_state_t
196                                                  state_initial,
197                                                  tmgr_trace_t state_trace)
198 {
199   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
200
201
202   nw_link->resource = (surf_resource_t) surf_network_resource;
203   nw_link->name = name;
204   nw_link->bw_current = bw_initial;
205   if (bw_trace)
206     nw_link->bw_event =
207         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
208   nw_link->state_current = state_initial;
209   if (state_trace)
210     nw_link->state_event =
211         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
212
213   nw_link->constraint =
214       lmm_constraint_new(maxmin_system_network_KCCFLN05, nw_link, nw_link->bw_current);
215
216   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
217
218   return nw_link;
219 }
220
221 static void parse_network_link(void)
222 {
223   char *name;
224   double bw_initial;
225   tmgr_trace_t bw_trace;
226   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
227   tmgr_trace_t state_trace;
228
229   name = xbt_strdup(A_network_link_name);
230   surf_parse_get_double(&bw_initial,A_network_link_bandwidth);
231   surf_parse_get_trace(&bw_trace, A_network_link_bandwidth_file);
232
233   xbt_assert0((A_network_link_state==A_network_link_state_ON)||
234               (A_network_link_state==A_network_link_state_OFF),
235               "Invalid state")
236   if (A_network_link_state==A_network_link_state_ON) 
237     state_initial = SURF_NETWORK_LINK_ON;
238   if (A_network_link_state==A_network_link_state_OFF) 
239     state_initial = SURF_NETWORK_LINK_OFF;
240   surf_parse_get_trace(&state_trace,A_network_link_state_file);
241
242   network_link_new(name, bw_initial, bw_trace, state_initial, state_trace);
243 }
244
245 static void route_new(int src_id, int dst_id, char **links, int nb_link,
246                       double impact_on_src, double impact_on_dst,
247                       double impact_on_src_with_other_recv,
248                       double impact_on_dst_with_other_send)
249 {
250   network_link_KCCFLN05_t *link_list = NULL;
251   int i;
252   route_KCCFLN05_t route = &(ROUTE(src_id,dst_id));
253
254   route->size= nb_link;
255   link_list = route->links = xbt_new0(network_link_KCCFLN05_t, nb_link);
256   for (i = 0; i < nb_link; i++) {
257     xbt_dict_get(network_link_set, links[i], (void *) &(link_list[i]));
258     xbt_free(links[i]);
259   }
260   xbt_free(links);
261   route->impact_on_src=impact_on_src;
262   route->impact_on_dst=impact_on_src;
263   route->impact_on_src_with_other_recv=impact_on_src_with_other_recv; 
264   route->impact_on_dst_with_other_send=impact_on_dst_with_other_send;
265 }
266
267 static int nb_link = 0;
268 static char **link_name = NULL;
269 static int src_id = -1;
270 static int dst_id = -1;
271 static double impact_on_src;
272 static double impact_on_dst;
273 static double impact_on_src_with_other_recv; 
274 static double impact_on_dst_with_other_send;
275
276 static void parse_route_set_endpoints(void)
277 {
278   src_id = ((workstation_KCCFLN05_t) name_service(A_route_src))->id;
279   dst_id = ((workstation_KCCFLN05_t) name_service(A_route_dst))->id;
280   surf_parse_get_double(&impact_on_src,A_route_impact_on_src);
281   surf_parse_get_double(&impact_on_dst,A_route_impact_on_dst);
282   surf_parse_get_double(&impact_on_src_with_other_recv,A_route_impact_on_src_with_other_recv);
283   surf_parse_get_double(&impact_on_dst_with_other_send,A_route_impact_on_dst_with_other_send);
284
285   nb_link = 0;
286   link_name = NULL;
287 }
288
289 static void parse_route_elem(void)
290 {
291   nb_link++;
292   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
293   link_name[(nb_link) - 1] = xbt_strdup(A_route_element_name);
294 }
295
296 static void parse_route_set_route(void)
297 {
298   route_new(src_id, dst_id, link_name, nb_link, impact_on_src, impact_on_dst,
299             impact_on_src_with_other_recv, impact_on_dst_with_other_send);
300 }
301
302 static void parse_file(const char *file)
303 {
304   /* Figuring out the workstations */
305   surf_parse_reset_parser();
306   ETag_cpu_fun=parse_workstation;
307   surf_parse_open(file);
308   xbt_assert1((!surf_parse()),"Parse error in %s",file);
309   surf_parse_close();
310
311   create_routing_table();
312
313   /* Figuring out the network links */
314   surf_parse_reset_parser();
315   ETag_network_link_fun=parse_network_link;
316   surf_parse_open(file);
317   xbt_assert1((!surf_parse()),"Parse error in %s",file);
318   surf_parse_close();
319
320   /* Building the routes */
321   surf_parse_reset_parser();
322   STag_route_fun=parse_route_set_endpoints;
323   ETag_route_element_fun=parse_route_elem;
324   ETag_route_fun=parse_route_set_route;
325   surf_parse_open(file);
326   xbt_assert1((!surf_parse()),"Parse error in %s",file);
327   surf_parse_close();
328 }
329
330 static void update_network_KCCFLN05_state(void *id,
331                                           tmgr_trace_event_t event_type,
332                                           double value)
333 {
334   network_link_KCCFLN05_t nw_link = id;
335
336   if (event_type == nw_link->bw_event) {
337     nw_link->bw_current = value;
338     lmm_update_constraint_bound(maxmin_system_network_KCCFLN05, nw_link->constraint,
339                                 nw_link->bw_current);
340   } else if (event_type == nw_link->state_event) {
341     if (value > 0)
342       nw_link->state_current = SURF_NETWORK_LINK_ON;
343     else
344       nw_link->state_current = SURF_NETWORK_LINK_OFF;
345   } else {
346     CRITICAL0("Unknown event ! \n");
347     xbt_abort();
348   }
349
350   return;
351 }
352
353 /**************************************/
354 /*************** actions **************/
355 /**************************************/
356 /*************** network **************/
357 static void action_network_KCCFLN05_free(surf_action_t action)
358 {
359   int cpt;
360   surf_action_t act = NULL;
361   workstation_KCCFLN05_t src = ((surf_action_network_KCCFLN05_t) action)->src;
362   workstation_KCCFLN05_t dst = ((surf_action_network_KCCFLN05_t) action)->dst;
363
364   xbt_swag_remove(action, action->state_set);
365   if(((surf_action_network_KCCFLN05_t)action)->variable)
366     lmm_variable_free(maxmin_system_network_KCCFLN05, 
367                     ((surf_action_network_KCCFLN05_t) action)->variable);
368
369   xbt_dynar_foreach (src->outgoing_communications,cpt,act) {
370     if(act==action) {
371       xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
372       break;
373     }
374   }
375
376   xbt_dynar_foreach (dst->incomming_communications,cpt,act) {
377     if(act==action) {
378       xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
379       break;
380     }
381   }
382
383   xbt_free(action);
384 }
385
386 static double share_network_KCCFLN05_resources(double now)
387 {
388  s_surf_action_network_KCCFLN05_t action;
389  return generic_maxmin_share_resources2(surf_network_resource->common_public->
390                                         states.running_action_set,
391                                         xbt_swag_offset(action, variable),
392                                         maxmin_system_network_KCCFLN05);
393 }
394
395 static void action_network_KCCFLN05_change_state(surf_action_t action,
396                                                  e_surf_action_state_t state)
397 {
398   int cpt;
399   surf_action_t act = NULL;
400   workstation_KCCFLN05_t src = ((surf_action_network_KCCFLN05_t) action)->src;
401   workstation_KCCFLN05_t dst = ((surf_action_network_KCCFLN05_t) action)->dst;
402
403   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED))
404     if(((surf_action_network_KCCFLN05_t)action)->variable) {
405       lmm_variable_disable(maxmin_system_network_KCCFLN05, 
406                            ((surf_action_network_KCCFLN05_t)action)->variable);
407       ((surf_action_network_KCCFLN05_t)action)->variable = NULL;
408     }
409
410   xbt_dynar_foreach (src->outgoing_communications,cpt,act) {
411     if(act==action) {
412       xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
413       break;
414     }
415   }
416
417   xbt_dynar_foreach (dst->incomming_communications,cpt,act) {
418     if(act==action) {
419       xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
420       break;
421     }
422   }
423   surf_action_change_state(action, state);
424   return;
425 }
426
427 static void update_actions_network_KCCFLN05_state(double now, double delta)
428 {
429   surf_action_network_KCCFLN05_t action = NULL;
430   surf_action_network_KCCFLN05_t next_action = NULL;
431   xbt_swag_t running_actions =
432       surf_network_resource->common_public->states.running_action_set;
433   xbt_swag_t failed_actions =
434       surf_network_resource->common_public->states.failed_action_set;
435
436   xbt_swag_foreach_safe(action, next_action, running_actions) {
437     surf_double_update(&(action->generic_action.remains),
438                        lmm_variable_getvalue(action->variable) * delta);
439     if (action->generic_action.max_duration != NO_MAX_DURATION)
440           surf_double_update(&(action->generic_action.max_duration), delta);
441     if ((action->generic_action.remains <= 0.0) && 
442         (lmm_get_variable_weight(action->variable)>0)) {
443       action->generic_action.finish = surf_get_clock();
444       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
445     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
446                (action->generic_action.max_duration <= 0.0)) {
447       action->generic_action.finish = surf_get_clock();
448       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
449     } else {                    /* Need to check that none of the resource has failed */
450       /* FIXME : FAILURES ARE NOT HANDLED YET */
451     }
452   }
453
454   return;
455 }
456
457 static surf_action_t communicate_KCCFLN05(void *src, void *dst, double size,
458                                           double rate)
459 {
460   surf_action_network_KCCFLN05_t action = NULL;
461   workstation_KCCFLN05_t card_src = src;
462   workstation_KCCFLN05_t card_dst = dst;
463   route_KCCFLN05_t route = &ROUTE(card_src->id, card_dst->id);
464   int i;
465
466   action = xbt_new0(s_surf_action_network_KCCFLN05_t, 1);
467
468   action->generic_action.cost = size;
469   action->generic_action.remains = size;
470   action->generic_action.max_duration = NO_MAX_DURATION;
471   action->generic_action.start = -1.0;
472   action->generic_action.finish = -1.0;
473   action->generic_action.resource_type =
474       (surf_resource_t) surf_network_resource;
475
476   action->generic_action.state_set =
477       surf_network_resource->common_public->states.running_action_set;
478
479   xbt_swag_insert(action, action->generic_action.state_set);
480
481   if(rate>0)
482     action->variable = lmm_variable_new(maxmin_system_network_KCCFLN05, action, 1.0, rate,
483                                         route->size+1);
484   else
485     action->variable = lmm_variable_new(maxmin_system_network_KCCFLN05, action, 1.0, -1.0,
486                                         route->size+1);
487
488   for (i = 0; i < route->size; i++)
489     lmm_expand(maxmin_system_network_KCCFLN05, route->links[i]->constraint, 
490                action->variable, 1.0);
491
492   if(card_src->bus) 
493     lmm_expand(maxmin_system_network_KCCFLN05, card_src->bus, 
494                action->variable, 1.0);
495
496   action->src=src;
497   action->dst=dst;
498
499   xbt_dynar_push(card_src->outgoing_communications,&action);
500   xbt_dynar_push(card_dst->incomming_communications,&action);
501
502   return (surf_action_t) action;
503 }
504
505 static void network_KCCFLN05_action_suspend(surf_action_t action)
506 {
507   lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
508                              ((surf_action_network_KCCFLN05_t) action)->variable, 0.0);
509 }
510
511 static void network_KCCFLN05_action_resume(surf_action_t action)
512 {
513   lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
514                              ((surf_action_network_KCCFLN05_t) action)->variable, 1.0);
515 }
516
517 static int network_KCCFLN05_action_is_suspended(surf_action_t action)
518 {
519   return (lmm_get_variable_weight(((surf_action_network_KCCFLN05_t) action)->variable) == 0.0);
520 }
521
522 /***************** CPU ****************/
523 static void action_cpu_KCCFLN05_free(surf_action_t action)
524 {
525   xbt_swag_remove(action, action->state_set);
526   if(((surf_action_cpu_KCCFLN05_t)action)->variable)
527     lmm_variable_free(maxmin_system_cpu_KCCFLN05, ((surf_action_cpu_KCCFLN05_t)action)->variable);
528   xbt_free(action);
529 }
530
531 static void action_cpu_KCCFLN05_change_state(surf_action_t action,
532                                 e_surf_action_state_t state)
533 {
534   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED))
535     if(((surf_action_cpu_KCCFLN05_t)action)->variable) {
536       lmm_variable_disable(maxmin_system_cpu_KCCFLN05, 
537                            ((surf_action_cpu_KCCFLN05_t)action)->variable);
538       ((surf_action_cpu_KCCFLN05_t)action)->variable = NULL;
539     }
540
541   surf_action_change_state(action, state);
542   return;
543 }
544
545 /* #define WARNING(format, ...) (fprintf(stderr, "[%s , %s : %d] ", __FILE__, __FUNCTION__, __LINE__),\ */
546 /*                               fprintf(stderr, format, ## __VA_ARGS__), \ */
547 /*                               fprintf(stderr, "\n")) */
548 /* #define VOIRP(expr) WARNING("  {" #expr " = %p }", expr) */
549 /* #define VOIRD(expr) WARNING("  {" #expr " = %d }", expr) */
550 /* #define VOIRG(expr) WARNING("  {" #expr " = %lg }", expr) */
551
552 static double share_cpu_KCCFLN05_resources(double now)
553 {
554   s_surf_action_cpu_KCCFLN05_t s_cpu_action;
555   lmm_constraint_t cnst = NULL;
556   workstation_KCCFLN05_t workstation = NULL;
557   double W=0.0;
558   double scale=0.0;
559   int cpt;
560   surf_action_network_KCCFLN05_t action;
561
562   if(surf_get_clock()>=475.895) 
563     {
564       W=0.0;
565     }
566
567   for(cnst = lmm_get_first_active_constraint(maxmin_system_cpu_KCCFLN05);
568       cnst;
569       cnst= lmm_get_next_active_constraint(maxmin_system_cpu_KCCFLN05, cnst))
570     {
571       workstation = lmm_constraint_id(cnst);
572       W=workstation->power_current * workstation->power_scale;
573       
574       if((!xbt_dynar_length(workstation->incomming_communications)) &&
575          (!xbt_dynar_length(workstation->outgoing_communications))) {
576         scale = 1.0;
577       } else if((!xbt_dynar_length(workstation->incomming_communications)) &&
578                 (xbt_dynar_length(workstation->outgoing_communications))) {
579         scale = workstation->interference_send;
580         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
581 /*        VOIRD(action->src->id); */
582 /*        VOIRD(action->dst->id); */
583 /*        VOIRP(&ROUTE(action->src->id,action->dst->id)); */
584 /*        VOIRG(ROUTE(action->src->id,action->dst->id).impact_on_src); */
585           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src *
586             lmm_variable_getvalue(action->variable);
587         }
588         if(scale<0.0) scale=0.0;
589         xbt_assert0(scale>=0.0,"Negative interference !");
590       } else if((xbt_dynar_length(workstation->incomming_communications)) &&
591                 (!xbt_dynar_length(workstation->outgoing_communications))) {
592         scale = workstation->interference_recv;
593         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
594           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst *
595             lmm_variable_getvalue(action->variable);
596         }
597         if(scale<0.0) scale=0.0;
598         xbt_assert0(scale>=0.0,"Negative interference !");
599       } else {
600         scale = workstation->interference_send_recv;
601         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
602           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src_with_other_recv *
603             lmm_variable_getvalue(action->variable);
604         }
605         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
606           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst_with_other_send *
607             lmm_variable_getvalue(action->variable);
608         }
609         if(scale<0.0) scale=0.1;
610         xbt_assert0(scale>=0.0,"Negative interference !");
611       }
612       lmm_update_constraint_bound(maxmin_system_cpu_KCCFLN05,workstation->constraint,
613                                   W*scale);
614     }
615
616   return generic_maxmin_share_resources2(surf_cpu_resource->common_public->
617                                          states.running_action_set,
618                                          xbt_swag_offset(s_cpu_action, variable),
619                                          maxmin_system_cpu_KCCFLN05);
620 }
621
622 static void update_actions_cpu_KCCFLN05_state(double now, double delta)
623 {
624   surf_action_cpu_KCCFLN05_t action = NULL;
625   surf_action_cpu_KCCFLN05_t next_action = NULL;
626   xbt_swag_t running_actions =
627       surf_cpu_resource->common_public->states.running_action_set;
628   xbt_swag_t failed_actions =
629       surf_cpu_resource->common_public->states.failed_action_set;
630
631   xbt_swag_foreach_safe(action, next_action, running_actions) {
632     surf_double_update(&(action->generic_action.remains),
633         lmm_variable_getvalue(action->variable) * delta);
634     if (action->generic_action.max_duration != NO_MAX_DURATION)
635       action->generic_action.max_duration -= delta;
636     if ((action->generic_action.remains <= 0) && 
637         (lmm_get_variable_weight(action->variable)>0)) {
638       action->generic_action.finish = surf_get_clock();
639       action_cpu_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
640     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
641                (action->generic_action.max_duration <= 0)) {
642       action->generic_action.finish = surf_get_clock();
643       action_cpu_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
644     } else {                    /* Need to check that none of the resource has failed */
645       lmm_constraint_t cnst = NULL;
646       int i = 0;
647       workstation_KCCFLN05_t cpu = NULL;
648
649       while ((cnst =
650               lmm_get_cnst_from_var(maxmin_system_cpu_KCCFLN05, action->variable,
651                                     i++))) {
652         cpu = lmm_constraint_id(cnst);
653         if (cpu->state_current == SURF_CPU_OFF) {
654           action->generic_action.finish = surf_get_clock();
655           action_cpu_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_FAILED);
656           break;
657         }
658       }
659     }
660   }
661
662   return;
663 }
664
665 static surf_action_t execute_KCCFLN05(void *cpu, double size)
666 {
667   surf_action_cpu_KCCFLN05_t action = NULL;
668   workstation_KCCFLN05_t CPU = cpu;
669
670   action = xbt_new0(s_surf_action_cpu_KCCFLN05_t, 1);
671
672   action->generic_action.cost = size;
673   action->generic_action.remains = size;
674   action->generic_action.max_duration = NO_MAX_DURATION;
675   action->generic_action.start = surf_get_clock();
676   action->generic_action.finish = -1.0;
677   action->generic_action.resource_type =
678       (surf_resource_t) surf_cpu_resource;
679
680   if (CPU->state_current == SURF_CPU_ON)
681     action->generic_action.state_set =
682         surf_cpu_resource->common_public->states.running_action_set;
683   else
684     action->generic_action.state_set =
685         surf_cpu_resource->common_public->states.failed_action_set;
686   xbt_swag_insert(action, action->generic_action.state_set);
687
688   action->variable = lmm_variable_new(maxmin_system_cpu_KCCFLN05, action, 1.0, -1.0, 1);
689   lmm_expand(maxmin_system_cpu_KCCFLN05, CPU->constraint, action->variable,
690              1.0);
691
692   return (surf_action_t) action;
693 }
694
695 static void cpu_KCCFLN05_action_suspend(surf_action_t action)
696 {
697   lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
698                              ((surf_action_cpu_KCCFLN05_t) action)->variable, 0.0);
699 }
700
701 static void cpu_KCCFLN05_action_resume(surf_action_t action)
702 {
703   lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
704                              ((surf_action_cpu_KCCFLN05_t) action)->variable, 1.0);
705 }
706
707 static int cpu_KCCFLN05_action_is_suspended(surf_action_t action)
708 {
709   return (lmm_get_variable_weight(((surf_action_cpu_KCCFLN05_t) action)->variable) == 0.0);
710 }
711
712 /************* workstation ************/
713 static void action_change_state(surf_action_t action,
714                                 e_surf_action_state_t state)
715 {
716   if(action->resource_type==(surf_resource_t)surf_network_resource) 
717     surf_network_resource->common_public->action_change_state(action,state);
718   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
719     surf_cpu_resource->common_public->action_change_state(action,state);
720   else DIE_IMPOSSIBLE;
721   return;
722 }
723
724 static void action_free(surf_action_t action)
725 {
726   if(action->resource_type==(surf_resource_t)surf_network_resource) 
727     surf_network_resource->common_public->action_free(action);
728   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
729     surf_cpu_resource->common_public->action_free(action);
730   else DIE_IMPOSSIBLE;
731   return;
732 }
733
734 static int resource_used(void *resource_id)
735 {
736   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
737   return 0;
738 }
739
740 static double share_resources(double now)
741 {
742   return -1.0;
743 }
744
745 static void update_actions_state(double now, double delta)
746 {
747   return;
748 }
749
750 static void update_resource_state(void *id,
751                                   tmgr_trace_event_t event_type,
752                                   double value)
753 {
754   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
755   return;
756 }
757
758 static void action_suspend(surf_action_t action)
759 {
760   if(action->resource_type==(surf_resource_t)surf_network_resource) 
761     surf_network_resource->common_public->suspend(action);
762   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
763     surf_cpu_resource->common_public->suspend(action);
764   else DIE_IMPOSSIBLE;
765 }
766
767 static void action_resume(surf_action_t action)
768 {
769   if(action->resource_type==(surf_resource_t)surf_network_resource)
770     surf_network_resource->common_public->resume(action);
771   else if(action->resource_type==(surf_resource_t)surf_cpu_resource)
772     surf_cpu_resource->common_public->resume(action);
773   else DIE_IMPOSSIBLE;
774 }
775
776 static int action_is_suspended(surf_action_t action)
777 {
778   if(action->resource_type==(surf_resource_t)surf_network_resource) 
779     return surf_network_resource->common_public->is_suspended(action);
780   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
781     return surf_cpu_resource->common_public->is_suspended(action);
782   DIE_IMPOSSIBLE;
783 }
784
785 /**************************************/
786 /********* Module  creation ***********/
787 /**************************************/
788 static void cpu_KCCFLN05_finalize(void)
789 {
790   xbt_dict_free(&workstation_set);
791   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
792   xbt_swag_free(surf_cpu_resource->common_public->states.
793                 running_action_set);
794   xbt_swag_free(surf_cpu_resource->common_public->states.
795                 failed_action_set);
796   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
797   xbt_free(surf_cpu_resource->common_public);
798   xbt_free(surf_cpu_resource->common_private);
799   xbt_free(surf_cpu_resource->extension_public);
800
801   xbt_free(surf_cpu_resource);
802   surf_cpu_resource = NULL;
803
804   if (maxmin_system_cpu_KCCFLN05) {
805     lmm_system_free(maxmin_system_cpu_KCCFLN05);
806     maxmin_system_cpu_KCCFLN05 = NULL;
807   }
808 }
809
810 static void cpu_KCCFLN05_resource_init_internal(void)
811 {
812   s_surf_action_t action;
813
814   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
815
816   surf_cpu_resource->common_private =
817       xbt_new0(s_surf_resource_private_t, 1);
818   surf_cpu_resource->common_public = 
819     xbt_new0(s_surf_resource_public_t, 1);
820
821   surf_cpu_resource->extension_public =
822       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
823
824   surf_cpu_resource->common_public->states.ready_action_set =
825       xbt_swag_new(xbt_swag_offset(action, state_hookup));
826   surf_cpu_resource->common_public->states.running_action_set =
827       xbt_swag_new(xbt_swag_offset(action, state_hookup));
828   surf_cpu_resource->common_public->states.failed_action_set =
829       xbt_swag_new(xbt_swag_offset(action, state_hookup));
830   surf_cpu_resource->common_public->states.done_action_set =
831       xbt_swag_new(xbt_swag_offset(action, state_hookup));
832
833   surf_cpu_resource->common_public->name_service = name_service;
834   surf_cpu_resource->common_public->get_resource_name = get_resource_name; 
835   surf_cpu_resource->common_public->action_get_state =
836       surf_action_get_state;
837   surf_cpu_resource->common_public->action_free = action_cpu_KCCFLN05_free;
838   surf_cpu_resource->common_public->action_cancel = NULL;
839   surf_cpu_resource->common_public->action_recycle = NULL;
840   surf_cpu_resource->common_public->action_change_state = action_cpu_KCCFLN05_change_state;
841   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
842   surf_cpu_resource->common_public->name = "CPU KCCFLN05";
843
844   surf_cpu_resource->common_private->resource_used = cpu_used;
845   surf_cpu_resource->common_private->share_resources = share_cpu_KCCFLN05_resources;
846   surf_cpu_resource->common_private->update_actions_state =
847     update_actions_cpu_KCCFLN05_state;
848   surf_cpu_resource->common_private->update_resource_state =
849       update_cpu_KCCFLN05_state;
850   surf_cpu_resource->common_private->finalize = cpu_KCCFLN05_finalize;
851
852   surf_cpu_resource->extension_public->execute = execute_KCCFLN05;
853 /*FIXME*//*   surf_cpu_resource->extension_public->sleep = action_sleep; */
854
855   surf_cpu_resource->common_public->suspend = cpu_KCCFLN05_action_suspend;
856   surf_cpu_resource->common_public->resume = cpu_KCCFLN05_action_resume;
857   surf_cpu_resource->common_public->is_suspended = cpu_KCCFLN05_action_is_suspended;
858
859   surf_cpu_resource->extension_public->get_state = get_state;
860
861   workstation_set = xbt_dict_new();
862
863   maxmin_system_cpu_KCCFLN05 = lmm_system_new();
864 }
865
866 static void network_KCCFLN05_finalize(void)
867 {
868   int i,j;
869
870   xbt_dict_free(&network_link_set);
871   xbt_swag_free(surf_network_resource->common_public->states.
872                 ready_action_set);
873   xbt_swag_free(surf_network_resource->common_public->states.
874                 running_action_set);
875   xbt_swag_free(surf_network_resource->common_public->states.
876                 failed_action_set);
877   xbt_swag_free(surf_network_resource->common_public->states.
878                 done_action_set);
879   xbt_free(surf_network_resource->common_public);
880   xbt_free(surf_network_resource->common_private);
881   xbt_free(surf_network_resource->extension_public);
882
883   xbt_free(surf_network_resource);
884   surf_network_resource = NULL;
885
886   for (i = 0; i < nb_workstation; i++) 
887     for (j = 0; j < nb_workstation; j++) 
888       xbt_free(ROUTE(i,j).links);
889   xbt_free(routing_table);
890   routing_table = NULL;
891   nb_workstation = 0;
892
893   if (maxmin_system_network_KCCFLN05) {
894     lmm_system_free(maxmin_system_network_KCCFLN05);
895     maxmin_system_network_KCCFLN05 = NULL;
896   }
897 }
898
899 static void network_KCCFLN05_resource_init_internal(void)
900 {
901   s_surf_action_t action;
902
903   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
904
905   surf_network_resource->common_private =
906       xbt_new0(s_surf_resource_private_t, 1);
907   surf_network_resource->common_public =
908       xbt_new0(s_surf_resource_public_t, 1);
909   surf_network_resource->extension_public =
910       xbt_new0(s_surf_network_resource_extension_public_t, 1);
911
912   surf_network_resource->common_public->states.ready_action_set =
913       xbt_swag_new(xbt_swag_offset(action, state_hookup));
914   surf_network_resource->common_public->states.running_action_set =
915       xbt_swag_new(xbt_swag_offset(action, state_hookup));
916   surf_network_resource->common_public->states.failed_action_set =
917       xbt_swag_new(xbt_swag_offset(action, state_hookup));
918   surf_network_resource->common_public->states.done_action_set =
919       xbt_swag_new(xbt_swag_offset(action, state_hookup));
920
921   surf_network_resource->common_public->name_service = name_service;
922   surf_network_resource->common_public->get_resource_name = get_resource_name;
923   surf_network_resource->common_public->action_get_state =
924       surf_action_get_state;
925   surf_network_resource->common_public->action_free = action_network_KCCFLN05_free;
926   surf_network_resource->common_public->action_cancel = NULL;
927   surf_network_resource->common_public->action_recycle = NULL;
928   surf_network_resource->common_public->action_change_state = action_network_KCCFLN05_change_state;
929   surf_network_resource->common_public->action_set_data = surf_action_set_data;
930   surf_network_resource->common_public->name = "network KCCFLN05";
931
932   surf_network_resource->common_private->resource_used = link_used;
933   surf_network_resource->common_private->share_resources = share_network_KCCFLN05_resources;
934   surf_network_resource->common_private->update_actions_state =
935     update_actions_network_KCCFLN05_state;
936   surf_network_resource->common_private->update_resource_state =
937     update_network_KCCFLN05_state;
938   surf_network_resource->common_private->finalize = network_KCCFLN05_finalize;
939
940   surf_network_resource->common_public->suspend = network_KCCFLN05_action_suspend;
941   surf_network_resource->common_public->resume = network_KCCFLN05_action_resume;
942   surf_network_resource->common_public->is_suspended = network_KCCFLN05_action_is_suspended;
943
944   surf_network_resource->extension_public->communicate = communicate_KCCFLN05;
945
946   network_link_set = xbt_dict_new();
947
948
949   maxmin_system_network_KCCFLN05 = lmm_system_new();
950 }
951
952 static void workstation_KCCFLN05_finalize(void)
953 {
954   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
955   xbt_swag_free(surf_workstation_resource->common_public->states.running_action_set);
956   xbt_swag_free(surf_workstation_resource->common_public->states.failed_action_set);
957   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
958
959   xbt_free(surf_workstation_resource->common_public);
960   xbt_free(surf_workstation_resource->common_private);
961   xbt_free(surf_workstation_resource->extension_public);
962
963   xbt_free(surf_workstation_resource);
964   surf_workstation_resource = NULL;
965 }
966
967 static void workstation_KCCFLN05_resource_init_internal(void)
968 {
969   s_surf_action_t action;
970
971   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
972
973   surf_workstation_resource->common_private =
974       xbt_new0(s_surf_resource_private_t, 1);
975   surf_workstation_resource->common_public =
976       xbt_new0(s_surf_resource_public_t, 1);
977   surf_workstation_resource->extension_public =
978       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
979
980   surf_workstation_resource->common_public->states.ready_action_set =
981       xbt_swag_new(xbt_swag_offset(action, state_hookup));
982   surf_workstation_resource->common_public->states.running_action_set =
983       xbt_swag_new(xbt_swag_offset(action, state_hookup));
984   surf_workstation_resource->common_public->states.failed_action_set =
985       xbt_swag_new(xbt_swag_offset(action, state_hookup));
986   surf_workstation_resource->common_public->states.done_action_set =
987       xbt_swag_new(xbt_swag_offset(action, state_hookup));
988
989   surf_workstation_resource->common_public->name_service = name_service;
990   surf_workstation_resource->common_public->get_resource_name =
991       get_resource_name;
992   surf_workstation_resource->common_public->action_get_state =
993       surf_action_get_state;
994   surf_workstation_resource->common_public->action_free = action_free;
995   surf_workstation_resource->common_public->action_cancel = NULL;
996   surf_workstation_resource->common_public->action_recycle = NULL;
997   surf_workstation_resource->common_public->action_change_state = action_change_state;
998   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
999   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
1000
1001   surf_workstation_resource->common_private->resource_used = resource_used;
1002   surf_workstation_resource->common_private->share_resources = share_resources;
1003   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
1004   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
1005   surf_workstation_resource->common_private->finalize = workstation_KCCFLN05_finalize;
1006
1007
1008   surf_workstation_resource->common_public->suspend = action_suspend;
1009   surf_workstation_resource->common_public->resume = action_resume;
1010   surf_workstation_resource->common_public->is_suspended = action_is_suspended;
1011
1012   surf_workstation_resource->extension_public->execute = execute_KCCFLN05;
1013 /*FIXME*//*  surf_workstation_resource->extension_public->sleep = action_sleep; */
1014   surf_workstation_resource->extension_public->get_state = get_state;
1015   surf_workstation_resource->extension_public->communicate = communicate_KCCFLN05;
1016 }
1017
1018 /**************************************/
1019 /*************** Generic **************/
1020 /**************************************/
1021
1022 void surf_workstation_resource_init_KCCFLN05(const char *filename)
1023 {
1024   xbt_assert0(!surf_cpu_resource,"CPU resource type already defined");
1025   cpu_KCCFLN05_resource_init_internal();
1026   xbt_assert0(!surf_network_resource,"network resource type already defined");
1027   network_KCCFLN05_resource_init_internal();
1028   workstation_KCCFLN05_resource_init_internal();
1029   parse_file(filename);
1030
1031   xbt_dynar_push(resource_list, &surf_network_resource);
1032   xbt_dynar_push(resource_list, &surf_cpu_resource);
1033   xbt_dynar_push(resource_list, &surf_workstation_resource);
1034
1035   if (maxmin_system) { /* To avoid using this useless system */
1036     lmm_system_free(maxmin_system);
1037     maxmin_system = NULL;
1038   }
1039 }