Logo AND Algorithmique Numérique Distribuée

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