1 /* Copyright (c) 2009-2011, 2013-2014. 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 "plugins/energy.hpp"
10 #include "maxmin_private.hpp"
11 #include "simgrid/sg_config.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
14 "Logging specific to the SURF CPU IMPROVED module");
20 static void cpu_define_callbacks()
22 sg_platf_host_add_cb(cpu_parse_init);
23 sg_platf_postparse_add_cb(cpu_add_traces);
29 void surf_cpu_model_init_Cas01()
31 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
33 xbt_assert(!surf_cpu_model_pm);
34 xbt_assert(!surf_cpu_model_vm);
36 if (!strcmp(optim, "TI")) {
37 surf_cpu_model_init_ti();
41 surf_cpu_model_pm = new CpuCas01Model();
42 surf_cpu_model_vm = new CpuCas01Model();
44 cpu_define_callbacks();
45 ModelPtr model_pm = surf_cpu_model_pm;
46 ModelPtr model_vm = surf_cpu_model_vm;
47 xbt_dynar_push(model_list, &model_pm);
48 xbt_dynar_push(model_list, &model_vm);
51 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
53 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
54 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
56 if (!strcmp(optim, "Full")) {
57 p_updateMechanism = UM_FULL;
58 m_selectiveUpdate = select;
59 } else if (!strcmp(optim, "Lazy")) {
60 p_updateMechanism = UM_LAZY;
61 m_selectiveUpdate = 1;
62 xbt_assert((select == 1)
64 (xbt_cfg_is_default_value
65 (_sg_cfg_set, "cpu/maxmin_selective_update")),
66 "Disabling selective update while using the lazy update mechanism is dumb!");
68 xbt_die("Unsupported optimization (%s) for this model", optim);
71 p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
73 if (getUpdateMechanism() == UM_LAZY) {
74 shareResources = &CpuCas01Model::shareResourcesLazy;
75 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
77 } else if (getUpdateMechanism() == UM_FULL) {
78 shareResources = &CpuCas01Model::shareResourcesFull;
79 updateActionsState = &CpuCas01Model::updateActionsStateFull;
81 xbt_die("Invalid cpu update mechanism!");
83 if (!p_maxminSystem) {
84 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
87 if (getUpdateMechanism() == UM_LAZY) {
88 p_actionHeap = new ActionHeap();
89 p_modifiedSet = new ActionLmmList();
90 p_maxminSystem->keep_track = p_modifiedSet;
94 CpuCas01Model::~CpuCas01Model()
96 lmm_system_free(p_maxminSystem);
97 p_maxminSystem = NULL;
101 delete p_modifiedSet;
103 surf_cpu_model_pm = NULL;
105 delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
108 CpuPtr CpuCas01Model::createCpu(const char *name, xbt_dynar_t power_peak,
109 int pstate, double power_scale,
110 tmgr_trace_t power_trace, int core,
111 e_surf_resource_state_t state_initial,
112 tmgr_trace_t state_trace,
113 xbt_dict_t cpu_properties)
116 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
117 "Host '%s' declared several times in the platform file",
119 xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
120 "Power has to be >0.0");
121 xbt_assert(core > 0, "Invalid number of cores %d", core);
123 cpu = new CpuCas01(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
124 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu);
129 double CpuCas01Model::shareResourcesFull(double /*now*/)
131 return Model::shareResourcesMaxMin(getRunningActionSet(),
132 p_maxminSystem, lmm_solve);
135 void CpuCas01Model::addTraces()
137 xbt_dict_cursor_t cursor = NULL;
138 char *trace_name, *elm;
139 static int called = 0;
144 /* connect all traces relative to hosts */
145 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
146 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
147 CpuCas01Ptr host = static_cast<CpuCas01Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
149 xbt_assert(host, "Host %s undefined", elm);
150 xbt_assert(trace, "Trace %s undefined", trace_name);
152 host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
155 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
156 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
157 CpuCas01Ptr host = static_cast<CpuCas01Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
159 xbt_assert(host, "Host %s undefined", elm);
160 xbt_assert(trace, "Trace %s undefined", trace_name);
162 host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
169 CpuCas01::CpuCas01(CpuCas01ModelPtr model, const char *name, xbt_dynar_t powerPeak,
170 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
171 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
172 xbt_dict_t properties)
173 : Cpu(model, name, properties,
174 lmm_constraint_new(model->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
175 core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
177 p_powerPeakList = powerPeak;
180 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
183 setState(stateInitial);
185 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
188 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
191 CpuCas01::~CpuCas01(){
192 if (getModel() == surf_cpu_model_pm)
193 xbt_dynar_free(&p_powerPeakList);
196 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
198 p_stateEvent = stateEvent;
201 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
203 p_powerEvent = powerEvent;
206 xbt_dynar_t CpuCas01::getPowerPeakList(){
207 return p_powerPeakList;
210 int CpuCas01::getPState()
215 bool CpuCas01::isUsed()
217 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
220 void CpuCas01::updateState(tmgr_trace_event_t event_type, double value, double date)
222 lmm_variable_t var = NULL;
223 lmm_element_t elem = NULL;
225 if (event_type == p_powerEvent) {
226 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
227 xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
229 m_powerScale = value;
230 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
231 m_core * m_powerScale *
234 TRACE_surf_host_set_power(date, getName(),
235 m_core * m_powerScale *
238 while ((var = lmm_get_var_from_cnst
239 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
240 CpuCas01ActionPtr action = static_cast<CpuCas01ActionPtr>(lmm_variable_id(var));
242 lmm_update_variable_bound(getModel()->getMaxminSystem(),
243 action->getVariable(),
244 m_powerScale * m_powerPeak);
246 if (tmgr_trace_event_free(event_type))
248 } else if (event_type == p_stateEvent) {
249 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
250 xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
253 if(getState() == SURF_RESOURCE_OFF)
254 xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
255 setState(SURF_RESOURCE_ON);
257 lmm_constraint_t cnst = getConstraint();
259 setState(SURF_RESOURCE_OFF);
261 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
262 ActionPtr action = static_cast<ActionPtr>(lmm_variable_id(var));
264 if (action->getState() == SURF_ACTION_RUNNING ||
265 action->getState() == SURF_ACTION_READY ||
266 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
267 action->setFinishTime(date);
268 action->setState(SURF_ACTION_FAILED);
272 if (tmgr_trace_event_free(event_type))
275 XBT_CRITICAL("Unknown event ! \n");
282 CpuActionPtr CpuCas01::execute(double size)
285 XBT_IN("(%s,%g)", getName(), size);
286 CpuCas01ActionPtr action = new CpuCas01Action(getModel(), size, getState() != SURF_RESOURCE_ON,
287 m_powerScale * m_powerPeak, getConstraint());
293 CpuActionPtr CpuCas01::sleep(double duration)
296 duration = MAX(duration, sg_surf_precision);
298 XBT_IN("(%s,%g)", getName(), duration);
299 CpuCas01ActionPtr action = new CpuCas01Action(getModel(), 1.0, getState() != SURF_RESOURCE_ON,
300 m_powerScale * m_powerPeak, getConstraint());
303 // FIXME: sleep variables should not consume 1.0 in lmm_expand
304 action->m_maxDuration = duration;
305 action->m_suspended = 2;
306 if (duration == NO_MAX_DURATION) {
307 /* Move to the *end* of the corresponding action set. This convention
308 is used to speed up update_resource_state */
309 action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
310 action->p_stateSet = static_cast<CpuCas01ModelPtr>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
311 action->getStateSet()->push_back(*action);
314 lmm_update_variable_weight(getModel()->getMaxminSystem(),
315 action->getVariable(), 0.0);
316 if (getModel()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
317 action->heapRemove();
318 // this is necessary for a variable with weight 0 since such
319 // variables are ignored in lmm and we need to set its max_duration
320 // correctly at the next call to share_resources
321 getModel()->getModifiedSet()->push_front(*action);
328 double CpuCas01::getCurrentPowerPeak()
333 double CpuCas01::getPowerPeakAt(int pstate_index)
335 xbt_dynar_t plist = p_powerPeakList;
336 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
338 return xbt_dynar_get_as(plist, pstate_index, double);
341 int CpuCas01::getNbPstates()
343 return xbt_dynar_length(p_powerPeakList);
346 void CpuCas01::setPowerPeakAt(int pstate_index)
348 xbt_dynar_t plist = p_powerPeakList;
349 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
351 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
352 m_pstate = pstate_index;
353 m_powerPeak = new_power_peak;
360 CpuCas01Action::CpuCas01Action(ModelPtr model, double cost, bool failed, double power, lmm_constraint_t constraint)
361 : CpuAction(model, cost, failed,
362 lmm_variable_new(model->getMaxminSystem(), this,
366 if (model->getUpdateMechanism() == UM_LAZY) {
367 m_lastUpdate = surf_get_clock();
370 lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);