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 CpuPtr getActionCpu(CpuActionPtr action) {
15 return static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var
16 (action->getModel()->getMaxminSystem(),
17 action->getVariable(), 0)));
19 surf_callback(void, CpuPtr) createCpuCallbacks;
20 surf_callback(void, CpuPtr) deleteCpuCallbacks;
21 surf_callback(void, CpuActionPtr) updateCpuActionCallbacks;
27 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
30 while ((xbt_heap_size(getActionHeap()) > 0)
31 && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
32 action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(xbt_heap_pop(getActionHeap())));
33 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
35 if (TRACE_is_enabled()) {
36 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
37 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
38 lmm_variable_getvalue(action->getVariable()),
39 action->getLastUpdate(),
40 now - action->getLastUpdate());
45 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
47 surf_callback_emit(updateCpuActionCallbacks, action);
49 /* set the remains to 0 due to precision problems when updating the remaining amount */
50 action->setRemains(0);
51 action->setState(SURF_ACTION_DONE);
52 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
55 if (TRACE_is_enabled()) {
56 //defining the last timestamp that we can safely dump to trace file
57 //without losing the event ascending order (considering all CPU's)
59 ActionListPtr actionSet = getRunningActionSet();
60 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
61 ; it != itend ; ++it) {
62 action = static_cast<CpuActionPtr>(&*it);
64 smaller = action->getLastUpdate();
67 if (action->getLastUpdate() < smaller) {
68 smaller = action->getLastUpdate();
72 TRACE_last_timestamp_to_dump = smaller;
79 void CpuModel::updateActionsStateFull(double now, double delta)
81 CpuActionPtr action = NULL;
82 ActionListPtr running_actions = getRunningActionSet();
84 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
85 ; it != itend ; it=itNext) {
87 action = static_cast<CpuActionPtr>(&*it);
89 if (TRACE_is_enabled()) {
90 CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
91 (getMaxminSystem(), action->getVariable(), 0));
93 TRACE_surf_host_set_utilization(x->getName(),
94 action->getCategory(),
95 lmm_variable_getvalue(action->getVariable()),
98 TRACE_last_timestamp_to_dump = now - delta;
102 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
105 if (action->getMaxDuration() != NO_MAX_DURATION)
106 action->updateMaxDuration(delta);
109 if ((action->getRemains() <= 0) &&
110 (lmm_get_variable_weight(action->getVariable()) > 0)) {
112 action->setState(SURF_ACTION_DONE);
113 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
114 (action->getMaxDuration() <= 0)) {
116 action->setState(SURF_ACTION_DONE);
118 surf_callback_emit(updateCpuActionCallbacks, action);
119 //action->updateEnergy();
129 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
130 int core, double powerPeak, double powerScale)
131 : Resource(model, name, props)
133 , m_powerPeak(powerPeak)
134 , m_powerScale(powerScale)
135 , p_constraintCore(NULL)
136 , p_constraintCoreId(NULL)
137 {surf_callback_emit(createCpuCallbacks, this);}
139 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
140 lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
141 : Resource(model, name, props, constraint)
143 , m_powerPeak(powerPeak)
144 , m_powerScale(powerScale)
146 surf_callback_emit(createCpuCallbacks, this);
147 /* At now, we assume that a VM does not have a multicore CPU. */
149 xbt_assert(model == surf_cpu_model_pm);
151 p_constraintCore = NULL;
152 p_constraintCoreId = NULL;
153 if (model->getUpdateMechanism() != UM_UNDEFINED) {
154 p_constraintCore = xbt_new(lmm_constraint_t, core);
155 p_constraintCoreId = xbt_new(void*, core);
158 for (i = 0; i < core; i++) {
159 /* just for a unique id, never used as a string. */
160 p_constraintCoreId[i] = bprintf("%s:%i", name, i);
161 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
167 surf_callback_emit(deleteCpuCallbacks, this);
168 if (p_constraintCoreId){
169 for (int i = 0; i < m_core; i++) {
170 xbt_free(p_constraintCoreId[i]);
172 xbt_free(p_constraintCore);
174 if (p_constraintCoreId)
175 xbt_free(p_constraintCoreId);
178 double Cpu::getSpeed(double load)
180 return load * m_powerPeak;
183 double Cpu::getAvailableSpeed()
185 /* number between 0 and 1 */
198 void CpuAction::updateRemainingLazy(double now)
202 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
203 "You're updating an action that is not running.");
205 /* bogus priority, skip it */
206 xbt_assert(getPriority() > 0,
207 "You're updating an action that seems suspended.");
209 delta = now - m_lastUpdate;
212 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
213 double_update(&(m_remains), m_lastValue * delta);
216 if (TRACE_is_enabled()) {
217 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
218 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
221 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
225 m_lastValue = lmm_variable_getvalue(getVariable());
228 void CpuAction::setBound(double bound)
230 XBT_IN("(%p,%g)", this, bound);
232 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
234 if (getModel()->getUpdateMechanism() == UM_LAZY)
235 heapRemove(getModel()->getActionHeap());
241 * This function formulates a constraint problem that pins a given task to
242 * particular cores. Currently, it is possible to pin a task to an exactly one
243 * specific core. The system links the variable object of the task to the
244 * per-core constraint object.
246 * But, the taskset command on Linux takes a mask value specifying a CPU
247 * affinity setting of a given task. If the mask value is 0x03, the given task
248 * will be executed on the first core (CPU0) or the second core (CPU1) on the
249 * given PM. The schedular will determine appropriate placements of tasks,
250 * considering given CPU affinities and task activities.
252 * How should the system formulate constraint problems for an affinity to
255 * The cpu argument must be the host where the task is being executed. The
256 * action object does not have the information about the location where the
257 * action is being executed.
259 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
261 lmm_variable_t var_obj = getVariable();
262 XBT_IN("(%p,%lx)", this, mask);
265 unsigned long nbits = 0;
267 /* FIXME: There is much faster algorithms doing this. */
268 for (int i = 0; i < cpu->m_core; i++) {
269 unsigned long has_affinity = (1UL << i) & mask;
275 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
276 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
281 for (int i = 0; i < cpu->m_core; i++) {
282 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
283 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
285 unsigned long has_affinity = (1UL << i) & mask;
287 /* This function only accepts an affinity setting on the host where the
288 * task is now running. In future, a task might move to another host.
289 * But, at this moment, this function cannot take an affinity setting on
292 * It might be possible to extend the code to allow this function to
293 * accept affinity settings on a future host. We might be able to assign
294 * zero to elem->value to maintain such inactive affinity settings in the
295 * system. But, this will make the system complex. */
296 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
297 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
301 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
302 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */