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    /** ClockedObject who is responsible for this Ticked's actions/stats */
6410259SAndrew.Bardsley@arm.com    ClockedObject &object;
6510259SAndrew.Bardsley@arm.com
6612125Sspwilson2@wisc.edu    /** The wrapper for processClockEvent */
6712125Sspwilson2@wisc.edu    EventFunctionWrapper event;
6812125Sspwilson2@wisc.edu
6912125Sspwilson2@wisc.edu    /** Evaluate and reschedule */
7012125Sspwilson2@wisc.edu    void processClockEvent();
7110259SAndrew.Bardsley@arm.com
7210259SAndrew.Bardsley@arm.com    /** Have I been started? and am not stopped */
7310259SAndrew.Bardsley@arm.com    bool running;
7410259SAndrew.Bardsley@arm.com
7510259SAndrew.Bardsley@arm.com    /** Time of last stop event to calculate run time */
7610259SAndrew.Bardsley@arm.com    Cycles lastStopped;
7710259SAndrew.Bardsley@arm.com
7810259SAndrew.Bardsley@arm.com  private:
7910259SAndrew.Bardsley@arm.com    /** Locally allocated stats */
8010259SAndrew.Bardsley@arm.com    Stats::Scalar *numCyclesLocal;
8110259SAndrew.Bardsley@arm.com
8210259SAndrew.Bardsley@arm.com  protected:
8310259SAndrew.Bardsley@arm.com    /** Total number of cycles either ticked or spend stopped */
8410259SAndrew.Bardsley@arm.com    Stats::Scalar &numCycles;
8510259SAndrew.Bardsley@arm.com
8610259SAndrew.Bardsley@arm.com    /** Number of cycles ticked */
8710259SAndrew.Bardsley@arm.com    Stats::Scalar tickCycles;
8810259SAndrew.Bardsley@arm.com
8910259SAndrew.Bardsley@arm.com    /** Number of cycles stopped */
9010259SAndrew.Bardsley@arm.com    Stats::Formula idleCycles;
9110259SAndrew.Bardsley@arm.com
9210259SAndrew.Bardsley@arm.com  public:
9310259SAndrew.Bardsley@arm.com    Ticked(ClockedObject &object_,
9410259SAndrew.Bardsley@arm.com        Stats::Scalar *imported_num_cycles = NULL,
9510259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
9610259SAndrew.Bardsley@arm.com
9710259SAndrew.Bardsley@arm.com    virtual ~Ticked() { }
9810259SAndrew.Bardsley@arm.com
9910259SAndrew.Bardsley@arm.com    /** Register {num,ticks}Cycles if necessary.  If numCycles is
10010259SAndrew.Bardsley@arm.com     *  imported, be sure to register it *before* calling this regStats */
10110259SAndrew.Bardsley@arm.com    void regStats();
10210259SAndrew.Bardsley@arm.com
10310259SAndrew.Bardsley@arm.com    /** Start ticking */
10410259SAndrew.Bardsley@arm.com    void
10510259SAndrew.Bardsley@arm.com    start()
10610259SAndrew.Bardsley@arm.com    {
10710259SAndrew.Bardsley@arm.com        if (!running) {
10810259SAndrew.Bardsley@arm.com            if (!event.scheduled())
10910259SAndrew.Bardsley@arm.com                object.schedule(event, object.clockEdge(Cycles(1)));
11010259SAndrew.Bardsley@arm.com            running = true;
11110259SAndrew.Bardsley@arm.com            numCycles += cyclesSinceLastStopped();
11210464SAndreas.Sandberg@ARM.com            countCycles(cyclesSinceLastStopped());
11310259SAndrew.Bardsley@arm.com        }
11410259SAndrew.Bardsley@arm.com    }
11510259SAndrew.Bardsley@arm.com
11610259SAndrew.Bardsley@arm.com    /** How long have we been stopped for? */
11710259SAndrew.Bardsley@arm.com    Cycles
11810259SAndrew.Bardsley@arm.com    cyclesSinceLastStopped() const
11910259SAndrew.Bardsley@arm.com    {
12010259SAndrew.Bardsley@arm.com        return object.curCycle() - lastStopped;
12110259SAndrew.Bardsley@arm.com    }
12210259SAndrew.Bardsley@arm.com
12310259SAndrew.Bardsley@arm.com    /** Reset stopped time to current time */
12410259SAndrew.Bardsley@arm.com    void
12510259SAndrew.Bardsley@arm.com    resetLastStopped()
12610259SAndrew.Bardsley@arm.com    {
12710259SAndrew.Bardsley@arm.com        lastStopped = object.curCycle();
12810259SAndrew.Bardsley@arm.com    }
12910259SAndrew.Bardsley@arm.com
13010259SAndrew.Bardsley@arm.com    /** Cancel the next tick event and issue no more */
13110259SAndrew.Bardsley@arm.com    void
13210259SAndrew.Bardsley@arm.com    stop()
13310259SAndrew.Bardsley@arm.com    {
13410259SAndrew.Bardsley@arm.com        if (running) {
13510259SAndrew.Bardsley@arm.com            if (event.scheduled())
13610259SAndrew.Bardsley@arm.com                object.deschedule(event);
13710259SAndrew.Bardsley@arm.com            running = false;
13810259SAndrew.Bardsley@arm.com            resetLastStopped();
13910259SAndrew.Bardsley@arm.com        }
14010259SAndrew.Bardsley@arm.com    }
14110259SAndrew.Bardsley@arm.com
14210259SAndrew.Bardsley@arm.com    /** Checkpoint lastStopped */
14311168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
14411168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
14510259SAndrew.Bardsley@arm.com
14610259SAndrew.Bardsley@arm.com    /** Action to call on the clock tick */
14710259SAndrew.Bardsley@arm.com    virtual void evaluate() = 0;
14810464SAndreas.Sandberg@ARM.com
14910464SAndreas.Sandberg@ARM.com    /**
15010464SAndreas.Sandberg@ARM.com     * Callback to handle cycle statistics and probes.
15110464SAndreas.Sandberg@ARM.com     *
15210464SAndreas.Sandberg@ARM.com     * This callback is called at the beginning of a new cycle active
15310464SAndreas.Sandberg@ARM.com     * cycle and when restarting the ticked object. The delta
15410464SAndreas.Sandberg@ARM.com     * parameter indicates the number of cycles elapsed since the
15510464SAndreas.Sandberg@ARM.com     * previous call is normally '1' unless the object has been
15610464SAndreas.Sandberg@ARM.com     * stopped and restarted.
15710464SAndreas.Sandberg@ARM.com     *
15810464SAndreas.Sandberg@ARM.com     * @param delta Number of cycles since the previous call.
15910464SAndreas.Sandberg@ARM.com     */
16010464SAndreas.Sandberg@ARM.com    virtual void countCycles(Cycles delta) {}
16110259SAndrew.Bardsley@arm.com};
16210259SAndrew.Bardsley@arm.com
16310259SAndrew.Bardsley@arm.com/** TickedObject attaches Ticked to ClockedObject and can be used as
16410259SAndrew.Bardsley@arm.com *  a base class where ticked operation */
16510259SAndrew.Bardsley@arm.comclass TickedObject : public ClockedObject, public Ticked
16610259SAndrew.Bardsley@arm.com{
16710259SAndrew.Bardsley@arm.com  public:
16812113Sjose.marinho@arm.com    TickedObject(const TickedObjectParams *params,
16910259SAndrew.Bardsley@arm.com        Event::Priority priority = Event::CPU_Tick_Pri);
17010259SAndrew.Bardsley@arm.com
17110259SAndrew.Bardsley@arm.com    /** Disambiguate to make these functions overload correctly */
17210259SAndrew.Bardsley@arm.com    using ClockedObject::regStats;
17310259SAndrew.Bardsley@arm.com    using ClockedObject::serialize;
17410259SAndrew.Bardsley@arm.com    using ClockedObject::unserialize;
17510259SAndrew.Bardsley@arm.com
17610259SAndrew.Bardsley@arm.com    /** Pass on regStats, serialize etc. onto Ticked */
17711169Sandreas.hansson@arm.com    void regStats() override;
17811168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
17911168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
18010259SAndrew.Bardsley@arm.com};
18110259SAndrew.Bardsley@arm.com
18210259SAndrew.Bardsley@arm.com#endif /* __SIM_TICKED_OBJECT_HH__ */
183