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 parse_cpu_init(sg_platf_host_cbarg_t host){
21 ((CpuCas01ModelPtr)surf_cpu_model_pm)->parseInit(host);
24 static void cpu_add_traces_cpu(){
25 surf_cpu_model_pm->addTraces();
28 static void cpu_define_callbacks()
30 sg_platf_host_add_cb(parse_cpu_init);
31 sg_platf_postparse_add_cb(cpu_add_traces_cpu);
37 void surf_cpu_model_init_Cas01()
39 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
41 xbt_assert(!surf_cpu_model_pm);
42 xbt_assert(!surf_cpu_model_vm);
44 if (!strcmp(optim, "TI")) {
45 surf_cpu_model_init_ti();
49 surf_cpu_model_pm = new CpuCas01Model();
50 surf_cpu_model_vm = new CpuCas01Model();
52 cpu_define_callbacks();
53 ModelPtr model_pm = static_cast<ModelPtr>(surf_cpu_model_pm);
54 ModelPtr model_vm = static_cast<ModelPtr>(surf_cpu_model_vm);
55 xbt_dynar_push(model_list, &model_pm);
56 xbt_dynar_push(model_list, &model_vm);
59 CpuCas01Model::CpuCas01Model() : CpuModel("cpu")
61 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
62 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
64 if (!strcmp(optim, "Full")) {
65 p_updateMechanism = UM_FULL;
66 m_selectiveUpdate = select;
67 } else if (!strcmp(optim, "Lazy")) {
68 p_updateMechanism = UM_LAZY;
69 m_selectiveUpdate = 1;
70 xbt_assert((select == 1)
72 (xbt_cfg_is_default_value
73 (_sg_cfg_set, "cpu/maxmin_selective_update")),
74 "Disabling selective update while using the lazy update mechanism is dumb!");
76 xbt_die("Unsupported optimization (%s) for this model", optim);
79 p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
81 if (getUpdateMechanism() == UM_LAZY) {
82 shareResources = &CpuCas01Model::shareResourcesLazy;
83 updateActionsState = &CpuCas01Model::updateActionsStateLazy;
85 } else if (getUpdateMechanism() == UM_FULL) {
86 shareResources = &CpuCas01Model::shareResourcesFull;
87 updateActionsState = &CpuCas01Model::updateActionsStateFull;
89 xbt_die("Invalid cpu update mechanism!");
91 if (!p_maxminSystem) {
92 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
95 if (getUpdateMechanism() == UM_LAZY) {
96 p_actionHeap = xbt_heap_new(8, NULL);
97 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
98 p_modifiedSet = new ActionLmmList();
99 p_maxminSystem->keep_track = p_modifiedSet;
103 CpuCas01Model::~CpuCas01Model()
105 lmm_system_free(p_maxminSystem);
106 p_maxminSystem = NULL;
109 xbt_heap_free(p_actionHeap);
110 delete p_modifiedSet;
112 surf_cpu_model_pm = NULL;
114 delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
117 void CpuCas01Model::parseInit(sg_platf_host_cbarg_t host)
119 createResource(host->id,
130 CpuPtr CpuCas01Model::createResource(const char *name, xbt_dynar_t power_peak,
131 int pstate, double power_scale,
132 tmgr_trace_t power_trace, int core,
133 e_surf_resource_state_t state_initial,
134 tmgr_trace_t state_trace,
135 xbt_dict_t cpu_properties)
138 xbt_assert(!surf_cpu_resource_priv(surf_cpu_resource_by_name(name)),
139 "Host '%s' declared several times in the platform file",
141 xbt_assert(xbt_dynar_getfirst_as(power_peak, double) > 0.0,
142 "Power has to be >0.0");
143 xbt_assert(core > 0, "Invalid number of cores %d", core);
145 cpu = new CpuCas01(this, name, power_peak, pstate, power_scale, power_trace, core, state_initial, state_trace, cpu_properties);
146 xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, static_cast<ResourcePtr>(cpu));
151 double CpuCas01Model::shareResourcesFull(double /*now*/)
153 return Model::shareResourcesMaxMin(getRunningActionSet(),
154 p_maxminSystem, lmm_solve);
157 void CpuCas01Model::addTraces()
159 xbt_dict_cursor_t cursor = NULL;
160 char *trace_name, *elm;
161 static int called = 0;
166 /* connect all traces relative to hosts */
167 xbt_dict_foreach(trace_connect_list_host_avail, cursor, trace_name, elm) {
168 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
169 CpuCas01Ptr host = static_cast<CpuCas01Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
171 xbt_assert(host, "Host %s undefined", elm);
172 xbt_assert(trace, "Trace %s undefined", trace_name);
174 host->setStateEvent(tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host)));
177 xbt_dict_foreach(trace_connect_list_power, cursor, trace_name, elm) {
178 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
179 CpuCas01Ptr host = static_cast<CpuCas01Ptr>(surf_cpu_resource_priv(surf_cpu_resource_by_name(elm)));
181 xbt_assert(host, "Host %s undefined", elm);
182 xbt_assert(trace, "Trace %s undefined", trace_name);
184 host->setPowerEvent(tmgr_history_add_trace(history, trace, 0.0, 0, static_cast<ResourcePtr>(host)));
191 CpuCas01::CpuCas01(CpuCas01ModelPtr model, const char *name, xbt_dynar_t powerPeak,
192 int pstate, double powerScale, tmgr_trace_t powerTrace, int core,
193 e_surf_resource_state_t stateInitial, tmgr_trace_t stateTrace,
194 xbt_dict_t properties)
195 : Cpu(model, name, properties,
196 lmm_constraint_new(model->getMaxminSystem(), this, core * powerScale * xbt_dynar_get_as(powerPeak, pstate, double)),
197 core, xbt_dynar_get_as(powerPeak, pstate, double), powerScale) {
199 p_powerPeakList = powerPeak;
202 XBT_DEBUG("CPU create: peak=%f, pstate=%d", m_powerPeak, m_pstate);
205 setState(stateInitial);
207 p_powerEvent = tmgr_history_add_trace(history, powerTrace, 0.0, 0, static_cast<ResourcePtr>(this));
210 p_stateEvent = tmgr_history_add_trace(history, stateTrace, 0.0, 0, static_cast<ResourcePtr>(this));
213 CpuCas01::~CpuCas01(){
214 if (getModel() == surf_cpu_model_pm)
215 xbt_dynar_free(&p_powerPeakList);
218 void CpuCas01::setStateEvent(tmgr_trace_event_t stateEvent)
220 p_stateEvent = stateEvent;
223 void CpuCas01::setPowerEvent(tmgr_trace_event_t powerEvent)
225 p_powerEvent = powerEvent;
228 xbt_dynar_t CpuCas01::getPowerPeakList(){
229 return p_powerPeakList;
232 int CpuCas01::getPState()
237 bool CpuCas01::isUsed()
239 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
242 void CpuCas01::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(getModel()->getMaxminSystem(), getConstraint(),
253 m_core * m_powerScale *
256 TRACE_surf_host_set_power(date, getName(),
257 m_core * m_powerScale *
260 while ((var = lmm_get_var_from_cnst
261 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
262 CpuCas01ActionPtr action = static_cast<CpuCas01ActionPtr>(static_cast<ActionPtr>(lmm_variable_id(var)));
264 lmm_update_variable_bound(getModel()->getMaxminSystem(),
265 action->getVariable(),
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(getState() == SURF_RESOURCE_OFF)
276 xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
277 setState(SURF_RESOURCE_ON);
279 lmm_constraint_t cnst = getConstraint();
281 setState(SURF_RESOURCE_OFF);
283 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
284 ActionPtr action = static_cast<ActionPtr>(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->setFinishTime(date);
290 action->setState(SURF_ACTION_FAILED);
294 if (tmgr_trace_event_free(event_type))
297 XBT_CRITICAL("Unknown event ! \n");
304 CpuActionPtr CpuCas01::execute(double size)
307 XBT_IN("(%s,%g)", getName(), size);
308 CpuCas01ActionPtr action = new CpuCas01Action(getModel(), size, getState() != SURF_RESOURCE_ON,
309 m_powerScale * m_powerPeak, getConstraint());
315 CpuActionPtr CpuCas01::sleep(double duration)
318 duration = MAX(duration, MAXMIN_PRECISION);
320 XBT_IN("(%s,%g)", getName(), duration);
321 CpuCas01ActionPtr action = new CpuCas01Action(getModel(), 1.0, getState() != SURF_RESOURCE_ON,
322 m_powerScale * m_powerPeak, getConstraint());
325 // FIXME: sleep variables should not consume 1.0 in lmm_expand
326 action->m_maxDuration = duration;
327 action->m_suspended = 2;
328 if (duration == NO_MAX_DURATION) {
329 /* Move to the *end* of the corresponding action set. This convention
330 is used to speed up update_resource_state */
331 action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
332 action->p_stateSet = static_cast<CpuCas01ModelPtr>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
333 action->getStateSet()->push_back(*action);
336 lmm_update_variable_weight(getModel()->getMaxminSystem(),
337 action->getVariable(), 0.0);
338 if (getModel()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
339 action->heapRemove(getModel()->getActionHeap());
340 // this is necessary for a variable with weight 0 since such
341 // variables are ignored in lmm and we need to set its max_duration
342 // correctly at the next call to share_resources
343 getModel()->getModifiedSet()->push_front(*action);
350 double CpuCas01::getCurrentPowerPeak()
355 double CpuCas01::getPowerPeakAt(int pstate_index)
357 xbt_dynar_t plist = p_powerPeakList;
358 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
360 return xbt_dynar_get_as(plist, pstate_index, double);
363 int CpuCas01::getNbPstates()
365 return xbt_dynar_length(p_powerPeakList);
368 void CpuCas01::setPowerPeakAt(int pstate_index)
370 xbt_dynar_t plist = p_powerPeakList;
371 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
373 double new_power_peak = xbt_dynar_get_as(plist, pstate_index, double);
374 m_pstate = pstate_index;
375 m_powerPeak = new_power_peak;
382 CpuCas01Action::CpuCas01Action(ModelPtr model, double cost, bool failed, double power, lmm_constraint_t constraint)
383 : CpuAction(model, cost, failed,
384 lmm_variable_new(model->getMaxminSystem(), static_cast<ActionPtr>(this),
388 if (model->getUpdateMechanism() == UM_LAZY) {
390 m_lastUpdate = surf_get_clock();
393 lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);