2 * Copyright (C) 1995-2011 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
31 #include "firmstat_t.h"
34 * calculates a hash value for an address
36 static unsigned addr_hash(const void *object)
38 return HASH_PTR(object);
42 * calculates a hash value for an integer
44 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)
54 const distrib_entry_t *p1 = (const distrib_entry_t*)elt;
55 const distrib_entry_t *p2 = (const distrib_entry_t*)key;
57 if (p1->object == p2->object)
59 return p1->object < p2->object ? -1 : 1;
63 * create a new distribution table
65 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
67 distrib_tbl_t *res = XMALLOC(distrib_tbl_t);
69 obstack_init(&res->cnts);
71 /* create the hash-table */
72 res->hash_map = new_pset(cmp_func, 8);
73 res->hash_func = hash_func ? hash_func : addr_hash;
80 * create a new distribution table for an integer distribution
82 distrib_tbl_t *stat_new_int_distrib_tbl(void)
84 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
93 * destroy a distribution table
95 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
98 /* free all entries */
99 obstack_free(&tbl->cnts, NULL);
101 /* delete the hash table */
102 del_pset(tbl->hash_map);
107 * Returns the associates distrib_entry_t for an object
109 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
112 distrib_entry_t *elem;
116 elem = (distrib_entry_t*)pset_find(tbl->hash_map, &key, tbl->hash_func(object));
120 elem = OALLOC(&tbl->cnts, distrib_entry_t);
125 elem->object = object;
127 return (distrib_entry_t*)pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
131 * adds a new object count into the distribution table
133 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
135 distrib_entry_t *elem = distrib_get_entry(tbl, object);
137 cnt_add(&elem->cnt, cnt);
141 * adds a new key count into the integer distribution table
143 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
145 stat_add_distrib_tbl(tbl, INT_TO_PTR(key), cnt);
149 * increases object count by one
151 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
153 distrib_entry_t *elem = distrib_get_entry(tbl, object);
159 * increases key count by one
161 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
163 stat_inc_distrib_tbl(tbl, INT_TO_PTR(key));
167 * inserts a new object with count 0 into the distribution table
168 * if object is already present, nothing happens
170 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
172 /* executed for side effect */
173 (void)distrib_get_entry(tbl, object);
177 * inserts a new key with count 0 into the integer distribution table
178 * if key is already present, nothing happens
180 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
182 stat_insert_distrib_tbl(tbl, INT_TO_PTR(key));
186 * returns the sum over all counters in a distribution table
188 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl)
190 distrib_entry_t *entry;
191 counter_t cnt = ZERO_CNT;
193 foreach_pset(tbl->hash_map, distrib_entry_t*, entry)
194 cnt_add(&cnt, &entry->cnt);
195 return cnt_to_uint(&cnt);
199 * calculates the mean value of a distribution
201 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
203 distrib_entry_t *entry;
208 /* integer distribution, need min, max */
211 entry = (distrib_entry_t*)pset_first(tbl->hash_map);
217 max = PTR_TO_INT(entry->object);
218 sum = cnt_to_dbl(&entry->cnt);
221 for (entry = (distrib_entry_t*)pset_next(tbl->hash_map); entry != NULL;
222 entry = (distrib_entry_t*)pset_next(tbl->hash_map)) {
223 int value = PTR_TO_INT(entry->object);
230 sum += cnt_to_dbl(&entry->cnt);
232 count = max - min + 1;
236 foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
237 sum += cnt_to_dbl(&entry->cnt);
242 return count ? sum / (double)count : 0.0;
246 * calculates the average value of a distribution
248 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
250 distrib_entry_t *entry;
255 if (pset_count(tbl->hash_map) <= 0)
258 foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
259 sum += cnt_to_dbl(&entry->cnt) * PTR_TO_INT(entry->object);
260 count += cnt_to_uint(&entry->cnt);
263 foreach_pset(tbl->hash_map, distrib_entry_t*, entry) {
264 sum += cnt_to_dbl(&entry->cnt);
269 return count ? sum / (double)count : 0.0;
273 * iterates over all entries in a distribution table
275 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
277 distrib_entry_t *entry;
279 foreach_pset(tbl->hash_map, distrib_entry_t*, entry)