111527Sdavid.guillen@arm.com/*
211967Sandreas.sandberg@arm.com * Copyright (c) 2016-2017 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/mathexpr_powermodel.hh"
4111527Sdavid.guillen@arm.com
4211972Sstephan.diestelhorst@arm.com#include <string>
4311972Sstephan.diestelhorst@arm.com
4411527Sdavid.guillen@arm.com#include "base/statistics.hh"
4511527Sdavid.guillen@arm.com#include "params/MathExprPowerModel.hh"
4611527Sdavid.guillen@arm.com#include "sim/mathexpr.hh"
4711527Sdavid.guillen@arm.com#include "sim/power/thermal_model.hh"
4811527Sdavid.guillen@arm.com#include "sim/sim_object.hh"
4911527Sdavid.guillen@arm.com
5011527Sdavid.guillen@arm.comMathExprPowerModel::MathExprPowerModel(const Params *p)
5111967Sandreas.sandberg@arm.com    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
5211527Sdavid.guillen@arm.com{
5311527Sdavid.guillen@arm.com    // Calculate the name of the object we belong to
5411527Sdavid.guillen@arm.com    std::vector<std::string> path;
5511527Sdavid.guillen@arm.com    tokenize(path, name(), '.', true);
5611527Sdavid.guillen@arm.com    // It's something like xyz.power_model.pm2
5711527Sdavid.guillen@arm.com    assert(path.size() > 2);
5811527Sdavid.guillen@arm.com    for (unsigned i = 0; i < path.size() - 2; i++)
5911527Sdavid.guillen@arm.com        basename += path[i] + ".";
6011527Sdavid.guillen@arm.com}
6111527Sdavid.guillen@arm.com
6211527Sdavid.guillen@arm.comvoid
6311527Sdavid.guillen@arm.comMathExprPowerModel::startup()
6411527Sdavid.guillen@arm.com{
6511527Sdavid.guillen@arm.com    // Create a map with stats and pointers for quick access
6611527Sdavid.guillen@arm.com    // Has to be done here, since we need access to the statsList
6711972Sstephan.diestelhorst@arm.com    for (auto & i: Stats::statsList()) {
6811972Sstephan.diestelhorst@arm.com        if (i->name.find(basename) == 0) {
6911972Sstephan.diestelhorst@arm.com            // Add stats for this sim object and its child objects
7011527Sdavid.guillen@arm.com            stats_map[i->name.substr(basename.size())] = i;
7111972Sstephan.diestelhorst@arm.com        } else if (i->name.find(".") == std::string::npos) {
7211972Sstephan.diestelhorst@arm.com            // Add global stats (sim_seconds, for example)
7311972Sstephan.diestelhorst@arm.com            stats_map[i->name] = i;
7411972Sstephan.diestelhorst@arm.com        }
7511972Sstephan.diestelhorst@arm.com    }
7611967Sandreas.sandberg@arm.com
7711967Sandreas.sandberg@arm.com    tryEval(st_expr);
7811967Sandreas.sandberg@arm.com    const bool st_failed = failed;
7911967Sandreas.sandberg@arm.com
8011967Sandreas.sandberg@arm.com    tryEval(dyn_expr);
8111967Sandreas.sandberg@arm.com    const bool dyn_failed = failed;
8211967Sandreas.sandberg@arm.com
8311967Sandreas.sandberg@arm.com    if (st_failed || dyn_failed) {
8411967Sandreas.sandberg@arm.com        const auto *p = dynamic_cast<const Params *>(params());
8511967Sandreas.sandberg@arm.com        assert(p);
8611967Sandreas.sandberg@arm.com
8711967Sandreas.sandberg@arm.com        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
8811967Sandreas.sandberg@arm.com              st_failed ? p->st : "",
8911967Sandreas.sandberg@arm.com              st_failed && dyn_failed ? "\n" : "",
9011967Sandreas.sandberg@arm.com              dyn_failed ? p->dyn : "");
9111967Sandreas.sandberg@arm.com    }
9211527Sdavid.guillen@arm.com}
9311527Sdavid.guillen@arm.com
9411527Sdavid.guillen@arm.comdouble
9511967Sandreas.sandberg@arm.comMathExprPowerModel::eval(const MathExpr &expr) const
9611967Sandreas.sandberg@arm.com{
9711967Sandreas.sandberg@arm.com    const double value = tryEval(expr);
9811967Sandreas.sandberg@arm.com
9911967Sandreas.sandberg@arm.com    // This shouldn't happen unless something went wrong the equations
10011967Sandreas.sandberg@arm.com    // were verified in startup().
10111967Sandreas.sandberg@arm.com    panic_if(failed, "Failed to evaluate power expression '%s'\n",
10211967Sandreas.sandberg@arm.com             expr.toStr());
10311967Sandreas.sandberg@arm.com
10411967Sandreas.sandberg@arm.com    return value;
10511967Sandreas.sandberg@arm.com}
10611967Sandreas.sandberg@arm.com
10711967Sandreas.sandberg@arm.comdouble
10811967Sandreas.sandberg@arm.comMathExprPowerModel::tryEval(const MathExpr &expr) const
10911967Sandreas.sandberg@arm.com{
11011967Sandreas.sandberg@arm.com    failed = false;
11111967Sandreas.sandberg@arm.com    const double value = expr.eval(
11211967Sandreas.sandberg@arm.com        std::bind(&MathExprPowerModel::getStatValue,
11311967Sandreas.sandberg@arm.com                  this, std::placeholders::_1)
11411967Sandreas.sandberg@arm.com        );
11511967Sandreas.sandberg@arm.com
11611967Sandreas.sandberg@arm.com    return value;
11711967Sandreas.sandberg@arm.com}
11811967Sandreas.sandberg@arm.com
11911967Sandreas.sandberg@arm.com
12011967Sandreas.sandberg@arm.comdouble
12111527Sdavid.guillen@arm.comMathExprPowerModel::getStatValue(const std::string &name) const
12211527Sdavid.guillen@arm.com{
12311527Sdavid.guillen@arm.com    using namespace Stats;
12411527Sdavid.guillen@arm.com
12511527Sdavid.guillen@arm.com    // Automatic variables:
12611968Sandreas.sandberg@arm.com    if (name == "temp") {
12711527Sdavid.guillen@arm.com        return _temp;
12811968Sandreas.sandberg@arm.com    } else if (name == "voltage") {
12911968Sandreas.sandberg@arm.com        return clocked_object->voltage();
13013021Selhabbalsherif@gmail.com    } else if (name=="clock_period") {
13113021Selhabbalsherif@gmail.com        return clocked_object->clockPeriod();
13213021Selhabbalsherif@gmail.com    }
13311527Sdavid.guillen@arm.com
13411527Sdavid.guillen@arm.com    // Try to cast the stat, only these are supported right now
13511967Sandreas.sandberg@arm.com    const auto it = stats_map.find(name);
13611967Sandreas.sandberg@arm.com    if (it == stats_map.cend()) {
13711967Sandreas.sandberg@arm.com        warn("Failed to find stat '%s'\n", name);
13811967Sandreas.sandberg@arm.com        failed = true;
13911967Sandreas.sandberg@arm.com        return 0;
14011967Sandreas.sandberg@arm.com    }
14111527Sdavid.guillen@arm.com
14211967Sandreas.sandberg@arm.com    const Info *info = it->second;
14311967Sandreas.sandberg@arm.com
14411967Sandreas.sandberg@arm.com    auto si = dynamic_cast<const ScalarInfo *>(info);
14511527Sdavid.guillen@arm.com    if (si)
14611527Sdavid.guillen@arm.com        return si->value();
14711967Sandreas.sandberg@arm.com    auto fi = dynamic_cast<const FormulaInfo *>(info);
14811527Sdavid.guillen@arm.com    if (fi)
14911527Sdavid.guillen@arm.com        return fi->total();
15011527Sdavid.guillen@arm.com
15111527Sdavid.guillen@arm.com    panic("Unknown stat type!\n");
15211527Sdavid.guillen@arm.com}
15311527Sdavid.guillen@arm.com
15411527Sdavid.guillen@arm.comvoid
15511527Sdavid.guillen@arm.comMathExprPowerModel::regStats()
15611527Sdavid.guillen@arm.com{
15711527Sdavid.guillen@arm.com    PowerModelState::regStats();
15811527Sdavid.guillen@arm.com}
15911527Sdavid.guillen@arm.com
16011527Sdavid.guillen@arm.comMathExprPowerModel*
16111527Sdavid.guillen@arm.comMathExprPowerModelParams::create()
16211527Sdavid.guillen@arm.com{
16311527Sdavid.guillen@arm.com    return new MathExprPowerModel(this);
16411527Sdavid.guillen@arm.com}
165