1 /* Copyright (c) 2013-2015. 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"
8 #include "plugins/energy.hpp"
10 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
12 "Logging specific to the SURF cpu module");
14 int autoload_surf_cpu_model = 1;
15 void_f_void_t surf_cpu_model_init_preparse = NULL;
17 simgrid::surf::CpuModel *surf_cpu_model_pm;
18 simgrid::surf::CpuModel *surf_cpu_model_vm;
27 Cpu *getActionCpu(CpuAction *action) {
28 return static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var
29 (action->getModel()->getMaxminSystem(),
30 action->getVariable(), 0)));
33 surf_callback(void, Cpu*) cpuCreatedCallbacks;
34 surf_callback(void, Cpu*) cpuDestructedCallbacks;
35 surf_callback(void, Cpu*, e_surf_resource_state_t, e_surf_resource_state_t) cpuStateChangedCallbacks;
36 surf_callback(void, CpuAction*, e_surf_action_state_t, e_surf_action_state_t) cpuActionStateChangedCallbacks;
37 void cpu_add_traces(){
38 surf_cpu_model_pm->addTraces();
45 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
48 while ((xbt_heap_size(getActionHeap()) > 0)
49 && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
50 action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
51 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
52 if (TRACE_is_enabled()) {
53 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
54 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
55 lmm_variable_getvalue(action->getVariable()),
56 action->getLastUpdate(),
57 now - action->getLastUpdate());
61 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
63 /* set the remains to 0 due to precision problems when updating the remaining amount */
64 action->setRemains(0);
65 action->setState(SURF_ACTION_DONE);
66 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
68 if (TRACE_is_enabled()) {
69 //defining the last timestamp that we can safely dump to trace file
70 //without losing the event ascending order (considering all CPU's)
72 ActionList *actionSet = getRunningActionSet();
73 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
74 ; it != itend ; ++it) {
75 action = static_cast<CpuAction*>(&*it);
77 smaller = action->getLastUpdate();
80 if (action->getLastUpdate() < smaller) {
81 smaller = action->getLastUpdate();
85 TRACE_last_timestamp_to_dump = smaller;
91 void CpuModel::updateActionsStateFull(double now, double delta)
93 CpuAction *action = NULL;
94 ActionList *running_actions = getRunningActionSet();
96 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
97 ; it != itend ; it=itNext) {
99 action = static_cast<CpuAction*>(&*it);
100 if (TRACE_is_enabled()) {
101 Cpu *x = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
103 TRACE_surf_host_set_utilization(x->getName(),
104 action->getCategory(),
105 lmm_variable_getvalue(action->getVariable()),
108 TRACE_last_timestamp_to_dump = now - delta;
111 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
114 if (action->getMaxDuration() != NO_MAX_DURATION)
115 action->updateMaxDuration(delta);
118 if ((action->getRemainsNoUpdate() <= 0) &&
119 (lmm_get_variable_weight(action->getVariable()) > 0)) {
121 action->setState(SURF_ACTION_DONE);
122 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
123 (action->getMaxDuration() <= 0)) {
125 action->setState(SURF_ACTION_DONE);
139 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
140 int core, double speedPeak, double speedScale,
141 e_surf_resource_state_t stateInitial)
142 : Resource(model, name, props, stateInitial)
144 , m_speedPeak(speedPeak)
145 , m_speedScale(speedScale)
146 , p_constraintCore(NULL)
147 , p_constraintCoreId(NULL)
152 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
153 lmm_constraint_t constraint, int core, double speedPeak,
154 double speedScale, e_surf_resource_state_t stateInitial)
155 : Resource(model, name, props, constraint, stateInitial)
157 , m_speedPeak(speedPeak)
158 , m_speedScale(speedScale)
160 /* At now, we assume that a VM does not have a multicore CPU. */
162 xbt_assert(model == surf_cpu_model_pm);
164 p_constraintCore = NULL;
165 p_constraintCoreId = NULL;
166 if (model->getUpdateMechanism() != UM_UNDEFINED) {
167 p_constraintCore = xbt_new(lmm_constraint_t, core);
168 p_constraintCoreId = xbt_new(void*, core);
171 for (i = 0; i < core; i++) {
172 /* just for a unique id, never used as a string. */
173 p_constraintCoreId[i] = bprintf("%s:%i", name, i);
174 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_speedScale * m_speedPeak);
179 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
180 lmm_constraint_t constraint, int core, double speedPeak, double speedScale)
181 : Cpu(model, name, props, constraint, core, speedPeak, speedScale, SURF_RESOURCE_ON)
184 Cpu::Cpu(Model *model, const char *name, xbt_dict_t props,
185 int core, double speedPeak, double speedScale)
186 : Cpu(model, name, props, core, speedPeak, speedScale, SURF_RESOURCE_ON)
190 surf_callback_emit(cpuDestructedCallbacks, this);
191 if (p_constraintCoreId){
192 for (int i = 0; i < m_core; i++) {
193 xbt_free(p_constraintCoreId[i]);
195 xbt_free(p_constraintCore);
197 if (p_constraintCoreId)
198 xbt_free(p_constraintCoreId);
201 double Cpu::getCurrentPowerPeak()
206 double Cpu::getSpeed(double load)
208 return load * m_speedPeak;
211 double Cpu::getAvailableSpeed()
213 /* number between 0 and 1 */
222 void Cpu::setState(e_surf_resource_state_t state)
224 e_surf_resource_state_t old = Resource::getState();
225 Resource::setState(state);
226 surf_callback_emit(cpuStateChangedCallbacks, this, old, state);
233 void CpuAction::updateRemainingLazy(double now)
237 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
238 "You're updating an action that is not running.");
240 /* bogus priority, skip it */
241 xbt_assert(getPriority() > 0,
242 "You're updating an action that seems suspended.");
244 delta = now - m_lastUpdate;
247 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
248 double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
250 if (TRACE_is_enabled()) {
251 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
252 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
254 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
258 m_lastValue = lmm_variable_getvalue(getVariable());
263 * This function formulates a constraint problem that pins a given task to
264 * particular cores. Currently, it is possible to pin a task to an exactly one
265 * specific core. The system links the variable object of the task to the
266 * per-core constraint object.
268 * But, the taskset command on Linux takes a mask value specifying a CPU
269 * affinity setting of a given task. If the mask value is 0x03, the given task
270 * will be executed on the first core (CPU0) or the second core (CPU1) on the
271 * given PM. The schedular will determine appropriate placements of tasks,
272 * considering given CPU affinities and task activities.
274 * How should the system formulate constraint problems for an affinity to
277 * The cpu argument must be the host where the task is being executed. The
278 * action object does not have the information about the location where the
279 * action is being executed.
281 void CpuAction::setAffinity(Cpu *cpu, unsigned long mask)
283 lmm_variable_t var_obj = getVariable();
284 XBT_IN("(%p,%lx)", this, mask);
287 unsigned long nbits = 0;
289 /* FIXME: There is much faster algorithms doing this. */
290 for (int i = 0; i < cpu->m_core; i++) {
291 unsigned long has_affinity = (1UL << i) & mask;
297 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
298 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
303 for (int i = 0; i < cpu->m_core; i++) {
304 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
305 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
307 unsigned long has_affinity = (1UL << i) & mask;
309 /* This function only accepts an affinity setting on the host where the
310 * task is now running. In future, a task might move to another host.
311 * But, at this moment, this function cannot take an affinity setting on
314 * It might be possible to extend the code to allow this function to
315 * accept affinity settings on a future host. We might be able to assign
316 * zero to elem->value to maintain such inactive affinity settings in the
317 * system. But, this will make the system complex. */
318 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
319 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
323 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
324 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
329 void CpuAction::setState(e_surf_action_state_t state){
330 e_surf_action_state_t old = getState();
331 Action::setState(state);
332 surf_callback_emit(cpuActionStateChangedCallbacks, this, old, state);