clocked_object.cc revision 12265
1/*
2 * Copyright (c) 2015-2016 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Akash Bagdia
38 *          David Guillen Fandos
39 */
40
41#include "sim/clocked_object.hh"
42
43#include "base/misc.hh"
44#include "sim/power/power_model.hh"
45
46ClockedObject::ClockedObject(const ClockedObjectParams *p) :
47    SimObject(p), Clocked(*p->clk_domain),
48    _currPwrState(p->default_p_state),
49    prvEvalTick(0)
50{
51    // Register the power_model with the object
52    for (auto & power_model: p->power_model)
53        power_model->setClockedObject(this);
54}
55
56void
57ClockedObject::serialize(CheckpointOut &cp) const
58{
59    unsigned int currPwrState = (unsigned int)_currPwrState;
60
61    SERIALIZE_SCALAR(currPwrState);
62    SERIALIZE_SCALAR(prvEvalTick);
63}
64
65void
66ClockedObject::unserialize(CheckpointIn &cp)
67{
68    unsigned int currPwrState;
69
70    UNSERIALIZE_SCALAR(currPwrState);
71    UNSERIALIZE_SCALAR(prvEvalTick);
72
73    _currPwrState = Enums::PwrState(currPwrState);
74}
75
76void
77ClockedObject::pwrState(Enums::PwrState p)
78{
79    // Function should ideally be called only when there is a state change
80    if (_currPwrState == p) {
81        warn_once("ClockedObject: Already in the requested power state, " \
82                  "request ignored");
83        return;
84    }
85
86    // No need to compute stats if in the same tick, update state though. This
87    // can happen in cases like a) during start of the simulation multiple
88    // state changes happens in init/startup phase, b) one takes a decision to
89    // migrate state but decides to reverts back to the original state in the
90    // same tick if other conditions are not met elsewhere.
91    // Any state change related stats would have been recorded on previous call
92    // to the pwrState() function.
93    if (prvEvalTick == curTick() && curTick() != 0) {
94       warn("ClockedObject %s: More than one power state change request "\
95             "encountered within the same simulation tick %d",
96             name(), prvEvalTick);
97        _currPwrState = p;
98        return;
99    }
100
101    // Record stats for previous state.
102    computeStats();
103
104    _currPwrState = p;
105
106    numPwrStateTransitions++;
107}
108
109void
110ClockedObject::computeStats()
111{
112    // Calculate time elapsed from last (valid) state change
113    Tick elapsed_time = curTick() - prvEvalTick;
114
115    pwrStateResidencyTicks[_currPwrState] += elapsed_time;
116
117    // Time spent in CLK_GATED state, this might change depending on
118    // transition to other low power states in respective simulation
119    // objects.
120    if (_currPwrState == Enums::PwrState::CLK_GATED) {
121        pwrStateClkGateDist.sample(elapsed_time);
122    }
123
124    prvEvalTick = curTick();
125}
126
127std::vector<double>
128ClockedObject::pwrStateWeights() const
129{
130    // Get residency stats
131    std::vector<double> ret;
132    Stats::VCounter residencies;
133    pwrStateResidencyTicks.value(residencies);
134
135    // Account for current state too!
136    Tick elapsed_time = curTick() - prvEvalTick;
137    residencies[_currPwrState] += elapsed_time;
138
139    ret.resize(Enums::PwrState::Num_PwrState);
140    for (unsigned i = 0; i < Enums::PwrState::Num_PwrState; i++)
141        ret[i] = residencies[i] / \
142                     (pwrStateResidencyTicks.total() + elapsed_time);
143
144    return ret;
145}
146
147void
148ClockedObject::regStats()
149{
150    SimObject::regStats();
151
152    using namespace Stats;
153
154    numPwrStateTransitions
155        .name(params()->name + ".numPwrStateTransitions")
156        .desc("Number of power state transitions")
157        .flags(nozero)
158        ;
159
160    // Each sample is time in ticks
161    unsigned num_bins = std::max(params()->p_state_clk_gate_bins, 10U);
162    pwrStateClkGateDist
163        .init(params()->p_state_clk_gate_min, params()->p_state_clk_gate_max,
164          (params()->p_state_clk_gate_max / num_bins))
165        .name(params()->name + ".pwrStateClkGateDist")
166        .desc("Distribution of time spent in the clock gated state")
167        .flags(pdf | nozero | nonan)
168        ;
169
170    pwrStateResidencyTicks
171        .init(Enums::PwrState::Num_PwrState)
172        .name(params()->name + ".pwrStateResidencyTicks")
173        .desc("Cumulative time (in ticks) in various power states")
174        .flags(nozero)
175        ;
176    for (int i = 0; i < Enums::PwrState::Num_PwrState; i++) {
177        pwrStateResidencyTicks.subname(i, Enums::PwrStateStrings[i]);
178    }
179
180    numPwrStateTransitions = 0;
181
182    /**
183     * For every stats dump, the power state residency and other distribution
184     * stats should be computed just before the dump to ensure correct stats
185     * value being reported for current dump window. It avoids things like
186     * having any unreported time spent in a power state to be forwarded to the
187     * next dump window which might have rather unpleasant effects (like
188     * perturbing the distribution stats).
189     */
190    registerDumpCallback(new ClockedObjectDumpCallback(this));
191}
192