Logo AND Algorithmique Numérique Distribuée

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