1 /* Copyright (c) 2013-2014. 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_interface.hpp"
9 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
11 "Logging specific to the SURF cpu module");
13 CpuModelPtr surf_cpu_model_pm;
14 CpuModelPtr surf_cpu_model_vm;
20 CpuPtr getActionCpu(CpuActionPtr action) {
21 return static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var
22 (action->getModel()->getMaxminSystem(),
23 action->getVariable(), 0)));
26 surf_callback(void, CpuPtr) cpuCreatedCallbacks;
27 surf_callback(void, CpuPtr) cpuDestructedCallbacks;
28 surf_callback(void, CpuPtr) cpuStateChangedCallbacks;
29 surf_callback(void, CpuActionPtr) cpuActionStateChangedCallbacks;
35 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
38 while ((xbt_heap_size(getActionHeap()) > 0)
39 && (double_equals(xbt_heap_maxkey(getActionHeap()), now))) {
40 action = static_cast<CpuActionPtr>(static_cast<ActionPtr>(xbt_heap_pop(getActionHeap())));
41 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
43 if (TRACE_is_enabled()) {
44 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
45 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
46 lmm_variable_getvalue(action->getVariable()),
47 action->getLastUpdate(),
48 now - action->getLastUpdate());
53 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
55 /* set the remains to 0 due to precision problems when updating the remaining amount */
56 action->setRemains(0);
57 action->setState(SURF_ACTION_DONE);
58 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
61 if (TRACE_is_enabled()) {
62 //defining the last timestamp that we can safely dump to trace file
63 //without losing the event ascending order (considering all CPU's)
65 ActionListPtr actionSet = getRunningActionSet();
66 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
67 ; it != itend ; ++it) {
68 action = static_cast<CpuActionPtr>(&*it);
70 smaller = action->getLastUpdate();
73 if (action->getLastUpdate() < smaller) {
74 smaller = action->getLastUpdate();
78 TRACE_last_timestamp_to_dump = smaller;
85 void CpuModel::updateActionsStateFull(double now, double delta)
87 CpuActionPtr action = NULL;
88 ActionListPtr running_actions = getRunningActionSet();
90 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
91 ; it != itend ; it=itNext) {
93 action = static_cast<CpuActionPtr>(&*it);
95 if (TRACE_is_enabled()) {
96 CpuPtr x = (CpuPtr) lmm_constraint_id(lmm_get_cnst_from_var
97 (getMaxminSystem(), action->getVariable(), 0));
99 TRACE_surf_host_set_utilization(x->getName(),
100 action->getCategory(),
101 lmm_variable_getvalue(action->getVariable()),
104 TRACE_last_timestamp_to_dump = now - delta;
108 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
111 if (action->getMaxDuration() != NO_MAX_DURATION)
112 action->updateMaxDuration(delta);
115 if ((action->getRemainsNoUpdate() <= 0) &&
116 (lmm_get_variable_weight(action->getVariable()) > 0)) {
118 action->setState(SURF_ACTION_DONE);
119 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
120 (action->getMaxDuration() <= 0)) {
122 action->setState(SURF_ACTION_DONE);
134 surf_callback_emit(cpuCreatedCallbacks, this);
137 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
138 int core, double powerPeak, double powerScale)
139 : Resource(model, name, props)
141 , m_powerPeak(powerPeak)
142 , m_powerScale(powerScale)
143 , p_constraintCore(NULL)
144 , p_constraintCoreId(NULL)
146 surf_callback_emit(cpuCreatedCallbacks, this);
149 Cpu::Cpu(ModelPtr model, const char *name, xbt_dict_t props,
150 lmm_constraint_t constraint, int core, double powerPeak, double powerScale)
151 : Resource(model, name, props, constraint)
153 , m_powerPeak(powerPeak)
154 , m_powerScale(powerScale)
156 surf_callback_emit(cpuCreatedCallbacks, this);
157 /* At now, we assume that a VM does not have a multicore CPU. */
159 xbt_assert(model == surf_cpu_model_pm);
161 p_constraintCore = NULL;
162 p_constraintCoreId = NULL;
163 if (model->getUpdateMechanism() != UM_UNDEFINED) {
164 p_constraintCore = xbt_new(lmm_constraint_t, core);
165 p_constraintCoreId = xbt_new(void*, core);
168 for (i = 0; i < core; i++) {
169 /* just for a unique id, never used as a string. */
170 p_constraintCoreId[i] = bprintf("%s:%i", name, i);
171 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_powerScale * m_powerPeak);
177 surf_callback_emit(cpuDestructedCallbacks, this);
178 if (p_constraintCoreId){
179 for (int i = 0; i < m_core; i++) {
180 xbt_free(p_constraintCoreId[i]);
182 xbt_free(p_constraintCore);
184 if (p_constraintCoreId)
185 xbt_free(p_constraintCoreId);
188 double Cpu::getSpeed(double load)
190 return load * m_powerPeak;
193 double Cpu::getAvailableSpeed()
195 /* number between 0 and 1 */
204 void Cpu::setState(e_surf_resource_state_t state)
206 Resource::setState(state);
207 surf_callback_emit(cpuStateChangedCallbacks, this);
213 void CpuAction::updateRemainingLazy(double now)
217 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
218 "You're updating an action that is not running.");
220 /* bogus priority, skip it */
221 xbt_assert(getPriority() > 0,
222 "You're updating an action that seems suspended.");
224 delta = now - m_lastUpdate;
227 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
228 double_update(&(m_remains), m_lastValue * delta);
231 if (TRACE_is_enabled()) {
232 CpuPtr cpu = static_cast<CpuPtr>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
233 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
236 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
240 m_lastValue = lmm_variable_getvalue(getVariable());
245 * This function formulates a constraint problem that pins a given task to
246 * particular cores. Currently, it is possible to pin a task to an exactly one
247 * specific core. The system links the variable object of the task to the
248 * per-core constraint object.
250 * But, the taskset command on Linux takes a mask value specifying a CPU
251 * affinity setting of a given task. If the mask value is 0x03, the given task
252 * will be executed on the first core (CPU0) or the second core (CPU1) on the
253 * given PM. The schedular will determine appropriate placements of tasks,
254 * considering given CPU affinities and task activities.
256 * How should the system formulate constraint problems for an affinity to
259 * The cpu argument must be the host where the task is being executed. The
260 * action object does not have the information about the location where the
261 * action is being executed.
263 void CpuAction::setAffinity(CpuPtr cpu, unsigned long mask)
265 lmm_variable_t var_obj = getVariable();
266 XBT_IN("(%p,%lx)", this, mask);
269 unsigned long nbits = 0;
271 /* FIXME: There is much faster algorithms doing this. */
272 for (int i = 0; i < cpu->m_core; i++) {
273 unsigned long has_affinity = (1UL << i) & mask;
279 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
280 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
285 for (int i = 0; i < cpu->m_core; i++) {
286 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
287 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
289 unsigned long has_affinity = (1UL << i) & mask;
291 /* This function only accepts an affinity setting on the host where the
292 * task is now running. In future, a task might move to another host.
293 * But, at this moment, this function cannot take an affinity setting on
296 * It might be possible to extend the code to allow this function to
297 * accept affinity settings on a future host. We might be able to assign
298 * zero to elem->value to maintain such inactive affinity settings in the
299 * system. But, this will make the system complex. */
300 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
301 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
305 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
306 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
311 void CpuAction::setState(e_surf_action_state_t state){
312 Action::setState(state);
313 surf_callback_emit(cpuActionStateChangedCallbacks, this);