1 #include "cpu_interface.hpp"
3 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
5 "Logging specific to the SURF cpu module");
7 CpuModelPtr surf_cpu_model_pm;
8 CpuModelPtr surf_cpu_model_vm;
14 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
16 CpuActionLmmPtr action;
17 while ((xbt_heap_size(getActionHeap()) > 0)
18 && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
19 action = dynamic_cast<CpuActionLmmPtr>(static_cast<ActionLmmPtr>(xbt_heap_pop(getActionHeap())));
20 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
22 if (TRACE_is_enabled()) {
23 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
24 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
25 lmm_variable_getvalue(action->getVariable()),
26 action->getLastUpdate(),
27 now - action->getLastUpdate());
32 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
34 action->updateEnergy();
36 /* set the remains to 0 due to precision problems when updating the remaining amount */
37 action->setRemains(0);
38 action->setState(SURF_ACTION_DONE);
39 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
42 if (TRACE_is_enabled()) {
43 //defining the last timestamp that we can safely dump to trace file
44 //without losing the event ascending order (considering all CPU's)
46 ActionListPtr actionSet = getRunningActionSet();
47 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
48 ; it != itend ; ++it) {
49 action = dynamic_cast<CpuActionLmmPtr>(&*it);
51 smaller = action->getLastUpdate();
54 if (action->getLastUpdate() < smaller) {
55 smaller = action->getLastUpdate();
59 TRACE_last_timestamp_to_dump = smaller;
66 void CpuModel::updateActionsStateFull(double now, double delta)
68 CpuActionLmmPtr action = NULL;
69 ActionListPtr running_actions = getRunningActionSet();
71 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
72 ; it != itend ; it=itNext) {
74 action = dynamic_cast<CpuActionLmmPtr>(&*it);
76 if (TRACE_is_enabled()) {
77 CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
78 (getMaxminSystem(), action->getVariable(), 0));
80 TRACE_surf_host_set_utilization(x->getName(),
81 action->getCategory(),
82 lmm_variable_getvalue(action->getVariable()),
85 TRACE_last_timestamp_to_dump = now - delta;
89 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
92 if (action->getMaxDuration() != NO_MAX_DURATION)
93 action->updateMaxDuration(delta);
96 if ((action->getRemains() <= 0) &&
97 (lmm_get_variable_weight(action->getVariable()) > 0)) {
99 action->setState(SURF_ACTION_DONE);
100 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
101 (action->getMaxDuration() <= 0)) {
103 action->setState(SURF_ACTION_DONE);
105 action->updateEnergy();
115 Cpu::Cpu(int core, double powerPeak, double powerScale)
116 : m_core(core), m_powerPeak(powerPeak), m_powerScale(powerScale)
119 double Cpu::getSpeed(double load)
121 return load * m_powerPeak;
124 double Cpu::getAvailableSpeed()
126 /* number between 0 and 1 */
135 CpuLmm::CpuLmm(lmm_constraint_t constraint)
136 : ResourceLmm(constraint), p_constraintCore(NULL), p_constraintCoreId(NULL)
139 CpuLmm::CpuLmm(lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
140 : ResourceLmm(constraint)
141 , Cpu(core, powerPeak, powerScale)
143 /* At now, we assume that a VM does not have a multicore CPU. */
145 xbt_assert(getModel() == surf_cpu_model_pm);
148 p_constraintCore = xbt_new(lmm_constraint_t, core);
149 p_constraintCoreId = xbt_new(void*, core);
152 for (i = 0; i < core; i++) {
153 /* just for a unique id, never used as a string. */
154 p_constraintCoreId[i] = bprintf("%s:%i", getName(), i);
155 p_constraintCore[i] = lmm_constraint_new(getModel()->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
160 if (p_constraintCore){
161 for (int i = 0; i < m_core; i++) {
162 xbt_free(p_constraintCoreId[i]);
164 xbt_free(p_constraintCore);
165 xbt_free(p_constraintCoreId);
173 void CpuActionLmm::updateRemainingLazy(double now)
177 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
178 "You're updating an action that is not running.");
180 /* bogus priority, skip it */
181 xbt_assert(getPriority() > 0,
182 "You're updating an action that seems suspended.");
184 delta = now - m_lastUpdate;
187 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
188 double_update(&(m_remains), m_lastValue * delta);
191 if (TRACE_is_enabled()) {
192 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
193 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
196 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
200 m_lastValue = lmm_variable_getvalue(getVariable());
203 void CpuActionLmm::setBound(double bound)
205 XBT_IN("(%p,%g)", this, bound);
207 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
209 if (getModel()->getUpdateMechanism() == UM_LAZY)
210 heapRemove(getModel()->getActionHeap());
216 * This function formulates a constraint problem that pins a given task to
217 * particular cores. Currently, it is possible to pin a task to an exactly one
218 * specific core. The system links the variable object of the task to the
219 * per-core constraint object.
221 * But, the taskset command on Linux takes a mask value specifying a CPU
222 * affinity setting of a given task. If the mask value is 0x03, the given task
223 * will be executed on the first core (CPU0) or the second core (CPU1) on the
224 * given PM. The schedular will determine appropriate placements of tasks,
225 * considering given CPU affinities and task activities.
227 * How should the system formulate constraint problems for an affinity to
230 * The cpu argument must be the host where the task is being executed. The
231 * action object does not have the information about the location where the
232 * action is being executed.
234 void CpuActionLmm::setAffinity(CpuPtr _cpu, unsigned long mask)
236 lmm_variable_t var_obj = getVariable();
237 CpuLmmPtr cpu = reinterpret_cast<CpuLmmPtr>(_cpu);
238 XBT_IN("(%p,%lx)", this, mask);
241 unsigned long nbits = 0;
243 /* FIXME: There is much faster algorithms doing this. */
244 for (int i = 0; i < cpu->m_core; i++) {
245 unsigned long has_affinity = (1UL << i) & mask;
251 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
252 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
257 for (int i = 0; i < cpu->m_core; i++) {
258 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
259 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
261 unsigned long has_affinity = (1UL << i) & mask;
263 /* This function only accepts an affinity setting on the host where the
264 * task is now running. In future, a task might move to another host.
265 * But, at this moment, this function cannot take an affinity setting on
268 * It might be possible to extend the code to allow this function to
269 * accept affinity settings on a future host. We might be able to assign
270 * zero to elem->value to maintain such inactive affinity settings in the
271 * system. But, this will make the system complex. */
272 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
273 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
277 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
278 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */