1/**
2 * @file db_stat.c
3 * Statistics routines for libdb
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author Philippe Elie
9 */
10
11#include <stdlib.h>
12#include <stdio.h>
13
14#include "odb.h"
15#include "op_types.h"
16
17/// hold various statistics data for a db file
18struct odb_hash_stat_t {
19	odb_node_nr_t node_nr;			/**< allocated node number */
20	odb_node_nr_t used_node_nr;		/**< in use node number */
21	count_type    total_count;		/**< cumulated samples count */
22	odb_index_t   hash_table_size;		/**< hash table entry number */
23	odb_node_nr_t max_list_length;		/**< worst case   */
24	double       average_list_length;	/**< average case */
25	/* do we need variance ? */
26};
27
28odb_hash_stat_t * odb_hash_stat(odb_t const * odb)
29{
30	size_t max_length = 0;
31	double total_length = 0.0;
32	size_t nr_non_empty_list = 0;
33	size_t pos;
34	odb_data_t * data = odb->data;
35
36	odb_hash_stat_t * result = calloc(1, sizeof(odb_hash_stat_t));
37	if (!result) {
38		fprintf(stderr, "not enough memory\n");
39		exit(EXIT_FAILURE);
40	}
41
42	result->node_nr = data->descr->size;
43	result->used_node_nr = data->descr->current_size;
44	result->hash_table_size = data->descr->size * BUCKET_FACTOR;
45
46	/* FIXME: I'm dubious if this do right statistics for hash table
47	 * efficiency check */
48
49	for (pos = 0 ; pos < result->hash_table_size ; ++pos) {
50		size_t cur_length = 0;
51		size_t index = data->hash_base[pos];
52		while (index) {
53			result->total_count += data->node_base[index].value;
54			index = data->node_base[index].next;
55			++cur_length;
56		}
57
58		if (cur_length > max_length)
59			max_length = cur_length;
60
61		if (cur_length) {
62			total_length += cur_length;
63			++nr_non_empty_list;
64		}
65	}
66
67	result->max_list_length = max_length;
68	result->average_list_length = total_length / nr_non_empty_list;
69
70	return result;
71}
72
73
74void odb_hash_display_stat(odb_hash_stat_t const * stat)
75{
76	printf("total node number:   %d\n", stat->node_nr);
77	printf("total used node:     %d\n", stat->used_node_nr);
78	printf("total count:         %llu\n", stat->total_count);
79	printf("hash table size:     %d\n", stat->hash_table_size);
80	printf("greater list length: %d\n", stat->max_list_length);
81	printf("average non empty list length: %2.4f\n", stat->average_list_length);
82}
83
84
85void odb_hash_free_stat(odb_hash_stat_t * stat)
86{
87	free(stat);
88}
89