Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Parsing values is good. Taking into account parsed values is better...
[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 {
428   surf_action_network_KCCFLN05_t action = NULL;
429   workstation_KCCFLN05_t card_src = src;
430   workstation_KCCFLN05_t card_dst = dst;
431   route_KCCFLN05_t route = &ROUTE(card_src->id, card_dst->id);
432   int i;
433
434   action = xbt_new0(s_surf_action_network_KCCFLN05_t, 1);
435
436   action->generic_action.cost = size;
437   action->generic_action.remains = size;
438   action->generic_action.max_duration = NO_MAX_DURATION;
439   action->generic_action.start = -1.0;
440   action->generic_action.finish = -1.0;
441   action->generic_action.resource_type =
442       (surf_resource_t) surf_network_resource;
443
444   action->generic_action.state_set =
445       surf_network_resource->common_public->states.running_action_set;
446
447   xbt_swag_insert(action, action->generic_action.state_set);
448
449   action->variable = lmm_variable_new(maxmin_system_network_KCCFLN05, action, 1.0, -1.0,
450                                       route->size);
451
452   for (i = 0; i < route->size; i++)
453     lmm_expand(maxmin_system_network_KCCFLN05, route->links[i]->constraint, 
454                action->variable, 1.0);
455
456   action->src=src;
457   action->dst=dst;
458
459   xbt_dynar_push(card_src->outgoing_communications,&action);
460   xbt_dynar_push(card_dst->incomming_communications,&action);
461
462   return (surf_action_t) action;
463 }
464
465 /***************** CPU ****************/
466 static void action_cpu_KCCFLN05_free(surf_action_t action)
467 {
468   xbt_swag_remove(action, action->state_set);
469   lmm_variable_free(maxmin_system_cpu_KCCFLN05, ((surf_action_cpu_KCCFLN05_t)action)->variable);
470   xbt_free(action);
471 }
472
473 /* #define WARNING(format, ...) (fprintf(stderr, "[%s , %s : %d] ", __FILE__, __FUNCTION__, __LINE__),\ */
474 /*                               fprintf(stderr, format, ## __VA_ARGS__), \ */
475 /*                               fprintf(stderr, "\n")) */
476 /* #define VOIRP(expr) WARNING("  {" #expr " = %p }", expr) */
477 /* #define VOIRD(expr) WARNING("  {" #expr " = %d }", expr) */
478 /* #define VOIRG(expr) WARNING("  {" #expr " = %lg }", expr) */
479
480 static double share_cpu_KCCFLN05_resources(double now)
481 {
482   s_surf_action_cpu_KCCFLN05_t s_cpu_action;
483   lmm_constraint_t cnst = NULL;
484   workstation_KCCFLN05_t workstation = NULL;
485   double W=0.0;
486   double scale=0.0;
487   int cpt;
488   surf_action_network_KCCFLN05_t action;
489
490   for(cnst = lmm_get_first_active_constraint(maxmin_system_cpu_KCCFLN05);
491       cnst;
492       cnst= lmm_get_next_active_constraint(maxmin_system_cpu_KCCFLN05, cnst))
493     {
494       workstation = lmm_constraint_id(cnst);
495       W=workstation->power_current * workstation->power_scale;
496       
497       if((!xbt_dynar_length(workstation->incomming_communications)) &&
498          (!xbt_dynar_length(workstation->outgoing_communications))) {
499         scale = 1.0;
500       } else if((!xbt_dynar_length(workstation->incomming_communications)) &&
501                 (xbt_dynar_length(workstation->outgoing_communications))) {
502         scale = workstation->interference_send;
503         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
504 /*        VOIRD(action->src->id); */
505 /*        VOIRD(action->dst->id); */
506 /*        VOIRP(&ROUTE(action->src->id,action->dst->id)); */
507 /*        VOIRG(ROUTE(action->src->id,action->dst->id).impact_on_src); */
508           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src *
509             lmm_variable_getvalue(action->variable);
510         }
511         xbt_assert0(scale>0.0,"Negative interference !");
512       } else if((xbt_dynar_length(workstation->incomming_communications)) &&
513                 (!xbt_dynar_length(workstation->outgoing_communications))) {
514         scale = workstation->interference_recv;
515         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
516           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst *
517             lmm_variable_getvalue(action->variable);
518         }
519         xbt_assert0(scale>0.0,"Negative interference !");
520       } else {
521         scale = workstation->interference_send_recv;
522         xbt_dynar_foreach (workstation->outgoing_communications,cpt,action) {
523           scale -= ROUTE(action->src->id,action->dst->id).impact_on_src_with_other_recv *
524             lmm_variable_getvalue(action->variable);
525         }
526         xbt_dynar_foreach (workstation->incomming_communications,cpt,action) {
527           scale -= ROUTE(action->src->id,action->dst->id).impact_on_dst_with_other_send *
528             lmm_variable_getvalue(action->variable);
529         }
530         xbt_assert0(scale>0.0,"Negative interference !");
531       }
532       lmm_update_constraint_bound(maxmin_system_cpu_KCCFLN05,workstation->constraint,
533                                   W*scale);
534     }
535
536   return generic_maxmin_share_resources2(surf_cpu_resource->common_public->
537                                          states.running_action_set,
538                                          xbt_swag_offset(s_cpu_action, variable),
539                                          maxmin_system_cpu_KCCFLN05);
540 }
541
542 static void update_actions_cpu_KCCFLN05_state(double now, double delta)
543 {
544   surf_action_cpu_KCCFLN05_t action = NULL;
545   surf_action_cpu_KCCFLN05_t next_action = NULL;
546   xbt_swag_t running_actions =
547       surf_cpu_resource->common_public->states.running_action_set;
548   xbt_swag_t failed_actions =
549       surf_cpu_resource->common_public->states.failed_action_set;
550
551   xbt_swag_foreach_safe(action, next_action, running_actions) {
552     action->generic_action.remains -=
553         lmm_variable_getvalue(action->variable) * delta;
554     if (action->generic_action.max_duration != NO_MAX_DURATION)
555       action->generic_action.max_duration -= delta;
556     if ((action->generic_action.remains <= 0) && 
557         (lmm_get_variable_weight(action->variable)>0)) {
558       action->generic_action.finish = surf_get_clock();
559       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
560     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
561                (action->generic_action.max_duration <= 0)) {
562       action->generic_action.finish = surf_get_clock();
563       surf_action_change_state((surf_action_t) action, SURF_ACTION_DONE);
564     } else {                    /* Need to check that none of the resource has failed */
565       lmm_constraint_t cnst = NULL;
566       int i = 0;
567       workstation_KCCFLN05_t cpu = NULL;
568
569       while ((cnst =
570               lmm_get_cnst_from_var(maxmin_system_cpu_KCCFLN05, action->variable,
571                                     i++))) {
572         cpu = lmm_constraint_id(cnst);
573         if (cpu->state_current == SURF_CPU_OFF) {
574           action->generic_action.finish = surf_get_clock();
575           surf_action_change_state((surf_action_t) action, SURF_ACTION_FAILED);
576           break;
577         }
578       }
579     }
580   }
581
582   xbt_swag_foreach_safe(action, next_action, failed_actions) {
583     lmm_variable_disable(maxmin_system_cpu_KCCFLN05, action->variable);
584   }
585
586   return;
587 }
588
589 static surf_action_t execute_KCCFLN05(void *cpu, double size)
590 {
591   surf_action_cpu_KCCFLN05_t action = NULL;
592   workstation_KCCFLN05_t CPU = cpu;
593
594   action = xbt_new0(s_surf_action_cpu_KCCFLN05_t, 1);
595
596   action->generic_action.cost = size;
597   action->generic_action.remains = size;
598   action->generic_action.max_duration = NO_MAX_DURATION;
599   action->generic_action.start = surf_get_clock();
600   action->generic_action.finish = -1.0;
601   action->generic_action.resource_type =
602       (surf_resource_t) surf_cpu_resource;
603
604   if (CPU->state_current == SURF_CPU_ON)
605     action->generic_action.state_set =
606         surf_cpu_resource->common_public->states.running_action_set;
607   else
608     action->generic_action.state_set =
609         surf_cpu_resource->common_public->states.failed_action_set;
610   xbt_swag_insert(action, action->generic_action.state_set);
611
612   action->variable = lmm_variable_new(maxmin_system_cpu_KCCFLN05, action, 1.0, -1.0, 1);
613   lmm_expand(maxmin_system_cpu_KCCFLN05, CPU->constraint, action->variable,
614              1.0);
615
616   return (surf_action_t) action;
617 }
618
619 static void cpu_KCCFLN05_action_suspend(surf_action_t action)
620 {
621   if(action->resource_type==(surf_resource_t)surf_network_resource) 
622     lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
623                                ((surf_action_network_KCCFLN05_t) action)->variable, 0.0);
624   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
625     lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
626                                ((surf_action_cpu_KCCFLN05_t) action)->variable, 0.0);
627   else DIE_IMPOSSIBLE;
628 }
629
630 static void cpu_KCCFLN05_action_resume(surf_action_t action)
631 {
632   if(action->resource_type==(surf_resource_t)surf_network_resource) 
633     lmm_update_variable_weight(maxmin_system_network_KCCFLN05,
634                                ((surf_action_network_KCCFLN05_t) action)->variable, 1.0);
635   else if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
636     lmm_update_variable_weight(maxmin_system_cpu_KCCFLN05,
637                                ((surf_action_cpu_KCCFLN05_t) action)->variable, 1.0);
638   else DIE_IMPOSSIBLE;
639 }
640
641 static int cpu_KCCFLN05_action_is_suspended(surf_action_t action)
642 {
643   if(action->resource_type==(surf_resource_t)surf_network_resource) 
644     return (lmm_get_variable_weight(((surf_action_cpu_KCCFLN05_t) action)->variable) == 0.0);
645   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
646     return (lmm_get_variable_weight(((surf_action_network_KCCFLN05_t) action)->variable) == 0.0);
647   DIE_IMPOSSIBLE;
648 }
649
650 /************* workstation ************/
651 static void action_change_state(surf_action_t action,
652                                 e_surf_action_state_t state)
653 {
654   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
655   surf_action_change_state(action, state);
656   return;
657 }
658
659 static void action_free(surf_action_t action)
660 {
661   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
662   return;
663 }
664
665 static int resource_used(void *resource_id)
666 {
667   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
668   return 0;
669 }
670
671 static double share_resources(double now)
672 {
673   return -1.0;
674 }
675
676 static void update_actions_state(double now, double delta)
677 {
678   return;
679 }
680
681 static void update_resource_state(void *id,
682                                   tmgr_trace_event_t event_type,
683                                   double value)
684 {
685   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
686   return;
687 }
688
689 static void action_suspend(surf_action_t action)
690 {
691   xbt_assert0(action->resource_type ==
692               ((surf_resource_t) surf_cpu_resource),
693               "Resource type mismatch");
694   surf_cpu_resource->extension_public->suspend(action);
695 }
696
697 static void action_resume(surf_action_t action)
698 {
699   xbt_assert0(action->resource_type ==
700               ((surf_resource_t) surf_cpu_resource),
701               "Resource type mismatch");
702   surf_cpu_resource->extension_public->resume(action);
703 }
704
705 static int action_is_suspended(surf_action_t action)
706 {
707   if(action->resource_type==(surf_resource_t)surf_network_resource) 
708     return 0;
709   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
710     return surf_cpu_resource->extension_public->is_suspended(action);
711   DIE_IMPOSSIBLE;
712 }
713
714
715 /**************************************/
716 /********* Module  creation ***********/
717 /**************************************/
718 static void cpu_KCCFLN05_finalize(void)
719 {
720   xbt_dict_free(&workstation_set);
721   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
722   xbt_swag_free(surf_cpu_resource->common_public->states.
723                 running_action_set);
724   xbt_swag_free(surf_cpu_resource->common_public->states.
725                 failed_action_set);
726   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
727   xbt_free(surf_cpu_resource->common_public);
728   xbt_free(surf_cpu_resource->common_private);
729   xbt_free(surf_cpu_resource->extension_public);
730
731   xbt_free(surf_cpu_resource);
732   surf_cpu_resource = NULL;
733
734   if (maxmin_system_cpu_KCCFLN05) {
735     lmm_system_free(maxmin_system_cpu_KCCFLN05);
736     maxmin_system_cpu_KCCFLN05 = NULL;
737   }
738 }
739
740 static void cpu_KCCFLN05_resource_init_internal(void)
741 {
742   s_surf_action_t action;
743
744   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
745
746   surf_cpu_resource->common_private =
747       xbt_new0(s_surf_resource_private_t, 1);
748   surf_cpu_resource->common_public = 
749     xbt_new0(s_surf_resource_public_t, 1);
750
751   surf_cpu_resource->extension_public =
752       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
753
754   surf_cpu_resource->common_public->states.ready_action_set =
755       xbt_swag_new(xbt_swag_offset(action, state_hookup));
756   surf_cpu_resource->common_public->states.running_action_set =
757       xbt_swag_new(xbt_swag_offset(action, state_hookup));
758   surf_cpu_resource->common_public->states.failed_action_set =
759       xbt_swag_new(xbt_swag_offset(action, state_hookup));
760   surf_cpu_resource->common_public->states.done_action_set =
761       xbt_swag_new(xbt_swag_offset(action, state_hookup));
762
763   surf_cpu_resource->common_public->name_service = name_service;
764   surf_cpu_resource->common_public->get_resource_name = get_resource_name; 
765   surf_cpu_resource->common_public->action_get_state =
766       surf_action_get_state;
767   surf_cpu_resource->common_public->action_free = action_cpu_KCCFLN05_free;
768   surf_cpu_resource->common_public->action_cancel = NULL;
769   surf_cpu_resource->common_public->action_recycle = NULL;
770   surf_cpu_resource->common_public->action_change_state = surf_action_change_state;
771   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
772   surf_cpu_resource->common_public->name = "CPU KCCFLN05";
773
774   surf_cpu_resource->common_private->resource_used = cpu_used;
775   surf_cpu_resource->common_private->share_resources = share_cpu_KCCFLN05_resources;
776   surf_cpu_resource->common_private->update_actions_state =
777     update_actions_cpu_KCCFLN05_state;
778   surf_cpu_resource->common_private->update_resource_state =
779       update_cpu_KCCFLN05_state;
780   surf_cpu_resource->common_private->finalize = cpu_KCCFLN05_finalize;
781
782   surf_cpu_resource->extension_public->execute = execute_KCCFLN05;
783 /*FIXME*//*   surf_cpu_resource->extension_public->sleep = action_sleep; */
784   surf_cpu_resource->extension_public->suspend = cpu_KCCFLN05_action_suspend;
785   surf_cpu_resource->extension_public->resume = cpu_KCCFLN05_action_resume;
786   surf_cpu_resource->extension_public->is_suspended = cpu_KCCFLN05_action_is_suspended;
787
788   surf_cpu_resource->extension_public->get_state = get_state;
789
790   workstation_set = xbt_dict_new();
791
792   maxmin_system_cpu_KCCFLN05 = lmm_system_new();
793 }
794
795 static void network_KCCFLN05_finalize(void)
796 {
797   int i,j;
798
799   xbt_dict_free(&network_link_set);
800   xbt_swag_free(surf_network_resource->common_public->states.
801                 ready_action_set);
802   xbt_swag_free(surf_network_resource->common_public->states.
803                 running_action_set);
804   xbt_swag_free(surf_network_resource->common_public->states.
805                 failed_action_set);
806   xbt_swag_free(surf_network_resource->common_public->states.
807                 done_action_set);
808   xbt_free(surf_network_resource->common_public);
809   xbt_free(surf_network_resource->common_private);
810   xbt_free(surf_network_resource->extension_public);
811
812   xbt_free(surf_network_resource);
813   surf_network_resource = NULL;
814
815   for (i = 0; i < nb_workstation; i++) 
816     for (j = 0; j < nb_workstation; j++) 
817       xbt_free(ROUTE(i,j).links);
818   xbt_free(routing_table);
819   routing_table = NULL;
820   nb_workstation = 0;
821
822   if (maxmin_system_network_KCCFLN05) {
823     lmm_system_free(maxmin_system_network_KCCFLN05);
824     maxmin_system_network_KCCFLN05 = NULL;
825   }
826 }
827
828 static void network_KCCFLN05_resource_init_internal(void)
829 {
830   s_surf_action_t action;
831
832   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
833
834   surf_network_resource->common_private =
835       xbt_new0(s_surf_resource_private_t, 1);
836   surf_network_resource->common_public =
837       xbt_new0(s_surf_resource_public_t, 1);
838   surf_network_resource->extension_public =
839       xbt_new0(s_surf_network_resource_extension_public_t, 1);
840
841   surf_network_resource->common_public->states.ready_action_set =
842       xbt_swag_new(xbt_swag_offset(action, state_hookup));
843   surf_network_resource->common_public->states.running_action_set =
844       xbt_swag_new(xbt_swag_offset(action, state_hookup));
845   surf_network_resource->common_public->states.failed_action_set =
846       xbt_swag_new(xbt_swag_offset(action, state_hookup));
847   surf_network_resource->common_public->states.done_action_set =
848       xbt_swag_new(xbt_swag_offset(action, state_hookup));
849
850   surf_network_resource->common_public->name_service = name_service;
851   surf_network_resource->common_public->get_resource_name = get_resource_name;
852   surf_network_resource->common_public->action_get_state =
853       surf_action_get_state;
854   surf_network_resource->common_public->action_free = action_network_KCCFLN05_free;
855   surf_network_resource->common_public->action_cancel = NULL;
856   surf_network_resource->common_public->action_recycle = NULL;
857   surf_network_resource->common_public->action_change_state = action_network_KCCFLN05_change_state;
858   surf_network_resource->common_public->action_set_data = surf_action_set_data;
859   surf_network_resource->common_public->name = "network KCCFLN05";
860
861   surf_network_resource->common_private->resource_used = link_used;
862   surf_network_resource->common_private->share_resources = share_network_KCCFLN05_resources;
863   surf_network_resource->common_private->update_actions_state =
864     update_actions_network_KCCFLN05_state;
865   surf_network_resource->common_private->update_resource_state =
866     update_network_KCCFLN05_state;
867   surf_network_resource->common_private->finalize = network_KCCFLN05_finalize;
868
869  surf_network_resource->extension_public->communicate = communicate_KCCFLN05;
870
871   network_link_set = xbt_dict_new();
872
873
874   maxmin_system_network_KCCFLN05 = lmm_system_new();
875 }
876
877 static void workstation_KCCFLN05_finalize(void)
878 {
879   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
880   xbt_swag_free(surf_workstation_resource->common_public->states.running_action_set);
881   xbt_swag_free(surf_workstation_resource->common_public->states.failed_action_set);
882   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
883
884   xbt_free(surf_workstation_resource->common_public);
885   xbt_free(surf_workstation_resource->common_private);
886   xbt_free(surf_workstation_resource->extension_public);
887
888   xbt_free(surf_workstation_resource);
889   surf_workstation_resource = NULL;
890 }
891
892 static void workstation_KCCFLN05_resource_init_internal(void)
893 {
894   s_surf_action_t action;
895
896   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
897
898   surf_workstation_resource->common_private =
899       xbt_new0(s_surf_resource_private_t, 1);
900   surf_workstation_resource->common_public =
901       xbt_new0(s_surf_resource_public_t, 1);
902   surf_workstation_resource->extension_public =
903       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
904
905   surf_workstation_resource->common_public->states.ready_action_set =
906       xbt_swag_new(xbt_swag_offset(action, state_hookup));
907   surf_workstation_resource->common_public->states.running_action_set =
908       xbt_swag_new(xbt_swag_offset(action, state_hookup));
909   surf_workstation_resource->common_public->states.failed_action_set =
910       xbt_swag_new(xbt_swag_offset(action, state_hookup));
911   surf_workstation_resource->common_public->states.done_action_set =
912       xbt_swag_new(xbt_swag_offset(action, state_hookup));
913
914   surf_workstation_resource->common_public->name_service = name_service;
915   surf_workstation_resource->common_public->get_resource_name =
916       get_resource_name;
917   surf_workstation_resource->common_public->action_get_state =
918       surf_action_get_state;
919   surf_workstation_resource->common_public->action_free = action_free;
920   surf_workstation_resource->common_public->action_cancel = NULL;
921   surf_workstation_resource->common_public->action_recycle = NULL;
922   surf_workstation_resource->common_public->action_change_state = action_change_state;
923   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
924   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
925
926   surf_workstation_resource->common_private->resource_used = resource_used;
927   surf_workstation_resource->common_private->share_resources = share_resources;
928   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
929   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
930   surf_workstation_resource->common_private->finalize = workstation_KCCFLN05_finalize;
931
932   surf_workstation_resource->extension_public->execute = execute_KCCFLN05;
933 /*FIXME*//*  surf_workstation_resource->extension_public->sleep = action_sleep; */
934   surf_workstation_resource->extension_public->suspend = action_suspend;
935   surf_workstation_resource->extension_public->resume = action_resume;
936   surf_workstation_resource->extension_public->is_suspended = action_is_suspended;
937   surf_workstation_resource->extension_public->get_state = get_state;
938   surf_workstation_resource->extension_public->communicate = communicate_KCCFLN05;
939 }
940
941 /**************************************/
942 /*************** Generic **************/
943 /**************************************/
944
945 void surf_workstation_resource_init_KCCFLN05(const char *filename)
946 {
947   xbt_assert0(!surf_cpu_resource,"CPU resource type already defined");
948   cpu_KCCFLN05_resource_init_internal();
949   xbt_assert0(!surf_network_resource,"network resource type already defined");
950   network_KCCFLN05_resource_init_internal();
951   workstation_KCCFLN05_resource_init_internal();
952   parse_file(filename);
953
954   xbt_dynar_push(resource_list, &surf_network_resource);
955   xbt_dynar_push(resource_list, &surf_cpu_resource);
956   xbt_dynar_push(resource_list, &surf_workstation_resource);
957
958   if (maxmin_system) { /* To avoid using this useless system */
959     lmm_system_free(maxmin_system);
960     maxmin_system = NULL;
961   }
962 }