Logo AND Algorithmique Numérique Distribuée

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