1 /* Copyright (c) 2009-2013. 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_cas01_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_cas01_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=%f, 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;
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)
193 power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
194 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
196 pstate_nb = xbt_dynar_length(all_power_values);
197 for (i=0; i< pstate_nb; i++)
199 /* retrieve the power values associated with the current pstate */
200 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
201 xbt_assert(xbt_dynar_length(current_power_values) > 1,
202 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
203 cpu_model->generic_resource.name);
205 /* min_power corresponds to the idle power (cpu load = 0) */
206 /* max_power is the power consumed at 100% cpu load */
207 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
208 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
210 power_tuple = xbt_dynar_new(sizeof(double), NULL);
211 xbt_dynar_push_as(power_tuple, double, min_power);
212 xbt_dynar_push_as(power_tuple, double, max_power);
214 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
215 xbt_dynar_free(¤t_power_values);
217 xbt_dynar_free(&all_power_values);
218 return power_range_list;
223 * Computes the power consumed by the host according to the current pstate and processor load
226 static double cpu_get_current_watts_value(cpu_Cas01_t cpu_model, double cpu_load)
228 xbt_dynar_t power_range_list = cpu_model->energy->power_range_watts_list;
230 if (power_range_list == NULL)
232 XBT_DEBUG("No power range properties specified for host %s", cpu_model->generic_resource.name);
235 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(cpu_model->power_peak_list),
236 "The number of power ranges in the properties does not match the number of pstates for host %s",
237 cpu_model->generic_resource.name);
239 /* retrieve the power values associated with the current pstate */
240 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, cpu_model->pstate, xbt_dynar_t);
242 /* min_power corresponds to the idle power (cpu load = 0) */
243 /* max_power is the power consumed at 100% cpu load */
244 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
245 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
246 double power_slope = max_power - min_power;
248 double current_power = min_power + cpu_load * power_slope;
250 XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
251 XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
253 return current_power;
258 * Updates the total energy consumed as the sum of the current energy and
259 * the energy consumed by the current action
261 void cpu_update_energy(cpu_Cas01_t cpu_model, double cpu_load)
264 double start_time = cpu_model->energy->last_updated;
265 double finish_time = surf_get_clock();
267 XBT_DEBUG("[cpu_update_energy] action time interval=(%f-%f), current power peak=%f, current pstate=%d",
268 start_time, finish_time, cpu_model->power_peak, cpu_model->pstate);
269 double current_energy = cpu_model->energy->total_energy;
270 double action_energy = cpu_get_current_watts_value(cpu_model, cpu_load)*(finish_time-start_time);
272 cpu_model->energy->total_energy = current_energy + action_energy;
273 cpu_model->energy->last_updated = finish_time;
275 XBT_DEBUG("[cpu_update_energy] old_energy_value=%f, action_energy_value=%f", current_energy, action_energy);
279 static void cpu_update_resource_state(void *id,
280 tmgr_trace_event_t event_type,
281 double value, double date)
283 cpu_Cas01_t cpu = id;
284 lmm_variable_t var = NULL;
285 lmm_element_t elem = NULL;
287 if (event_type == cpu->power_event) {
288 cpu->power_scale = value;
289 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
290 cpu->core * cpu->power_scale *
293 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
294 cpu->core * cpu->power_scale *
297 while ((var = lmm_get_var_from_cnst
298 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
299 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
300 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
301 GENERIC_LMM_ACTION(action).variable,
302 cpu->power_scale * cpu->power_peak);
304 if (tmgr_trace_event_free(event_type))
305 cpu->power_event = NULL;
306 } else if (event_type == cpu->state_event) {
308 if(cpu->state_current == SURF_RESOURCE_OFF)
309 xbt_dynar_push_as(host_that_restart, char*, (cpu->generic_resource.name));
310 cpu->state_current = SURF_RESOURCE_ON;
312 lmm_constraint_t cnst = cpu->constraint;
314 cpu->state_current = SURF_RESOURCE_OFF;
316 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
317 surf_action_t action = lmm_variable_id(var);
319 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
320 surf_action_state_get(action) == SURF_ACTION_READY ||
321 surf_action_state_get(action) ==
322 SURF_ACTION_NOT_IN_THE_SYSTEM) {
323 action->finish = date;
324 surf_action_state_set(action, SURF_ACTION_FAILED);
328 if (tmgr_trace_event_free(event_type))
329 cpu->state_event = NULL;
331 XBT_CRITICAL("Unknown event ! \n");
338 static surf_action_t cpu_execute(void *cpu, double size)
340 surf_action_cpu_Cas01_t action = NULL;
341 //xbt_dict_cursor_t cursor = NULL;
342 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
343 //xbt_dict_t props = CPU->generic_resource.properties;
345 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
347 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
349 CPU->state_current != SURF_RESOURCE_ON);
351 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
352 calloc but it seems to help valgrind... */
354 GENERIC_LMM_ACTION(action).variable =
355 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
356 GENERIC_ACTION(action).priority,
357 CPU->power_scale * CPU->power_peak, 1);
358 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
359 GENERIC_LMM_ACTION(action).index_heap = -1;
360 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
361 GENERIC_LMM_ACTION(action).last_value = 0.0;
364 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
365 GENERIC_LMM_ACTION(action).variable, 1.0);
367 return (surf_action_t) action;
370 static surf_action_t cpu_action_sleep(void *cpu, double duration)
372 surf_action_cpu_Cas01_t action = NULL;
375 duration = MAX(duration, MAXMIN_PRECISION);
377 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
378 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
379 // FIXME: sleep variables should not consume 1.0 in lmm_expand
380 GENERIC_ACTION(action).max_duration = duration;
381 GENERIC_LMM_ACTION(action).suspended = 2;
382 if (duration == NO_MAX_DURATION) {
383 /* Move to the *end* of the corresponding action set. This convention
384 is used to speed up update_resource_state */
385 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
386 ((surf_action_t) action)->state_set =
387 cpu_running_action_set_that_does_not_need_being_checked;
388 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
391 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
392 GENERIC_LMM_ACTION(action).variable, 0.0);
393 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
394 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
395 // this is necessary for a variable with weight 0 since such
396 // variables are ignored in lmm and we need to set its max_duration
397 // correctly at the next call to share_resources
398 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
402 return (surf_action_t) action;
405 static e_surf_resource_state_t cpu_get_state(void *cpu)
407 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
410 static double cpu_get_speed(void *cpu, double load)
412 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
415 static int cpu_get_core(void *cpu)
417 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->core;
421 static double cpu_get_available_speed(void *cpu)
423 /* number between 0 and 1 */
424 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
427 static double cpu_get_current_power_peak(void *cpu)
429 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
432 static double cpu_get_power_peak_at(void *cpu, int pstate_index)
434 xbt_dynar_t plist = ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list;
435 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
437 return xbt_dynar_get_as(plist, pstate_index, double);
440 static int cpu_get_nb_pstates(void *cpu)
442 return xbt_dynar_length(((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list);
445 static void cpu_set_power_peak_at(void *cpu, int pstate_index)
447 cpu_Cas01_t cpu_implem = (cpu_Cas01_t)surf_cpu_resource_priv(cpu);
448 xbt_dynar_t plist = cpu_implem->power_peak_list;
449 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
451 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
452 cpu_implem->pstate = pstate_index;
453 cpu_implem->power_peak = new_power_peak;
456 static double cpu_get_consumed_energy(void *cpu)
458 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->energy->total_energy;
461 static void cpu_finalize(void)
463 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
464 surf_cpu_model->model_private->maxmin_system = NULL;
466 if (surf_cpu_model->model_private->action_heap)
467 xbt_heap_free(surf_cpu_model->model_private->action_heap);
468 xbt_swag_free(surf_cpu_model->model_private->modified_set);
470 surf_model_exit(surf_cpu_model);
471 surf_cpu_model = NULL;
473 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
474 cpu_running_action_set_that_does_not_need_being_checked = NULL;
477 static void surf_cpu_model_init_internal()
479 s_surf_action_t action;
480 s_surf_action_cpu_Cas01_t comp;
482 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
484 xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
486 surf_cpu_model = surf_model_init();
488 if (!strcmp(optim, "Full")) {
489 surf_cpu_model->model_private->update_mechanism = UM_FULL;
490 surf_cpu_model->model_private->selective_update = select;
491 } else if (!strcmp(optim, "Lazy")) {
492 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
493 surf_cpu_model->model_private->selective_update = 1;
494 xbt_assert((select == 1)
496 (xbt_cfg_is_default_value
497 (_sg_cfg_set, "cpu/maxmin_selective_update")),
498 "Disabling selective update while using the lazy update mechanism is dumb!");
500 xbt_die("Unsupported optimization (%s) for this model", optim);
503 cpu_running_action_set_that_does_not_need_being_checked =
504 xbt_swag_new(xbt_swag_offset(action, state_hookup));
506 surf_cpu_model->name = "cpu";
508 surf_cpu_model->action_unref = surf_action_unref;
509 surf_cpu_model->action_cancel = surf_action_cancel;
510 surf_cpu_model->action_state_set = surf_action_state_set;
512 surf_cpu_model->model_private->resource_used = cpu_resource_used;
514 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
515 surf_cpu_model->model_private->share_resources =
516 cpu_share_resources_lazy;
517 surf_cpu_model->model_private->update_actions_state =
518 cpu_update_actions_state_lazy;
519 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
520 surf_cpu_model->model_private->share_resources =
521 cpu_share_resources_full;
522 surf_cpu_model->model_private->update_actions_state =
523 cpu_update_actions_state_full;
525 xbt_die("Invalid cpu update mechanism!");
527 surf_cpu_model->model_private->update_resource_state =
528 cpu_update_resource_state;
529 surf_cpu_model->model_private->finalize = cpu_finalize;
531 surf_cpu_model->suspend = surf_action_suspend;
532 surf_cpu_model->resume = surf_action_resume;
533 surf_cpu_model->is_suspended = surf_action_is_suspended;
534 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
535 surf_cpu_model->set_priority = surf_action_set_priority;
537 surf_cpu_model->set_category = surf_action_set_category;
539 surf_cpu_model->get_remains = surf_action_get_remains;
541 surf_cpu_model->extension.cpu.execute = cpu_execute;
542 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
544 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
545 surf_cpu_model->extension.cpu.get_core = cpu_get_core;
546 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
547 surf_cpu_model->extension.cpu.get_available_speed =
548 cpu_get_available_speed;
549 surf_cpu_model->extension.cpu.get_current_power_peak = cpu_get_current_power_peak;
550 surf_cpu_model->extension.cpu.get_power_peak_at = cpu_get_power_peak_at;
551 surf_cpu_model->extension.cpu.get_nb_pstates = cpu_get_nb_pstates;
552 surf_cpu_model->extension.cpu.set_power_peak_at = cpu_set_power_peak_at;
553 surf_cpu_model->extension.cpu.get_consumed_energy = cpu_get_consumed_energy;
555 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
557 if (!surf_cpu_model->model_private->maxmin_system) {
558 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
560 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
561 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
562 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
563 surf_action_lmm_update_index_heap);
564 surf_cpu_model->model_private->modified_set =
565 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
566 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
570 /*********************************************************************/
571 /* Basic sharing model for CPU: that is where all this started... ;) */
572 /*********************************************************************/
573 /* @InProceedings{casanova01simgrid, */
574 /* author = "H. Casanova", */
575 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
576 /* and the Grid (CCGrid'01)", */
577 /* publisher = "IEEE Computer Society", */
578 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
582 /* note = "Available at */
583 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
586 void surf_cpu_model_init_Cas01()
588 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
593 if (!strcmp(optim, "TI")) {
594 surf_cpu_model_init_ti();
598 surf_cpu_model_init_internal();
599 cpu_define_callbacks();
600 xbt_dynar_push(model_list, &surf_cpu_model);