2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* This file is a wrapper bitset implementation for the other bitset
20 implementations. It provides bitset compatibility checking and
21 statistics gathering without having to instrument the bitset
22 implementations. When statistics gathering is enabled, the bitset
23 operations get vectored through here and we then call the appropriate
28 #include "bitset_stats.h"
40 #define _(Msgid) gettext (Msgid)
42 /* Configuration macros. */
43 #define BITSET_STATS_FILE "bitset.dat"
44 #define BITSET_LOG_COUNT_BINS 10
45 #define BITSET_LOG_SIZE_BINS 16
46 #define BITSET_DENSITY_BINS 20
49 /* Accessor macros. */
50 #define BITSET_STATS_ALLOCS_INC(TYPE) \
51 bitset_stats_info->types[(TYPE)].allocs++
52 #define BITSET_STATS_FREES_INC(BSET) \
53 bitset_stats_info->types[BITSET_TYPE_ (BSET)].frees++
54 #define BITSET_STATS_SETS_INC(BSET) \
55 bitset_stats_info->types[BITSET_TYPE_ (BSET)].sets++
56 #define BITSET_STATS_CACHE_SETS_INC(BSET) \
57 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_sets++
58 #define BITSET_STATS_RESETS_INC(BSET) \
59 bitset_stats_info->types[BITSET_TYPE_ (BSET)].resets++
60 #define BITSET_STATS_CACHE_RESETS_INC(BSET) \
61 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_resets++
62 #define BITSET_STATS_TESTS_INC(BSET) \
63 bitset_stats_info->types[BITSET_TYPE_ (BSET)].tests++
64 #define BITSET_STATS_CACHE_TESTS_INC(BSET) \
65 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_tests++
66 #define BITSET_STATS_LISTS_INC(BSET) \
67 bitset_stats_info->types[BITSET_TYPE_ (BSET)].lists++
68 #define BITSET_STATS_LIST_COUNTS_INC(BSET, I) \
69 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_counts[(I)]++
70 #define BITSET_STATS_LIST_SIZES_INC(BSET, I) \
71 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_sizes[(I)]++
72 #define BITSET_STATS_LIST_DENSITY_INC(BSET, I) \
73 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_density[(I)]++
76 struct bitset_type_info_struct
82 unsigned int cache_sets
;
84 unsigned int cache_resets
;
86 unsigned int cache_tests
;
87 unsigned int list_counts
[BITSET_LOG_COUNT_BINS
];
88 unsigned int list_sizes
[BITSET_LOG_SIZE_BINS
];
89 unsigned int list_density
[BITSET_DENSITY_BINS
];
92 struct bitset_stats_info_struct
95 struct bitset_type_info_struct types
[BITSET_TYPE_NUM
];
99 struct bitset_stats_info_struct bitset_stats_info_data
;
100 struct bitset_stats_info_struct
*bitset_stats_info
;
101 bool bitset_stats_enabled
= false;
104 /* Print a percentage histogram with message MSG to FILE. */
106 bitset_percent_histogram_print (FILE *file
, const char *name
, const char *msg
,
107 unsigned int n_bins
, unsigned int *bins
)
113 for (i
= 0; i
< n_bins
; i
++)
119 fprintf (file
, "%s %s", name
, msg
);
120 for (i
= 0; i
< n_bins
; i
++)
121 fprintf (file
, "%.0f-%.0f%%\t%8u (%5.1f%%)\n",
123 (i
+ 1) * 100.0 / n_bins
, bins
[i
],
124 (100.0 * bins
[i
]) / total
);
128 /* Print a log histogram with message MSG to FILE. */
130 bitset_log_histogram_print (FILE *file
, const char *name
, const char *msg
,
131 unsigned int n_bins
, unsigned int *bins
)
135 unsigned int max_width
;
138 for (i
= 0; i
< n_bins
; i
++)
144 /* Determine number of useful bins. */
145 for (i
= n_bins
; i
> 3 && ! bins
[i
- 1]; i
--)
149 /* 2 * ceil (log10 (2) * (N - 1)) + 1. */
150 max_width
= 2 * (unsigned int) (0.30103 * (n_bins
- 1) + 0.9999) + 1;
152 fprintf (file
, "%s %s", name
, msg
);
153 for (i
= 0; i
< 2; i
++)
154 fprintf (file
, "%*d\t%8u (%5.1f%%)\n",
155 max_width
, i
, bins
[i
], 100.0 * bins
[i
] / total
);
157 for (; i
< n_bins
; i
++)
158 fprintf (file
, "%*lu-%lu\t%8u (%5.1f%%)\n",
159 max_width
- ((unsigned int) (0.30103 * (i
) + 0.9999) + 1),
163 (100.0 * bins
[i
]) / total
);
167 /* Print bitset statistics to FILE. */
169 bitset_stats_print_1 (FILE *file
, const char *name
,
170 struct bitset_type_info_struct
*stats
)
175 fprintf (file
, "%s:\n", name
);
176 fprintf (file
, _("%u bitset_allocs, %u freed (%.2f%%).\n"),
177 stats
->allocs
, stats
->frees
,
178 stats
->allocs
? 100.0 * stats
->frees
/ stats
->allocs
: 0);
179 fprintf (file
, _("%u bitset_sets, %u cached (%.2f%%)\n"),
180 stats
->sets
, stats
->cache_sets
,
181 stats
->sets
? 100.0 * stats
->cache_sets
/ stats
->sets
: 0);
182 fprintf (file
, _("%u bitset_resets, %u cached (%.2f%%)\n"),
183 stats
->resets
, stats
->cache_resets
,
184 stats
->resets
? 100.0 * stats
->cache_resets
/ stats
->resets
: 0);
185 fprintf (file
, _("%u bitset_tests, %u cached (%.2f%%)\n"),
186 stats
->tests
, stats
->cache_tests
,
187 stats
->tests
? 100.0 * stats
->cache_tests
/ stats
->tests
: 0);
189 fprintf (file
, _("%u bitset_lists\n"), stats
->lists
);
191 bitset_log_histogram_print (file
, name
, _("count log histogram\n"),
192 BITSET_LOG_COUNT_BINS
, stats
->list_counts
);
194 bitset_log_histogram_print (file
, name
, _("size log histogram\n"),
195 BITSET_LOG_SIZE_BINS
, stats
->list_sizes
);
197 bitset_percent_histogram_print (file
, name
, _("density histogram\n"),
198 BITSET_DENSITY_BINS
, stats
->list_density
);
202 /* Print all bitset statistics to FILE. */
204 bitset_stats_print (FILE *file
, bool verbose ATTRIBUTE_UNUSED
)
208 if (!bitset_stats_info
)
211 fprintf (file
, _("Bitset statistics:\n\n"));
213 if (bitset_stats_info
->runs
> 1)
214 fprintf (file
, _("Accumulated runs = %u\n"), bitset_stats_info
->runs
);
216 for (i
= 0; i
< BITSET_TYPE_NUM
; i
++)
217 bitset_stats_print_1 (file
, bitset_type_names
[i
],
218 &bitset_stats_info
->types
[i
]);
222 /* Initialise bitset statistics logging. */
224 bitset_stats_enable (void)
226 if (!bitset_stats_info
)
227 bitset_stats_info
= &bitset_stats_info_data
;
228 bitset_stats_enabled
= true;
233 bitset_stats_disable (void)
235 bitset_stats_enabled
= false;
239 /* Read bitset statistics file. */
241 bitset_stats_read (const char *file_name
)
245 if (!bitset_stats_info
)
249 file_name
= BITSET_STATS_FILE
;
251 file
= fopen (file_name
, "r");
254 if (fread (&bitset_stats_info_data
, sizeof (bitset_stats_info_data
),
258 perror (_("Could not read stats file."));
260 fprintf (stderr
, _("Bad stats file size.\n"));
262 if (fclose (file
) != 0)
263 perror (_("Could not read stats file."));
265 bitset_stats_info_data
.runs
++;
269 /* Write bitset statistics file. */
271 bitset_stats_write (const char *file_name
)
275 if (!bitset_stats_info
)
279 file_name
= BITSET_STATS_FILE
;
281 file
= fopen (file_name
, "w");
284 if (fwrite (&bitset_stats_info_data
, sizeof (bitset_stats_info_data
),
286 perror (_("Could not write stats file."));
287 if (fclose (file
) != 0)
288 perror (_("Could not write stats file."));
291 perror (_("Could not open stats file for writing."));
295 /* Dump bitset statistics to FILE. */
297 bitset_stats_dump (FILE *file
)
299 bitset_stats_print (file
, false);
303 /* Function to be called from debugger to print bitset stats. */
305 debug_bitset_stats (void)
307 bitset_stats_print (stderr
, true);
312 bitset_stats_set (bitset dst
, bitset_bindex bitno
)
314 bitset bset
= dst
->s
.bset
;
315 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
316 bitset_windex offset
= wordno
- bset
->b
.cindex
;
318 BITSET_STATS_SETS_INC (bset
);
320 if (offset
< bset
->b
.csize
)
322 bset
->b
.cdata
[offset
] |= (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
323 BITSET_STATS_CACHE_SETS_INC (bset
);
326 BITSET_SET_ (bset
, bitno
);
331 bitset_stats_reset (bitset dst
, bitset_bindex bitno
)
333 bitset bset
= dst
->s
.bset
;
334 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
335 bitset_windex offset
= wordno
- bset
->b
.cindex
;
337 BITSET_STATS_RESETS_INC (bset
);
339 if (offset
< bset
->b
.csize
)
341 bset
->b
.cdata
[offset
] &=
342 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
343 BITSET_STATS_CACHE_RESETS_INC (bset
);
346 BITSET_RESET_ (bset
, bitno
);
351 bitset_stats_toggle (bitset src
, bitset_bindex bitno
)
353 return BITSET_TOGGLE_ (src
->s
.bset
, bitno
);
358 bitset_stats_test (bitset src
, bitset_bindex bitno
)
360 bitset bset
= src
->s
.bset
;
361 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
362 bitset_windex offset
= wordno
- bset
->b
.cindex
;
364 BITSET_STATS_TESTS_INC (bset
);
366 if (offset
< bset
->b
.csize
)
368 BITSET_STATS_CACHE_TESTS_INC (bset
);
369 return (bset
->b
.cdata
[offset
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
372 return BITSET_TEST_ (bset
, bitno
);
377 bitset_stats_resize (bitset src
, bitset_bindex size
)
379 return BITSET_RESIZE_ (src
->s
.bset
, size
);
384 bitset_stats_size (bitset src
)
386 return BITSET_SIZE_ (src
->s
.bset
);
391 bitset_stats_count (bitset src
)
393 return BITSET_COUNT_ (src
->s
.bset
);
398 bitset_stats_empty_p (bitset dst
)
400 return BITSET_EMPTY_P_ (dst
->s
.bset
);
405 bitset_stats_ones (bitset dst
)
407 BITSET_ONES_ (dst
->s
.bset
);
412 bitset_stats_zero (bitset dst
)
414 BITSET_ZERO_ (dst
->s
.bset
);
419 bitset_stats_copy (bitset dst
, bitset src
)
421 BITSET_CHECK2_ (dst
, src
);
422 BITSET_COPY_ (dst
->s
.bset
, src
->s
.bset
);
427 bitset_stats_disjoint_p (bitset dst
, bitset src
)
429 BITSET_CHECK2_ (dst
, src
);
430 return BITSET_DISJOINT_P_ (dst
->s
.bset
, src
->s
.bset
);
435 bitset_stats_equal_p (bitset dst
, bitset src
)
437 BITSET_CHECK2_ (dst
, src
);
438 return BITSET_EQUAL_P_ (dst
->s
.bset
, src
->s
.bset
);
443 bitset_stats_not (bitset dst
, bitset src
)
445 BITSET_CHECK2_ (dst
, src
);
446 BITSET_NOT_ (dst
->s
.bset
, src
->s
.bset
);
451 bitset_stats_subset_p (bitset dst
, bitset src
)
453 BITSET_CHECK2_ (dst
, src
);
454 return BITSET_SUBSET_P_ (dst
->s
.bset
, src
->s
.bset
);
459 bitset_stats_and (bitset dst
, bitset src1
, bitset src2
)
461 BITSET_CHECK3_ (dst
, src1
, src2
);
462 BITSET_AND_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
467 bitset_stats_and_cmp (bitset dst
, bitset src1
, bitset src2
)
469 BITSET_CHECK3_ (dst
, src1
, src2
);
470 return BITSET_AND_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
475 bitset_stats_andn (bitset dst
, bitset src1
, bitset src2
)
477 BITSET_CHECK3_ (dst
, src1
, src2
);
478 BITSET_ANDN_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
483 bitset_stats_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
485 BITSET_CHECK3_ (dst
, src1
, src2
);
486 return BITSET_ANDN_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
491 bitset_stats_or (bitset dst
, bitset src1
, bitset src2
)
493 BITSET_CHECK3_ (dst
, src1
, src2
);
494 BITSET_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
499 bitset_stats_or_cmp (bitset dst
, bitset src1
, bitset src2
)
501 BITSET_CHECK3_ (dst
, src1
, src2
);
502 return BITSET_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
507 bitset_stats_xor (bitset dst
, bitset src1
, bitset src2
)
509 BITSET_CHECK3_ (dst
, src1
, src2
);
510 BITSET_XOR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
515 bitset_stats_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
517 BITSET_CHECK3_ (dst
, src1
, src2
);
518 return BITSET_XOR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
523 bitset_stats_and_or (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
525 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
526 BITSET_AND_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
531 bitset_stats_and_or_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
533 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
534 return BITSET_AND_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
539 bitset_stats_andn_or (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
541 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
542 BITSET_ANDN_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
547 bitset_stats_andn_or_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
549 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
550 return BITSET_ANDN_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
555 bitset_stats_or_and (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
557 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
558 BITSET_OR_AND_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
563 bitset_stats_or_and_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
565 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
566 return BITSET_OR_AND_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
571 bitset_stats_list (bitset bset
, bitset_bindex
*list
,
572 bitset_bindex num
, bitset_bindex
*next
)
578 enum bitset_type type
;
580 count
= BITSET_LIST_ (bset
->s
.bset
, list
, num
, next
);
582 type
= BITSET_TYPE_ (bset
->s
.bset
);
583 BITSET_STATS_LISTS_INC (bset
->s
.bset
);
585 /* Log histogram of number of set bits. */
586 for (i
= 0, tmp
= count
; tmp
; tmp
>>= 1, i
++)
588 if (i
>= BITSET_LOG_COUNT_BINS
)
589 i
= BITSET_LOG_COUNT_BINS
- 1;
590 BITSET_STATS_LIST_COUNTS_INC (bset
->s
.bset
, i
);
592 /* Log histogram of number of bits in set. */
593 size
= BITSET_SIZE_ (bset
->s
.bset
);
594 for (i
= 0, tmp
= size
; tmp
; tmp
>>= 1, i
++)
596 if (i
>= BITSET_LOG_SIZE_BINS
)
597 i
= BITSET_LOG_SIZE_BINS
- 1;
598 BITSET_STATS_LIST_SIZES_INC (bset
->s
.bset
, i
);
600 /* Histogram of fraction of bits set. */
601 i
= size
? (count
* BITSET_DENSITY_BINS
) / size
: 0;
602 if (i
>= BITSET_DENSITY_BINS
)
603 i
= BITSET_DENSITY_BINS
- 1;
604 BITSET_STATS_LIST_DENSITY_INC (bset
->s
.bset
, i
);
610 bitset_stats_list_reverse (bitset bset
, bitset_bindex
*list
,
611 bitset_bindex num
, bitset_bindex
*next
)
613 return BITSET_LIST_REVERSE_ (bset
->s
.bset
, list
, num
, next
);
618 bitset_stats_free (bitset bset
)
620 BITSET_STATS_FREES_INC (bset
->s
.bset
);
621 BITSET_FREE_ (bset
->s
.bset
);
625 struct bitset_vtable bitset_stats_vtable
= {
633 bitset_stats_empty_p
,
637 bitset_stats_disjoint_p
,
638 bitset_stats_equal_p
,
640 bitset_stats_subset_p
,
642 bitset_stats_and_cmp
,
644 bitset_stats_andn_cmp
,
648 bitset_stats_xor_cmp
,
650 bitset_stats_and_or_cmp
,
651 bitset_stats_andn_or
,
652 bitset_stats_andn_or_cmp
,
654 bitset_stats_or_and_cmp
,
656 bitset_stats_list_reverse
,
662 /* Return enclosed bitset type. */
664 bitset_stats_type_get (bitset bset
)
666 return BITSET_TYPE_ (bset
->s
.bset
);
671 bitset_stats_bytes (void)
673 return sizeof (struct bitset_stats_struct
);
678 bitset_stats_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
683 bset
->b
.vtable
= &bitset_stats_vtable
;
690 BITSET_NBITS_ (bset
) = n_bits
;
692 /* Set up the actual bitset implementation that
693 we are a wrapper over. */
700 bytes
= abitset_bytes (n_bits
);
701 sbset
= xcalloc (1, bytes
);
702 abitset_init (sbset
, n_bits
);
706 bytes
= lbitset_bytes (n_bits
);
707 sbset
= xcalloc (1, bytes
);
708 lbitset_init (sbset
, n_bits
);
712 bytes
= ebitset_bytes (n_bits
);
713 sbset
= xcalloc (1, bytes
);
714 ebitset_init (sbset
, n_bits
);
718 bytes
= vbitset_bytes (n_bits
);
719 sbset
= xcalloc (1, bytes
);
720 vbitset_init (sbset
, n_bits
);
724 bset
->s
.bset
= sbset
;
726 BITSET_STATS_ALLOCS_INC (type
);