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 "maxmin_private.hpp"
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");
18 void surf_cpu_model_init_Cas01()
20 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
22 xbt_assert(!surf_cpu_model_pm);
23 xbt_assert(!surf_cpu_model_vm);
25 if (!strcmp(optim, "TI")) {
26 surf_cpu_model_init_ti();
30 surf_cpu_model_pm = new CpuCas01Model();
31 surf_cpu_model_vm = new CpuCas01Model();
33 sg_platf_host_add_cb(cpu_parse_init);
34 sg_platf_postparse_add_cb(cpu_add_traces);
36 Model *model_pm = surf_cpu_model_pm;
37 Model *model_vm = surf_cpu_model_vm;
38 xbt_dynar_push(model_list, &model_pm);
39 xbt_dynar_push(model_list, &model_vm);
42 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
44 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
45 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
47 if (!strcmp(optim, "Full")) {
48 p_updateMechanism = UM_FULL;
49 m_selectiveUpdate = select;
50 } else if (!strcmp(optim, "Lazy")) {
51 p_updateMechanism = UM_LAZY;
52 m_selectiveUpdate = 1;
53 xbt_assert((select == 1)
55 (xbt_cfg_is_default_value
56 (_sg_cfg_set, "cpu/maxmin_selective_update")),
57 "Disabling selective update while using the lazy update mechanism is dumb!");
59 xbt_die("Unsupported optimization (%s) for this model", optim);
62 p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
64 if (getUpdateMechanism() == UM_LAZY) {
65 shareResources = &CpuCas01Model::shareResourcesLazy;
66 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
68 } else if (getUpdateMechanism() == UM_FULL) {
69 shareResources = &CpuCas01Model::shareResourcesFull;
70 updateActionsState = &CpuCas01Model::updateActionsStateFull;
72 xbt_die("Invalid cpu update mechanism!");
74 if (!p_maxminSystem) {
75 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
78 if (getUpdateMechanism() == UM_LAZY) {
79 p_actionHeap = xbt_heap_new(8, NULL);
80 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
81 p_modifiedSet = new ActionLmmList();
82 p_maxminSystem->keep_track = p_modifiedSet;
86 CpuCas01Model::~CpuCas01Model()
88 lmm_system_free(p_maxminSystem);
89 p_maxminSystem = NULL;
92 xbt_heap_free(p_actionHeap);
95 surf_cpu_model_pm = NULL;
97 delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
100 Cpu *CpuCas01Model::createCpu(const char *name, xbt_dynar_t power_peak,
101 int pstate, double power_scale,
102 tmgr_trace_t power_trace, int core,
103 e_surf_resource_state_t state_initial,
104 tmgr_trace_t state_trace,
105 xbt_dict_t cpu_properties)
108 sg_host_t host = sg_host_by_name(name);
109 xbt_assert(!sg_host_surfcpu(host),
110 "Host '%s' declared several times in the platform file",
112 xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
113 "Power has to be >0.0. Did you forget to specify the mandatory power attribute?");
114 xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
116 cpu = new CpuCas01(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
117 sg_host_surfcpu_set(host, cpu);
122 double CpuCas01Model::shareResourcesFull(double /*now*/)
124 return Model::shareResourcesMaxMin(getRunningActionSet(),
125 p_maxminSystem, lmm_solve);
128 void CpuCas01Model::addTraces()
130 xbt_dict_cursor_t cursor = NULL;
131 char *trace_name, *elm;
132 static int called = 0;
137 /* connect all traces relative to hosts */
138 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
139 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
140 CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
142 xbt_assert(host, "Host %s undefined", elm);
143 xbt_assert(trace, "Trace %s undefined", trace_name);
145 host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
148 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
149 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
150 CpuCas01 *host = static_cast<CpuCas01*>(sg_host_surfcpu(sg_host_by_name(elm)));
152 xbt_assert(host, "Host %s undefined", elm);
153 xbt_assert(trace, "Trace %s undefined", trace_name);
155 host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, host));
162 CpuCas01::CpuCas01(CpuCas01Model *model, const char *name, xbt_dynar_t powerPeak,
163 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
164 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
165 xbt_dict_t properties)
166 : Cpu(model, name, properties,
167 lmm_constraint_new(model->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
168 core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
170 p_powerPeakList = powerPeak;
173 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
176 setState(stateInitial);
178 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, this);
181 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, this);
184 CpuCas01::~CpuCas01(){
185 if (getModel() == surf_cpu_model_pm)
186 xbt_dynar_free(&p_powerPeakList);
189 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
191 p_stateEvent = stateEvent;
194 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
196 p_powerEvent = powerEvent;
199 xbt_dynar_t CpuCas01::getPowerPeakList(){
200 return p_powerPeakList;
203 int CpuCas01::getPState()
208 bool CpuCas01::isUsed()
210 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
213 void CpuCas01::updateState(tmgr_trace_event_t event_type, double value, double date)
215 lmm_variable_t var = NULL;
216 lmm_element_t elem = NULL;
218 if (event_type == p_powerEvent) {
219 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
220 xbt_assert(m_core == 1, "FIXME: add power scaling code also for constraint_core[i]");
222 m_powerScale = value;
223 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
224 m_core * m_powerScale *
226 TRACE_surf_host_set_power(date, getName(),
227 m_core * m_powerScale *
229 while ((var = lmm_get_var_from_cnst
230 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
231 CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
233 lmm_update_variable_bound(getModel()->getMaxminSystem(),
234 action->getVariable(),
235 m_powerScale * m_powerPeak);
237 if (tmgr_trace_event_free(event_type))
239 } else if (event_type == p_stateEvent) {
240 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
241 xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
244 if(getState() == SURF_RESOURCE_OFF)
245 xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
246 setState(SURF_RESOURCE_ON);
248 lmm_constraint_t cnst = getConstraint();
250 setState(SURF_RESOURCE_OFF);
252 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
253 Action *action = static_cast<Action*>(lmm_variable_id(var));
255 if (action->getState() == SURF_ACTION_RUNNING ||
256 action->getState() == SURF_ACTION_READY ||
257 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
258 action->setFinishTime(date);
259 action->setState(SURF_ACTION_FAILED);
263 if (tmgr_trace_event_free(event_type))
266 XBT_CRITICAL("Unknown event ! \n");
273 CpuAction *CpuCas01::execute(double size)
276 XBT_IN("(%s,%g)", getName(), size);
277 CpuCas01Action *action = new CpuCas01Action(getModel(), size, getState() != SURF_RESOURCE_ON,
278 m_powerScale * m_powerPeak, getConstraint());
284 CpuAction *CpuCas01::sleep(double duration)
287 duration = MAX(duration, sg_surf_precision);
289 XBT_IN("(%s,%g)", getName(), duration);
290 CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, getState() != SURF_RESOURCE_ON,
291 m_powerScale * m_powerPeak, getConstraint());
294 // FIXME: sleep variables should not consume 1.0 in lmm_expand
295 action->m_maxDuration = duration;
296 action->m_suspended = 2;
297 if (duration == NO_MAX_DURATION) {
298 /* Move to the *end* of the corresponding action set. This convention
299 is used to speed up update_resource_state */
300 action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
301 action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
302 action->getStateSet()->push_back(*action);
305 lmm_update_variable_weight(getModel()->getMaxminSystem(),
306 action->getVariable(), 0.0);
307 if (getModel()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
308 action->heapRemove(getModel()->getActionHeap());
309 // this is necessary for a variable with weight 0 since such
310 // variables are ignored in lmm and we need to set its max_duration
311 // correctly at the next call to share_resources
312 getModel()->getModifiedSet()->push_front(*action);
319 double CpuCas01::getCurrentPowerPeak()
324 double CpuCas01::getPowerPeakAt(int pstate_index)
326 xbt_dynar_t plist = p_powerPeakList;
327 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
329 return xbt_dynar_get_as(plist, pstate_index, double);
332 int CpuCas01::getNbPstates()
334 return xbt_dynar_length(p_powerPeakList);
337 void CpuCas01::setPstate(int pstate_index)
339 xbt_dynar_t plist = p_powerPeakList;
340 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
342 double new_pstate = xbt_dynar_get_as(plist, pstate_index, double);
343 m_pstate = pstate_index;
344 m_powerPeak = new_pstate;
347 int CpuCas01::getPstate()
356 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double power, lmm_constraint_t constraint)
357 : CpuAction(model, cost, failed,
358 lmm_variable_new(model->getMaxminSystem(), this,
362 if (model->getUpdateMechanism() == UM_LAZY) {
364 m_lastUpdate = surf_get_clock();
367 lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);