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;
26 /* This function is registered as a callback to sg_platf_new_host() and never called directly */
27 static void *cpu_create_resource(const char *name, xbt_dynar_t power_peak,
30 tmgr_trace_t power_trace,
32 e_surf_resource_state_t state_initial,
33 tmgr_trace_t state_trace,
34 xbt_dict_t cpu_properties)
36 cpu_Cas01_t cpu = NULL;
38 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
39 "Host '%s' declared several times in the platform file",
41 cpu = (cpu_Cas01_t) surf_resource_new(sizeof(s_cpu_Cas01_t),
44 cpu->power_peak = xbt_dynar_get_as(power_peak, pstate, double);
45 cpu->power_peak_list = power_peak;
48 cpu->energy = xbt_new(s_energy_cpu_cas01_t, 1);
49 cpu->energy->total_energy = 0;
50 cpu->energy->power_range_watts_list = cpu_get_watts_range_list(cpu);
51 cpu->energy->last_updated = surf_get_clock();
53 XBT_DEBUG("CPU create: peak=%lf, pstate=%d",cpu->power_peak, cpu->pstate);
55 xbt_assert(cpu->power_peak > 0, "Power has to be >0");
56 cpu->power_scale = power_scale;
58 xbt_assert(core > 0, "Invalid number of cores %d", core);
62 tmgr_history_add_trace(history, power_trace, 0.0, 0, cpu);
64 cpu->state_current = state_initial;
67 tmgr_history_add_trace(history, state_trace, 0.0, 0, cpu);
70 lmm_constraint_new(surf_cpu_model->model_private->maxmin_system, cpu,
71 cpu->core * cpu->power_scale * cpu->power_peak);
73 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
75 return xbt_lib_get_elm_or_null(host_lib, name);;
79 static void parse_cpu_init(sg_platf_host_cbarg_t host)
81 cpu_create_resource(host->id,
88 host->state_trace, host->properties);
91 static void cpu_add_traces_cpu(void)
93 xbt_dict_cursor_t cursor = NULL;
94 char *trace_name, *elm;
95 static int called = 0;
100 /* connect all traces relative to hosts */
101 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
102 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
103 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
105 xbt_assert(host, "Host %s undefined", elm);
106 xbt_assert(trace, "Trace %s undefined", trace_name);
109 tmgr_history_add_trace(history, trace, 0.0, 0, host);
112 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
113 tmgr_trace_t trace = xbt_dict_get_or_null(traces_set_list, trace_name);
114 cpu_Cas01_t host = surf_cpu_resource_by_name(elm);
116 xbt_assert(host, "Host %s undefined", elm);
117 xbt_assert(trace, "Trace %s undefined", trace_name);
120 tmgr_history_add_trace(history, trace, 0.0, 0, host);
124 static void cpu_define_callbacks()
126 sg_platf_host_add_cb(parse_cpu_init);
127 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
130 static int cpu_resource_used(void *resource)
132 return lmm_constraint_used(surf_cpu_model->model_private->maxmin_system,
133 ((cpu_Cas01_t) resource)->constraint);
136 static double cpu_share_resources_lazy(double now)
138 return generic_share_resources_lazy(now, surf_cpu_model);
141 static double cpu_share_resources_full(double now)
143 s_surf_action_cpu_Cas01_t action;
144 return generic_maxmin_share_resources(surf_cpu_model->states.
146 xbt_swag_offset(action,
149 surf_cpu_model->model_private->maxmin_system, lmm_solve);
152 static void cpu_update_actions_state_lazy(double now, double delta)
154 generic_update_actions_state_lazy(now, delta, surf_cpu_model);
157 static void cpu_update_actions_state_full(double now, double delta)
159 generic_update_actions_state_full(now, delta, surf_cpu_model);
162 xbt_dynar_t cpu_get_watts_range_list(cpu_Cas01_t cpu_model)
164 xbt_dynar_t power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
165 xbt_dynar_t power_tuple;
166 int i = 0, pstate_nb=0;
167 xbt_dynar_t current_power_values;
168 double min_power, max_power;
169 xbt_dict_t props = cpu_model->generic_resource.properties;
174 char* all_power_values_str = xbt_dict_get_or_null(props, "power_per_state");
176 if (all_power_values_str == NULL)
179 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
181 pstate_nb = xbt_dynar_length(all_power_values);
182 for (i=0; i< pstate_nb; i++)
184 /* retrieve the power values associated with the current pstate */
185 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
186 xbt_assert(xbt_dynar_length(current_power_values) > 1,
187 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
188 cpu_model->generic_resource.name);
190 /* min_power corresponds to the idle power (cpu load = 0) */
191 /* max_power is the power consumed at 100% cpu load */
192 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
193 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
195 power_tuple = xbt_dynar_new(sizeof(double), NULL);
196 xbt_dynar_push_as(power_tuple, double, min_power);
197 xbt_dynar_push_as(power_tuple, double, max_power);
199 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
202 return power_range_list;
207 * Computes the power consumed by the host according to the current pstate and processor load
210 static double cpu_get_current_watts_value(cpu_Cas01_t cpu_model, double cpu_load)
212 xbt_dynar_t power_range_list = cpu_model->energy->power_range_watts_list;
214 if (power_range_list == NULL)
216 XBT_DEBUG("No power range properties specified for host %s", cpu_model->generic_resource.name);
219 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(cpu_model->power_peak_list),
220 "The number of power ranges in the properties does not match the number of pstates for host %s",
221 cpu_model->generic_resource.name);
223 /* retrieve the power values associated with the current pstate */
224 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, cpu_model->pstate, xbt_dynar_t);
226 /* min_power corresponds to the idle power (cpu load = 0) */
227 /* max_power is the power consumed at 100% cpu load */
228 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
229 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
230 double power_slope = max_power - min_power;
232 double current_power = min_power + cpu_load * power_slope;
234 XBT_DEBUG("[get_current_watts] min_power=%lf, max_power=%lf, slope=%lf", min_power, max_power, power_slope);
235 XBT_DEBUG("[get_current_watts] Current power (watts) = %lf, load = %lf", current_power, cpu_load);
237 return current_power;
242 * Updates the total energy consumed as the sum of the current energy and
243 * the energy consumed by the current action
245 void cpu_update_energy(cpu_Cas01_t cpu_model, double cpu_load)
248 double start_time = cpu_model->energy->last_updated;
249 double finish_time = surf_get_clock();
251 XBT_DEBUG("[cpu_update_energy] action time interval=(%lf-%lf), current power peak=%lf, current pstate=%d",
252 start_time, finish_time, cpu_model->power_peak, cpu_model->pstate);
253 double current_energy = cpu_model->energy->total_energy;
254 double action_energy = cpu_get_current_watts_value(cpu_model, cpu_load)*(finish_time-start_time);
256 cpu_model->energy->total_energy = current_energy + action_energy;
257 cpu_model->energy->last_updated = finish_time;
259 XBT_DEBUG("[cpu_update_energy] old_energy_value=%lf, action_energy_value=%lf", current_energy, action_energy);
263 static void cpu_update_resource_state(void *id,
264 tmgr_trace_event_t event_type,
265 double value, double date)
267 cpu_Cas01_t cpu = id;
268 lmm_variable_t var = NULL;
269 lmm_element_t elem = NULL;
271 surf_watched_hosts();
273 if (event_type == cpu->power_event) {
274 cpu->power_scale = value;
275 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
276 cpu->core * cpu->power_scale *
279 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
280 cpu->core * cpu->power_scale *
283 while ((var = lmm_get_var_from_cnst
284 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
285 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
286 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
287 GENERIC_LMM_ACTION(action).variable,
288 cpu->power_scale * cpu->power_peak);
290 if (tmgr_trace_event_free(event_type))
291 cpu->power_event = NULL;
292 } else if (event_type == cpu->state_event) {
294 cpu->state_current = SURF_RESOURCE_ON;
296 lmm_constraint_t cnst = cpu->constraint;
298 cpu->state_current = SURF_RESOURCE_OFF;
300 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
301 surf_action_t action = lmm_variable_id(var);
303 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
304 surf_action_state_get(action) == SURF_ACTION_READY ||
305 surf_action_state_get(action) ==
306 SURF_ACTION_NOT_IN_THE_SYSTEM) {
307 action->finish = date;
308 surf_action_state_set(action, SURF_ACTION_FAILED);
312 if (tmgr_trace_event_free(event_type))
313 cpu->state_event = NULL;
315 XBT_CRITICAL("Unknown event ! \n");
322 static surf_action_t cpu_execute(void *cpu, double size)
324 surf_action_cpu_Cas01_t action = NULL;
325 //xbt_dict_cursor_t cursor = NULL;
326 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
327 //xbt_dict_t props = CPU->generic_resource.properties;
329 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
331 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
333 CPU->state_current != SURF_RESOURCE_ON);
335 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
336 calloc but it seems to help valgrind... */
338 GENERIC_LMM_ACTION(action).variable =
339 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
340 GENERIC_ACTION(action).priority,
341 CPU->power_scale * CPU->power_peak, 1);
342 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
343 GENERIC_LMM_ACTION(action).index_heap = -1;
344 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
345 GENERIC_LMM_ACTION(action).last_value = 0.0;
348 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
349 GENERIC_LMM_ACTION(action).variable, 1.0);
351 return (surf_action_t) action;
354 static surf_action_t cpu_action_sleep(void *cpu, double duration)
356 surf_action_cpu_Cas01_t action = NULL;
359 duration = MAX(duration, MAXMIN_PRECISION);
361 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
362 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
363 // FIXME: sleep variables should not consume 1.0 in lmm_expand
364 GENERIC_ACTION(action).max_duration = duration;
365 GENERIC_LMM_ACTION(action).suspended = 2;
366 if (duration == NO_MAX_DURATION) {
367 /* Move to the *end* of the corresponding action set. This convention
368 is used to speed up update_resource_state */
369 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
370 ((surf_action_t) action)->state_set =
371 cpu_running_action_set_that_does_not_need_being_checked;
372 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
375 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
376 GENERIC_LMM_ACTION(action).variable, 0.0);
377 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
378 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
379 // this is necessary for a variable with weight 0 since such
380 // variables are ignored in lmm and we need to set its max_duration
381 // correctly at the next call to share_resources
382 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
386 return (surf_action_t) action;
389 static e_surf_resource_state_t cpu_get_state(void *cpu)
391 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
394 static double cpu_get_speed(void *cpu, double load)
396 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
399 static int cpu_get_core(void *cpu)
401 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->core;
405 static double cpu_get_available_speed(void *cpu)
407 /* number between 0 and 1 */
408 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
411 static double cpu_get_current_power_peak(void *cpu)
413 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
416 static double cpu_get_power_peak_at(void *cpu, int pstate_index)
418 xbt_dynar_t plist = ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list;
419 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
421 return xbt_dynar_get_as(plist, pstate_index, double);
424 static int cpu_get_nb_pstates(void *cpu)
426 return xbt_dynar_length(((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list);
429 static void cpu_set_power_peak_at(void *cpu, int pstate_index)
431 cpu_Cas01_t cpu_implem = (cpu_Cas01_t)surf_cpu_resource_priv(cpu);
432 xbt_dynar_t plist = cpu_implem->power_peak_list;
433 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
435 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
436 cpu_implem->pstate = pstate_index;
437 cpu_implem->power_peak = new_power_peak;
440 static double cpu_get_consumed_energy(void *cpu)
442 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->energy->total_energy;
445 static void cpu_finalize(void)
447 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
448 surf_cpu_model->model_private->maxmin_system = NULL;
450 if (surf_cpu_model->model_private->action_heap)
451 xbt_heap_free(surf_cpu_model->model_private->action_heap);
452 xbt_swag_free(surf_cpu_model->model_private->modified_set);
454 surf_model_exit(surf_cpu_model);
455 surf_cpu_model = NULL;
457 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
458 cpu_running_action_set_that_does_not_need_being_checked = NULL;
461 static void surf_cpu_model_init_internal()
463 s_surf_action_t action;
464 s_surf_action_cpu_Cas01_t comp;
466 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
468 xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
470 surf_cpu_model = surf_model_init();
472 if (!strcmp(optim, "Full")) {
473 surf_cpu_model->model_private->update_mechanism = UM_FULL;
474 surf_cpu_model->model_private->selective_update = select;
475 } else if (!strcmp(optim, "Lazy")) {
476 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
477 surf_cpu_model->model_private->selective_update = 1;
478 xbt_assert((select == 1)
480 (xbt_cfg_is_default_value
481 (_sg_cfg_set, "cpu/maxmin_selective_update")),
482 "Disabling selective update while using the lazy update mechanism is dumb!");
484 xbt_die("Unsupported optimization (%s) for this model", optim);
487 cpu_running_action_set_that_does_not_need_being_checked =
488 xbt_swag_new(xbt_swag_offset(action, state_hookup));
490 surf_cpu_model->name = "cpu";
492 surf_cpu_model->action_unref = surf_action_unref;
493 surf_cpu_model->action_cancel = surf_action_cancel;
494 surf_cpu_model->action_state_set = surf_action_state_set;
496 surf_cpu_model->model_private->resource_used = cpu_resource_used;
498 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
499 surf_cpu_model->model_private->share_resources =
500 cpu_share_resources_lazy;
501 surf_cpu_model->model_private->update_actions_state =
502 cpu_update_actions_state_lazy;
503 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
504 surf_cpu_model->model_private->share_resources =
505 cpu_share_resources_full;
506 surf_cpu_model->model_private->update_actions_state =
507 cpu_update_actions_state_full;
509 xbt_die("Invalid cpu update mechanism!");
511 surf_cpu_model->model_private->update_resource_state =
512 cpu_update_resource_state;
513 surf_cpu_model->model_private->finalize = cpu_finalize;
515 surf_cpu_model->suspend = surf_action_suspend;
516 surf_cpu_model->resume = surf_action_resume;
517 surf_cpu_model->is_suspended = surf_action_is_suspended;
518 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
519 surf_cpu_model->set_priority = surf_action_set_priority;
521 surf_cpu_model->set_category = surf_action_set_category;
523 surf_cpu_model->get_remains = surf_action_get_remains;
525 surf_cpu_model->extension.cpu.execute = cpu_execute;
526 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
528 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
529 surf_cpu_model->extension.cpu.get_core = cpu_get_core;
530 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
531 surf_cpu_model->extension.cpu.get_available_speed =
532 cpu_get_available_speed;
533 surf_cpu_model->extension.cpu.get_current_power_peak = cpu_get_current_power_peak;
534 surf_cpu_model->extension.cpu.get_power_peak_at = cpu_get_power_peak_at;
535 surf_cpu_model->extension.cpu.get_nb_pstates = cpu_get_nb_pstates;
536 surf_cpu_model->extension.cpu.set_power_peak_at = cpu_set_power_peak_at;
537 surf_cpu_model->extension.cpu.get_consumed_energy = cpu_get_consumed_energy;
539 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
541 if (!surf_cpu_model->model_private->maxmin_system) {
542 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
544 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
545 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
546 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
547 surf_action_lmm_update_index_heap);
548 surf_cpu_model->model_private->modified_set =
549 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
550 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
554 /*********************************************************************/
555 /* Basic sharing model for CPU: that is where all this started... ;) */
556 /*********************************************************************/
557 /* @InProceedings{casanova01simgrid, */
558 /* author = "H. Casanova", */
559 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
560 /* and the Grid (CCGrid'01)", */
561 /* publisher = "IEEE Computer Society", */
562 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
566 /* note = "Available at */
567 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
570 void surf_cpu_model_init_Cas01()
572 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
577 if (!strcmp(optim, "TI")) {
578 surf_cpu_model_init_ti();
582 surf_cpu_model_init_internal();
583 cpu_define_callbacks();
584 xbt_dynar_push(model_list, &surf_cpu_model);