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*/)
17 CpuActionLmmPtr action;
18 while ((xbt_heap_size(getActionHeap()) > 0)
19 && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
20 action = dynamic_cast<CpuActionLmmPtr>(static_cast<ActionLmmPtr>(xbt_heap_pop(getActionHeap())));
21 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
23 if (TRACE_is_enabled()) {
24 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
25 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
26 lmm_variable_getvalue(action->getVariable()),
27 action->getLastUpdate(),
28 now - action->getLastUpdate());
33 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
35 action->updateEnergy();
37 /* set the remains to 0 due to precision problems when updating the remaining amount */
38 action->setRemains(0);
39 action->setState(SURF_ACTION_DONE);
40 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
43 if (TRACE_is_enabled()) {
44 //defining the last timestamp that we can safely dump to trace file
45 //without losing the event ascending order (considering all CPU's)
47 xbt_swag_t actionSet = getRunningActionSet();
48 xbt_swag_foreach(_action, actionSet) {
49 action = dynamic_cast<CpuActionLmmPtr>(static_cast<ActionPtr>(_action));
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 void *_action, *_next_action;
69 CpuActionLmmPtr action = NULL;
70 xbt_swag_t running_actions = getRunningActionSet();
72 xbt_swag_foreach_safe(_action, _next_action, running_actions) {
73 action = dynamic_cast<CpuActionLmmPtr>(static_cast<ActionPtr>(_action));
75 if (TRACE_is_enabled()) {
76 CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
77 (getMaxminSystem(), action->getVariable(), 0));
79 TRACE_surf_host_set_utilization(x->getName(),
80 action->getCategory(),
81 lmm_variable_getvalue(action->getVariable()),
84 TRACE_last_timestamp_to_dump = now - delta;
88 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
91 if (action->getMaxDuration() != NO_MAX_DURATION)
92 action->updateMaxDuration(delta);
95 if ((action->getRemains() <= 0) &&
96 (lmm_get_variable_weight(action->getVariable()) > 0)) {
98 action->setState(SURF_ACTION_DONE);
99 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
100 (action->getMaxDuration() <= 0)) {
102 action->setState(SURF_ACTION_DONE);
104 action->updateEnergy();
114 Cpu::Cpu(int core, double powerPeak, double powerScale)
115 : m_core(core), m_powerPeak(powerPeak), m_powerScale(powerScale)
118 double Cpu::getSpeed(double load)
120 return load * m_powerPeak;
123 double Cpu::getAvailableSpeed()
125 /* number between 0 and 1 */
134 CpuLmm::CpuLmm(lmm_constraint_t constraint)
135 : ResourceLmm(constraint), p_constraintCore(NULL), p_constraintCoreId(NULL)
138 CpuLmm::CpuLmm(lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
139 : ResourceLmm(constraint)
140 , Cpu(core, powerPeak, powerScale)
142 /* At now, we assume that a VM does not have a multicore CPU. */
144 xbt_assert(getModel() == surf_cpu_model_pm);
147 p_constraintCore = xbt_new(lmm_constraint_t, core);
148 p_constraintCoreId = xbt_new(void*, core);
151 for (i = 0; i < core; i++) {
152 /* just for a unique id, never used as a string. */
153 p_constraintCoreId[i] = bprintf("%s:%i", getName(), i);
154 p_constraintCore[i] = lmm_constraint_new(getModel()->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
159 if (p_constraintCore){
160 for (int i = 0; i < m_core; i++) {
161 xbt_free(p_constraintCoreId[i]);
163 xbt_free(p_constraintCore);
164 xbt_free(p_constraintCoreId);
172 void CpuActionLmm::updateRemainingLazy(double now)
176 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
177 "You're updating an action that is not running.");
179 /* bogus priority, skip it */
180 xbt_assert(getPriority() > 0,
181 "You're updating an action that seems suspended.");
183 delta = now - m_lastUpdate;
186 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
187 double_update(&(m_remains), m_lastValue * delta);
190 if (TRACE_is_enabled()) {
191 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
192 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
195 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
199 m_lastValue = lmm_variable_getvalue(getVariable());
202 void CpuActionLmm::setBound(double bound)
204 XBT_IN("(%p,%g)", this, bound);
206 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
208 if (getModel()->getUpdateMechanism() == UM_LAZY)
209 heapRemove(getModel()->getActionHeap());
215 * This function formulates a constraint problem that pins a given task to
216 * particular cores. Currently, it is possible to pin a task to an exactly one
217 * specific core. The system links the variable object of the task to the
218 * per-core constraint object.
220 * But, the taskset command on Linux takes a mask value specifying a CPU
221 * affinity setting of a given task. If the mask value is 0x03, the given task
222 * will be executed on the first core (CPU0) or the second core (CPU1) on the
223 * given PM. The schedular will determine appropriate placements of tasks,
224 * considering given CPU affinities and task activities.
226 * How should the system formulate constraint problems for an affinity to
229 * The cpu argument must be the host where the task is being executed. The
230 * action object does not have the information about the location where the
231 * action is being executed.
233 void CpuActionLmm::setAffinity(CpuPtr _cpu, unsigned long mask)
235 lmm_variable_t var_obj = getVariable();
236 CpuLmmPtr cpu = reinterpret_cast<CpuLmmPtr>(_cpu);
237 XBT_IN("(%p,%lx)", this, mask);
240 unsigned long nbits = 0;
242 /* FIXME: There is much faster algorithms doing this. */
243 for (int i = 0; i < cpu->m_core; i++) {
244 unsigned long has_affinity = (1UL << i) & mask;
250 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
251 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
256 for (int i = 0; i < cpu->m_core; i++) {
257 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
258 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
260 unsigned long has_affinity = (1UL << i) & mask;
262 /* This function only accepts an affinity setting on the host where the
263 * task is now running. In future, a task might move to another host.
264 * But, at this moment, this function cannot take an affinity setting on
267 * It might be possible to extend the code to allow this function to
268 * accept affinity settings on a future host. We might be able to assign
269 * zero to elem->value to maintain such inactive affinity settings in the
270 * system. But, this will make the system complex. */
271 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
272 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
276 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
277 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */