2 * Copyright (C) 1995-2008 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) {
37 return HASH_PTR(object);
41 * calculates a hash value for an integer
43 static unsigned int_hash(const void *object) {
44 return (unsigned)PTR_TO_INT(object);
48 * compare function for integer distribution tables
50 static int int_cmp_fun(const void *elt, const void *key) {
51 const distrib_entry_t *p1 = elt;
52 const distrib_entry_t *p2 = key;
54 return (char *)p1->object - (char *)p2->object;
58 * create a new distribution table
60 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func) {
61 distrib_tbl_t *res = XMALLOC(distrib_tbl_t);
63 obstack_init(&res->cnts);
65 /* create the hash-table */
66 res->hash_map = new_pset(cmp_func, 8);
67 res->hash_func = hash_func ? hash_func : addr_hash;
74 * create a new distribution table for an integer distribution
76 distrib_tbl_t *stat_new_int_distrib_tbl(void) {
77 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
86 * destroy a distribution table
88 void stat_delete_distrib_tbl(distrib_tbl_t *tbl) {
90 /* free all entries */
91 obstack_free(&tbl->cnts, NULL);
93 /* delete the hash table */
94 del_pset(tbl->hash_map);
99 * Returns the associates distrib_entry_t for an object
101 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object) {
103 distrib_entry_t *elem;
107 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
111 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
116 elem->object = object;
118 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
122 * adds a new object count into the distribution table
124 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt) {
125 distrib_entry_t *elem = distrib_get_entry(tbl, object);
127 cnt_add(&elem->cnt, cnt);
131 * adds a new key count into the integer distribution table
133 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt) {
134 stat_add_distrib_tbl(tbl, INT_TO_PTR(key), cnt);
138 * increases object count by one
140 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object) {
141 distrib_entry_t *elem = distrib_get_entry(tbl, object);
147 * increases key count by one
149 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key) {
150 stat_inc_distrib_tbl(tbl, INT_TO_PTR(key));
154 * inserts a new object with count 0 into the distribution table
155 * if object is already present, nothing happens
157 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object) {
158 /* executed for side effect */
159 (void)distrib_get_entry(tbl, object);
163 * inserts a new key with count 0 into the integer distribution table
164 * if key is already present, nothing happens
166 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key) {
167 stat_insert_distrib_tbl(tbl, INT_TO_PTR(key));
171 * returns the sum over all counters in a distribution table
173 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl) {
174 distrib_entry_t *entry;
175 counter_t cnt = ZERO_CNT;
177 foreach_pset(tbl->hash_map, entry)
178 cnt_add(&cnt, &entry->cnt);
179 return cnt_to_uint(&cnt);
183 * calculates the mean value of a distribution
185 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl) {
186 distrib_entry_t *entry;
191 /* integer distribution, need min, max */
194 entry = pset_first(tbl->hash_map);
200 max = PTR_TO_INT(entry->object);
201 sum = cnt_to_dbl(&entry->cnt);
204 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
205 int value = PTR_TO_INT(entry->object);
212 sum += cnt_to_dbl(&entry->cnt);
214 count = max - min + 1;
218 foreach_pset(tbl->hash_map, entry) {
219 sum += cnt_to_dbl(&entry->cnt);
224 return count ? sum / (double)count : 0.0;
228 * calculates the average value of a distribution
230 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl) {
231 distrib_entry_t *entry;
236 if (pset_count(tbl->hash_map) <= 0)
239 foreach_pset(tbl->hash_map, entry) {
240 sum += cnt_to_dbl(&entry->cnt) * PTR_TO_INT(entry->object);
241 count += cnt_to_uint(&entry->cnt);
244 foreach_pset(tbl->hash_map, entry) {
245 sum += cnt_to_dbl(&entry->cnt);
250 return count ? sum / (double)count : 0.0;
254 * iterates over all entries in a distribution table
256 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env) {
257 distrib_entry_t *entry;
259 foreach_pset(tbl->hash_map, entry)