Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
It worries me however, to realize how tough an ass-hole I have had to be, in
[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   char *name = NULL;
83   double power_scale = 0.0;
84   double power_initial = 0.0;
85   tmgr_trace_t power_trace = NULL;
86   e_surf_cpu_state_t state_initial = SURF_CPU_OFF;
87   tmgr_trace_t state_trace = NULL;
88   double interference_send = 0.0;
89   double interference_recv = 0.0;
90   double interference_send_recv = 0.0;
91      
92   name = xbt_strdup(A_cpu_name);
93   surf_parse_get_double(&power_scale,A_cpu_power);
94   surf_parse_get_double(&power_initial,A_cpu_availability);
95   surf_parse_get_trace(&power_trace,A_cpu_availability_file);
96
97   xbt_assert0((A_cpu_state==A_cpu_state_ON)||
98               (A_cpu_state==A_cpu_state_OFF),
99               "Invalid state")
100   if (A_cpu_state==A_cpu_state_ON) state_initial = SURF_CPU_ON;
101   if (A_cpu_state==A_cpu_state_OFF) state_initial = SURF_CPU_OFF;
102   surf_parse_get_trace(&state_trace,A_cpu_state_file);
103
104   surf_parse_get_double(&interference_send,A_cpu_interference_send);
105   surf_parse_get_double(&interference_recv,A_cpu_interference_recv);
106   surf_parse_get_double(&interference_send_recv,A_cpu_interference_send_recv);
107
108   workstation_new(name, power_scale, power_initial, power_trace, state_initial,
109                   state_trace, interference_send, interference_recv,
110                   interference_send_recv);
111 }
112
113 /*********** resource management ***********/
114
115 static void *name_service(const char *name)
116 {
117   void *workstation = NULL;
118
119   xbt_dict_get(workstation_set, name, &workstation);
120
121   return workstation;
122 }
123
124 static const char *get_resource_name(void *resource_id)
125 {
126   return ((workstation_KCCFLN05_t) resource_id)->name;
127 }
128
129 static int cpu_used(void *resource_id)
130 {
131   return lmm_constraint_used(maxmin_system,
132                              ((workstation_KCCFLN05_t) resource_id)->constraint);
133 }
134
135 static int link_used(void *resource_id)
136 {
137   return lmm_constraint_used(maxmin_system_network_KCCFLN05,
138                              ((network_link_KCCFLN05_t) resource_id)->constraint);
139 }
140
141 static e_surf_cpu_state_t get_state(void *cpu)
142 {
143   return ((workstation_KCCFLN05_t) cpu)->state_current;
144 }
145
146 static void update_cpu_KCCFLN05_state(void *id,
147                                       tmgr_trace_event_t event_type,
148                                       double value)
149 {
150   workstation_KCCFLN05_t cpu = id;
151
152   if (event_type == cpu->power_event) {
153     cpu->power_current = value;
154     /*** The bound is updated in share_cpu_KCCFLN05_resources ***/
155     /*     lmm_update_constraint_bound(maxmin_system_cpu_KCCFLN05, cpu->constraint, */
156     /*                          cpu->power_current * cpu->power_scale); */
157   } else if (event_type == cpu->state_event) {
158     if (value > 0)
159       cpu->state_current = SURF_CPU_ON;
160     else
161       cpu->state_current = SURF_CPU_OFF;
162   } else {
163     CRITICAL0("Unknown event ! \n");
164     xbt_abort();
165   }
166
167   return;
168 }
169
170 /**************************************/
171 /*********** network object ***********/
172 /**************************************/
173
174 static void create_routing_table(void)
175 {
176   routing_table = xbt_new0(s_route_KCCFLN05_t, nb_workstation * nb_workstation);
177 }
178
179 static void network_link_free(void *nw_link)
180 {
181   xbt_free(((network_link_KCCFLN05_t)nw_link)->name);
182   xbt_free(nw_link);
183 }
184
185 static network_link_KCCFLN05_t network_link_new(char *name,
186                                                  double bw_initial,
187                                                  tmgr_trace_t bw_trace,
188                                                  e_surf_network_link_state_t
189                                                  state_initial,
190                                                  tmgr_trace_t state_trace)
191 {
192   network_link_KCCFLN05_t nw_link = xbt_new0(s_network_link_KCCFLN05_t, 1);
193
194
195   nw_link->resource = (surf_resource_t) surf_network_resource;
196   nw_link->name = name;
197   nw_link->bw_current = bw_initial;
198   if (bw_trace)
199     nw_link->bw_event =
200         tmgr_history_add_trace(history, bw_trace, 0.0, 0, nw_link);
201   nw_link->state_current = state_initial;
202   if (state_trace)
203     nw_link->state_event =
204         tmgr_history_add_trace(history, state_trace, 0.0, 0, nw_link);
205
206   nw_link->constraint =
207       lmm_constraint_new(maxmin_system_network_KCCFLN05, nw_link, nw_link->bw_current);
208
209   xbt_dict_set(network_link_set, name, nw_link, network_link_free);
210
211   return nw_link;
212 }
213
214 static void parse_network_link(void)
215 {
216   char *name;
217   double bw_initial;
218   tmgr_trace_t bw_trace;
219   e_surf_network_link_state_t state_initial = SURF_NETWORK_LINK_ON;
220   tmgr_trace_t state_trace;
221
222   name = xbt_strdup(A_network_link_name);
223   surf_parse_get_double(&bw_initial,A_network_link_bandwidth);
224   surf_parse_get_trace(&bw_trace, A_network_link_bandwidth_file);
225
226   xbt_assert0((A_network_link_state==A_network_link_state_ON)||
227               (A_network_link_state==A_network_link_state_OFF),
228               "Invalid state")
229   if (A_network_link_state==A_network_link_state_ON) 
230     state_initial = SURF_NETWORK_LINK_ON;
231   if (A_network_link_state==A_network_link_state_OFF) 
232     state_initial = SURF_NETWORK_LINK_OFF;
233   surf_parse_get_trace(&state_trace,A_network_link_state_file);
234
235   network_link_new(name, bw_initial, bw_trace, state_initial, state_trace);
236 }
237
238 static void route_new(int src_id, int dst_id, char **links, int nb_link,
239                       double impact_on_src, double impact_on_dst,
240                       double impact_on_src_with_other_recv,
241                       double impact_on_dst_with_other_send)
242 {
243   network_link_KCCFLN05_t *link_list = NULL;
244   int i;
245   route_KCCFLN05_t route = &(ROUTE(src_id,dst_id));
246
247   route->size= nb_link;
248   link_list = route->links = xbt_new0(network_link_KCCFLN05_t, nb_link);
249   for (i = 0; i < nb_link; i++) {
250     xbt_dict_get(network_link_set, links[i], (void *) &(link_list[i]));
251     xbt_free(links[i]);
252   }
253   xbt_free(links);
254   route->impact_on_src=impact_on_src;
255   route->impact_on_dst=impact_on_src;
256   route->impact_on_src_with_other_recv=impact_on_src_with_other_recv; 
257   route->impact_on_dst_with_other_send=impact_on_dst_with_other_send;
258 }
259
260 static int nb_link = 0;
261 static char **link_name = NULL;
262 static int src_id = -1;
263 static int dst_id = -1;
264 static double impact_on_src;
265 static double impact_on_dst;
266 static double impact_on_src_with_other_recv; 
267 static double impact_on_dst_with_other_send;
268
269 static void parse_route_set_endpoints(void)
270 {
271   src_id = ((workstation_KCCFLN05_t) name_service(A_route_src))->id;
272   dst_id = ((workstation_KCCFLN05_t) name_service(A_route_dst))->id;
273   nb_link = 0;
274   link_name = NULL;
275 }
276
277 static void parse_route_elem(void)
278 {
279   nb_link++;
280   link_name = xbt_realloc(link_name, (nb_link) * sizeof(char *));
281   link_name[(nb_link) - 1] = xbt_strdup(A_route_element_name);
282 }
283
284 static void parse_route_set_route(void)
285 {
286   route_new(src_id, dst_id, link_name, nb_link, impact_on_src, impact_on_dst,
287             impact_on_src_with_other_recv, impact_on_dst_with_other_send);
288 }
289
290 static void parse_file(const char *file)
291 {
292   /* Figuring out the workstations */
293   surf_parse_reset_parser();
294   ETag_cpu_fun=parse_workstation;
295   surf_parse_open(file);
296   xbt_assert1((!surf_parse()),"Parse error in %s",file);
297   surf_parse_close();
298
299   create_routing_table();
300
301   /* Figuring out the network links */
302   surf_parse_reset_parser();
303   ETag_network_link_fun=parse_network_link;
304   surf_parse_open(file);
305   xbt_assert1((!surf_parse()),"Parse error in %s",file);
306   surf_parse_close();
307
308   /* Building the routes */
309   surf_parse_reset_parser();
310   STag_route_fun=parse_route_set_endpoints;
311   ETag_route_element_fun=parse_route_elem;
312   ETag_route_fun=parse_route_set_route;
313   surf_parse_open(file);
314   xbt_assert1((!surf_parse()),"Parse error in %s",file);
315   surf_parse_close();
316 }
317
318 static void update_network_KCCFLN05_state(void *id,
319                                           tmgr_trace_event_t event_type,
320                                           double value)
321 {
322   network_link_KCCFLN05_t nw_link = id;
323
324   if (event_type == nw_link->bw_event) {
325     nw_link->bw_current = value;
326     lmm_update_constraint_bound(maxmin_system_network_KCCFLN05, nw_link->constraint,
327                                 nw_link->bw_current);
328   } else if (event_type == nw_link->state_event) {
329     if (value > 0)
330       nw_link->state_current = SURF_NETWORK_LINK_ON;
331     else
332       nw_link->state_current = SURF_NETWORK_LINK_OFF;
333   } else {
334     CRITICAL0("Unknown event ! \n");
335     xbt_abort();
336   }
337
338   return;
339 }
340
341 /**************************************/
342 /*************** actions **************/
343 /**************************************/
344 /*************** network **************/
345 static void action_network_KCCFLN05_free(surf_action_t action)
346 {
347   int cpt;
348   surf_action_t act = NULL;
349   workstation_KCCFLN05_t src = ((surf_action_network_KCCFLN05_t) action)->src;
350   workstation_KCCFLN05_t dst = ((surf_action_network_KCCFLN05_t) action)->dst;
351
352   xbt_swag_remove(action, action->state_set);
353   lmm_variable_free(maxmin_system_network_KCCFLN05, 
354                     ((surf_action_network_KCCFLN05_t) action)->variable);
355
356   xbt_dynar_foreach (src->outgoing_communications,cpt,act) {
357     if(act==action) {
358       xbt_dynar_remove_at(src->outgoing_communications, cpt, act);
359       break;
360     }
361   }
362
363   xbt_dynar_foreach (dst->incomming_communications,cpt,act) {
364     if(act==action) {
365       xbt_dynar_remove_at(dst->incomming_communications, cpt, act);
366       break;
367     }
368   }
369
370   xbt_free(action);
371 }
372
373 static double share_network_KCCFLN05_resources(double now)
374 {
375  s_surf_action_network_KCCFLN05_t action;
376  return generic_maxmin_share_resources2(surf_network_resource->common_public->
377                                         states.running_action_set,
378                                         xbt_swag_offset(action, variable),
379                                         maxmin_system_network_KCCFLN05);
380 }
381
382 static void action_network_KCCFLN05_change_state(surf_action_t action,
383                                                  e_surf_action_state_t state)
384 {
385   surf_action_change_state(action, state);
386   return;
387 }
388
389 static void update_actions_network_KCCFLN05_state(double now, double delta)
390 {
391   surf_action_network_KCCFLN05_t action = NULL;
392   surf_action_network_KCCFLN05_t next_action = NULL;
393   xbt_swag_t running_actions =
394       surf_network_resource->common_public->states.running_action_set;
395   xbt_swag_t failed_actions =
396       surf_network_resource->common_public->states.failed_action_set;
397
398   xbt_swag_foreach_safe(action, next_action, running_actions) {
399     action->generic_action.remains -=
400         lmm_variable_getvalue(action->variable) * delta;
401     if (action->generic_action.max_duration != NO_MAX_DURATION)
402       action->generic_action.max_duration -= delta;
403     if ((action->generic_action.remains <= 0) && 
404         (lmm_get_variable_weight(action->variable)>0)) {
405       action->generic_action.finish = surf_get_clock();
406       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
407     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
408                (action->generic_action.max_duration <= 0)) {
409       action->generic_action.finish = surf_get_clock();
410       action_network_KCCFLN05_change_state((surf_action_t) action, SURF_ACTION_DONE);
411     } else {                    /* Need to check that none of the resource has failed */
412       /* FIXME : FAILURES ARE NOT HANDLED YET */
413     }
414   }
415
416   xbt_swag_foreach_safe(action, next_action, failed_actions) {
417     lmm_variable_disable(maxmin_system_network_KCCFLN05, action->variable);
418   }
419
420   return;
421 }
422
423 static surf_action_t communicate_KCCFLN05(void *src, void *dst, double size)
424 {
425   surf_action_network_KCCFLN05_t action = NULL;
426   workstation_KCCFLN05_t card_src = src;
427   workstation_KCCFLN05_t card_dst = dst;
428   route_KCCFLN05_t route = &ROUTE(card_src->id, card_dst->id);
429   int i;
430
431   action = xbt_new0(s_surf_action_network_KCCFLN05_t, 1);
432
433   action->generic_action.cost = size;
434   action->generic_action.remains = size;
435   action->generic_action.max_duration = NO_MAX_DURATION;
436   action->generic_action.start = -1.0;
437   action->generic_action.finish = -1.0;
438   action->generic_action.resource_type =
439       (surf_resource_t) surf_network_resource;
440
441   action->generic_action.state_set =
442       surf_network_resource->common_public->states.running_action_set;
443
444   xbt_swag_insert(action, action->generic_action.state_set);
445
446   action->variable = lmm_variable_new(maxmin_system_network_KCCFLN05, action, 1.0, -1.0,
447                                       route->size);
448
449   for (i = 0; i < route->size; i++)
450     lmm_expand(maxmin_system_network_KCCFLN05, route->links[i]->constraint, 
451                action->variable, 1.0);
452
453   xbt_dynar_push(card_src->outgoing_communications,action);
454   xbt_dynar_push(card_dst->incomming_communications,action);
455
456   return (surf_action_t) action;
457 }
458
459 /***************** CPU ****************/
460 static void action_cpu_KCCFLN05_free(surf_action_t action)
461 {
462   xbt_swag_remove(action, action->state_set);
463   lmm_variable_free(maxmin_system_cpu_KCCFLN05, ((surf_action_cpu_KCCFLN05_t)action)->variable);
464   xbt_free(action);
465 }
466
467 static double share_cpu_KCCFLN05_resources(double now)
468 {
469   s_surf_action_cpu_KCCFLN05_t s_cpu_action;
470   lmm_constraint_t cnst = NULL;
471   workstation_KCCFLN05_t workstation = NULL;
472   double value = share_network_KCCFLN05_resources(now);
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, action, 1.0, -1.0, 1);
597   lmm_expand(maxmin_system, 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   lmm_update_variable_weight(maxmin_system,
606                              ((surf_action_cpu_KCCFLN05_t) action)->variable, 0.0);
607 }
608
609 static void cpu_KCCFLN05_action_resume(surf_action_t action)
610 {
611   lmm_update_variable_weight(maxmin_system,
612                              ((surf_action_cpu_KCCFLN05_t) action)->variable, 1.0);
613 }
614
615 static int cpu_KCCFLN05_action_is_suspended(surf_action_t action)
616 {
617   return (lmm_get_variable_weight(((surf_action_cpu_KCCFLN05_t) action)->variable) == 0.0);
618 }
619
620 /************* workstation ************/
621 static void action_change_state(surf_action_t action,
622                                 e_surf_action_state_t state)
623 {
624   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
625   surf_action_change_state(action, state);
626   return;
627 }
628
629 static void action_free(surf_action_t action)
630 {
631   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
632   return;
633 }
634
635 static int resource_used(void *resource_id)
636 {
637   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
638   return 0;
639 }
640
641 static double share_resources(double now)
642 {
643   return -1.0;
644 }
645
646 static void update_actions_state(double now, double delta)
647 {
648   return;
649 }
650
651 static void update_resource_state(void *id,
652                                   tmgr_trace_event_t event_type,
653                                   double value)
654 {
655   xbt_assert0(0, "Workstation is a virtual resource. I should not be there!");
656   return;
657 }
658
659 static void action_suspend(surf_action_t action)
660 {
661   xbt_assert0(action->resource_type ==
662               ((surf_resource_t) surf_cpu_resource),
663               "Resource type mismatch");
664   surf_cpu_resource->extension_public->suspend(action);
665 }
666
667 static void action_resume(surf_action_t action)
668 {
669   xbt_assert0(action->resource_type ==
670               ((surf_resource_t) surf_cpu_resource),
671               "Resource type mismatch");
672   surf_cpu_resource->extension_public->resume(action);
673 }
674
675 static int action_is_suspended(surf_action_t action)
676 {
677   if(action->resource_type==(surf_resource_t)surf_network_resource) 
678     return 0;
679   if(action->resource_type==(surf_resource_t)surf_cpu_resource) 
680     return surf_cpu_resource->extension_public->is_suspended(action);
681   DIE_IMPOSSIBLE;
682 }
683
684
685 /**************************************/
686 /********* Module  creation ***********/
687 /**************************************/
688 static void cpu_KCCFLN05_finalize(void)
689 {
690   xbt_dict_free(&workstation_set);
691   xbt_swag_free(surf_cpu_resource->common_public->states.ready_action_set);
692   xbt_swag_free(surf_cpu_resource->common_public->states.
693                 running_action_set);
694   xbt_swag_free(surf_cpu_resource->common_public->states.
695                 failed_action_set);
696   xbt_swag_free(surf_cpu_resource->common_public->states.done_action_set);
697   xbt_free(surf_cpu_resource->common_public);
698   xbt_free(surf_cpu_resource->common_private);
699   xbt_free(surf_cpu_resource->extension_public);
700
701   xbt_free(surf_cpu_resource);
702   surf_cpu_resource = NULL;
703
704   if (maxmin_system_cpu_KCCFLN05) {
705     lmm_system_free(maxmin_system_cpu_KCCFLN05);
706     maxmin_system_cpu_KCCFLN05 = NULL;
707   }
708 }
709
710 static void cpu_KCCFLN05_resource_init_internal(void)
711 {
712   s_surf_action_t action;
713
714   surf_cpu_resource = xbt_new0(s_surf_cpu_resource_t, 1);
715
716   surf_cpu_resource->common_private =
717       xbt_new0(s_surf_resource_private_t, 1);
718   surf_cpu_resource->common_public = 
719     xbt_new0(s_surf_resource_public_t, 1);
720
721   surf_cpu_resource->extension_public =
722       xbt_new0(s_surf_cpu_resource_extension_public_t, 1);
723
724   surf_cpu_resource->common_public->states.ready_action_set =
725       xbt_swag_new(xbt_swag_offset(action, state_hookup));
726   surf_cpu_resource->common_public->states.running_action_set =
727       xbt_swag_new(xbt_swag_offset(action, state_hookup));
728   surf_cpu_resource->common_public->states.failed_action_set =
729       xbt_swag_new(xbt_swag_offset(action, state_hookup));
730   surf_cpu_resource->common_public->states.done_action_set =
731       xbt_swag_new(xbt_swag_offset(action, state_hookup));
732
733   surf_cpu_resource->common_public->name_service = name_service;
734   surf_cpu_resource->common_public->get_resource_name = get_resource_name; 
735   surf_cpu_resource->common_public->action_get_state =
736       surf_action_get_state;
737   surf_cpu_resource->common_public->action_free = action_cpu_KCCFLN05_free;
738   surf_cpu_resource->common_public->action_cancel = NULL;
739   surf_cpu_resource->common_public->action_recycle = NULL;
740   surf_cpu_resource->common_public->action_change_state = surf_action_change_state;
741   surf_cpu_resource->common_public->action_set_data = surf_action_set_data;
742   surf_cpu_resource->common_public->name = "CPU KCCFLN05";
743
744   surf_cpu_resource->common_private->resource_used = cpu_used;
745   surf_cpu_resource->common_private->share_resources = share_cpu_KCCFLN05_resources;
746   surf_cpu_resource->common_private->update_actions_state =
747     update_actions_cpu_KCCFLN05_state;
748   surf_cpu_resource->common_private->update_resource_state =
749       update_cpu_KCCFLN05_state;
750   surf_cpu_resource->common_private->finalize = cpu_KCCFLN05_finalize;
751
752   surf_cpu_resource->extension_public->execute = execute_KCCFLN05;
753 /*FIXME*//*   surf_cpu_resource->extension_public->sleep = action_sleep; */
754   surf_cpu_resource->extension_public->suspend = cpu_KCCFLN05_action_suspend;
755   surf_cpu_resource->extension_public->resume = cpu_KCCFLN05_action_resume;
756   surf_cpu_resource->extension_public->is_suspended = cpu_KCCFLN05_action_is_suspended;
757
758   surf_cpu_resource->extension_public->get_state = get_state;
759
760   workstation_set = xbt_dict_new();
761
762   maxmin_system_cpu_KCCFLN05 = lmm_system_new();
763 }
764
765 static void network_KCCFLN05_finalize(void)
766 {
767   int i,j;
768
769   xbt_dict_free(&network_link_set);
770   xbt_swag_free(surf_network_resource->common_public->states.
771                 ready_action_set);
772   xbt_swag_free(surf_network_resource->common_public->states.
773                 running_action_set);
774   xbt_swag_free(surf_network_resource->common_public->states.
775                 failed_action_set);
776   xbt_swag_free(surf_network_resource->common_public->states.
777                 done_action_set);
778   xbt_free(surf_network_resource->common_public);
779   xbt_free(surf_network_resource->common_private);
780   xbt_free(surf_network_resource->extension_public);
781
782   xbt_free(surf_network_resource);
783   surf_network_resource = NULL;
784
785   for (i = 0; i < nb_workstation; i++) 
786     for (j = 0; j < nb_workstation; j++) 
787       xbt_free(ROUTE(i,j).links);
788   xbt_free(routing_table);
789   routing_table = NULL;
790   nb_workstation = 0;
791
792   if (maxmin_system_network_KCCFLN05) {
793     lmm_system_free(maxmin_system_network_KCCFLN05);
794     maxmin_system_network_KCCFLN05 = NULL;
795   }
796 }
797
798 static void network_KCCFLN05_resource_init_internal(void)
799 {
800   s_surf_action_t action;
801
802   surf_network_resource = xbt_new0(s_surf_network_resource_t, 1);
803
804   surf_network_resource->common_private =
805       xbt_new0(s_surf_resource_private_t, 1);
806   surf_network_resource->common_public =
807       xbt_new0(s_surf_resource_public_t, 1);
808   surf_network_resource->extension_public =
809       xbt_new0(s_surf_network_resource_extension_public_t, 1);
810
811   surf_network_resource->common_public->states.ready_action_set =
812       xbt_swag_new(xbt_swag_offset(action, state_hookup));
813   surf_network_resource->common_public->states.running_action_set =
814       xbt_swag_new(xbt_swag_offset(action, state_hookup));
815   surf_network_resource->common_public->states.failed_action_set =
816       xbt_swag_new(xbt_swag_offset(action, state_hookup));
817   surf_network_resource->common_public->states.done_action_set =
818       xbt_swag_new(xbt_swag_offset(action, state_hookup));
819
820   surf_network_resource->common_public->name_service = name_service;
821   surf_network_resource->common_public->get_resource_name = get_resource_name;
822   surf_network_resource->common_public->action_get_state =
823       surf_action_get_state;
824   surf_network_resource->common_public->action_free = action_network_KCCFLN05_free;
825   surf_network_resource->common_public->action_cancel = NULL;
826   surf_network_resource->common_public->action_recycle = NULL;
827   surf_network_resource->common_public->action_change_state = action_network_KCCFLN05_change_state;
828   surf_network_resource->common_public->action_set_data = surf_action_set_data;
829   surf_network_resource->common_public->name = "network KCCFLN05";
830
831   surf_network_resource->common_private->resource_used = link_used;
832   surf_network_resource->common_private->share_resources = share_network_KCCFLN05_resources;
833   surf_network_resource->common_private->update_actions_state =
834     update_actions_network_KCCFLN05_state;
835   surf_network_resource->common_private->update_resource_state =
836     update_network_KCCFLN05_state;
837   surf_network_resource->common_private->finalize = network_KCCFLN05_finalize;
838
839  surf_network_resource->extension_public->communicate = communicate_KCCFLN05;
840
841   network_link_set = xbt_dict_new();
842
843
844   maxmin_system_network_KCCFLN05 = lmm_system_new();
845 }
846
847 static void workstation_KCCFLN05_finalize(void)
848 {
849   xbt_swag_free(surf_workstation_resource->common_public->states.ready_action_set);
850   xbt_swag_free(surf_workstation_resource->common_public->states.running_action_set);
851   xbt_swag_free(surf_workstation_resource->common_public->states.failed_action_set);
852   xbt_swag_free(surf_workstation_resource->common_public->states.done_action_set);
853
854   xbt_free(surf_workstation_resource->common_public);
855   xbt_free(surf_workstation_resource->common_private);
856   xbt_free(surf_workstation_resource->extension_public);
857
858   xbt_free(surf_workstation_resource);
859   surf_workstation_resource = NULL;
860 }
861
862 static void workstation_KCCFLN05_resource_init_internal(void)
863 {
864   s_surf_action_t action;
865
866   surf_workstation_resource = xbt_new0(s_surf_workstation_resource_t, 1);
867
868   surf_workstation_resource->common_private =
869       xbt_new0(s_surf_resource_private_t, 1);
870   surf_workstation_resource->common_public =
871       xbt_new0(s_surf_resource_public_t, 1);
872   surf_workstation_resource->extension_public =
873       xbt_new0(s_surf_workstation_resource_extension_public_t, 1);
874
875   surf_workstation_resource->common_public->states.ready_action_set =
876       xbt_swag_new(xbt_swag_offset(action, state_hookup));
877   surf_workstation_resource->common_public->states.running_action_set =
878       xbt_swag_new(xbt_swag_offset(action, state_hookup));
879   surf_workstation_resource->common_public->states.failed_action_set =
880       xbt_swag_new(xbt_swag_offset(action, state_hookup));
881   surf_workstation_resource->common_public->states.done_action_set =
882       xbt_swag_new(xbt_swag_offset(action, state_hookup));
883
884   surf_workstation_resource->common_public->name_service = name_service;
885   surf_workstation_resource->common_public->get_resource_name =
886       get_resource_name;
887   surf_workstation_resource->common_public->action_get_state =
888       surf_action_get_state;
889   surf_workstation_resource->common_public->action_free = action_free;
890   surf_workstation_resource->common_public->action_cancel = NULL;
891   surf_workstation_resource->common_public->action_recycle = NULL;
892   surf_workstation_resource->common_public->action_change_state = action_change_state;
893   surf_workstation_resource->common_public->action_set_data = surf_action_set_data;
894   surf_workstation_resource->common_public->name = "Workstation KCCFLN05";
895
896   surf_workstation_resource->common_private->resource_used = resource_used;
897   surf_workstation_resource->common_private->share_resources = share_resources;
898   surf_workstation_resource->common_private->update_actions_state = update_actions_state;
899   surf_workstation_resource->common_private->update_resource_state = update_resource_state;
900   surf_workstation_resource->common_private->finalize = workstation_KCCFLN05_finalize;
901
902   surf_workstation_resource->extension_public->execute = execute_KCCFLN05;
903 /*FIXME*//*  surf_workstation_resource->extension_public->sleep = action_sleep; */
904   surf_workstation_resource->extension_public->suspend = action_suspend;
905   surf_workstation_resource->extension_public->resume = action_resume;
906   surf_workstation_resource->extension_public->is_suspended = action_is_suspended;
907   surf_workstation_resource->extension_public->get_state = get_state;
908   surf_workstation_resource->extension_public->communicate = communicate_KCCFLN05;
909 }
910
911 /**************************************/
912 /*************** Generic **************/
913 /**************************************/
914
915 void surf_workstation_resource_init_KCCFLN05(const char *filename)
916 {
917   xbt_assert0(!surf_cpu_resource,"CPU resource type already defined");
918   cpu_KCCFLN05_resource_init_internal();
919   xbt_assert0(!surf_network_resource,"network resource type already defined");
920   network_KCCFLN05_resource_init_internal();
921   workstation_KCCFLN05_resource_init_internal();
922   parse_file(filename);
923
924   xbt_dynar_push(resource_list, &surf_network_resource);
925   xbt_dynar_push(resource_list, &surf_cpu_resource);
926   xbt_dynar_push(resource_list, &surf_workstation_resource);
927 }