Logo AND Algorithmique Numérique Distribuée

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