Logo AND Algorithmique Numérique Distribuée

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