clocked_object.cc revision 11525
111524Sdavid.guillen@arm.com/*
211524Sdavid.guillen@arm.com * Copyright (c) 2015-2016 ARM Limited
311524Sdavid.guillen@arm.com * All rights reserved
411524Sdavid.guillen@arm.com *
511524Sdavid.guillen@arm.com * The license below extends only to copyright in the software and shall
611524Sdavid.guillen@arm.com * not be construed as granting a license to any other intellectual
711524Sdavid.guillen@arm.com * property including but not limited to intellectual property relating
811524Sdavid.guillen@arm.com * to a hardware implementation of the functionality of the software
911524Sdavid.guillen@arm.com * licensed hereunder.  You may use the software subject to the license
1011524Sdavid.guillen@arm.com * terms below provided that you ensure that this notice is replicated
1111524Sdavid.guillen@arm.com * unmodified and in its entirety in all distributions of the software,
1211524Sdavid.guillen@arm.com * modified or unmodified, in source code or in binary form.
1311524Sdavid.guillen@arm.com *
1411524Sdavid.guillen@arm.com * Redistribution and use in source and binary forms, with or without
1511524Sdavid.guillen@arm.com * modification, are permitted provided that the following conditions are
1611524Sdavid.guillen@arm.com * met: redistributions of source code must retain the above copyright
1711524Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer;
1811524Sdavid.guillen@arm.com * redistributions in binary form must reproduce the above copyright
1911524Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer in the
2011524Sdavid.guillen@arm.com * documentation and/or other materials provided with the distribution;
2111524Sdavid.guillen@arm.com * neither the name of the copyright holders nor the names of its
2211524Sdavid.guillen@arm.com * contributors may be used to endorse or promote products derived from
2311524Sdavid.guillen@arm.com * this software without specific prior written permission.
2411524Sdavid.guillen@arm.com *
2511524Sdavid.guillen@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2611524Sdavid.guillen@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2711524Sdavid.guillen@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2811524Sdavid.guillen@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2911524Sdavid.guillen@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3011524Sdavid.guillen@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3111524Sdavid.guillen@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3211524Sdavid.guillen@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3311524Sdavid.guillen@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3411524Sdavid.guillen@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3511524Sdavid.guillen@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3611524Sdavid.guillen@arm.com *
3711524Sdavid.guillen@arm.com * Authors: Akash Bagdia
3811524Sdavid.guillen@arm.com *          David Guillen Fandos
3911524Sdavid.guillen@arm.com */
4011524Sdavid.guillen@arm.com
4111524Sdavid.guillen@arm.com#include "sim/clocked_object.hh"
4211524Sdavid.guillen@arm.com
4311524Sdavid.guillen@arm.com#include "base/misc.hh"
4411524Sdavid.guillen@arm.com
4511524Sdavid.guillen@arm.comvoid
4611524Sdavid.guillen@arm.comClockedObject::serialize(CheckpointOut &cp) const
4711524Sdavid.guillen@arm.com{
4811524Sdavid.guillen@arm.com    unsigned int currPwrState = (unsigned int)_currPwrState;
4911524Sdavid.guillen@arm.com
5011524Sdavid.guillen@arm.com    SERIALIZE_SCALAR(currPwrState);
5111524Sdavid.guillen@arm.com    SERIALIZE_SCALAR(prvEvalTick);
5211524Sdavid.guillen@arm.com}
5311524Sdavid.guillen@arm.com
5411524Sdavid.guillen@arm.comvoid
5511524Sdavid.guillen@arm.comClockedObject::unserialize(CheckpointIn &cp)
5611524Sdavid.guillen@arm.com{
5711524Sdavid.guillen@arm.com    unsigned int currPwrState;
5811524Sdavid.guillen@arm.com
5911524Sdavid.guillen@arm.com    UNSERIALIZE_SCALAR(currPwrState);
6011524Sdavid.guillen@arm.com    UNSERIALIZE_SCALAR(prvEvalTick);
6111524Sdavid.guillen@arm.com
6211524Sdavid.guillen@arm.com    _currPwrState = Enums::PwrState(currPwrState);
6311524Sdavid.guillen@arm.com}
6411524Sdavid.guillen@arm.com
6511524Sdavid.guillen@arm.comvoid
6611524Sdavid.guillen@arm.comClockedObject::pwrState(Enums::PwrState p)
6711524Sdavid.guillen@arm.com{
6811524Sdavid.guillen@arm.com    // Function should ideally be called only when there is a state change
6911524Sdavid.guillen@arm.com    if (_currPwrState == p) {
7011524Sdavid.guillen@arm.com        warn("ClockedObject: Already in the requested power state, request "\
7111524Sdavid.guillen@arm.com             "ignored");
7211524Sdavid.guillen@arm.com        return;
7311524Sdavid.guillen@arm.com    }
7411524Sdavid.guillen@arm.com
7511524Sdavid.guillen@arm.com    // No need to compute stats if in the same tick, update state though. This
7611524Sdavid.guillen@arm.com    // can happen in cases like a) during start of the simulation multiple
7711524Sdavid.guillen@arm.com    // state changes happens in init/startup phase, b) one takes a decision to
7811524Sdavid.guillen@arm.com    // migrate state but decides to reverts back to the original state in the
7911524Sdavid.guillen@arm.com    // same tick if other conditions are not met elsewhere.
8011524Sdavid.guillen@arm.com    // Any state change related stats would have been recorded on previous call
8111524Sdavid.guillen@arm.com    // to the pwrState() function.
8211524Sdavid.guillen@arm.com    if (prvEvalTick == curTick()) {
8311524Sdavid.guillen@arm.com        warn("ClockedObject: More than one power state change request "\
8411524Sdavid.guillen@arm.com             "encountered within the same simulation tick");
8511524Sdavid.guillen@arm.com        _currPwrState = p;
8611524Sdavid.guillen@arm.com        return;
8711524Sdavid.guillen@arm.com    }
8811524Sdavid.guillen@arm.com
8911524Sdavid.guillen@arm.com    // Record stats for previous state.
9011524Sdavid.guillen@arm.com    computeStats();
9111524Sdavid.guillen@arm.com
9211524Sdavid.guillen@arm.com    _currPwrState = p;
9311524Sdavid.guillen@arm.com
9411524Sdavid.guillen@arm.com    numPwrStateTransitions++;
9511524Sdavid.guillen@arm.com}
9611524Sdavid.guillen@arm.com
9711524Sdavid.guillen@arm.comvoid
9811524Sdavid.guillen@arm.comClockedObject::computeStats()
9911524Sdavid.guillen@arm.com{
10011524Sdavid.guillen@arm.com    // Calculate time elapsed from last (valid) state change
10111524Sdavid.guillen@arm.com    Tick elapsed_time = curTick() - prvEvalTick;
10211524Sdavid.guillen@arm.com
10311524Sdavid.guillen@arm.com    pwrStateResidencyTicks[_currPwrState] += elapsed_time;
10411524Sdavid.guillen@arm.com
10511524Sdavid.guillen@arm.com    // Time spent in CLK_GATED state, this might change depending on
10611524Sdavid.guillen@arm.com    // transition to other low power states in respective simulation
10711524Sdavid.guillen@arm.com    // objects.
10811524Sdavid.guillen@arm.com    if (_currPwrState == Enums::PwrState::CLK_GATED) {
10911524Sdavid.guillen@arm.com        pwrStateClkGateDist.sample(elapsed_time);
11011524Sdavid.guillen@arm.com    }
11111524Sdavid.guillen@arm.com
11211524Sdavid.guillen@arm.com    prvEvalTick = curTick();
11311524Sdavid.guillen@arm.com}
11411524Sdavid.guillen@arm.com
11511524Sdavid.guillen@arm.comstd::vector<double>
11611524Sdavid.guillen@arm.comClockedObject::pwrStateWeights() const
11711524Sdavid.guillen@arm.com{
11811524Sdavid.guillen@arm.com    // Get residency stats
11911524Sdavid.guillen@arm.com    std::vector<double> ret;
12011524Sdavid.guillen@arm.com    Stats::VCounter residencies;
12111524Sdavid.guillen@arm.com    pwrStateResidencyTicks.value(residencies);
12211524Sdavid.guillen@arm.com
12311524Sdavid.guillen@arm.com    // Account for current state too!
12411524Sdavid.guillen@arm.com    Tick elapsed_time = curTick() - prvEvalTick;
12511524Sdavid.guillen@arm.com    residencies[_currPwrState] += elapsed_time;
12611524Sdavid.guillen@arm.com
12711524Sdavid.guillen@arm.com    ret.resize(Enums::PwrState::Num_PwrState);
12811524Sdavid.guillen@arm.com    for (unsigned i = 0; i < Enums::PwrState::Num_PwrState; i++)
12911524Sdavid.guillen@arm.com        ret[i] = residencies[i] / \
13011524Sdavid.guillen@arm.com                     (pwrStateResidencyTicks.total() + elapsed_time);
13111524Sdavid.guillen@arm.com
13211524Sdavid.guillen@arm.com    return ret;
13311524Sdavid.guillen@arm.com}
13411524Sdavid.guillen@arm.com
13511524Sdavid.guillen@arm.comvoid
13611524Sdavid.guillen@arm.comClockedObject::regStats()
13711524Sdavid.guillen@arm.com{
13811524Sdavid.guillen@arm.com    SimObject::regStats();
13911524Sdavid.guillen@arm.com
14011524Sdavid.guillen@arm.com    using namespace Stats;
14111524Sdavid.guillen@arm.com
14211524Sdavid.guillen@arm.com    numPwrStateTransitions
14311524Sdavid.guillen@arm.com        .name(params()->name + ".numPwrStateTransitions")
14411524Sdavid.guillen@arm.com        .desc("Number of power state transitions")
14511525Sandreas.sandberg@arm.com        .flags(nozero)
14611524Sdavid.guillen@arm.com        ;
14711524Sdavid.guillen@arm.com
14811524Sdavid.guillen@arm.com    // Each sample is time in ticks
14911524Sdavid.guillen@arm.com    unsigned num_bins = std::max(params()->p_state_clk_gate_bins, 10U);
15011524Sdavid.guillen@arm.com    pwrStateClkGateDist
15111524Sdavid.guillen@arm.com        .init(params()->p_state_clk_gate_min, params()->p_state_clk_gate_max,
15211524Sdavid.guillen@arm.com          (params()->p_state_clk_gate_max / num_bins))
15311524Sdavid.guillen@arm.com        .name(params()->name + ".pwrStateClkGateDist")
15411524Sdavid.guillen@arm.com        .desc("Distribution of time spent in the clock gated state")
15511525Sandreas.sandberg@arm.com        .flags(pdf | nozero | nonan)
15611524Sdavid.guillen@arm.com        ;
15711524Sdavid.guillen@arm.com
15811524Sdavid.guillen@arm.com    pwrStateResidencyTicks
15911524Sdavid.guillen@arm.com        .init(Enums::PwrState::Num_PwrState)
16011524Sdavid.guillen@arm.com        .name(params()->name + ".pwrStateResidencyTicks")
16111524Sdavid.guillen@arm.com        .desc("Cumulative time (in ticks) in various power states")
16211525Sandreas.sandberg@arm.com        .flags(nozero)
16311524Sdavid.guillen@arm.com        ;
16411524Sdavid.guillen@arm.com    for (int i = 0; i < Enums::PwrState::Num_PwrState; i++) {
16511524Sdavid.guillen@arm.com        pwrStateResidencyTicks.subname(i, Enums::PwrStateStrings[i]);
16611524Sdavid.guillen@arm.com    }
16711524Sdavid.guillen@arm.com
16811524Sdavid.guillen@arm.com    numPwrStateTransitions = 0;
16911524Sdavid.guillen@arm.com
17011524Sdavid.guillen@arm.com    /**
17111524Sdavid.guillen@arm.com     * For every stats dump, the power state residency and other distribution
17211524Sdavid.guillen@arm.com     * stats should be computed just before the dump to ensure correct stats
17311524Sdavid.guillen@arm.com     * value being reported for current dump window. It avoids things like
17411524Sdavid.guillen@arm.com     * having any unreported time spent in a power state to be forwarded to the
17511524Sdavid.guillen@arm.com     * next dump window which might have rather unpleasant effects (like
17611524Sdavid.guillen@arm.com     * perturbing the distribution stats).
17711524Sdavid.guillen@arm.com     */
17811524Sdavid.guillen@arm.com    registerDumpCallback(new ClockedObjectDumpCallback(this));
17911524Sdavid.guillen@arm.com}
180