Logo AND Algorithmique Numérique Distribuée

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