text.cc (12334:e0ab29a34764) text.cc (14205:197360deaa20)
1/*
1/*
2 * Copyright (c) 2019 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 *
2 * Copyright (c) 2004-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#if defined(__APPLE__)
32#define _GLIBCPP_USE_C99 1
33#endif
34
35#if defined(__sun)
36#include <cmath>
37
38#endif
39
40#include <cassert>
41
42#ifdef __SUNPRO_CC
43#include <cmath>
44
45#endif
46#include "base/stats/text.hh"
47
48#include <cmath>
49#include <fstream>
50#include <iostream>
51#include <sstream>
52#include <string>
53
54#include "base/cast.hh"
55#include "base/logging.hh"
56#include "base/stats/info.hh"
57#include "base/str.hh"
58
59using namespace std;
60
61#ifndef NAN
62float __nan();
63/** Define Not a number. */
64#define NAN (__nan())
65/** Need to define __nan() */
66#define __M5_NAN
67#endif
68
69#ifdef __M5_NAN
70float
71__nan()
72{
73 union {
74 uint32_t ui;
75 float f;
76 } nan;
77
78 nan.ui = 0x7fc00000;
79 return nan.f;
80}
81#endif
82
83namespace Stats {
84
85std::list<Info *> &statsList();
86
87Text::Text()
88 : mystream(false), stream(NULL), descriptions(false)
89{
90}
91
92Text::Text(std::ostream &stream)
93 : mystream(false), stream(NULL), descriptions(false)
94{
95 open(stream);
96}
97
98Text::Text(const std::string &file)
99 : mystream(false), stream(NULL), descriptions(false)
100{
101 open(file);
102}
103
104
105Text::~Text()
106{
107 if (mystream) {
108 assert(stream);
109 delete stream;
110 }
111}
112
113void
114Text::open(std::ostream &_stream)
115{
116 if (stream)
117 panic("stream already set!");
118
119 mystream = false;
120 stream = &_stream;
121 if (!valid())
122 fatal("Unable to open output stream for writing\n");
123}
124
125void
126Text::open(const std::string &file)
127{
128 if (stream)
129 panic("stream already set!");
130
131 mystream = true;
132 stream = new ofstream(file.c_str(), ios::trunc);
133 if (!valid())
134 fatal("Unable to open statistics file for writing\n");
135}
136
137bool
138Text::valid() const
139{
140 return stream != NULL && stream->good();
141}
142
143void
144Text::begin()
145{
146 ccprintf(*stream, "\n---------- Begin Simulation Statistics ----------\n");
147}
148
149void
150Text::end()
151{
152 ccprintf(*stream, "\n---------- End Simulation Statistics ----------\n");
153 stream->flush();
154}
155
14 * Copyright (c) 2004-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Nathan Binkert
41 */
42
43#if defined(__APPLE__)
44#define _GLIBCPP_USE_C99 1
45#endif
46
47#if defined(__sun)
48#include <cmath>
49
50#endif
51
52#include <cassert>
53
54#ifdef __SUNPRO_CC
55#include <cmath>
56
57#endif
58#include "base/stats/text.hh"
59
60#include <cmath>
61#include <fstream>
62#include <iostream>
63#include <sstream>
64#include <string>
65
66#include "base/cast.hh"
67#include "base/logging.hh"
68#include "base/stats/info.hh"
69#include "base/str.hh"
70
71using namespace std;
72
73#ifndef NAN
74float __nan();
75/** Define Not a number. */
76#define NAN (__nan())
77/** Need to define __nan() */
78#define __M5_NAN
79#endif
80
81#ifdef __M5_NAN
82float
83__nan()
84{
85 union {
86 uint32_t ui;
87 float f;
88 } nan;
89
90 nan.ui = 0x7fc00000;
91 return nan.f;
92}
93#endif
94
95namespace Stats {
96
97std::list<Info *> &statsList();
98
99Text::Text()
100 : mystream(false), stream(NULL), descriptions(false)
101{
102}
103
104Text::Text(std::ostream &stream)
105 : mystream(false), stream(NULL), descriptions(false)
106{
107 open(stream);
108}
109
110Text::Text(const std::string &file)
111 : mystream(false), stream(NULL), descriptions(false)
112{
113 open(file);
114}
115
116
117Text::~Text()
118{
119 if (mystream) {
120 assert(stream);
121 delete stream;
122 }
123}
124
125void
126Text::open(std::ostream &_stream)
127{
128 if (stream)
129 panic("stream already set!");
130
131 mystream = false;
132 stream = &_stream;
133 if (!valid())
134 fatal("Unable to open output stream for writing\n");
135}
136
137void
138Text::open(const std::string &file)
139{
140 if (stream)
141 panic("stream already set!");
142
143 mystream = true;
144 stream = new ofstream(file.c_str(), ios::trunc);
145 if (!valid())
146 fatal("Unable to open statistics file for writing\n");
147}
148
149bool
150Text::valid() const
151{
152 return stream != NULL && stream->good();
153}
154
155void
156Text::begin()
157{
158 ccprintf(*stream, "\n---------- Begin Simulation Statistics ----------\n");
159}
160
161void
162Text::end()
163{
164 ccprintf(*stream, "\n---------- End Simulation Statistics ----------\n");
165 stream->flush();
166}
167
168std::string
169Text::statName(const std::string &name) const
170{
171 if (path.empty())
172 return name;
173 else
174 return csprintf("%s.%s", path.top(), name);
175}
176
177void
178Text::beginGroup(const char *name)
179{
180 if (path.empty()) {
181 path.push(name);
182 } else {
183 path.push(csprintf("%s.%s", path.top(), name));
184 }
185}
186
187void
188Text::endGroup()
189{
190 assert(!path.empty());
191 path.pop();
192}
193
156bool
157Text::noOutput(const Info &info)
158{
159 if (!info.flags.isSet(display))
160 return true;
161
162 if (info.prereq && info.prereq->zero())
163 return true;
164
165 return false;
166}
167
168string
169ValueToString(Result value, int precision)
170{
171 stringstream val;
172
173 if (!std::isnan(value)) {
174 if (precision != -1)
175 val.precision(precision);
176 else if (value == rint(value))
177 val.precision(0);
178
179 val.unsetf(ios::showpoint);
180 val.setf(ios::fixed);
181 val << value;
182 } else {
183 val << "nan";
184 }
185
186 return val.str();
187}
188
189struct ScalarPrint
190{
191 Result value;
192 string name;
193 string desc;
194 Flags flags;
195 bool descriptions;
196 int precision;
197 Result pdf;
198 Result cdf;
199
200 void update(Result val, Result total);
201 void operator()(ostream &stream, bool oneLine = false) const;
202};
203
204void
205ScalarPrint::update(Result val, Result total)
206{
207 value = val;
208 if (total) {
209 pdf = val / total;
210 cdf += pdf;
211 }
212}
213
214void
215ScalarPrint::operator()(ostream &stream, bool oneLine) const
216{
217 if ((flags.isSet(nozero) && (!oneLine) && value == 0.0) ||
218 (flags.isSet(nonan) && std::isnan(value)))
219 return;
220
221 stringstream pdfstr, cdfstr;
222
223 if (!std::isnan(pdf))
224 ccprintf(pdfstr, "%.2f%%", pdf * 100.0);
225
226 if (!std::isnan(cdf))
227 ccprintf(cdfstr, "%.2f%%", cdf * 100.0);
228
229 if (oneLine) {
230 ccprintf(stream, " |%12s %10s %10s",
231 ValueToString(value, precision), pdfstr.str(), cdfstr.str());
232 } else {
233 ccprintf(stream, "%-40s %12s %10s %10s", name,
234 ValueToString(value, precision), pdfstr.str(), cdfstr.str());
235
236 if (descriptions) {
237 if (!desc.empty())
238 ccprintf(stream, " # %s", desc);
239 }
240 stream << endl;
241 }
242}
243
244struct VectorPrint
245{
246 string name;
247 string separatorString;
248 string desc;
249 vector<string> subnames;
250 vector<string> subdescs;
251 Flags flags;
252 bool descriptions;
253 int precision;
254 VResult vec;
255 Result total;
256 bool forceSubnames;
257
258 void operator()(ostream &stream) const;
259};
260
261void
262VectorPrint::operator()(std::ostream &stream) const
263{
264 size_type _size = vec.size();
265 Result _total = 0.0;
266
267 if (flags.isSet(pdf | cdf)) {
268 for (off_type i = 0; i < _size; ++i) {
269 _total += vec[i];
270 }
271 }
272
273 string base = name + separatorString;
274
275 ScalarPrint print;
276 print.name = name;
277 print.desc = desc;
278 print.precision = precision;
279 print.descriptions = descriptions;
280 print.flags = flags;
281 print.pdf = _total ? 0.0 : NAN;
282 print.cdf = _total ? 0.0 : NAN;
283
284 bool havesub = !subnames.empty();
285
286 if (_size == 1) {
287 // If forceSubnames is set, get the first subname (or index in
288 // the case where there are no subnames) and append it to the
289 // base name.
290 if (forceSubnames)
291 print.name = base + (havesub ? subnames[0] : std::to_string(0));
292 print.value = vec[0];
293 print(stream);
294 return;
295 }
296
297 if ((!flags.isSet(nozero)) || (total != 0)) {
298 if (flags.isSet(oneline)) {
299 ccprintf(stream, "%-40s", name);
300 print.flags = print.flags & (~nozero);
301 }
302
303 for (off_type i = 0; i < _size; ++i) {
304 if (havesub && (i >= subnames.size() || subnames[i].empty()))
305 continue;
306
307 print.name = base + (havesub ? subnames[i] : std::to_string(i));
308 print.desc = subdescs.empty() ? desc : subdescs[i];
309
310 print.update(vec[i], _total);
311 print(stream, flags.isSet(oneline));
312 }
313
314 if (flags.isSet(oneline)) {
315 if (descriptions) {
316 if (!desc.empty())
317 ccprintf(stream, " # %s", desc);
318 }
319 stream << endl;
320 }
321 }
322
323 if (flags.isSet(::Stats::total)) {
324 print.pdf = NAN;
325 print.cdf = NAN;
326 print.name = base + "total";
327 print.desc = desc;
328 print.value = total;
329 print(stream);
330 }
331}
332
333struct DistPrint
334{
335 string name;
336 string separatorString;
337 string desc;
338 Flags flags;
339 bool descriptions;
340 int precision;
341
342 const DistData &data;
343
344 DistPrint(const Text *text, const DistInfo &info);
345 DistPrint(const Text *text, const VectorDistInfo &info, int i);
346 void init(const Text *text, const Info &info);
347 void operator()(ostream &stream) const;
348};
349
350DistPrint::DistPrint(const Text *text, const DistInfo &info)
351 : data(info.data)
352{
353 init(text, info);
354}
355
356DistPrint::DistPrint(const Text *text, const VectorDistInfo &info, int i)
357 : data(info.data[i])
358{
359 init(text, info);
360
361 name = info.name + "_" +
362 (info.subnames[i].empty() ? (std::to_string(i)) : info.subnames[i]);
363
364 if (!info.subdescs[i].empty())
365 desc = info.subdescs[i];
366}
367
368void
369DistPrint::init(const Text *text, const Info &info)
370{
194bool
195Text::noOutput(const Info &info)
196{
197 if (!info.flags.isSet(display))
198 return true;
199
200 if (info.prereq && info.prereq->zero())
201 return true;
202
203 return false;
204}
205
206string
207ValueToString(Result value, int precision)
208{
209 stringstream val;
210
211 if (!std::isnan(value)) {
212 if (precision != -1)
213 val.precision(precision);
214 else if (value == rint(value))
215 val.precision(0);
216
217 val.unsetf(ios::showpoint);
218 val.setf(ios::fixed);
219 val << value;
220 } else {
221 val << "nan";
222 }
223
224 return val.str();
225}
226
227struct ScalarPrint
228{
229 Result value;
230 string name;
231 string desc;
232 Flags flags;
233 bool descriptions;
234 int precision;
235 Result pdf;
236 Result cdf;
237
238 void update(Result val, Result total);
239 void operator()(ostream &stream, bool oneLine = false) const;
240};
241
242void
243ScalarPrint::update(Result val, Result total)
244{
245 value = val;
246 if (total) {
247 pdf = val / total;
248 cdf += pdf;
249 }
250}
251
252void
253ScalarPrint::operator()(ostream &stream, bool oneLine) const
254{
255 if ((flags.isSet(nozero) && (!oneLine) && value == 0.0) ||
256 (flags.isSet(nonan) && std::isnan(value)))
257 return;
258
259 stringstream pdfstr, cdfstr;
260
261 if (!std::isnan(pdf))
262 ccprintf(pdfstr, "%.2f%%", pdf * 100.0);
263
264 if (!std::isnan(cdf))
265 ccprintf(cdfstr, "%.2f%%", cdf * 100.0);
266
267 if (oneLine) {
268 ccprintf(stream, " |%12s %10s %10s",
269 ValueToString(value, precision), pdfstr.str(), cdfstr.str());
270 } else {
271 ccprintf(stream, "%-40s %12s %10s %10s", name,
272 ValueToString(value, precision), pdfstr.str(), cdfstr.str());
273
274 if (descriptions) {
275 if (!desc.empty())
276 ccprintf(stream, " # %s", desc);
277 }
278 stream << endl;
279 }
280}
281
282struct VectorPrint
283{
284 string name;
285 string separatorString;
286 string desc;
287 vector<string> subnames;
288 vector<string> subdescs;
289 Flags flags;
290 bool descriptions;
291 int precision;
292 VResult vec;
293 Result total;
294 bool forceSubnames;
295
296 void operator()(ostream &stream) const;
297};
298
299void
300VectorPrint::operator()(std::ostream &stream) const
301{
302 size_type _size = vec.size();
303 Result _total = 0.0;
304
305 if (flags.isSet(pdf | cdf)) {
306 for (off_type i = 0; i < _size; ++i) {
307 _total += vec[i];
308 }
309 }
310
311 string base = name + separatorString;
312
313 ScalarPrint print;
314 print.name = name;
315 print.desc = desc;
316 print.precision = precision;
317 print.descriptions = descriptions;
318 print.flags = flags;
319 print.pdf = _total ? 0.0 : NAN;
320 print.cdf = _total ? 0.0 : NAN;
321
322 bool havesub = !subnames.empty();
323
324 if (_size == 1) {
325 // If forceSubnames is set, get the first subname (or index in
326 // the case where there are no subnames) and append it to the
327 // base name.
328 if (forceSubnames)
329 print.name = base + (havesub ? subnames[0] : std::to_string(0));
330 print.value = vec[0];
331 print(stream);
332 return;
333 }
334
335 if ((!flags.isSet(nozero)) || (total != 0)) {
336 if (flags.isSet(oneline)) {
337 ccprintf(stream, "%-40s", name);
338 print.flags = print.flags & (~nozero);
339 }
340
341 for (off_type i = 0; i < _size; ++i) {
342 if (havesub && (i >= subnames.size() || subnames[i].empty()))
343 continue;
344
345 print.name = base + (havesub ? subnames[i] : std::to_string(i));
346 print.desc = subdescs.empty() ? desc : subdescs[i];
347
348 print.update(vec[i], _total);
349 print(stream, flags.isSet(oneline));
350 }
351
352 if (flags.isSet(oneline)) {
353 if (descriptions) {
354 if (!desc.empty())
355 ccprintf(stream, " # %s", desc);
356 }
357 stream << endl;
358 }
359 }
360
361 if (flags.isSet(::Stats::total)) {
362 print.pdf = NAN;
363 print.cdf = NAN;
364 print.name = base + "total";
365 print.desc = desc;
366 print.value = total;
367 print(stream);
368 }
369}
370
371struct DistPrint
372{
373 string name;
374 string separatorString;
375 string desc;
376 Flags flags;
377 bool descriptions;
378 int precision;
379
380 const DistData &data;
381
382 DistPrint(const Text *text, const DistInfo &info);
383 DistPrint(const Text *text, const VectorDistInfo &info, int i);
384 void init(const Text *text, const Info &info);
385 void operator()(ostream &stream) const;
386};
387
388DistPrint::DistPrint(const Text *text, const DistInfo &info)
389 : data(info.data)
390{
391 init(text, info);
392}
393
394DistPrint::DistPrint(const Text *text, const VectorDistInfo &info, int i)
395 : data(info.data[i])
396{
397 init(text, info);
398
399 name = info.name + "_" +
400 (info.subnames[i].empty() ? (std::to_string(i)) : info.subnames[i]);
401
402 if (!info.subdescs[i].empty())
403 desc = info.subdescs[i];
404}
405
406void
407DistPrint::init(const Text *text, const Info &info)
408{
371 name = info.name;
409 name = text->statName(info.name);
372 separatorString = info.separatorString;
373 desc = info.desc;
374 flags = info.flags;
375 precision = info.precision;
376 descriptions = text->descriptions;
377}
378
379void
380DistPrint::operator()(ostream &stream) const
381{
382 if (flags.isSet(nozero) && data.samples == 0) return;
383 string base = name + separatorString;
384
385 ScalarPrint print;
386 print.precision = precision;
387 print.flags = flags;
388 print.descriptions = descriptions;
389 print.desc = desc;
390 print.pdf = NAN;
391 print.cdf = NAN;
392
393 if (flags.isSet(oneline)) {
394 print.name = base + "bucket_size";
395 print.value = data.bucket_size;
396 print(stream);
397
398 print.name = base + "min_bucket";
399 print.value = data.min;
400 print(stream);
401
402 print.name = base + "max_bucket";
403 print.value = data.max;
404 print(stream);
405 }
406
407 print.name = base + "samples";
408 print.value = data.samples;
409 print(stream);
410
411 print.name = base + "mean";
412 print.value = data.samples ? data.sum / data.samples : NAN;
413 print(stream);
414
415 if (data.type == Hist) {
416 print.name = base + "gmean";
417 print.value = data.samples ? exp(data.logs / data.samples) : NAN;
418 print(stream);
419 }
420
421 Result stdev = NAN;
422 if (data.samples)
423 stdev = sqrt((data.samples * data.squares - data.sum * data.sum) /
424 (data.samples * (data.samples - 1.0)));
425 print.name = base + "stdev";
426 print.value = stdev;
427 print(stream);
428
429 if (data.type == Deviation)
430 return;
431
432 size_t size = data.cvec.size();
433
434 Result total = 0.0;
435 if (data.type == Dist && data.underflow != NAN)
436 total += data.underflow;
437 for (off_type i = 0; i < size; ++i)
438 total += data.cvec[i];
439 if (data.type == Dist && data.overflow != NAN)
440 total += data.overflow;
441
442 if (total) {
443 print.pdf = 0.0;
444 print.cdf = 0.0;
445 }
446
447 if (data.type == Dist && data.underflow != NAN) {
448 print.name = base + "underflows";
449 print.update(data.underflow, total);
450 print(stream);
451 }
452
453 if (flags.isSet(oneline)) {
454 ccprintf(stream, "%-40s", name);
455 }
456
457 for (off_type i = 0; i < size; ++i) {
458 stringstream namestr;
459 namestr << base;
460
461 Counter low = i * data.bucket_size + data.min;
462 Counter high = ::min(low + data.bucket_size - 1.0, data.max);
463 namestr << low;
464 if (low < high)
465 namestr << "-" << high;
466
467 print.name = namestr.str();
468 print.update(data.cvec[i], total);
469 print(stream, flags.isSet(oneline));
470 }
471
472 if (flags.isSet(oneline)) {
473 if (descriptions) {
474 if (!desc.empty())
475 ccprintf(stream, " # %s", desc);
476 }
477 stream << endl;
478 }
479
480 if (data.type == Dist && data.overflow != NAN) {
481 print.name = base + "overflows";
482 print.update(data.overflow, total);
483 print(stream);
484 }
485
486 print.pdf = NAN;
487 print.cdf = NAN;
488
489 if (data.type == Dist && data.min_val != NAN) {
490 print.name = base + "min_value";
491 print.value = data.min_val;
492 print(stream);
493 }
494
495 if (data.type == Dist && data.max_val != NAN) {
496 print.name = base + "max_value";
497 print.value = data.max_val;
498 print(stream);
499 }
500
501 print.name = base + "total";
502 print.value = total;
503 print(stream);
504}
505
506void
507Text::visit(const ScalarInfo &info)
508{
509 if (noOutput(info))
510 return;
511
512 ScalarPrint print;
513 print.value = info.result();
410 separatorString = info.separatorString;
411 desc = info.desc;
412 flags = info.flags;
413 precision = info.precision;
414 descriptions = text->descriptions;
415}
416
417void
418DistPrint::operator()(ostream &stream) const
419{
420 if (flags.isSet(nozero) && data.samples == 0) return;
421 string base = name + separatorString;
422
423 ScalarPrint print;
424 print.precision = precision;
425 print.flags = flags;
426 print.descriptions = descriptions;
427 print.desc = desc;
428 print.pdf = NAN;
429 print.cdf = NAN;
430
431 if (flags.isSet(oneline)) {
432 print.name = base + "bucket_size";
433 print.value = data.bucket_size;
434 print(stream);
435
436 print.name = base + "min_bucket";
437 print.value = data.min;
438 print(stream);
439
440 print.name = base + "max_bucket";
441 print.value = data.max;
442 print(stream);
443 }
444
445 print.name = base + "samples";
446 print.value = data.samples;
447 print(stream);
448
449 print.name = base + "mean";
450 print.value = data.samples ? data.sum / data.samples : NAN;
451 print(stream);
452
453 if (data.type == Hist) {
454 print.name = base + "gmean";
455 print.value = data.samples ? exp(data.logs / data.samples) : NAN;
456 print(stream);
457 }
458
459 Result stdev = NAN;
460 if (data.samples)
461 stdev = sqrt((data.samples * data.squares - data.sum * data.sum) /
462 (data.samples * (data.samples - 1.0)));
463 print.name = base + "stdev";
464 print.value = stdev;
465 print(stream);
466
467 if (data.type == Deviation)
468 return;
469
470 size_t size = data.cvec.size();
471
472 Result total = 0.0;
473 if (data.type == Dist && data.underflow != NAN)
474 total += data.underflow;
475 for (off_type i = 0; i < size; ++i)
476 total += data.cvec[i];
477 if (data.type == Dist && data.overflow != NAN)
478 total += data.overflow;
479
480 if (total) {
481 print.pdf = 0.0;
482 print.cdf = 0.0;
483 }
484
485 if (data.type == Dist && data.underflow != NAN) {
486 print.name = base + "underflows";
487 print.update(data.underflow, total);
488 print(stream);
489 }
490
491 if (flags.isSet(oneline)) {
492 ccprintf(stream, "%-40s", name);
493 }
494
495 for (off_type i = 0; i < size; ++i) {
496 stringstream namestr;
497 namestr << base;
498
499 Counter low = i * data.bucket_size + data.min;
500 Counter high = ::min(low + data.bucket_size - 1.0, data.max);
501 namestr << low;
502 if (low < high)
503 namestr << "-" << high;
504
505 print.name = namestr.str();
506 print.update(data.cvec[i], total);
507 print(stream, flags.isSet(oneline));
508 }
509
510 if (flags.isSet(oneline)) {
511 if (descriptions) {
512 if (!desc.empty())
513 ccprintf(stream, " # %s", desc);
514 }
515 stream << endl;
516 }
517
518 if (data.type == Dist && data.overflow != NAN) {
519 print.name = base + "overflows";
520 print.update(data.overflow, total);
521 print(stream);
522 }
523
524 print.pdf = NAN;
525 print.cdf = NAN;
526
527 if (data.type == Dist && data.min_val != NAN) {
528 print.name = base + "min_value";
529 print.value = data.min_val;
530 print(stream);
531 }
532
533 if (data.type == Dist && data.max_val != NAN) {
534 print.name = base + "max_value";
535 print.value = data.max_val;
536 print(stream);
537 }
538
539 print.name = base + "total";
540 print.value = total;
541 print(stream);
542}
543
544void
545Text::visit(const ScalarInfo &info)
546{
547 if (noOutput(info))
548 return;
549
550 ScalarPrint print;
551 print.value = info.result();
514 print.name = info.name;
552 print.name = statName(info.name);
515 print.desc = info.desc;
516 print.flags = info.flags;
517 print.descriptions = descriptions;
518 print.precision = info.precision;
519 print.pdf = NAN;
520 print.cdf = NAN;
521
522 print(*stream);
523}
524
525void
526Text::visit(const VectorInfo &info)
527{
528 if (noOutput(info))
529 return;
530
531 size_type size = info.size();
532 VectorPrint print;
533
553 print.desc = info.desc;
554 print.flags = info.flags;
555 print.descriptions = descriptions;
556 print.precision = info.precision;
557 print.pdf = NAN;
558 print.cdf = NAN;
559
560 print(*stream);
561}
562
563void
564Text::visit(const VectorInfo &info)
565{
566 if (noOutput(info))
567 return;
568
569 size_type size = info.size();
570 VectorPrint print;
571
534 print.name = info.name;
572 print.name = statName(info.name);
535 print.separatorString = info.separatorString;
536 print.desc = info.desc;
537 print.flags = info.flags;
538 print.descriptions = descriptions;
539 print.precision = info.precision;
540 print.vec = info.result();
541 print.total = info.total();
542 print.forceSubnames = false;
543
544 if (!info.subnames.empty()) {
545 for (off_type i = 0; i < size; ++i) {
546 if (!info.subnames[i].empty()) {
547 print.subnames = info.subnames;
548 print.subnames.resize(size);
549 for (off_type i = 0; i < size; ++i) {
550 if (!info.subnames[i].empty() &&
551 !info.subdescs[i].empty()) {
552 print.subdescs = info.subdescs;
553 print.subdescs.resize(size);
554 break;
555 }
556 }
557 break;
558 }
559 }
560 }
561
562 print(*stream);
563}
564
565void
566Text::visit(const Vector2dInfo &info)
567{
568 if (noOutput(info))
569 return;
570
571 bool havesub = false;
572 VectorPrint print;
573
574 if (!info.y_subnames.empty()) {
575 for (off_type i = 0; i < info.y; ++i) {
576 if (!info.y_subnames[i].empty()) {
577 print.subnames = info.y_subnames;
578 break;
579 }
580 }
581 }
582 print.flags = info.flags;
583 print.separatorString = info.separatorString;
584 print.descriptions = descriptions;
585 print.precision = info.precision;
586 print.forceSubnames = true;
587
588 if (!info.subnames.empty()) {
589 for (off_type i = 0; i < info.x; ++i)
590 if (!info.subnames[i].empty())
591 havesub = true;
592 }
593
594 VResult tot_vec(info.y);
595 for (off_type i = 0; i < info.x; ++i) {
596 if (havesub && (i >= info.subnames.size() || info.subnames[i].empty()))
597 continue;
598
599 off_type iy = i * info.y;
600 VResult yvec(info.y);
601
602 Result total = 0.0;
603 for (off_type j = 0; j < info.y; ++j) {
604 yvec[j] = info.cvec[iy + j];
605 tot_vec[j] += yvec[j];
606 total += yvec[j];
607 }
608
573 print.separatorString = info.separatorString;
574 print.desc = info.desc;
575 print.flags = info.flags;
576 print.descriptions = descriptions;
577 print.precision = info.precision;
578 print.vec = info.result();
579 print.total = info.total();
580 print.forceSubnames = false;
581
582 if (!info.subnames.empty()) {
583 for (off_type i = 0; i < size; ++i) {
584 if (!info.subnames[i].empty()) {
585 print.subnames = info.subnames;
586 print.subnames.resize(size);
587 for (off_type i = 0; i < size; ++i) {
588 if (!info.subnames[i].empty() &&
589 !info.subdescs[i].empty()) {
590 print.subdescs = info.subdescs;
591 print.subdescs.resize(size);
592 break;
593 }
594 }
595 break;
596 }
597 }
598 }
599
600 print(*stream);
601}
602
603void
604Text::visit(const Vector2dInfo &info)
605{
606 if (noOutput(info))
607 return;
608
609 bool havesub = false;
610 VectorPrint print;
611
612 if (!info.y_subnames.empty()) {
613 for (off_type i = 0; i < info.y; ++i) {
614 if (!info.y_subnames[i].empty()) {
615 print.subnames = info.y_subnames;
616 break;
617 }
618 }
619 }
620 print.flags = info.flags;
621 print.separatorString = info.separatorString;
622 print.descriptions = descriptions;
623 print.precision = info.precision;
624 print.forceSubnames = true;
625
626 if (!info.subnames.empty()) {
627 for (off_type i = 0; i < info.x; ++i)
628 if (!info.subnames[i].empty())
629 havesub = true;
630 }
631
632 VResult tot_vec(info.y);
633 for (off_type i = 0; i < info.x; ++i) {
634 if (havesub && (i >= info.subnames.size() || info.subnames[i].empty()))
635 continue;
636
637 off_type iy = i * info.y;
638 VResult yvec(info.y);
639
640 Result total = 0.0;
641 for (off_type j = 0; j < info.y; ++j) {
642 yvec[j] = info.cvec[iy + j];
643 tot_vec[j] += yvec[j];
644 total += yvec[j];
645 }
646
609 print.name = info.name + "_" +
610 (havesub ? info.subnames[i] : std::to_string(i));
647 print.name = statName(
648 info.name + "_" +
649 (havesub ? info.subnames[i] : std::to_string(i)));
611 print.desc = info.desc;
612 print.vec = yvec;
613 print.total = total;
614 print(*stream);
615 }
616
617 // Create a subname for printing the total
618 vector<string> total_subname;
619 total_subname.push_back("total");
620
621 if (info.flags.isSet(::Stats::total) && (info.x > 1)) {
650 print.desc = info.desc;
651 print.vec = yvec;
652 print.total = total;
653 print(*stream);
654 }
655
656 // Create a subname for printing the total
657 vector<string> total_subname;
658 total_subname.push_back("total");
659
660 if (info.flags.isSet(::Stats::total) && (info.x > 1)) {
622 print.name = info.name;
661 print.name = statName(info.name);
623 print.subnames = total_subname;
624 print.desc = info.desc;
625 print.vec = VResult(1, info.total());
626 print.flags = print.flags & ~total;
627 print(*stream);
628 }
629}
630
631void
632Text::visit(const DistInfo &info)
633{
634 if (noOutput(info))
635 return;
636
637 DistPrint print(this, info);
638 print(*stream);
639}
640
641void
642Text::visit(const VectorDistInfo &info)
643{
644 if (noOutput(info))
645 return;
646
647 for (off_type i = 0; i < info.size(); ++i) {
648 DistPrint print(this, info, i);
649 print(*stream);
650 }
651}
652
653void
654Text::visit(const FormulaInfo &info)
655{
656 visit((const VectorInfo &)info);
657}
658
659/*
660 This struct implements the output methods for the sparse
661 histogram stat
662*/
663struct SparseHistPrint
664{
665 string name;
666 string separatorString;
667 string desc;
668 Flags flags;
669 bool descriptions;
670 int precision;
671
672 const SparseHistData &data;
673
674 SparseHistPrint(const Text *text, const SparseHistInfo &info);
675 void init(const Text *text, const Info &info);
676 void operator()(ostream &stream) const;
677};
678
679/* Call initialization function */
680SparseHistPrint::SparseHistPrint(const Text *text, const SparseHistInfo &info)
681 : data(info.data)
682{
683 init(text, info);
684}
685
686/* Initialization function */
687void
688SparseHistPrint::init(const Text *text, const Info &info)
689{
662 print.subnames = total_subname;
663 print.desc = info.desc;
664 print.vec = VResult(1, info.total());
665 print.flags = print.flags & ~total;
666 print(*stream);
667 }
668}
669
670void
671Text::visit(const DistInfo &info)
672{
673 if (noOutput(info))
674 return;
675
676 DistPrint print(this, info);
677 print(*stream);
678}
679
680void
681Text::visit(const VectorDistInfo &info)
682{
683 if (noOutput(info))
684 return;
685
686 for (off_type i = 0; i < info.size(); ++i) {
687 DistPrint print(this, info, i);
688 print(*stream);
689 }
690}
691
692void
693Text::visit(const FormulaInfo &info)
694{
695 visit((const VectorInfo &)info);
696}
697
698/*
699 This struct implements the output methods for the sparse
700 histogram stat
701*/
702struct SparseHistPrint
703{
704 string name;
705 string separatorString;
706 string desc;
707 Flags flags;
708 bool descriptions;
709 int precision;
710
711 const SparseHistData &data;
712
713 SparseHistPrint(const Text *text, const SparseHistInfo &info);
714 void init(const Text *text, const Info &info);
715 void operator()(ostream &stream) const;
716};
717
718/* Call initialization function */
719SparseHistPrint::SparseHistPrint(const Text *text, const SparseHistInfo &info)
720 : data(info.data)
721{
722 init(text, info);
723}
724
725/* Initialization function */
726void
727SparseHistPrint::init(const Text *text, const Info &info)
728{
690 name = info.name;
729 name = text->statName(info.name);
691 separatorString = info.separatorString;
692 desc = info.desc;
693 flags = info.flags;
694 precision = info.precision;
695 descriptions = text->descriptions;
696}
697
698/* Grab data from map and write to output stream */
699void
700SparseHistPrint::operator()(ostream &stream) const
701{
702 string base = name + separatorString;
703
704 ScalarPrint print;
705 print.precision = precision;
706 print.flags = flags;
707 print.descriptions = descriptions;
708 print.desc = desc;
709 print.pdf = NAN;
710 print.cdf = NAN;
711
712 print.name = base + "samples";
713 print.value = data.samples;
714 print(stream);
715
716 MCounter::const_iterator it;
717 for (it = data.cmap.begin(); it != data.cmap.end(); it++) {
718 stringstream namestr;
719 namestr << base;
720
721 namestr <<(*it).first;
722 print.name = namestr.str();
723 print.value = (*it).second;
724 print(stream);
725 }
726}
727
728void
729Text::visit(const SparseHistInfo &info)
730{
731 if (noOutput(info))
732 return;
733
734 SparseHistPrint print(this, info);
735 print(*stream);
736}
737
738Output *
739initText(const string &filename, bool desc)
740{
741 static Text text;
742 static bool connected = false;
743
744 if (!connected) {
745 text.open(*simout.findOrCreate(filename)->stream());
746 text.descriptions = desc;
747 connected = true;
748 }
749
750 return &text;
751}
752
753} // namespace Stats
730 separatorString = info.separatorString;
731 desc = info.desc;
732 flags = info.flags;
733 precision = info.precision;
734 descriptions = text->descriptions;
735}
736
737/* Grab data from map and write to output stream */
738void
739SparseHistPrint::operator()(ostream &stream) const
740{
741 string base = name + separatorString;
742
743 ScalarPrint print;
744 print.precision = precision;
745 print.flags = flags;
746 print.descriptions = descriptions;
747 print.desc = desc;
748 print.pdf = NAN;
749 print.cdf = NAN;
750
751 print.name = base + "samples";
752 print.value = data.samples;
753 print(stream);
754
755 MCounter::const_iterator it;
756 for (it = data.cmap.begin(); it != data.cmap.end(); it++) {
757 stringstream namestr;
758 namestr << base;
759
760 namestr <<(*it).first;
761 print.name = namestr.str();
762 print.value = (*it).second;
763 print(stream);
764 }
765}
766
767void
768Text::visit(const SparseHistInfo &info)
769{
770 if (noOutput(info))
771 return;
772
773 SparseHistPrint print(this, info);
774 print(*stream);
775}
776
777Output *
778initText(const string &filename, bool desc)
779{
780 static Text text;
781 static bool connected = false;
782
783 if (!connected) {
784 text.open(*simout.findOrCreate(filename)->stream());
785 text.descriptions = desc;
786 connected = true;
787 }
788
789 return &text;
790}
791
792} // namespace Stats