base: Tag API methods in amo.hh
[gem5.git] / src / sim / power_state.cc
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 "base/trace.hh"
42 #include "debug/PowerDomain.hh"
43 #include "sim/power_domain.hh"
44
45 PowerState::PowerState(const PowerStateParams *p) :
46 SimObject(p), _currState(p->default_state),
47 possibleStates(p->possible_states.begin(),
48 p->possible_states.end()),
49 stats(*this)
50 {
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);
55 }
56 }
57
58 void
59 PowerState::setControlledDomain(PowerDomain* pwr_dom)
60 {
61 // Only a power domain can register as dependant of a power stated
62 // object
63 controlledDomain = pwr_dom;
64 DPRINTF(PowerDomain, "%s is registered as controlled by %s \n",
65 pwr_dom->name(), name());
66 }
67
68 void
69 PowerState::serialize(CheckpointOut &cp) const
70 {
71 unsigned int currState = (unsigned int)_currState;
72
73 SERIALIZE_SCALAR(currState);
74 SERIALIZE_SCALAR(prvEvalTick);
75 }
76
77 void
78 PowerState::unserialize(CheckpointIn &cp)
79 {
80 unsigned int currState;
81
82 UNSERIALIZE_SCALAR(currState);
83 UNSERIALIZE_SCALAR(prvEvalTick);
84
85 _currState = Enums::PwrState(currState);
86 }
87
88 void
89 PowerState::set(Enums::PwrState p)
90 {
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());
95
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, "
99 "request ignored");
100 return;
101 }
102
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
109 // to this function.
110 if (prvEvalTick == curTick() && curTick() != 0) {
111 warn("PowerState: More than one power state change request "
112 "encountered within the same simulation tick");
113 _currState = p;
114 return;
115 }
116
117 // Record stats for previous state.
118 computeStats();
119
120 _currState = p;
121
122 stats.numTransitions++;
123
124 // Update the domain this object controls, if there is one
125 if (controlledDomain) {
126 controlledDomain->pwrStateChangeCallback(p, this);
127 }
128
129 }
130
131 Enums::PwrState
132 PowerState::matchPwrState(Enums::PwrState p)
133 {
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);
137
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]);
142 return _currState;
143 }
144
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
148 set(p);
149 } else {
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)]);
161 set(*(rev_it));
162 break;
163 }
164 }
165 }
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]);
174
175 stats.numPwrMatchStateTransitions++;
176 return _currState;
177 }
178
179 void
180 PowerState::computeStats()
181 {
182 // Calculate time elapsed from last (valid) state change
183 Tick elapsed_time = curTick() - prvEvalTick;
184
185 stats.pwrStateResidencyTicks[_currState] += elapsed_time;
186
187 // Time spent in CLK_GATED state, this might change depending on
188 // transition to other low power states in respective simulation
189 // objects.
190 if (_currState == Enums::PwrState::CLK_GATED) {
191 stats.ticksClkGated.sample(elapsed_time);
192 }
193
194 prvEvalTick = curTick();
195 }
196
197 std::vector<double>
198 PowerState::getWeights() const
199 {
200 // Get residency stats
201 std::vector<double> ret;
202 Stats::VCounter residencies;
203 stats.pwrStateResidencyTicks.value(residencies);
204
205 // Account for current state too!
206 Tick elapsed_time = curTick() - prvEvalTick;
207 residencies[_currState] += elapsed_time;
208
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);
213
214 return ret;
215 }
216
217 PowerState::PowerStateStats::PowerStateStats(PowerState &co)
218 : Stats::Group(&co),
219 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")
228 {
229 }
230
231 void
232 PowerState::PowerStateStats::regStats()
233 {
234 Stats::Group::regStats();
235
236 using namespace Stats;
237
238 const PowerStateParams *p = powerState.params();
239
240 numTransitions.flags(nozero);
241 numPwrMatchStateTransitions.flags(nozero);
242
243 // Each sample is time in ticks
244 unsigned num_bins = std::max(p->clk_gate_bins, 10U);
245 ticksClkGated
246 .init(p->clk_gate_min, p->clk_gate_max,
247 (p->clk_gate_max / num_bins))
248 .flags(pdf | nozero | nonan)
249 ;
250
251 pwrStateResidencyTicks
252 .init(Enums::PwrState::Num_PwrState)
253 .flags(nozero)
254 ;
255 for (int i = 0; i < Enums::PwrState::Num_PwrState; i++) {
256 pwrStateResidencyTicks.subname(i, Enums::PwrStateStrings[i]);
257 }
258
259 numTransitions = 0;
260 }
261
262 void
263 PowerState::PowerStateStats::preDumpStats()
264 {
265 Stats::Group::preDumpStats();
266
267 /**
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).
274 */
275 powerState.computeStats();
276 }
277
278 PowerState*
279 PowerStateParams::create()
280 {
281 return new PowerState(this);
282 }