statistics.cc revision 9857:8301f882a916
112955Sgabeblack@google.com/* 212955Sgabeblack@google.com * Copyright (c) 2003-2005 The Regents of The University of Michigan 312955Sgabeblack@google.com * All rights reserved. 412955Sgabeblack@google.com * 512955Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 612955Sgabeblack@google.com * modification, are permitted provided that the following conditions are 712955Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 812955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 912955Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1012955Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1112955Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1212955Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1312955Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1412955Sgabeblack@google.com * this software without specific prior written permission. 1512955Sgabeblack@google.com * 1612955Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712955Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812955Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912955Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012955Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112955Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212955Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312955Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412955Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512955Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612955Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712955Sgabeblack@google.com * 2812955Sgabeblack@google.com * Authors: Nathan Binkert 2912955Sgabeblack@google.com */ 3012955Sgabeblack@google.com 3112955Sgabeblack@google.com#include <fstream> 3212955Sgabeblack@google.com#include <iomanip> 3313193Sgabeblack@google.com#include <list> 3412955Sgabeblack@google.com#include <map> 3512955Sgabeblack@google.com#include <string> 3612955Sgabeblack@google.com 3712957Sgabeblack@google.com#include "base/callback.hh" 3812955Sgabeblack@google.com#include "base/cprintf.hh" 3912955Sgabeblack@google.com#include "base/debug.hh" 4013063Sgabeblack@google.com#include "base/hostinfo.hh" 4112955Sgabeblack@google.com#include "base/misc.hh" 4212955Sgabeblack@google.com#include "base/statistics.hh" 4312955Sgabeblack@google.com#include "base/str.hh" 4412955Sgabeblack@google.com#include "base/time.hh" 4512955Sgabeblack@google.com#include "base/trace.hh" 4612955Sgabeblack@google.com 4712955Sgabeblack@google.comusing namespace std; 4812955Sgabeblack@google.com 4912955Sgabeblack@google.comnamespace Stats { 5012955Sgabeblack@google.com 5112955Sgabeblack@google.comstd::string Info::separatorString = "::"; 5212955Sgabeblack@google.com 5312955Sgabeblack@google.com// We wrap these in a function to make sure they're built in time. 5412955Sgabeblack@google.comlist<Info *> & 5512955Sgabeblack@google.comstatsList() 5612957Sgabeblack@google.com{ 5712957Sgabeblack@google.com static list<Info *> the_list; 5812955Sgabeblack@google.com return the_list; 5912955Sgabeblack@google.com} 6012955Sgabeblack@google.com 6112955Sgabeblack@google.comMapType & 6212955Sgabeblack@google.comstatsMap() 6312955Sgabeblack@google.com{ 6412955Sgabeblack@google.com static MapType the_map; 6512955Sgabeblack@google.com return the_map; 6612955Sgabeblack@google.com} 6712955Sgabeblack@google.com 6812955Sgabeblack@google.comvoid 6912955Sgabeblack@google.comInfoAccess::setInfo(Info *info) 7012955Sgabeblack@google.com{ 7112955Sgabeblack@google.com if (statsMap().find(this) != statsMap().end()) 7212955Sgabeblack@google.com panic("shouldn't register stat twice!"); 7312955Sgabeblack@google.com 7412955Sgabeblack@google.com statsList().push_back(info); 7512955Sgabeblack@google.com 7612955Sgabeblack@google.com#ifndef NDEBUG 7712955Sgabeblack@google.com pair<MapType::iterator, bool> result = 7812955Sgabeblack@google.com#endif 7912955Sgabeblack@google.com statsMap().insert(make_pair(this, info)); 8012955Sgabeblack@google.com assert(result.second && "this should never fail"); 8112955Sgabeblack@google.com assert(statsMap().find(this) != statsMap().end()); 8212955Sgabeblack@google.com} 8312955Sgabeblack@google.com 8412955Sgabeblack@google.comvoid 8512955Sgabeblack@google.comInfoAccess::setParams(const StorageParams *params) 8612955Sgabeblack@google.com{ 8712955Sgabeblack@google.com info()->storageParams = params; 8812955Sgabeblack@google.com} 8912955Sgabeblack@google.com 9012955Sgabeblack@google.comvoid 9112955Sgabeblack@google.comInfoAccess::setInit() 9212955Sgabeblack@google.com{ 9312955Sgabeblack@google.com info()->flags.set(init); 9412955Sgabeblack@google.com} 9512955Sgabeblack@google.com 9613193Sgabeblack@google.comInfo * 9713193Sgabeblack@google.comInfoAccess::info() 9812957Sgabeblack@google.com{ 9912955Sgabeblack@google.com MapType::const_iterator i = statsMap().find(this); 10012955Sgabeblack@google.com assert(i != statsMap().end()); 10112955Sgabeblack@google.com return (*i).second; 10212955Sgabeblack@google.com} 10312955Sgabeblack@google.com 10412955Sgabeblack@google.comconst Info * 10512955Sgabeblack@google.comInfoAccess::info() const 10612955Sgabeblack@google.com{ 10712957Sgabeblack@google.com MapType::const_iterator i = statsMap().find(this); 10813063Sgabeblack@google.com assert(i != statsMap().end()); 10912957Sgabeblack@google.com return (*i).second; 11013193Sgabeblack@google.com} 11112955Sgabeblack@google.com 11212955Sgabeblack@google.comStorageParams::~StorageParams() 11312955Sgabeblack@google.com{ 11412955Sgabeblack@google.com} 11512955Sgabeblack@google.com 11612955Sgabeblack@google.comNameMapType & 11712955Sgabeblack@google.comnameMap() 11812955Sgabeblack@google.com{ 11912955Sgabeblack@google.com static NameMapType the_map; 12012955Sgabeblack@google.com return the_map; 121} 122 123int Info::id_count = 0; 124 125int debug_break_id = -1; 126 127Info::Info() 128 : flags(none), precision(-1), prereq(0), storageParams(NULL) 129{ 130 id = id_count++; 131 if (debug_break_id >= 0 and debug_break_id == id) 132 Debug::breakpoint(); 133} 134 135Info::~Info() 136{ 137} 138 139bool 140validateStatName(const string &name) 141{ 142 if (name.empty()) 143 return false; 144 145 vector<string> vec; 146 tokenize(vec, name, '.'); 147 vector<string>::const_iterator item = vec.begin(); 148 while (item != vec.end()) { 149 if (item->empty()) 150 return false; 151 152 string::const_iterator c = item->begin(); 153 154 // The first character is different 155 if (!isalpha(*c) && *c != '_') 156 return false; 157 158 // The rest of the characters have different rules. 159 while (++c != item->end()) { 160 if (!isalnum(*c) && *c != '_') 161 return false; 162 } 163 164 ++item; 165 } 166 167 return true; 168} 169 170void 171Info::setName(const string &name) 172{ 173 if (!validateStatName(name)) 174 panic("invalid stat name '%s'", name); 175 176 pair<NameMapType::iterator, bool> p = 177 nameMap().insert(make_pair(name, this)); 178 179 Info *other = p.first->second; 180 bool result = p.second; 181 182 if (!result) { 183 // using other->name instead of just name to avoid a compiler 184 // warning. They should be the same. 185 panic("same statistic name used twice! name=%s\n", other->name); 186 } 187 188 this->name = name; 189} 190 191bool 192Info::less(Info *stat1, Info *stat2) 193{ 194 const string &name1 = stat1->name; 195 const string &name2 = stat2->name; 196 197 vector<string> v1; 198 vector<string> v2; 199 200 tokenize(v1, name1, '.'); 201 tokenize(v2, name2, '.'); 202 203 size_type last = min(v1.size(), v2.size()) - 1; 204 for (off_type i = 0; i < last; ++i) 205 if (v1[i] != v2[i]) 206 return v1[i] < v2[i]; 207 208 // Special compare for last element. 209 if (v1[last] == v2[last]) 210 return v1.size() < v2.size(); 211 else 212 return v1[last] < v2[last]; 213 214 return false; 215} 216 217bool 218Info::baseCheck() const 219{ 220 if (!(flags & Stats::init)) { 221#ifdef DEBUG 222 cprintf("this is stat number %d\n", id); 223#endif 224 panic("Not all stats have been initialized"); 225 return false; 226 } 227 228 if ((flags & display) && name.empty()) { 229 panic("all printable stats must be named"); 230 return false; 231 } 232 233 return true; 234} 235 236void 237Info::enable() 238{ 239} 240 241void 242VectorInfo::enable() 243{ 244 size_type s = size(); 245 if (subnames.size() < s) 246 subnames.resize(s); 247 if (subdescs.size() < s) 248 subdescs.resize(s); 249} 250 251void 252VectorDistInfo::enable() 253{ 254 size_type s = size(); 255 if (subnames.size() < s) 256 subnames.resize(s); 257 if (subdescs.size() < s) 258 subdescs.resize(s); 259} 260 261void 262Vector2dInfo::enable() 263{ 264 if (subnames.size() < x) 265 subnames.resize(x); 266 if (subdescs.size() < x) 267 subdescs.resize(x); 268 if (y_subnames.size() < y) 269 y_subnames.resize(y); 270} 271 272void 273HistStor::grow_out() 274{ 275 int size = cvec.size(); 276 int zero = size / 2; // round down! 277 int top_half = zero + (size - zero + 1) / 2; // round up! 278 int bottom_half = (size - zero) / 2; // round down! 279 280 // grow down 281 int low_pair = zero - 1; 282 for (int i = zero - 1; i >= bottom_half; i--) { 283 cvec[i] = cvec[low_pair]; 284 if (low_pair - 1 >= 0) 285 cvec[i] += cvec[low_pair - 1]; 286 low_pair -= 2; 287 } 288 assert(low_pair == 0 || low_pair == -1 || low_pair == -2); 289 290 for (int i = bottom_half - 1; i >= 0; i--) 291 cvec[i] = Counter(); 292 293 // grow up 294 int high_pair = zero; 295 for (int i = zero; i < top_half; i++) { 296 cvec[i] = cvec[high_pair]; 297 if (high_pair + 1 < size) 298 cvec[i] += cvec[high_pair + 1]; 299 high_pair += 2; 300 } 301 assert(high_pair == size || high_pair == size + 1); 302 303 for (int i = top_half; i < size; i++) 304 cvec[i] = Counter(); 305 306 max_bucket *= 2; 307 min_bucket *= 2; 308 bucket_size *= 2; 309} 310 311void 312HistStor::grow_convert() 313{ 314 int size = cvec.size(); 315 int half = (size + 1) / 2; // round up! 316 //bool even = (size & 1) == 0; 317 318 int pair = size - 1; 319 for (int i = size - 1; i >= half; --i) { 320 cvec[i] = cvec[pair]; 321 if (pair - 1 >= 0) 322 cvec[i] += cvec[pair - 1]; 323 pair -= 2; 324 } 325 326 for (int i = half - 1; i >= 0; i--) 327 cvec[i] = Counter(); 328 329 min_bucket = -max_bucket;// - (even ? bucket_size : 0); 330 bucket_size *= 2; 331} 332 333void 334HistStor::grow_up() 335{ 336 int size = cvec.size(); 337 int half = (size + 1) / 2; // round up! 338 339 int pair = 0; 340 for (int i = 0; i < half; i++) { 341 cvec[i] = cvec[pair]; 342 if (pair + 1 < size) 343 cvec[i] += cvec[pair + 1]; 344 pair += 2; 345 } 346 assert(pair == size || pair == size + 1); 347 348 for (int i = half; i < size; i++) 349 cvec[i] = Counter(); 350 351 max_bucket *= 2; 352 bucket_size *= 2; 353} 354 355Formula::Formula() 356{ 357} 358 359Formula::Formula(Temp r) 360{ 361 root = r; 362 setInit(); 363 assert(size()); 364} 365 366const Formula & 367Formula::operator=(Temp r) 368{ 369 assert(!root && "Can't change formulas"); 370 root = r; 371 setInit(); 372 assert(size()); 373 return *this; 374} 375 376const Formula & 377Formula::operator+=(Temp r) 378{ 379 if (root) 380 root = NodePtr(new BinaryNode<std::plus<Result> >(root, r)); 381 else { 382 root = r; 383 setInit(); 384 } 385 386 assert(size()); 387 return *this; 388} 389 390const Formula & 391Formula::operator/=(Temp r) 392{ 393 assert (root); 394 root = NodePtr(new BinaryNode<std::divides<Result> >(root, r)); 395 396 assert(size()); 397 return *this; 398} 399 400void 401Formula::result(VResult &vec) const 402{ 403 if (root) 404 vec = root->result(); 405} 406 407Result 408Formula::total() const 409{ 410 return root ? root->total() : 0.0; 411} 412 413size_type 414Formula::size() const 415{ 416 if (!root) 417 return 0; 418 else 419 return root->size(); 420} 421 422void 423Formula::reset() 424{ 425} 426 427bool 428Formula::zero() const 429{ 430 VResult vec; 431 result(vec); 432 for (VResult::size_type i = 0; i < vec.size(); ++i) 433 if (vec[i] != 0.0) 434 return false; 435 return true; 436} 437 438string 439Formula::str() const 440{ 441 return root ? root->str() : ""; 442} 443 444CallbackQueue dumpQueue; 445CallbackQueue resetQueue; 446 447void 448registerResetCallback(Callback *cb) 449{ 450 resetQueue.add(cb); 451} 452 453bool _enabled = false; 454 455bool 456enabled() 457{ 458 return _enabled; 459} 460 461void 462enable() 463{ 464 if (_enabled) 465 fatal("Stats are already enabled"); 466 467 _enabled = true; 468} 469 470void 471registerDumpCallback(Callback *cb) 472{ 473 dumpQueue.add(cb); 474} 475 476} // namespace Stats 477 478void 479debugDumpStats() 480{ 481 Stats::dump(); 482} 483