Logo AND Algorithmique Numérique Distribuée

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