sim: Delete authors lists from files in sim.
[gem5.git] / src / sim / clocked_object.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
39 /**
40 * @file
41 * ClockedObject declaration and implementation.
42 */
43
44 #ifndef __SIM_CLOCKED_OBJECT_HH__
45 #define __SIM_CLOCKED_OBJECT_HH__
46
47 #include "base/callback.hh"
48 #include "base/intmath.hh"
49 #include "enums/PwrState.hh"
50 #include "params/ClockedObject.hh"
51 #include "sim/core.hh"
52 #include "sim/clock_domain.hh"
53 #include "sim/sim_object.hh"
54
55 /**
56 * Helper class for objects that need to be clocked. Clocked objects
57 * typically inherit from this class. Objects that need SimObject
58 * functionality as well should inherit from ClockedObject.
59 */
60 class Clocked
61 {
62
63 private:
64 // the tick value of the next clock edge (>= curTick()) at the
65 // time of the last call to update()
66 mutable Tick tick;
67
68 // The cycle counter value corresponding to the current value of
69 // 'tick'
70 mutable Cycles cycle;
71
72 /**
73 * Align cycle and tick to the next clock edge if not already done. When
74 * complete, tick must be at least curTick().
75 */
76 void
77 update() const
78 {
79 // both tick and cycle are up-to-date and we are done, note
80 // that the >= is important as it captures cases where tick
81 // has already passed curTick()
82 if (tick >= curTick())
83 return;
84
85 // optimise for the common case and see if the tick should be
86 // advanced by a single clock period
87 tick += clockPeriod();
88 ++cycle;
89
90 // see if we are done at this point
91 if (tick >= curTick())
92 return;
93
94 // if not, we have to recalculate the cycle and tick, we
95 // perform the calculations in terms of relative cycles to
96 // allow changes to the clock period in the future
97 Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod()));
98 cycle += elapsedCycles;
99 tick += elapsedCycles * clockPeriod();
100 }
101
102 /**
103 * The clock domain this clocked object belongs to
104 */
105 ClockDomain &clockDomain;
106
107 protected:
108
109 /**
110 * Create a clocked object and set the clock domain based on the
111 * parameters.
112 */
113 Clocked(ClockDomain &clk_domain)
114 : tick(0), cycle(0), clockDomain(clk_domain)
115 {
116 // Register with the clock domain, so that if the clock domain
117 // frequency changes, we can update this object's tick.
118 clockDomain.registerWithClockDomain(this);
119 }
120
121 Clocked(Clocked &) = delete;
122 Clocked &operator=(Clocked &) = delete;
123
124 /**
125 * Virtual destructor due to inheritance.
126 */
127 virtual ~Clocked() { }
128
129 /**
130 * Reset the object's clock using the current global tick value. Likely
131 * to be used only when the global clock is reset. Currently, this done
132 * only when Ruby is done warming up the memory system.
133 */
134 void
135 resetClock() const
136 {
137 Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
138 cycle = elapsedCycles;
139 tick = elapsedCycles * clockPeriod();
140 }
141
142 /**
143 * A hook subclasses can implement so they can do any extra work that's
144 * needed when the clock rate is changed.
145 */
146 virtual void clockPeriodUpdated() {}
147
148 public:
149
150 /**
151 * Update the tick to the current tick.
152 */
153 void
154 updateClockPeriod()
155 {
156 update();
157 clockPeriodUpdated();
158 }
159
160 /**
161 * Determine the tick when a cycle begins, by default the current one, but
162 * the argument also enables the caller to determine a future cycle. When
163 * curTick() is on a clock edge, the number of cycles in the parameter is
164 * added to curTick() to be returned. When curTick() is not aligned to a
165 * clock edge, the number of cycles in the parameter is added to the next
166 * clock edge.
167 *
168 * @param cycles The number of cycles into the future
169 *
170 * @return The start tick when the requested clock edge occurs. Precisely,
171 * this tick can be
172 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles
173 */
174 Tick
175 clockEdge(Cycles cycles=Cycles(0)) const
176 {
177 // align tick to the next clock edge
178 update();
179
180 // figure out when this future cycle is
181 return tick + clockPeriod() * cycles;
182 }
183
184 /**
185 * Determine the current cycle, corresponding to a tick aligned to
186 * a clock edge.
187 *
188 * @return When curTick() is on a clock edge, return the Cycle corresponding
189 * to that clock edge. When curTick() is not on a clock edge, return the
190 * Cycle corresponding to the next clock edge.
191 */
192 Cycles
193 curCycle() const
194 {
195 // align cycle to the next clock edge.
196 update();
197
198 return cycle;
199 }
200
201 /**
202 * Based on the clock of the object, determine the start tick of the first
203 * cycle that is at least one cycle in the future. When curTick() is at the
204 * current cycle edge, this returns the next clock edge. When calling this
205 * during the middle of a cycle, this returns 2 clock edges in the future.
206 *
207 * @return The start tick of the first cycle that is at least one cycle in
208 * the future. Precisely, the returned tick can be in the range
209 * curTick() + [clockPeriod(), 2 * clockPeriod())
210 */
211 Tick nextCycle() const { return clockEdge(Cycles(1)); }
212
213 uint64_t frequency() const { return SimClock::Frequency / clockPeriod(); }
214
215 Tick clockPeriod() const { return clockDomain.clockPeriod(); }
216
217 double voltage() const { return clockDomain.voltage(); }
218
219 Cycles
220 ticksToCycles(Tick t) const
221 {
222 return Cycles(divCeil(t, clockPeriod()));
223 }
224
225 Tick cyclesToTicks(Cycles c) const { return clockPeriod() * c; }
226 };
227
228 /**
229 * The ClockedObject class extends the SimObject with a clock and
230 * accessor functions to relate ticks to the cycles of the object.
231 */
232 class ClockedObject : public SimObject, public Clocked
233 {
234 public:
235 ClockedObject(const ClockedObjectParams *p);
236
237 /** Parameters of ClockedObject */
238 typedef ClockedObjectParams Params;
239 const Params *
240 params() const
241 {
242 return reinterpret_cast<const Params*>(_params);
243 }
244
245 void serialize(CheckpointOut &cp) const override;
246 void unserialize(CheckpointIn &cp) override;
247
248 Enums::PwrState pwrState() const { return _currPwrState; }
249
250 std::string
251 pwrStateName() const
252 {
253 return Enums::PwrStateStrings[_currPwrState];
254 }
255
256 /** Returns the percentage residency for each power state */
257 std::vector<double> pwrStateWeights() const;
258
259 /**
260 * Record stats values like state residency by computing the time
261 * difference from previous update. Also, updates the previous evaluation
262 * tick once all stats are recorded.
263 * Usually called on power state change and stats dump callback.
264 */
265 void computeStats();
266
267 void pwrState(Enums::PwrState);
268
269 protected:
270
271 /** To keep track of the current power state */
272 Enums::PwrState _currPwrState;
273
274 Tick prvEvalTick;
275
276 struct ClockedObjectStats : public Stats::Group
277 {
278 ClockedObjectStats(ClockedObject &co);
279
280 void regStats() override;
281 void preDumpStats() override;
282
283 ClockedObject &clockedObject;
284 Stats::Scalar numPwrStateTransitions;
285 Stats::Distribution pwrStateClkGateDist;
286 Stats::Vector pwrStateResidencyTicks;
287 } stats;
288 };
289
290 #endif //__SIM_CLOCKED_OBJECT_HH__