ticked_object.hh revision 10259
110259SAndrew.Bardsley@arm.com/* 210259SAndrew.Bardsley@arm.com * Copyright (c) 2013-2014 ARM Limited 310259SAndrew.Bardsley@arm.com * All rights reserved 410259SAndrew.Bardsley@arm.com * 510259SAndrew.Bardsley@arm.com * The license below extends only to copyright in the software and shall 610259SAndrew.Bardsley@arm.com * not be construed as granting a license to any other intellectual 710259SAndrew.Bardsley@arm.com * property including but not limited to intellectual property relating 810259SAndrew.Bardsley@arm.com * to a hardware implementation of the functionality of the software 910259SAndrew.Bardsley@arm.com * licensed hereunder. You may use the software subject to the license 1010259SAndrew.Bardsley@arm.com * terms below provided that you ensure that this notice is replicated 1110259SAndrew.Bardsley@arm.com * unmodified and in its entirety in all distributions of the software, 1210259SAndrew.Bardsley@arm.com * modified or unmodified, in source code or in binary form. 1310259SAndrew.Bardsley@arm.com * 1410259SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without 1510259SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are 1610259SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright 1710259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer; 1810259SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright 1910259SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the 2010259SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution; 2110259SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its 2210259SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from 2310259SAndrew.Bardsley@arm.com * this software without specific prior written permission. 2410259SAndrew.Bardsley@arm.com * 2510259SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2610259SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2710259SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2810259SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2910259SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3010259SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3110259SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3210259SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3310259SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3410259SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3510259SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3610259SAndrew.Bardsley@arm.com * 3710259SAndrew.Bardsley@arm.com * Authors: Andrew Bardsley 3810259SAndrew.Bardsley@arm.com */ 3910259SAndrew.Bardsley@arm.com 4010259SAndrew.Bardsley@arm.com/** 4110259SAndrew.Bardsley@arm.com * @file 4210259SAndrew.Bardsley@arm.com * 4310259SAndrew.Bardsley@arm.com * Base classes for ClockedObjects which have evaluate functions to 4410259SAndrew.Bardsley@arm.com * look like clock ticking operations. TickedObject attaches gem5's event 4510259SAndrew.Bardsley@arm.com * queue to Ticked to apply actual scheduling. 4610259SAndrew.Bardsley@arm.com */ 4710259SAndrew.Bardsley@arm.com 4810259SAndrew.Bardsley@arm.com#ifndef __SIM_TICKED_OBJECT_HH__ 4910259SAndrew.Bardsley@arm.com#define __SIM_TICKED_OBJECT_HH__ 5010259SAndrew.Bardsley@arm.com 5110259SAndrew.Bardsley@arm.com#include "params/TickedObject.hh" 5210259SAndrew.Bardsley@arm.com#include "sim/clocked_object.hh" 5310259SAndrew.Bardsley@arm.com 5410259SAndrew.Bardsley@arm.com/** Ticked attaches gem5's event queue/scheduler to evaluate 5510259SAndrew.Bardsley@arm.com * calls and provides a start/stop interface to ticking. 5610259SAndrew.Bardsley@arm.com * 5710259SAndrew.Bardsley@arm.com * Ticked is not a ClockedObject but can be attached to one by 5810259SAndrew.Bardsley@arm.com * inheritance and by calling regStats, serialize/unserialize */ 5910259SAndrew.Bardsley@arm.comclass Ticked 6010259SAndrew.Bardsley@arm.com{ 6110259SAndrew.Bardsley@arm.com protected: 6210259SAndrew.Bardsley@arm.com /** An event to call process periodically */ 6310259SAndrew.Bardsley@arm.com class ClockEvent : public Event 6410259SAndrew.Bardsley@arm.com { 6510259SAndrew.Bardsley@arm.com public: 6610259SAndrew.Bardsley@arm.com Ticked &owner; 6710259SAndrew.Bardsley@arm.com 6810259SAndrew.Bardsley@arm.com ClockEvent(Ticked &owner_, Priority priority) : 6910259SAndrew.Bardsley@arm.com Event(priority), 7010259SAndrew.Bardsley@arm.com owner(owner_) 7110259SAndrew.Bardsley@arm.com { } 7210259SAndrew.Bardsley@arm.com 7310259SAndrew.Bardsley@arm.com /** Evaluate and reschedule */ 7410259SAndrew.Bardsley@arm.com void 7510259SAndrew.Bardsley@arm.com process() 7610259SAndrew.Bardsley@arm.com { 7710259SAndrew.Bardsley@arm.com ++owner.tickCycles; 7810259SAndrew.Bardsley@arm.com ++owner.numCycles; 7910259SAndrew.Bardsley@arm.com owner.evaluate(); 8010259SAndrew.Bardsley@arm.com if (owner.running) { 8110259SAndrew.Bardsley@arm.com owner.object.schedule(this, 8210259SAndrew.Bardsley@arm.com owner.object.clockEdge(Cycles(1))); 8310259SAndrew.Bardsley@arm.com } 8410259SAndrew.Bardsley@arm.com } 8510259SAndrew.Bardsley@arm.com }; 8610259SAndrew.Bardsley@arm.com 8710259SAndrew.Bardsley@arm.com friend class ClockEvent; 8810259SAndrew.Bardsley@arm.com 8910259SAndrew.Bardsley@arm.com /** ClockedObject who is responsible for this Ticked's actions/stats */ 9010259SAndrew.Bardsley@arm.com ClockedObject &object; 9110259SAndrew.Bardsley@arm.com 9210259SAndrew.Bardsley@arm.com /** The single instance of ClockEvent used */ 9310259SAndrew.Bardsley@arm.com ClockEvent event; 9410259SAndrew.Bardsley@arm.com 9510259SAndrew.Bardsley@arm.com /** Have I been started? and am not stopped */ 9610259SAndrew.Bardsley@arm.com bool running; 9710259SAndrew.Bardsley@arm.com 9810259SAndrew.Bardsley@arm.com /** Time of last stop event to calculate run time */ 9910259SAndrew.Bardsley@arm.com Cycles lastStopped; 10010259SAndrew.Bardsley@arm.com 10110259SAndrew.Bardsley@arm.com private: 10210259SAndrew.Bardsley@arm.com /** Locally allocated stats */ 10310259SAndrew.Bardsley@arm.com Stats::Scalar *numCyclesLocal; 10410259SAndrew.Bardsley@arm.com 10510259SAndrew.Bardsley@arm.com protected: 10610259SAndrew.Bardsley@arm.com /** Total number of cycles either ticked or spend stopped */ 10710259SAndrew.Bardsley@arm.com Stats::Scalar &numCycles; 10810259SAndrew.Bardsley@arm.com 10910259SAndrew.Bardsley@arm.com /** Number of cycles ticked */ 11010259SAndrew.Bardsley@arm.com Stats::Scalar tickCycles; 11110259SAndrew.Bardsley@arm.com 11210259SAndrew.Bardsley@arm.com /** Number of cycles stopped */ 11310259SAndrew.Bardsley@arm.com Stats::Formula idleCycles; 11410259SAndrew.Bardsley@arm.com 11510259SAndrew.Bardsley@arm.com public: 11610259SAndrew.Bardsley@arm.com Ticked(ClockedObject &object_, 11710259SAndrew.Bardsley@arm.com Stats::Scalar *imported_num_cycles = NULL, 11810259SAndrew.Bardsley@arm.com Event::Priority priority = Event::CPU_Tick_Pri); 11910259SAndrew.Bardsley@arm.com 12010259SAndrew.Bardsley@arm.com virtual ~Ticked() { } 12110259SAndrew.Bardsley@arm.com 12210259SAndrew.Bardsley@arm.com /** Register {num,ticks}Cycles if necessary. If numCycles is 12310259SAndrew.Bardsley@arm.com * imported, be sure to register it *before* calling this regStats */ 12410259SAndrew.Bardsley@arm.com void regStats(); 12510259SAndrew.Bardsley@arm.com 12610259SAndrew.Bardsley@arm.com /** Start ticking */ 12710259SAndrew.Bardsley@arm.com void 12810259SAndrew.Bardsley@arm.com start() 12910259SAndrew.Bardsley@arm.com { 13010259SAndrew.Bardsley@arm.com if (!running) { 13110259SAndrew.Bardsley@arm.com if (!event.scheduled()) 13210259SAndrew.Bardsley@arm.com object.schedule(event, object.clockEdge(Cycles(1))); 13310259SAndrew.Bardsley@arm.com running = true; 13410259SAndrew.Bardsley@arm.com numCycles += cyclesSinceLastStopped(); 13510259SAndrew.Bardsley@arm.com } 13610259SAndrew.Bardsley@arm.com } 13710259SAndrew.Bardsley@arm.com 13810259SAndrew.Bardsley@arm.com /** How long have we been stopped for? */ 13910259SAndrew.Bardsley@arm.com Cycles 14010259SAndrew.Bardsley@arm.com cyclesSinceLastStopped() const 14110259SAndrew.Bardsley@arm.com { 14210259SAndrew.Bardsley@arm.com return object.curCycle() - lastStopped; 14310259SAndrew.Bardsley@arm.com } 14410259SAndrew.Bardsley@arm.com 14510259SAndrew.Bardsley@arm.com /** Reset stopped time to current time */ 14610259SAndrew.Bardsley@arm.com void 14710259SAndrew.Bardsley@arm.com resetLastStopped() 14810259SAndrew.Bardsley@arm.com { 14910259SAndrew.Bardsley@arm.com lastStopped = object.curCycle(); 15010259SAndrew.Bardsley@arm.com } 15110259SAndrew.Bardsley@arm.com 15210259SAndrew.Bardsley@arm.com /** Cancel the next tick event and issue no more */ 15310259SAndrew.Bardsley@arm.com void 15410259SAndrew.Bardsley@arm.com stop() 15510259SAndrew.Bardsley@arm.com { 15610259SAndrew.Bardsley@arm.com if (running) { 15710259SAndrew.Bardsley@arm.com if (event.scheduled()) 15810259SAndrew.Bardsley@arm.com object.deschedule(event); 15910259SAndrew.Bardsley@arm.com running = false; 16010259SAndrew.Bardsley@arm.com resetLastStopped(); 16110259SAndrew.Bardsley@arm.com } 16210259SAndrew.Bardsley@arm.com } 16310259SAndrew.Bardsley@arm.com 16410259SAndrew.Bardsley@arm.com /** Checkpoint lastStopped */ 16510259SAndrew.Bardsley@arm.com void serialize(std::ostream &os); 16610259SAndrew.Bardsley@arm.com void unserialize(Checkpoint *cp, const std::string §ion); 16710259SAndrew.Bardsley@arm.com 16810259SAndrew.Bardsley@arm.com /** Action to call on the clock tick */ 16910259SAndrew.Bardsley@arm.com virtual void evaluate() = 0; 17010259SAndrew.Bardsley@arm.com}; 17110259SAndrew.Bardsley@arm.com 17210259SAndrew.Bardsley@arm.com/** TickedObject attaches Ticked to ClockedObject and can be used as 17310259SAndrew.Bardsley@arm.com * a base class where ticked operation */ 17410259SAndrew.Bardsley@arm.comclass TickedObject : public ClockedObject, public Ticked 17510259SAndrew.Bardsley@arm.com{ 17610259SAndrew.Bardsley@arm.com public: 17710259SAndrew.Bardsley@arm.com TickedObject(TickedObjectParams *params, 17810259SAndrew.Bardsley@arm.com Event::Priority priority = Event::CPU_Tick_Pri); 17910259SAndrew.Bardsley@arm.com 18010259SAndrew.Bardsley@arm.com /** Disambiguate to make these functions overload correctly */ 18110259SAndrew.Bardsley@arm.com using ClockedObject::regStats; 18210259SAndrew.Bardsley@arm.com using ClockedObject::serialize; 18310259SAndrew.Bardsley@arm.com using ClockedObject::unserialize; 18410259SAndrew.Bardsley@arm.com 18510259SAndrew.Bardsley@arm.com /** Pass on regStats, serialize etc. onto Ticked */ 18610259SAndrew.Bardsley@arm.com void regStats(); 18710259SAndrew.Bardsley@arm.com void serialize(std::ostream &os); 18810259SAndrew.Bardsley@arm.com void unserialize(Checkpoint *cp, const std::string §ion); 18910259SAndrew.Bardsley@arm.com}; 19010259SAndrew.Bardsley@arm.com 19110259SAndrew.Bardsley@arm.com#endif /* __SIM_TICKED_OBJECT_HH__ */ 192