Logo AND Algorithmique Numérique Distribuée

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