Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't hide variables with multiple declarations.
[simgrid.git] / src / surf / cpu_cas01.c
1 /* Copyright (c) 2009-2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_private.h"
8 #include "surf/surf_resource.h"
9
10 surf_model_t surf_cpu_model = NULL;
11 lmm_system_t cpu_maxmin_system = NULL;
12 e_UM_t cpu_update_mechanism = UM_UNDEFINED;
13 static int cpu_selective_update = 0;
14
15 static xbt_swag_t cpu_modified_cpu = NULL;
16 static xbt_heap_t cpu_action_heap = NULL;
17
18 #undef GENERIC_LMM_ACTION
19 #undef GENERIC_ACTION
20 #undef ACTION_GET_CPU
21 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
22 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
23 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_t) action)->cpu
24
25 typedef struct surf_action_cpu_cas01 {
26   s_surf_action_lmm_t generic_lmm_action;
27   s_xbt_swag_hookup_t cpu_list_hookup;
28   int index_heap;
29   void *cpu;
30 } s_surf_action_cpu_Cas01_t, *surf_action_cpu_Cas01_t;
31
32 typedef struct cpu_Cas01 {
33   s_surf_resource_t generic_resource;
34   s_xbt_swag_hookup_t modified_cpu_hookup;
35   double power_peak;
36   double power_scale;
37   tmgr_trace_event_t power_event;
38   int core;
39   e_surf_resource_state_t state_current;
40   tmgr_trace_event_t state_event;
41   lmm_constraint_t constraint;
42   xbt_swag_t action_set;
43   double last_update;
44 } s_cpu_Cas01_t, *cpu_Cas01_t;
45
46 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
47                                 "Logging specific to the SURF CPU IMPROVED module");
48
49
50
51 static xbt_swag_t
52     cpu_running_action_set_that_does_not_need_being_checked = NULL;
53
54 static void* cpu_create_resource(const char *name, double power_peak,
55                                  double power_scale,
56                                  tmgr_trace_t power_trace,
57                                  int core,
58                                  e_surf_resource_state_t state_initial,
59                                  tmgr_trace_t state_trace,
60                                  xbt_dict_t cpu_properties)
61 {
62   cpu_Cas01_t cpu = NULL;
63   s_surf_action_cpu_Cas01_t action;
64
65   xbt_assert(!surf_cpu_resource_by_name(name),
66               "Host '%s' declared several times in the platform file",
67               name);
68   cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
69           surf_cpu_model, name,cpu_properties);
70   cpu->power_peak = power_peak;
71   xbt_assert(cpu->power_peak > 0, "Power has to be >0");
72   cpu->power_scale = power_scale;
73   cpu->core = core;
74   xbt_assert(core>0,"Invalid number of cores %d",core);
75
76   if (power_trace)
77     cpu->power_event =
78         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
79
80   cpu->state_current = state_initial;
81   if (state_trace)
82     cpu->state_event =
83         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
84
85   cpu->constraint =
86       lmm_constraint_new(cpu_maxmin_system, cpu,
87                          cpu->core * cpu->power_scale * cpu->power_peak);
88
89   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
90   if(cpu_update_mechanism == UM_LAZY)
91     cpu->action_set = xbt_swag_new(xbt_swag_offset(action, cpu_list_hookup));
92
93   return cpu;
94 }
95
96
97 static void parse_cpu_init(sg_platf_host_cbarg_t host)
98 {
99         cpu_create_resource(host->id,
100                           host->power_peak,
101                           host->power_scale,
102                           host->power_trace,
103                           host->core_amount,
104                           host->initial_state,
105                           host->state_trace,
106                           host->properties);
107 }
108
109 static void cpu_add_traces_cpu(void)
110 {
111   xbt_dict_cursor_t cursor = NULL;
112   char *trace_name, *elm;
113   static int called = 0;
114   if (called)
115     return;
116   called = 1;
117
118   /* connect all traces relative to hosts */
119   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
120     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
121     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
122
123     xbt_assert(host, "Host %s undefined", elm);
124     xbt_assert(trace, "Trace %s undefined", trace_name);
125
126     host->state_event =
127         tmgr_history_add_trace(history, trace, 0.0, 0, host);
128   }
129
130   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
131     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
132     cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
133
134     xbt_assert(host, "Host %s undefined", elm);
135     xbt_assert(trace, "Trace %s undefined", trace_name);
136
137     host->power_event =
138         tmgr_history_add_trace(history, trace, 0.0, 0, host);
139   }
140 }
141
142 static void cpu_define_callbacks()
143 {
144   sg_platf_host_add_cb(parse_cpu_init);
145   sg_platf_postparse_add_cb(cpu_add_traces_cpu);
146 }
147
148 static int cpu_resource_used(void *resource)
149 {
150   return lmm_constraint_used(cpu_maxmin_system,
151                              ((cpu_Cas01_t) resource)->constraint);
152 }
153
154 static int cpu_action_unref(surf_action_t action)
155 {
156   action->refcount--;
157   if (!action->refcount) {
158     xbt_swag_remove(action, action->state_set);
159     if (((surf_action_lmm_t) action)->variable)
160       lmm_variable_free(cpu_maxmin_system,
161                         ((surf_action_lmm_t) action)->variable);
162     if(cpu_update_mechanism == UM_LAZY){
163     /* remove from heap */
164     xbt_heap_remove(cpu_action_heap,
165                     ((surf_action_cpu_Cas01_t) action)->index_heap);
166     xbt_swag_remove(action,
167                     ((cpu_Cas01_t) ACTION_GET_CPU(action))->action_set);
168     xbt_swag_insert(ACTION_GET_CPU(action), cpu_modified_cpu);
169     }
170 #ifdef HAVE_TRACING
171     xbt_free(action->category);
172 #endif
173     surf_action_free(&action);
174     return 1;
175   }
176   return 0;
177 }
178
179 static void cpu_action_cancel(surf_action_t action)
180 {
181   surf_action_state_set(action, SURF_ACTION_FAILED);
182   if(cpu_update_mechanism == UM_LAZY){
183   xbt_heap_remove(cpu_action_heap,
184                   ((surf_action_cpu_Cas01_t) action)->index_heap);
185   xbt_swag_remove(action,
186                   ((cpu_Cas01_t) ACTION_GET_CPU(action))->action_set);
187   }
188   return;
189 }
190
191 static void cpu_cpu_action_state_set(surf_action_t action,
192                                         e_surf_action_state_t state)
193 {
194 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
195 /*     if(((surf_action_lmm_t)action)->variable) { */
196 /*       lmm_variable_disable(cpu_maxmin_system, ((surf_action_lmm_t)action)->variable); */
197 /*       ((surf_action_lmm_t)action)->variable = NULL; */
198 /*     } */
199
200   surf_action_state_set(action, state);
201   return;
202 }
203
204 static void cpu_update_remains(cpu_Cas01_t cpu, double now)
205 {
206   surf_action_cpu_Cas01_t action;
207
208   if (cpu->last_update >= now)
209     return;
210   xbt_swag_foreach(action, cpu->action_set) {
211     if (GENERIC_ACTION(action).state_set !=
212         surf_cpu_model->states.running_action_set)
213       continue;
214
215     /* bogus priority, skip it */
216     if (GENERIC_ACTION(action).priority <= 0)
217       continue;
218
219     if (GENERIC_ACTION(action).remains > 0) {
220       double_update(&(GENERIC_ACTION(action).remains),
221                     lmm_variable_getvalue(GENERIC_LMM_ACTION
222                                           (action).variable) * (now -
223                                                                 cpu->last_update));
224 #ifdef HAVE_TRACING
225       if (TRACE_is_enabled()) {
226         TRACE_surf_host_set_utilization(cpu->generic_resource.name,
227                                         action->
228                                         generic_lmm_action.generic_action.
229                                         data, (surf_action_t) action,
230                                         lmm_variable_getvalue
231                                         (GENERIC_LMM_ACTION
232                                          (action).variable),
233                                         cpu->last_update,
234                                         now - cpu->last_update);
235       }
236 #endif
237       XBT_DEBUG("Update action(%p) remains %lf", action,
238              GENERIC_ACTION(action).remains);
239     }
240   }
241   cpu->last_update = now;
242 }
243
244 static double cpu_share_resources_lazy(double now)
245 {
246   surf_action_cpu_Cas01_t action;
247   double min;
248   double value;
249   cpu_Cas01_t cpu, cpu_next;
250
251   xbt_swag_foreach(cpu, cpu_modified_cpu)
252       cpu_update_remains(cpu, now);
253
254   lmm_solve(cpu_maxmin_system);
255
256   xbt_swag_foreach_safe(cpu, cpu_next, cpu_modified_cpu) {
257     xbt_swag_foreach(action, cpu->action_set) {
258       if (GENERIC_ACTION(action).state_set !=
259           surf_cpu_model->states.running_action_set)
260         continue;
261
262       /* bogus priority, skip it */
263       if (GENERIC_ACTION(action).priority <= 0)
264         continue;
265
266       min = -1;
267       value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
268       if (value > 0) {
269         if (GENERIC_ACTION(action).remains > 0) {
270           value = GENERIC_ACTION(action).remains / value;
271           min = now + value;
272         } else {
273           value = 0.0;
274           min = now;
275         }
276       }
277
278       if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
279           && (min == -1
280               || GENERIC_ACTION(action).start +
281               GENERIC_ACTION(action).max_duration < min))
282         min =
283             GENERIC_ACTION(action).start +
284             GENERIC_ACTION(action).max_duration;
285
286       XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
287              GENERIC_ACTION(action).start, now + value,
288              GENERIC_ACTION(action).max_duration);
289
290       if (action->index_heap >= 0) {
291         surf_action_cpu_Cas01_t heap_act =
292             xbt_heap_remove(cpu_action_heap, action->index_heap);
293         if (heap_act != action)
294           DIE_IMPOSSIBLE;
295       }
296       if (min != -1) {
297         xbt_heap_push(cpu_action_heap, action, min);
298         XBT_DEBUG("Insert at heap action(%p) min %lf", action, min);
299       }
300     }
301     xbt_swag_remove(cpu, cpu_modified_cpu);
302   }
303   return xbt_heap_size(cpu_action_heap) >
304       0 ? xbt_heap_maxkey(cpu_action_heap) - now : -1;
305 }
306
307 static double cpu_share_resources_full(double now) {
308   surf_action_cpu_Cas01_t action;
309   return generic_maxmin_share_resources(surf_cpu_model->states.running_action_set,
310       xbt_swag_offset(*action, generic_lmm_action.variable),
311       cpu_maxmin_system, lmm_solve);
312 }
313
314 static void cpu_update_actions_state_lazy(double now, double delta)
315 {
316   surf_action_cpu_Cas01_t action;
317   while ((xbt_heap_size(cpu_action_heap) > 0)
318          && (double_equals(xbt_heap_maxkey(cpu_action_heap), now))) {
319     action = xbt_heap_pop(cpu_action_heap);
320     XBT_DEBUG("Action %p: finish", action);
321     GENERIC_ACTION(action).finish = surf_get_clock();
322     /* set the remains to 0 due to precision problems when updating the remaining amount */
323 #ifdef HAVE_TRACING
324     if (TRACE_is_enabled()) {
325       cpu_Cas01_t cpu = ((cpu_Cas01_t)(action->cpu));
326       TRACE_surf_host_set_utilization(cpu->generic_resource.name,
327           GENERIC_LMM_ACTION(action).generic_action.data,
328           (surf_action_t) action,
329           lmm_variable_getvalue (GENERIC_LMM_ACTION(action).variable),
330           cpu->last_update,
331           now - cpu->last_update);
332     }
333 #endif
334     GENERIC_ACTION(action).remains = 0;
335     cpu_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
336     cpu_update_remains(action->cpu, surf_get_clock());
337   }
338 #ifdef HAVE_TRACING
339   if (TRACE_is_enabled()) {
340     //defining the last timestamp that we can safely dump to trace file
341     //without losing the event ascending order (considering all CPU's)
342     void **data;
343     cpu_Cas01_t cpu;
344     xbt_lib_cursor_t cursor;
345     char *key;
346     double smaller = -1;
347     xbt_lib_foreach(host_lib, cursor, key, data){
348       if(data[SURF_CPU_LEVEL]){
349         cpu = data[SURF_CPU_LEVEL];
350         if (smaller < 0){
351           smaller = cpu->last_update;
352           continue;
353         }
354         if (cpu->last_update < smaller){
355           smaller = cpu->last_update;
356         }
357       }
358     }
359     if (smaller > 0) {
360       TRACE_last_timestamp_to_dump = smaller;
361     }
362   }
363 #endif
364   return;
365 }
366
367 static void cpu_update_actions_state_full(double now, double delta)
368 {
369   surf_action_cpu_Cas01_t action = NULL;
370   surf_action_cpu_Cas01_t next_action = NULL;
371   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
372   xbt_swag_foreach_safe(action, next_action, running_actions) {
373 #ifdef HAVE_TRACING
374     if (TRACE_is_enabled()) {
375       cpu_Cas01_t x =
376         lmm_constraint_id(lmm_get_cnst_from_var
377                           (cpu_maxmin_system, GENERIC_LMM_ACTION(action).variable, 0));
378
379       TRACE_surf_host_set_utilization(x->generic_resource.name,
380                                       GENERIC_ACTION(action).data,
381                                       (surf_action_t) action,
382                                       lmm_variable_getvalue
383                                       (GENERIC_LMM_ACTION(action).variable), now - delta,
384                                       delta);
385       TRACE_last_timestamp_to_dump = now-delta;
386     }
387 #endif
388     double_update(&(GENERIC_ACTION(action).remains),
389                   lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
390     if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
391       double_update(&(GENERIC_ACTION(action).max_duration), delta);
392     if ((GENERIC_ACTION(action).remains <= 0) &&
393         (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
394       GENERIC_ACTION(action).finish = surf_get_clock();
395       cpu_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
396     } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
397                (GENERIC_ACTION(action).max_duration <= 0)) {
398       GENERIC_ACTION(action).finish = surf_get_clock();
399       cpu_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
400     }
401   }
402
403   return;
404 }
405
406 static void cpu_update_resource_state(void *id,
407                                          tmgr_trace_event_t event_type,
408                                          double value, double date)
409 {
410   cpu_Cas01_t cpu = id;
411   lmm_variable_t var = NULL;
412   lmm_element_t elem = NULL;
413
414   if (event_type == cpu->power_event) {
415     cpu->power_scale = value;
416     lmm_update_constraint_bound(cpu_maxmin_system, cpu->constraint,
417                                 cpu->core * cpu->power_scale * cpu->power_peak);
418 #ifdef HAVE_TRACING
419     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
420                               cpu->core * cpu->power_scale * cpu->power_peak);
421 #endif
422     while ((var = lmm_get_var_from_cnst
423             (cpu_maxmin_system, cpu->constraint, &elem))) {
424         surf_action_cpu_Cas01_t action = lmm_variable_id(var);
425         lmm_update_variable_bound(cpu_maxmin_system, GENERIC_LMM_ACTION(action).variable,
426                                   cpu->power_scale * cpu->power_peak);
427     }
428     if(cpu_update_mechanism == UM_LAZY)
429       xbt_swag_insert(cpu, cpu_modified_cpu);
430     if (tmgr_trace_event_free(event_type))
431       cpu->power_event = NULL;
432   } else if (event_type == cpu->state_event) {
433     if (value > 0)
434       cpu->state_current = SURF_RESOURCE_ON;
435     else {
436       lmm_constraint_t cnst = cpu->constraint;
437
438       cpu->state_current = SURF_RESOURCE_OFF;
439
440       while ((var =
441               lmm_get_var_from_cnst(cpu_maxmin_system, cnst, &elem))) {
442         surf_action_t action = lmm_variable_id(var);
443
444         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
445             surf_action_state_get(action) == SURF_ACTION_READY ||
446             surf_action_state_get(action) ==
447             SURF_ACTION_NOT_IN_THE_SYSTEM) {
448           action->finish = date;
449           cpu_cpu_action_state_set(action, SURF_ACTION_FAILED);
450         }
451       }
452     }
453     if (tmgr_trace_event_free(event_type))
454       cpu->state_event = NULL;
455   } else {
456     XBT_CRITICAL("Unknown event ! \n");
457     xbt_abort();
458   }
459
460   return;
461 }
462
463 static surf_action_t cpu_execute(void *cpu, double size)
464 {
465   surf_action_cpu_Cas01_t action = NULL;
466   cpu_Cas01_t CPU = cpu;
467
468   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
469   action =
470       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
471                       surf_cpu_model,
472                       CPU->state_current != SURF_RESOURCE_ON);
473
474   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
475                                                    calloc but it seems to help valgrind... */
476
477   GENERIC_LMM_ACTION(action).variable =
478       lmm_variable_new(cpu_maxmin_system, action,
479                        GENERIC_ACTION(action).priority, CPU->power_scale * CPU->power_peak, 1);
480   if(cpu_update_mechanism == UM_LAZY){
481     action->index_heap = -1;
482     action->cpu = CPU;
483     xbt_swag_insert(CPU, cpu_modified_cpu);
484     xbt_swag_insert(action, CPU->action_set);
485   }
486   lmm_expand(cpu_maxmin_system, CPU->constraint,
487              GENERIC_LMM_ACTION(action).variable, 1.0);
488   XBT_OUT();
489   return (surf_action_t) action;
490 }
491
492 static surf_action_t cpu_action_sleep(void *cpu, double duration)
493 {
494   surf_action_cpu_Cas01_t action = NULL;
495
496   if (duration > 0)
497     duration = MAX(duration, MAXMIN_PRECISION);
498
499   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
500   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
501   GENERIC_ACTION(action).max_duration = duration;
502   GENERIC_LMM_ACTION(action).suspended = 2;
503   if (duration == NO_MAX_DURATION) {
504     /* Move to the *end* of the corresponding action set. This convention
505        is used to speed up update_resource_state  */
506     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
507     ((surf_action_t) action)->state_set =
508         cpu_running_action_set_that_does_not_need_being_checked;
509     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
510   }
511
512   lmm_update_variable_weight(cpu_maxmin_system,
513                              GENERIC_LMM_ACTION(action).variable, 0.0);
514   if(cpu_update_mechanism == UM_LAZY)
515     xbt_swag_insert(cpu, cpu_modified_cpu);
516   XBT_OUT();
517   return (surf_action_t) action;
518 }
519
520 static void cpu_action_suspend(surf_action_t action)
521 {
522   XBT_IN("(%p)", action);
523   if (((surf_action_lmm_t) action)->suspended != 2) {
524     lmm_update_variable_weight(cpu_maxmin_system,
525                                ((surf_action_lmm_t) action)->variable,
526                                0.0);
527     ((surf_action_lmm_t) action)->suspended = 1;
528     if(cpu_update_mechanism == UM_LAZY){
529       xbt_heap_remove(cpu_action_heap,
530                       ((surf_action_cpu_Cas01_t) action)->index_heap);
531       xbt_swag_insert(ACTION_GET_CPU(action), cpu_modified_cpu);
532     }
533   }
534   XBT_OUT();
535 }
536
537 static void cpu_action_resume(surf_action_t action)
538 {
539
540   XBT_IN("(%p)", action);
541   if (((surf_action_lmm_t) action)->suspended != 2) {
542     lmm_update_variable_weight(cpu_maxmin_system,
543                                ((surf_action_lmm_t) action)->variable,
544                                action->priority);
545     ((surf_action_lmm_t) action)->suspended = 0;
546     if(cpu_update_mechanism == UM_LAZY)
547       xbt_swag_insert(ACTION_GET_CPU(action), cpu_modified_cpu);
548   }
549   XBT_OUT();
550 }
551
552 static int cpu_action_is_suspended(surf_action_t action)
553 {
554   return (((surf_action_lmm_t) action)->suspended == 1);
555 }
556
557 static void cpu_action_set_max_duration(surf_action_t action,
558                                            double duration)
559 {
560   XBT_IN("(%p,%g)", action, duration);
561
562   action->max_duration = duration;
563   /* insert cpu in modified_cpu set to notice the max duration change */
564   if(cpu_update_mechanism == UM_LAZY)
565     xbt_swag_insert(ACTION_GET_CPU(action), cpu_modified_cpu);
566   XBT_OUT();
567 }
568
569 static void cpu_action_set_priority(surf_action_t action,
570                                        double priority)
571 {
572   XBT_IN("(%p,%g)", action, priority);
573   action->priority = priority;
574   lmm_update_variable_weight(cpu_maxmin_system,
575                              ((surf_action_lmm_t) action)->variable,
576                              priority);
577
578   if(cpu_update_mechanism == UM_LAZY)
579     xbt_swag_insert(ACTION_GET_CPU(action), cpu_modified_cpu);
580   XBT_OUT();
581 }
582
583 #ifdef HAVE_TRACING
584 static void cpu_action_set_category(surf_action_t action,
585                                        const char *category)
586 {
587   XBT_IN("(%p,%s)", action, category);
588   action->category = xbt_strdup (category);
589   XBT_OUT();
590 }
591 #endif
592
593 static double cpu_action_get_remains(surf_action_t action)
594 {
595   XBT_IN("(%p)", action);
596   /* update remains before return it */
597   if(cpu_update_mechanism == UM_LAZY)
598     cpu_update_remains(ACTION_GET_CPU(action), surf_get_clock());
599   XBT_OUT();
600   return action->remains;
601 }
602
603 static e_surf_resource_state_t cpu_get_state(void *cpu)
604 {
605   return ((cpu_Cas01_t) cpu)->state_current;
606 }
607
608 static double cpu_get_speed(void *cpu, double load)
609 {
610   return load * (((cpu_Cas01_t) cpu)->power_peak);
611 }
612
613 static double cpu_get_available_speed(void *cpu)
614 {
615   /* number between 0 and 1 */
616   return ((cpu_Cas01_t) cpu)->power_scale;
617 }
618
619 static void cpu_action_update_index_heap(void *action, int i)
620 {
621   ((surf_action_cpu_Cas01_t) action)->index_heap = i;
622 }
623
624 static void cpu_finalize(void)
625 {
626   void **cpu;
627   xbt_lib_cursor_t cursor;
628   char *key;
629
630   xbt_lib_foreach(host_lib, cursor, key, cpu){
631     if(cpu[SURF_CPU_LEVEL])
632     {
633         cpu_Cas01_t CPU = cpu[SURF_CPU_LEVEL];
634         xbt_swag_free(CPU->action_set);
635     }
636   }
637
638   lmm_system_free(cpu_maxmin_system);
639   cpu_maxmin_system = NULL;
640
641   surf_model_exit(surf_cpu_model);
642   surf_cpu_model = NULL;
643
644   xbt_swag_free
645       (cpu_running_action_set_that_does_not_need_being_checked);
646   cpu_running_action_set_that_does_not_need_being_checked = NULL;
647   if(cpu_action_heap)  xbt_heap_free(cpu_action_heap);
648   if(cpu_modified_cpu) xbt_swag_free(cpu_modified_cpu);
649 }
650
651 static void surf_cpu_model_init_internal()
652 {
653   s_surf_action_t action;
654   s_cpu_Cas01_t cpu;
655
656   surf_cpu_model = surf_model_init();
657
658   cpu_running_action_set_that_does_not_need_being_checked =
659       xbt_swag_new(xbt_swag_offset(action, state_hookup));
660
661   surf_cpu_model->name = "cpu";
662
663   surf_cpu_model->action_unref = cpu_action_unref;
664   surf_cpu_model->action_cancel = cpu_action_cancel;
665   surf_cpu_model->action_state_set = cpu_cpu_action_state_set;
666
667   surf_cpu_model->model_private->resource_used = cpu_resource_used;
668
669   if(cpu_update_mechanism == UM_LAZY) {
670     surf_cpu_model->model_private->share_resources = cpu_share_resources_lazy;
671     surf_cpu_model->model_private->update_actions_state = cpu_update_actions_state_lazy;
672   } else if (cpu_update_mechanism == UM_FULL) {
673     surf_cpu_model->model_private->share_resources = cpu_share_resources_full;
674     surf_cpu_model->model_private->update_actions_state = cpu_update_actions_state_full;
675   } else
676     xbt_die("Invalid update mechanism!");
677
678   surf_cpu_model->model_private->update_resource_state =
679       cpu_update_resource_state;
680   surf_cpu_model->model_private->finalize = cpu_finalize;
681
682   surf_cpu_model->suspend = cpu_action_suspend;
683   surf_cpu_model->resume = cpu_action_resume;
684   surf_cpu_model->is_suspended = cpu_action_is_suspended;
685   surf_cpu_model->set_max_duration = cpu_action_set_max_duration;
686   surf_cpu_model->set_priority = cpu_action_set_priority;
687 #ifdef HAVE_TRACING
688   surf_cpu_model->set_category = cpu_action_set_category;
689 #endif
690   surf_cpu_model->get_remains = cpu_action_get_remains;
691
692   surf_cpu_model->extension.cpu.execute = cpu_execute;
693   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
694
695   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
696   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
697   surf_cpu_model->extension.cpu.get_available_speed =
698       cpu_get_available_speed;
699   surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
700   surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
701
702   if (!cpu_maxmin_system) {
703     cpu_maxmin_system = lmm_system_new(cpu_selective_update);
704   }
705   if(cpu_update_mechanism == UM_LAZY){
706     cpu_action_heap = xbt_heap_new(8, NULL);
707     xbt_heap_set_update_callback(cpu_action_heap,
708                                  cpu_action_update_index_heap);
709     cpu_modified_cpu =
710         xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
711   }
712 }
713
714 /*********************************************************************/
715 /* Basic sharing model for CPU: that is where all this started... ;) */
716 /*********************************************************************/
717 /* @InProceedings{casanova01simgrid, */
718 /*   author =       "H. Casanova", */
719 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
720 /*                  and the Grid (CCGrid'01)", */
721 /*   publisher =    "IEEE Computer Society", */
722 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
723 /*                  Scheduling", */
724 /*   year =         "2001", */
725 /*   month =        may, */
726 /*   note =         "Available at */
727 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
728 /* } */
729
730 void surf_cpu_model_init_Cas01()
731 {
732   char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
733   int select = xbt_cfg_get_int(_surf_cfg_set, "cpu/maxmin_selective_update");
734
735   if(!strcmp(optim,"Full")) {
736     cpu_update_mechanism = UM_FULL;
737     cpu_selective_update = select;
738   } else if (!strcmp(optim,"Lazy")) {
739     cpu_update_mechanism = UM_LAZY;
740     cpu_selective_update = 1;
741     xbt_assert((select==1) || (xbt_cfg_is_default_value(_surf_cfg_set,"cpu/maxmin_selective_update")),
742         "Disabling selective update while using the lazy update mechanism is dumb!");
743   } else if (!strcmp(optim,"TI")) {
744     surf_cpu_model_init_ti();
745     return;
746   } else {
747     xbt_die("Unsupported optimization (%s) for this model",optim);
748   }
749
750   if (surf_cpu_model)
751     return;
752   surf_cpu_model_init_internal();
753   cpu_define_callbacks();
754   xbt_dynar_push(model_list, &surf_cpu_model);
755 }