statistics.cc revision 9554:406fbcf60223
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
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
390void
391Formula::result(VResult &vec) const
392{
393    if (root)
394        vec = root->result();
395}
396
397Result
398Formula::total() const
399{
400    return root ? root->total() : 0.0;
401}
402
403size_type
404Formula::size() const
405{
406    if (!root)
407        return 0;
408    else
409        return root->size();
410}
411
412void
413Formula::reset()
414{
415}
416
417bool
418Formula::zero() const
419{
420    VResult vec;
421    result(vec);
422    for (VResult::size_type i = 0; i < vec.size(); ++i)
423        if (vec[i] != 0.0)
424            return false;
425    return true;
426}
427
428string
429Formula::str() const
430{
431    return root ? root->str() : "";
432}
433
434CallbackQueue dumpQueue;
435CallbackQueue resetQueue;
436
437void
438registerResetCallback(Callback *cb)
439{
440    resetQueue.add(cb);
441}
442
443bool _enabled = false;
444
445bool
446enabled()
447{
448    return _enabled;
449}
450
451void
452enable()
453{
454    if (_enabled)
455        fatal("Stats are already enabled");
456
457    _enabled = true;
458}
459
460void
461registerDumpCallback(Callback *cb)
462{
463    dumpQueue.add(cb);
464}
465
466} // namespace Stats
467
468void
469debugDumpStats()
470{
471    Stats::dump();
472}
473