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.
35 #include "base/callback.hh"
36 #include "base/cprintf.hh"
37 #include "base/hostinfo.hh"
38 #include "base/misc.hh"
39 #include "base/statistics.hh"
40 #include "base/str.hh"
41 #include "base/time.hh"
42 #include "base/trace.hh"
43 #include "base/stats/statdb.hh"
44 #include "config/stats_binning.hh"
51 DataAccess::find() const
53 return Database::find(const_cast<void *>((const void *)this));
57 getStatData(const void *stat
)
59 return Database::find(const_cast<void *>(stat
));
63 DataAccess::map(StatData
*data
)
65 Database::regStat(this, data
);
69 DataAccess::statData()
71 StatData
*ptr
= find();
77 DataAccess::statData() const
79 const StatData
*ptr
= find();
87 statData()->flags
|= init
;
91 DataAccess::setPrint()
93 Database::regPrint(this);
97 : flags(none
), precision(-1), prereq(0)
103 StatData::~StatData()
108 StatData::less(StatData
*stat1
, StatData
*stat2
)
110 const string
&name1
= stat1
->name
;
111 const string
&name2
= stat2
->name
;
116 tokenize(v1
, name1
, '.');
117 tokenize(v2
, name2
, '.');
119 int last
= min(v1
.size(), v2
.size()) - 1;
120 for (int i
= 0; i
< last
; ++i
)
122 return v1
[i
] < v2
[i
];
124 // Special compare for last element.
125 if (v1
[last
] == v2
[last
])
126 return v1
.size() < v2
.size();
128 return v1
[last
] < v2
[last
];
134 StatData::baseCheck() const
136 if (!(flags
& init
)) {
138 cprintf("this is stat number %d\n", id
);
140 panic("Not all stats have been initialized");
144 if ((flags
& print
) && name
.empty()) {
145 panic("all printable stats must be named");
154 FormulaBase::result(VResult
&vec
) const
157 vec
= root
->result();
161 FormulaBase::total() const
163 return root
? root
->total() : 0.0;
167 FormulaBase::size() const
176 FormulaBase::binned() const
178 return root
&& root
->binned();
187 FormulaBase::zero() const
191 for (int i
= 0; i
< vec
.size(); ++i
)
198 FormulaBase::update(StatData
*)
203 FormulaBase::str() const
205 return root
? root
->str() : "";
213 Formula::Formula(Temp r
)
220 Formula::operator=(Temp r
)
222 assert(!root
&& "Can't change formulas");
229 Formula::operator+=(Temp r
)
232 root
= NodePtr(new BinaryNode
<std::plus
<Result
> >(root
, r
));
239 MainBin::MainBin(const string
&name
)
240 : _name(name
), mem(NULL
), memsize(-1)
242 Database::regBin(this, name
);
252 MainBin::memory(off_t off
)
255 memsize
= ceilPow2((size_t) offset());
258 mem
= new char[memsize
];
259 memset(mem
, 0, memsize
);
262 assert(offset() <= size());
269 typedef Database::stat_list_t::iterator iter_t
;
271 iter_t i
, end
= Database::stats().end();
272 for (i
= Database::stats().begin(); i
!= end
; ++i
) {
275 if (!data
->check() || !data
->baseCheck())
276 panic("stat check failed for %s\n", data
->name
);
280 for (i
= Database::stats().begin(); i
!= end
; ++i
) {
282 if (!(data
->flags
& print
))
283 data
->name
= "__Stat" + to_string(j
++);
286 Database::stats().sort(StatData::less
);
289 if (MainBin::curBin() == NULL
) {
290 static MainBin
mainBin("main bin");
301 for (i
= Database::stats().begin(); i
!= end
; ++i
) {
302 if ((*i
)->name
== (*last
)->name
)
303 panic("same name used twice! name=%s\n", (*i
)->name
);
309 CallbackQueue resetQueue
;
314 // reset non-binned stats
315 Database::stat_list_t::iterator i
= Database::stats().begin();
316 Database::stat_list_t::iterator end
= Database::stats().end();
324 // save the bin so we can go back to where we were
325 MainBin
*orig
= MainBin::curBin();
327 // reset binned stats
328 Database::bin_list_t::iterator bi
= Database::bins().begin();
329 Database::bin_list_t::iterator be
= Database::bins().end();
334 i
= Database::stats().begin();
345 MainBin::curBin() = orig
;
347 resetQueue
.process();
351 registerResetCallback(Callback
*cb
)
356 /* namespace Stats */ }