111527Sdavid.guillen@arm.com/*
212546Sanouk.vanlaer@arm.com * Copyright (c) 2016-2018 ARM Limited
311527Sdavid.guillen@arm.com * All rights reserved
411527Sdavid.guillen@arm.com *
511527Sdavid.guillen@arm.com * The license below extends only to copyright in the software and shall
611527Sdavid.guillen@arm.com * not be construed as granting a license to any other intellectual
711527Sdavid.guillen@arm.com * property including but not limited to intellectual property relating
811527Sdavid.guillen@arm.com * to a hardware implementation of the functionality of the software
911527Sdavid.guillen@arm.com * licensed hereunder.  You may use the software subject to the license
1011527Sdavid.guillen@arm.com * terms below provided that you ensure that this notice is replicated
1111527Sdavid.guillen@arm.com * unmodified and in its entirety in all distributions of the software,
1211527Sdavid.guillen@arm.com * modified or unmodified, in source code or in binary form.
1311527Sdavid.guillen@arm.com *
1411527Sdavid.guillen@arm.com * Redistribution and use in source and binary forms, with or without
1511527Sdavid.guillen@arm.com * modification, are permitted provided that the following conditions are
1611527Sdavid.guillen@arm.com * met: redistributions of source code must retain the above copyright
1711527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer;
1811527Sdavid.guillen@arm.com * redistributions in binary form must reproduce the above copyright
1911527Sdavid.guillen@arm.com * notice, this list of conditions and the following disclaimer in the
2011527Sdavid.guillen@arm.com * documentation and/or other materials provided with the distribution;
2111527Sdavid.guillen@arm.com * neither the name of the copyright holders nor the names of its
2211527Sdavid.guillen@arm.com * contributors may be used to endorse or promote products derived from
2311527Sdavid.guillen@arm.com * this software without specific prior written permission.
2411527Sdavid.guillen@arm.com *
2511527Sdavid.guillen@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2611527Sdavid.guillen@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2711527Sdavid.guillen@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2811527Sdavid.guillen@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2911527Sdavid.guillen@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3011527Sdavid.guillen@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3111527Sdavid.guillen@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3211527Sdavid.guillen@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3311527Sdavid.guillen@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3411527Sdavid.guillen@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3511527Sdavid.guillen@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3611527Sdavid.guillen@arm.com *
3711527Sdavid.guillen@arm.com * Authors: David Guillen Fandos
3811527Sdavid.guillen@arm.com */
3911527Sdavid.guillen@arm.com
4011527Sdavid.guillen@arm.com#include "sim/power/power_model.hh"
4111527Sdavid.guillen@arm.com
4211527Sdavid.guillen@arm.com#include "base/statistics.hh"
4311527Sdavid.guillen@arm.com#include "params/PowerModel.hh"
4411527Sdavid.guillen@arm.com#include "params/PowerModelState.hh"
4511800Sbrandon.potter@amd.com#include "sim/clocked_object.hh"
4611527Sdavid.guillen@arm.com#include "sim/sub_system.hh"
4711527Sdavid.guillen@arm.com
4811527Sdavid.guillen@arm.comPowerModelState::PowerModelState(const Params *p)
4911527Sdavid.guillen@arm.com    : SimObject(p), _temp(0), clocked_object(NULL)
5011527Sdavid.guillen@arm.com{
5111527Sdavid.guillen@arm.com}
5211527Sdavid.guillen@arm.com
5311527Sdavid.guillen@arm.comPowerModel::PowerModel(const Params *p)
5411527Sdavid.guillen@arm.com    : SimObject(p), states_pm(p->pm), subsystem(p->subsystem),
5512546Sanouk.vanlaer@arm.com      clocked_object(NULL), power_model_type(p->pm_type)
5611527Sdavid.guillen@arm.com{
5711527Sdavid.guillen@arm.com    panic_if(subsystem == NULL,
5811527Sdavid.guillen@arm.com             "Subsystem is NULL! This is not acceptable for a PowerModel!\n");
5911527Sdavid.guillen@arm.com    subsystem->registerPowerProducer(this);
6012547Sanouk.vanlaer@arm.com    // The temperature passed here will be overwritten, if there is
6112547Sanouk.vanlaer@arm.com    // a thermal model present
6212547Sanouk.vanlaer@arm.com    for (auto & pms: states_pm){
6312547Sanouk.vanlaer@arm.com        pms->setTemperature(p->ambient_temp);
6412547Sanouk.vanlaer@arm.com    }
6512547Sanouk.vanlaer@arm.com
6611527Sdavid.guillen@arm.com}
6711527Sdavid.guillen@arm.com
6811527Sdavid.guillen@arm.comvoid
6911527Sdavid.guillen@arm.comPowerModel::setClockedObject(ClockedObject * clkobj)
7011527Sdavid.guillen@arm.com{
7111527Sdavid.guillen@arm.com    this->clocked_object = clkobj;
7211527Sdavid.guillen@arm.com
7311527Sdavid.guillen@arm.com    for (auto & pms: states_pm)
7411527Sdavid.guillen@arm.com        pms->setClockedObject(clkobj);
7511527Sdavid.guillen@arm.com}
7611527Sdavid.guillen@arm.com
7711527Sdavid.guillen@arm.comvoid
7811527Sdavid.guillen@arm.comPowerModel::thermalUpdateCallback(const double & temp)
7911527Sdavid.guillen@arm.com{
8011527Sdavid.guillen@arm.com    for (auto & pms: states_pm)
8111527Sdavid.guillen@arm.com        pms->setTemperature(temp);
8211527Sdavid.guillen@arm.com}
8311527Sdavid.guillen@arm.com
8411527Sdavid.guillen@arm.comvoid
8511527Sdavid.guillen@arm.comPowerModel::regProbePoints()
8611527Sdavid.guillen@arm.com{
8711527Sdavid.guillen@arm.com    thermalListener.reset(new ThermalProbeListener (
8811527Sdavid.guillen@arm.com        *this, this->subsystem->getProbeManager(), "thermalUpdate"
8911527Sdavid.guillen@arm.com    ));
9011527Sdavid.guillen@arm.com}
9111527Sdavid.guillen@arm.com
9211527Sdavid.guillen@arm.comPowerModel*
9311527Sdavid.guillen@arm.comPowerModelParams::create()
9411527Sdavid.guillen@arm.com{
9511527Sdavid.guillen@arm.com    return new PowerModel(this);
9611527Sdavid.guillen@arm.com}
9711527Sdavid.guillen@arm.com
9811527Sdavid.guillen@arm.comdouble
9911527Sdavid.guillen@arm.comPowerModel::getDynamicPower() const
10011527Sdavid.guillen@arm.com{
10111527Sdavid.guillen@arm.com    assert(clocked_object);
10211527Sdavid.guillen@arm.com
10312546Sanouk.vanlaer@arm.com    if (power_model_type == Enums::PMType::Static) {
10412546Sanouk.vanlaer@arm.com        // This power model only collects static data
10512546Sanouk.vanlaer@arm.com        return 0;
10612546Sanouk.vanlaer@arm.com    }
10711527Sdavid.guillen@arm.com    std::vector<double> w = clocked_object->pwrStateWeights();
10811527Sdavid.guillen@arm.com
10911527Sdavid.guillen@arm.com    // Same number of states (excluding UNDEFINED)
11011527Sdavid.guillen@arm.com    assert(w.size() - 1 == states_pm.size());
11111527Sdavid.guillen@arm.com
11211527Sdavid.guillen@arm.com    // Make sure we have no UNDEFINED state
11311527Sdavid.guillen@arm.com    warn_if(w[Enums::PwrState::UNDEFINED] > 0,
11411527Sdavid.guillen@arm.com        "SimObject in UNDEFINED power state! Power figures might be wrong!\n");
11511527Sdavid.guillen@arm.com
11611527Sdavid.guillen@arm.com    double power = 0;
11711527Sdavid.guillen@arm.com    for (unsigned i = 0; i < states_pm.size(); i++)
11811527Sdavid.guillen@arm.com        if (w[i + 1] > 0.0f)
11911527Sdavid.guillen@arm.com            power += states_pm[i]->getDynamicPower() * w[i + 1];
12011527Sdavid.guillen@arm.com
12111527Sdavid.guillen@arm.com    return power;
12211527Sdavid.guillen@arm.com}
12311527Sdavid.guillen@arm.com
12411527Sdavid.guillen@arm.comdouble
12511527Sdavid.guillen@arm.comPowerModel::getStaticPower() const
12611527Sdavid.guillen@arm.com{
12711527Sdavid.guillen@arm.com    assert(clocked_object);
12811527Sdavid.guillen@arm.com
12911527Sdavid.guillen@arm.com    std::vector<double> w = clocked_object->pwrStateWeights();
13011527Sdavid.guillen@arm.com
13112546Sanouk.vanlaer@arm.com    if (power_model_type == Enums::PMType::Dynamic) {
13212546Sanouk.vanlaer@arm.com        // This power model only collects dynamic data
13312546Sanouk.vanlaer@arm.com        return 0;
13412546Sanouk.vanlaer@arm.com    }
13512546Sanouk.vanlaer@arm.com
13611527Sdavid.guillen@arm.com    // Same number of states (excluding UNDEFINED)
13711527Sdavid.guillen@arm.com    assert(w.size() - 1 == states_pm.size());
13811527Sdavid.guillen@arm.com
13911527Sdavid.guillen@arm.com    // Make sure we have no UNDEFINED state
14011527Sdavid.guillen@arm.com    if (w[0] > 0)
14111527Sdavid.guillen@arm.com        warn("SimObject in UNDEFINED power state! "
14211527Sdavid.guillen@arm.com             "Power figures might be wrong!\n");
14311527Sdavid.guillen@arm.com
14411527Sdavid.guillen@arm.com    // We have N+1 states, being state #0 the default 'UNDEFINED' state
14511527Sdavid.guillen@arm.com    double power = 0;
14611527Sdavid.guillen@arm.com    for (unsigned i = 0; i < states_pm.size(); i++)
14711527Sdavid.guillen@arm.com        // Don't evaluate power if the object hasn't been in that state
14811527Sdavid.guillen@arm.com        // This fixes issues with NaNs and similar.
14911527Sdavid.guillen@arm.com        if (w[i + 1] > 0.0f)
15011527Sdavid.guillen@arm.com            power += states_pm[i]->getStaticPower() * w[i + 1];
15111527Sdavid.guillen@arm.com
15211527Sdavid.guillen@arm.com    return power;
15311527Sdavid.guillen@arm.com}
154