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 "cpu_cas01.hpp"
10 #include "maxmin_private.h"
11 #include "simgrid/sg_config.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
15 "Logging specific to the SURF CPU IMPROVED module");
19 cpu_running_action_set_that_does_not_need_being_checked = NULL;
25 static void parse_cpu_init(sg_platf_host_cbarg_t host){
26 ((CpuCas01ModelPtr)surf_cpu_model)->parseInit(host);
29 static void cpu_add_traces_cpu(){
30 surf_cpu_model->addTraces();
33 static void cpu_define_callbacks()
35 sg_platf_host_add_cb(parse_cpu_init);
36 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
42 void surf_cpu_model_init_Cas01()
44 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
49 if (!strcmp(optim, "TI")) {
50 surf_cpu_model_init_ti();
54 surf_cpu_model = new CpuCas01Model();
55 cpu_define_callbacks();
56 ModelPtr model = static_cast<ModelPtr>(surf_cpu_model);
57 xbt_dynar_push(model_list, &model);
60 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
63 ActionLmmPtr actionlmm;
65 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
66 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
68 if (!strcmp(optim, "Full")) {
69 p_updateMechanism = UM_FULL;
70 m_selectiveUpdate = select;
71 } else if (!strcmp(optim, "Lazy")) {
72 p_updateMechanism = UM_LAZY;
73 m_selectiveUpdate = 1;
74 xbt_assert((select == 1)
76 (xbt_cfg_is_default_value
77 (_sg_cfg_set, "cpu/maxmin_selective_update")),
78 "Disabling selective update while using the lazy update mechanism is dumb!");
80 xbt_die("Unsupported optimization (%s) for this model", optim);
83 cpu_running_action_set_that_does_not_need_being_checked =
84 xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
86 if (p_updateMechanism == UM_LAZY) {
87 shareResources = &CpuCas01Model::shareResourcesLazy;
88 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
90 } else if (p_updateMechanism == UM_FULL) {
91 shareResources = &CpuCas01Model::shareResourcesFull;
92 updateActionsState = &CpuCas01Model::updateActionsStateFull;
94 xbt_die("Invalid cpu update mechanism!");
96 if (!p_maxminSystem) {
97 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
100 if (p_updateMechanism == UM_LAZY) {
101 p_actionHeap = xbt_heap_new(8, NULL);
102 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
103 p_modifiedSet = xbt_swag_new(xbt_swag_offset(*actionlmm, p_actionListHookup));
104 p_maxminSystem->keep_track = p_modifiedSet;
108 CpuCas01Model::~CpuCas01Model()
110 lmm_system_free(p_maxminSystem);
111 p_maxminSystem = NULL;
114 xbt_heap_free(p_actionHeap);
115 xbt_swag_free(p_modifiedSet);
117 surf_cpu_model = NULL;
119 xbt_swag_free(cpu_running_action_set_that_does_not_need_being_checked);
120 cpu_running_action_set_that_does_not_need_being_checked = NULL;
123 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
125 createResource(host->id,
136 CpuCas01LmmPtr CpuCas01Model::createResource(const char *name, xbt_dynar_t power_peak,
137 int pstate, double power_scale,
138 tmgr_trace_t power_trace, int core,
139 e_surf_resource_state_t state_initial,
140 tmgr_trace_t state_trace,
141 xbt_dict_t cpu_properties)
144 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
145 "Host '%s' declared several times in the platform file",
147 xbt_assert(power_peak > 0, "Power has to be >0");
148 xbt_assert(core > 0, "Invalid number of cores %d", core);
150 cpu = new CpuCas01Lmm(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
151 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
153 return (CpuCas01LmmPtr) xbt_lib_get_elm_or_null(host_lib, name);
156 double CpuCas01Model::shareResourcesFull(double now)
158 return Model::shareResourcesMaxMin(p_runningActionSet,
159 p_maxminSystem, lmm_solve);
162 void CpuCas01Model::addTraces()
164 xbt_dict_cursor_t cursor = NULL;
165 char *trace_name, *elm;
166 static int called = 0;
171 /* connect all traces relative to hosts */
172 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
173 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
174 CpuCas01LmmPtr host = static_cast<CpuCas01LmmPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
176 xbt_assert(host, "Host %s undefined", elm);
177 xbt_assert(trace, "Trace %s undefined", trace_name);
180 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
183 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
184 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
185 CpuCas01LmmPtr host = dynamic_cast<CpuCas01LmmPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
187 xbt_assert(host, "Host %s undefined", elm);
188 xbt_assert(trace, "Trace %s undefined", trace_name);
191 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
198 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, xbt_dynar_t powerPeak,
199 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
200 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
201 xbt_dict_t properties) :
202 CpuLmm(model, name, properties), Resource(model, name, properties) {
203 m_powerPeak = xbt_dynar_get_as(powerPeak, pstate, double);
204 p_powerPeakList = powerPeak;
207 p_energy = xbt_new(s_energy_cpu_cas01_t, 1);
208 p_energy->total_energy = 0;
209 p_energy->power_range_watts_list = getWattsRangeList();
210 p_energy->last_updated = surf_get_clock();
212 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
214 m_powerScale = powerScale;
216 p_stateCurrent = stateInitial;
218 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
221 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
223 p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
226 CpuCas01Lmm::~CpuCas01Lmm(){
228 xbt_dynar_t power_tuple = NULL;
229 xbt_dynar_foreach(p_energy->power_range_watts_list, iter, power_tuple)
230 xbt_dynar_free(&power_tuple);
231 xbt_dynar_free(&p_energy->power_range_watts_list);
232 xbt_dynar_free(&p_powerPeakList);
237 bool CpuCas01Lmm::isUsed()
239 return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
242 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
244 lmm_variable_t var = NULL;
245 lmm_element_t elem = NULL;
247 if (event_type == p_powerEvent) {
248 m_powerScale = value;
249 lmm_update_constraint_bound(surf_cpu_model->p_maxminSystem, p_constraint,
250 m_core * m_powerScale *
253 TRACE_surf_host_set_power(date, m_name,
254 m_core * m_powerScale *
257 while ((var = lmm_get_var_from_cnst
258 (surf_cpu_model->p_maxminSystem, p_constraint, &elem))) {
259 CpuCas01ActionLmmPtr action = static_cast<CpuCas01ActionLmmPtr>(static_cast<ActionLmmPtr>(lmm_variable_id(var)));
261 lmm_update_variable_bound(surf_cpu_model->p_maxminSystem,
263 m_powerScale * m_powerPeak);
265 if (tmgr_trace_event_free(event_type))
267 } else if (event_type == p_stateEvent) {
269 if(p_stateCurrent == SURF_RESOURCE_OFF)
270 xbt_dynar_push_as(host_that_restart, char*, (char *)m_name);
271 p_stateCurrent = SURF_RESOURCE_ON;
273 lmm_constraint_t cnst = p_constraint;
275 p_stateCurrent = SURF_RESOURCE_OFF;
277 while ((var = lmm_get_var_from_cnst(surf_cpu_model->p_maxminSystem, cnst, &elem))) {
278 ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
280 if (action->getState() == SURF_ACTION_RUNNING ||
281 action->getState() == SURF_ACTION_READY ||
282 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
283 action->m_finish = date;
284 action->setState(SURF_ACTION_FAILED);
288 if (tmgr_trace_event_free(event_type))
291 XBT_CRITICAL("Unknown event ! \n");
298 ActionPtr CpuCas01Lmm::execute(double size)
301 XBT_IN("(%s,%g)", m_name, size);
302 CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model, size, p_stateCurrent != SURF_RESOURCE_ON);
304 action->m_suspended = 0; /* Should be useless because of the
305 calloc but it seems to help valgrind... */
308 lmm_variable_new(surf_cpu_model->p_maxminSystem, static_cast<ActionLmmPtr>(action),
310 m_powerScale * m_powerPeak, 1);
311 if (surf_cpu_model->p_updateMechanism == UM_LAZY) {
312 action->m_indexHeap = -1;
313 action->m_lastUpdate = surf_get_clock();
314 action->m_lastValue = 0.0;
316 lmm_expand(surf_cpu_model->p_maxminSystem, p_constraint,
317 action->p_variable, 1.0);
322 ActionPtr CpuCas01Lmm::sleep(double duration)
325 duration = MAX(duration, MAXMIN_PRECISION);
327 XBT_IN("(%s,%g)", m_name, duration);
328 CpuCas01ActionLmmPtr action = dynamic_cast<CpuCas01ActionLmmPtr>(execute(1.0));
330 // FIXME: sleep variables should not consume 1.0 in lmm_expand
331 action->m_maxDuration = duration;
332 action->m_suspended = 2;
333 if (duration == NO_MAX_DURATION) {
334 /* Move to the *end* of the corresponding action set. This convention
335 is used to speed up update_resource_state */
336 xbt_swag_remove(static_cast<ActionPtr>(action), action->p_stateSet);
337 action->p_stateSet = cpu_running_action_set_that_does_not_need_being_checked;
338 xbt_swag_insert(static_cast<ActionPtr>(action), action->p_stateSet);
341 lmm_update_variable_weight(surf_cpu_model->p_maxminSystem,
342 action->p_variable, 0.0);
343 if (surf_cpu_model->p_updateMechanism == UM_LAZY) { // remove action from the heap
344 action->heapRemove(surf_cpu_model->p_actionHeap);
345 // this is necessary for a variable with weight 0 since such
346 // variables are ignored in lmm and we need to set its max_duration
347 // correctly at the next call to share_resources
348 xbt_swag_insert_at_head(static_cast<ActionLmmPtr>(action), surf_cpu_model->p_modifiedSet);
355 xbt_dynar_t CpuCas01Lmm::getWattsRangeList()
357 xbt_dynar_t power_range_list;
358 xbt_dynar_t power_tuple;
359 int i = 0, pstate_nb=0;
360 xbt_dynar_t current_power_values;
361 double min_power, max_power;
363 if (m_properties == NULL)
366 char* all_power_values_str = (char*)xbt_dict_get_or_null(m_properties, "power_per_state");
368 if (all_power_values_str == NULL)
372 power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
373 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
375 pstate_nb = xbt_dynar_length(all_power_values);
376 for (i=0; i< pstate_nb; i++)
378 /* retrieve the power values associated with the current pstate */
379 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
380 xbt_assert(xbt_dynar_length(current_power_values) > 1,
381 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
384 /* min_power corresponds to the idle power (cpu load = 0) */
385 /* max_power is the power consumed at 100% cpu load */
386 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
387 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
389 power_tuple = xbt_dynar_new(sizeof(double), NULL);
390 xbt_dynar_push_as(power_tuple, double, min_power);
391 xbt_dynar_push_as(power_tuple, double, max_power);
393 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
394 xbt_dynar_free(¤t_power_values);
396 xbt_dynar_free(&all_power_values);
397 return power_range_list;
401 * Computes the power consumed by the host according to the current pstate and processor load
404 double CpuCas01Lmm::getCurrentWattsValue(double cpu_load)
406 xbt_dynar_t power_range_list = p_energy->power_range_watts_list;
408 if (power_range_list == NULL)
410 XBT_DEBUG("No power range properties specified for host %s", m_name);
413 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(p_powerPeakList),
414 "The number of power ranges in the properties does not match the number of pstates for host %s",
417 /* retrieve the power values associated with the current pstate */
418 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, m_pstate, xbt_dynar_t);
420 /* min_power corresponds to the idle power (cpu load = 0) */
421 /* max_power is the power consumed at 100% cpu load */
422 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
423 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
424 double power_slope = max_power - min_power;
426 double current_power = min_power + cpu_load * power_slope;
428 XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
429 XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
431 return current_power;
435 * Updates the total energy consumed as the sum of the current energy and
436 * the energy consumed by the current action
438 void CpuCas01Lmm::updateEnergy(double cpu_load)
440 double start_time = p_energy->last_updated;
441 double finish_time = surf_get_clock();
443 XBT_DEBUG("[cpu_update_energy] action time interval=(%f-%f), current power peak=%f, current pstate=%d",
444 start_time, finish_time, m_powerPeak, m_pstate);
445 double current_energy = p_energy->total_energy;
446 double action_energy = getCurrentWattsValue(cpu_load)*(finish_time-start_time);
448 p_energy->total_energy = current_energy + action_energy;
449 p_energy->last_updated = finish_time;
451 XBT_DEBUG("[cpu_update_energy] old_energy_value=%f, action_energy_value=%f", current_energy, action_energy);
454 double CpuCas01Lmm::getCurrentPowerPeak()
459 double CpuCas01Lmm::getPowerPeakAt(int pstate_index)
461 xbt_dynar_t plist = p_powerPeakList;
462 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
464 return xbt_dynar_get_as(plist, pstate_index, double);
467 int CpuCas01Lmm::getNbPstates()
469 return xbt_dynar_length(p_powerPeakList);
472 void CpuCas01Lmm::setPowerPeakAt(int pstate_index)
474 xbt_dynar_t plist = p_powerPeakList;
475 xbt_assert((pstate_index <= xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
477 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
478 m_pstate = pstate_index;
479 m_powerPeak = new_power_peak;
482 double CpuCas01Lmm::getConsumedEnergy()
484 return p_energy->total_energy;
492 * Update the CPU total energy for a finished action
495 void CpuCas01ActionLmm::updateEnergy()
497 CpuCas01LmmPtr cpu = static_cast<CpuCas01LmmPtr>(lmm_constraint_id(lmm_get_cnst_from_var
498 (p_model->p_maxminSystem,
501 if(cpu->p_energy->last_updated < surf_get_clock()) {
502 double load = lmm_constraint_get_usage(cpu->p_constraint) / cpu->m_powerPeak;
503 cpu->updateEnergy(load);