2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Statistics for Firm. Distribution tables.
16 #include "firmstat_t.h"
19 * calculates a hash value for an address
21 static unsigned addr_hash(const void *object)
23 return hash_ptr(object);
27 * calculates a hash value for an integer
29 static unsigned int_hash(const void *object)
31 return (unsigned)PTR_TO_INT(object);
35 * compare function for integer distribution tables
37 static int int_cmp_fun(const void *elt, const void *key)
39 const distrib_entry_t *p1 = (const distrib_entry_t*)elt;
40 const distrib_entry_t *p2 = (const distrib_entry_t*)key;
42 if (p1->object == p2->object)
44 return p1->object < p2->object ? -1 : 1;
48 * create a new distribution table
50 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
52 distrib_tbl_t *res = XMALLOC(distrib_tbl_t);
54 obstack_init(&res->cnts);
56 /* create the hash-table */
57 res->hash_map = new_pset(cmp_func, 8);
58 res->hash_func = hash_func ? hash_func : addr_hash;
65 * create a new distribution table for an integer distribution
67 distrib_tbl_t *stat_new_int_distrib_tbl(void)
69 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
78 * destroy a distribution table
80 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
83 /* free all entries */
84 obstack_free(&tbl->cnts, NULL);
86 /* delete the hash table */
87 del_pset(tbl->hash_map);
92 * Returns the associates distrib_entry_t for an object
94 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
97 distrib_entry_t *elem;
101 elem = (distrib_entry_t*)pset_find(tbl->hash_map, &key, tbl->hash_func(object));
105 elem = OALLOC(&tbl->cnts, distrib_entry_t);
110 elem->object = object;
112 return (distrib_entry_t*)pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
116 * adds a new object count into the distribution table
118 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
120 distrib_entry_t *elem = distrib_get_entry(tbl, object);
122 cnt_add(&elem->cnt, cnt);
126 * adds a new key count into the integer distribution table
128 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
130 stat_add_distrib_tbl(tbl, INT_TO_PTR(key), cnt);
134 * increases object count by one
136 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
138 distrib_entry_t *elem = distrib_get_entry(tbl, object);
144 * increases key count by one
146 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
148 stat_inc_distrib_tbl(tbl, INT_TO_PTR(key));
152 * inserts a new object with count 0 into the distribution table
153 * if object is already present, nothing happens
155 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
157 /* executed for side effect */
158 (void)distrib_get_entry(tbl, object);
162 * inserts a new key with count 0 into the integer distribution table
163 * if key is already present, nothing happens
165 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)
175 counter_t cnt = ZERO_CNT;
177 foreach_pset(tbl->hash_map, distrib_entry_t, 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)
191 /* integer distribution, need min, max */
192 if (pset_count(tbl->hash_map) == 0)
199 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
200 int value = PTR_TO_INT(entry->object);
207 sum += cnt_to_dbl(&entry->cnt);
209 count = max - min + 1;
213 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
214 sum += cnt_to_dbl(&entry->cnt);
219 return count ? sum / (double)count : 0.0;
223 * calculates the average value of a distribution
225 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
231 if (pset_count(tbl->hash_map) <= 0)
234 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
235 sum += cnt_to_dbl(&entry->cnt) * PTR_TO_INT(entry->object);
236 count += cnt_to_uint(&entry->cnt);
239 foreach_pset(tbl->hash_map, distrib_entry_t, entry) {
240 sum += cnt_to_dbl(&entry->cnt);
245 return count ? sum / (double)count : 0.0;
249 * iterates over all entries in a distribution table
251 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
253 foreach_pset(tbl->hash_map, distrib_entry_t, entry)