Logo AND Algorithmique Numérique Distribuée

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