Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
96a465501ddeab5330394ee964f085de64ae2f3d
[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   nb_link = 0;
272   link_name = NULL;
273 }
274
275 static void parse_route_elem(void)
276 {
277   nb_link++;
278   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
279   link_name[(nb_link) - 1] = xbt_strdup(A_route_element_name);
280 }
281
282 static void parse_route_set_route(void)
283 {
284   route_new(src_id, dst_id, link_name, nb_link, impact_on_src, impact_on_dst,
285             impact_on_src_with_other_recv, impact_on_dst_with_other_send);
286 }
287
288 static void parse_file(const char *file)
289 {
290   /* Figuring out the workstations */
291   surf_parse_reset_parser();
292   ETag_cpu_fun=parse_workstation;
293   surf_parse_open(file);
294   xbt_assert1((!surf_parse()),"Parse error in %s",file);
295   surf_parse_close();
296
297   create_routing_table();
298
299   /* Figuring out the network links */
300   surf_parse_reset_parser();
301   ETag_network_link_fun=parse_network_link;
302   surf_parse_open(file);
303   xbt_assert1((!surf_parse()),"Parse error in %s",file);
304   surf_parse_close();
305
306   /* Building the routes */
307   surf_parse_reset_parser();
308   STag_route_fun=parse_route_set_endpoints;
309   ETag_route_element_fun=parse_route_elem;
310   ETag_route_fun=parse_route_set_route;
311   surf_parse_open(file);
312   xbt_assert1((!surf_parse()),"Parse error in %s",file);
313   surf_parse_close();
314 }
315
316 static void update_network_KCCFLN05_state(void *id,
317                                           tmgr_trace_event_t event_type,
318                                           double value)
319 {
320   network_link_KCCFLN05_t nw_link = id;
321
322   if (event_type == nw_link->bw_event) {
323     nw_link->bw_current = value;
324     lmm_update_constraint_bound(maxmin_system_network_KCCFLN05, nw_link->constraint,
325                                 nw_link->bw_current);
326   } else if (event_type == nw_link->state_event) {
327     if (value > 0)
328       nw_link->state_current = SURF_NETWORK_LINK_ON;
329     else
330       nw_link->state_current = SURF_NETWORK_LINK_OFF;
331   } else {
332     CRITICAL0("Unknown event ! \n");
333     xbt_abort();
334   }
335
336   return;
337 }
338
339 /**************************************/
340 /*************** actions **************/
341 /**************************************/
342 /*************** network **************/
343 static void action_network_KCCFLN05_free(surf_action_t action)
344 {
345   int cpt;
346   surf_action_t act = NULL;
347   workstation_KCCFLN05_t src = ((surf_action_network_KCCFLN05_t) action)->src;
348   workstation_KCCFLN05_t dst = ((surf_action_network_KCCFLN05_t) action)->dst;
349
350   xbt_swag_remove(action, action->state_set);
351   lmm_variable_free(maxmin_system_network_KCCFLN05, 
352                     ((surf_action_network_KCCFLN05_t) action)->variable);
353
354   xbt_dynar_foreach (src->outgoing_communications,cpt,act) {
355     if(act==action) {
356       xbt_dynar_remove_at(src->outgoing_communications, cpt, &act);
357       break;
358     }
359   }
360
361   xbt_dynar_foreach (dst->incomming_communications,cpt,act) {
362     if(act==action) {
363       xbt_dynar_remove_at(dst->incomming_communications, cpt, &act);
364       break;
365     }
366   }
367
368   xbt_free(action);
369 }
370
371 static double share_network_KCCFLN05_resources(double now)
372 {
373  s_surf_action_network_KCCFLN05_t action;
374  return generic_maxmin_share_resources2(surf_network_resource->common_public->
375                                         states.running_action_set,
376                                         xbt_swag_offset(action, variable),
377                                         maxmin_system_network_KCCFLN05);
378 }
379
380 static void action_network_KCCFLN05_change_state(surf_action_t action,
381                                                  e_surf_action_state_t state)
382 {
383   surf_action_change_state(action, state);
384   return;
385 }
386
387 static void update_actions_network_KCCFLN05_state(double now, double delta)
388 {
389   surf_action_network_KCCFLN05_t action = NULL;
390   surf_action_network_KCCFLN05_t next_action = NULL;
391   xbt_swag_t running_actions =
392       surf_network_resource->common_public->states.running_action_set;
393   xbt_swag_t failed_actions =
394       surf_network_resource->common_public->states.failed_action_set;
395
396   xbt_swag_foreach_safe(action, next_action, running_actions) {
397     action->generic_action.remains -=
398         lmm_variable_getvalue(action->variable) * delta;
399     if (action->generic_action.max_duration != NO_MAX_DURATION)
400       action->generic_action.max_duration -= delta;
401     if ((action->generic_action.remains <= 0) && 
402         (lmm_get_variable_weight(action->variable)>0)) {
403       action->generic_action.finish = surf_get_clock();
404       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
405     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
406                (action->generic_action.max_duration <= 0)) {
407       action->generic_action.finish = surf_get_clock();
408       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
409     } else {                    /* Need to check that none of the resource has failed */
410       /* FIXME : FAILURES ARE NOT HANDLED YET */
411     }
412   }
413
414   xbt_swag_foreach_safe(action, next_action, failed_actions) {
415     lmm_variable_disable(maxmin_system_network_KCCFLN05, action->variable);
416   }
417
418   return;
419 }
420
421 static surf_action_t communicate_KCCFLN05(void *src, void *dst, double size)
422 {
423   surf_action_network_KCCFLN05_t action = NULL;
424   workstation_KCCFLN05_t card_src = src;
425   workstation_KCCFLN05_t card_dst = dst;
426   route_KCCFLN05_t route = &ROUTE(card_src->id, card_dst->id);
427   int i;
428
429   action = xbt_new0(s_surf_action_network_KCCFLN05_t, 1);
430
431   action->generic_action.cost = size;
432   action->generic_action.remains = size;
433   action->generic_action.max_duration = NO_MAX_DURATION;
434   action->generic_action.start = -1.0;
435   action->generic_action.finish = -1.0;
436   action->generic_action.resource_type =
437       (surf_resource_t) surf_network_resource;
438
439   action->generic_action.state_set =
440       surf_network_resource->common_public->states.running_action_set;
441
442   xbt_swag_insert(action, action->generic_action.state_set);
443
444   action->variable = lmm_variable_new(maxmin_system_network_KCCFLN05, action, 1.0, -1.0,
445                                       route->size);
446
447   for (i = 0; i < route->size; i++)
448     lmm_expand(maxmin_system_network_KCCFLN05, route->links[i]->constraint, 
449                action->variable, 1.0);
450
451   action->src=src;
452   action->dst=dst;
453
454   xbt_dynar_push(card_src->outgoing_communications,&action);
455   xbt_dynar_push(card_dst->incomming_communications,&action);
456
457   return (surf_action_t) action;
458 }
459
460 /***************** CPU ****************/
461 static void action_cpu_KCCFLN05_free(surf_action_t action)
462 {
463   xbt_swag_remove(action, action->state_set);
464   lmm_variable_free(maxmin_system_cpu_KCCFLN05, ((surf_action_cpu_KCCFLN05_t)action)->variable);
465   xbt_free(action);
466 }
467
468 static double share_cpu_KCCFLN05_resources(double now)
469 {
470   s_surf_action_cpu_KCCFLN05_t s_cpu_action;
471   lmm_constraint_t cnst = NULL;
472   workstation_KCCFLN05_t workstation = NULL;
473   double W=0.0;
474   double scale=0.0;
475   int cpt;
476   surf_action_network_KCCFLN05_t action;
477
478   for(cnst = lmm_get_first_active_constraint(maxmin_system_cpu_KCCFLN05);
479       cnst;
480       cnst= lmm_get_next_active_constraint(maxmin_system_cpu_KCCFLN05, cnst))
481     {
482       workstation = lmm_constraint_id(cnst);
483       W=workstation->power_current * workstation->power_scale;
484       
485       if((!xbt_dynar_length(workstation->incomming_communications)) &&
486          (!xbt_dynar_length(workstation->outgoing_communications))) {
487         scale = 1.0;
488       } else if((!xbt_dynar_length(workstation->incomming_communications)) &&
489                 (xbt_dynar_length(workstation->outgoing_communications))) {
490         scale = workstation->interference_send;
491         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
492           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src *
493             lmm_variable_getvalue(action->variable);
494         }
495         xbt_assert0(scale>0.0,"Negative interference !");
496       } else if((xbt_dynar_length(workstation->incomming_communications)) &&
497                 (!xbt_dynar_length(workstation->outgoing_communications))) {
498         scale = workstation->interference_recv;
499         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
500           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst *
501             lmm_variable_getvalue(action->variable);
502         }
503         xbt_assert0(scale>0.0,"Negative interference !");
504       } else {
505         scale = workstation->interference_send_recv;
506         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
507           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src_with_other_recv *
508             lmm_variable_getvalue(action->variable);
509         }
510         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
511           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst_with_other_send *
512             lmm_variable_getvalue(action->variable);
513         }
514         xbt_assert0(scale>0.0,"Negative interference !");
515       }
516       lmm_update_constraint_bound(maxmin_system_cpu_KCCFLN05,workstation->constraint,
517                                   W*scale);
518     }
519
520   return generic_maxmin_share_resources2(surf_cpu_resource->common_public->
521                                          states.running_action_set,
522                                          xbt_swag_offset(s_cpu_action, variable),
523                                          maxmin_system_cpu_KCCFLN05);
524 }
525
526 static void update_actions_cpu_KCCFLN05_state(double now, double delta)
527 {
528   surf_action_cpu_KCCFLN05_t action = NULL;
529   surf_action_cpu_KCCFLN05_t next_action = NULL;
530   xbt_swag_t running_actions =
531       surf_cpu_resource->common_public->states.running_action_set;
532   xbt_swag_t failed_actions =
533       surf_cpu_resource->common_public->states.failed_action_set;
534
535   xbt_swag_foreach_safe(action, next_action, running_actions) {
536     action->generic_action.remains -=
537         lmm_variable_getvalue(action->variable) * delta;
538     if (action->generic_action.max_duration != NO_MAX_DURATION)
539       action->generic_action.max_duration -= delta;
540     if ((action->generic_action.remains <= 0) && 
541         (lmm_get_variable_weight(action->variable)>0)) {
542       action->generic_action.finish = surf_get_clock();
543       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
544     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
545                (action->generic_action.max_duration <= 0)) {
546       action->generic_action.finish = surf_get_clock();
547       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
548     } else {                    /* Need to check that none of the resource has failed */
549       lmm_constraint_t cnst = NULL;
550       int i = 0;
551       workstation_KCCFLN05_t cpu = NULL;
552
553       while ((cnst =
554               lmm_get_cnst_from_var(maxmin_system_cpu_KCCFLN05, action->variable,
555                                     i++))) {
556         cpu = lmm_constraint_id(cnst);
557         if (cpu->state_current == SURF_CPU_OFF) {
558           action->generic_action.finish = surf_get_clock();
559           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
560           break;
561         }
562       }
563     }
564   }
565
566   xbt_swag_foreach_safe(action, next_action, failed_actions) {
567     lmm_variable_disable(maxmin_system_cpu_KCCFLN05, action->variable);
568   }
569
570   return;
571 }
572
573 static surf_action_t execute_KCCFLN05(void *cpu, double size)
574 {
575   surf_action_cpu_KCCFLN05_t action = NULL;
576   workstation_KCCFLN05_t CPU = cpu;
577
578   action = xbt_new0(s_surf_action_cpu_KCCFLN05_t, 1);
579
580   action->generic_action.cost = size;
581   action->generic_action.remains = size;
582   action->generic_action.max_duration = NO_MAX_DURATION;
583   action->generic_action.start = surf_get_clock();
584   action->generic_action.finish = -1.0;
585   action->generic_action.resource_type =
586       (surf_resource_t) surf_cpu_resource;
587
588   if (CPU->state_current == SURF_CPU_ON)
589     action->generic_action.state_set =
590         surf_cpu_resource->common_public->states.running_action_set;
591   else
592     action->generic_action.state_set =
593         surf_cpu_resource->common_public->states.failed_action_set;
594   xbt_swag_insert(action, action->generic_action.state_set);
595
596   action->variable = lmm_variable_new(maxmin_system_cpu_KCCFLN05, action, 1.0, -1.0, 1);
597   lmm_expand(maxmin_system_cpu_KCCFLN05, CPU->constraint, action->variable,
598              1.0);
599
600   return (surf_action_t) action;
601 }
602
603 static void cpu_KCCFLN05_action_suspend(surf_action_t action)
604 {
605   if(action->resource_type==(surf_resource_t)surf_network_resource) 
606     lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
607                                ((surf_action_network_KCCFLN05_t) action)->variable, 0.0);
608   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
609     lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
610                                ((surf_action_cpu_KCCFLN05_t) action)->variable, 0.0);
611   else DIE_IMPOSSIBLE;
612 }
613
614 static void cpu_KCCFLN05_action_resume(surf_action_t action)
615 {
616   if(action->resource_type==(surf_resource_t)surf_network_resource) 
617     lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
618                                ((surf_action_network_KCCFLN05_t) action)->variable, 1.0);
619   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
620     lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
621                                ((surf_action_cpu_KCCFLN05_t) action)->variable, 1.0);
622   else DIE_IMPOSSIBLE;
623 }
624
625 static int cpu_KCCFLN05_action_is_suspended(surf_action_t action)
626 {
627   if(action->resource_type==(surf_resource_t)surf_network_resource) 
628     return (lmm_get_variable_weight(((surf_action_cpu_KCCFLN05_t) action)->variable) == 0.0);
629   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
630     return (lmm_get_variable_weight(((surf_action_network_KCCFLN05_t) action)->variable) == 0.0);
631   DIE_IMPOSSIBLE;
632 }
633
634 /************* workstation ************/
635 static void action_change_state(surf_action_t action,
636                                 e_surf_action_state_t state)
637 {
638   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
639   surf_action_change_state(action, state);
640   return;
641 }
642
643 static void action_free(surf_action_t action)
644 {
645   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
646   return;
647 }
648
649 static int resource_used(void *resource_id)
650 {
651   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
652   return 0;
653 }
654
655 static double share_resources(double now)
656 {
657   return -1.0;
658 }
659
660 static void update_actions_state(double now, double delta)
661 {
662   return;
663 }
664
665 static void update_resource_state(void *id,
666                                   tmgr_trace_event_t event_type,
667                                   double value)
668 {
669   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
670   return;
671 }
672
673 static void action_suspend(surf_action_t action)
674 {
675   xbt_assert0(action->resource_type ==
676               ((surf_resource_t) surf_cpu_resource),
677               "Resource type mismatch");
678   surf_cpu_resource->extension_public->suspend(action);
679 }
680
681 static void action_resume(surf_action_t action)
682 {
683   xbt_assert0(action->resource_type ==
684               ((surf_resource_t) surf_cpu_resource),
685               "Resource type mismatch");
686   surf_cpu_resource->extension_public->resume(action);
687 }
688
689 static int action_is_suspended(surf_action_t action)
690 {
691   if(action->resource_type==(surf_resource_t)surf_network_resource) 
692     return 0;
693   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
694     return surf_cpu_resource->extension_public->is_suspended(action);
695   DIE_IMPOSSIBLE;
696 }
697
698
699 /**************************************/
700 /********* Module  creation ***********/
701 /**************************************/
702 static void cpu_KCCFLN05_finalize(void)
703 {
704   xbt_dict_free(&workstation_set);
705   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
706   xbt_swag_free(surf_cpu_resource->common_public->states.
707                 running_action_set);
708   xbt_swag_free(surf_cpu_resource->common_public->states.
709                 failed_action_set);
710   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
711   xbt_free(surf_cpu_resource->common_public);
712   xbt_free(surf_cpu_resource->common_private);
713   xbt_free(surf_cpu_resource->extension_public);
714
715   xbt_free(surf_cpu_resource);
716   surf_cpu_resource = NULL;
717
718   if (maxmin_system_cpu_KCCFLN05) {
719     lmm_system_free(maxmin_system_cpu_KCCFLN05);
720     maxmin_system_cpu_KCCFLN05 = NULL;
721   }
722 }
723
724 static void cpu_KCCFLN05_resource_init_internal(void)
725 {
726   s_surf_action_t action;
727
728   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
729
730   surf_cpu_resource->common_private =
731       xbt_new0(s_surf_resource_private_t, 1);
732   surf_cpu_resource->common_public = 
733     xbt_new0(s_surf_resource_public_t, 1);
734
735   surf_cpu_resource->extension_public =
736       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
737
738   surf_cpu_resource->common_public->states.ready_action_set =
739       xbt_swag_new(xbt_swag_offset(action, state_hookup));
740   surf_cpu_resource->common_public->states.running_action_set =
741       xbt_swag_new(xbt_swag_offset(action, state_hookup));
742   surf_cpu_resource->common_public->states.failed_action_set =
743       xbt_swag_new(xbt_swag_offset(action, state_hookup));
744   surf_cpu_resource->common_public->states.done_action_set =
745       xbt_swag_new(xbt_swag_offset(action, state_hookup));
746
747   surf_cpu_resource->common_public->name_service = name_service;
748   surf_cpu_resource->common_public->get_resource_name = get_resource_name; 
749   surf_cpu_resource->common_public->action_get_state =
750       surf_action_get_state;
751   surf_cpu_resource->common_public->action_free = action_cpu_KCCFLN05_free;
752   surf_cpu_resource->common_public->action_cancel = NULL;
753   surf_cpu_resource->common_public->action_recycle = NULL;
754   surf_cpu_resource->common_public->action_change_state = surf_action_change_state;
755   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
756   surf_cpu_resource->common_public->name = "CPU KCCFLN05";
757
758   surf_cpu_resource->common_private->resource_used = cpu_used;
759   surf_cpu_resource->common_private->share_resources = share_cpu_KCCFLN05_resources;
760   surf_cpu_resource->common_private->update_actions_state =
761     update_actions_cpu_KCCFLN05_state;
762   surf_cpu_resource->common_private->update_resource_state =
763       update_cpu_KCCFLN05_state;
764   surf_cpu_resource->common_private->finalize = cpu_KCCFLN05_finalize;
765
766   surf_cpu_resource->extension_public->execute = execute_KCCFLN05;
767 /*FIXME*//*   surf_cpu_resource->extension_public->sleep = action_sleep; */
768   surf_cpu_resource->extension_public->suspend = cpu_KCCFLN05_action_suspend;
769   surf_cpu_resource->extension_public->resume = cpu_KCCFLN05_action_resume;
770   surf_cpu_resource->extension_public->is_suspended = cpu_KCCFLN05_action_is_suspended;
771
772   surf_cpu_resource->extension_public->get_state = get_state;
773
774   workstation_set = xbt_dict_new();
775
776   maxmin_system_cpu_KCCFLN05 = lmm_system_new();
777 }
778
779 static void network_KCCFLN05_finalize(void)
780 {
781   int i,j;
782
783   xbt_dict_free(&network_link_set);
784   xbt_swag_free(surf_network_resource->common_public->states.
785                 ready_action_set);
786   xbt_swag_free(surf_network_resource->common_public->states.
787                 running_action_set);
788   xbt_swag_free(surf_network_resource->common_public->states.
789                 failed_action_set);
790   xbt_swag_free(surf_network_resource->common_public->states.
791                 done_action_set);
792   xbt_free(surf_network_resource->common_public);
793   xbt_free(surf_network_resource->common_private);
794   xbt_free(surf_network_resource->extension_public);
795
796   xbt_free(surf_network_resource);
797   surf_network_resource = NULL;
798
799   for (i = 0; i < nb_workstation; i++) 
800     for (j = 0; j < nb_workstation; j++) 
801       xbt_free(ROUTE(i,j).links);
802   xbt_free(routing_table);
803   routing_table = NULL;
804   nb_workstation = 0;
805
806   if (maxmin_system_network_KCCFLN05) {
807     lmm_system_free(maxmin_system_network_KCCFLN05);
808     maxmin_system_network_KCCFLN05 = NULL;
809   }
810 }
811
812 static void network_KCCFLN05_resource_init_internal(void)
813 {
814   s_surf_action_t action;
815
816   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
817
818   surf_network_resource->common_private =
819       xbt_new0(s_surf_resource_private_t, 1);
820   surf_network_resource->common_public =
821       xbt_new0(s_surf_resource_public_t, 1);
822   surf_network_resource->extension_public =
823       xbt_new0(s_surf_network_resource_extension_public_t, 1);
824
825   surf_network_resource->common_public->states.ready_action_set =
826       xbt_swag_new(xbt_swag_offset(action, state_hookup));
827   surf_network_resource->common_public->states.running_action_set =
828       xbt_swag_new(xbt_swag_offset(action, state_hookup));
829   surf_network_resource->common_public->states.failed_action_set =
830       xbt_swag_new(xbt_swag_offset(action, state_hookup));
831   surf_network_resource->common_public->states.done_action_set =
832       xbt_swag_new(xbt_swag_offset(action, state_hookup));
833
834   surf_network_resource->common_public->name_service = name_service;
835   surf_network_resource->common_public->get_resource_name = get_resource_name;
836   surf_network_resource->common_public->action_get_state =
837       surf_action_get_state;
838   surf_network_resource->common_public->action_free = action_network_KCCFLN05_free;
839   surf_network_resource->common_public->action_cancel = NULL;
840   surf_network_resource->common_public->action_recycle = NULL;
841   surf_network_resource->common_public->action_change_state = action_network_KCCFLN05_change_state;
842   surf_network_resource->common_public->action_set_data = surf_action_set_data;
843   surf_network_resource->common_public->name = "network KCCFLN05";
844
845   surf_network_resource->common_private->resource_used = link_used;
846   surf_network_resource->common_private->share_resources = share_network_KCCFLN05_resources;
847   surf_network_resource->common_private->update_actions_state =
848     update_actions_network_KCCFLN05_state;
849   surf_network_resource->common_private->update_resource_state =
850     update_network_KCCFLN05_state;
851   surf_network_resource->common_private->finalize = network_KCCFLN05_finalize;
852
853  surf_network_resource->extension_public->communicate = communicate_KCCFLN05;
854
855   network_link_set = xbt_dict_new();
856
857
858   maxmin_system_network_KCCFLN05 = lmm_system_new();
859 }
860
861 static void workstation_KCCFLN05_finalize(void)
862 {
863   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
864   xbt_swag_free(surf_workstation_resource->common_public->states.running_action_set);
865   xbt_swag_free(surf_workstation_resource->common_public->states.failed_action_set);
866   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
867
868   xbt_free(surf_workstation_resource->common_public);
869   xbt_free(surf_workstation_resource->common_private);
870   xbt_free(surf_workstation_resource->extension_public);
871
872   xbt_free(surf_workstation_resource);
873   surf_workstation_resource = NULL;
874 }
875
876 static void workstation_KCCFLN05_resource_init_internal(void)
877 {
878   s_surf_action_t action;
879
880   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
881
882   surf_workstation_resource->common_private =
883       xbt_new0(s_surf_resource_private_t, 1);
884   surf_workstation_resource->common_public =
885       xbt_new0(s_surf_resource_public_t, 1);
886   surf_workstation_resource->extension_public =
887       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
888
889   surf_workstation_resource->common_public->states.ready_action_set =
890       xbt_swag_new(xbt_swag_offset(action, state_hookup));
891   surf_workstation_resource->common_public->states.running_action_set =
892       xbt_swag_new(xbt_swag_offset(action, state_hookup));
893   surf_workstation_resource->common_public->states.failed_action_set =
894       xbt_swag_new(xbt_swag_offset(action, state_hookup));
895   surf_workstation_resource->common_public->states.done_action_set =
896       xbt_swag_new(xbt_swag_offset(action, state_hookup));
897
898   surf_workstation_resource->common_public->name_service = name_service;
899   surf_workstation_resource->common_public->get_resource_name =
900       get_resource_name;
901   surf_workstation_resource->common_public->action_get_state =
902       surf_action_get_state;
903   surf_workstation_resource->common_public->action_free = action_free;
904   surf_workstation_resource->common_public->action_cancel = NULL;
905   surf_workstation_resource->common_public->action_recycle = NULL;
906   surf_workstation_resource->common_public->action_change_state = action_change_state;
907   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
908   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
909
910   surf_workstation_resource->common_private->resource_used = resource_used;
911   surf_workstation_resource->common_private->share_resources = share_resources;
912   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
913   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
914   surf_workstation_resource->common_private->finalize = workstation_KCCFLN05_finalize;
915
916   surf_workstation_resource->extension_public->execute = execute_KCCFLN05;
917 /*FIXME*//*  surf_workstation_resource->extension_public->sleep = action_sleep; */
918   surf_workstation_resource->extension_public->suspend = action_suspend;
919   surf_workstation_resource->extension_public->resume = action_resume;
920   surf_workstation_resource->extension_public->is_suspended = action_is_suspended;
921   surf_workstation_resource->extension_public->get_state = get_state;
922   surf_workstation_resource->extension_public->communicate = communicate_KCCFLN05;
923 }
924
925 /**************************************/
926 /*************** Generic **************/
927 /**************************************/
928
929 void surf_workstation_resource_init_KCCFLN05(const char *filename)
930 {
931   xbt_assert0(!surf_cpu_resource,"CPU resource type already defined");
932   cpu_KCCFLN05_resource_init_internal();
933   xbt_assert0(!surf_network_resource,"network resource type already defined");
934   network_KCCFLN05_resource_init_internal();
935   workstation_KCCFLN05_resource_init_internal();
936   parse_file(filename);
937
938   xbt_dynar_push(resource_list, &surf_network_resource);
939   xbt_dynar_push(resource_list, &surf_cpu_resource);
940   xbt_dynar_push(resource_list, &surf_workstation_resource);
941
942   if (maxmin_system) { /* To avoid using this useless system */
943     lmm_system_free(maxmin_system);
944     maxmin_system = NULL;
945   }
946 }