1 /* Copyright (c) 2009-2011, 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 "cpu_cas01.hpp"
9 #include "maxmin_private.h"
10 #include "simgrid/sg_config.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
13 "Logging specific to the SURF CPU IMPROVED module");
19 static void parse_cpu_init(sg_platf_host_cbarg_t host){
20 ((CpuCas01ModelPtr)surf_cpu_model_pm)->parseInit(host);
23 static void cpu_add_traces_cpu(){
24 surf_cpu_model_pm->addTraces();
27 static void cpu_define_callbacks()
29 sg_platf_host_add_cb(parse_cpu_init);
30 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
36 void surf_cpu_model_init_Cas01()
38 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
40 xbt_assert(!surf_cpu_model_pm);
41 xbt_assert(!surf_cpu_model_vm);
43 if (!strcmp(optim, "TI")) {
44 surf_cpu_model_init_ti();
48 surf_cpu_model_pm = new CpuCas01Model();
49 surf_cpu_model_vm = new CpuCas01Model();
51 cpu_define_callbacks();
52 ModelPtr model_pm = static_cast<ModelPtr>(surf_cpu_model_pm);
53 ModelPtr model_vm = static_cast<ModelPtr>(surf_cpu_model_vm);
54 xbt_dynar_push(model_list, &model_pm);
55 xbt_dynar_push(model_list, &model_vm);
58 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
60 ActionPtr action = NULL;
61 ActionLmmPtr actionlmm = NULL;
63 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
64 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
66 if (!strcmp(optim, "Full")) {
67 p_updateMechanism = UM_FULL;
68 m_selectiveUpdate = select;
69 } else if (!strcmp(optim, "Lazy")) {
70 p_updateMechanism = UM_LAZY;
71 m_selectiveUpdate = 1;
72 xbt_assert((select == 1)
74 (xbt_cfg_is_default_value
75 (_sg_cfg_set, "cpu/maxmin_selective_update")),
76 "Disabling selective update while using the lazy update mechanism is dumb!");
78 xbt_die("Unsupported optimization (%s) for this model", optim);
81 p_cpuRunningActionSetThatDoesNotNeedBeingChecked =
82 xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
84 if (p_updateMechanism == UM_LAZY) {
85 shareResources = &CpuCas01Model::shareResourcesLazy;
86 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
88 } else if (p_updateMechanism == UM_FULL) {
89 shareResources = &CpuCas01Model::shareResourcesFull;
90 updateActionsState = &CpuCas01Model::updateActionsStateFull;
92 xbt_die("Invalid cpu update mechanism!");
94 if (!p_maxminSystem) {
95 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
98 if (p_updateMechanism == UM_LAZY) {
99 p_actionHeap = xbt_heap_new(8, NULL);
100 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
101 p_modifiedSet = xbt_swag_new(xbt_swag_offset(*actionlmm, p_actionListHookup));
102 p_maxminSystem->keep_track = p_modifiedSet;
106 CpuCas01Model::~CpuCas01Model()
108 lmm_system_free(p_maxminSystem);
109 p_maxminSystem = NULL;
112 xbt_heap_free(p_actionHeap);
113 xbt_swag_free(p_modifiedSet);
115 surf_cpu_model_pm = NULL;
117 xbt_swag_free(p_cpuRunningActionSetThatDoesNotNeedBeingChecked);
120 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
122 createResource(host->id,
133 CpuPtr CpuCas01Model::createResource(const char *name, xbt_dynar_t power_peak,
134 int pstate, double power_scale,
135 tmgr_trace_t power_trace, int core,
136 e_surf_resource_state_t state_initial,
137 tmgr_trace_t state_trace,
138 xbt_dict_t cpu_properties)
141 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
142 "Host '%s' declared several times in the platform file",
144 xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
145 "Power has to be >0.0");
146 xbt_assert(core > 0, "Invalid number of cores %d", core);
148 cpu = new CpuCas01Lmm(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
149 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
154 double CpuCas01Model::shareResourcesFull(double /*now*/)
156 return Model::shareResourcesMaxMin(p_runningActionSet,
157 p_maxminSystem, lmm_solve);
160 void CpuCas01Model::addTraces()
162 xbt_dict_cursor_t cursor = NULL;
163 char *trace_name, *elm;
164 static int called = 0;
169 /* connect all traces relative to hosts */
170 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
171 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
172 CpuCas01LmmPtr host = static_cast<CpuCas01LmmPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
174 xbt_assert(host, "Host %s undefined", elm);
175 xbt_assert(trace, "Trace %s undefined", trace_name);
178 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
181 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
182 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
183 CpuCas01LmmPtr host = dynamic_cast<CpuCas01LmmPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
185 xbt_assert(host, "Host %s undefined", elm);
186 xbt_assert(trace, "Trace %s undefined", trace_name);
189 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
196 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, xbt_dynar_t powerPeak,
197 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
198 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
199 xbt_dict_t properties)
200 : Resource(model, name, properties)
201 , CpuLmm(model, name, properties, core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
203 p_powerPeakList = powerPeak;
206 p_energy = xbt_new(s_energy_cpu_cas01_t, 1);
207 p_energy->total_energy = 0;
208 p_energy->power_range_watts_list = getWattsRangeList();
209 p_energy->last_updated = surf_get_clock();
211 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
214 p_stateCurrent = stateInitial;
216 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
219 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
221 p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
224 CpuCas01Lmm::~CpuCas01Lmm(){
226 xbt_dynar_t power_tuple = NULL;
227 xbt_dynar_foreach(p_energy->power_range_watts_list, iter, power_tuple)
228 xbt_dynar_free(&power_tuple);
229 xbt_dynar_free(&p_energy->power_range_watts_list);
230 xbt_dynar_free(&p_powerPeakList);
234 bool CpuCas01Lmm::isUsed()
236 return lmm_constraint_used(p_model->p_maxminSystem, p_constraint);
239 void CpuCas01Lmm::updateState(tmgr_trace_event_t event_type, double value, double date)
241 lmm_variable_t var = NULL;
242 lmm_element_t elem = NULL;
244 if (event_type == p_powerEvent) {
245 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
246 xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
248 m_powerScale = value;
249 lmm_update_constraint_bound(surf_cpu_model_pm->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_pm->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_pm->p_maxminSystem,
263 m_powerScale * m_powerPeak);
265 if (tmgr_trace_event_free(event_type))
267 } else if (event_type == p_stateEvent) {
268 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
269 xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
272 if(p_stateCurrent == SURF_RESOURCE_OFF)
273 xbt_dynar_push_as(host_that_restart, char*, (char *)m_name);
274 p_stateCurrent = SURF_RESOURCE_ON;
276 lmm_constraint_t cnst = p_constraint;
278 p_stateCurrent = SURF_RESOURCE_OFF;
280 while ((var = lmm_get_var_from_cnst(surf_cpu_model_pm->p_maxminSystem, cnst, &elem))) {
281 ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
283 if (action->getState() == SURF_ACTION_RUNNING ||
284 action->getState() == SURF_ACTION_READY ||
285 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
286 action->m_finish = date;
287 action->setState(SURF_ACTION_FAILED);
291 if (tmgr_trace_event_free(event_type))
294 XBT_CRITICAL("Unknown event ! \n");
301 ActionPtr CpuCas01Lmm::execute(double size)
304 XBT_IN("(%s,%g)", m_name, size);
305 CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model_pm, size, p_stateCurrent != SURF_RESOURCE_ON);
307 action->m_suspended = 0; /* Should be useless because of the
308 calloc but it seems to help valgrind... */
311 lmm_variable_new(surf_cpu_model_pm->p_maxminSystem, static_cast<ActionLmmPtr>(action),
313 m_powerScale * m_powerPeak, 1);
314 if (surf_cpu_model_pm->p_updateMechanism == UM_LAZY) {
315 action->m_indexHeap = -1;
316 action->m_lastUpdate = surf_get_clock();
317 action->m_lastValue = 0.0;
319 lmm_expand(surf_cpu_model_pm->p_maxminSystem, p_constraint,
320 action->p_variable, 1.0);
325 ActionPtr CpuCas01Lmm::sleep(double duration)
328 duration = MAX(duration, MAXMIN_PRECISION);
330 XBT_IN("(%s,%g)", m_name, duration);
331 CpuCas01ActionLmmPtr action = dynamic_cast<CpuCas01ActionLmmPtr>(execute(1.0));
333 // FIXME: sleep variables should not consume 1.0 in lmm_expand
334 action->m_maxDuration = duration;
335 action->m_suspended = 2;
336 if (duration == NO_MAX_DURATION) {
337 /* Move to the *end* of the corresponding action set. This convention
338 is used to speed up update_resource_state */
339 xbt_swag_remove(static_cast<ActionPtr>(action), action->p_stateSet);
340 action->p_stateSet = static_cast<CpuCas01ModelPtr>(p_model)->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
341 xbt_swag_insert(static_cast<ActionPtr>(action), action->p_stateSet);
344 lmm_update_variable_weight(surf_cpu_model_pm->p_maxminSystem,
345 action->p_variable, 0.0);
346 if (surf_cpu_model_pm->p_updateMechanism == UM_LAZY) { // remove action from the heap
347 action->heapRemove(surf_cpu_model_pm->p_actionHeap);
348 // this is necessary for a variable with weight 0 since such
349 // variables are ignored in lmm and we need to set its max_duration
350 // correctly at the next call to share_resources
351 xbt_swag_insert_at_head(static_cast<ActionLmmPtr>(action), surf_cpu_model_pm->p_modifiedSet);
358 xbt_dynar_t CpuCas01Lmm::getWattsRangeList()
360 xbt_dynar_t power_range_list;
361 xbt_dynar_t power_tuple;
362 int i = 0, pstate_nb=0;
363 xbt_dynar_t current_power_values;
364 double min_power, max_power;
366 if (m_properties == NULL)
369 char* all_power_values_str = (char*)xbt_dict_get_or_null(m_properties, "power_per_state");
371 if (all_power_values_str == NULL)
375 power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
376 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
378 pstate_nb = xbt_dynar_length(all_power_values);
379 for (i=0; i< pstate_nb; i++)
381 /* retrieve the power values associated with the current pstate */
382 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
383 xbt_assert(xbt_dynar_length(current_power_values) > 1,
384 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
387 /* min_power corresponds to the idle power (cpu load = 0) */
388 /* max_power is the power consumed at 100% cpu load */
389 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
390 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
392 power_tuple = xbt_dynar_new(sizeof(double), NULL);
393 xbt_dynar_push_as(power_tuple, double, min_power);
394 xbt_dynar_push_as(power_tuple, double, max_power);
396 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
397 xbt_dynar_free(¤t_power_values);
399 xbt_dynar_free(&all_power_values);
400 return power_range_list;
404 * Computes the power consumed by the host according to the current pstate and processor load
407 double CpuCas01Lmm::getCurrentWattsValue(double cpu_load)
409 xbt_dynar_t power_range_list = p_energy->power_range_watts_list;
411 if (power_range_list == NULL)
413 XBT_DEBUG("No power range properties specified for host %s", m_name);
416 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(p_powerPeakList),
417 "The number of power ranges in the properties does not match the number of pstates for host %s",
420 /* retrieve the power values associated with the current pstate */
421 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, m_pstate, xbt_dynar_t);
423 /* min_power corresponds to the idle power (cpu load = 0) */
424 /* max_power is the power consumed at 100% cpu load */
425 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
426 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
427 double power_slope = max_power - min_power;
429 double current_power = min_power + cpu_load * power_slope;
431 XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
432 XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
434 return current_power;
438 * Updates the total energy consumed as the sum of the current energy and
439 * the energy consumed by the current action
441 void CpuCas01Lmm::updateEnergy(double cpu_load)
443 double start_time = p_energy->last_updated;
444 double finish_time = surf_get_clock();
446 XBT_DEBUG("[cpu_update_energy] action time interval=(%f-%f), current power peak=%f, current pstate=%d",
447 start_time, finish_time, m_powerPeak, m_pstate);
448 double current_energy = p_energy->total_energy;
449 double action_energy = getCurrentWattsValue(cpu_load)*(finish_time-start_time);
451 p_energy->total_energy = current_energy + action_energy;
452 p_energy->last_updated = finish_time;
454 XBT_DEBUG("[cpu_update_energy] old_energy_value=%f, action_energy_value=%f", current_energy, action_energy);
457 double CpuCas01Lmm::getCurrentPowerPeak()
462 double CpuCas01Lmm::getPowerPeakAt(int pstate_index)
464 xbt_dynar_t plist = p_powerPeakList;
465 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
467 return xbt_dynar_get_as(plist, pstate_index, double);
470 int CpuCas01Lmm::getNbPstates()
472 return xbt_dynar_length(p_powerPeakList);
475 void CpuCas01Lmm::setPowerPeakAt(int pstate_index)
477 xbt_dynar_t plist = p_powerPeakList;
478 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
480 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
481 m_pstate = pstate_index;
482 m_powerPeak = new_power_peak;
485 double CpuCas01Lmm::getConsumedEnergy()
487 return p_energy->total_energy;
495 * Update the CPU total energy for a finished action
498 void CpuCas01ActionLmm::updateEnergy()
500 CpuCas01LmmPtr cpu = static_cast<CpuCas01LmmPtr>(lmm_constraint_id(lmm_get_cnst_from_var
501 (p_model->p_maxminSystem,
504 if(cpu->p_energy->last_updated < surf_get_clock()) {
505 double load = lmm_constraint_get_usage(cpu->p_constraint) / cpu->m_powerPeak;
506 cpu->updateEnergy(load);