Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
50b23c03f5b65849714196fc13e524d6a8edd4fc
[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 typedef enum {
14   UM_FULL,
15   UM_LAZY,
16   UM_UNDEFINED,
17 } e_UM_t;
18
19 e_UM_t update_mechanism = UM_LAZY;
20
21 #undef GENERIC_LMM_ACTION
22 #undef GENERIC_ACTION
23 #undef ACTION_GET_CPU
24 #define GENERIC_LMM_ACTION(action) action->generic_lmm_action
25 #define GENERIC_ACTION(action) GENERIC_LMM_ACTION(action).generic_action
26 #define ACTION_GET_CPU(action) ((surf_action_cpu_Cas01_im_t) action)->cpu
27
28 typedef struct surf_action_cpu_cas01_im {
29   s_surf_action_lmm_t generic_lmm_action;
30   s_xbt_swag_hookup_t cpu_list_hookup;
31   int index_heap;
32   void *cpu;
33 } s_surf_action_cpu_Cas01_im_t, *surf_action_cpu_Cas01_im_t;
34
35 typedef struct cpu_Cas01_im {
36   s_surf_resource_t generic_resource;
37   s_xbt_swag_hookup_t modified_cpu_hookup;
38   double power_peak;
39   double power_scale;
40   tmgr_trace_event_t power_event;
41   int core;
42   e_surf_resource_state_t state_current;
43   tmgr_trace_event_t state_event;
44   lmm_constraint_t constraint;
45   xbt_swag_t action_set;
46   double last_update;
47 } s_cpu_Cas01_im_t, *cpu_Cas01_im_t;
48
49 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_im, surf,
50                                 "Logging specific to the SURF CPU IMPROVED module");
51
52 lmm_system_t cpu_im_maxmin_system = NULL;
53 static xbt_swag_t cpu_im_modified_cpu = NULL;
54 static xbt_heap_t cpu_im_action_heap = NULL;
55 extern int sg_maxmin_selective_update;
56
57
58 static xbt_swag_t
59     cpu_im_running_action_set_that_does_not_need_being_checked = NULL;
60
61 static void* cpu_im_create_resource(const char *name, double power_peak,
62                                  double power_scale,
63                                  tmgr_trace_t power_trace,
64                                  int core,
65                                  e_surf_resource_state_t state_initial,
66                                  tmgr_trace_t state_trace,
67                                  xbt_dict_t cpu_properties)
68 {
69   cpu_Cas01_im_t cpu = NULL;
70   s_surf_action_cpu_Cas01_im_t action;
71
72   xbt_assert(!surf_cpu_resource_by_name(name),
73               "Host '%s' declared several times in the platform file",
74               name);
75   cpu = (cpu_Cas01_im_t) surf_resource_new(sizeof(s_cpu_Cas01_im_t),
76           surf_cpu_model, name,cpu_properties);
77   cpu->power_peak = power_peak;
78   xbt_assert(cpu->power_peak > 0, "Power has to be >0");
79   cpu->power_scale = power_scale;
80   cpu->core = core;
81   xbt_assert(core>0,"Invalid number of cores %d",core);
82
83   if (power_trace)
84     cpu->power_event =
85         tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
86
87   cpu->state_current = state_initial;
88   if (state_trace)
89     cpu->state_event =
90         tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
91
92   cpu->constraint =
93       lmm_constraint_new(cpu_im_maxmin_system, cpu,
94                          cpu->core * cpu->power_scale * cpu->power_peak);
95
96   xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
97   if(update_mechanism == UM_FULL)
98     cpu->action_set = xbt_swag_new(xbt_swag_offset(action, cpu_list_hookup));
99
100   return cpu;
101 }
102
103
104 static void parse_cpu_im_init(sg_platf_host_cbarg_t host)
105 {
106         cpu_im_create_resource(host->id,
107                           host->power_peak,
108                           host->power_scale,
109                           host->power_trace,
110                           host->core_amount,
111                           host->initial_state,
112                           host->state_trace,
113                           host->properties);
114 }
115
116 static void cpu_im_add_traces_cpu(void)
117 {
118   xbt_dict_cursor_t cursor = NULL;
119   char *trace_name, *elm;
120   static int called = 0;
121   if (called)
122     return;
123   called = 1;
124
125   /* connect all traces relative to hosts */
126   xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
127     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
128     cpu_Cas01_im_t host = surf_cpu_resource_by_name(elm);
129
130     xbt_assert(host, "Host %s undefined", elm);
131     xbt_assert(trace, "Trace %s undefined", trace_name);
132
133     host->state_event =
134         tmgr_history_add_trace(history, trace, 0.0, 0, host);
135   }
136
137   xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
138     tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
139     cpu_Cas01_im_t host = surf_cpu_resource_by_name(elm);
140
141     xbt_assert(host, "Host %s undefined", elm);
142     xbt_assert(trace, "Trace %s undefined", trace_name);
143
144     host->power_event =
145         tmgr_history_add_trace(history, trace, 0.0, 0, host);
146   }
147 }
148
149 static void cpu_im_define_callbacks()
150 {
151   sg_platf_host_add_cb(parse_cpu_im_init);
152   sg_platf_postparse_add_cb(cpu_im_add_traces_cpu);
153 }
154
155 static int cpu_im_resource_used(void *resource)
156 {
157   return lmm_constraint_used(cpu_im_maxmin_system,
158                              ((cpu_Cas01_im_t) resource)->constraint);
159 }
160
161 static int cpu_im_action_unref(surf_action_t action)
162 {
163   action->refcount--;
164   if (!action->refcount) {
165     xbt_swag_remove(action, action->state_set);
166     if (((surf_action_lmm_t) action)->variable)
167       lmm_variable_free(cpu_im_maxmin_system,
168                         ((surf_action_lmm_t) action)->variable);
169     if(update_mechanism == UM_FULL){
170     /* remove from heap */
171     xbt_heap_remove(cpu_im_action_heap,
172                     ((surf_action_cpu_Cas01_im_t) action)->index_heap);
173     xbt_swag_remove(action,
174                     ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
175     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
176     }
177 #ifdef HAVE_TRACING
178     xbt_free(action->category);
179 #endif
180     surf_action_free(&action);
181     return 1;
182   }
183   return 0;
184 }
185
186 static void cpu_im_action_cancel(surf_action_t action)
187 {
188   surf_action_state_set(action, SURF_ACTION_FAILED);
189   if(update_mechanism == UM_FULL){
190   xbt_heap_remove(cpu_im_action_heap,
191                   ((surf_action_cpu_Cas01_im_t) action)->index_heap);
192   xbt_swag_remove(action,
193                   ((cpu_Cas01_im_t) ACTION_GET_CPU(action))->action_set);
194   }
195   return;
196 }
197
198 static void cpu_im_cpu_action_state_set(surf_action_t action,
199                                         e_surf_action_state_t state)
200 {
201 /*   if((state==SURF_ACTION_DONE) || (state==SURF_ACTION_FAILED)) */
202 /*     if(((surf_action_lmm_t)action)->variable) { */
203 /*       lmm_variable_disable(cpu_im_maxmin_system, ((surf_action_lmm_t)action)->variable); */
204 /*       ((surf_action_lmm_t)action)->variable = NULL; */
205 /*     } */
206
207   surf_action_state_set(action, state);
208   return;
209 }
210
211 static void cpu_im_update_remains(cpu_Cas01_im_t cpu, double now)
212 {
213   surf_action_cpu_Cas01_im_t action;
214
215   if (cpu->last_update >= now)
216     return;
217   xbt_swag_foreach(action, cpu->action_set) {
218     if (GENERIC_ACTION(action).state_set !=
219         surf_cpu_model->states.running_action_set)
220       continue;
221
222     /* bogus priority, skip it */
223     if (GENERIC_ACTION(action).priority <= 0)
224       continue;
225
226     if (GENERIC_ACTION(action).remains > 0) {
227       double_update(&(GENERIC_ACTION(action).remains),
228                     lmm_variable_getvalue(GENERIC_LMM_ACTION
229                                           (action).variable) * (now -
230                                                                 cpu->last_update));
231 #ifdef HAVE_TRACING
232       if (TRACE_is_enabled()) {
233         TRACE_surf_host_set_utilization(cpu->generic_resource.name,
234                                         action->
235                                         generic_lmm_action.generic_action.
236                                         data, (surf_action_t) action,
237                                         lmm_variable_getvalue
238                                         (GENERIC_LMM_ACTION
239                                          (action).variable),
240                                         cpu->last_update,
241                                         now - cpu->last_update);
242       }
243 #endif
244       XBT_DEBUG("Update action(%p) remains %lf", action,
245              GENERIC_ACTION(action).remains);
246     }
247   }
248   cpu->last_update = now;
249 }
250
251 static double cpu_im_share_resources(double now)
252 {
253   surf_action_cpu_Cas01_im_t action;
254   double min;
255   double value;
256   cpu_Cas01_im_t cpu, cpu_next;
257
258   xbt_swag_foreach(cpu, cpu_im_modified_cpu)
259       cpu_im_update_remains(cpu, now);
260
261   lmm_solve(cpu_im_maxmin_system);
262
263   xbt_swag_foreach_safe(cpu, cpu_next, cpu_im_modified_cpu) {
264     xbt_swag_foreach(action, cpu->action_set) {
265       if (GENERIC_ACTION(action).state_set !=
266           surf_cpu_model->states.running_action_set)
267         continue;
268
269       /* bogus priority, skip it */
270       if (GENERIC_ACTION(action).priority <= 0)
271         continue;
272
273       min = -1;
274       value = lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable);
275       if (value > 0) {
276         if (GENERIC_ACTION(action).remains > 0) {
277           value = GENERIC_ACTION(action).remains / value;
278           min = now + value;
279         } else {
280           value = 0.0;
281           min = now;
282         }
283       }
284
285       if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION)
286           && (min == -1
287               || GENERIC_ACTION(action).start +
288               GENERIC_ACTION(action).max_duration < min))
289         min =
290             GENERIC_ACTION(action).start +
291             GENERIC_ACTION(action).max_duration;
292
293       XBT_DEBUG("Action(%p) Start %lf Finish %lf Max_duration %lf", action,
294              GENERIC_ACTION(action).start, now + value,
295              GENERIC_ACTION(action).max_duration);
296
297       if (action->index_heap >= 0) {
298         surf_action_cpu_Cas01_im_t heap_act =
299             xbt_heap_remove(cpu_im_action_heap, action->index_heap);
300         if (heap_act != action)
301           DIE_IMPOSSIBLE;
302       }
303       if (min != -1) {
304         xbt_heap_push(cpu_im_action_heap, action, min);
305         XBT_DEBUG("Insert at heap action(%p) min %lf", action, min);
306       }
307     }
308     xbt_swag_remove(cpu, cpu_im_modified_cpu);
309   }
310   return xbt_heap_size(cpu_im_action_heap) >
311       0 ? xbt_heap_maxkey(cpu_im_action_heap) - now : -1;
312 }
313
314 static void cpu_im_update_actions_state(double now, double delta)
315 {
316   surf_action_cpu_Cas01_im_t action;
317   while ((xbt_heap_size(cpu_im_action_heap) > 0)
318          && (double_equals(xbt_heap_maxkey(cpu_im_action_heap), now))) {
319     action = xbt_heap_pop(cpu_im_action_heap);
320     XBT_DEBUG("Action %p: finish", action);
321     GENERIC_ACTION(action).finish = surf_get_clock();
322     /* set the remains to 0 due to precision problems when updating the remaining amount */
323 #ifdef HAVE_TRACING
324     if (TRACE_is_enabled()) {
325       cpu_Cas01_im_t cpu = ((cpu_Cas01_im_t)(action->cpu));
326       TRACE_surf_host_set_utilization(cpu->generic_resource.name,
327           GENERIC_LMM_ACTION(action).generic_action.data,
328           (surf_action_t) action,
329           lmm_variable_getvalue (GENERIC_LMM_ACTION(action).variable),
330           cpu->last_update,
331           now - cpu->last_update);
332     }
333 #endif
334     GENERIC_ACTION(action).remains = 0;
335     cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
336     cpu_im_update_remains(action->cpu, surf_get_clock());
337   }
338 #ifdef HAVE_TRACING
339   if (TRACE_is_enabled()) {
340     //defining the last timestamp that we can safely dump to trace file
341     //without losing the event ascending order (considering all CPU's)
342     void **data;
343     cpu_Cas01_im_t cpu;
344     xbt_lib_cursor_t cursor;
345     char *key;
346     double smaller = -1;
347     xbt_lib_foreach(host_lib, cursor, key, data){
348       if(data[SURF_CPU_LEVEL]){
349         cpu = data[SURF_CPU_LEVEL];
350         if (smaller < 0){
351           smaller = cpu->last_update;
352           continue;
353         }
354         if (cpu->last_update < smaller){
355           smaller = cpu->last_update;
356         }
357       }
358     }
359     if (smaller > 0) {
360       TRACE_last_timestamp_to_dump = smaller;
361     }
362   }
363 #endif
364   return;
365 }
366
367 static void cpu_im_update_resource_state(void *id,
368                                          tmgr_trace_event_t event_type,
369                                          double value, double date)
370 {
371   cpu_Cas01_im_t cpu = id;
372   lmm_variable_t var = NULL;
373   lmm_element_t elem = NULL;
374
375   if (event_type == cpu->power_event) {
376     cpu->power_scale = value;
377     lmm_update_constraint_bound(cpu_im_maxmin_system, cpu->constraint,
378                                 cpu->core * cpu->power_scale * cpu->power_peak);
379 #ifdef HAVE_TRACING
380     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
381                               cpu->core * cpu->power_scale * cpu->power_peak);
382 #endif
383     while ((var = lmm_get_var_from_cnst
384             (cpu_im_maxmin_system, cpu->constraint, &elem))) {
385         surf_action_cpu_Cas01_im_t action = lmm_variable_id(var);
386         lmm_update_variable_bound(cpu_im_maxmin_system, GENERIC_LMM_ACTION(action).variable,
387                                   cpu->power_scale * cpu->power_peak);
388     }
389     if(update_mechanism == UM_FULL)
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   if(update_mechanism == UM_FULL){
444     action->index_heap = -1;
445     action->cpu = CPU;
446     xbt_swag_insert(CPU, cpu_im_modified_cpu);
447     xbt_swag_insert(action, CPU->action_set);
448   }
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_IN("(%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   if(update_mechanism == UM_FULL)
478     xbt_swag_insert(cpu, cpu_im_modified_cpu);
479   XBT_OUT();
480   return (surf_action_t) action;
481 }
482
483 static void cpu_im_action_suspend(surf_action_t action)
484 {
485   XBT_IN("(%p)", action);
486   if (((surf_action_lmm_t) action)->suspended != 2) {
487     lmm_update_variable_weight(cpu_im_maxmin_system,
488                                ((surf_action_lmm_t) action)->variable,
489                                0.0);
490     ((surf_action_lmm_t) action)->suspended = 1;
491     if(update_mechanism == UM_FULL){
492       xbt_heap_remove(cpu_im_action_heap,
493                       ((surf_action_cpu_Cas01_im_t) action)->index_heap);
494       xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
495     }
496   }
497   XBT_OUT();
498 }
499
500 static void cpu_im_action_resume(surf_action_t action)
501 {
502
503   XBT_IN("(%p)", action);
504   if (((surf_action_lmm_t) action)->suspended != 2) {
505     lmm_update_variable_weight(cpu_im_maxmin_system,
506                                ((surf_action_lmm_t) action)->variable,
507                                action->priority);
508     ((surf_action_lmm_t) action)->suspended = 0;
509     if(update_mechanism == UM_FULL)
510       xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
511   }
512   XBT_OUT();
513 }
514
515 static int cpu_im_action_is_suspended(surf_action_t action)
516 {
517   return (((surf_action_lmm_t) action)->suspended == 1);
518 }
519
520 static void cpu_im_action_set_max_duration(surf_action_t action,
521                                            double duration)
522 {
523   XBT_IN("(%p,%g)", action, duration);
524
525   action->max_duration = duration;
526   /* insert cpu in modified_cpu set to notice the max duration change */
527   if(update_mechanism == UM_FULL)
528     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
529   XBT_OUT();
530 }
531
532 static void cpu_im_action_set_priority(surf_action_t action,
533                                        double priority)
534 {
535   XBT_IN("(%p,%g)", action, priority);
536   action->priority = priority;
537   lmm_update_variable_weight(cpu_im_maxmin_system,
538                              ((surf_action_lmm_t) action)->variable,
539                              priority);
540
541   if(update_mechanism == UM_FULL)
542     xbt_swag_insert(ACTION_GET_CPU(action), cpu_im_modified_cpu);
543   XBT_OUT();
544 }
545
546 #ifdef HAVE_TRACING
547 static void cpu_im_action_set_category(surf_action_t action,
548                                        const char *category)
549 {
550   XBT_IN("(%p,%s)", action, category);
551   action->category = xbt_strdup (category);
552   XBT_OUT();
553 }
554 #endif
555
556 static double cpu_im_action_get_remains(surf_action_t action)
557 {
558   XBT_IN("(%p)", action);
559   /* update remains before return it */
560   if(update_mechanism == UM_FULL)
561     cpu_im_update_remains(ACTION_GET_CPU(action), surf_get_clock());
562   return action->remains;
563   XBT_OUT();
564 }
565
566 static e_surf_resource_state_t cpu_im_get_state(void *cpu)
567 {
568   return ((cpu_Cas01_im_t) cpu)->state_current;
569 }
570
571 static double cpu_im_get_speed(void *cpu, double load)
572 {
573   return load * (((cpu_Cas01_im_t) cpu)->power_peak);
574 }
575
576 static double cpu_im_get_available_speed(void *cpu)
577 {
578   /* number between 0 and 1 */
579   return ((cpu_Cas01_im_t) cpu)->power_scale;
580 }
581
582 static void cpu_im_action_update_index_heap(void *action, int i)
583 {
584   ((surf_action_cpu_Cas01_im_t) action)->index_heap = i;
585 }
586
587 static void cpu_im_finalize(void)
588 {
589   void **cpu;
590   xbt_lib_cursor_t cursor;
591   char *key;
592
593   xbt_lib_foreach(host_lib, cursor, key, cpu){
594     if(cpu[SURF_CPU_LEVEL])
595     {
596         cpu_Cas01_im_t CPU = cpu[SURF_CPU_LEVEL];
597         xbt_swag_free(CPU->action_set);
598     }
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   if(cpu_im_action_heap)  xbt_heap_free(cpu_im_action_heap);
611   if(cpu_im_modified_cpu) xbt_swag_free(cpu_im_modified_cpu);
612 }
613
614 static void surf_cpu_im_model_init_internal(const char* name)
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 = name;
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 = generic_share_resources;
632   surf_cpu_model->model_private->update_actions_state =
633       generic_update_actions_state;//ok
634   surf_cpu_model->model_private->update_resource_state =
635       cpu_im_update_resource_state;//ok
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   if(update_mechanism == UM_FULL){
663     cpu_im_action_heap = xbt_heap_new(8, NULL);
664     xbt_heap_set_update_callback(cpu_im_action_heap,
665                                  cpu_im_action_update_index_heap);
666     cpu_im_modified_cpu =
667         xbt_swag_new(xbt_swag_offset(cpu, modified_cpu_hookup));
668   }
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
687 void surf_cpu_model_init_Cas01_im()
688 {
689   char* name;
690   if( strcmp(xbt_cfg_get_string(_surf_cfg_set, "cpu/model"),"Cas01"))
691     update_mechanism = UM_FULL;
692   else
693     update_mechanism = UM_LAZY;
694
695   if (surf_cpu_model)
696     return;
697   surf_cpu_im_model_init_internal("CPU");
698   cpu_im_define_callbacks();
699   xbt_dynar_push(model_list, &surf_cpu_model);
700 }
701
702 double generic_share_resources(double now)
703 {
704   surf_action_cpu_Cas01_im_t action;
705   if(update_mechanism == UM_FULL)
706     return cpu_im_share_resources(now);
707   else if (update_mechanism == UM_LAZY)
708   {
709     return generic_maxmin_share_resources(surf_cpu_model->states.running_action_set,
710         xbt_swag_offset(*action, generic_lmm_action.variable),
711         cpu_im_maxmin_system, lmm_solve);
712   } else {
713     xbt_die("Invalide update mechanism!");
714     return 0;
715   }
716 }
717
718 static void cpu_update_actions_state(double now, double delta)
719 {
720   surf_action_cpu_Cas01_im_t action = NULL;
721   surf_action_cpu_Cas01_im_t next_action = NULL;
722   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
723   xbt_swag_foreach_safe(action, next_action, running_actions) {
724 #ifdef HAVE_TRACING
725     if (TRACE_is_enabled()) {
726       cpu_Cas01_im_t x =
727         lmm_constraint_id(lmm_get_cnst_from_var
728                           (cpu_maxmin_system, GENERIC_LMM_ACTION(action).variable, 0));
729
730       TRACE_surf_host_set_utilization(x->generic_resource.name,
731                                       GENERIC_ACTION(action).data,
732                                       (surf_action_t) action,
733                                       lmm_variable_getvalue
734                                       (GENERIC_LMM_ACTION(action).variable), now - delta,
735                                       delta);
736       TRACE_last_timestamp_to_dump = now-delta;
737     }
738 #endif
739     double_update(&(GENERIC_ACTION(action).remains),
740                   lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable) * delta);
741     if (GENERIC_LMM_ACTION(action).generic_action.max_duration != NO_MAX_DURATION)
742       double_update(&(GENERIC_ACTION(action).max_duration), delta);
743     if ((GENERIC_ACTION(action).remains <= 0) &&
744         (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) > 0)) {
745       GENERIC_ACTION(action).finish = surf_get_clock();
746       cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
747     } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
748                (GENERIC_ACTION(action).max_duration <= 0)) {
749       GENERIC_ACTION(action).finish = surf_get_clock();
750       cpu_im_cpu_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
751     }
752   }
753
754   return;
755 }
756
757 void generic_update_actions_state(double now, double delta)
758 {
759   if(update_mechanism == UM_FULL)
760     cpu_im_update_actions_state(now, delta);
761   else if(update_mechanism == UM_LAZY)
762   {
763     cpu_update_actions_state(now, delta);
764   }
765   else
766     xbt_die("Not recognized surf cpu model!");
767 }