2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm. Distribution tables.
23 * @author Michael Beck
33 #include "firmstat_t.h"
36 * calculates a hash value for an address
38 static unsigned addr_hash(const void *object) {
39 return HASH_PTR(object);
43 * calculates a hash value for an integer
45 static unsigned int_hash(const void *object) {
46 return (unsigned)PTR_TO_INT(object);
50 * compare function for integer distribution tables
52 static int int_cmp_fun(const void *elt, const void *key) {
53 const distrib_entry_t *p1 = elt;
54 const distrib_entry_t *p2 = key;
56 return (char *)p1->object - (char *)p2->object;
60 * create a new distribution table
62 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func) {
65 res = xmalloc(sizeof(*res));
67 obstack_init(&res->cnts);
69 /* create the hash-table */
70 res->hash_map = new_pset(cmp_func, 8);
71 res->hash_func = hash_func ? hash_func : addr_hash;
78 * create a new distribution table for an integer distribution
80 distrib_tbl_t *stat_new_int_distrib_tbl(void) {
81 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
90 * destroy a distribution table
92 void stat_delete_distrib_tbl(distrib_tbl_t *tbl) {
94 /* free all entries */
95 obstack_free(&tbl->cnts, NULL);
97 /* delete the hash table */
98 del_pset(tbl->hash_map);
103 * Returns the associates distrib_entry_t for an object
105 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object) {
107 distrib_entry_t *elem;
111 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
115 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
120 elem->object = object;
122 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
126 * adds a new object count into the distribution table
128 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt) {
129 distrib_entry_t *elem = distrib_get_entry(tbl, object);
131 cnt_add(&elem->cnt, cnt);
135 * adds a new key count into the integer distribution table
137 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt) {
138 stat_add_distrib_tbl(tbl, (const void *)key, cnt);
142 * increases object count by one
144 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object) {
145 distrib_entry_t *elem = distrib_get_entry(tbl, object);
151 * increases key count by one
153 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key) {
154 stat_inc_distrib_tbl(tbl, (const void *)key);
158 * inserts a new object with count 0 into the distribution table
159 * if object is already present, nothing happens
161 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object) {
162 /* executed for side effect */
163 (void)distrib_get_entry(tbl, object);
167 * inserts a new key with count 0 into the integer distribution table
168 * if key is already present, nothing happens
170 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key) {
171 stat_insert_distrib_tbl(tbl, (const void *)key);
175 * returns the sum over all counters in a distribution table
177 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl) {
178 distrib_entry_t *entry;
179 counter_t cnt = ZERO_CNT;
181 foreach_pset(tbl->hash_map, entry)
182 cnt_add(&cnt, &entry->cnt);
183 return cnt_to_uint(&cnt);
187 * calculates the mean value of a distribution
189 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl) {
190 distrib_entry_t *entry;
195 /* integer distribution, need min, max */
198 entry = pset_first(tbl->hash_map);
204 max = (int)entry->object;
205 sum = cnt_to_dbl(&entry->cnt);
208 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
209 int value = (int)entry->object;
216 sum += cnt_to_dbl(&entry->cnt);
218 count = max - min + 1;
222 foreach_pset(tbl->hash_map, entry) {
223 sum += cnt_to_dbl(&entry->cnt);
228 return count ? sum / (double)count : 0.0;
232 * calculates the average value of a distribution
234 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl) {
235 distrib_entry_t *entry;
240 if (pset_count(tbl->hash_map) <= 0)
243 foreach_pset(tbl->hash_map, entry) {
244 sum += cnt_to_dbl(&entry->cnt) * (int)entry->object;
245 count += cnt_to_uint(&entry->cnt);
248 foreach_pset(tbl->hash_map, entry) {
249 sum += cnt_to_dbl(&entry->cnt);
254 return count ? sum / (double)count : 0.0;
258 * iterates over all entries in a distribution table
260 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env) {
261 distrib_entry_t *entry;
263 foreach_pset(tbl->hash_map, entry)