mathexpr_powermodel.cc revision 13021
111731Sjason@lowepower.com/* 211731Sjason@lowepower.com * Copyright (c) 2016-2017 ARM Limited 311731Sjason@lowepower.com * All rights reserved 411731Sjason@lowepower.com * 511731Sjason@lowepower.com * The license below extends only to copyright in the software and shall 612137Sar4jc@virginia.edu * not be construed as granting a license to any other intellectual 712137Sar4jc@virginia.edu * property including but not limited to intellectual property relating 812137Sar4jc@virginia.edu * to a hardware implementation of the functionality of the software 912137Sar4jc@virginia.edu * licensed hereunder. You may use the software subject to the license 1011731Sjason@lowepower.com * terms below provided that you ensure that this notice is replicated 1111731Sjason@lowepower.com * unmodified and in its entirety in all distributions of the software, 1211731Sjason@lowepower.com * modified or unmodified, in source code or in binary form. 1311731Sjason@lowepower.com * 1411731Sjason@lowepower.com * Redistribution and use in source and binary forms, with or without 1511731Sjason@lowepower.com * modification, are permitted provided that the following conditions are 1611731Sjason@lowepower.com * met: redistributions of source code must retain the above copyright 1711731Sjason@lowepower.com * notice, this list of conditions and the following disclaimer; 1811731Sjason@lowepower.com * redistributions in binary form must reproduce the above copyright 1911731Sjason@lowepower.com * notice, this list of conditions and the following disclaimer in the 2011731Sjason@lowepower.com * documentation and/or other materials provided with the distribution; 2111731Sjason@lowepower.com * neither the name of the copyright holders nor the names of its 2211731Sjason@lowepower.com * contributors may be used to endorse or promote products derived from 2311731Sjason@lowepower.com * this software without specific prior written permission. 2411731Sjason@lowepower.com * 2511731Sjason@lowepower.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2611731Sjason@lowepower.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2711731Sjason@lowepower.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2811731Sjason@lowepower.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2911731Sjason@lowepower.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3011731Sjason@lowepower.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3111731Sjason@lowepower.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3211731Sjason@lowepower.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3311731Sjason@lowepower.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3411731Sjason@lowepower.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3511731Sjason@lowepower.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3611731Sjason@lowepower.com * 3711731Sjason@lowepower.com * Authors: David Guillen Fandos 3811731Sjason@lowepower.com */ 3911731Sjason@lowepower.com 4011731Sjason@lowepower.com#include "sim/power/mathexpr_powermodel.hh" 4111731Sjason@lowepower.com 4211731Sjason@lowepower.com#include <string> 4311731Sjason@lowepower.com 4411731Sjason@lowepower.com#include "base/statistics.hh" 4511731Sjason@lowepower.com#include "params/MathExprPowerModel.hh" 4611731Sjason@lowepower.com#include "sim/mathexpr.hh" 4712137Sar4jc@virginia.edu#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