ticked_object.hh revision 11168:f98eb2da15a4
13804Ssaidi@eecs.umich.edu/*
23804Ssaidi@eecs.umich.edu * Copyright (c) 2013-2014 ARM Limited
33804Ssaidi@eecs.umich.edu * All rights reserved
43804Ssaidi@eecs.umich.edu *
53804Ssaidi@eecs.umich.edu * The license below extends only to copyright in the software and shall
63804Ssaidi@eecs.umich.edu * not be construed as granting a license to any other intellectual
73804Ssaidi@eecs.umich.edu * property including but not limited to intellectual property relating
83804Ssaidi@eecs.umich.edu * to a hardware implementation of the functionality of the software
93804Ssaidi@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
103804Ssaidi@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
113804Ssaidi@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
123804Ssaidi@eecs.umich.edu * modified or unmodified, in source code or in binary form.
133804Ssaidi@eecs.umich.edu *
143804Ssaidi@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
153804Ssaidi@eecs.umich.edu * modification, are permitted provided that the following conditions are
163804Ssaidi@eecs.umich.edu * met: redistributions of source code must retain the above copyright
173804Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
183804Ssaidi@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
193804Ssaidi@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
203804Ssaidi@eecs.umich.edu * documentation and/or other materials provided with the distribution;
213804Ssaidi@eecs.umich.edu * neither the name of the copyright holders nor the names of its
223804Ssaidi@eecs.umich.edu * contributors may be used to endorse or promote products derived from
233804Ssaidi@eecs.umich.edu * this software without specific prior written permission.
243804Ssaidi@eecs.umich.edu *
253804Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
263804Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
273804Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
283804Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
293804Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
303804Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
313804Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3211793Sbrandon.potter@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333804Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343804Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
353804Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363804Ssaidi@eecs.umich.edu *
377741Sgblack@eecs.umich.edu * Authors: Andrew Bardsley
383804Ssaidi@eecs.umich.edu */
3910905Sandreas.sandberg@arm.com
403804Ssaidi@eecs.umich.edu/**
413804Ssaidi@eecs.umich.edu * @file
423804Ssaidi@eecs.umich.edu *
433804Ssaidi@eecs.umich.edu *  Base classes for ClockedObjects which have evaluate functions to
443804Ssaidi@eecs.umich.edu *  look like clock ticking operations.  TickedObject attaches gem5's event
453804Ssaidi@eecs.umich.edu *  queue to Ticked to apply actual scheduling.
464000Ssaidi@eecs.umich.edu */
474000Ssaidi@eecs.umich.edu
484000Ssaidi@eecs.umich.edu#ifndef __SIM_TICKED_OBJECT_HH__
493804Ssaidi@eecs.umich.edu#define __SIM_TICKED_OBJECT_HH__
503804Ssaidi@eecs.umich.edu
514000Ssaidi@eecs.umich.edu#include "params/TickedObject.hh"
523804Ssaidi@eecs.umich.edu#include "sim/clocked_object.hh"
533804Ssaidi@eecs.umich.edu
543804Ssaidi@eecs.umich.edu/** Ticked attaches gem5's event queue/scheduler to evaluate
553804Ssaidi@eecs.umich.edu *  calls and provides a start/stop interface to ticking.
5610905Sandreas.sandberg@arm.com *
573804Ssaidi@eecs.umich.edu *  Ticked is not a ClockedObject but can be attached to one by
583804Ssaidi@eecs.umich.edu *  inheritance and by calling regStats, serialize/unserialize */
593804Ssaidi@eecs.umich.educlass Ticked : public Serializable
603804Ssaidi@eecs.umich.edu{
613804Ssaidi@eecs.umich.edu  protected:
623804Ssaidi@eecs.umich.edu    /** An event to call process periodically */
633804Ssaidi@eecs.umich.edu    class ClockEvent : public Event
643804Ssaidi@eecs.umich.edu    {
654000Ssaidi@eecs.umich.edu      public:
664000Ssaidi@eecs.umich.edu        Ticked &owner;
673804Ssaidi@eecs.umich.edu
684000Ssaidi@eecs.umich.edu        ClockEvent(Ticked &owner_, Priority priority) :
693804Ssaidi@eecs.umich.edu            Event(priority),
703804Ssaidi@eecs.umich.edu            owner(owner_)
713804Ssaidi@eecs.umich.edu        { }
727741Sgblack@eecs.umich.edu
737741Sgblack@eecs.umich.edu        /** Evaluate and reschedule */
743804Ssaidi@eecs.umich.edu        void
753804Ssaidi@eecs.umich.edu        process()
763804Ssaidi@eecs.umich.edu        {
77            ++owner.tickCycles;
78            ++owner.numCycles;
79            owner.countCycles(Cycles(1));
80            owner.evaluate();
81            if (owner.running) {
82                owner.object.schedule(this,
83                    owner.object.clockEdge(Cycles(1)));
84            }
85        }
86    };
87
88    friend class ClockEvent;
89
90    /** ClockedObject who is responsible for this Ticked's actions/stats */
91    ClockedObject &object;
92
93    /** The single instance of ClockEvent used */
94    ClockEvent event;
95
96    /** Have I been started? and am not stopped */
97    bool running;
98
99    /** Time of last stop event to calculate run time */
100    Cycles lastStopped;
101
102  private:
103    /** Locally allocated stats */
104    Stats::Scalar *numCyclesLocal;
105
106  protected:
107    /** Total number of cycles either ticked or spend stopped */
108    Stats::Scalar &numCycles;
109
110    /** Number of cycles ticked */
111    Stats::Scalar tickCycles;
112
113    /** Number of cycles stopped */
114    Stats::Formula idleCycles;
115
116  public:
117    Ticked(ClockedObject &object_,
118        Stats::Scalar *imported_num_cycles = NULL,
119        Event::Priority priority = Event::CPU_Tick_Pri);
120
121    virtual ~Ticked() { }
122
123    /** Register {num,ticks}Cycles if necessary.  If numCycles is
124     *  imported, be sure to register it *before* calling this regStats */
125    void regStats();
126
127    /** Start ticking */
128    void
129    start()
130    {
131        if (!running) {
132            if (!event.scheduled())
133                object.schedule(event, object.clockEdge(Cycles(1)));
134            running = true;
135            numCycles += cyclesSinceLastStopped();
136            countCycles(cyclesSinceLastStopped());
137        }
138    }
139
140    /** How long have we been stopped for? */
141    Cycles
142    cyclesSinceLastStopped() const
143    {
144        return object.curCycle() - lastStopped;
145    }
146
147    /** Reset stopped time to current time */
148    void
149    resetLastStopped()
150    {
151        lastStopped = object.curCycle();
152    }
153
154    /** Cancel the next tick event and issue no more */
155    void
156    stop()
157    {
158        if (running) {
159            if (event.scheduled())
160                object.deschedule(event);
161            running = false;
162            resetLastStopped();
163        }
164    }
165
166    /** Checkpoint lastStopped */
167    void serialize(CheckpointOut &cp) const override;
168    void unserialize(CheckpointIn &cp) override;
169
170    /** Action to call on the clock tick */
171    virtual void evaluate() = 0;
172
173    /**
174     * Callback to handle cycle statistics and probes.
175     *
176     * This callback is called at the beginning of a new cycle active
177     * cycle and when restarting the ticked object. The delta
178     * parameter indicates the number of cycles elapsed since the
179     * previous call is normally '1' unless the object has been
180     * stopped and restarted.
181     *
182     * @param delta Number of cycles since the previous call.
183     */
184    virtual void countCycles(Cycles delta) {}
185};
186
187/** TickedObject attaches Ticked to ClockedObject and can be used as
188 *  a base class where ticked operation */
189class TickedObject : public ClockedObject, public Ticked
190{
191  public:
192    TickedObject(TickedObjectParams *params,
193        Event::Priority priority = Event::CPU_Tick_Pri);
194
195    /** Disambiguate to make these functions overload correctly */
196    using ClockedObject::regStats;
197    using ClockedObject::serialize;
198    using ClockedObject::unserialize;
199
200    /** Pass on regStats, serialize etc. onto Ticked */
201    void regStats();
202    void serialize(CheckpointOut &cp) const override;
203    void unserialize(CheckpointIn &cp) override;
204};
205
206#endif /* __SIM_TICKED_OBJECT_HH__ */
207