Logo AND Algorithmique Numérique Distribuée

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