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"
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");
22 static void parse_cpu_init(sg_platf_host_cbarg_t host){
23 ((CpuCas01ModelPtr)surf_cpu_model_pm)->parseInit(host);
26 static void cpu_add_traces_cpu(){
27 surf_cpu_model_pm->addTraces();
30 static void cpu_define_callbacks()
32 sg_platf_host_add_cb(parse_cpu_init);
33 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
39 void surf_cpu_model_init_Cas01()
41 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
43 xbt_assert(!surf_cpu_model_pm);
44 xbt_assert(!surf_cpu_model_vm);
46 if (!strcmp(optim, "TI")) {
47 surf_cpu_model_init_ti();
51 surf_cpu_model_pm = new CpuCas01Model();
52 surf_cpu_model_vm = new CpuCas01Model();
54 cpu_define_callbacks();
55 ModelPtr model_pm = static_cast<ModelPtr>(surf_cpu_model_pm);
56 ModelPtr model_vm = static_cast<ModelPtr>(surf_cpu_model_vm);
57 xbt_dynar_push(model_list, &model_pm);
58 xbt_dynar_push(model_list, &model_vm);
61 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
63 ActionPtr action = NULL;
64 ActionLmmPtr actionlmm = NULL;
66 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
67 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
69 if (!strcmp(optim, "Full")) {
70 p_updateMechanism = UM_FULL;
71 m_selectiveUpdate = select;
72 } else if (!strcmp(optim, "Lazy")) {
73 p_updateMechanism = UM_LAZY;
74 m_selectiveUpdate = 1;
75 xbt_assert((select == 1)
77 (xbt_cfg_is_default_value
78 (_sg_cfg_set, "cpu/maxmin_selective_update")),
79 "Disabling selective update while using the lazy update mechanism is dumb!");
81 xbt_die("Unsupported optimization (%s) for this model", optim);
84 p_cpuRunningActionSetThatDoesNotNeedBeingChecked =
85 xbt_swag_new(xbt_swag_offset(*action, p_stateHookup));
87 if (p_updateMechanism == UM_LAZY) {
88 shareResources = &CpuCas01Model::shareResourcesLazy;
89 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
91 } else if (p_updateMechanism == UM_FULL) {
92 shareResources = &CpuCas01Model::shareResourcesFull;
93 updateActionsState = &CpuCas01Model::updateActionsStateFull;
95 xbt_die("Invalid cpu update mechanism!");
97 if (!p_maxminSystem) {
98 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
101 if (p_updateMechanism == UM_LAZY) {
102 p_actionHeap = xbt_heap_new(8, NULL);
103 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
104 p_modifiedSet = xbt_swag_new(xbt_swag_offset(*actionlmm, p_actionListHookup));
105 p_maxminSystem->keep_track = p_modifiedSet;
109 CpuCas01Model::~CpuCas01Model()
111 lmm_system_free(p_maxminSystem);
112 p_maxminSystem = NULL;
115 xbt_heap_free(p_actionHeap);
116 xbt_swag_free(p_modifiedSet);
118 surf_cpu_model_pm = NULL;
120 xbt_swag_free(p_cpuRunningActionSetThatDoesNotNeedBeingChecked);
123 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
125 createResource(host->id,
136 CpuPtr 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(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
148 "Power has to be >0.0");
149 xbt_assert(core > 0, "Invalid number of cores %d", core);
151 cpu = new CpuCas01Lmm(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
152 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
157 double CpuCas01Model::shareResourcesFull(double /*now*/)
159 return Model::shareResourcesMaxMin(p_runningActionSet,
160 p_maxminSystem, lmm_solve);
163 void CpuCas01Model::addTraces()
165 xbt_dict_cursor_t cursor = NULL;
166 char *trace_name, *elm;
167 static int called = 0;
172 /* connect all traces relative to hosts */
173 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
174 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
175 CpuCas01LmmPtr host = static_cast<CpuCas01LmmPtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
177 xbt_assert(host, "Host %s undefined", elm);
178 xbt_assert(trace, "Trace %s undefined", trace_name);
181 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
184 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
185 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
186 CpuCas01LmmPtr host = dynamic_cast<CpuCas01LmmPtr>(static_cast<ResourcePtr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm))));
188 xbt_assert(host, "Host %s undefined", elm);
189 xbt_assert(trace, "Trace %s undefined", trace_name);
192 tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host));
199 CpuCas01Lmm::CpuCas01Lmm(CpuCas01ModelPtr model, const char *name, xbt_dynar_t powerPeak,
200 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
201 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
202 xbt_dict_t properties)
203 : Resource(model, name, properties)
204 , CpuLmm(model, name, properties, core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
206 p_powerPeakList = powerPeak;
209 p_energy = xbt_new(s_energy_cpu_cas01_t, 1);
210 p_energy->total_energy = 0;
211 p_energy->power_range_watts_list = getWattsRangeList();
212 p_energy->last_updated = surf_get_clock();
214 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
217 p_stateCurrent = stateInitial;
219 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
222 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
224 p_constraint = lmm_constraint_new(p_model->p_maxminSystem, this, m_core * m_powerScale * m_powerPeak);
227 CpuCas01Lmm::~CpuCas01Lmm(){
229 xbt_dynar_t power_tuple = NULL;
230 xbt_dynar_foreach(p_energy->power_range_watts_list, iter, power_tuple)
231 xbt_dynar_free(&power_tuple);
232 xbt_dynar_free(&p_energy->power_range_watts_list);
233 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 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
249 xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
251 m_powerScale = value;
252 lmm_update_constraint_bound(surf_cpu_model_pm->p_maxminSystem, p_constraint,
253 m_core * m_powerScale *
256 TRACE_surf_host_set_power(date, m_name,
257 m_core * m_powerScale *
260 while ((var = lmm_get_var_from_cnst
261 (surf_cpu_model_pm->p_maxminSystem, p_constraint, &elem))) {
262 CpuCas01ActionLmmPtr action = static_cast<CpuCas01ActionLmmPtr>(static_cast<ActionLmmPtr>(lmm_variable_id(var)));
264 lmm_update_variable_bound(surf_cpu_model_pm->p_maxminSystem,
266 m_powerScale * m_powerPeak);
268 if (tmgr_trace_event_free(event_type))
270 } else if (event_type == p_stateEvent) {
271 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
272 xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
275 if(p_stateCurrent == SURF_RESOURCE_OFF)
276 xbt_dynar_push_as(host_that_restart, char*, (char *)m_name);
277 p_stateCurrent = SURF_RESOURCE_ON;
279 lmm_constraint_t cnst = p_constraint;
281 p_stateCurrent = SURF_RESOURCE_OFF;
283 while ((var = lmm_get_var_from_cnst(surf_cpu_model_pm->p_maxminSystem, cnst, &elem))) {
284 ActionLmmPtr action = static_cast<ActionLmmPtr>(lmm_variable_id(var));
286 if (action->getState() == SURF_ACTION_RUNNING ||
287 action->getState() == SURF_ACTION_READY ||
288 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
289 action->m_finish = date;
290 action->setState(SURF_ACTION_FAILED);
294 if (tmgr_trace_event_free(event_type))
297 XBT_CRITICAL("Unknown event ! \n");
304 ActionPtr CpuCas01Lmm::execute(double size)
307 XBT_IN("(%s,%g)", m_name, size);
308 CpuCas01ActionLmmPtr action = new CpuCas01ActionLmm(surf_cpu_model_pm, size, p_stateCurrent != SURF_RESOURCE_ON);
310 action->m_suspended = 0; /* Should be useless because of the
311 calloc but it seems to help valgrind... */
314 lmm_variable_new(surf_cpu_model_pm->p_maxminSystem, static_cast<ActionLmmPtr>(action),
316 m_powerScale * m_powerPeak, 1);
317 if (surf_cpu_model_pm->p_updateMechanism == UM_LAZY) {
318 action->m_indexHeap = -1;
319 action->m_lastUpdate = surf_get_clock();
320 action->m_lastValue = 0.0;
322 lmm_expand(surf_cpu_model_pm->p_maxminSystem, p_constraint,
323 action->p_variable, 1.0);
328 ActionPtr CpuCas01Lmm::sleep(double duration)
331 duration = MAX(duration, MAXMIN_PRECISION);
333 XBT_IN("(%s,%g)", m_name, duration);
334 CpuCas01ActionLmmPtr action = dynamic_cast<CpuCas01ActionLmmPtr>(execute(1.0));
336 // FIXME: sleep variables should not consume 1.0 in lmm_expand
337 action->m_maxDuration = duration;
338 action->m_suspended = 2;
339 if (duration == NO_MAX_DURATION) {
340 /* Move to the *end* of the corresponding action set. This convention
341 is used to speed up update_resource_state */
342 xbt_swag_remove(static_cast<ActionPtr>(action), action->p_stateSet);
343 action->p_stateSet = static_cast<CpuCas01ModelPtr>(p_model)->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
344 xbt_swag_insert(static_cast<ActionPtr>(action), action->p_stateSet);
347 lmm_update_variable_weight(surf_cpu_model_pm->p_maxminSystem,
348 action->p_variable, 0.0);
349 if (surf_cpu_model_pm->p_updateMechanism == UM_LAZY) { // remove action from the heap
350 action->heapRemove(surf_cpu_model_pm->p_actionHeap);
351 // this is necessary for a variable with weight 0 since such
352 // variables are ignored in lmm and we need to set its max_duration
353 // correctly at the next call to share_resources
354 xbt_swag_insert_at_head(static_cast<ActionLmmPtr>(action), surf_cpu_model_pm->p_modifiedSet);
361 xbt_dynar_t CpuCas01Lmm::getWattsRangeList()
363 xbt_dynar_t power_range_list;
364 xbt_dynar_t power_tuple;
365 int i = 0, pstate_nb=0;
366 xbt_dynar_t current_power_values;
367 double min_power, max_power;
369 if (m_properties == NULL)
372 char* all_power_values_str = (char*)xbt_dict_get_or_null(m_properties, "power_per_state");
374 if (all_power_values_str == NULL)
378 power_range_list = xbt_dynar_new(sizeof(xbt_dynar_t), NULL);
379 xbt_dynar_t all_power_values = xbt_str_split(all_power_values_str, ",");
381 pstate_nb = xbt_dynar_length(all_power_values);
382 for (i=0; i< pstate_nb; i++)
384 /* retrieve the power values associated with the current pstate */
385 current_power_values = xbt_str_split(xbt_dynar_get_as(all_power_values, i, char*), ":");
386 xbt_assert(xbt_dynar_length(current_power_values) > 1,
387 "Power properties incorrectly defined - could not retrieve min and max power values for host %s",
390 /* min_power corresponds to the idle power (cpu load = 0) */
391 /* max_power is the power consumed at 100% cpu load */
392 min_power = atof(xbt_dynar_get_as(current_power_values, 0, char*));
393 max_power = atof(xbt_dynar_get_as(current_power_values, 1, char*));
395 power_tuple = xbt_dynar_new(sizeof(double), NULL);
396 xbt_dynar_push_as(power_tuple, double, min_power);
397 xbt_dynar_push_as(power_tuple, double, max_power);
399 xbt_dynar_push_as(power_range_list, xbt_dynar_t, power_tuple);
400 xbt_dynar_free(¤t_power_values);
402 xbt_dynar_free(&all_power_values);
403 return power_range_list;
407 * Computes the power consumed by the host according to the current pstate and processor load
410 double CpuCas01Lmm::getCurrentWattsValue(double cpu_load)
412 xbt_dynar_t power_range_list = p_energy->power_range_watts_list;
414 if (power_range_list == NULL)
416 XBT_DEBUG("No power range properties specified for host %s", m_name);
419 xbt_assert(xbt_dynar_length(power_range_list) == xbt_dynar_length(p_powerPeakList),
420 "The number of power ranges in the properties does not match the number of pstates for host %s",
423 /* retrieve the power values associated with the current pstate */
424 xbt_dynar_t current_power_values = xbt_dynar_get_as(power_range_list, m_pstate, xbt_dynar_t);
426 /* min_power corresponds to the idle power (cpu load = 0) */
427 /* max_power is the power consumed at 100% cpu load */
428 double min_power = xbt_dynar_get_as(current_power_values, 0, double);
429 double max_power = xbt_dynar_get_as(current_power_values, 1, double);
430 double power_slope = max_power - min_power;
432 double current_power = min_power + cpu_load * power_slope;
434 XBT_DEBUG("[get_current_watts] min_power=%f, max_power=%f, slope=%f", min_power, max_power, power_slope);
435 XBT_DEBUG("[get_current_watts] Current power (watts) = %f, load = %f", current_power, cpu_load);
437 return current_power;
441 * Updates the total energy consumed as the sum of the current energy and
442 * the energy consumed by the current action
444 void CpuCas01Lmm::updateEnergy(double cpu_load)
446 double start_time = p_energy->last_updated;
447 double finish_time = surf_get_clock();
449 XBT_DEBUG("[cpu_update_energy] action time interval=(%f-%f), current power peak=%f, current pstate=%d",
450 start_time, finish_time, m_powerPeak, m_pstate);
451 double current_energy = p_energy->total_energy;
452 double action_energy = getCurrentWattsValue(cpu_load)*(finish_time-start_time);
454 p_energy->total_energy = current_energy + action_energy;
455 p_energy->last_updated = finish_time;
457 XBT_DEBUG("[cpu_update_energy] old_energy_value=%f, action_energy_value=%f", current_energy, action_energy);
460 double CpuCas01Lmm::getCurrentPowerPeak()
465 double CpuCas01Lmm::getPowerPeakAt(int pstate_index)
467 xbt_dynar_t plist = p_powerPeakList;
468 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
470 return xbt_dynar_get_as(plist, pstate_index, double);
473 int CpuCas01Lmm::getNbPstates()
475 return xbt_dynar_length(p_powerPeakList);
478 void CpuCas01Lmm::setPowerPeakAt(int pstate_index)
480 xbt_dynar_t plist = p_powerPeakList;
481 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
483 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
484 m_pstate = pstate_index;
485 m_powerPeak = new_power_peak;
488 double CpuCas01Lmm::getConsumedEnergy()
490 return p_energy->total_energy;
498 * Update the CPU total energy for a finished action
501 void CpuCas01ActionLmm::updateEnergy()
503 CpuCas01LmmPtr cpu = static_cast<CpuCas01LmmPtr>(lmm_constraint_id(lmm_get_cnst_from_var
504 (p_model->p_maxminSystem,
507 if(cpu->p_energy->last_updated < surf_get_clock()) {
508 double load = lmm_constraint_get_usage(cpu->p_constraint) / cpu->m_powerPeak;
509 cpu->updateEnergy(load);