1 /* Copyright (c) 2009-2011, 2013-2016. 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"
11 #include "src/surf/platform.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu_cas, surf_cpu,
14 "Logging specific to the SURF CPU IMPROVED module");
19 void surf_cpu_model_init_Cas01()
21 xbt_assert(!surf_cpu_model_pm);
22 xbt_assert(!surf_cpu_model_vm);
24 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
25 if (!strcmp(optim, "TI")) {
26 surf_cpu_model_init_ti();
30 surf_cpu_model_pm = new simgrid::surf::CpuCas01Model();
31 xbt_dynar_push(all_existing_models, &surf_cpu_model_pm);
33 surf_cpu_model_vm = new simgrid::surf::CpuCas01Model();
34 xbt_dynar_push(all_existing_models, &surf_cpu_model_vm);
36 simgrid::surf::on_postparse.connect([]() {
37 surf_cpu_model_pm->addTraces();
44 CpuCas01Model::CpuCas01Model() : simgrid::surf::CpuModel()
46 char *optim = xbt_cfg_get_string(_sg_cfg_set, "cpu/optim");
47 int select = xbt_cfg_get_boolean(_sg_cfg_set, "cpu/maxmin_selective_update");
49 if (!strcmp(optim, "Full")) {
50 p_updateMechanism = UM_FULL;
51 m_selectiveUpdate = select;
52 } else if (!strcmp(optim, "Lazy")) {
53 p_updateMechanism = UM_LAZY;
54 m_selectiveUpdate = 1;
55 xbt_assert((select == 1)
57 (xbt_cfg_is_default_value
58 (_sg_cfg_set, "cpu/maxmin_selective_update")),
59 "Disabling selective update while using the lazy update mechanism is dumb!");
61 xbt_die("Unsupported optimization (%s) for this model", optim);
64 p_cpuRunningActionSetThatDoesNotNeedBeingChecked = new ActionList();
66 p_maxminSystem = lmm_system_new(m_selectiveUpdate);
68 if (getUpdateMechanism() == UM_LAZY) {
69 p_actionHeap = xbt_heap_new(8, NULL);
70 xbt_heap_set_update_callback(p_actionHeap, surf_action_lmm_update_index_heap);
71 p_modifiedSet = new ActionLmmList();
72 p_maxminSystem->keep_track = p_modifiedSet;
76 CpuCas01Model::~CpuCas01Model()
78 lmm_system_free(p_maxminSystem);
79 p_maxminSystem = NULL;
82 xbt_heap_free(p_actionHeap);
85 surf_cpu_model_pm = NULL;
87 delete p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
90 Cpu *CpuCas01Model::createCpu(simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
91 int pstate, double speedScale,
92 tmgr_trace_t speedTrace, int core,
94 tmgr_trace_t state_trace)
96 xbt_assert(xbt_dynar_getfirst_as(speedPeak, double) > 0.0,
97 "Speed has to be >0.0. Did you forget to specify the mandatory power attribute?");
98 xbt_assert(core > 0, "Invalid number of cores %d. Must be larger than 0", core);
99 Cpu *cpu = new CpuCas01(this, host, speedPeak, pstate, speedScale, speedTrace, core, initiallyOn, state_trace);
103 double CpuCas01Model::next_occuring_event_full(double /*now*/)
105 return Model::shareResourcesMaxMin(getRunningActionSet(), p_maxminSystem, lmm_solve);
108 void CpuCas01Model::addTraces()
110 xbt_dict_cursor_t cursor = NULL;
111 char *trace_name, *elm;
112 static int called = 0;
117 /* connect host speed traces */
118 xbt_dict_foreach(trace_connect_list_host_speed, cursor, trace_name, elm) {
119 tmgr_trace_t trace = (tmgr_trace_t) xbt_dict_get_or_null(traces_set_list, trace_name);
120 Cpu *cpu = sg_host_by_name(elm)->pimpl_cpu;
122 xbt_assert(cpu, "Host %s undefined", elm);
123 xbt_assert(trace, "Trace %s undefined", trace_name);
125 cpu->set_speed_trace(trace);
132 CpuCas01::CpuCas01(CpuCas01Model *model, simgrid::s4u::Host *host, xbt_dynar_t speedPeak,
133 int pstate, double speedScale, tmgr_trace_t speedTrace, int core,
134 int initiallyOn, tmgr_trace_t stateTrace)
136 lmm_constraint_new(model->getMaxminSystem(), this, core * speedScale * xbt_dynar_get_as(speedPeak, pstate, double)),
138 core, xbt_dynar_get_as(speedPeak, pstate, double), speedScale,
141 XBT_DEBUG("CPU create: peak=%f, pstate=%d", p_speed.peak, m_pstate);
145 p_speed.event = future_evt_set->add_trace(speedTrace, 0.0, this);
148 p_stateEvent = future_evt_set->add_trace(stateTrace, 0.0, this);
151 CpuCas01::~CpuCas01()
153 if (getModel() == surf_cpu_model_pm)
154 xbt_dynar_free(&p_speedPeakList);
157 xbt_dynar_t CpuCas01::getSpeedPeakList(){
158 return p_speedPeakList;
161 bool CpuCas01::isUsed()
163 return lmm_constraint_used(getModel()->getMaxminSystem(), getConstraint());
166 /** @brief take into account changes of speed (either load or max) */
167 void CpuCas01::onSpeedChange() {
168 lmm_variable_t var = NULL;
169 lmm_element_t elem = NULL;
171 lmm_update_constraint_bound(getModel()->getMaxminSystem(), getConstraint(),
172 m_core * p_speed.scale * p_speed.peak);
173 while ((var = lmm_get_var_from_cnst
174 (getModel()->getMaxminSystem(), getConstraint(), &elem))) {
175 CpuCas01Action *action = static_cast<CpuCas01Action*>(lmm_variable_id(var));
177 lmm_update_variable_bound(getModel()->getMaxminSystem(),
178 action->getVariable(),
179 p_speed.scale * p_speed.peak);
182 Cpu::onSpeedChange();
185 void CpuCas01::apply_event(tmgr_trace_iterator_t event, double value)
187 lmm_variable_t var = NULL;
188 lmm_element_t elem = NULL;
190 if (event == p_speed.event) {
191 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
192 xbt_assert(m_core == 1, "FIXME: add speed scaling code also for constraint_core[i]");
194 p_speed.scale = value;
197 tmgr_trace_event_unref(&p_speed.event);
198 } else if (event == p_stateEvent) {
199 /* TODO (Hypervisor): do the same thing for constraint_core[i] */
200 xbt_assert(m_core == 1, "FIXME: add state change code also for constraint_core[i]");
204 xbt_dynar_push_as(host_that_restart, char*, (char *)getName());
207 lmm_constraint_t cnst = getConstraint();
208 double date = surf_get_clock();
212 while ((var = lmm_get_var_from_cnst(getModel()->getMaxminSystem(), cnst, &elem))) {
213 Action *action = static_cast<Action*>(lmm_variable_id(var));
215 if (action->getState() == SURF_ACTION_RUNNING ||
216 action->getState() == SURF_ACTION_READY ||
217 action->getState() == SURF_ACTION_NOT_IN_THE_SYSTEM) {
218 action->setFinishTime(date);
219 action->setState(SURF_ACTION_FAILED);
223 tmgr_trace_event_unref(&p_stateEvent);
225 xbt_die("Unknown event!\n");
231 CpuAction *CpuCas01::execution_start(double size)
234 XBT_IN("(%s,%g)", getName(), size);
235 CpuCas01Action *action = new CpuCas01Action(getModel(), size, isOff(),
236 p_speed.scale * p_speed.peak, getConstraint());
242 CpuAction *CpuCas01::sleep(double duration)
245 duration = MAX(duration, sg_surf_precision);
247 XBT_IN("(%s,%g)", getName(), duration);
248 CpuCas01Action *action = new CpuCas01Action(getModel(), 1.0, isOff(),
249 p_speed.scale * p_speed.peak, getConstraint());
252 // FIXME: sleep variables should not consume 1.0 in lmm_expand
253 action->m_maxDuration = duration;
254 action->m_suspended = 2;
255 if (duration == NO_MAX_DURATION) {
256 /* Move to the *end* of the corresponding action set. This convention
257 is used to speed up update_resource_state */
258 action->getStateSet()->erase(action->getStateSet()->iterator_to(*action));
259 action->p_stateSet = static_cast<CpuCas01Model*>(getModel())->p_cpuRunningActionSetThatDoesNotNeedBeingChecked;
260 action->getStateSet()->push_back(*action);
263 lmm_update_variable_weight(getModel()->getMaxminSystem(),
264 action->getVariable(), 0.0);
265 if (getModel()->getUpdateMechanism() == UM_LAZY) { // remove action from the heap
266 action->heapRemove(getModel()->getActionHeap());
267 // this is necessary for a variable with weight 0 since such
268 // variables are ignored in lmm and we need to set its max_duration
269 // correctly at the next call to share_resources
270 getModel()->getModifiedSet()->push_front(*action);
281 CpuCas01Action::CpuCas01Action(Model *model, double cost, bool failed, double speed, lmm_constraint_t constraint)
282 : CpuAction(model, cost, failed,
283 lmm_variable_new(model->getMaxminSystem(), this,
286 if (model->getUpdateMechanism() == UM_LAZY) {
288 m_lastUpdate = surf_get_clock();
291 lmm_expand(model->getMaxminSystem(), constraint, getVariable(), 1.0);