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 while ((xbt_heap_size(getActionHeap()) > 0)
18 && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
19 action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(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 = static_cast<CpuActionPtr>(&*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 CpuActionPtr 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 = static_cast<CpuActionPtr>(&*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(ModelPtr model, const char *name, xbt_dict_t props,
116 int core, double powerPeak, double powerScale)
117 : Resource(model, name, props)
119 , m_powerPeak(powerPeak)
120 , m_powerScale(powerScale)
121 , p_constraintCore(NULL)
122 , p_constraintCoreId(NULL)
125 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
126 lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
127 : Resource(model, name, props, constraint)
129 , m_powerPeak(powerPeak)
130 , m_powerScale(powerScale)
132 /* At now, we assume that a VM does not have a multicore CPU. */
134 xbt_assert(model == surf_cpu_model_pm);
136 p_constraintCore = NULL;
137 p_constraintCoreId = NULL;
138 if (model->getUpdateMechanism() != UM_UNDEFINED) {
139 p_constraintCore = xbt_new(lmm_constraint_t, core);
140 p_constraintCoreId = xbt_new(void*, core);
143 for (i = 0; i < core; i++) {
144 /* just for a unique id, never used as a string. */
145 p_constraintCoreId[i] = bprintf("%s:%i", name, i);
146 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
152 if (getModel()->getUpdateMechanism() != UM_UNDEFINED){
153 for (int i = 0; i < m_core; i++) {
154 xbt_free(p_constraintCoreId[i]);
156 xbt_free(p_constraintCore);
157 xbt_free(p_constraintCoreId);
161 double Cpu::getSpeed(double load)
163 return load * m_powerPeak;
166 double Cpu::getAvailableSpeed()
168 /* number between 0 and 1 */
181 void CpuAction::updateRemainingLazy(double now)
185 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
186 "You're updating an action that is not running.");
188 /* bogus priority, skip it */
189 xbt_assert(getPriority() > 0,
190 "You're updating an action that seems suspended.");
192 delta = now - m_lastUpdate;
195 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %lf, last_update was: %lf", this, m_remains, m_lastUpdate);
196 double_update(&(m_remains), m_lastValue * delta);
199 if (TRACE_is_enabled()) {
200 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
201 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
204 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %lf", this, m_remains);
208 m_lastValue = lmm_variable_getvalue(getVariable());
211 void CpuAction::setBound(double bound)
213 XBT_IN("(%p,%g)", this, bound);
215 lmm_update_variable_bound(getModel()->getMaxminSystem(), getVariable(), bound);
217 if (getModel()->getUpdateMechanism() == UM_LAZY)
218 heapRemove(getModel()->getActionHeap());
224 * This function formulates a constraint problem that pins a given task to
225 * particular cores. Currently, it is possible to pin a task to an exactly one
226 * specific core. The system links the variable object of the task to the
227 * per-core constraint object.
229 * But, the taskset command on Linux takes a mask value specifying a CPU
230 * affinity setting of a given task. If the mask value is 0x03, the given task
231 * will be executed on the first core (CPU0) or the second core (CPU1) on the
232 * given PM. The schedular will determine appropriate placements of tasks,
233 * considering given CPU affinities and task activities.
235 * How should the system formulate constraint problems for an affinity to
238 * The cpu argument must be the host where the task is being executed. The
239 * action object does not have the information about the location where the
240 * action is being executed.
242 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
244 lmm_variable_t var_obj = getVariable();
245 XBT_IN("(%p,%lx)", this, mask);
248 unsigned long nbits = 0;
250 /* FIXME: There is much faster algorithms doing this. */
251 for (int i = 0; i < cpu->m_core; i++) {
252 unsigned long has_affinity = (1UL << i) & mask;
258 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
259 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
264 for (int i = 0; i < cpu->m_core; i++) {
265 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
266 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
268 unsigned long has_affinity = (1UL << i) & mask;
270 /* This function only accepts an affinity setting on the host where the
271 * task is now running. In future, a task might move to another host.
272 * But, at this moment, this function cannot take an affinity setting on
275 * It might be possible to extend the code to allow this function to
276 * accept affinity settings on a future host. We might be able to assign
277 * zero to elem->value to maintain such inactive affinity settings in the
278 * system. But, this will make the system complex. */
279 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
280 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
284 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
285 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */