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 "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
14 simgrid::surf::CpuModel *surf_cpu_model_pm;
15 simgrid::surf::CpuModel *surf_cpu_model_vm;
24 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
26 while ((xbt_heap_size(getActionHeap()) > 0)
27 && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
29 CpuAction *action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
30 XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
31 if (TRACE_is_enabled()) {
32 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
33 TRACE_surf_host_set_utilization(cpu->cname(), action->getCategory(), lmm_variable_getvalue(action->getVariable()),
34 action->getLastUpdate(), now - action->getLastUpdate());
38 XBT_CDEBUG(surf_kernel, "Action %p finished", action);
40 /* set the remains to 0 due to precision problems when updating the remaining amount */
41 action->setRemains(0);
42 action->setState(Action::State::done);
44 if (TRACE_is_enabled()) {
45 //defining the last timestamp that we can safely dump to trace file
46 //without losing the event ascending order (considering all CPU's)
48 ActionList *actionSet = getRunningActionSet();
49 ActionList::iterator it(actionSet->begin());
50 ActionList::iterator itend(actionSet->end());
51 for (; it != itend; ++it) {
52 CpuAction *action = static_cast<CpuAction*>(&*it);
53 if (smaller < 0 || action->getLastUpdate() < smaller)
54 smaller = action->getLastUpdate();
57 TRACE_last_timestamp_to_dump = smaller;
62 void CpuModel::updateActionsStateFull(double now, double delta)
64 CpuAction *action = nullptr;
65 ActionList *running_actions = getRunningActionSet();
66 ActionList::iterator it(running_actions->begin());
67 ActionList::iterator itNext = it;
68 ActionList::iterator itend(running_actions->end());
69 for (; it != itend; it = itNext) {
71 action = static_cast<CpuAction*>(&*it);
72 if (TRACE_is_enabled()) {
73 Cpu *cpu = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
75 TRACE_surf_host_set_utilization(cpu->cname(), action->getCategory(), lmm_variable_getvalue(action->getVariable()),
77 TRACE_last_timestamp_to_dump = now - delta;
80 action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
83 if (action->getMaxDuration() != NO_MAX_DURATION)
84 action->updateMaxDuration(delta);
87 if ((action->getRemainsNoUpdate() <= 0) &&
88 (lmm_get_variable_weight(action->getVariable()) > 0)) {
90 action->setState(Action::State::done);
91 } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
92 (action->getMaxDuration() <= 0)) {
94 action->setState(Action::State::done);
102 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
103 : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
107 Cpu::Cpu(Model* model, simgrid::s4u::Host* host, lmm_constraint_t constraint, std::vector<double>* speedPerPstate,
109 : Resource(model, host->cname(), constraint), coresAmount_(core), host_(host)
111 xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->cname());
113 speed_.peak = speedPerPstate->front();
115 host->pimpl_cpu = this;
116 xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->cname());
118 // Copy the power peak array:
119 for (double value : *speedPerPstate) {
120 speedPerPstate_.push_back(value);
123 xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
126 Cpu::~Cpu() = default;
128 /** @brief The amount of flop per second that this CPU can compute at its current DVFS level */
129 double Cpu::getPstateSpeedCurrent()
134 int Cpu::getNbPStates()
136 return speedPerPstate_.size();
139 void Cpu::setPState(int pstate_index)
141 xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
142 "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", cname(), pstate_index,
143 static_cast<int>(speedPerPstate_.size()));
145 double new_peak_speed = speedPerPstate_[pstate_index];
146 pstate_ = pstate_index;
147 speed_.peak = new_peak_speed;
157 double Cpu::getPstateSpeed(int pstate_index)
159 xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
161 return speedPerPstate_[pstate_index];
164 double Cpu::getSpeed(double load)
166 return load * speed_.peak;
169 double Cpu::getAvailableSpeed()
171 /* number between 0 and 1 */
175 void Cpu::onSpeedChange() {
176 TRACE_surf_host_set_speed(surf_get_clock(), cname(), coresAmount_ * speed_.scale * speed_.peak);
177 s4u::Host::onSpeedChange(*host_);
185 void Cpu::setStateTrace(tmgr_trace_t trace)
187 xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->cname());
189 stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
191 void Cpu::setSpeedTrace(tmgr_trace_t trace)
193 xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->cname());
195 speed_.event = future_evt_set->add_trace(trace, 0.0, this);
203 void CpuAction::updateRemainingLazy(double now)
205 xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
206 xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
208 double delta = now - lastUpdate_;
211 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
212 double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
214 if (TRACE_is_enabled()) {
215 Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
216 TRACE_surf_host_set_utilization(cpu->cname(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
218 XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
222 lastValue_ = lmm_variable_getvalue(getVariable());
225 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
227 void CpuAction::setState(Action::State state){
228 Action::State previous = getState();
229 Action::setState(state);
230 onStateChange(this, previous);
232 /** @brief returns a list of all CPUs that this action is using */
233 std::list<Cpu*> CpuAction::cpus() {
234 std::list<Cpu*> retlist;
235 lmm_system_t sys = getModel()->getMaxminSystem();
236 int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
238 for (int i = 0; i < llen; i++) {
239 /* Beware of composite actions: ptasks put links and cpus together */
240 // extra pb: we cannot dynamic_cast from void*...
241 Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
242 Cpu* cpu = dynamic_cast<Cpu*>(resource);
244 retlist.push_back(cpu);