2 * Copyright (c) 2015-2017, 2019-2020 ARM Limited
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.
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.
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.
38 #include "sim/power_state.hh"
40 #include "base/logging.hh"
41 #include "base/trace.hh"
42 #include "debug/PowerDomain.hh"
43 #include "sim/power_domain.hh"
45 PowerState::PowerState(const PowerStateParams
*p
) :
46 SimObject(p
), _currState(p
->default_state
),
47 possibleStates(p
->possible_states
.begin(),
48 p
->possible_states
.end()),
51 for (auto &pm
: p
->leaders
) {
52 // Register this object as a follower. This object is
53 // dependent on pm for power state transitions
54 pm
->addFollower(this);
59 PowerState::setControlledDomain(PowerDomain
* pwr_dom
)
61 // Only a power domain can register as dependant of a power stated
63 controlledDomain
= pwr_dom
;
64 DPRINTF(PowerDomain
, "%s is registered as controlled by %s \n",
65 pwr_dom
->name(), name());
69 PowerState::serialize(CheckpointOut
&cp
) const
71 unsigned int currState
= (unsigned int)_currState
;
73 SERIALIZE_SCALAR(currState
);
74 SERIALIZE_SCALAR(prvEvalTick
);
78 PowerState::unserialize(CheckpointIn
&cp
)
80 unsigned int currState
;
82 UNSERIALIZE_SCALAR(currState
);
83 UNSERIALIZE_SCALAR(prvEvalTick
);
85 _currState
= Enums::PwrState(currState
);
89 PowerState::set(Enums::PwrState p
)
91 // Check if this power state is actually allowed by checking whether it is
92 // present in pwrStateToIndex-dictionary
93 panic_if(possibleStates
.find(p
) == possibleStates
.end(),
94 "Cannot go to %s in %s \n", Enums::PwrStateStrings
[p
], name());
96 // Function should ideally be called only when there is a state change
97 if (_currState
== p
) {
98 warn_once("PowerState: Already in the requested power state, "
103 // No need to compute stats if in the same tick, update state though. This
104 // can happen in cases like a) during start of the simulation multiple
105 // state changes happens in init/startup phase, b) one takes a decision to
106 // migrate state but decides to reverts back to the original state in the
107 // same tick if other conditions are not met elsewhere.
108 // Any state change related stats would have been recorded on previous call
110 if (prvEvalTick
== curTick() && curTick() != 0) {
111 warn("PowerState: More than one power state change request "
112 "encountered within the same simulation tick");
117 // Record stats for previous state.
122 stats
.numTransitions
++;
124 // Update the domain this object controls, if there is one
125 if (controlledDomain
) {
126 controlledDomain
->pwrStateChangeCallback(p
, this);
132 PowerState::matchPwrState(Enums::PwrState p
)
134 // If the object is asked to match a power state, it has to be a follower
135 // and hence should not have a pointer to a powerDomain
136 assert(controlledDomain
== nullptr);
138 // If we are already in this power state, ignore request
139 if (_currState
== p
) {
140 DPRINTF(PowerDomain
, "Already in p-state %s requested to match \n",
141 Enums::PwrStateStrings
[p
]);
145 Enums::PwrState old_state
= _currState
;
146 if (possibleStates
.find(p
) != possibleStates
.end()) {
147 // If this power state is allowed in this object, just go there
150 // Loop over all power states in this object and find a power state
151 // which is more performant than the requested one (considering we
152 // cannot match it exactly)
153 for (auto rev_it
= possibleStates
.crbegin();
154 rev_it
!= possibleStates
.crend(); rev_it
++) {
155 if (*(rev_it
) <= p
) {
156 // This power state is the least performant power state that is
157 // still more performant than the requested one
158 DPRINTF(PowerDomain
, "Best match for %s is %s \n",
159 Enums::PwrStateStrings
[p
],
160 Enums::PwrStateStrings
[*(rev_it
)]);
166 // Check if the transition happened
167 // The only case in which the power state cannot change is if the
168 // object is already at in its most performant state.
169 warn_if((_currState
== old_state
) &&
170 possibleStates
.find(_currState
) != possibleStates
.begin(),
171 "Transition to power state %s was not possible, SimObject already"
172 " in the most performance state %s",
173 Enums::PwrStateStrings
[p
], Enums::PwrStateStrings
[_currState
]);
175 stats
.numPwrMatchStateTransitions
++;
180 PowerState::computeStats()
182 // Calculate time elapsed from last (valid) state change
183 Tick elapsed_time
= curTick() - prvEvalTick
;
185 stats
.pwrStateResidencyTicks
[_currState
] += elapsed_time
;
187 // Time spent in CLK_GATED state, this might change depending on
188 // transition to other low power states in respective simulation
190 if (_currState
== Enums::PwrState::CLK_GATED
) {
191 stats
.ticksClkGated
.sample(elapsed_time
);
194 prvEvalTick
= curTick();
198 PowerState::getWeights() const
200 // Get residency stats
201 std::vector
<double> ret
;
202 Stats::VCounter residencies
;
203 stats
.pwrStateResidencyTicks
.value(residencies
);
205 // Account for current state too!
206 Tick elapsed_time
= curTick() - prvEvalTick
;
207 residencies
[_currState
] += elapsed_time
;
209 ret
.resize(Enums::PwrState::Num_PwrState
);
210 for (unsigned i
= 0; i
< Enums::PwrState::Num_PwrState
; i
++)
211 ret
[i
] = residencies
[i
] / \
212 (stats
.pwrStateResidencyTicks
.total() + elapsed_time
);
217 PowerState::PowerStateStats::PowerStateStats(PowerState
&co
)
220 ADD_STAT(numTransitions
,
221 "Number of power state transitions"),
222 ADD_STAT(numPwrMatchStateTransitions
,
223 "Number of power state transitions due match request"),
224 ADD_STAT(ticksClkGated
,
225 "Distribution of time spent in the clock gated state"),
226 ADD_STAT(pwrStateResidencyTicks
,
227 "Cumulative time (in ticks) in various power states")
232 PowerState::PowerStateStats::regStats()
234 Stats::Group::regStats();
236 using namespace Stats
;
238 const PowerStateParams
*p
= powerState
.params();
240 numTransitions
.flags(nozero
);
241 numPwrMatchStateTransitions
.flags(nozero
);
243 // Each sample is time in ticks
244 unsigned num_bins
= std::max(p
->clk_gate_bins
, 10U);
246 .init(p
->clk_gate_min
, p
->clk_gate_max
,
247 (p
->clk_gate_max
/ num_bins
))
248 .flags(pdf
| nozero
| nonan
)
251 pwrStateResidencyTicks
252 .init(Enums::PwrState::Num_PwrState
)
255 for (int i
= 0; i
< Enums::PwrState::Num_PwrState
; i
++) {
256 pwrStateResidencyTicks
.subname(i
, Enums::PwrStateStrings
[i
]);
263 PowerState::PowerStateStats::preDumpStats()
265 Stats::Group::preDumpStats();
268 * For every stats dump, the power state residency and other distribution
269 * stats should be computed just before the dump to ensure correct stats
270 * value being reported for current dump window. It avoids things like
271 * having any unreported time spent in a power state to be forwarded to the
272 * next dump window which might have rather unpleasant effects (like
273 * perturbing the distribution stats).
275 powerState
.computeStats();
279 PowerStateParams::create()
281 return new PowerState(this);