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. */
8 #include "cpu_interface.hpp"
9 #include "plugins/energy.hpp"
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf,
13 "Logging specific to the SURF cpu module");
15 int autoload_surf_cpu_model = 1;
16 void_f_void_t surf_cpu_model_init_preparse = NULL;
18 simgrid::surf::CpuModel *surf_cpu_model_pm;
19 simgrid::surf::CpuModel *surf_cpu_model_vm;
28 Cpu *getActionCpu(CpuAction *action) {
29 return static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var
30 (action->getModel()->getMaxminSystem(),
31 action->getVariable(), 0)));
34 simgrid::surf::signal<void(CpuAction*, e_surf_action_state_t, e_surf_action_state_t)> cpuActionStateChangedCallbacks;
36 void cpu_add_traces(){
37 surf_cpu_model_pm->addTraces();
44 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
47 while ((xbt_heap_size(getActionHeap()) > 0)
48 && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
49 action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
50 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
51 if (TRACE_is_enabled()) {
52 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
53 TRACE_surf_host_set_utilization(cpu->getName(), action->getCategory(),
54 lmm_variable_getvalue(action->getVariable()),
55 action->getLastUpdate(),
56 now - action->getLastUpdate());
60 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
62 /* set the remains to 0 due to precision problems when updating the remaining amount */
63 action->setRemains(0);
64 action->setState(SURF_ACTION_DONE);
65 action->heapRemove(getActionHeap()); //FIXME: strange call since action was already popped
67 if (TRACE_is_enabled()) {
68 //defining the last timestamp that we can safely dump to trace file
69 //without losing the event ascending order (considering all CPU's)
71 ActionList *actionSet = getRunningActionSet();
72 for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
73 ; it != itend ; ++it) {
74 action = static_cast<CpuAction*>(&*it);
76 smaller = action->getLastUpdate();
79 if (action->getLastUpdate() < smaller) {
80 smaller = action->getLastUpdate();
84 TRACE_last_timestamp_to_dump = smaller;
90 void CpuModel::updateActionsStateFull(double now, double delta)
92 CpuAction *action = NULL;
93 ActionList *running_actions = getRunningActionSet();
95 for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
96 ; it != itend ; it=itNext) {
98 action = static_cast<CpuAction*>(&*it);
99 if (TRACE_is_enabled()) {
100 Cpu *x = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
102 TRACE_surf_host_set_utilization(x->getName(),
103 action->getCategory(),
104 lmm_variable_getvalue(action->getVariable()),
107 TRACE_last_timestamp_to_dump = now - delta;
110 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
113 if (action->getMaxDuration() != NO_MAX_DURATION)
114 action->updateMaxDuration(delta);
117 if ((action->getRemainsNoUpdate() <= 0) &&
118 (lmm_get_variable_weight(action->getVariable()) > 0)) {
120 action->setState(SURF_ACTION_DONE);
121 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
122 (action->getMaxDuration() <= 0)) {
124 action->setState(SURF_ACTION_DONE);
134 Cpu::Cpu(Model *model, simgrid::Host *host,
135 xbt_dynar_t speedPeakList, int pstate,
136 int core, double speedPeak, double speedScale,
138 : Cpu(model, host, NULL/*constraint*/, speedPeakList, pstate, core, speedPeak, speedScale, initiallyOn)
142 Cpu::Cpu(Model *model, simgrid::Host *host, lmm_constraint_t constraint,
143 xbt_dynar_t speedPeakList, int pstate,
144 int core, double speedPeak,
145 double speedScale, int initiallyOn)
146 : Resource(model, host->getName().c_str(), constraint, initiallyOn)
148 , m_speedPeak(speedPeak)
149 , m_speedScale(speedScale)
152 host->pimpl_cpu = this;
153 xbt_assert(m_speedScale > 0, "Available speed has to be >0");
155 // Copy the power peak array:
156 p_speedPeakList = xbt_dynar_new(sizeof(double), nullptr);
157 unsigned long n = xbt_dynar_length(speedPeakList);
158 for (unsigned long i = 0; i != n; ++i) {
159 double value = xbt_dynar_get_as(speedPeakList, i, double);
160 xbt_dynar_push(p_speedPeakList, &value);
165 /* Currently, we assume that a VM does not have a multicore CPU. */
167 xbt_assert(model == surf_cpu_model_pm);
169 if (model->getUpdateMechanism() != UM_UNDEFINED) {
170 p_constraintCore = xbt_new(lmm_constraint_t, core);
171 p_constraintCoreId = xbt_new(void*, core);
174 for (i = 0; i < core; i++) {
175 /* just for a unique id, never used as a string. */
176 p_constraintCoreId[i] = bprintf("%s:%i", host->getName().c_str(), i);
177 p_constraintCore[i] = lmm_constraint_new(model->getMaxminSystem(), p_constraintCoreId[i], m_speedScale * m_speedPeak);
184 if (p_constraintCoreId){
185 for (int i = 0; i < m_core; i++) {
186 xbt_free(p_constraintCoreId[i]);
188 xbt_free(p_constraintCore);
190 if (p_constraintCoreId)
191 xbt_free(p_constraintCoreId);
194 double Cpu::getCurrentPowerPeak()
199 int Cpu::getNbPStates()
201 return xbt_dynar_length(p_speedPeakList);
204 void Cpu::setPState(int pstate_index)
206 xbt_dynar_t plist = p_speedPeakList;
207 xbt_assert(pstate_index <= (int)xbt_dynar_length(plist),
208 "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", getName(), pstate_index, (int)xbt_dynar_length(plist));
210 double new_peak_speed = xbt_dynar_get_as(plist, pstate_index, double);
211 m_pstate = pstate_index;
212 m_speedPeak = new_peak_speed;
222 double Cpu::getPowerPeakAt(int pstate_index)
224 xbt_dynar_t plist = p_speedPeakList;
225 xbt_assert((pstate_index <= (int)xbt_dynar_length(plist)), "Invalid parameters (pstate index out of bounds)");
227 return xbt_dynar_get_as(plist, pstate_index, double);
230 double Cpu::getSpeed(double load)
232 return load * m_speedPeak;
235 double Cpu::getAvailableSpeed()
237 /* number between 0 and 1 */
241 void Cpu::onSpeedChange() {
242 TRACE_surf_host_set_speed(surf_get_clock(), getName(),
243 m_core * m_speedScale * m_speedPeak);
256 void CpuAction::updateRemainingLazy(double now)
260 xbt_assert(getStateSet() == getModel()->getRunningActionSet(),
261 "You're updating an action that is not running.");
263 /* bogus priority, skip it */
264 xbt_assert(getPriority() > 0,
265 "You're updating an action that seems suspended.");
267 delta = now - m_lastUpdate;
270 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, m_remains, m_lastUpdate);
271 double_update(&(m_remains), m_lastValue * delta, sg_maxmin_precision*sg_surf_precision);
273 if (TRACE_is_enabled()) {
274 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
275 TRACE_surf_host_set_utilization(cpu->getName(), getCategory(), m_lastValue, m_lastUpdate, now - m_lastUpdate);
277 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, m_remains);
281 m_lastValue = lmm_variable_getvalue(getVariable());
286 * This function formulates a constraint problem that pins a given task to
287 * particular cores. Currently, it is possible to pin a task to an exactly one
288 * specific core. The system links the variable object of the task to the
289 * per-core constraint object.
291 * But, the taskset command on Linux takes a mask value specifying a CPU
292 * affinity setting of a given task. If the mask value is 0x03, the given task
293 * will be executed on the first core (CPU0) or the second core (CPU1) on the
294 * given PM. The schedular will determine appropriate placements of tasks,
295 * considering given CPU affinities and task activities.
297 * How should the system formulate constraint problems for an affinity to
300 * The cpu argument must be the host where the task is being executed. The
301 * action object does not have the information about the location where the
302 * action is being executed.
304 void CpuAction::setAffinity(Cpu *cpu, unsigned long mask)
306 lmm_variable_t var_obj = getVariable();
307 XBT_IN("(%p,%lx)", this, mask);
310 unsigned long nbits = 0;
312 /* FIXME: There is much faster algorithms doing this. */
313 for (int i = 0; i < cpu->m_core; i++) {
314 unsigned long has_affinity = (1UL << i) & mask;
320 XBT_CRITICAL("Do not specify multiple cores for an affinity mask.");
321 XBT_CRITICAL("See the comment in cpu_action_set_affinity().");
326 for (int i = 0; i < cpu->m_core; i++) {
327 XBT_DEBUG("clear affinity %p to cpu-%d@%s", this, i, cpu->getName());
328 lmm_shrink(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj);
330 unsigned long has_affinity = (1UL << i) & mask;
332 /* This function only accepts an affinity setting on the host where the
333 * task is now running. In future, a task might move to another host.
334 * But, at this moment, this function cannot take an affinity setting on
337 * It might be possible to extend the code to allow this function to
338 * accept affinity settings on a future host. We might be able to assign
339 * zero to elem->value to maintain such inactive affinity settings in the
340 * system. But, this will make the system complex. */
341 XBT_DEBUG("set affinity %p to cpu-%d@%s", this, i, cpu->getName());
342 lmm_expand(cpu->getModel()->getMaxminSystem(), cpu->p_constraintCore[i], var_obj, 1.0);
346 if (cpu->getModel()->getUpdateMechanism() == UM_LAZY) {
347 /* FIXME (hypervisor): Do we need to do something for the LAZY mode? */
352 simgrid::surf::signal<void(simgrid::surf::CpuAction*, e_surf_action_state_t, e_surf_action_state_t)> CpuAction::onStateChange;
354 void CpuAction::setState(e_surf_action_state_t state){
355 e_surf_action_state_t old = getState();
356 Action::setState(state);
357 onStateChange(this, old, state);