statistics.cc revision 10453:d0365cc3d05f
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 <fstream>
32#include <iomanip>
33#include <list>
34#include <map>
35#include <string>
36
37#include "base/callback.hh"
38#include "base/cprintf.hh"
39#include "base/debug.hh"
40#include "base/hostinfo.hh"
41#include "base/misc.hh"
42#include "base/statistics.hh"
43#include "base/str.hh"
44#include "base/time.hh"
45#include "base/trace.hh"
46
47using namespace std;
48
49namespace Stats {
50
51std::string Info::separatorString = "::";
52
53// We wrap these in a function to make sure they're built in time.
54list<Info *> &
55statsList()
56{
57    static list<Info *> the_list;
58    return the_list;
59}
60
61MapType &
62statsMap()
63{
64    static MapType the_map;
65    return the_map;
66}
67
68void
69InfoAccess::setInfo(Info *info)
70{
71    if (statsMap().find(this) != statsMap().end())
72        panic("shouldn't register stat twice!");
73
74    statsList().push_back(info);
75
76#ifndef NDEBUG
77    pair<MapType::iterator, bool> result =
78#endif
79        statsMap().insert(make_pair(this, info));
80    assert(result.second && "this should never fail");
81    assert(statsMap().find(this) != statsMap().end());
82}
83
84void
85InfoAccess::setParams(const StorageParams *params)
86{
87    info()->storageParams = params;
88}
89
90void
91InfoAccess::setInit()
92{
93    info()->flags.set(init);
94}
95
96Info *
97InfoAccess::info()
98{
99    MapType::const_iterator i = statsMap().find(this);
100    assert(i != statsMap().end());
101    return (*i).second;
102}
103
104const Info *
105InfoAccess::info() const
106{
107    MapType::const_iterator i = statsMap().find(this);
108    assert(i != statsMap().end());
109    return (*i).second;
110}
111
112StorageParams::~StorageParams()
113{
114}
115
116NameMapType &
117nameMap()
118{
119    static NameMapType the_map;
120    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
355void
356HistStor::add(HistStor *hs)
357{
358    int b_size = hs->size();
359    assert(size() == b_size);
360    assert(min_bucket == hs->min_bucket);
361
362    sum += hs->sum;
363    logs += hs->logs;
364    squares += hs->squares;
365    samples += hs->samples;
366
367    while(bucket_size > hs->bucket_size)
368        hs->grow_up();
369    while(bucket_size < hs->bucket_size)
370        grow_up();
371
372    for (uint32_t i = 0; i < b_size; i++)
373        cvec[i] += hs->cvec[i];
374}
375
376Formula::Formula()
377{
378}
379
380Formula::Formula(Temp r)
381{
382    root = r;
383    setInit();
384    assert(size());
385}
386
387const Formula &
388Formula::operator=(Temp r)
389{
390    assert(!root && "Can't change formulas");
391    root = r;
392    setInit();
393    assert(size());
394    return *this;
395}
396
397const Formula &
398Formula::operator+=(Temp r)
399{
400    if (root)
401        root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
402    else {
403        root = r;
404        setInit();
405    }
406
407    assert(size());
408    return *this;
409}
410
411const Formula &
412Formula::operator/=(Temp r)
413{
414    assert (root);
415    root = NodePtr(new BinaryNode<std::divides<Result> >(root, r));
416
417    assert(size());
418    return *this;
419}
420
421void
422Formula::result(VResult &vec) const
423{
424    if (root)
425        vec = root->result();
426}
427
428Result
429Formula::total() const
430{
431    return root ? root->total() : 0.0;
432}
433
434size_type
435Formula::size() const
436{
437    if (!root)
438        return 0;
439    else
440        return root->size();
441}
442
443void
444Formula::reset()
445{
446}
447
448bool
449Formula::zero() const
450{
451    VResult vec;
452    result(vec);
453    for (VResult::size_type i = 0; i < vec.size(); ++i)
454        if (vec[i] != 0.0)
455            return false;
456    return true;
457}
458
459string
460Formula::str() const
461{
462    return root ? root->str() : "";
463}
464
465Handler resetHandler = NULL;
466Handler dumpHandler = NULL;
467
468void
469registerHandlers(Handler reset_handler, Handler dump_handler)
470{
471    resetHandler = reset_handler;
472    dumpHandler = dump_handler;
473}
474
475CallbackQueue dumpQueue;
476CallbackQueue resetQueue;
477
478void
479processResetQueue()
480{
481    resetQueue.process();
482}
483
484void
485processDumpQueue()
486{
487    dumpQueue.process();
488}
489
490void
491registerResetCallback(Callback *cb)
492{
493    resetQueue.add(cb);
494}
495
496bool _enabled = false;
497
498bool
499enabled()
500{
501    return _enabled;
502}
503
504void
505enable()
506{
507    if (_enabled)
508        fatal("Stats are already enabled");
509
510    _enabled = true;
511}
512
513void
514dump()
515{
516    if (dumpHandler)
517        dumpHandler();
518    else
519        fatal("No registered Stats::dump handler");
520}
521
522void
523reset()
524{
525    if (resetHandler)
526        resetHandler();
527    else
528        fatal("No registered Stats::reset handler");
529}
530
531void
532registerDumpCallback(Callback *cb)
533{
534    dumpQueue.add(cb);
535}
536
537} // namespace Stats
538
539void
540debugDumpStats()
541{
542    Stats::dump();
543}
544