gem5  v21.1.0.2
power_state.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017, 2019-2020 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "sim/power_state.hh"
39 
40 #include <cassert>
41 
42 #include "base/logging.hh"
43 #include "base/trace.hh"
44 #include "debug/PowerDomain.hh"
45 #include "sim/cur_tick.hh"
46 #include "sim/power_domain.hh"
47 #include "sim/serialize.hh"
48 
49 namespace gem5
50 {
51 
52 PowerState::PowerState(const PowerStateParams &p) :
53  SimObject(p), _currState(p.default_state),
54  possibleStates(p.possible_states.begin(),
55  p.possible_states.end()),
56  stats(*this)
57 {
58  for (auto &pm: p.leaders) {
59  // Register this object as a follower. This object is
60  // dependent on pm for power state transitions
61  pm->addFollower(this);
62  }
63 }
64 
65 void
67 {
68  // Only a power domain can register as dependant of a power stated
69  // object
70  controlledDomain = pwr_dom;
71  DPRINTF(PowerDomain, "%s is registered as controlled by %s \n",
72  pwr_dom->name(), name());
73 }
74 
75 void
77 {
78  unsigned int currState = (unsigned int)_currState;
79 
80  SERIALIZE_SCALAR(currState);
82 }
83 
84 void
86 {
87  unsigned int currState;
88 
89  UNSERIALIZE_SCALAR(currState);
91 
92  _currState = enums::PwrState(currState);
93 }
94 
95 void
96 PowerState::set(enums::PwrState p)
97 {
98  // Check if this power state is actually allowed by checking whether it is
99  // present in pwrStateToIndex-dictionary
100  panic_if(possibleStates.find(p) == possibleStates.end(),
101  "Cannot go to %s in %s \n", enums::PwrStateStrings[p], name());
102 
103  // Function should ideally be called only when there is a state change
104  if (_currState == p) {
105  warn_once("PowerState: Already in the requested power state, "
106  "request ignored");
107  return;
108  }
109 
110  // No need to compute stats if in the same tick, update state though. This
111  // can happen in cases like a) during start of the simulation multiple
112  // state changes happens in init/startup phase, b) one takes a decision to
113  // migrate state but decides to reverts back to the original state in the
114  // same tick if other conditions are not met elsewhere.
115  // Any state change related stats would have been recorded on previous call
116  // to this function.
117  if (prvEvalTick == curTick() && curTick() != 0) {
118  warn("PowerState: More than one power state change request "
119  "encountered within the same simulation tick");
120  _currState = p;
121  return;
122  }
123 
124  // Record stats for previous state.
125  computeStats();
126 
127  _currState = p;
128 
130 
131  // Update the domain this object controls, if there is one
132  if (controlledDomain) {
134  }
135 
136 }
137 
138 enums::PwrState
139 PowerState::matchPwrState(enums::PwrState p)
140 {
141  // If the object is asked to match a power state, it has to be a follower
142  // and hence should not have a pointer to a powerDomain
143  assert(controlledDomain == nullptr);
144 
145  // If we are already in this power state, ignore request
146  if (_currState == p) {
147  DPRINTF(PowerDomain, "Already in p-state %s requested to match \n",
148  enums::PwrStateStrings[p]);
149  return _currState;
150  }
151 
152  enums::PwrState old_state = _currState;
153  if (possibleStates.find(p) != possibleStates.end()) {
154  // If this power state is allowed in this object, just go there
155  set(p);
156  } else {
157  // Loop over all power states in this object and find a power state
158  // which is more performant than the requested one (considering we
159  // cannot match it exactly)
160  for (auto rev_it = possibleStates.crbegin();
161  rev_it != possibleStates.crend(); rev_it++) {
162  if (*(rev_it) <= p) {
163  // This power state is the least performant power state that is
164  // still more performant than the requested one
165  DPRINTF(PowerDomain, "Best match for %s is %s \n",
166  enums::PwrStateStrings[p],
167  enums::PwrStateStrings[*(rev_it)]);
168  set(*(rev_it));
169  break;
170  }
171  }
172  }
173  // Check if the transition happened
174  // The only case in which the power state cannot change is if the
175  // object is already at in its most performant state.
176  warn_if((_currState == old_state) &&
177  possibleStates.find(_currState) != possibleStates.begin(),
178  "Transition to power state %s was not possible, SimObject already"
179  " in the most performance state %s",
180  enums::PwrStateStrings[p], enums::PwrStateStrings[_currState]);
181 
183  return _currState;
184 }
185 
186 void
188 {
189  // Calculate time elapsed from last (valid) state change
190  Tick elapsed_time = curTick() - prvEvalTick;
191 
192  stats.pwrStateResidencyTicks[_currState] += elapsed_time;
193 
194  // Time spent in CLK_GATED state, this might change depending on
195  // transition to other low power states in respective simulation
196  // objects.
197  if (_currState == enums::PwrState::CLK_GATED) {
198  stats.ticksClkGated.sample(elapsed_time);
199  }
200 
201  prvEvalTick = curTick();
202 }
203 
206 {
207  // Get residency stats
209  statistics::VCounter residencies;
210  stats.pwrStateResidencyTicks.value(residencies);
211 
212  // Account for current state too!
213  Tick elapsed_time = curTick() - prvEvalTick;
214  residencies[_currState] += elapsed_time;
215 
216  ret.resize(enums::PwrState::Num_PwrState);
217  for (unsigned i = 0; i < enums::PwrState::Num_PwrState; i++)
218  ret[i] = residencies[i] / \
219  (stats.pwrStateResidencyTicks.total() + elapsed_time);
220 
221  return ret;
222 }
223 
225  : statistics::Group(&co),
226  powerState(co),
227  ADD_STAT(numTransitions, statistics::units::Count::get(),
228  "Number of power state transitions"),
229  ADD_STAT(numPwrMatchStateTransitions, statistics::units::Count::get(),
230  "Number of power state transitions due match request"),
231  ADD_STAT(ticksClkGated, statistics::units::Tick::get(),
232  "Distribution of time spent in the clock gated state"),
233  ADD_STAT(pwrStateResidencyTicks, statistics::units::Tick::get(),
234  "Cumulative time (in ticks) in various power states")
235 {
236 }
237 
238 void
240 {
242 
243  using namespace statistics;
244 
245  const PowerStateParams &p = powerState.params();
246 
247  numTransitions.flags(nozero);
248  numPwrMatchStateTransitions.flags(nozero);
249 
250  // Each sample is time in ticks
251  unsigned num_bins = std::max(p.clk_gate_bins, 10U);
252  ticksClkGated
253  .init(p.clk_gate_min, p.clk_gate_max,
254  (p.clk_gate_max - p.clk_gate_min + 1.0) / num_bins)
255  .flags(pdf | nozero | nonan)
256  ;
257 
258  pwrStateResidencyTicks
259  .init(enums::PwrState::Num_PwrState)
260  .flags(nozero)
261  ;
262  for (int i = 0; i < enums::PwrState::Num_PwrState; i++) {
263  pwrStateResidencyTicks.subname(i, enums::PwrStateStrings[i]);
264  }
265 
266  numTransitions = 0;
267 }
268 
269 void
271 {
273 
282  powerState.computeStats();
283 }
284 
285 } // namespace gem5
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
gem5::PowerDomain
The PowerDomain groups PowerState objects together to regulate their power states.
Definition: power_domain.hh:59
power_state.hh
warn
#define warn(...)
Definition: logging.hh:245
gem5::PowerState::possibleStates
std::set< enums::PwrState > possibleStates
The possible power states this object can be in.
Definition: power_state.hh:125
gem5::PowerState::PowerStateStats::PowerStateStats
PowerStateStats(PowerState &ps)
Definition: power_state.cc:224
serialize.hh
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:575
gem5::statistics::VectorBase::total
Result total() const
Return a total of all entries in this vector.
Definition: statistics.hh:988
warn_once
#define warn_once(...)
Definition: logging.hh:249
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::statistics::nozero
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:68
cur_tick.hh
gem5::PowerState::controlledDomain
PowerDomain * controlledDomain
The power domain that this power state leads, nullptr if it doesn't lead any.
Definition: power_state.hh:134
std::vector< double >
gem5::statistics::nonan
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:70
gem5::PowerState::setControlledDomain
void setControlledDomain(PowerDomain *pwr_dom)
Definition: power_state.cc:66
gem5::PowerState::PowerStateStats::numTransitions
statistics::Scalar numTransitions
Definition: power_state.hh:145
gem5::PowerState::computeStats
void computeStats()
Record stats values like state residency by computing the time difference from previous update.
Definition: power_state.cc:187
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::PowerState::PowerStateStats::numPwrMatchStateTransitions
statistics::Scalar numPwrMatchStateTransitions
Definition: power_state.hh:146
gem5::statistics::DistBase::sample
void sample(const U &v, int n=1)
Add a value to the distribtion n times.
Definition: statistics.hh:1325
gem5::PowerDomain::pwrStateChangeCallback
void pwrStateChangeCallback(enums::PwrState new_pwr_state, PowerState *leader)
Register the change in power state in one of the leader.
Definition: power_domain.cc:214
gem5::PowerState::PowerState
PowerState(const PowerStateParams &p)
Definition: power_state.cc:52
gem5::PowerState::stats
gem5::PowerState::PowerStateStats stats
gem5::statistics::VectorBase::value
void value(VCounter &vec) const
Definition: statistics.hh:964
gem5::statistics::pdf
const FlagsType pdf
Print the percent of the total that this entry represents.
Definition: info.hh:62
power_domain.hh
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::PowerState::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: power_state.cc:85
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::PowerState
Helper class for objects that have power states.
Definition: power_state.hh:64
gem5::PowerState::matchPwrState
enums::PwrState matchPwrState(enums::PwrState p)
Change the power state of this object to a power state equal to OR more performant than p.
Definition: power_state.cc:139
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::PowerState::PowerStateStats::preDumpStats
void preDumpStats() override
Callback before stats are dumped.
Definition: power_state.cc:270
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:568
gem5::statistics::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
gem5::PowerState::PowerStateStats::ticksClkGated
statistics::Distribution ticksClkGated
Definition: power_state.hh:147
warn_if
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:272
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::PowerState::PowerStateStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: power_state.cc:239
gem5::PowerState::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: power_state.cc:76
logging.hh
gem5::statistics::Group
Statistics container.
Definition: group.hh:93
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::PowerState::getWeights
std::vector< double > getWeights() const
Returns the percentage residency for each power state.
Definition: power_state.cc:205
trace.hh
gem5::PowerState::prvEvalTick
Tick prvEvalTick
Last tick the power stats were calculated.
Definition: power_state.hh:128
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::PowerState::set
void set(enums::PwrState p)
Change the power state of this object to the power state p.
Definition: power_state.cc:96
gem5::PowerState::PowerStateStats::pwrStateResidencyTicks
statistics::Vector pwrStateResidencyTicks
Tracks the time spent in each of the power states.
Definition: power_state.hh:149
gem5::statistics::Group::preDumpStats
virtual void preDumpStats()
Callback before stats are dumped.
Definition: group.cc:99
gem5::PowerState::_currState
enums::PwrState _currState
To keep track of the current power state.
Definition: power_state.hh:122

Generated on Tue Sep 21 2021 12:25:48 for gem5 by doxygen 1.8.17