Logo AND Algorithmique Numérique Distribuée

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