1/*
2 * Copyright (c) 2016-2017 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: David Guillen Fandos
38 */
39
40#include "sim/power/mathexpr_powermodel.hh"
41
42#include <string>
43
44#include "base/statistics.hh"
45#include "params/MathExprPowerModel.hh"
46#include "sim/mathexpr.hh"
47#include "sim/power/thermal_model.hh"
48#include "sim/sim_object.hh"
49
50MathExprPowerModel::MathExprPowerModel(const Params *p)
51    : PowerModelState(p), dyn_expr(p->dyn), st_expr(p->st), failed(false)
52{
53    // Calculate the name of the object we belong to
54    std::vector<std::string> path;
55    tokenize(path, name(), '.', true);
56    // It's something like xyz.power_model.pm2
57    assert(path.size() > 2);
58    for (unsigned i = 0; i < path.size() - 2; i++)
59        basename += path[i] + ".";
60}
61
62void
63MathExprPowerModel::startup()
64{
65    // Create a map with stats and pointers for quick access
66    // Has to be done here, since we need access to the statsList
67    for (auto & i: Stats::statsList()) {
68        if (i->name.find(basename) == 0) {
69            // Add stats for this sim object and its child objects
70            stats_map[i->name.substr(basename.size())] = i;
71        } else if (i->name.find(".") == std::string::npos) {
72            // Add global stats (sim_seconds, for example)
73            stats_map[i->name] = i;
74        }
75    }
76
77    tryEval(st_expr);
78    const bool st_failed = failed;
79
80    tryEval(dyn_expr);
81    const bool dyn_failed = failed;
82
83    if (st_failed || dyn_failed) {
84        const auto *p = dynamic_cast<const Params *>(params());
85        assert(p);
86
87        fatal("Failed to evaluate power expressions:\n%s%s%s\n",
88              st_failed ? p->st : "",
89              st_failed && dyn_failed ? "\n" : "",
90              dyn_failed ? p->dyn : "");
91    }
92}
93
94double
95MathExprPowerModel::eval(const MathExpr &expr) const
96{
97    const double value = tryEval(expr);
98
99    // This shouldn't happen unless something went wrong the equations
100    // were verified in startup().
101    panic_if(failed, "Failed to evaluate power expression '%s'\n",
102             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