mathexpr_powermodel.cc revision 11968
1360SN/A/*
21458SN/A * Copyright (c) 2016-2017 ARM Limited
3360SN/A * All rights reserved
4360SN/A *
5360SN/A * The license below extends only to copyright in the software and shall
6360SN/A * not be construed as granting a license to any other intellectual
7360SN/A * property including but not limited to intellectual property relating
8360SN/A * to a hardware implementation of the functionality of the software
9360SN/A * licensed hereunder.  You may use the software subject to the license
10360SN/A * terms below provided that you ensure that this notice is replicated
11360SN/A * unmodified and in its entirety in all distributions of the software,
12360SN/A * modified or unmodified, in source code or in binary form.
13360SN/A *
14360SN/A * Redistribution and use in source and binary forms, with or without
15360SN/A * modification, are permitted provided that the following conditions are
16360SN/A * met: redistributions of source code must retain the above copyright
17360SN/A * notice, this list of conditions and the following disclaimer;
18360SN/A * redistributions in binary form must reproduce the above copyright
19360SN/A * notice, this list of conditions and the following disclaimer in the
20360SN/A * documentation and/or other materials provided with the distribution;
21360SN/A * neither the name of the copyright holders nor the names of its
22360SN/A * contributors may be used to endorse or promote products derived from
23360SN/A * this software without specific prior written permission.
24360SN/A *
25360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272665Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282665Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292665Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
321354SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
331354SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
352764Sstever@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362764Sstever@eecs.umich.edu *
372064SN/A * Authors: David Guillen Fandos
38360SN/A */
39360SN/A
40360SN/A#include "sim/power/mathexpr_powermodel.hh"
41360SN/A
42360SN/A#include "base/statistics.hh"
43360SN/A#include "params/MathExprPowerModel.hh"
441354SN/A#include "sim/mathexpr.hh"
45360SN/A#include "sim/power/thermal_model.hh"
461809SN/A#include "sim/sim_object.hh"
471809SN/A
481809SN/AMathExprPowerModel::MathExprPowerModel(const Params *p)
493113Sgblack@eecs.umich.edu    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
503113Sgblack@eecs.umich.edu{
511999SN/A    // Calculate the name of the object we belong to
52360SN/A    std::vector<std::string> path;
533113Sgblack@eecs.umich.edu    tokenize(path, name(), '.', true);
542474SN/A    // It's something like xyz.power_model.pm2
55360SN/A    assert(path.size() > 2);
562462SN/A    for (unsigned i = 0; i < path.size() - 2; i++)
571354SN/A        basename += path[i] + ".";
582474SN/A}
592680Sktlim@umich.edu
602474SN/Avoid
612474SN/AMathExprPowerModel::startup()
621354SN/A{
63360SN/A    // Create a map with stats and pointers for quick access
64360SN/A    // Has to be done here, since we need access to the statsList
65360SN/A    for (auto & i: Stats::statsList())
66360SN/A        if (i->name.find(basename) == 0)
67360SN/A            stats_map[i->name.substr(basename.size())] = i;
68360SN/A
69360SN/A    tryEval(st_expr);
70360SN/A    const bool st_failed = failed;
71378SN/A
721450SN/A    tryEval(dyn_expr);
733114Sgblack@eecs.umich.edu    const bool dyn_failed = failed;
74360SN/A
75360SN/A    if (st_failed || dyn_failed) {
76360SN/A        const auto *p = dynamic_cast<const Params *>(params());
77360SN/A        assert(p);
78360SN/A
79360SN/A        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
80360SN/A              st_failed ? p->st : "",
81360SN/A              st_failed && dyn_failed ? "\n" : "",
82360SN/A              dyn_failed ? p->dyn : "");
832680Sktlim@umich.edu    }
84360SN/A}
85360SN/A
86360SN/Adouble
87360SN/AMathExprPowerModel::eval(const MathExpr &expr) const
88360SN/A{
89360SN/A    const double value = tryEval(expr);
90360SN/A
91360SN/A    // This shouldn't happen unless something went wrong the equations
92360SN/A    // were verified in startup().
93360SN/A    panic_if(failed, "Failed to evaluate power expression '%s'\n",
94360SN/A             expr.toStr());
953114Sgblack@eecs.umich.edu
96360SN/A    return value;
97360SN/A}
98360SN/A
99360SN/Adouble
100360SN/AMathExprPowerModel::tryEval(const MathExpr &expr) const
101360SN/A{
102360SN/A    failed = false;
103360SN/A    const double value = expr.eval(
104360SN/A        std::bind(&MathExprPowerModel::getStatValue,
105360SN/A                  this, std::placeholders::_1)
106360SN/A        );
107360SN/A
108360SN/A    return value;
109360SN/A}
110360SN/A
111360SN/A
112360SN/Adouble
113360SN/AMathExprPowerModel::getStatValue(const std::string &name) const
114360SN/A{
115360SN/A    using namespace Stats;
116360SN/A
1172400SN/A    // Automatic variables:
118360SN/A    if (name == "temp") {
1192461SN/A        return _temp;
120360SN/A    } else if (name == "voltage") {
121360SN/A        return clocked_object->voltage();
122360SN/A    }
123360SN/A
124360SN/A    // Try to cast the stat, only these are supported right now
125360SN/A    const auto it = stats_map.find(name);
1262400SN/A    if (it == stats_map.cend()) {
127360SN/A        warn("Failed to find stat '%s'\n", name);
1282461SN/A        failed = true;
129360SN/A        return 0;
130360SN/A    }
131360SN/A
132360SN/A    const Info *info = it->second;
133360SN/A
134360SN/A    auto si = dynamic_cast<const ScalarInfo *>(info);
135360SN/A    if (si)
136360SN/A        return si->value();
137360SN/A    auto fi = dynamic_cast<const FormulaInfo *>(info);
138360SN/A    if (fi)
139360SN/A        return fi->total();
140360SN/A
141360SN/A    panic("Unknown stat type!\n");
142360SN/A}
143360SN/A
144360SN/Avoid
145360SN/AMathExprPowerModel::regStats()
146360SN/A{
147360SN/A    PowerModelState::regStats();
148360SN/A}
149360SN/A
150360SN/AMathExprPowerModel*
151360SN/AMathExprPowerModelParams::create()
152360SN/A{
153360SN/A    return new MathExprPowerModel(this);
154360SN/A}
155360SN/A