📄 text.cc
字号:
/* * Copyright (c) 2004, 2005 * The Regents of The University of Michigan * All Rights Reserved * * This code is part of the M5 simulator, developed by Nathan Binkert, * Erik Hallnor, Steve Raasch, and Steve Reinhardt, with contributions * from Ron Dreslinski, Dave Greene, Lisa Hsu, Kevin Lim, Ali Saidi, * and Andrew Schultz. * * Permission is granted to use, copy, create derivative works and * redistribute this software and such derivative works for any * purpose, so long as the copyright notice above, this grant of * permission, and the disclaimer below appear in all copies made; and * so long as the name of The University of Michigan is not used in * any advertising or publicity pertaining to the use or distribution * of this software without specific, written prior authorization. * * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT, * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH * DAMAGES. */#if defined(__APPLE__)#define _GLIBCPP_USE_C99 1#endif#include <iostream>#include <fstream>#include <string>#include "base/misc.hh"#include "base/statistics.hh"#include "base/stats/statdb.hh"#include "base/stats/text.hh"#include "base/stats/visit.hh"using namespace std;#ifndef NANfloat __nan();/** Define Not a number. */#define NAN (__nan())/** Need to define __nan() */#define __M5_NAN#endif#ifdef __M5_NANfloat__nan(){ union { uint32_t ui; float f; } nan; nan.ui = 0x7fc00000; return nan.f;}#endifnamespace Stats {Text::Text() : mystream(false), stream(NULL), compat(false), descriptions(false){}Text::Text(std::ostream &stream) : mystream(false), stream(NULL), compat(false), descriptions(false){ open(stream);}Text::Text(const std::string &file) : mystream(false), stream(NULL), compat(false), descriptions(false){ open(file);}Text::~Text(){ if (mystream) { assert(stream); delete stream; }}voidText::open(std::ostream &_stream){ if (stream) panic("stream already set!"); mystream = false; stream = &_stream; assert(valid());}voidText::open(const std::string &file){ if (stream) panic("stream already set!"); mystream = true; stream = new ofstream(file.c_str(), ios::trunc); assert(valid());}boolText::valid() const{ return stream != NULL;}voidText::output(){ using namespace Database; ccprintf(*stream, "\n---------- Begin Simulation Statistics ----------\n"); if (bins().empty() || bins().size() == 1) { stat_list_t::const_iterator i, end = stats().end(); for (i = stats().begin(); i != end; ++i) (*i)->visit(*this); } else { ccprintf(*stream, "PRINTING BINNED STATS\n"); bin_list_t::iterator i, end = bins().end(); for (i = bins().begin(); i != end; ++i) { MainBin *bin = *i; bin->activate(); ccprintf(*stream,"---%s Bin------------\n", bin->name()); stat_list_t::const_iterator i, end = stats().end(); for (i = stats().begin(); i != end; ++i) (*i)->visit(*this); ccprintf(*stream, "---------------------------------\n"); } } ccprintf(*stream, "\n---------- End Simulation Statistics ----------\n"); stream->flush();}boolText::noOutput(const StatData &data){ if (!(data.flags & print)) return true; if (data.prereq && data.prereq->zero()) return true; return false;}stringValueToString(Result value, int precision, bool compat){ stringstream val; if (!isnan(value)) { if (precision != -1) val.precision(precision); else if (value == rint(value)) val.precision(0); val.unsetf(ios::showpoint); val.setf(ios::fixed); val << value; } else { val << (compat ? "<err: div-0>" : "no value"); } return val.str();}struct ScalarPrint{ Result value; string name; string desc; StatFlags flags; bool compat; bool descriptions; int precision; Result pdf; Result cdf; void operator()(ostream &stream) const;};voidScalarPrint::operator()(ostream &stream) const{ if (flags & nozero && value == 0.0 || flags & nonan && isnan(value)) return; stringstream pdfstr, cdfstr; if (!isnan(pdf)) ccprintf(pdfstr, "%.2f%%", pdf * 100.0); if (!isnan(cdf)) ccprintf(cdfstr, "%.2f%%", cdf * 100.0); if (compat && flags & __substat) { ccprintf(stream, "%32s %12s %10s %10s", name, ValueToString(value, precision, compat), pdfstr, cdfstr); } else { ccprintf(stream, "%-40s %12s %10s %10s", name, ValueToString(value, precision, compat), pdfstr, cdfstr); } if (descriptions) { if (!desc.empty()) ccprintf(stream, " # %s", desc); } stream << endl;}struct VectorPrint{ string name; string desc; vector<string> subnames; vector<string> subdescs; StatFlags flags; bool compat; bool descriptions; int precision; VResult vec; Result total; void operator()(ostream &stream) const;};voidVectorPrint::operator()(std::ostream &stream) const{ int _size = vec.size(); Result _total = 0.0; if (flags & (pdf | cdf)) { for (int i = 0; i < _size; ++i) { _total += vec[i]; } } string base = name + (compat ? "_" : "::"); ScalarPrint print; print.name = name; print.desc = desc; print.precision = precision; print.descriptions = descriptions; print.flags = flags; print.pdf = NAN; print.cdf = NAN; bool havesub = !subnames.empty(); if (_size == 1) { print.value = vec[0]; print(stream); } else if (!compat) { for (int i = 0; i < _size; ++i) { if (havesub && (i >= subnames.size() || subnames[i].empty())) continue; print.name = base + (havesub ? subnames[i] : to_string(i)); print.desc = subdescs.empty() ? desc : subdescs[i]; print.value = vec[i]; if (_total && (flags & pdf)) { print.pdf = vec[i] / _total; print.cdf += print.pdf; } print(stream); } if (flags & ::Stats::total) { print.name = base + "total"; print.desc = desc; print.value = total; print(stream); } } else { if (flags & ::Stats::total) { print.value = total; print(stream); } Result _pdf = 0.0; Result _cdf = 0.0; if (flags & dist) { ccprintf(stream, "%s.start_dist\n", name); for (int i = 0; i < _size; ++i) { print.name = havesub ? subnames[i] : to_string(i); print.desc = subdescs.empty() ? desc : subdescs[i]; print.flags |= __substat; print.value = vec[i]; if (_total) { _pdf = vec[i] / _total; _cdf += _pdf; } if (flags & pdf) print.pdf = _pdf; if (flags & cdf) print.cdf = _cdf; print(stream); } ccprintf(stream, "%s.end_dist\n", name); } else { for (int i = 0; i < _size; ++i) { if (havesub && subnames[i].empty()) continue; print.name = base; print.name += havesub ? subnames[i] : to_string(i); print.desc = subdescs.empty() ? desc : subdescs[i]; print.value = vec[i]; if (_total) { _pdf = vec[i] / _total; _cdf += _pdf; } else { _pdf = _cdf = NAN; } if (flags & pdf) { print.pdf = _pdf; print.cdf = _cdf; } print(stream); } } }}struct DistPrint{ string name; string desc; StatFlags flags; bool compat; bool descriptions; int precision; Result min_val; Result max_val; Result underflow; Result overflow; VResult vec; Result sum; Result squares;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -