Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Forget to remove this line from previous commit
[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 double cpu_share_resources_lazy(double now)
143 {
144   return generic_share_resources_lazy(now, surf_cpu_model);
145 }
146
147 static double cpu_share_resources_full(double now)
148 {
149   s_surf_action_cpu_Cas01_t action;
150   return generic_maxmin_share_resources(surf_cpu_model->states.
151                                         running_action_set,
152                                         xbt_swag_offset(action,
153                                                         generic_lmm_action.
154                                                         variable),
155                                         surf_cpu_model->model_private->maxmin_system, lmm_solve);
156 }
157
158 static void cpu_update_actions_state_lazy(double now, double delta)
159 {
160   surf_action_cpu_Cas01_t action;
161   while ((xbt_heap_size(surf_cpu_model->model_private->action_heap) > 0)
162          && (double_equals(xbt_heap_maxkey(surf_cpu_model->model_private->action_heap), now))) {
163     action = xbt_heap_pop(surf_cpu_model->model_private->action_heap);
164     XBT_DEBUG("Action %p: finish", action);
165     GENERIC_ACTION(action).finish = surf_get_clock();
166 #ifdef HAVE_TRACING
167     if (TRACE_is_enabled()) {
168       cpu_Cas01_t cpu =
169           lmm_constraint_id(lmm_get_cnst_from_var
170                             (surf_cpu_model->model_private->maxmin_system,
171                              GENERIC_LMM_ACTION(action).variable, 0));
172       TRACE_surf_host_set_utilization(cpu->generic_resource.name,
173                                       ((surf_action_t)action)->category,
174                                       lmm_variable_getvalue(GENERIC_LMM_ACTION(action).variable),
175                                       GENERIC_LMM_ACTION(action).last_update,
176                                       now - GENERIC_LMM_ACTION(action).last_update);
177     }
178 #endif
179     /* set the remains to 0 due to precision problems when updating the remaining amount */
180     GENERIC_ACTION(action).remains = 0;
181     surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
182     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
183   }
184 #ifdef HAVE_TRACING
185   if (TRACE_is_enabled()) {
186     //defining the last timestamp that we can safely dump to trace file
187     //without losing the event ascending order (considering all CPU's)
188     double smaller = -1;
189     xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
190     xbt_swag_foreach(action, running_actions) {
191         if (smaller < 0) {
192           smaller = GENERIC_LMM_ACTION(action).last_update;
193           continue;
194         }
195         if (GENERIC_LMM_ACTION(action).last_update < smaller) {
196           smaller = GENERIC_LMM_ACTION(action).last_update;
197         }
198     }
199     if (smaller > 0) {
200       TRACE_last_timestamp_to_dump = smaller;
201     }
202   }
203 #endif
204   return;
205 }
206
207 static void cpu_update_actions_state_full(double now, double delta)
208 {
209   surf_action_cpu_Cas01_t action = NULL;
210   surf_action_cpu_Cas01_t next_action = NULL;
211   xbt_swag_t running_actions = surf_cpu_model->states.running_action_set;
212   xbt_swag_foreach_safe(action, next_action, running_actions) {
213 #ifdef HAVE_TRACING
214     if (TRACE_is_enabled()) {
215       cpu_Cas01_t x =
216           lmm_constraint_id(lmm_get_cnst_from_var
217                             (surf_cpu_model->model_private->maxmin_system,
218                              GENERIC_LMM_ACTION(action).variable, 0));
219
220       TRACE_surf_host_set_utilization(x->generic_resource.name,
221                                       ((surf_action_t)action)->category,
222                                       lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
223                                        variable),
224                                       now - delta,
225                                       delta);
226       TRACE_last_timestamp_to_dump = now - delta;
227     }
228 #endif
229     double_update(&(GENERIC_ACTION(action).remains),
230                   lmm_variable_getvalue(GENERIC_LMM_ACTION(action).
231                                         variable) * delta);
232     if (GENERIC_LMM_ACTION(action).generic_action.max_duration !=
233         NO_MAX_DURATION)
234       double_update(&(GENERIC_ACTION(action).max_duration), delta);
235     if ((GENERIC_ACTION(action).remains <= 0) &&
236         (lmm_get_variable_weight(GENERIC_LMM_ACTION(action).variable) >
237          0)) {
238       GENERIC_ACTION(action).finish = surf_get_clock();
239       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
240     } else if ((GENERIC_ACTION(action).max_duration != NO_MAX_DURATION) &&
241                (GENERIC_ACTION(action).max_duration <= 0)) {
242       GENERIC_ACTION(action).finish = surf_get_clock();
243       surf_action_state_set((surf_action_t) action, SURF_ACTION_DONE);
244     }
245   }
246
247   return;
248 }
249
250 static void cpu_update_resource_state(void *id,
251                                       tmgr_trace_event_t event_type,
252                                       double value, double date)
253 {
254   cpu_Cas01_t cpu = id;
255   lmm_variable_t var = NULL;
256   lmm_element_t elem = NULL;
257
258   if (event_type == cpu->power_event) {
259     cpu->power_scale = value;
260     lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
261                                 cpu->core * cpu->power_scale *
262                                 cpu->power_peak);
263 #ifdef HAVE_TRACING
264     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
265                               cpu->core * cpu->power_scale *
266                               cpu->power_peak);
267 #endif
268     while ((var = lmm_get_var_from_cnst
269             (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
270       surf_action_cpu_Cas01_t action = lmm_variable_id(var);
271       lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
272                                 GENERIC_LMM_ACTION(action).variable,
273                                 cpu->power_scale * cpu->power_peak);
274     }
275     if (tmgr_trace_event_free(event_type))
276       cpu->power_event = NULL;
277   } else if (event_type == cpu->state_event) {
278     if (value > 0)
279       cpu->state_current = SURF_RESOURCE_ON;
280     else {
281       lmm_constraint_t cnst = cpu->constraint;
282
283       cpu->state_current = SURF_RESOURCE_OFF;
284
285       while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
286         surf_action_t action = lmm_variable_id(var);
287
288         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
289             surf_action_state_get(action) == SURF_ACTION_READY ||
290             surf_action_state_get(action) ==
291             SURF_ACTION_NOT_IN_THE_SYSTEM) {
292           action->finish = date;
293           surf_action_state_set(action, SURF_ACTION_FAILED);
294         }
295       }
296     }
297     if (tmgr_trace_event_free(event_type))
298       cpu->state_event = NULL;
299   } else {
300     XBT_CRITICAL("Unknown event ! \n");
301     xbt_abort();
302   }
303
304   return;
305 }
306
307 static surf_action_t cpu_execute(void *cpu, double size)
308 {
309   surf_action_cpu_Cas01_t action = NULL;
310   cpu_Cas01_t CPU = cpu;
311
312   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
313   action =
314       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
315                       surf_cpu_model,
316                       CPU->state_current != SURF_RESOURCE_ON);
317
318   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
319                                                    calloc but it seems to help valgrind... */
320
321   GENERIC_LMM_ACTION(action).variable =
322       lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
323                        GENERIC_ACTION(action).priority,
324                        CPU->power_scale * CPU->power_peak, 1);
325   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
326     GENERIC_LMM_ACTION(action).index_heap = -1;
327     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
328     GENERIC_LMM_ACTION(action).last_value = 0.0;
329   }
330   lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
331              GENERIC_LMM_ACTION(action).variable, 1.0);
332   XBT_OUT();
333   return (surf_action_t) action;
334 }
335
336 static surf_action_t cpu_action_sleep(void *cpu, double duration)
337 {
338   surf_action_cpu_Cas01_t action = NULL;
339
340   if (duration > 0)
341     duration = MAX(duration, MAXMIN_PRECISION);
342
343   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
344   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
345   // FIXME: sleep variables should not consume 1.0 in lmm_expand
346   GENERIC_ACTION(action).max_duration = duration;
347   GENERIC_LMM_ACTION(action).suspended = 2;
348   if (duration == NO_MAX_DURATION) {
349     /* Move to the *end* of the corresponding action set. This convention
350        is used to speed up update_resource_state  */
351     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
352     ((surf_action_t) action)->state_set =
353         cpu_running_action_set_that_does_not_need_being_checked;
354     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
355   }
356
357   lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
358                              GENERIC_LMM_ACTION(action).variable, 0.0);
359   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {     // remove action from the heap
360     surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
361     // this is necessary for a variable with weight 0 since such
362     // variables are ignored in lmm and we need to set its max_duration
363     // correctly at the next call to share_resources
364     xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
365   }
366
367   XBT_OUT();
368   return (surf_action_t) action;
369 }
370
371 static e_surf_resource_state_t cpu_get_state(void *cpu)
372 {
373   return ((cpu_Cas01_t) cpu)->state_current;
374 }
375
376 static double cpu_get_speed(void *cpu, double load)
377 {
378   return load * (((cpu_Cas01_t) cpu)->power_peak);
379 }
380
381 static double cpu_get_available_speed(void *cpu)
382 {
383   /* number between 0 and 1 */
384   return ((cpu_Cas01_t) cpu)->power_scale;
385 }
386
387 static void cpu_finalize(void)
388 {
389   lmm_system_free(surf_cpu_model->model_private->maxmin_system);
390   surf_cpu_model->model_private->maxmin_system = NULL;
391
392   if (surf_cpu_model->model_private->action_heap)
393     xbt_heap_free(surf_cpu_model->model_private->action_heap);
394   xbt_swag_free(surf_cpu_model->model_private->modified_set);
395
396   surf_model_exit(surf_cpu_model);
397   surf_cpu_model = NULL;
398
399   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
400   cpu_running_action_set_that_does_not_need_being_checked = NULL;
401 }
402
403 static void surf_cpu_model_init_internal()
404 {
405   s_surf_action_t action;
406   s_surf_action_cpu_Cas01_t comp;
407
408   char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
409   int select =
410       xbt_cfg_get_int(_surf_cfg_set, "cpu/maxmin_selective_update");
411
412   surf_cpu_model = surf_model_init();
413
414   if (!strcmp(optim, "Full")) {
415     surf_cpu_model->model_private->update_mechanism = UM_FULL;
416     surf_cpu_model->model_private->selective_update = select;
417   } else if (!strcmp(optim, "Lazy")) {
418     surf_cpu_model->model_private->update_mechanism = UM_LAZY;
419     surf_cpu_model->model_private->selective_update = 1;
420     xbt_assert((select == 1)
421                ||
422                (xbt_cfg_is_default_value
423                 (_surf_cfg_set, "cpu/maxmin_selective_update")),
424                "Disabling selective update while using the lazy update mechanism is dumb!");
425   } else {
426     xbt_die("Unsupported optimization (%s) for this model", optim);
427   }
428
429   cpu_running_action_set_that_does_not_need_being_checked =
430       xbt_swag_new(xbt_swag_offset(action, state_hookup));
431
432   surf_cpu_model->name = "cpu";
433
434   surf_cpu_model->action_unref = surf_action_unref;
435   surf_cpu_model->action_cancel = surf_action_cancel;
436   surf_cpu_model->action_state_set = surf_action_state_set;
437
438   surf_cpu_model->model_private->resource_used = cpu_resource_used;
439
440   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
441     surf_cpu_model->model_private->share_resources =
442         cpu_share_resources_lazy;
443     surf_cpu_model->model_private->update_actions_state =
444         cpu_update_actions_state_lazy;
445   } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
446     surf_cpu_model->model_private->share_resources =
447         cpu_share_resources_full;
448     surf_cpu_model->model_private->update_actions_state =
449         cpu_update_actions_state_full;
450   } else
451     xbt_die("Invalid cpu update mechanism!");
452
453   surf_cpu_model->model_private->update_resource_state =
454       cpu_update_resource_state;
455   surf_cpu_model->model_private->finalize = cpu_finalize;
456
457   surf_cpu_model->suspend = surf_action_suspend;
458   surf_cpu_model->resume = surf_action_resume;
459   surf_cpu_model->is_suspended = surf_action_is_suspended;
460   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
461   surf_cpu_model->set_priority = surf_action_set_priority;
462 #ifdef HAVE_TRACING
463   surf_cpu_model->set_category = surf_action_set_category;
464 #endif
465   surf_cpu_model->get_remains = surf_action_get_remains;
466
467   surf_cpu_model->extension.cpu.execute = cpu_execute;
468   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
469
470   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
471   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
472   surf_cpu_model->extension.cpu.get_available_speed =
473       cpu_get_available_speed;
474   surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
475   surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
476
477   if (!surf_cpu_model->model_private->maxmin_system) {
478     surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
479   }
480   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
481     surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
482     xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
483         surf_action_lmm_update_index_heap);
484     surf_cpu_model->model_private->modified_set =
485         xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
486     surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
487   }
488 }
489
490 /*********************************************************************/
491 /* Basic sharing model for CPU: that is where all this started... ;) */
492 /*********************************************************************/
493 /* @InProceedings{casanova01simgrid, */
494 /*   author =       "H. Casanova", */
495 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
496 /*                  and the Grid (CCGrid'01)", */
497 /*   publisher =    "IEEE Computer Society", */
498 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
499 /*                  Scheduling", */
500 /*   year =         "2001", */
501 /*   month =        may, */
502 /*   note =         "Available at */
503 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
504 /* } */
505
506 void surf_cpu_model_init_Cas01()
507 {
508   char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
509
510   if (surf_cpu_model)
511     return;
512
513   if (!strcmp(optim, "TI")) {
514     surf_cpu_model_init_ti();
515     return;
516   }
517
518   surf_cpu_model_init_internal();
519   cpu_define_callbacks();
520   xbt_dynar_push(model_list, &surf_cpu_model);
521 }