clocked_object.hh (11532:05ac17048f55) clocked_object.hh (11800:54436a1784dc)
1/*
2 * Copyright (c) 2012-2013, 2015-2016 ARM Limited
3 * Copyright (c) 2013 Cornell University
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * Authors: Andreas Hansson
39 * Christopher Torng
40 * Akash Bagdia
41 * David Guillen Fandos
42 */
43
44/**
45 * @file
46 * ClockedObject declaration and implementation.
47 */
48
49#ifndef __SIM_CLOCKED_OBJECT_HH__
50#define __SIM_CLOCKED_OBJECT_HH__
51
52#include "base/callback.hh"
53#include "base/intmath.hh"
1/*
2 * Copyright (c) 2012-2013, 2015-2016 ARM Limited
3 * Copyright (c) 2013 Cornell University
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 * Authors: Andreas Hansson
39 * Christopher Torng
40 * Akash Bagdia
41 * David Guillen Fandos
42 */
43
44/**
45 * @file
46 * ClockedObject declaration and implementation.
47 */
48
49#ifndef __SIM_CLOCKED_OBJECT_HH__
50#define __SIM_CLOCKED_OBJECT_HH__
51
52#include "base/callback.hh"
53#include "base/intmath.hh"
54#include "base/misc.hh"
55#include "enums/PwrState.hh"
56#include "params/ClockedObject.hh"
57#include "sim/core.hh"
58#include "sim/clock_domain.hh"
59#include "sim/sim_object.hh"
60
61/**
62 * Helper class for objects that need to be clocked. Clocked objects
63 * typically inherit from this class. Objects that need SimObject
64 * functionality as well should inherit from ClockedObject.
65 */
66class Clocked
67{
68
69 private:
70 // the tick value of the next clock edge (>= curTick()) at the
71 // time of the last call to update()
72 mutable Tick tick;
73
74 // The cycle counter value corresponding to the current value of
75 // 'tick'
76 mutable Cycles cycle;
77
78 /**
79 * Align cycle and tick to the next clock edge if not already done. When
80 * complete, tick must be at least curTick().
81 */
82 void update() const
83 {
84 // both tick and cycle are up-to-date and we are done, note
85 // that the >= is important as it captures cases where tick
86 // has already passed curTick()
87 if (tick >= curTick())
88 return;
89
90 // optimise for the common case and see if the tick should be
91 // advanced by a single clock period
92 tick += clockPeriod();
93 ++cycle;
94
95 // see if we are done at this point
96 if (tick >= curTick())
97 return;
98
99 // if not, we have to recalculate the cycle and tick, we
100 // perform the calculations in terms of relative cycles to
101 // allow changes to the clock period in the future
102 Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod()));
103 cycle += elapsedCycles;
104 tick += elapsedCycles * clockPeriod();
105 }
106
107 /**
108 * The clock domain this clocked object belongs to
109 */
110 ClockDomain &clockDomain;
111
112 protected:
113
114 /**
115 * Create a clocked object and set the clock domain based on the
116 * parameters.
117 */
118 Clocked(ClockDomain &clk_domain)
119 : tick(0), cycle(0), clockDomain(clk_domain)
120 {
121 // Register with the clock domain, so that if the clock domain
122 // frequency changes, we can update this object's tick.
123 clockDomain.registerWithClockDomain(this);
124 }
125
126 Clocked(Clocked &) = delete;
127 Clocked &operator=(Clocked &) = delete;
128
129 /**
130 * Virtual destructor due to inheritance.
131 */
132 virtual ~Clocked() { }
133
134 /**
135 * Reset the object's clock using the current global tick value. Likely
136 * to be used only when the global clock is reset. Currently, this done
137 * only when Ruby is done warming up the memory system.
138 */
139 void resetClock() const
140 {
141 Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
142 cycle = elapsedCycles;
143 tick = elapsedCycles * clockPeriod();
144 }
145
146 public:
147
148 /**
149 * Update the tick to the current tick.
150 *
151 */
152 inline void updateClockPeriod() const
153 {
154 update();
155 }
156
157 /**
158 * Determine the tick when a cycle begins, by default the current one, but
159 * the argument also enables the caller to determine a future cycle. When
160 * curTick() is on a clock edge, the number of cycles in the parameter is
161 * added to curTick() to be returned. When curTick() is not aligned to a
162 * clock edge, the number of cycles in the parameter is added to the next
163 * clock edge.
164 *
165 * @param cycles The number of cycles into the future
166 *
167 * @return The start tick when the requested clock edge occurs. Precisely,
168 * this tick can be
169 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles
170 */
171 inline Tick clockEdge(Cycles cycles = Cycles(0)) const
172 {
173 // align tick to the next clock edge
174 update();
175
176 // figure out when this future cycle is
177 return tick + clockPeriod() * cycles;
178 }
179
180 /**
181 * Determine the current cycle, corresponding to a tick aligned to
182 * a clock edge.
183 *
184 * @return When curTick() is on a clock edge, return the Cycle corresponding
185 * to that clock edge. When curTick() is not on a clock edge, return the
186 * Cycle corresponding to the next clock edge.
187 */
188 inline Cycles curCycle() const
189 {
190 // align cycle to the next clock edge.
191 update();
192
193 return cycle;
194 }
195
196 /**
197 * Based on the clock of the object, determine the start tick of the first
198 * cycle that is at least one cycle in the future. When curTick() is at the
199 * current cycle edge, this returns the next clock edge. When calling this
200 * during the middle of a cycle, this returns 2 clock edges in the future.
201 *
202 * @return The start tick of the first cycle that is at least one cycle in
203 * the future. Precisely, the returned tick can be in the range
204 * curTick() + [clockPeriod(), 2 * clockPeriod())
205 */
206 Tick nextCycle() const
207 { return clockEdge(Cycles(1)); }
208
209 inline uint64_t frequency() const
210 {
211 return SimClock::Frequency / clockPeriod();
212 }
213
214 inline Tick clockPeriod() const
215 {
216 return clockDomain.clockPeriod();
217 }
218
219 inline double voltage() const
220 {
221 return clockDomain.voltage();
222 }
223
224 inline Cycles ticksToCycles(Tick t) const
225 { return Cycles(divCeil(t, clockPeriod())); }
226
227 inline Tick cyclesToTicks(Cycles c) const
228 { return clockPeriod() * c; }
229};
230
231/**
232 * The ClockedObject class extends the SimObject with a clock and
233 * accessor functions to relate ticks to the cycles of the object.
234 */
235class ClockedObject
236 : public SimObject, public Clocked
237{
238 public:
239 ClockedObject(const ClockedObjectParams *p);
240
241 /** Parameters of ClockedObject */
242 typedef ClockedObjectParams Params;
243 const Params* params() const
244 { return reinterpret_cast<const Params*>(_params); }
245
246 void serialize(CheckpointOut &cp) const override;
247 void unserialize(CheckpointIn &cp) override;
248
249 inline Enums::PwrState pwrState() const
250 { return _currPwrState; }
251
252 inline std::string pwrStateName() const
253 { return Enums::PwrStateStrings[_currPwrState]; }
254
255 /** Returns the percentage residency for each power state */
256 std::vector<double> pwrStateWeights() const;
257
258 /**
259 * Record stats values like state residency by computing the time
260 * difference from previous update. Also, updates the previous evaluation
261 * tick once all stats are recorded.
262 * Usually called on power state change and stats dump callback.
263 */
264 void computeStats();
265
266 void pwrState(Enums::PwrState);
267 void regStats() override;
268
269 protected:
270
271 /** To keep track of the current power state */
272 Enums::PwrState _currPwrState;
273
274 Tick prvEvalTick;
275
276 Stats::Scalar numPwrStateTransitions;
277 Stats::Distribution pwrStateClkGateDist;
278 Stats::Vector pwrStateResidencyTicks;
279
280};
281
282class ClockedObjectDumpCallback : public Callback
283{
284 ClockedObject *co;
285 public:
286 ClockedObjectDumpCallback(ClockedObject *co_t) : co(co_t) {}
287 virtual void process() { co->computeStats(); };
288};
289
290#endif //__SIM_CLOCKED_OBJECT_HH__
54#include "enums/PwrState.hh"
55#include "params/ClockedObject.hh"
56#include "sim/core.hh"
57#include "sim/clock_domain.hh"
58#include "sim/sim_object.hh"
59
60/**
61 * Helper class for objects that need to be clocked. Clocked objects
62 * typically inherit from this class. Objects that need SimObject
63 * functionality as well should inherit from ClockedObject.
64 */
65class Clocked
66{
67
68 private:
69 // the tick value of the next clock edge (>= curTick()) at the
70 // time of the last call to update()
71 mutable Tick tick;
72
73 // The cycle counter value corresponding to the current value of
74 // 'tick'
75 mutable Cycles cycle;
76
77 /**
78 * Align cycle and tick to the next clock edge if not already done. When
79 * complete, tick must be at least curTick().
80 */
81 void update() const
82 {
83 // both tick and cycle are up-to-date and we are done, note
84 // that the >= is important as it captures cases where tick
85 // has already passed curTick()
86 if (tick >= curTick())
87 return;
88
89 // optimise for the common case and see if the tick should be
90 // advanced by a single clock period
91 tick += clockPeriod();
92 ++cycle;
93
94 // see if we are done at this point
95 if (tick >= curTick())
96 return;
97
98 // if not, we have to recalculate the cycle and tick, we
99 // perform the calculations in terms of relative cycles to
100 // allow changes to the clock period in the future
101 Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod()));
102 cycle += elapsedCycles;
103 tick += elapsedCycles * clockPeriod();
104 }
105
106 /**
107 * The clock domain this clocked object belongs to
108 */
109 ClockDomain &clockDomain;
110
111 protected:
112
113 /**
114 * Create a clocked object and set the clock domain based on the
115 * parameters.
116 */
117 Clocked(ClockDomain &clk_domain)
118 : tick(0), cycle(0), clockDomain(clk_domain)
119 {
120 // Register with the clock domain, so that if the clock domain
121 // frequency changes, we can update this object's tick.
122 clockDomain.registerWithClockDomain(this);
123 }
124
125 Clocked(Clocked &) = delete;
126 Clocked &operator=(Clocked &) = delete;
127
128 /**
129 * Virtual destructor due to inheritance.
130 */
131 virtual ~Clocked() { }
132
133 /**
134 * Reset the object's clock using the current global tick value. Likely
135 * to be used only when the global clock is reset. Currently, this done
136 * only when Ruby is done warming up the memory system.
137 */
138 void resetClock() const
139 {
140 Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
141 cycle = elapsedCycles;
142 tick = elapsedCycles * clockPeriod();
143 }
144
145 public:
146
147 /**
148 * Update the tick to the current tick.
149 *
150 */
151 inline void updateClockPeriod() const
152 {
153 update();
154 }
155
156 /**
157 * Determine the tick when a cycle begins, by default the current one, but
158 * the argument also enables the caller to determine a future cycle. When
159 * curTick() is on a clock edge, the number of cycles in the parameter is
160 * added to curTick() to be returned. When curTick() is not aligned to a
161 * clock edge, the number of cycles in the parameter is added to the next
162 * clock edge.
163 *
164 * @param cycles The number of cycles into the future
165 *
166 * @return The start tick when the requested clock edge occurs. Precisely,
167 * this tick can be
168 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles
169 */
170 inline Tick clockEdge(Cycles cycles = Cycles(0)) const
171 {
172 // align tick to the next clock edge
173 update();
174
175 // figure out when this future cycle is
176 return tick + clockPeriod() * cycles;
177 }
178
179 /**
180 * Determine the current cycle, corresponding to a tick aligned to
181 * a clock edge.
182 *
183 * @return When curTick() is on a clock edge, return the Cycle corresponding
184 * to that clock edge. When curTick() is not on a clock edge, return the
185 * Cycle corresponding to the next clock edge.
186 */
187 inline Cycles curCycle() const
188 {
189 // align cycle to the next clock edge.
190 update();
191
192 return cycle;
193 }
194
195 /**
196 * Based on the clock of the object, determine the start tick of the first
197 * cycle that is at least one cycle in the future. When curTick() is at the
198 * current cycle edge, this returns the next clock edge. When calling this
199 * during the middle of a cycle, this returns 2 clock edges in the future.
200 *
201 * @return The start tick of the first cycle that is at least one cycle in
202 * the future. Precisely, the returned tick can be in the range
203 * curTick() + [clockPeriod(), 2 * clockPeriod())
204 */
205 Tick nextCycle() const
206 { return clockEdge(Cycles(1)); }
207
208 inline uint64_t frequency() const
209 {
210 return SimClock::Frequency / clockPeriod();
211 }
212
213 inline Tick clockPeriod() const
214 {
215 return clockDomain.clockPeriod();
216 }
217
218 inline double voltage() const
219 {
220 return clockDomain.voltage();
221 }
222
223 inline Cycles ticksToCycles(Tick t) const
224 { return Cycles(divCeil(t, clockPeriod())); }
225
226 inline Tick cyclesToTicks(Cycles c) const
227 { return clockPeriod() * c; }
228};
229
230/**
231 * The ClockedObject class extends the SimObject with a clock and
232 * accessor functions to relate ticks to the cycles of the object.
233 */
234class ClockedObject
235 : public SimObject, public Clocked
236{
237 public:
238 ClockedObject(const ClockedObjectParams *p);
239
240 /** Parameters of ClockedObject */
241 typedef ClockedObjectParams Params;
242 const Params* params() const
243 { return reinterpret_cast<const Params*>(_params); }
244
245 void serialize(CheckpointOut &cp) const override;
246 void unserialize(CheckpointIn &cp) override;
247
248 inline Enums::PwrState pwrState() const
249 { return _currPwrState; }
250
251 inline std::string pwrStateName() const
252 { return Enums::PwrStateStrings[_currPwrState]; }
253
254 /** Returns the percentage residency for each power state */
255 std::vector<double> pwrStateWeights() const;
256
257 /**
258 * Record stats values like state residency by computing the time
259 * difference from previous update. Also, updates the previous evaluation
260 * tick once all stats are recorded.
261 * Usually called on power state change and stats dump callback.
262 */
263 void computeStats();
264
265 void pwrState(Enums::PwrState);
266 void regStats() override;
267
268 protected:
269
270 /** To keep track of the current power state */
271 Enums::PwrState _currPwrState;
272
273 Tick prvEvalTick;
274
275 Stats::Scalar numPwrStateTransitions;
276 Stats::Distribution pwrStateClkGateDist;
277 Stats::Vector pwrStateResidencyTicks;
278
279};
280
281class ClockedObjectDumpCallback : public Callback
282{
283 ClockedObject *co;
284 public:
285 ClockedObjectDumpCallback(ClockedObject *co_t) : co(co_t) {}
286 virtual void process() { co->computeStats(); };
287};
288
289#endif //__SIM_CLOCKED_OBJECT_HH__