statistics.cc revision 5599:5bad83cddb8c
1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 */ 30 31#include <iomanip> 32#include <fstream> 33#include <list> 34#include <map> 35#include <string> 36 37#include "base/callback.hh" 38#include "base/cprintf.hh" 39#include "base/hostinfo.hh" 40#include "base/misc.hh" 41#include "base/statistics.hh" 42#include "base/str.hh" 43#include "base/time.hh" 44#include "base/trace.hh" 45#include "base/stats/statdb.hh" 46 47using namespace std; 48 49namespace Stats { 50 51StatData * 52DataAccess::find() const 53{ 54 return Database::find(const_cast<void *>((const void *)this)); 55} 56 57const StatData * 58getStatData(const void *stat) 59{ 60 return Database::find(const_cast<void *>(stat)); 61} 62 63void 64DataAccess::map(StatData *data) 65{ 66 Database::regStat(this, data); 67} 68 69StatData * 70DataAccess::statData() 71{ 72 StatData *ptr = find(); 73 assert(ptr); 74 return ptr; 75} 76 77const StatData * 78DataAccess::statData() const 79{ 80 const StatData *ptr = find(); 81 assert(ptr); 82 return ptr; 83} 84 85void 86DataAccess::setInit() 87{ 88 statData()->flags |= init; 89} 90 91void 92DataAccess::setPrint() 93{ 94 Database::regPrint(this); 95} 96 97StatData::StatData() 98 : flags(none), precision(-1), prereq(0) 99{ 100 static int count = 0; 101 id = count++; 102} 103 104StatData::~StatData() 105{ 106} 107 108bool 109StatData::less(StatData *stat1, StatData *stat2) 110{ 111 const string &name1 = stat1->name; 112 const string &name2 = stat2->name; 113 114 vector<string> v1; 115 vector<string> v2; 116 117 tokenize(v1, name1, '.'); 118 tokenize(v2, name2, '.'); 119 120 size_type last = min(v1.size(), v2.size()) - 1; 121 for (off_type i = 0; i < last; ++i) 122 if (v1[i] != v2[i]) 123 return v1[i] < v2[i]; 124 125 // Special compare for last element. 126 if (v1[last] == v2[last]) 127 return v1.size() < v2.size(); 128 else 129 return v1[last] < v2[last]; 130 131 return false; 132} 133 134bool 135StatData::baseCheck() const 136{ 137 if (!(flags & init)) { 138#ifdef DEBUG 139 cprintf("this is stat number %d\n", id); 140#endif 141 panic("Not all stats have been initialized"); 142 return false; 143 } 144 145 if ((flags & print) && name.empty()) { 146 panic("all printable stats must be named"); 147 return false; 148 } 149 150 return true; 151} 152 153 154void 155FormulaBase::result(VResult &vec) const 156{ 157 if (root) 158 vec = root->result(); 159} 160 161Result 162FormulaBase::total() const 163{ 164 return root ? root->total() : 0.0; 165} 166 167size_type 168FormulaBase::size() const 169{ 170 if (!root) 171 return 0; 172 else 173 return root->size(); 174} 175 176void 177FormulaBase::reset() 178{ 179} 180 181bool 182FormulaBase::zero() const 183{ 184 VResult vec; 185 result(vec); 186 for (off_t i = 0; i < vec.size(); ++i) 187 if (vec[i] != 0.0) 188 return false; 189 return true; 190} 191 192void 193FormulaBase::update(StatData *) 194{ 195} 196 197string 198FormulaBase::str() const 199{ 200 return root ? root->str() : ""; 201} 202 203Formula::Formula() 204{ 205 setInit(); 206} 207 208Formula::Formula(Temp r) 209{ 210 root = r; 211 assert(size()); 212} 213 214const Formula & 215Formula::operator=(Temp r) 216{ 217 assert(!root && "Can't change formulas"); 218 root = r; 219 assert(size()); 220 return *this; 221} 222 223const Formula & 224Formula::operator+=(Temp r) 225{ 226 if (root) 227 root = NodePtr(new BinaryNode<std::plus<Result> >(root, r)); 228 else 229 root = r; 230 assert(size()); 231 return *this; 232} 233 234void 235check() 236{ 237 typedef Database::stat_list_t::iterator iter_t; 238 239 iter_t i, end = Database::stats().end(); 240 for (i = Database::stats().begin(); i != end; ++i) { 241 StatData *data = *i; 242 assert(data); 243 if (!data->check() || !data->baseCheck()) 244 panic("stat check failed for %s\n", data->name); 245 } 246 247 off_t j = 0; 248 for (i = Database::stats().begin(); i != end; ++i) { 249 StatData *data = *i; 250 if (!(data->flags & print)) 251 data->name = "__Stat" + to_string(j++); 252 } 253 254 Database::stats().sort(StatData::less); 255 256 if (i == end) 257 return; 258 259 iter_t last = i; 260 ++i; 261 262 for (i = Database::stats().begin(); i != end; ++i) { 263 if ((*i)->name == (*last)->name) 264 panic("same name used twice! name=%s\n", (*i)->name); 265 266 last = i; 267 } 268} 269 270CallbackQueue resetQueue; 271 272void 273reset() 274{ 275 Database::stat_list_t::iterator i = Database::stats().begin(); 276 Database::stat_list_t::iterator end = Database::stats().end(); 277 while (i != end) { 278 StatData *data = *i; 279 data->reset(); 280 ++i; 281 } 282 283 resetQueue.process(); 284} 285 286void 287registerResetCallback(Callback *cb) 288{ 289 resetQueue.add(cb); 290} 291 292/* namespace Stats */ } 293