1 /* Copyright (c) 2009-2011. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf_private.h"
8 #include "surf/surf_resource.h"
9 #include "maxmin_private.h"
10 #include "simgrid/sg_config.h"
11 #include "cpu_cas01_private.h"
16 surf_model_t surf_cpu_model = NULL;
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
20 "Logging specific to the SURF CPU IMPROVED module");
23 cpu_running_action_set_that_does_not_need_being_checked = NULL;
25 /* Additionnal callback function to cleanup some data, called from surf_resource_free */
27 static void cpu_cas1_cleanup(void* r){
28 cpu_Cas01_t cpu = (cpu_Cas01_t)r;
30 xbt_dynar_t power_tuple = NULL;
31 xbt_dynar_foreach(cpu->energy->power_range_watts_list, iter, power_tuple)
32 xbt_dynar_free(&power_tuple);
33 xbt_dynar_free(&cpu->energy->power_range_watts_list);
34 xbt_dynar_free(&cpu->power_peak_list);
35 xbt_free(cpu->energy);
39 /* This function is registered as a callback to sg_platf_new_host() and never called directly */
40 static void *cpu_create_resource(const char *name, xbt_dynar_t power_peak,
43 tmgr_trace_t power_trace,
45 e_surf_resource_state_t state_initial,
46 tmgr_trace_t state_trace,
47 xbt_dict_t cpu_properties)
49 cpu_Cas01_t cpu = NULL;
51 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
52 "Host '%s' declared several times in the platform file",
54 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
56 cpu_properties, &cpu_cas1_cleanup);
57 cpu->power_peak = xbt_dynar_get_as(power_peak, pstate, double);
58 cpu->power_peak_list = power_peak;
61 cpu->energy = xbt_new(s_energy_cpu_cas01_t, 1);
62 cpu->energy->total_energy = 0;
63 cpu->energy->power_range_watts_list = cpu_get_watts_range_list(cpu);
64 cpu->energy->last_updated = surf_get_clock();
66 XBT_DEBUG("CPU create: peak=%lf, pstate=%d",cpu->power_peak, cpu->pstate);
68 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
69 cpu->power_scale = power_scale;
71 xbt_assert(core > 0, "Invalid number of cores %d", core);
75 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
77 cpu->state_current = state_initial;
80 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
83 lmm_constraint_new(surf_cpu_model->model_private->maxmin_system, cpu,
84 cpu->core * cpu->power_scale * cpu->power_peak);
86 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
88 return xbt_lib_get_elm_or_null(host_lib, name);;
92 static void parse_cpu_init(sg_platf_host_cbarg_t host)
94 cpu_create_resource(host->id,
101 host->state_trace, host->properties);
104 static void cpu_add_traces_cpu(void)
106 xbt_dict_cursor_t cursor = NULL;
107 char *trace_name, *elm;
108 static int called = 0;
113 /* connect all traces relative to hosts */
114 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
115 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
116 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
118 xbt_assert(host, "Host %s undefined", elm);
119 xbt_assert(trace, "Trace %s undefined", trace_name);
122 tmgr_history_add_trace(history, trace, 0.0, 0, host);
125 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
126 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
127 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
129 xbt_assert(host, "Host %s undefined", elm);
130 xbt_assert(trace, "Trace %s undefined", trace_name);
133 tmgr_history_add_trace(history, trace, 0.0, 0, host);
137 static void cpu_define_callbacks()
139 sg_platf_host_add_cb(parse_cpu_init);
140 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
143 static int cpu_resource_used(void *resource)
145 return lmm_constraint_used(surf_cpu_model->model_private->maxmin_system,
146 ((cpu_Cas01_t) resource)->constraint);
149 static double cpu_share_resources_lazy(double now)
151 return generic_share_resources_lazy(now, surf_cpu_model);
154 static double cpu_share_resources_full(double now)
156 s_surf_action_cpu_Cas01_t action;
157 return generic_maxmin_share_resources(surf_cpu_model->states.
159 xbt_swag_offset(action,
162 surf_cpu_model->model_private->maxmin_system, lmm_solve);
165 static void cpu_update_actions_state_lazy(double now, double delta)
167 generic_update_actions_state_lazy(now, delta, surf_cpu_model);
170 static void cpu_update_actions_state_full(double now, double delta)
172 generic_update_actions_state_full(now, delta, surf_cpu_model);
175 xbt_dynar_t cpu_get_watts_range_list(cpu_Cas01_t cpu_model)
177 xbt_dynar_t power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
178 xbt_dynar_t power_tuple;
179 int i = 0, pstate_nb=0;
180 xbt_dynar_t current_power_values;
181 double min_power, max_power;
182 xbt_dict_t props = cpu_model->generic_resource.properties;
187 char* all_power_values_str = xbt_dict_get_or_null(props, "power_per_state");
189 if (all_power_values_str == NULL)
192 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
194 pstate_nb = xbt_dynar_length(all_power_values);
195 for (i=0; i< pstate_nb; i++)
197 /* retrieve the power values associated with the current pstate */
198 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
199 xbt_assert(xbt_dynar_length(current_power_values) > 1,
200 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
201 cpu_model->generic_resource.name);
203 /* min_power corresponds to the idle power (cpu load = 0) */
204 /* max_power is the power consumed at 100% cpu load */
205 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
206 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
208 power_tuple = xbt_dynar_new(sizeof(double), NULL);
209 xbt_dynar_push_as(power_tuple, double, min_power);
210 xbt_dynar_push_as(power_tuple, double, max_power);
212 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
215 return power_range_list;
220 * Computes the power consumed by the host according to the current pstate and processor load
223 static double cpu_get_current_watts_value(cpu_Cas01_t cpu_model, double cpu_load)
225 xbt_dynar_t power_range_list = cpu_model->energy->power_range_watts_list;
227 if (power_range_list == NULL)
229 XBT_DEBUG("No power range properties specified for host %s", cpu_model->generic_resource.name);
232 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(cpu_model->power_peak_list),
233 "The number of power ranges in the properties does not match the number of pstates for host %s",
234 cpu_model->generic_resource.name);
236 /* retrieve the power values associated with the current pstate */
237 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, cpu_model->pstate, xbt_dynar_t);
239 /* min_power corresponds to the idle power (cpu load = 0) */
240 /* max_power is the power consumed at 100% cpu load */
241 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
242 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
243 double power_slope = max_power - min_power;
245 double current_power = min_power + cpu_load * power_slope;
247 XBT_DEBUG("[get_current_watts] min_power=%lf, max_power=%lf, slope=%lf", min_power, max_power, power_slope);
248 XBT_DEBUG("[get_current_watts] Current power (watts) = %lf, load = %lf", current_power, cpu_load);
250 return current_power;
255 * Updates the total energy consumed as the sum of the current energy and
256 * the energy consumed by the current action
258 void cpu_update_energy(cpu_Cas01_t cpu_model, double cpu_load)
261 double start_time = cpu_model->energy->last_updated;
262 double finish_time = surf_get_clock();
264 XBT_DEBUG("[cpu_update_energy] action time interval=(%lf-%lf), current power peak=%lf, current pstate=%d",
265 start_time, finish_time, cpu_model->power_peak, cpu_model->pstate);
266 double current_energy = cpu_model->energy->total_energy;
267 double action_energy = cpu_get_current_watts_value(cpu_model, cpu_load)*(finish_time-start_time);
269 cpu_model->energy->total_energy = current_energy + action_energy;
270 cpu_model->energy->last_updated = finish_time;
272 XBT_DEBUG("[cpu_update_energy] old_energy_value=%lf, action_energy_value=%lf", current_energy, action_energy);
276 static void cpu_update_resource_state(void *id,
277 tmgr_trace_event_t event_type,
278 double value, double date)
280 cpu_Cas01_t cpu = id;
281 lmm_variable_t var = NULL;
282 lmm_element_t elem = NULL;
284 if (event_type == cpu->power_event) {
285 cpu->power_scale = value;
286 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
287 cpu->core * cpu->power_scale *
290 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
291 cpu->core * cpu->power_scale *
294 while ((var = lmm_get_var_from_cnst
295 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
296 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
297 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
298 GENERIC_LMM_ACTION(action).variable,
299 cpu->power_scale * cpu->power_peak);
301 if (tmgr_trace_event_free(event_type))
302 cpu->power_event = NULL;
303 } else if (event_type == cpu->state_event) {
305 if(cpu->state_current == SURF_RESOURCE_OFF)
306 xbt_dynar_push_as(host_that_restart, char*, (cpu->generic_resource.name));
307 cpu->state_current = SURF_RESOURCE_ON;
309 lmm_constraint_t cnst = cpu->constraint;
311 cpu->state_current = SURF_RESOURCE_OFF;
313 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
314 surf_action_t action = lmm_variable_id(var);
316 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
317 surf_action_state_get(action) == SURF_ACTION_READY ||
318 surf_action_state_get(action) ==
319 SURF_ACTION_NOT_IN_THE_SYSTEM) {
320 action->finish = date;
321 surf_action_state_set(action, SURF_ACTION_FAILED);
325 if (tmgr_trace_event_free(event_type))
326 cpu->state_event = NULL;
328 XBT_CRITICAL("Unknown event ! \n");
335 static surf_action_t cpu_execute(void *cpu, double size)
337 surf_action_cpu_Cas01_t action = NULL;
338 //xbt_dict_cursor_t cursor = NULL;
339 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
340 //xbt_dict_t props = CPU->generic_resource.properties;
342 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
344 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
346 CPU->state_current != SURF_RESOURCE_ON);
348 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
349 calloc but it seems to help valgrind... */
351 GENERIC_LMM_ACTION(action).variable =
352 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
353 GENERIC_ACTION(action).priority,
354 CPU->power_scale * CPU->power_peak, 1);
355 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
356 GENERIC_LMM_ACTION(action).index_heap = -1;
357 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
358 GENERIC_LMM_ACTION(action).last_value = 0.0;
361 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
362 GENERIC_LMM_ACTION(action).variable, 1.0);
364 return (surf_action_t) action;
367 static surf_action_t cpu_action_sleep(void *cpu, double duration)
369 surf_action_cpu_Cas01_t action = NULL;
372 duration = MAX(duration, MAXMIN_PRECISION);
374 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
375 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
376 // FIXME: sleep variables should not consume 1.0 in lmm_expand
377 GENERIC_ACTION(action).max_duration = duration;
378 GENERIC_LMM_ACTION(action).suspended = 2;
379 if (duration == NO_MAX_DURATION) {
380 /* Move to the *end* of the corresponding action set. This convention
381 is used to speed up update_resource_state */
382 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
383 ((surf_action_t) action)->state_set =
384 cpu_running_action_set_that_does_not_need_being_checked;
385 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
388 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
389 GENERIC_LMM_ACTION(action).variable, 0.0);
390 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
391 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
392 // this is necessary for a variable with weight 0 since such
393 // variables are ignored in lmm and we need to set its max_duration
394 // correctly at the next call to share_resources
395 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
399 return (surf_action_t) action;
402 static e_surf_resource_state_t cpu_get_state(void *cpu)
404 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
407 static double cpu_get_speed(void *cpu, double load)
409 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
412 static int cpu_get_core(void *cpu)
414 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->core;
418 static double cpu_get_available_speed(void *cpu)
420 /* number between 0 and 1 */
421 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
424 static double cpu_get_current_power_peak(void *cpu)
426 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
429 static double cpu_get_power_peak_at(void *cpu, int pstate_index)
431 xbt_dynar_t plist = ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list;
432 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
434 return xbt_dynar_get_as(plist, pstate_index, double);
437 static int cpu_get_nb_pstates(void *cpu)
439 return xbt_dynar_length(((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list);
442 static void cpu_set_power_peak_at(void *cpu, int pstate_index)
444 cpu_Cas01_t cpu_implem = (cpu_Cas01_t)surf_cpu_resource_priv(cpu);
445 xbt_dynar_t plist = cpu_implem->power_peak_list;
446 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
448 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
449 cpu_implem->pstate = pstate_index;
450 cpu_implem->power_peak = new_power_peak;
453 static double cpu_get_consumed_energy(void *cpu)
455 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->energy->total_energy;
458 static void cpu_finalize(void)
460 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
461 surf_cpu_model->model_private->maxmin_system = NULL;
463 if (surf_cpu_model->model_private->action_heap)
464 xbt_heap_free(surf_cpu_model->model_private->action_heap);
465 xbt_swag_free(surf_cpu_model->model_private->modified_set);
467 surf_model_exit(surf_cpu_model);
468 surf_cpu_model = NULL;
470 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
471 cpu_running_action_set_that_does_not_need_being_checked = NULL;
474 static void surf_cpu_model_init_internal()
476 s_surf_action_t action;
477 s_surf_action_cpu_Cas01_t comp;
479 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
481 xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
483 surf_cpu_model = surf_model_init();
485 if (!strcmp(optim, "Full")) {
486 surf_cpu_model->model_private->update_mechanism = UM_FULL;
487 surf_cpu_model->model_private->selective_update = select;
488 } else if (!strcmp(optim, "Lazy")) {
489 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
490 surf_cpu_model->model_private->selective_update = 1;
491 xbt_assert((select == 1)
493 (xbt_cfg_is_default_value
494 (_sg_cfg_set, "cpu/maxmin_selective_update")),
495 "Disabling selective update while using the lazy update mechanism is dumb!");
497 xbt_die("Unsupported optimization (%s) for this model", optim);
500 cpu_running_action_set_that_does_not_need_being_checked =
501 xbt_swag_new(xbt_swag_offset(action, state_hookup));
503 surf_cpu_model->name = "cpu";
505 surf_cpu_model->action_unref = surf_action_unref;
506 surf_cpu_model->action_cancel = surf_action_cancel;
507 surf_cpu_model->action_state_set = surf_action_state_set;
509 surf_cpu_model->model_private->resource_used = cpu_resource_used;
511 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
512 surf_cpu_model->model_private->share_resources =
513 cpu_share_resources_lazy;
514 surf_cpu_model->model_private->update_actions_state =
515 cpu_update_actions_state_lazy;
516 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
517 surf_cpu_model->model_private->share_resources =
518 cpu_share_resources_full;
519 surf_cpu_model->model_private->update_actions_state =
520 cpu_update_actions_state_full;
522 xbt_die("Invalid cpu update mechanism!");
524 surf_cpu_model->model_private->update_resource_state =
525 cpu_update_resource_state;
526 surf_cpu_model->model_private->finalize = cpu_finalize;
528 surf_cpu_model->suspend = surf_action_suspend;
529 surf_cpu_model->resume = surf_action_resume;
530 surf_cpu_model->is_suspended = surf_action_is_suspended;
531 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
532 surf_cpu_model->set_priority = surf_action_set_priority;
534 surf_cpu_model->set_category = surf_action_set_category;
536 surf_cpu_model->get_remains = surf_action_get_remains;
538 surf_cpu_model->extension.cpu.execute = cpu_execute;
539 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
541 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
542 surf_cpu_model->extension.cpu.get_core = cpu_get_core;
543 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
544 surf_cpu_model->extension.cpu.get_available_speed =
545 cpu_get_available_speed;
546 surf_cpu_model->extension.cpu.get_current_power_peak = cpu_get_current_power_peak;
547 surf_cpu_model->extension.cpu.get_power_peak_at = cpu_get_power_peak_at;
548 surf_cpu_model->extension.cpu.get_nb_pstates = cpu_get_nb_pstates;
549 surf_cpu_model->extension.cpu.set_power_peak_at = cpu_set_power_peak_at;
550 surf_cpu_model->extension.cpu.get_consumed_energy = cpu_get_consumed_energy;
552 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
554 if (!surf_cpu_model->model_private->maxmin_system) {
555 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
557 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
558 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
559 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
560 surf_action_lmm_update_index_heap);
561 surf_cpu_model->model_private->modified_set =
562 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
563 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
567 /*********************************************************************/
568 /* Basic sharing model for CPU: that is where all this started... ;) */
569 /*********************************************************************/
570 /* @InProceedings{casanova01simgrid, */
571 /* author = "H. Casanova", */
572 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
573 /* and the Grid (CCGrid'01)", */
574 /* publisher = "IEEE Computer Society", */
575 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
579 /* note = "Available at */
580 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
583 void surf_cpu_model_init_Cas01()
585 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
590 if (!strcmp(optim, "TI")) {
591 surf_cpu_model_init_ti();
595 surf_cpu_model_init_internal();
596 cpu_define_callbacks();
597 xbt_dynar_push(model_list, &surf_cpu_model);