clocked_object.hh revision 11527
16145Snate@binkert.org/* 26145Snate@binkert.org * Copyright (c) 2012-2013, 2015-2016 ARM Limited 36145Snate@binkert.org * Copyright (c) 2013 Cornell University 46145Snate@binkert.org * All rights reserved 56145Snate@binkert.org * 66145Snate@binkert.org * The license below extends only to copyright in the software and shall 76145Snate@binkert.org * not be construed as granting a license to any other intellectual 86145Snate@binkert.org * property including but not limited to intellectual property relating 96145Snate@binkert.org * to a hardware implementation of the functionality of the software 106145Snate@binkert.org * licensed hereunder. You may use the software subject to the license 116145Snate@binkert.org * terms below provided that you ensure that this notice is replicated 126145Snate@binkert.org * unmodified and in its entirety in all distributions of the software, 136145Snate@binkert.org * modified or unmodified, in source code or in binary form. 146145Snate@binkert.org * 156145Snate@binkert.org * Redistribution and use in source and binary forms, with or without 166145Snate@binkert.org * modification, are permitted provided that the following conditions are 176145Snate@binkert.org * met: redistributions of source code must retain the above copyright 186145Snate@binkert.org * notice, this list of conditions and the following disclaimer; 196145Snate@binkert.org * redistributions in binary form must reproduce the above copyright 206145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 216145Snate@binkert.org * documentation and/or other materials provided with the distribution; 226145Snate@binkert.org * neither the name of the copyright holders nor the names of its 236145Snate@binkert.org * contributors may be used to endorse or promote products derived from 246145Snate@binkert.org * this software without specific prior written permission. 256145Snate@binkert.org * 266145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 276145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 286145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 296154Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3011108Sdavid.hashe@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3111108Sdavid.hashe@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 328091Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3311025Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3411025Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 358091Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3611025Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3711025Snilay@cs.wisc.edu * 3811025Snilay@cs.wisc.edu * Authors: Andreas Hansson 3911025Snilay@cs.wisc.edu * Christopher Torng 4011025Snilay@cs.wisc.edu * Akash Bagdia 4111025Snilay@cs.wisc.edu * David Guillen Fandos 4211025Snilay@cs.wisc.edu */ 4311025Snilay@cs.wisc.edu 4411025Snilay@cs.wisc.edu/** 4511025Snilay@cs.wisc.edu * @file 468091Snilay@cs.wisc.edu * ClockedObject declaration and implementation. 478091Snilay@cs.wisc.edu */ 4811025Snilay@cs.wisc.edu 4911025Snilay@cs.wisc.edu#ifndef __SIM_CLOCKED_OBJECT_HH__ 508091Snilay@cs.wisc.edu#define __SIM_CLOCKED_OBJECT_HH__ 5111025Snilay@cs.wisc.edu 5211025Snilay@cs.wisc.edu#include "base/callback.hh" 5311025Snilay@cs.wisc.edu#include "base/intmath.hh" 5411025Snilay@cs.wisc.edu#include "base/misc.hh" 5511025Snilay@cs.wisc.edu#include "enums/PwrState.hh" 5611025Snilay@cs.wisc.edu#include "params/ClockedObject.hh" 5711025Snilay@cs.wisc.edu#include "sim/core.hh" 5811025Snilay@cs.wisc.edu#include "sim/clock_domain.hh" 5911025Snilay@cs.wisc.edu#include "sim/sim_object.hh" 6011025Snilay@cs.wisc.edu 6111025Snilay@cs.wisc.edu/** 6211025Snilay@cs.wisc.edu * Helper class for objects that need to be clocked. Clocked objects 6311025Snilay@cs.wisc.edu * typically inherit from this class. Objects that need SimObject 6411025Snilay@cs.wisc.edu * functionality as well should inherit from ClockedObject. 6511025Snilay@cs.wisc.edu */ 6611025Snilay@cs.wisc.educlass Clocked 6711025Snilay@cs.wisc.edu{ 6811025Snilay@cs.wisc.edu 6911025Snilay@cs.wisc.edu private: 7011025Snilay@cs.wisc.edu // the tick value of the next clock edge (>= curTick()) at the 718091Snilay@cs.wisc.edu // time of the last call to update() 728091Snilay@cs.wisc.edu mutable Tick tick; 7311025Snilay@cs.wisc.edu 7411025Snilay@cs.wisc.edu // The cycle counter value corresponding to the current value of 758091Snilay@cs.wisc.edu // 'tick' 7611025Snilay@cs.wisc.edu mutable Cycles cycle; 7711025Snilay@cs.wisc.edu 7811025Snilay@cs.wisc.edu /** 7911025Snilay@cs.wisc.edu * Align cycle and tick to the next clock edge if not already done. When 8011025Snilay@cs.wisc.edu * complete, tick must be at least curTick(). 8111025Snilay@cs.wisc.edu */ 8211025Snilay@cs.wisc.edu void update() const 8311025Snilay@cs.wisc.edu { 8411025Snilay@cs.wisc.edu // both tick and cycle are up-to-date and we are done, note 8511025Snilay@cs.wisc.edu // that the >= is important as it captures cases where tick 8611025Snilay@cs.wisc.edu // has already passed curTick() 8711025Snilay@cs.wisc.edu if (tick >= curTick()) 8811025Snilay@cs.wisc.edu return; 8911025Snilay@cs.wisc.edu 9011025Snilay@cs.wisc.edu // optimise for the common case and see if the tick should be 9111025Snilay@cs.wisc.edu // advanced by a single clock period 9211025Snilay@cs.wisc.edu tick += clockPeriod(); 9311025Snilay@cs.wisc.edu ++cycle; 9411025Snilay@cs.wisc.edu 9511025Snilay@cs.wisc.edu // see if we are done at this point 9611025Snilay@cs.wisc.edu if (tick >= curTick()) 9711025Snilay@cs.wisc.edu return; 9811025Snilay@cs.wisc.edu 9911025Snilay@cs.wisc.edu // if not, we have to recalculate the cycle and tick, we 10011025Snilay@cs.wisc.edu // perform the calculations in terms of relative cycles to 10111025Snilay@cs.wisc.edu // allow changes to the clock period in the future 10211025Snilay@cs.wisc.edu Cycles elapsedCycles(divCeil(curTick() - tick, clockPeriod())); 10311025Snilay@cs.wisc.edu cycle += elapsedCycles; 10411025Snilay@cs.wisc.edu tick += elapsedCycles * clockPeriod(); 10511025Snilay@cs.wisc.edu } 10611025Snilay@cs.wisc.edu 10711025Snilay@cs.wisc.edu /** 10811025Snilay@cs.wisc.edu * The clock domain this clocked object belongs to 10911025Snilay@cs.wisc.edu */ 11011025Snilay@cs.wisc.edu ClockDomain &clockDomain; 11111025Snilay@cs.wisc.edu 11211025Snilay@cs.wisc.edu protected: 11311025Snilay@cs.wisc.edu 11411025Snilay@cs.wisc.edu /** 1158091Snilay@cs.wisc.edu * Create a clocked object and set the clock domain based on the 1168091Snilay@cs.wisc.edu * parameters. 1178091Snilay@cs.wisc.edu */ 11811025Snilay@cs.wisc.edu Clocked(ClockDomain &clk_domain) 11911025Snilay@cs.wisc.edu : tick(0), cycle(0), clockDomain(clk_domain) 1208091Snilay@cs.wisc.edu { 12111025Snilay@cs.wisc.edu // Register with the clock domain, so that if the clock domain 12211025Snilay@cs.wisc.edu // frequency changes, we can update this object's tick. 1238091Snilay@cs.wisc.edu clockDomain.registerWithClockDomain(this); 12411118Snilay@cs.wisc.edu } 12511118Snilay@cs.wisc.edu 12611118Snilay@cs.wisc.edu Clocked(Clocked &) = delete; 12711118Snilay@cs.wisc.edu Clocked &operator=(Clocked &) = delete; 12811118Snilay@cs.wisc.edu 12911118Snilay@cs.wisc.edu /** 13011118Snilay@cs.wisc.edu * Virtual destructor due to inheritance. 13111118Snilay@cs.wisc.edu */ 13211118Snilay@cs.wisc.edu virtual ~Clocked() { } 13311118Snilay@cs.wisc.edu 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(); 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__ 291