2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * Copyright (c) 2013 Cornell University
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.
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.
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.
38 * Authors: Andreas Hansson
41 * David Guillen Fandos
46 * ClockedObject declaration and implementation.
49 #ifndef __SIM_CLOCKED_OBJECT_HH__
50 #define __SIM_CLOCKED_OBJECT_HH__
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"
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.
70 // the tick value of the next clock edge (>= curTick()) at the
71 // time of the last call to update()
74 // The cycle counter value corresponding to the current value of
79 * Align cycle and tick to the next clock edge if not already done. When
80 * complete, tick must be at least curTick().
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())
90 // optimise for the common case and see if the tick should be
91 // advanced by a single clock period
92 tick += clockPeriod();
95 // see if we are done at this point
96 if (tick >= curTick())
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();
108 * The clock domain this clocked object belongs to
110 ClockDomain &clockDomain;
115 * Create a clocked object and set the clock domain based on the
118 Clocked(ClockDomain &clk_domain)
119 : tick(0), cycle(0), clockDomain(clk_domain)
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);
126 Clocked(Clocked &) = delete;
127 Clocked &operator=(Clocked &) = delete;
130 * Virtual destructor due to inheritance.
132 virtual ~Clocked() { }
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.
139 void resetClock() const
141 Cycles elapsedCycles(divCeil(curTick(), clockPeriod()));
142 cycle = elapsedCycles;
143 tick = elapsedCycles * clockPeriod();
149 * Update the tick to the current tick.
152 inline void updateClockPeriod() const
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
165 * @param cycles The number of cycles into the future
167 * @return The start tick when the requested clock edge occurs. Precisely,
169 * curTick() + [0, clockPeriod()) + clockPeriod() * cycles
171 inline Tick clockEdge(Cycles cycles = Cycles(0)) const
173 // align tick to the next clock edge
176 // figure out when this future cycle is
177 return tick + clockPeriod() * cycles;
181 * Determine the current cycle, corresponding to a tick aligned to
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.
188 inline Cycles curCycle() const
190 // align cycle to the next clock edge.
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.
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())
206 Tick nextCycle() const
207 { return clockEdge(Cycles(1)); }
209 inline uint64_t frequency() const
211 return SimClock::Frequency / clockPeriod();
214 inline Tick clockPeriod() const
216 return clockDomain.clockPeriod();
219 inline double voltage() const
221 return clockDomain.voltage();
224 inline Cycles ticksToCycles(Tick t) const
225 { return Cycles(divCeil(t, clockPeriod())); }
227 inline Tick cyclesToTicks(Cycles c) const
228 { return clockPeriod() * c; }
232 * The ClockedObject class extends the SimObject with a clock and
233 * accessor functions to relate ticks to the cycles of the object.
236 : public SimObject, public Clocked
239 ClockedObject(const ClockedObjectParams *p)
240 : SimObject(p), Clocked(*p->clk_domain),
241 _currPwrState(p->default_p_state),
245 /** Parameters of ClockedObject */
246 typedef ClockedObjectParams Params;
247 const Params* params() const
248 { return reinterpret_cast<const Params*>(_params); }
250 void serialize(CheckpointOut &cp) const override;
251 void unserialize(CheckpointIn &cp) override;
253 inline Enums::PwrState pwrState() const
254 { return _currPwrState; }
256 inline std::string pwrStateName() const
257 { return Enums::PwrStateStrings[_currPwrState]; }
259 /** Returns the percentage residency for each power state */
260 std::vector<double> pwrStateWeights() const;
263 * Record stats values like state residency by computing the time
264 * difference from previous update. Also, updates the previous
265 * evaluation tick once all stats are recorded.
266 * Usually called on power state change and stats dump callback.
270 void pwrState(Enums::PwrState);
275 /** To keep track of the current power state */
276 Enums::PwrState _currPwrState;
280 Stats::Scalar numPwrStateTransitions;
281 Stats::Distribution pwrStateClkGateDist;
282 Stats::Vector pwrStateResidencyTicks;
286 class ClockedObjectDumpCallback : public Callback
290 ClockedObjectDumpCallback(ClockedObject *co_t) : co(co_t) {}
291 virtual void process() { co->computeStats(); };
294 #endif //__SIM_CLOCKED_OBJECT_HH__