Logo AND Algorithmique Numérique Distribuée

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