ticked_object.hh revision 12113
110259SAndrew.Bardsley@arm.com/*
212113Sjose.marinho@arm.com * Copyright (c) 2013-2014, 2017 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 "sim/clocked_object.hh"
5210259SAndrew.Bardsley@arm.com
5311800Sbrandon.potter@amd.comclass TickedObjectParams;
5411800Sbrandon.potter@amd.com
5510259SAndrew.Bardsley@arm.com/** Ticked attaches gem5's event queue/scheduler to evaluate
5610259SAndrew.Bardsley@arm.com *  calls and provides a start/stop interface to ticking.
5710259SAndrew.Bardsley@arm.com *
5810259SAndrew.Bardsley@arm.com *  Ticked is not a ClockedObject but can be attached to one by
5910259SAndrew.Bardsley@arm.com *  inheritance and by calling regStats, serialize/unserialize */
6010905Sandreas.sandberg@arm.comclass Ticked : public Serializable
6110259SAndrew.Bardsley@arm.com{
6210259SAndrew.Bardsley@arm.com  protected:
6310259SAndrew.Bardsley@arm.com    /** An event to call process periodically */
6410259SAndrew.Bardsley@arm.com    class ClockEvent : public Event
6510259SAndrew.Bardsley@arm.com    {
6610259SAndrew.Bardsley@arm.com      public:
6710259SAndrew.Bardsley@arm.com        Ticked &owner;
6810259SAndrew.Bardsley@arm.com
6910259SAndrew.Bardsley@arm.com        ClockEvent(Ticked &owner_, Priority priority) :
7010259SAndrew.Bardsley@arm.com            Event(priority),
7110259SAndrew.Bardsley@arm.com            owner(owner_)
7210259SAndrew.Bardsley@arm.com        { }
7310259SAndrew.Bardsley@arm.com
7410259SAndrew.Bardsley@arm.com        /** Evaluate and reschedule */
7510259SAndrew.Bardsley@arm.com        void
7610259SAndrew.Bardsley@arm.com        process()
7710259SAndrew.Bardsley@arm.com        {
7810259SAndrew.Bardsley@arm.com            ++owner.tickCycles;
7910259SAndrew.Bardsley@arm.com            ++owner.numCycles;
8010464SAndreas.Sandberg@ARM.com            owner.countCycles(Cycles(1));
8110259SAndrew.Bardsley@arm.com            owner.evaluate();
8210259SAndrew.Bardsley@arm.com            if (owner.running) {
8310259SAndrew.Bardsley@arm.com                owner.object.schedule(this,
8410259SAndrew.Bardsley@arm.com                    owner.object.clockEdge(Cycles(1)));
8510259SAndrew.Bardsley@arm.com            }
8610259SAndrew.Bardsley@arm.com        }
8710259SAndrew.Bardsley@arm.com    };
8810259SAndrew.Bardsley@arm.com
8910259SAndrew.Bardsley@arm.com    friend class ClockEvent;
9010259SAndrew.Bardsley@arm.com
9110259SAndrew.Bardsley@arm.com    /** ClockedObject who is responsible for this Ticked's actions/stats */
9210259SAndrew.Bardsley@arm.com    ClockedObject &object;
9310259SAndrew.Bardsley@arm.com
9410259SAndrew.Bardsley@arm.com    /** The single instance of ClockEvent used */
9510259SAndrew.Bardsley@arm.com    ClockEvent event;
9610259SAndrew.Bardsley@arm.com
9710259SAndrew.Bardsley@arm.com    /** Have I been started? and am not stopped */
9810259SAndrew.Bardsley@arm.com    bool running;
9910259SAndrew.Bardsley@arm.com
10010259SAndrew.Bardsley@arm.com    /** Time of last stop event to calculate run time */
10110259SAndrew.Bardsley@arm.com    Cycles lastStopped;
10210259SAndrew.Bardsley@arm.com
10310259SAndrew.Bardsley@arm.com  private:
10410259SAndrew.Bardsley@arm.com    /** Locally allocated stats */
10510259SAndrew.Bardsley@arm.com    Stats::Scalar *numCyclesLocal;
10610259SAndrew.Bardsley@arm.com
10710259SAndrew.Bardsley@arm.com  protected:
10810259SAndrew.Bardsley@arm.com    /** Total number of cycles either ticked or spend stopped */
10910259SAndrew.Bardsley@arm.com    Stats::Scalar &numCycles;
11010259SAndrew.Bardsley@arm.com
11110259SAndrew.Bardsley@arm.com    /** Number of cycles ticked */
11210259SAndrew.Bardsley@arm.com    Stats::Scalar tickCycles;
11310259SAndrew.Bardsley@arm.com
11410259SAndrew.Bardsley@arm.com    /** Number of cycles stopped */
11510259SAndrew.Bardsley@arm.com    Stats::Formula idleCycles;
11610259SAndrew.Bardsley@arm.com
11710259SAndrew.Bardsley@arm.com  public:
11810259SAndrew.Bardsley@arm.com    Ticked(ClockedObject &object_,
11910259SAndrew.Bardsley@arm.com        Stats::Scalar *imported_num_cycles = NULL,
12010259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
12110259SAndrew.Bardsley@arm.com
12210259SAndrew.Bardsley@arm.com    virtual ~Ticked() { }
12310259SAndrew.Bardsley@arm.com
12410259SAndrew.Bardsley@arm.com    /** Register {num,ticks}Cycles if necessary.  If numCycles is
12510259SAndrew.Bardsley@arm.com     *  imported, be sure to register it *before* calling this regStats */
12610259SAndrew.Bardsley@arm.com    void regStats();
12710259SAndrew.Bardsley@arm.com
12810259SAndrew.Bardsley@arm.com    /** Start ticking */
12910259SAndrew.Bardsley@arm.com    void
13010259SAndrew.Bardsley@arm.com    start()
13110259SAndrew.Bardsley@arm.com    {
13210259SAndrew.Bardsley@arm.com        if (!running) {
13310259SAndrew.Bardsley@arm.com            if (!event.scheduled())
13410259SAndrew.Bardsley@arm.com                object.schedule(event, object.clockEdge(Cycles(1)));
13510259SAndrew.Bardsley@arm.com            running = true;
13610259SAndrew.Bardsley@arm.com            numCycles += cyclesSinceLastStopped();
13710464SAndreas.Sandberg@ARM.com            countCycles(cyclesSinceLastStopped());
13810259SAndrew.Bardsley@arm.com        }
13910259SAndrew.Bardsley@arm.com    }
14010259SAndrew.Bardsley@arm.com
14110259SAndrew.Bardsley@arm.com    /** How long have we been stopped for? */
14210259SAndrew.Bardsley@arm.com    Cycles
14310259SAndrew.Bardsley@arm.com    cyclesSinceLastStopped() const
14410259SAndrew.Bardsley@arm.com    {
14510259SAndrew.Bardsley@arm.com        return object.curCycle() - lastStopped;
14610259SAndrew.Bardsley@arm.com    }
14710259SAndrew.Bardsley@arm.com
14810259SAndrew.Bardsley@arm.com    /** Reset stopped time to current time */
14910259SAndrew.Bardsley@arm.com    void
15010259SAndrew.Bardsley@arm.com    resetLastStopped()
15110259SAndrew.Bardsley@arm.com    {
15210259SAndrew.Bardsley@arm.com        lastStopped = object.curCycle();
15310259SAndrew.Bardsley@arm.com    }
15410259SAndrew.Bardsley@arm.com
15510259SAndrew.Bardsley@arm.com    /** Cancel the next tick event and issue no more */
15610259SAndrew.Bardsley@arm.com    void
15710259SAndrew.Bardsley@arm.com    stop()
15810259SAndrew.Bardsley@arm.com    {
15910259SAndrew.Bardsley@arm.com        if (running) {
16010259SAndrew.Bardsley@arm.com            if (event.scheduled())
16110259SAndrew.Bardsley@arm.com                object.deschedule(event);
16210259SAndrew.Bardsley@arm.com            running = false;
16310259SAndrew.Bardsley@arm.com            resetLastStopped();
16410259SAndrew.Bardsley@arm.com        }
16510259SAndrew.Bardsley@arm.com    }
16610259SAndrew.Bardsley@arm.com
16710259SAndrew.Bardsley@arm.com    /** Checkpoint lastStopped */
16811168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
16911168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
17010259SAndrew.Bardsley@arm.com
17110259SAndrew.Bardsley@arm.com    /** Action to call on the clock tick */
17210259SAndrew.Bardsley@arm.com    virtual void evaluate() = 0;
17310464SAndreas.Sandberg@ARM.com
17410464SAndreas.Sandberg@ARM.com    /**
17510464SAndreas.Sandberg@ARM.com     * Callback to handle cycle statistics and probes.
17610464SAndreas.Sandberg@ARM.com     *
17710464SAndreas.Sandberg@ARM.com     * This callback is called at the beginning of a new cycle active
17810464SAndreas.Sandberg@ARM.com     * cycle and when restarting the ticked object. The delta
17910464SAndreas.Sandberg@ARM.com     * parameter indicates the number of cycles elapsed since the
18010464SAndreas.Sandberg@ARM.com     * previous call is normally '1' unless the object has been
18110464SAndreas.Sandberg@ARM.com     * stopped and restarted.
18210464SAndreas.Sandberg@ARM.com     *
18310464SAndreas.Sandberg@ARM.com     * @param delta Number of cycles since the previous call.
18410464SAndreas.Sandberg@ARM.com     */
18510464SAndreas.Sandberg@ARM.com    virtual void countCycles(Cycles delta) {}
18610259SAndrew.Bardsley@arm.com};
18710259SAndrew.Bardsley@arm.com
18810259SAndrew.Bardsley@arm.com/** TickedObject attaches Ticked to ClockedObject and can be used as
18910259SAndrew.Bardsley@arm.com *  a base class where ticked operation */
19010259SAndrew.Bardsley@arm.comclass TickedObject : public ClockedObject, public Ticked
19110259SAndrew.Bardsley@arm.com{
19210259SAndrew.Bardsley@arm.com  public:
19312113Sjose.marinho@arm.com    TickedObject(const TickedObjectParams *params,
19410259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
19510259SAndrew.Bardsley@arm.com
19610259SAndrew.Bardsley@arm.com    /** Disambiguate to make these functions overload correctly */
19710259SAndrew.Bardsley@arm.com    using ClockedObject::regStats;
19810259SAndrew.Bardsley@arm.com    using ClockedObject::serialize;
19910259SAndrew.Bardsley@arm.com    using ClockedObject::unserialize;
20010259SAndrew.Bardsley@arm.com
20110259SAndrew.Bardsley@arm.com    /** Pass on regStats, serialize etc. onto Ticked */
20211169Sandreas.hansson@arm.com    void regStats() override;
20311168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
20411168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
20510259SAndrew.Bardsley@arm.com};
20610259SAndrew.Bardsley@arm.com
20710259SAndrew.Bardsley@arm.com#endif /* __SIM_TICKED_OBJECT_HH__ */
208