Logo AND Algorithmique Numérique Distribuée

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