mathexpr_powermodel.cc revision 13021
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.
369202Spalle@lyckegaard.dk *
379202Spalle@lyckegaard.dk * Authors: David Guillen Fandos
382064SN/A */
39360SN/A
40360SN/A#include "sim/power/mathexpr_powermodel.hh"
41360SN/A
42360SN/A#include <string>
43360SN/A
44360SN/A#include "base/statistics.hh"
451809SN/A#include "params/MathExprPowerModel.hh"
465543Ssaidi@eecs.umich.edu#include "sim/mathexpr.hh"
471809SN/A#include "sim/power/thermal_model.hh"
483113Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
498229Snate@binkert.org
508229Snate@binkert.orgMathExprPowerModel::MathExprPowerModel(const Params *p)
513113Sgblack@eecs.umich.edu    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
527075Snate@binkert.org{
538229Snate@binkert.org    // Calculate the name of the object we belong to
547075Snate@binkert.org    std::vector<std::string> path;
55360SN/A    tokenize(path, name(), '.', true);
562474SN/A    // It's something like xyz.power_model.pm2
575543Ssaidi@eecs.umich.edu    assert(path.size() > 2);
582462SN/A    for (unsigned i = 0; i < path.size() - 2; i++)
591354SN/A        basename += path[i] + ".";
606216Snate@binkert.org}
616658Snate@binkert.org
622474SN/Avoid
632680Sktlim@umich.eduMathExprPowerModel::startup()
648232Snate@binkert.org{
658229Snate@binkert.org    // Create a map with stats and pointers for quick access
668706Sandreas.hansson@arm.com    // Has to be done here, since we need access to the statsList
677678Sgblack@eecs.umich.edu    for (auto & i: Stats::statsList()) {
688229Snate@binkert.org        if (i->name.find(basename) == 0) {
698766Sgblack@eecs.umich.edu            // Add stats for this sim object and its child objects
706640Svince@csl.cornell.edu            stats_map[i->name.substr(basename.size())] = i;
71360SN/A        } else if (i->name.find(".") == std::string::npos) {
72360SN/A            // Add global stats (sim_seconds, for example)
73360SN/A            stats_map[i->name] = i;
74360SN/A        }
75360SN/A    }
76360SN/A
77360SN/A    tryEval(st_expr);
78360SN/A    const bool st_failed = failed;
79378SN/A
801450SN/A    tryEval(dyn_expr);
813114Sgblack@eecs.umich.edu    const bool dyn_failed = failed;
82360SN/A
835543Ssaidi@eecs.umich.edu    if (st_failed || dyn_failed) {
845543Ssaidi@eecs.umich.edu        const auto *p = dynamic_cast<const Params *>(params());
855543Ssaidi@eecs.umich.edu        assert(p);
86360SN/A
87360SN/A        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
88360SN/A              st_failed ? p->st : "",
89360SN/A              st_failed && dyn_failed ? "\n" : "",
90360SN/A              dyn_failed ? p->dyn : "");
912680Sktlim@umich.edu    }
92360SN/A}
93360SN/A
94360SN/Adouble
95360SN/AMathExprPowerModel::eval(const MathExpr &expr) const
96360SN/A{
97360SN/A    const double value = tryEval(expr);
98360SN/A
99360SN/A    // This shouldn't happen unless something went wrong the equations
100360SN/A    // were verified in startup().
101360SN/A    panic_if(failed, "Failed to evaluate power expression '%s'\n",
102360SN/A             expr.toStr());
1033114Sgblack@eecs.umich.edu
104360SN/A    return value;
105360SN/A}
106360SN/A
107360SN/Adouble
108360SN/AMathExprPowerModel::tryEval(const MathExpr &expr) const
109360SN/A{
110360SN/A    failed = false;
111360SN/A    const double value = expr.eval(
112360SN/A        std::bind(&MathExprPowerModel::getStatValue,
113360SN/A                  this, std::placeholders::_1)
114360SN/A        );
115360SN/A
116360SN/A    return value;
117360SN/A}
118360SN/A
119360SN/A
120360SN/Adouble
121360SN/AMathExprPowerModel::getStatValue(const std::string &name) const
122360SN/A{
123360SN/A    using namespace Stats;
124360SN/A
1258852Sandreas.hansson@arm.com    // Automatic variables:
126360SN/A    if (name == "temp") {
1278852Sandreas.hansson@arm.com        return _temp;
1285543Ssaidi@eecs.umich.edu    } else if (name == "voltage") {
129360SN/A        return clocked_object->voltage();
130360SN/A    } else if (name=="clock_period") {
131360SN/A        return clocked_object->clockPeriod();
132360SN/A    }
133360SN/A
1348852Sandreas.hansson@arm.com    // Try to cast the stat, only these are supported right now
135360SN/A    const auto it = stats_map.find(name);
1368852Sandreas.hansson@arm.com    if (it == stats_map.cend()) {
1375543Ssaidi@eecs.umich.edu        warn("Failed to find stat '%s'\n", name);
138360SN/A        failed = true;
139360SN/A        return 0;
140360SN/A    }
141360SN/A
142360SN/A    const Info *info = it->second;
143360SN/A
144360SN/A    auto si = dynamic_cast<const ScalarInfo *>(info);
145360SN/A    if (si)
146360SN/A        return si->value();
147360SN/A    auto fi = dynamic_cast<const FormulaInfo *>(info);
148360SN/A    if (fi)
149360SN/A        return fi->total();
150360SN/A
1515543Ssaidi@eecs.umich.edu    panic("Unknown stat type!\n");
152360SN/A}
153360SN/A
154360SN/Avoid
155360SN/AMathExprPowerModel::regStats()
156360SN/A{
157360SN/A    PowerModelState::regStats();
158360SN/A}
159360SN/A
160360SN/AMathExprPowerModel*
161360SN/AMathExprPowerModelParams::create()
162360SN/A{
163360SN/A    return new MathExprPowerModel(this);
164360SN/A}
165360SN/A