Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[trace] cosmetics
[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
160   surf_parse_reset_parser();
161   surfxml_add_callback(STag_surfxml_host_cb_list, parse_cpu_im_init);
162   surfxml_add_callback(ETag_surfxml_platform_cb_list,
163                        &cpu_im_add_traces_cpu);
164 }
165
166 static int cpu_im_resource_used(void *resource_id)
167 {
168   return lmm_constraint_used(cpu_im_maxmin_system,
169                              ((cpu_Cas01_im_t) resource_id)->constraint);
170 }
171
172 static int cpu_im_action_unref(surf_action_t action)
173 {
174   action->refcount--;
175   if (!action->refcount) {
176     xbt_swag_remove(action, action->state_set);
177     if (((surf_action_lmm_t) action)->variable)
178       lmm_variable_free(cpu_im_maxmin_system,
179                         ((surf_action_lmm_t) action)->variable);
180     /* remove from heap */
181     xbt_heap_remove(cpu_im_action_heap,
182                     ((surf_action_cpu_Cas01_im_t) action)->index_heap);
183     xbt_swag_remove(action,
184                     ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
185     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
186 #ifdef HAVE_TRACING
187     if (action->category)
188       xbt_free(action->category);
189 #endif
190     free(action);
191     return 1;
192   }
193   return 0;
194 }
195
196 static void cpu_im_action_cancel(surf_action_t action)
197 {
198   surf_action_state_set(action, SURF_ACTION_FAILED);
199   xbt_heap_remove(cpu_im_action_heap,
200                   ((surf_action_cpu_Cas01_im_t) action)->index_heap);
201   xbt_swag_remove(action,
202                   ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
203   return;
204 }
205
206 static void cpu_im_cpu_action_state_set(surf_action_t action,
207                                         e_surf_action_state_t state)
208 {
209 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
210 /*     if(((surf_action_lmm_t)action)->variable) { */
211 /*       lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
212 /*       ((surf_action_lmm_t)action)->variable = NULL; */
213 /*     } */
214
215   surf_action_state_set(action, state);
216   return;
217 }
218
219 static void cpu_im_update_remains(cpu_Cas01_im_t cpu, double now)
220 {
221   surf_action_cpu_Cas01_im_t action;
222
223   if (cpu->last_update >= now)
224     return;
225   xbt_swag_foreach(action, cpu->action_set) {
226     if (GENERIC_ACTION(action).state_set !=
227         surf_cpu_model->states.running_action_set)
228       continue;
229
230     /* bogus priority, skip it */
231     if (GENERIC_ACTION(action).priority <= 0)
232       continue;
233
234     if (GENERIC_ACTION(action).remains > 0) {
235       double_update(&(GENERIC_ACTION(action).remains),
236                     lmm_variable_getvalue(GENERIC_LMM_ACTION
237                                           (action).variable) * (now -
238                                                                 cpu->last_update));
239 #ifdef HAVE_TRACING
240       TRACE_surf_host_set_utilization(cpu->generic_resource.name,
241                                       action->
242                                       generic_lmm_action.generic_action.
243                                       data, (surf_action_t) action,
244                                       lmm_variable_getvalue
245                                       (GENERIC_LMM_ACTION
246                                        (action).variable),
247                                       cpu->last_update,
248                                       now - cpu->last_update);
249 #endif
250       DEBUG2("Update action(%p) remains %lf", action,
251              GENERIC_ACTION(action).remains);
252     }
253   }
254   cpu->last_update = now;
255 }
256
257 static double cpu_im_share_resources(double now)
258 {
259   surf_action_cpu_Cas01_im_t action;
260   double min;
261   double value;
262   cpu_Cas01_im_t cpu, cpu_next;
263
264   xbt_swag_foreach(cpu, cpu_im_modified_cpu)
265       cpu_im_update_remains(cpu, now);
266
267   lmm_solve(cpu_im_maxmin_system);
268
269   xbt_swag_foreach_safe(cpu, cpu_next, cpu_im_modified_cpu) {
270     xbt_swag_foreach(action, cpu->action_set) {
271       if (GENERIC_ACTION(action).state_set !=
272           surf_cpu_model->states.running_action_set)
273         continue;
274
275       /* bogus priority, skip it */
276       if (GENERIC_ACTION(action).priority <= 0)
277         continue;
278
279       min = -1;
280       value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
281       if (value > 0) {
282         if (GENERIC_ACTION(action).remains > 0) {
283           value = GENERIC_ACTION(action).remains / value;
284           min = now + value;
285         } else {
286           value = 0.0;
287           min = now;
288         }
289       }
290
291       if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
292           && (min == -1
293               || GENERIC_ACTION(action).start +
294               GENERIC_ACTION(action).max_duration < min))
295         min =
296             GENERIC_ACTION(action).start +
297             GENERIC_ACTION(action).max_duration;
298
299       DEBUG4("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
300              GENERIC_ACTION(action).start, now + value,
301              GENERIC_ACTION(action).max_duration);
302
303       if (action->index_heap >= 0) {
304         surf_action_cpu_Cas01_im_t heap_act =
305             xbt_heap_remove(cpu_im_action_heap, action->index_heap);
306         if (heap_act != action)
307           DIE_IMPOSSIBLE;
308       }
309       if (min != -1) {
310         xbt_heap_push(cpu_im_action_heap, action, min);
311         DEBUG2("Insert at heap action(%p) min %lf", action, min);
312       }
313     }
314     xbt_swag_remove(cpu, cpu_im_modified_cpu);
315   }
316   return xbt_heap_size(cpu_im_action_heap) >
317       0 ? xbt_heap_maxkey(cpu_im_action_heap) - now : -1;
318 }
319
320 static void cpu_im_update_actions_state(double now, double delta)
321 {
322   surf_action_cpu_Cas01_im_t action;
323
324   while ((xbt_heap_size(cpu_im_action_heap) > 0)
325          && (double_equals(xbt_heap_maxkey(cpu_im_action_heap), now))) {
326     action = xbt_heap_pop(cpu_im_action_heap);
327     DEBUG1("Action %p: finish", action);
328     GENERIC_ACTION(action).finish = surf_get_clock();
329     /* set the remains to 0 due to precision problems when updating the remaining amount */
330 #ifdef HAVE_TRACING
331     {
332       cpu_Cas01_im_t cpu = ((cpu_Cas01_im_t)(action->cpu));
333       TRACE_surf_host_set_utilization(cpu->generic_resource.name,
334           action->generic_lmm_action.generic_action.data,
335           (surf_action_t) action,
336           lmm_variable_getvalue (GENERIC_LMM_ACTION(action).variable),
337           cpu->last_update,
338           now - cpu->last_update);
339     }
340 #endif
341     GENERIC_ACTION(action).remains = 0;
342     cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
343     cpu_im_update_remains(action->cpu, surf_get_clock());
344   }
345 #ifdef HAVE_TRACING
346   {
347     //defining the last timestamp that we can safely dump to trace file
348     //without losing the event ascending order (considering all CPU's)
349     cpu_Cas01_im_t cpu;
350     xbt_dict_cursor_t cursor;
351     char *key;
352     double smaller = -1;
353     xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key, cpu){
354       if (smaller < 0){
355         smaller = cpu->last_update;
356         continue;
357       }
358       if (cpu->last_update < smaller){
359         smaller = cpu->last_update;
360       }
361     }
362     if (smaller > 0) {
363       TRACE_last_timestamp_to_dump = smaller;
364     }
365   }
366 #endif
367   return;
368 }
369
370 static void cpu_im_update_resource_state(void *id,
371                                          tmgr_trace_event_t event_type,
372                                          double value, double date)
373 {
374   cpu_Cas01_im_t cpu = id;
375   lmm_variable_t var = NULL;
376   lmm_element_t elem = NULL;
377
378   if (event_type == cpu->power_event) {
379     cpu->power_scale = value;
380     lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
381                                 cpu->core * cpu->power_scale * cpu->power_peak);
382 #ifdef HAVE_TRACING
383     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
384                               cpu->core * cpu->power_scale * cpu->power_peak);
385 #endif
386     while ((var = lmm_get_var_from_cnst
387             (cpu_im_maxmin_system, cpu->constraint, &elem))) {
388         surf_action_cpu_Cas01_im_t action = lmm_variable_id(var);
389         lmm_update_variable_bound(cpu_im_maxmin_system, action->generic_lmm_action.variable,
390                                   cpu->power_scale * cpu->power_peak);
391     }
392     xbt_swag_insert(cpu, cpu_im_modified_cpu);
393     if (tmgr_trace_event_free(event_type))
394       cpu->power_event = NULL;
395   } else if (event_type == cpu->state_event) {
396     if (value > 0)
397       cpu->state_current = SURF_RESOURCE_ON;
398     else {
399       lmm_constraint_t cnst = cpu->constraint;
400       lmm_variable_t var = NULL;
401       lmm_element_t elem = NULL;
402
403       cpu->state_current = SURF_RESOURCE_OFF;
404
405       while ((var =
406               lmm_get_var_from_cnst(cpu_im_maxmin_system, cnst, &elem))) {
407         surf_action_t action = lmm_variable_id(var);
408
409         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
410             surf_action_state_get(action) == SURF_ACTION_READY ||
411             surf_action_state_get(action) ==
412             SURF_ACTION_NOT_IN_THE_SYSTEM) {
413           action->finish = date;
414           cpu_im_cpu_action_state_set(action, SURF_ACTION_FAILED);
415         }
416       }
417     }
418     if (tmgr_trace_event_free(event_type))
419       cpu->state_event = NULL;
420   } else {
421     CRITICAL0("Unknown event ! \n");
422     xbt_abort();
423   }
424
425   return;
426 }
427
428 static surf_action_t cpu_im_execute(void *cpu, double size)
429 {
430   surf_action_cpu_Cas01_im_t action = NULL;
431   cpu_Cas01_im_t CPU = cpu;
432
433   XBT_IN2("(%s,%g)", surf_resource_name(CPU), size);
434   action =
435       surf_action_new(sizeof(s_surf_action_cpu_Cas01_im_t), size,
436                       surf_cpu_model,
437                       CPU->state_current != SURF_RESOURCE_ON);
438
439   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
440                                                    calloc but it seems to help valgrind... */
441
442   GENERIC_LMM_ACTION(action).variable =
443       lmm_variable_new(cpu_im_maxmin_system, action,
444                        GENERIC_ACTION(action).priority, CPU->power_scale * CPU->power_peak, 1);
445   action->index_heap = -1;
446   action->cpu = CPU;
447   xbt_swag_insert(CPU, cpu_im_modified_cpu);
448   xbt_swag_insert(action, CPU->action_set);
449   lmm_expand(cpu_im_maxmin_system, CPU->constraint,
450              GENERIC_LMM_ACTION(action).variable, 1.0);
451   XBT_OUT;
452   return (surf_action_t) action;
453 }
454
455 static surf_action_t cpu_im_action_sleep(void *cpu, double duration)
456 {
457   surf_action_cpu_Cas01_im_t action = NULL;
458
459   if (duration > 0)
460     duration = MAX(duration, MAXMIN_PRECISION);
461
462   XBT_IN2("(%s,%g)", surf_resource_name(cpu), duration);
463   action = (surf_action_cpu_Cas01_im_t) cpu_im_execute(cpu, 1.0);
464   GENERIC_ACTION(action).max_duration = duration;
465   GENERIC_LMM_ACTION(action).suspended = 2;
466   if (duration == NO_MAX_DURATION) {
467     /* Move to the *end* of the corresponding action set. This convention
468        is used to speed up update_resource_state  */
469     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
470     ((surf_action_t) action)->state_set =
471         cpu_im_running_action_set_that_does_not_need_being_checked;
472     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
473   }
474
475   lmm_update_variable_weight(cpu_im_maxmin_system,
476                              GENERIC_LMM_ACTION(action).variable, 0.0);
477   xbt_swag_insert(cpu, cpu_im_modified_cpu);
478   XBT_OUT;
479   return (surf_action_t) action;
480 }
481
482 static void cpu_im_action_suspend(surf_action_t action)
483 {
484   XBT_IN1("(%p)", action);
485   if (((surf_action_lmm_t) action)->suspended != 2) {
486     lmm_update_variable_weight(cpu_im_maxmin_system,
487                                ((surf_action_lmm_t) action)->variable,
488                                0.0);
489     ((surf_action_lmm_t) action)->suspended = 1;
490     xbt_heap_remove(cpu_im_action_heap,
491                     ((surf_action_cpu_Cas01_im_t) action)->index_heap);
492     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
493   }
494   XBT_OUT;
495 }
496
497 static void cpu_im_action_resume(surf_action_t action)
498 {
499
500   XBT_IN1("(%p)", action);
501   if (((surf_action_lmm_t) action)->suspended != 2) {
502     lmm_update_variable_weight(cpu_im_maxmin_system,
503                                ((surf_action_lmm_t) action)->variable,
504                                action->priority);
505     ((surf_action_lmm_t) action)->suspended = 0;
506     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
507   }
508   XBT_OUT;
509 }
510
511 static int cpu_im_action_is_suspended(surf_action_t action)
512 {
513   return (((surf_action_lmm_t) action)->suspended == 1);
514 }
515
516 static void cpu_im_action_set_max_duration(surf_action_t action,
517                                            double duration)
518 {
519   XBT_IN2("(%p,%g)", action, duration);
520
521   action->max_duration = duration;
522   /* insert cpu in modified_cpu set to notice the max duration change */
523   xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
524   XBT_OUT;
525 }
526
527 static void cpu_im_action_set_priority(surf_action_t action,
528                                        double priority)
529 {
530   XBT_IN2("(%p,%g)", action, priority);
531   action->priority = priority;
532   lmm_update_variable_weight(cpu_im_maxmin_system,
533                              ((surf_action_lmm_t) action)->variable,
534                              priority);
535
536   xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
537   XBT_OUT;
538 }
539
540 #ifdef HAVE_TRACING
541 static void cpu_im_action_set_category(surf_action_t action,
542                                        const char *category)
543 {
544   XBT_IN2("(%p,%s)", action, category);
545   action->category = xbt_strdup (category);
546   XBT_OUT;
547 }
548 #endif
549
550 static double cpu_im_action_get_remains(surf_action_t action)
551 {
552   XBT_IN1("(%p)", action);
553   /* update remains before return it */
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_create_resource(char *name, double power_peak,
581                                    double power_scale,
582                                    tmgr_trace_t power_trace,
583                                    int core,
584                                    e_surf_resource_state_t state_initial,
585                                    tmgr_trace_t state_trace,
586                                    xbt_dict_t cpu_properties)
587 {
588   cpu_im_new(name, power_peak, power_scale, power_trace, core,
589              state_initial, state_trace, cpu_properties);
590 }
591
592 static void cpu_im_finalize(void)
593 {
594   void *cpu;
595   xbt_dict_cursor_t cursor;
596   char *key;
597   xbt_dict_foreach(surf_model_resource_set(surf_cpu_model), cursor, key,
598                    cpu) {
599     cpu_Cas01_im_t CPU = cpu;
600     xbt_swag_free(CPU->action_set);
601   }
602
603   lmm_system_free(cpu_im_maxmin_system);
604   cpu_im_maxmin_system = NULL;
605
606   surf_model_exit(surf_cpu_model);
607   surf_cpu_model = NULL;
608
609   xbt_swag_free
610       (cpu_im_running_action_set_that_does_not_need_being_checked);
611   cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
612   xbt_heap_free(cpu_im_action_heap);
613   xbt_swag_free(cpu_im_modified_cpu);
614 }
615
616 static void surf_cpu_im_model_init_internal(void)
617 {
618   s_surf_action_t action;
619   s_cpu_Cas01_im_t cpu;
620
621   surf_cpu_model = surf_model_init();
622
623   cpu_im_running_action_set_that_does_not_need_being_checked =
624       xbt_swag_new(xbt_swag_offset(action, state_hookup));
625
626   surf_cpu_model->name = "CPU_IM";
627
628   surf_cpu_model->action_unref = cpu_im_action_unref;
629   surf_cpu_model->action_cancel = cpu_im_action_cancel;
630   surf_cpu_model->action_state_set = cpu_im_cpu_action_state_set;
631
632   surf_cpu_model->model_private->resource_used = cpu_im_resource_used;
633   surf_cpu_model->model_private->share_resources = cpu_im_share_resources;
634   surf_cpu_model->model_private->update_actions_state =
635       cpu_im_update_actions_state;
636   surf_cpu_model->model_private->update_resource_state =
637       cpu_im_update_resource_state;
638   surf_cpu_model->model_private->finalize = cpu_im_finalize;
639
640   surf_cpu_model->suspend = cpu_im_action_suspend;
641   surf_cpu_model->resume = cpu_im_action_resume;
642   surf_cpu_model->is_suspended = cpu_im_action_is_suspended;
643   surf_cpu_model->set_max_duration = cpu_im_action_set_max_duration;
644   surf_cpu_model->set_priority = cpu_im_action_set_priority;
645 #ifdef HAVE_TRACING
646   surf_cpu_model->set_category = cpu_im_action_set_category;
647 #endif
648   surf_cpu_model->get_remains = cpu_im_action_get_remains;
649
650   surf_cpu_model->extension.cpu.execute = cpu_im_execute;
651   surf_cpu_model->extension.cpu.sleep = cpu_im_action_sleep;
652
653   surf_cpu_model->extension.cpu.get_state = cpu_im_get_state;
654   surf_cpu_model->extension.cpu.get_speed = cpu_im_get_speed;
655   surf_cpu_model->extension.cpu.get_available_speed =
656       cpu_im_get_available_speed;
657   surf_cpu_model->extension.cpu.create_resource = cpu_im_create_resource;
658   surf_cpu_model->extension.cpu.add_traces = cpu_im_add_traces_cpu;
659
660   if (!cpu_im_maxmin_system) {
661     sg_maxmin_selective_update = 1;
662     cpu_im_maxmin_system = lmm_system_new();
663   }
664   cpu_im_action_heap = xbt_heap_new(8, NULL);
665   xbt_heap_set_update_callback(cpu_im_action_heap,
666                                cpu_im_action_update_index_heap);
667   cpu_im_modified_cpu =
668       xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
669 }
670
671 /*********************************************************************/
672 /* Basic sharing model for CPU: that is where all this started... ;) */
673 /*********************************************************************/
674 /* @InProceedings{casanova01simgrid, */
675 /*   author =       "H. Casanova", */
676 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
677 /*                  and the Grid (CCGrid'01)", */
678 /*   publisher =    "IEEE Computer Society", */
679 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
680 /*                  Scheduling", */
681 /*   year =         "2001", */
682 /*   month =        may, */
683 /*   note =         "Available at */
684 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
685 /* } */
686 void surf_cpu_model_init_Cas01_im(const char *filename)
687 {
688   if (surf_cpu_model)
689     return;
690   surf_cpu_im_model_init_internal();
691   cpu_im_define_callbacks(filename);
692   xbt_dynar_push(model_list, &surf_cpu_model);
693 }