mathexpr_powermodel.cc revision 11967
17405SAli.Saidi@ARM.com/*
212667Schuan.zhu@arm.com * Copyright (c) 2016-2017 ARM Limited
37405SAli.Saidi@ARM.com * All rights reserved
47405SAli.Saidi@ARM.com *
57405SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
67405SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
77405SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
87405SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
97405SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
107405SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
117405SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
127405SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
137405SAli.Saidi@ARM.com *
147405SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without
157405SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are
167405SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright
177405SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer;
187405SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright
197405SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the
207405SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution;
217405SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its
227405SAli.Saidi@ARM.com * contributors may be used to endorse or promote products derived from
237405SAli.Saidi@ARM.com * this software without specific prior written permission.
247405SAli.Saidi@ARM.com *
257405SAli.Saidi@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
267405SAli.Saidi@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
277405SAli.Saidi@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
287405SAli.Saidi@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
297405SAli.Saidi@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
307405SAli.Saidi@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
317405SAli.Saidi@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
327405SAli.Saidi@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
337405SAli.Saidi@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
347405SAli.Saidi@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
357405SAli.Saidi@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
367405SAli.Saidi@ARM.com *
377405SAli.Saidi@ARM.com * Authors: David Guillen Fandos
387405SAli.Saidi@ARM.com */
397405SAli.Saidi@ARM.com
407405SAli.Saidi@ARM.com#include "sim/power/mathexpr_powermodel.hh"
417405SAli.Saidi@ARM.com
4210461SAndreas.Sandberg@ARM.com#include "base/statistics.hh"
439050Schander.sudanthi@arm.com#include "params/MathExprPowerModel.hh"
4412406Sgabeblack@google.com#include "sim/mathexpr.hh"
4512605Sgiacomo.travaglini@arm.com#include "sim/power/thermal_model.hh"
4611793Sbrandon.potter@amd.com#include "sim/sim_object.hh"
478887Sgeoffrey.blake@arm.com
488232Snate@binkert.orgMathExprPowerModel::MathExprPowerModel(const Params *p)
498232Snate@binkert.org    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
5010844Sandreas.sandberg@arm.com{
519384SAndreas.Sandberg@arm.com    // Calculate the name of the object we belong to
527678Sgblack@eecs.umich.edu    std::vector<std::string> path;
538059SAli.Saidi@ARM.com    tokenize(path, name(), '.', true);
548284SAli.Saidi@ARM.com    // It's something like xyz.power_model.pm2
557405SAli.Saidi@ARM.com    assert(path.size() > 2);
567405SAli.Saidi@ARM.com    for (unsigned i = 0; i < path.size() - 2; i++)
577405SAli.Saidi@ARM.com        basename += path[i] + ".";
587405SAli.Saidi@ARM.com}
599384SAndreas.Sandberg@arm.com
6010461SAndreas.Sandberg@ARM.comvoid
6110461SAndreas.Sandberg@ARM.comMathExprPowerModel::startup()
6211165SRekai.GonzalezAlberquilla@arm.com{
6312109SRekai.GonzalezAlberquilla@arm.com    // Create a map with stats and pointers for quick access
6412714Sgiacomo.travaglini@arm.com    // Has to be done here, since we need access to the statsList
6512714Sgiacomo.travaglini@arm.com    for (auto & i: Stats::statsList())
669384SAndreas.Sandberg@arm.com        if (i->name.find(basename) == 0)
6711770SCurtis.Dunham@arm.com            stats_map[i->name.substr(basename.size())] = i;
6810037SARM gem5 Developers
6910461SAndreas.Sandberg@ARM.com    tryEval(st_expr);
7010461SAndreas.Sandberg@ARM.com    const bool st_failed = failed;
7110461SAndreas.Sandberg@ARM.com
7210461SAndreas.Sandberg@ARM.com    tryEval(dyn_expr);
7310461SAndreas.Sandberg@ARM.com    const bool dyn_failed = failed;
7410461SAndreas.Sandberg@ARM.com
7510609Sandreas.sandberg@arm.com    if (st_failed || dyn_failed) {
7610609Sandreas.sandberg@arm.com        const auto *p = dynamic_cast<const Params *>(params());
7710609Sandreas.sandberg@arm.com        assert(p);
7810037SARM gem5 Developers
7910037SARM gem5 Developers        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
8010037SARM gem5 Developers              st_failed ? p->st : "",
8110037SARM gem5 Developers              st_failed && dyn_failed ? "\n" : "",
8211771SCurtis.Dunham@arm.com              dyn_failed ? p->dyn : "");
8310037SARM gem5 Developers    }
8410037SARM gem5 Developers}
8510037SARM gem5 Developers
8610037SARM gem5 Developersdouble
8710037SARM gem5 DevelopersMathExprPowerModel::eval(const MathExpr &expr) const
8810037SARM gem5 Developers{
8911771SCurtis.Dunham@arm.com    const double value = tryEval(expr);
9010037SARM gem5 Developers
9110037SARM gem5 Developers    // This shouldn't happen unless something went wrong the equations
9210037SARM gem5 Developers    // were verified in startup().
9310037SARM gem5 Developers    panic_if(failed, "Failed to evaluate power expression '%s'\n",
9410037SARM gem5 Developers             expr.toStr());
9512477SCurtis.Dunham@arm.com
9610037SARM gem5 Developers    return value;
9710037SARM gem5 Developers}
989384SAndreas.Sandberg@arm.com
999384SAndreas.Sandberg@arm.comdouble
1009384SAndreas.Sandberg@arm.comMathExprPowerModel::tryEval(const MathExpr &expr) const
10112479SCurtis.Dunham@arm.com{
10212479SCurtis.Dunham@arm.com    failed = false;
1039384SAndreas.Sandberg@arm.com    const double value = expr.eval(
1049384SAndreas.Sandberg@arm.com        std::bind(&MathExprPowerModel::getStatValue,
1059384SAndreas.Sandberg@arm.com                  this, std::placeholders::_1)
1069384SAndreas.Sandberg@arm.com        );
1079384SAndreas.Sandberg@arm.com
1089384SAndreas.Sandberg@arm.com    return value;
1097427Sgblack@eecs.umich.edu}
1107427Sgblack@eecs.umich.edu
1117427Sgblack@eecs.umich.edu
1129385SAndreas.Sandberg@arm.comdouble
1139385SAndreas.Sandberg@arm.comMathExprPowerModel::getStatValue(const std::string &name) const
1147427Sgblack@eecs.umich.edu{
1157427Sgblack@eecs.umich.edu    using namespace Stats;
11610037SARM gem5 Developers
11710037SARM gem5 Developers    // Automatic variables:
11810037SARM gem5 Developers    if (name == "temp")
11910037SARM gem5 Developers        return _temp;
12010037SARM gem5 Developers
12110037SARM gem5 Developers    // Try to cast the stat, only these are supported right now
12212690Sgiacomo.travaglini@arm.com    const auto it = stats_map.find(name);
12312690Sgiacomo.travaglini@arm.com    if (it == stats_map.cend()) {
12412690Sgiacomo.travaglini@arm.com        warn("Failed to find stat '%s'\n", name);
12512690Sgiacomo.travaglini@arm.com        failed = true;
12612690Sgiacomo.travaglini@arm.com        return 0;
12712690Sgiacomo.travaglini@arm.com    }
12812690Sgiacomo.travaglini@arm.com
12912690Sgiacomo.travaglini@arm.com    const Info *info = it->second;
13012690Sgiacomo.travaglini@arm.com
13112690Sgiacomo.travaglini@arm.com    auto si = dynamic_cast<const ScalarInfo *>(info);
13212690Sgiacomo.travaglini@arm.com    if (si)
13312690Sgiacomo.travaglini@arm.com        return si->value();
13410037SARM gem5 Developers    auto fi = dynamic_cast<const FormulaInfo *>(info);
13510037SARM gem5 Developers    if (fi)
13610037SARM gem5 Developers        return fi->total();
13710037SARM gem5 Developers
13810037SARM gem5 Developers    panic("Unknown stat type!\n");
13910037SARM gem5 Developers}
14010037SARM gem5 Developers
14110037SARM gem5 Developersvoid
1427427Sgblack@eecs.umich.eduMathExprPowerModel::regStats()
1437427Sgblack@eecs.umich.edu{
1447427Sgblack@eecs.umich.edu    PowerModelState::regStats();
1457427Sgblack@eecs.umich.edu}
1467427Sgblack@eecs.umich.edu
1477427Sgblack@eecs.umich.eduMathExprPowerModel*
14810037SARM gem5 DevelopersMathExprPowerModelParams::create()
14910037SARM gem5 Developers{
15010037SARM gem5 Developers    return new MathExprPowerModel(this);
15110037SARM gem5 Developers}
1527427Sgblack@eecs.umich.edu