ticked_object.hh revision 10464
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;
7910464SAndreas.Sandberg@ARM.com            owner.countCycles(Cycles(1));
8010259SAndrew.Bardsley@arm.com            owner.evaluate();
8110259SAndrew.Bardsley@arm.com            if (owner.running) {
8210259SAndrew.Bardsley@arm.com                owner.object.schedule(this,
8310259SAndrew.Bardsley@arm.com                    owner.object.clockEdge(Cycles(1)));
8410259SAndrew.Bardsley@arm.com            }
8510259SAndrew.Bardsley@arm.com        }
8610259SAndrew.Bardsley@arm.com    };
8710259SAndrew.Bardsley@arm.com
8810259SAndrew.Bardsley@arm.com    friend class ClockEvent;
8910259SAndrew.Bardsley@arm.com
9010259SAndrew.Bardsley@arm.com    /** ClockedObject who is responsible for this Ticked's actions/stats */
9110259SAndrew.Bardsley@arm.com    ClockedObject &object;
9210259SAndrew.Bardsley@arm.com
9310259SAndrew.Bardsley@arm.com    /** The single instance of ClockEvent used */
9410259SAndrew.Bardsley@arm.com    ClockEvent event;
9510259SAndrew.Bardsley@arm.com
9610259SAndrew.Bardsley@arm.com    /** Have I been started? and am not stopped */
9710259SAndrew.Bardsley@arm.com    bool running;
9810259SAndrew.Bardsley@arm.com
9910259SAndrew.Bardsley@arm.com    /** Time of last stop event to calculate run time */
10010259SAndrew.Bardsley@arm.com    Cycles lastStopped;
10110259SAndrew.Bardsley@arm.com
10210259SAndrew.Bardsley@arm.com  private:
10310259SAndrew.Bardsley@arm.com    /** Locally allocated stats */
10410259SAndrew.Bardsley@arm.com    Stats::Scalar *numCyclesLocal;
10510259SAndrew.Bardsley@arm.com
10610259SAndrew.Bardsley@arm.com  protected:
10710259SAndrew.Bardsley@arm.com    /** Total number of cycles either ticked or spend stopped */
10810259SAndrew.Bardsley@arm.com    Stats::Scalar &numCycles;
10910259SAndrew.Bardsley@arm.com
11010259SAndrew.Bardsley@arm.com    /** Number of cycles ticked */
11110259SAndrew.Bardsley@arm.com    Stats::Scalar tickCycles;
11210259SAndrew.Bardsley@arm.com
11310259SAndrew.Bardsley@arm.com    /** Number of cycles stopped */
11410259SAndrew.Bardsley@arm.com    Stats::Formula idleCycles;
11510259SAndrew.Bardsley@arm.com
11610259SAndrew.Bardsley@arm.com  public:
11710259SAndrew.Bardsley@arm.com    Ticked(ClockedObject &object_,
11810259SAndrew.Bardsley@arm.com        Stats::Scalar *imported_num_cycles = NULL,
11910259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
12010259SAndrew.Bardsley@arm.com
12110259SAndrew.Bardsley@arm.com    virtual ~Ticked() { }
12210259SAndrew.Bardsley@arm.com
12310259SAndrew.Bardsley@arm.com    /** Register {num,ticks}Cycles if necessary.  If numCycles is
12410259SAndrew.Bardsley@arm.com     *  imported, be sure to register it *before* calling this regStats */
12510259SAndrew.Bardsley@arm.com    void regStats();
12610259SAndrew.Bardsley@arm.com
12710259SAndrew.Bardsley@arm.com    /** Start ticking */
12810259SAndrew.Bardsley@arm.com    void
12910259SAndrew.Bardsley@arm.com    start()
13010259SAndrew.Bardsley@arm.com    {
13110259SAndrew.Bardsley@arm.com        if (!running) {
13210259SAndrew.Bardsley@arm.com            if (!event.scheduled())
13310259SAndrew.Bardsley@arm.com                object.schedule(event, object.clockEdge(Cycles(1)));
13410259SAndrew.Bardsley@arm.com            running = true;
13510259SAndrew.Bardsley@arm.com            numCycles += cyclesSinceLastStopped();
13610464SAndreas.Sandberg@ARM.com            countCycles(cyclesSinceLastStopped());
13710259SAndrew.Bardsley@arm.com        }
13810259SAndrew.Bardsley@arm.com    }
13910259SAndrew.Bardsley@arm.com
14010259SAndrew.Bardsley@arm.com    /** How long have we been stopped for? */
14110259SAndrew.Bardsley@arm.com    Cycles
14210259SAndrew.Bardsley@arm.com    cyclesSinceLastStopped() const
14310259SAndrew.Bardsley@arm.com    {
14410259SAndrew.Bardsley@arm.com        return object.curCycle() - lastStopped;
14510259SAndrew.Bardsley@arm.com    }
14610259SAndrew.Bardsley@arm.com
14710259SAndrew.Bardsley@arm.com    /** Reset stopped time to current time */
14810259SAndrew.Bardsley@arm.com    void
14910259SAndrew.Bardsley@arm.com    resetLastStopped()
15010259SAndrew.Bardsley@arm.com    {
15110259SAndrew.Bardsley@arm.com        lastStopped = object.curCycle();
15210259SAndrew.Bardsley@arm.com    }
15310259SAndrew.Bardsley@arm.com
15410259SAndrew.Bardsley@arm.com    /** Cancel the next tick event and issue no more */
15510259SAndrew.Bardsley@arm.com    void
15610259SAndrew.Bardsley@arm.com    stop()
15710259SAndrew.Bardsley@arm.com    {
15810259SAndrew.Bardsley@arm.com        if (running) {
15910259SAndrew.Bardsley@arm.com            if (event.scheduled())
16010259SAndrew.Bardsley@arm.com                object.deschedule(event);
16110259SAndrew.Bardsley@arm.com            running = false;
16210259SAndrew.Bardsley@arm.com            resetLastStopped();
16310259SAndrew.Bardsley@arm.com        }
16410259SAndrew.Bardsley@arm.com    }
16510259SAndrew.Bardsley@arm.com
16610259SAndrew.Bardsley@arm.com    /** Checkpoint lastStopped */
16710259SAndrew.Bardsley@arm.com    void serialize(std::ostream &os);
16810259SAndrew.Bardsley@arm.com    void unserialize(Checkpoint *cp, const std::string &section);
16910259SAndrew.Bardsley@arm.com
17010259SAndrew.Bardsley@arm.com    /** Action to call on the clock tick */
17110259SAndrew.Bardsley@arm.com    virtual void evaluate() = 0;
17210464SAndreas.Sandberg@ARM.com
17310464SAndreas.Sandberg@ARM.com    /**
17410464SAndreas.Sandberg@ARM.com     * Callback to handle cycle statistics and probes.
17510464SAndreas.Sandberg@ARM.com     *
17610464SAndreas.Sandberg@ARM.com     * This callback is called at the beginning of a new cycle active
17710464SAndreas.Sandberg@ARM.com     * cycle and when restarting the ticked object. The delta
17810464SAndreas.Sandberg@ARM.com     * parameter indicates the number of cycles elapsed since the
17910464SAndreas.Sandberg@ARM.com     * previous call is normally '1' unless the object has been
18010464SAndreas.Sandberg@ARM.com     * stopped and restarted.
18110464SAndreas.Sandberg@ARM.com     *
18210464SAndreas.Sandberg@ARM.com     * @param delta Number of cycles since the previous call.
18310464SAndreas.Sandberg@ARM.com     */
18410464SAndreas.Sandberg@ARM.com    virtual void countCycles(Cycles delta) {}
18510259SAndrew.Bardsley@arm.com};
18610259SAndrew.Bardsley@arm.com
18710259SAndrew.Bardsley@arm.com/** TickedObject attaches Ticked to ClockedObject and can be used as
18810259SAndrew.Bardsley@arm.com *  a base class where ticked operation */
18910259SAndrew.Bardsley@arm.comclass TickedObject : public ClockedObject, public Ticked
19010259SAndrew.Bardsley@arm.com{
19110259SAndrew.Bardsley@arm.com  public:
19210259SAndrew.Bardsley@arm.com    TickedObject(TickedObjectParams *params,
19310259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
19410259SAndrew.Bardsley@arm.com
19510259SAndrew.Bardsley@arm.com    /** Disambiguate to make these functions overload correctly */
19610259SAndrew.Bardsley@arm.com    using ClockedObject::regStats;
19710259SAndrew.Bardsley@arm.com    using ClockedObject::serialize;
19810259SAndrew.Bardsley@arm.com    using ClockedObject::unserialize;
19910259SAndrew.Bardsley@arm.com
20010259SAndrew.Bardsley@arm.com    /** Pass on regStats, serialize etc. onto Ticked */
20110259SAndrew.Bardsley@arm.com    void regStats();
20210259SAndrew.Bardsley@arm.com    void serialize(std::ostream &os);
20310259SAndrew.Bardsley@arm.com    void unserialize(Checkpoint *cp, const std::string &section);
20410259SAndrew.Bardsley@arm.com};
20510259SAndrew.Bardsley@arm.com
20610259SAndrew.Bardsley@arm.com#endif /* __SIM_TICKED_OBJECT_HH__ */
207