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
30 #include "firmstat_t.h"
33 * calculates a hash value for an address
35 static unsigned addr_hash(const void *object)
37 return hash_ptr(object);
41 * calculates a hash value for an integer
43 static unsigned int_hash(const void *object)
45 return (unsigned)PTR_TO_INT(object);
49 * compare function for integer distribution tables
51 static int int_cmp_fun(const void *elt, const void *key)
53 const distrib_entry_t *p1 = (const distrib_entry_t*)elt;
54 const distrib_entry_t *p2 = (const distrib_entry_t*)key;
56 if (p1->object == p2->object)
58 return p1->object < p2->object ? -1 : 1;
62 * create a new distribution table
64 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
66 distrib_tbl_t *res = XMALLOC(distrib_tbl_t);
68 obstack_init(&res->cnts);
70 /* create the hash-table */
71 res->hash_map = new_pset(cmp_func, 8);
72 res->hash_func = hash_func ? hash_func : addr_hash;
79 * create a new distribution table for an integer distribution
81 distrib_tbl_t *stat_new_int_distrib_tbl(void)
83 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
92 * destroy a distribution table
94 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
97 /* free all entries */
98 obstack_free(&tbl->cnts, NULL);
100 /* delete the hash table */
101 del_pset(tbl->hash_map);
106 * Returns the associates distrib_entry_t for an object
108 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
111 distrib_entry_t *elem;
115 elem = (distrib_entry_t*)pset_find(tbl->hash_map, &key, tbl->hash_func(object));
119 elem = OALLOC(&tbl->cnts, distrib_entry_t);
124 elem->object = object;
126 return (distrib_entry_t*)pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
130 * adds a new object count into the distribution table
132 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
134 distrib_entry_t *elem = distrib_get_entry(tbl, object);
136 cnt_add(&elem->cnt, cnt);
140 * adds a new key count into the integer distribution table
142 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
144 stat_add_distrib_tbl(tbl, INT_TO_PTR(key), cnt);
148 * increases object count by one
150 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
152 distrib_entry_t *elem = distrib_get_entry(tbl, object);
158 * increases key count by one
160 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
162 stat_inc_distrib_tbl(tbl, INT_TO_PTR(key));
166 * inserts a new object with count 0 into the distribution table
167 * if object is already present, nothing happens
169 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
171 /* executed for side effect */
172 (void)distrib_get_entry(tbl, object);
176 * inserts a new key with count 0 into the integer distribution table
177 * if key is already present, nothing happens
179 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
181 stat_insert_distrib_tbl(tbl, INT_TO_PTR(key));
185 * returns the sum over all counters in a distribution table
187 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl)
189 counter_t cnt = ZERO_CNT;
191 foreach_pset(tbl->hash_map, distrib_entry_t, entry)
192 cnt_add(&cnt, &entry->cnt);
193 return cnt_to_uint(&cnt);
197 * calculates the mean value of a distribution
199 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
205 /* integer distribution, need min, max */
206 if (pset_count(tbl->hash_map) == 0)
213 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
214 int value = PTR_TO_INT(entry->object);
221 sum += cnt_to_dbl(&entry->cnt);
223 count = max - min + 1;
227 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
228 sum += cnt_to_dbl(&entry->cnt);
233 return count ? sum / (double)count : 0.0;
237 * calculates the average value of a distribution
239 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
245 if (pset_count(tbl->hash_map) <= 0)
248 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
249 sum += cnt_to_dbl(&entry->cnt) * PTR_TO_INT(entry->object);
250 count += cnt_to_uint(&entry->cnt);
253 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
254 sum += cnt_to_dbl(&entry->cnt);
259 return count ? sum / (double)count : 0.0;
263 * iterates over all entries in a distribution table
265 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
267 foreach_pset(tbl->hash_map, distrib_entry_t, entry)