gem5  v20.0.0.3
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 "base/logging.hh"
41 #include "debug/PowerDomain.hh"
42 #include "sim/power_domain.hh"
43 
44 PowerState::PowerState(const PowerStateParams *p) :
45  SimObject(p), _currState(p->default_state),
46  possibleStates(p->possible_states.begin(),
47  p->possible_states.end()),
48  stats(*this)
49 {
50  for (auto &pm: p->leaders) {
51  // Register this object as a follower. This object is
52  // dependent on pm for power state transitions
53  pm->addFollower(this);
54  }
55 }
56 
57 void
59 {
60  // Only a power domain can register as dependant of a power stated
61  // object
62  controlledDomain = pwr_dom;
63  DPRINTF(PowerDomain, "%s is registered as controlled by %s \n",
64  pwr_dom->name(), name());
65 }
66 
67 void
69 {
70  unsigned int currState = (unsigned int)_currState;
71 
72  SERIALIZE_SCALAR(currState);
74 }
75 
76 void
78 {
79  unsigned int currState;
80 
81  UNSERIALIZE_SCALAR(currState);
83 
84  _currState = Enums::PwrState(currState);
85 }
86 
87 void
88 PowerState::set(Enums::PwrState p)
89 {
90  // Check if this power state is actually allowed by checking whether it is
91  // present in pwrStateToIndex-dictionary
92  panic_if(possibleStates.find(p) == possibleStates.end(),
93  "Cannot go to %s in %s \n", Enums::PwrStateStrings[p], name());
94 
95  // Function should ideally be called only when there is a state change
96  if (_currState == p) {
97  warn_once("PowerState: Already in the requested power state, "
98  "request ignored");
99  return;
100  }
101 
102  // No need to compute stats if in the same tick, update state though. This
103  // can happen in cases like a) during start of the simulation multiple
104  // state changes happens in init/startup phase, b) one takes a decision to
105  // migrate state but decides to reverts back to the original state in the
106  // same tick if other conditions are not met elsewhere.
107  // Any state change related stats would have been recorded on previous call
108  // to this function.
109  if (prvEvalTick == curTick() && curTick() != 0) {
110  warn("PowerState: More than one power state change request "
111  "encountered within the same simulation tick");
112  _currState = p;
113  return;
114  }
115 
116  // Record stats for previous state.
117  computeStats();
118 
119  _currState = p;
120 
122 
123  // Update the domain this object controls, if there is one
124  if (controlledDomain) {
126  }
127 
128 }
129 
130 Enums::PwrState
131 PowerState::matchPwrState(Enums::PwrState p)
132 {
133  // If the object is asked to match a power state, it has to be a follower
134  // and hence should not have a pointer to a powerDomain
135  assert(controlledDomain == nullptr);
136 
137  // If we are already in this power state, ignore request
138  if (_currState == p) {
139  DPRINTF(PowerDomain, "Already in p-state %s requested to match \n",
140  Enums::PwrStateStrings[p]);
141  return _currState;
142  }
143 
144  Enums::PwrState old_state = _currState;
145  if (possibleStates.find(p) != possibleStates.end()) {
146  // If this power state is allowed in this object, just go there
147  set(p);
148  } else {
149  // Loop over all power states in this object and find a power state
150  // which is more performant than the requested one (considering we
151  // cannot match it exactly)
152  for (auto rev_it = possibleStates.crbegin();
153  rev_it != possibleStates.crend(); rev_it++) {
154  if (*(rev_it) <= p) {
155  // This power state is the least performant power state that is
156  // still more performant than the requested one
157  DPRINTF(PowerDomain, "Best match for %s is %s \n",
158  Enums::PwrStateStrings[p],
159  Enums::PwrStateStrings[*(rev_it)]);
160  set(*(rev_it));
161  break;
162  }
163  }
164  }
165  // Check if the transition happened
166  // The only case in which the power state cannot change is if the
167  // object is already at in its most performant state.
168  warn_if((_currState == old_state) &&
169  possibleStates.find(_currState) != possibleStates.begin(),
170  "Transition to power state %s was not possible, SimObject already"
171  " in the most performance state %s",
172  Enums::PwrStateStrings[p], Enums::PwrStateStrings[_currState]);
173 
175  return _currState;
176 }
177 
178 void
180 {
181  // Calculate time elapsed from last (valid) state change
182  Tick elapsed_time = curTick() - prvEvalTick;
183 
184  stats.pwrStateResidencyTicks[_currState] += elapsed_time;
185 
186  // Time spent in CLK_GATED state, this might change depending on
187  // transition to other low power states in respective simulation
188  // objects.
189  if (_currState == Enums::PwrState::CLK_GATED) {
190  stats.ticksClkGated.sample(elapsed_time);
191  }
192 
193  prvEvalTick = curTick();
194 }
195 
198 {
199  // Get residency stats
201  Stats::VCounter residencies;
202  stats.pwrStateResidencyTicks.value(residencies);
203 
204  // Account for current state too!
205  Tick elapsed_time = curTick() - prvEvalTick;
206  residencies[_currState] += elapsed_time;
207 
208  ret.resize(Enums::PwrState::Num_PwrState);
209  for (unsigned i = 0; i < Enums::PwrState::Num_PwrState; i++)
210  ret[i] = residencies[i] / \
211  (stats.pwrStateResidencyTicks.total() + elapsed_time);
212 
213  return ret;
214 }
215 
217  : Stats::Group(&co),
218  powerState(co),
219  ADD_STAT(numTransitions,
220  "Number of power state transitions"),
221  ADD_STAT(numPwrMatchStateTransitions,
222  "Number of power state transitions due match request"),
223  ADD_STAT(ticksClkGated,
224  "Distribution of time spent in the clock gated state"),
225  ADD_STAT(pwrStateResidencyTicks,
226  "Cumulative time (in ticks) in various power states")
227 {
228 }
229 
230 void
232 {
234 
235  using namespace Stats;
236 
237  const PowerStateParams *p = powerState.params();
238 
241 
242  // Each sample is time in ticks
243  unsigned num_bins = std::max(p->clk_gate_bins, 10U);
245  .init(p->clk_gate_min, p->clk_gate_max,
246  (p->clk_gate_max / num_bins))
247  .flags(pdf | nozero | nonan)
248  ;
249 
251  .init(Enums::PwrState::Num_PwrState)
252  .flags(nozero)
253  ;
254  for (int i = 0; i < Enums::PwrState::Num_PwrState; i++) {
255  pwrStateResidencyTicks.subname(i, Enums::PwrStateStrings[i]);
256  }
257 
258  numTransitions = 0;
259 }
260 
261 void
263 {
265 
275 }
276 
277 PowerState*
278 PowerStateParams::create()
279 {
280  return new PowerState(this);
281 }
#define DPRINTF(x,...)
Definition: trace.hh:225
const FlagsType pdf
Print the percent of the total that this entry represents.
Definition: info.hh:51
Derived & subname(off_type index, const std::string &name)
Set the subfield name for the given index, and marks this stat to print at the end of simulation...
Definition: statistics.hh:376
Bitfield< 7 > i
Enums::PwrState _currState
To keep track of the current power state.
Definition: power_state.hh:123
const FlagsType nonan
Don&#39;t print if this is NAN.
Definition: info.hh:59
void preDumpStats() override
Callback before stats are dumped.
Definition: power_state.cc:262
Result total() const
Return a total of all entries in this vector.
Definition: statistics.hh:1099
PowerState(const PowerStateParams *p)
Definition: power_state.cc:44
Stats::Vector pwrStateResidencyTicks
Tracks the time spent in each of the power states.
Definition: power_state.hh:150
void value(VCounter &vec) const
Definition: statistics.hh:1075
Definition: cprintf.cc:40
Helper class for objects that have power states.
Definition: power_state.hh:61
Derived & flags(Flags _flags)
Set the flags and marks this stat to print at the end of simulation.
Definition: statistics.hh:333
Derived & init(size_type size)
Set this vector to have the given size.
Definition: statistics.hh:1149
void computeStats()
Record stats values like state residency by computing the time difference from previous update...
Definition: power_state.cc:179
PowerState::PowerStateStats stats
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:770
virtual void preDumpStats()
Callback before stats are dumped.
Definition: group.cc:95
Tick curTick()
The current simulated tick.
Definition: core.hh:44
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: power_state.cc:77
uint64_t Tick
Tick count type.
Definition: types.hh:61
void setControlledDomain(PowerDomain *pwr_dom)
Definition: power_state.cc:58
std::set< Enums::PwrState > possibleStates
The possible power states this object can be in.
Definition: power_state.hh:126
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:224
PowerStateStats(PowerState &ps)
Definition: power_state.cc:216
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:131
void set(Enums::PwrState p)
Change the power state of this object to the power state p.
Definition: power_state.cc:88
Distribution & init(Counter min, Counter max, Counter bkt)
Set the parameters of this distribution.
Definition: statistics.hh:2606
const Params * params() const
Definition: power_state.hh:68
PowerDomain * controlledDomain
The power domain that this power state leads, nullptr if it doesn&#39;t lead any.
Definition: power_state.hh:135
Stats::Scalar numPwrMatchStateTransitions
Definition: power_state.hh:147
#define warn_once(...)
Definition: logging.hh:212
Tick prvEvalTick
Last tick the power stats were calculated.
Definition: power_state.hh:129
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:763
void pwrStateChangeCallback(Enums::PwrState new_pwr_state, PowerState *leader)
Register the change in power state in one of the leader.
The PowerDomain groups PowerState objects together to regulate their power states.
Definition: power_domain.hh:56
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:67
virtual const std::string name() const
Definition: sim_object.hh:129
std::ostream CheckpointOut
Definition: serialize.hh:63
Group()=delete
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: power_state.cc:68
std::vector< double > getWeights() const
Returns the percentage residency for each power state.
Definition: power_state.cc:197
#define warn(...)
Definition: logging.hh:208
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
PowerState declaration and implementation.
const FlagsType nozero
Don&#39;t print if this is zero.
Definition: info.hh:57
Bitfield< 0 > p
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:181
void regStats() override
Callback to set stat parameters.
Definition: power_state.cc:231
Abstract superclass for simulation objects.
Definition: sim_object.hh:93
void sample(const U &v, int n=1)
Add a value to the distribtion n times.
Definition: statistics.hh:1896
Stats::Distribution ticksClkGated
Definition: power_state.hh:148

Generated on Fri Jul 3 2020 15:53:04 for gem5 by doxygen 1.8.13