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;
206 static double cpu_get_current_watts_value(cpu_Cas01_t cpu_model, double cpu_load)
208 xbt_dynar_t power_range_list = cpu_model->energy->power_range_watts_list;
210 if (power_range_list == NULL)
212 XBT_DEBUG("No power range properties specified for host %s", cpu_model->generic_resource.name);
215 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(cpu_model->power_peak_list),
216 "The number of power ranges in the properties does not match the number of pstates for host %s",
217 cpu_model->generic_resource.name);
219 /* retrieve the power values associated with the current pstate */
220 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, cpu_model->pstate, xbt_dynar_t);
222 /* min_power corresponds to the idle power (cpu load = 0) */
223 /* max_power is the power consumed at 100% cpu load */
224 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
225 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
226 double power_slope = max_power - min_power;
228 double current_power = min_power + cpu_load * power_slope;
230 XBT_DEBUG("[get_current_watts] min_power=%lf, max_power=%lf, slope=%lf", min_power, max_power, power_slope);
231 XBT_DEBUG("[get_current_watts] Current power (watts) = %lf, load = %lf", current_power, cpu_load);
233 return current_power;
238 * Updates the total energy consumed as the sum of the current energy and
239 * the energy consumed by the current action
241 void cpu_update_energy(cpu_Cas01_t cpu_model, double cpu_load)
244 double start_time = cpu_model->energy->last_updated;
245 double finish_time = surf_get_clock();
247 XBT_DEBUG("[cpu_update_energy] action time interval=(%lf-%lf), current power peak=%lf, current pstate=%d",
248 start_time, finish_time, cpu_model->power_peak, cpu_model->pstate);
249 double current_energy = cpu_model->energy->total_energy;
250 double action_energy = cpu_get_current_watts_value(cpu_model, cpu_load)*(finish_time-start_time);
252 cpu_model->energy->total_energy = current_energy + action_energy;
253 cpu_model->energy->last_updated = finish_time;
255 XBT_DEBUG("[cpu_update_energy] old_energy_value=%lf, action_energy_value=%lf", current_energy, action_energy);
259 static void cpu_update_resource_state(void *id,
260 tmgr_trace_event_t event_type,
261 double value, double date)
263 cpu_Cas01_t cpu = id;
264 lmm_variable_t var = NULL;
265 lmm_element_t elem = NULL;
267 surf_watched_hosts();
269 if (event_type == cpu->power_event) {
270 cpu->power_scale = value;
271 lmm_update_constraint_bound(surf_cpu_model->model_private->maxmin_system, cpu->constraint,
272 cpu->core * cpu->power_scale *
275 TRACE_surf_host_set_power(date, cpu->generic_resource.name,
276 cpu->core * cpu->power_scale *
279 while ((var = lmm_get_var_from_cnst
280 (surf_cpu_model->model_private->maxmin_system, cpu->constraint, &elem))) {
281 surf_action_cpu_Cas01_t action = lmm_variable_id(var);
282 lmm_update_variable_bound(surf_cpu_model->model_private->maxmin_system,
283 GENERIC_LMM_ACTION(action).variable,
284 cpu->power_scale * cpu->power_peak);
286 if (tmgr_trace_event_free(event_type))
287 cpu->power_event = NULL;
288 } else if (event_type == cpu->state_event) {
290 cpu->state_current = SURF_RESOURCE_ON;
292 lmm_constraint_t cnst = cpu->constraint;
294 cpu->state_current = SURF_RESOURCE_OFF;
296 while ((var = lmm_get_var_from_cnst(surf_cpu_model->model_private->maxmin_system, cnst, &elem))) {
297 surf_action_t action = lmm_variable_id(var);
299 if (surf_action_state_get(action) == SURF_ACTION_RUNNING ||
300 surf_action_state_get(action) == SURF_ACTION_READY ||
301 surf_action_state_get(action) ==
302 SURF_ACTION_NOT_IN_THE_SYSTEM) {
303 action->finish = date;
304 surf_action_state_set(action, SURF_ACTION_FAILED);
308 if (tmgr_trace_event_free(event_type))
309 cpu->state_event = NULL;
311 XBT_CRITICAL("Unknown event ! \n");
318 static surf_action_t cpu_execute(void *cpu, double size)
320 surf_action_cpu_Cas01_t action = NULL;
321 //xbt_dict_cursor_t cursor = NULL;
322 cpu_Cas01_t CPU = surf_cpu_resource_priv(cpu);
323 //xbt_dict_t props = CPU->generic_resource.properties;
325 XBT_IN("(%s,%g)", surf_resource_name(CPU), size);
327 surf_action_new(sizeof(s_surf_action_cpu_Cas01_t), size,
329 CPU->state_current != SURF_RESOURCE_ON);
331 GENERIC_LMM_ACTION(action).suspended = 0; /* Should be useless because of the
332 calloc but it seems to help valgrind... */
334 GENERIC_LMM_ACTION(action).variable =
335 lmm_variable_new(surf_cpu_model->model_private->maxmin_system, action,
336 GENERIC_ACTION(action).priority,
337 CPU->power_scale * CPU->power_peak, 1);
338 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
339 GENERIC_LMM_ACTION(action).index_heap = -1;
340 GENERIC_LMM_ACTION(action).last_update = surf_get_clock();
341 GENERIC_LMM_ACTION(action).last_value = 0.0;
344 lmm_expand(surf_cpu_model->model_private->maxmin_system, CPU->constraint,
345 GENERIC_LMM_ACTION(action).variable, 1.0);
347 return (surf_action_t) action;
350 static surf_action_t cpu_action_sleep(void *cpu, double duration)
352 surf_action_cpu_Cas01_t action = NULL;
355 duration = MAX(duration, MAXMIN_PRECISION);
357 XBT_IN("(%s,%g)", surf_resource_name(surf_cpu_resource_priv(cpu)), duration);
358 action = (surf_action_cpu_Cas01_t) cpu_execute(cpu, 1.0);
359 // FIXME: sleep variables should not consume 1.0 in lmm_expand
360 GENERIC_ACTION(action).max_duration = duration;
361 GENERIC_LMM_ACTION(action).suspended = 2;
362 if (duration == NO_MAX_DURATION) {
363 /* Move to the *end* of the corresponding action set. This convention
364 is used to speed up update_resource_state */
365 xbt_swag_remove(action, ((surf_action_t) action)->state_set);
366 ((surf_action_t) action)->state_set =
367 cpu_running_action_set_that_does_not_need_being_checked;
368 xbt_swag_insert(action, ((surf_action_t) action)->state_set);
371 lmm_update_variable_weight(surf_cpu_model->model_private->maxmin_system,
372 GENERIC_LMM_ACTION(action).variable, 0.0);
373 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) { // remove action from the heap
374 surf_action_lmm_heap_remove(surf_cpu_model->model_private->action_heap,(surf_action_lmm_t)action);
375 // this is necessary for a variable with weight 0 since such
376 // variables are ignored in lmm and we need to set its max_duration
377 // correctly at the next call to share_resources
378 xbt_swag_insert_at_head(action,surf_cpu_model->model_private->modified_set);
382 return (surf_action_t) action;
385 static e_surf_resource_state_t cpu_get_state(void *cpu)
387 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->state_current;
390 static double cpu_get_speed(void *cpu, double load)
392 return load * ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
395 static double cpu_get_available_speed(void *cpu)
397 /* number between 0 and 1 */
398 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_scale;
401 static double cpu_get_current_power_peak(void *cpu)
403 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak;
406 static double cpu_get_power_peak_at(void *cpu, int pstate_index)
408 xbt_dynar_t plist = ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list;
409 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
411 return xbt_dynar_get_as(plist, pstate_index, double);
414 static int cpu_get_nb_pstates(void *cpu)
416 return xbt_dynar_length(((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->power_peak_list);
419 static void cpu_set_power_peak_at(void *cpu, int pstate_index)
421 cpu_Cas01_t cpu_implem = (cpu_Cas01_t)surf_cpu_resource_priv(cpu);
422 xbt_dynar_t plist = cpu_implem->power_peak_list;
423 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
425 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
426 cpu_implem->pstate = pstate_index;
427 cpu_implem->power_peak = new_power_peak;
430 static double cpu_get_consumed_energy(void *cpu)
432 return ((cpu_Cas01_t)surf_cpu_resource_priv(cpu))->energy->total_energy;
435 static void cpu_finalize(void)
437 lmm_system_free(surf_cpu_model->model_private->maxmin_system);
438 surf_cpu_model->model_private->maxmin_system = NULL;
440 if (surf_cpu_model->model_private->action_heap)
441 xbt_heap_free(surf_cpu_model->model_private->action_heap);
442 xbt_swag_free(surf_cpu_model->model_private->modified_set);
444 surf_model_exit(surf_cpu_model);
445 surf_cpu_model = NULL;
447 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
448 cpu_running_action_set_that_does_not_need_being_checked = NULL;
451 static void surf_cpu_model_init_internal()
453 s_surf_action_t action;
454 s_surf_action_cpu_Cas01_t comp;
456 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
458 xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
460 surf_cpu_model = surf_model_init();
462 if (!strcmp(optim, "Full")) {
463 surf_cpu_model->model_private->update_mechanism = UM_FULL;
464 surf_cpu_model->model_private->selective_update = select;
465 } else if (!strcmp(optim, "Lazy")) {
466 surf_cpu_model->model_private->update_mechanism = UM_LAZY;
467 surf_cpu_model->model_private->selective_update = 1;
468 xbt_assert((select == 1)
470 (xbt_cfg_is_default_value
471 (_sg_cfg_set, "cpu/maxmin_selective_update")),
472 "Disabling selective update while using the lazy update mechanism is dumb!");
474 xbt_die("Unsupported optimization (%s) for this model", optim);
477 cpu_running_action_set_that_does_not_need_being_checked =
478 xbt_swag_new(xbt_swag_offset(action, state_hookup));
480 surf_cpu_model->name = "cpu";
482 surf_cpu_model->action_unref = surf_action_unref;
483 surf_cpu_model->action_cancel = surf_action_cancel;
484 surf_cpu_model->action_state_set = surf_action_state_set;
486 surf_cpu_model->model_private->resource_used = cpu_resource_used;
488 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
489 surf_cpu_model->model_private->share_resources =
490 cpu_share_resources_lazy;
491 surf_cpu_model->model_private->update_actions_state =
492 cpu_update_actions_state_lazy;
493 } else if (surf_cpu_model->model_private->update_mechanism == UM_FULL) {
494 surf_cpu_model->model_private->share_resources =
495 cpu_share_resources_full;
496 surf_cpu_model->model_private->update_actions_state =
497 cpu_update_actions_state_full;
499 xbt_die("Invalid cpu update mechanism!");
501 surf_cpu_model->model_private->update_resource_state =
502 cpu_update_resource_state;
503 surf_cpu_model->model_private->finalize = cpu_finalize;
505 surf_cpu_model->suspend = surf_action_suspend;
506 surf_cpu_model->resume = surf_action_resume;
507 surf_cpu_model->is_suspended = surf_action_is_suspended;
508 surf_cpu_model->set_max_duration = surf_action_set_max_duration;
509 surf_cpu_model->set_priority = surf_action_set_priority;
511 surf_cpu_model->set_category = surf_action_set_category;
513 surf_cpu_model->get_remains = surf_action_get_remains;
515 surf_cpu_model->extension.cpu.execute = cpu_execute;
516 surf_cpu_model->extension.cpu.sleep = cpu_action_sleep;
518 surf_cpu_model->extension.cpu.get_state = cpu_get_state;
519 surf_cpu_model->extension.cpu.get_speed = cpu_get_speed;
520 surf_cpu_model->extension.cpu.get_available_speed =
521 cpu_get_available_speed;
522 surf_cpu_model->extension.cpu.get_current_power_peak = cpu_get_current_power_peak;
523 surf_cpu_model->extension.cpu.get_power_peak_at = cpu_get_power_peak_at;
524 surf_cpu_model->extension.cpu.get_nb_pstates = cpu_get_nb_pstates;
525 surf_cpu_model->extension.cpu.set_power_peak_at = cpu_set_power_peak_at;
526 surf_cpu_model->extension.cpu.get_consumed_energy = cpu_get_consumed_energy;
528 surf_cpu_model->extension.cpu.add_traces = cpu_add_traces_cpu;
530 if (!surf_cpu_model->model_private->maxmin_system) {
531 surf_cpu_model->model_private->maxmin_system = lmm_system_new(surf_cpu_model->model_private->selective_update);
533 if (surf_cpu_model->model_private->update_mechanism == UM_LAZY) {
534 surf_cpu_model->model_private->action_heap = xbt_heap_new(8, NULL);
535 xbt_heap_set_update_callback(surf_cpu_model->model_private->action_heap,
536 surf_action_lmm_update_index_heap);
537 surf_cpu_model->model_private->modified_set =
538 xbt_swag_new(xbt_swag_offset(comp, generic_lmm_action.action_list_hookup));
539 surf_cpu_model->model_private->maxmin_system->keep_track = surf_cpu_model->model_private->modified_set;
543 /*********************************************************************/
544 /* Basic sharing model for CPU: that is where all this started... ;) */
545 /*********************************************************************/
546 /* @InProceedings{casanova01simgrid, */
547 /* author = "H. Casanova", */
548 /* booktitle = "Proceedings of the IEEE Symposium on Cluster Computing */
549 /* and the Grid (CCGrid'01)", */
550 /* publisher = "IEEE Computer Society", */
551 /* title = "Simgrid: {A} Toolkit for the Simulation of Application */
555 /* note = "Available at */
556 /* \url{http://grail.sdsc.edu/papers/simgrid_ccgrid01.ps.gz}." */
559 void surf_cpu_model_init_Cas01()
561 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
566 if (!strcmp(optim, "TI")) {
567 surf_cpu_model_init_ti();
571 surf_cpu_model_init_internal();
572 cpu_define_callbacks();
573 xbt_dynar_push(model_list, &surf_cpu_model);