Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[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   generic_update_actions_state_lazy(now, delta, surf_cpu_model);
161 }
162
163 static void cpu_update_actions_state_full(double now, double delta)
164 {
165   generic_update_actions_state_full(now, delta, surf_cpu_model);
166 }
167
168 static void cpu_update_resource_state(void *id,
169                                       tmgr_trace_event_t event_type,
170                                       double value, double date)
171 {
172   cpu_Cas01_t cpu = id;
173   lmm_variable_t var = NULL;
174   lmm_element_t elem = NULL;
175
176   if (event_type == cpu->power_event) {
177     cpu->power_scale = value;
178     lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
179                                 cpu->core * cpu->power_scale *
180                                 cpu->power_peak);
181 #ifdef HAVE_TRACING
182     TRACE_surf_host_set_power(date, cpu->generic_resource.name,
183                               cpu->core * cpu->power_scale *
184                               cpu->power_peak);
185 #endif
186     while ((var = lmm_get_var_from_cnst
187             (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
188       surf_action_cpu_Cas01_t action = lmm_variable_id(var);
189       lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
190                                 GENERIC_LMM_ACTION(action).variable,
191                                 cpu->power_scale * cpu->power_peak);
192     }
193     if (tmgr_trace_event_free(event_type))
194       cpu->power_event = NULL;
195   } else if (event_type == cpu->state_event) {
196     if (value > 0)
197       cpu->state_current = SURF_RESOURCE_ON;
198     else {
199       lmm_constraint_t cnst = cpu->constraint;
200
201       cpu->state_current = SURF_RESOURCE_OFF;
202
203       while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
204         surf_action_t action = lmm_variable_id(var);
205
206         if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
207             surf_action_state_get(action) == SURF_ACTION_READY ||
208             surf_action_state_get(action) ==
209             SURF_ACTION_NOT_IN_THE_SYSTEM) {
210           action->finish = date;
211           surf_action_state_set(action, SURF_ACTION_FAILED);
212         }
213       }
214     }
215     if (tmgr_trace_event_free(event_type))
216       cpu->state_event = NULL;
217   } else {
218     XBT_CRITICAL("Unknown event ! \n");
219     xbt_abort();
220   }
221
222   return;
223 }
224
225 static surf_action_t cpu_execute(void *cpu, double size)
226 {
227   surf_action_cpu_Cas01_t action = NULL;
228   cpu_Cas01_t CPU = cpu;
229
230   XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
231   action =
232       surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
233                       surf_cpu_model,
234                       CPU->state_current != SURF_RESOURCE_ON);
235
236   GENERIC_LMM_ACTION(action).suspended = 0;     /* Should be useless because of the
237                                                    calloc but it seems to help valgrind... */
238
239   GENERIC_LMM_ACTION(action).variable =
240       lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
241                        GENERIC_ACTION(action).priority,
242                        CPU->power_scale * CPU->power_peak, 1);
243   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
244     GENERIC_LMM_ACTION(action).index_heap = -1;
245     GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
246     GENERIC_LMM_ACTION(action).last_value = 0.0;
247   }
248   lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
249              GENERIC_LMM_ACTION(action).variable, 1.0);
250   XBT_OUT();
251   return (surf_action_t) action;
252 }
253
254 static surf_action_t cpu_action_sleep(void *cpu, double duration)
255 {
256   surf_action_cpu_Cas01_t action = NULL;
257
258   if (duration > 0)
259     duration = MAX(duration, MAXMIN_PRECISION);
260
261   XBT_IN("(%s,%g)", surf_resource_name(cpu), duration);
262   action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
263   // FIXME: sleep variables should not consume 1.0 in lmm_expand
264   GENERIC_ACTION(action).max_duration = duration;
265   GENERIC_LMM_ACTION(action).suspended = 2;
266   if (duration == NO_MAX_DURATION) {
267     /* Move to the *end* of the corresponding action set. This convention
268        is used to speed up update_resource_state  */
269     xbt_swag_remove(action, ((surf_action_t) action)->state_set);
270     ((surf_action_t) action)->state_set =
271         cpu_running_action_set_that_does_not_need_being_checked;
272     xbt_swag_insert(action, ((surf_action_t) action)->state_set);
273   }
274
275   lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
276                              GENERIC_LMM_ACTION(action).variable, 0.0);
277   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {     // remove action from the heap
278     surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
279     // this is necessary for a variable with weight 0 since such
280     // variables are ignored in lmm and we need to set its max_duration
281     // correctly at the next call to share_resources
282     xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
283   }
284
285   XBT_OUT();
286   return (surf_action_t) action;
287 }
288
289 static e_surf_resource_state_t cpu_get_state(void *cpu)
290 {
291   return ((cpu_Cas01_t) cpu)->state_current;
292 }
293
294 static double cpu_get_speed(void *cpu, double load)
295 {
296   return load * (((cpu_Cas01_t) cpu)->power_peak);
297 }
298
299 static double cpu_get_available_speed(void *cpu)
300 {
301   /* number between 0 and 1 */
302   return ((cpu_Cas01_t) cpu)->power_scale;
303 }
304
305 static void cpu_finalize(void)
306 {
307   lmm_system_free(surf_cpu_model->model_private->maxmin_system);
308   surf_cpu_model->model_private->maxmin_system = NULL;
309
310   if (surf_cpu_model->model_private->action_heap)
311     xbt_heap_free(surf_cpu_model->model_private->action_heap);
312   xbt_swag_free(surf_cpu_model->model_private->modified_set);
313
314   surf_model_exit(surf_cpu_model);
315   surf_cpu_model = NULL;
316
317   xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
318   cpu_running_action_set_that_does_not_need_being_checked = NULL;
319 }
320
321 static void surf_cpu_model_init_internal()
322 {
323   s_surf_action_t action;
324   s_surf_action_cpu_Cas01_t comp;
325
326   char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
327   int select =
328       xbt_cfg_get_int(_surf_cfg_set, "cpu/maxmin_selective_update");
329
330   surf_cpu_model = surf_model_init();
331
332   if (!strcmp(optim, "Full")) {
333     surf_cpu_model->model_private->update_mechanism = UM_FULL;
334     surf_cpu_model->model_private->selective_update = select;
335   } else if (!strcmp(optim, "Lazy")) {
336     surf_cpu_model->model_private->update_mechanism = UM_LAZY;
337     surf_cpu_model->model_private->selective_update = 1;
338     xbt_assert((select == 1)
339                ||
340                (xbt_cfg_is_default_value
341                 (_surf_cfg_set, "cpu/maxmin_selective_update")),
342                "Disabling selective update while using the lazy update mechanism is dumb!");
343   } else {
344     xbt_die("Unsupported optimization (%s) for this model", optim);
345   }
346
347   cpu_running_action_set_that_does_not_need_being_checked =
348       xbt_swag_new(xbt_swag_offset(action, state_hookup));
349
350   surf_cpu_model->name = "cpu";
351
352   surf_cpu_model->action_unref = surf_action_unref;
353   surf_cpu_model->action_cancel = surf_action_cancel;
354   surf_cpu_model->action_state_set = surf_action_state_set;
355
356   surf_cpu_model->model_private->resource_used = cpu_resource_used;
357
358   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
359     surf_cpu_model->model_private->share_resources =
360         cpu_share_resources_lazy;
361     surf_cpu_model->model_private->update_actions_state =
362         cpu_update_actions_state_lazy;
363   } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
364     surf_cpu_model->model_private->share_resources =
365         cpu_share_resources_full;
366     surf_cpu_model->model_private->update_actions_state =
367         cpu_update_actions_state_full;
368   } else
369     xbt_die("Invalid cpu update mechanism!");
370
371   surf_cpu_model->model_private->update_resource_state =
372       cpu_update_resource_state;
373   surf_cpu_model->model_private->finalize = cpu_finalize;
374
375   surf_cpu_model->suspend = surf_action_suspend;
376   surf_cpu_model->resume = surf_action_resume;
377   surf_cpu_model->is_suspended = surf_action_is_suspended;
378   surf_cpu_model->set_max_duration = surf_action_set_max_duration;
379   surf_cpu_model->set_priority = surf_action_set_priority;
380 #ifdef HAVE_TRACING
381   surf_cpu_model->set_category = surf_action_set_category;
382 #endif
383   surf_cpu_model->get_remains = surf_action_get_remains;
384
385   surf_cpu_model->extension.cpu.execute = cpu_execute;
386   surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
387
388   surf_cpu_model->extension.cpu.get_state = cpu_get_state;
389   surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
390   surf_cpu_model->extension.cpu.get_available_speed =
391       cpu_get_available_speed;
392   surf_cpu_model->extension.cpu.create_resource = cpu_create_resource;
393   surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
394
395   if (!surf_cpu_model->model_private->maxmin_system) {
396     surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
397   }
398   if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
399     surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
400     xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
401         surf_action_lmm_update_index_heap);
402     surf_cpu_model->model_private->modified_set =
403         xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
404     surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
405   }
406 }
407
408 /*********************************************************************/
409 /* Basic sharing model for CPU: that is where all this started... ;) */
410 /*********************************************************************/
411 /* @InProceedings{casanova01simgrid, */
412 /*   author =       "H. Casanova", */
413 /*   booktitle =    "Proceedings of the IEEE Symposium on Cluster Computing */
414 /*                  and the Grid (CCGrid'01)", */
415 /*   publisher =    "IEEE Computer Society", */
416 /*   title =        "Simgrid: {A} Toolkit for the Simulation of Application */
417 /*                  Scheduling", */
418 /*   year =         "2001", */
419 /*   month =        may, */
420 /*   note =         "Available at */
421 /*                  \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
422 /* } */
423
424 void surf_cpu_model_init_Cas01()
425 {
426   char *optim = xbt_cfg_get_string(_surf_cfg_set, "cpu/optim");
427
428   if (surf_cpu_model)
429     return;
430
431   if (!strcmp(optim, "TI")) {
432     surf_cpu_model_init_ti();
433     return;
434   }
435
436   surf_cpu_model_init_internal();
437   cpu_define_callbacks();
438   xbt_dynar_push(model_list, &surf_cpu_model);
439 }