power_model.cc revision 12546:8182d78bebcb
16498Snate@binkert.org/*
24479Sbinkertn@umich.edu * Copyright (c) 2016-2018 ARM Limited
34479Sbinkertn@umich.edu * All rights reserved
44479Sbinkertn@umich.edu *
54479Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
66498Snate@binkert.org * not be construed as granting a license to any other intellectual
74479Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84479Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94479Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
106498Snate@binkert.org * terms below provided that you ensure that this notice is replicated
114479Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124479Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134479Sbinkertn@umich.edu *
144479Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
154479Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
164479Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
174479Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
184479Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
194479Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
204479Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
214479Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
224479Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
234479Sbinkertn@umich.edu * this software without specific prior written permission.
244479Sbinkertn@umich.edu *
254479Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
264479Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
276498Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
284479Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
294479Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
304479Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
316498Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
324479Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
334479Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344479Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
354479Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
364479Sbinkertn@umich.edu *
374479Sbinkertn@umich.edu * Authors: David Guillen Fandos
384479Sbinkertn@umich.edu */
394479Sbinkertn@umich.edu
404479Sbinkertn@umich.edu#include "sim/power/power_model.hh"
41
42#include "base/statistics.hh"
43#include "params/PowerModel.hh"
44#include "params/PowerModelState.hh"
45#include "sim/clocked_object.hh"
46#include "sim/sub_system.hh"
47
48PowerModelState::PowerModelState(const Params *p)
49    : SimObject(p), _temp(0), clocked_object(NULL)
50{
51}
52
53PowerModel::PowerModel(const Params *p)
54    : SimObject(p), states_pm(p->pm), subsystem(p->subsystem),
55      clocked_object(NULL), power_model_type(p->pm_type)
56{
57    panic_if(subsystem == NULL,
58             "Subsystem is NULL! This is not acceptable for a PowerModel!\n");
59    subsystem->registerPowerProducer(this);
60}
61
62void
63PowerModel::setClockedObject(ClockedObject * clkobj)
64{
65    this->clocked_object = clkobj;
66
67    for (auto & pms: states_pm)
68        pms->setClockedObject(clkobj);
69}
70
71void
72PowerModel::thermalUpdateCallback(const double & temp)
73{
74    for (auto & pms: states_pm)
75        pms->setTemperature(temp);
76}
77
78void
79PowerModel::regProbePoints()
80{
81    thermalListener.reset(new ThermalProbeListener (
82        *this, this->subsystem->getProbeManager(), "thermalUpdate"
83    ));
84}
85
86PowerModel*
87PowerModelParams::create()
88{
89    return new PowerModel(this);
90}
91
92double
93PowerModel::getDynamicPower() const
94{
95    assert(clocked_object);
96
97    if (power_model_type == Enums::PMType::Static) {
98        // This power model only collects static data
99        return 0;
100    }
101    std::vector<double> w = clocked_object->pwrStateWeights();
102
103    // Same number of states (excluding UNDEFINED)
104    assert(w.size() - 1 == states_pm.size());
105
106    // Make sure we have no UNDEFINED state
107    warn_if(w[Enums::PwrState::UNDEFINED] > 0,
108        "SimObject in UNDEFINED power state! Power figures might be wrong!\n");
109
110    double power = 0;
111    for (unsigned i = 0; i < states_pm.size(); i++)
112        if (w[i + 1] > 0.0f)
113            power += states_pm[i]->getDynamicPower() * w[i + 1];
114
115    return power;
116}
117
118double
119PowerModel::getStaticPower() const
120{
121    assert(clocked_object);
122
123    std::vector<double> w = clocked_object->pwrStateWeights();
124
125    if (power_model_type == Enums::PMType::Dynamic) {
126        // This power model only collects dynamic data
127        return 0;
128    }
129
130    // Same number of states (excluding UNDEFINED)
131    assert(w.size() - 1 == states_pm.size());
132
133    // Make sure we have no UNDEFINED state
134    if (w[0] > 0)
135        warn("SimObject in UNDEFINED power state! "
136             "Power figures might be wrong!\n");
137
138    // We have N+1 states, being state #0 the default 'UNDEFINED' state
139    double power = 0;
140    for (unsigned i = 0; i < states_pm.size(); i++)
141        // Don't evaluate power if the object hasn't been in that state
142        // This fixes issues with NaNs and similar.
143        if (w[i + 1] > 0.0f)
144            power += states_pm[i]->getStaticPower() * w[i + 1];
145
146    return power;
147}
148