2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
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"
51 std::string
Info::separatorString
= "::";
53 // We wrap these in a function to make sure they're built in time.
57 static list
<Info
*> the_list
;
64 static MapType the_map
;
69 InfoAccess::setInfo(Info
*info
)
71 if (statsMap().find(this) != statsMap().end())
72 panic("shouldn't register stat twice!");
74 statsList().push_back(info
);
77 pair
<MapType::iterator
, bool> result
=
79 statsMap().insert(make_pair(this, info
));
80 assert(result
.second
&& "this should never fail");
81 assert(statsMap().find(this) != statsMap().end());
85 InfoAccess::setParams(const StorageParams
*params
)
87 info()->storageParams
= params
;
93 info()->flags
.set(init
);
99 MapType::const_iterator i
= statsMap().find(this);
100 assert(i
!= statsMap().end());
105 InfoAccess::info() const
107 MapType::const_iterator i
= statsMap().find(this);
108 assert(i
!= statsMap().end());
112 StorageParams::~StorageParams()
119 static NameMapType the_map
;
123 int Info::id_count
= 0;
125 int debug_break_id
= -1;
128 : flags(none
), precision(-1), prereq(0), storageParams(NULL
)
131 if (debug_break_id
>= 0 and debug_break_id
== id
)
140 validateStatName(const string
&name
)
146 tokenize(vec
, name
, '.');
147 vector
<string
>::const_iterator item
= vec
.begin();
148 while (item
!= vec
.end()) {
152 string::const_iterator c
= item
->begin();
154 // The first character is different
155 if (!isalpha(*c
) && *c
!= '_')
158 // The rest of the characters have different rules.
159 while (++c
!= item
->end()) {
160 if (!isalnum(*c
) && *c
!= '_')
171 Info::setName(const string
&name
)
173 if (!validateStatName(name
))
174 panic("invalid stat name '%s'", name
);
176 pair
<NameMapType::iterator
, bool> p
=
177 nameMap().insert(make_pair(name
, this));
179 Info
*other
= p
.first
->second
;
180 bool result
= p
.second
;
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
);
192 Info::less(Info
*stat1
, Info
*stat2
)
194 const string
&name1
= stat1
->name
;
195 const string
&name2
= stat2
->name
;
200 tokenize(v1
, name1
, '.');
201 tokenize(v2
, name2
, '.');
203 size_type last
= min(v1
.size(), v2
.size()) - 1;
204 for (off_type i
= 0; i
< last
; ++i
)
206 return v1
[i
] < v2
[i
];
208 // Special compare for last element.
209 if (v1
[last
] == v2
[last
])
210 return v1
.size() < v2
.size();
212 return v1
[last
] < v2
[last
];
218 Info::baseCheck() const
220 if (!(flags
& Stats::init
)) {
222 cprintf("this is stat number %d\n", id
);
224 panic("Not all stats have been initialized");
228 if ((flags
& display
) && name
.empty()) {
229 panic("all printable stats must be named");
244 size_type s
= size();
245 if (subnames
.size() < s
)
247 if (subdescs
.size() < s
)
252 VectorDistInfo::enable()
254 size_type s
= size();
255 if (subnames
.size() < s
)
257 if (subdescs
.size() < s
)
262 Vector2dInfo::enable()
264 if (subnames
.size() < x
)
266 if (subdescs
.size() < x
)
268 if (y_subnames
.size() < y
)
269 y_subnames
.resize(y
);
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!
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];
288 assert(low_pair
== 0 || low_pair
== -1 || low_pair
== -2);
290 for (int i
= bottom_half
- 1; i
>= 0; i
--)
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];
301 assert(high_pair
== size
|| high_pair
== size
+ 1);
303 for (int i
= top_half
; i
< size
; i
++)
312 HistStor::grow_convert()
314 int size
= cvec
.size();
315 int half
= (size
+ 1) / 2; // round up!
316 //bool even = (size & 1) == 0;
319 for (int i
= size
- 1; i
>= half
; --i
) {
320 cvec
[i
] = cvec
[pair
];
322 cvec
[i
] += cvec
[pair
- 1];
326 for (int i
= half
- 1; i
>= 0; i
--)
329 min_bucket
= -max_bucket
;// - (even ? bucket_size : 0);
336 int size
= cvec
.size();
337 int half
= (size
+ 1) / 2; // round up!
340 for (int i
= 0; i
< half
; i
++) {
341 cvec
[i
] = cvec
[pair
];
343 cvec
[i
] += cvec
[pair
+ 1];
346 assert(pair
== size
|| pair
== size
+ 1);
348 for (int i
= half
; i
< size
; i
++)
356 HistStor::add(HistStor
*hs
)
358 int b_size
= hs
->size();
359 assert(size() == b_size
);
360 assert(min_bucket
== hs
->min_bucket
);
364 squares
+= hs
->squares
;
365 samples
+= hs
->samples
;
367 while(bucket_size
> hs
->bucket_size
)
369 while(bucket_size
< hs
->bucket_size
)
372 for (uint32_t i
= 0; i
< b_size
; i
++)
373 cvec
[i
] += hs
->cvec
[i
];
380 Formula::Formula(Temp r
)
388 Formula::operator=(Temp r
)
390 assert(!root
&& "Can't change formulas");
398 Formula::operator+=(Temp r
)
401 root
= NodePtr(new BinaryNode
<std::plus
<Result
> >(root
, r
));
412 Formula::operator/=(Temp r
)
415 root
= NodePtr(new BinaryNode
<std::divides
<Result
> >(root
, r
));
422 Formula::result(VResult
&vec
) const
425 vec
= root
->result();
429 Formula::total() const
431 return root
? root
->total() : 0.0;
435 Formula::size() const
449 Formula::zero() const
453 for (VResult::size_type i
= 0; i
< vec
.size(); ++i
)
462 return root
? root
->str() : "";
465 Handler resetHandler
= NULL
;
466 Handler dumpHandler
= NULL
;
469 registerHandlers(Handler reset_handler
, Handler dump_handler
)
471 resetHandler
= reset_handler
;
472 dumpHandler
= dump_handler
;
475 CallbackQueue dumpQueue
;
476 CallbackQueue resetQueue
;
481 resetQueue
.process();
491 registerResetCallback(Callback
*cb
)
496 bool _enabled
= false;
508 fatal("Stats are already enabled");
519 fatal("No registered Stats::dump handler");
528 fatal("No registered Stats::reset handler");
532 registerDumpCallback(Callback
*cb
)