mathexpr_powermodel.cc revision 13021:f57df5a2660e
113219Sodanrc@yahoo.com.br/*
213219Sodanrc@yahoo.com.br * Copyright (c) 2016-2017 ARM Limited
313219Sodanrc@yahoo.com.br * All rights reserved
413219Sodanrc@yahoo.com.br *
513219Sodanrc@yahoo.com.br * The license below extends only to copyright in the software and shall
613219Sodanrc@yahoo.com.br * not be construed as granting a license to any other intellectual
713219Sodanrc@yahoo.com.br * property including but not limited to intellectual property relating
813219Sodanrc@yahoo.com.br * to a hardware implementation of the functionality of the software
913219Sodanrc@yahoo.com.br * licensed hereunder.  You may use the software subject to the license
1013219Sodanrc@yahoo.com.br * terms below provided that you ensure that this notice is replicated
1113219Sodanrc@yahoo.com.br * unmodified and in its entirety in all distributions of the software,
1213219Sodanrc@yahoo.com.br * modified or unmodified, in source code or in binary form.
1313219Sodanrc@yahoo.com.br *
1413219Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
1513219Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
1613219Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
1713219Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
1813219Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1913219Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
2013219Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
2113219Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
2213219Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
2313219Sodanrc@yahoo.com.br * this software without specific prior written permission.
2413219Sodanrc@yahoo.com.br *
2513219Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2613219Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2713219Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2813219Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2913219Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3013219Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3113219Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3213219Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3313219Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3413219Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3513219Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3613219Sodanrc@yahoo.com.br *
3713219Sodanrc@yahoo.com.br * Authors: David Guillen Fandos
3813219Sodanrc@yahoo.com.br */
3913219Sodanrc@yahoo.com.br
4013219Sodanrc@yahoo.com.br#include "sim/power/mathexpr_powermodel.hh"
4113219Sodanrc@yahoo.com.br
4213219Sodanrc@yahoo.com.br#include <string>
4313219Sodanrc@yahoo.com.br
4413219Sodanrc@yahoo.com.br#include "base/statistics.hh"
4513219Sodanrc@yahoo.com.br#include "params/MathExprPowerModel.hh"
4613219Sodanrc@yahoo.com.br#include "sim/mathexpr.hh"
4713219Sodanrc@yahoo.com.br#include "sim/power/thermal_model.hh"
4813219Sodanrc@yahoo.com.br#include "sim/sim_object.hh"
4913219Sodanrc@yahoo.com.br
5013219Sodanrc@yahoo.com.brMathExprPowerModel::MathExprPowerModel(const Params *p)
5113219Sodanrc@yahoo.com.br    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
5213219Sodanrc@yahoo.com.br{
5313219Sodanrc@yahoo.com.br    // Calculate the name of the object we belong to
5413219Sodanrc@yahoo.com.br    std::vector<std::string> path;
5513219Sodanrc@yahoo.com.br    tokenize(path, name(), '.', true);
5613219Sodanrc@yahoo.com.br    // It's something like xyz.power_model.pm2
5713219Sodanrc@yahoo.com.br    assert(path.size() > 2);
5813219Sodanrc@yahoo.com.br    for (unsigned i = 0; i < path.size() - 2; i++)
5913219Sodanrc@yahoo.com.br        basename += path[i] + ".";
6013219Sodanrc@yahoo.com.br}
6113219Sodanrc@yahoo.com.br
6213219Sodanrc@yahoo.com.brvoid
6313219Sodanrc@yahoo.com.brMathExprPowerModel::startup()
6413219Sodanrc@yahoo.com.br{
6513219Sodanrc@yahoo.com.br    // Create a map with stats and pointers for quick access
6613219Sodanrc@yahoo.com.br    // Has to be done here, since we need access to the statsList
6713219Sodanrc@yahoo.com.br    for (auto & i: Stats::statsList()) {
6813219Sodanrc@yahoo.com.br        if (i->name.find(basename) == 0) {
6913219Sodanrc@yahoo.com.br            // Add stats for this sim object and its child objects
7013219Sodanrc@yahoo.com.br            stats_map[i->name.substr(basename.size())] = i;
7113219Sodanrc@yahoo.com.br        } else if (i->name.find(".") == std::string::npos) {
7213219Sodanrc@yahoo.com.br            // Add global stats (sim_seconds, for example)
7313219Sodanrc@yahoo.com.br            stats_map[i->name] = i;
7413219Sodanrc@yahoo.com.br        }
7513219Sodanrc@yahoo.com.br    }
7613219Sodanrc@yahoo.com.br
7713219Sodanrc@yahoo.com.br    tryEval(st_expr);
7813219Sodanrc@yahoo.com.br    const bool st_failed = failed;
7913219Sodanrc@yahoo.com.br
8013219Sodanrc@yahoo.com.br    tryEval(dyn_expr);
8113219Sodanrc@yahoo.com.br    const bool dyn_failed = failed;
8213219Sodanrc@yahoo.com.br
8313219Sodanrc@yahoo.com.br    if (st_failed || dyn_failed) {
8413219Sodanrc@yahoo.com.br        const auto *p = dynamic_cast<const Params *>(params());
8513219Sodanrc@yahoo.com.br        assert(p);
8613219Sodanrc@yahoo.com.br
8713219Sodanrc@yahoo.com.br        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
8813219Sodanrc@yahoo.com.br              st_failed ? p->st : "",
8913219Sodanrc@yahoo.com.br              st_failed && dyn_failed ? "\n" : "",
9013219Sodanrc@yahoo.com.br              dyn_failed ? p->dyn : "");
9113219Sodanrc@yahoo.com.br    }
9213219Sodanrc@yahoo.com.br}
9313219Sodanrc@yahoo.com.br
9413219Sodanrc@yahoo.com.brdouble
9513219Sodanrc@yahoo.com.brMathExprPowerModel::eval(const MathExpr &expr) const
9613219Sodanrc@yahoo.com.br{
9713219Sodanrc@yahoo.com.br    const double value = tryEval(expr);
9813219Sodanrc@yahoo.com.br
9913219Sodanrc@yahoo.com.br    // This shouldn't happen unless something went wrong the equations
10013219Sodanrc@yahoo.com.br    // were verified in startup().
10113219Sodanrc@yahoo.com.br    panic_if(failed, "Failed to evaluate power expression '%s'\n",
10213219Sodanrc@yahoo.com.br             expr.toStr());
103
104    return value;
105}
106
107double
108MathExprPowerModel::tryEval(const MathExpr &expr) const
109{
110    failed = false;
111    const double value = expr.eval(
112        std::bind(&MathExprPowerModel::getStatValue,
113                  this, std::placeholders::_1)
114        );
115
116    return value;
117}
118
119
120double
121MathExprPowerModel::getStatValue(const std::string &name) const
122{
123    using namespace Stats;
124
125    // Automatic variables:
126    if (name == "temp") {
127        return _temp;
128    } else if (name == "voltage") {
129        return clocked_object->voltage();
130    } else if (name=="clock_period") {
131        return clocked_object->clockPeriod();
132    }
133
134    // Try to cast the stat, only these are supported right now
135    const auto it = stats_map.find(name);
136    if (it == stats_map.cend()) {
137        warn("Failed to find stat '%s'\n", name);
138        failed = true;
139        return 0;
140    }
141
142    const Info *info = it->second;
143
144    auto si = dynamic_cast<const ScalarInfo *>(info);
145    if (si)
146        return si->value();
147    auto fi = dynamic_cast<const FormulaInfo *>(info);
148    if (fi)
149        return fi->total();
150
151    panic("Unknown stat type!\n");
152}
153
154void
155MathExprPowerModel::regStats()
156{
157    PowerModelState::regStats();
158}
159
160MathExprPowerModel*
161MathExprPowerModelParams::create()
162{
163    return new MathExprPowerModel(this);
164}
165