2 Copyright (C) 2002, 2003, 2004 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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* This file is a wrapper bitset implementation for the other bitset
21 implementations. It provides bitset compatibility checking and
22 statistics gathering without having to instrument the bitset
23 implementations. When statistics gathering is enabled, the bitset
24 operations get vectored through here and we then call the appropriate
37 #include "bitset_stats.h"
43 #define _(Msgid) gettext (Msgid)
45 /* Configuration macros. */
46 #define BITSET_STATS_FILE "bitset.dat"
47 #define BITSET_LOG_COUNT_BINS 10
48 #define BITSET_LOG_SIZE_BINS 16
49 #define BITSET_DENSITY_BINS 20
52 /* Accessor macros. */
53 #define BITSET_STATS_ALLOCS_INC(TYPE) \
54 bitset_stats_info->types[(TYPE)].allocs++
55 #define BITSET_STATS_FREES_INC(BSET) \
56 bitset_stats_info->types[BITSET_TYPE_ (BSET)].frees++
57 #define BITSET_STATS_SETS_INC(BSET) \
58 bitset_stats_info->types[BITSET_TYPE_ (BSET)].sets++
59 #define BITSET_STATS_CACHE_SETS_INC(BSET) \
60 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_sets++
61 #define BITSET_STATS_RESETS_INC(BSET) \
62 bitset_stats_info->types[BITSET_TYPE_ (BSET)].resets++
63 #define BITSET_STATS_CACHE_RESETS_INC(BSET) \
64 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_resets++
65 #define BITSET_STATS_TESTS_INC(BSET) \
66 bitset_stats_info->types[BITSET_TYPE_ (BSET)].tests++
67 #define BITSET_STATS_CACHE_TESTS_INC(BSET) \
68 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_tests++
69 #define BITSET_STATS_LISTS_INC(BSET) \
70 bitset_stats_info->types[BITSET_TYPE_ (BSET)].lists++
71 #define BITSET_STATS_LIST_COUNTS_INC(BSET, I) \
72 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_counts[(I)]++
73 #define BITSET_STATS_LIST_SIZES_INC(BSET, I) \
74 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_sizes[(I)]++
75 #define BITSET_STATS_LIST_DENSITY_INC(BSET, I) \
76 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_density[(I)]++
79 struct bitset_type_info_struct
85 unsigned int cache_sets
;
87 unsigned int cache_resets
;
89 unsigned int cache_tests
;
90 unsigned int list_counts
[BITSET_LOG_COUNT_BINS
];
91 unsigned int list_sizes
[BITSET_LOG_SIZE_BINS
];
92 unsigned int list_density
[BITSET_DENSITY_BINS
];
95 struct bitset_stats_info_struct
98 struct bitset_type_info_struct types
[BITSET_TYPE_NUM
];
102 struct bitset_stats_info_struct bitset_stats_info_data
;
103 struct bitset_stats_info_struct
*bitset_stats_info
;
104 bool bitset_stats_enabled
= false;
107 /* Print a percentage histogram with message MSG to FILE. */
109 bitset_percent_histogram_print (FILE *file
, const char *name
, const char *msg
,
110 unsigned int n_bins
, unsigned int *bins
)
116 for (i
= 0; i
< n_bins
; i
++)
122 fprintf (file
, "%s %s", name
, msg
);
123 for (i
= 0; i
< n_bins
; i
++)
124 fprintf (file
, "%.0f-%.0f%%\t%8u (%5.1f%%)\n",
126 (i
+ 1) * 100.0 / n_bins
, bins
[i
],
127 (100.0 * bins
[i
]) / total
);
131 /* Print a log histogram with message MSG to FILE. */
133 bitset_log_histogram_print (FILE *file
, const char *name
, const char *msg
,
134 unsigned int n_bins
, unsigned int *bins
)
138 unsigned int max_width
;
141 for (i
= 0; i
< n_bins
; i
++)
147 /* Determine number of useful bins. */
148 for (i
= n_bins
; i
> 3 && ! bins
[i
- 1]; i
--)
152 /* 2 * ceil (log10 (2) * (N - 1)) + 1. */
153 max_width
= 2 * (unsigned int) (0.30103 * (n_bins
- 1) + 0.9999) + 1;
155 fprintf (file
, "%s %s", name
, msg
);
156 for (i
= 0; i
< 2; i
++)
157 fprintf (file
, "%*d\t%8u (%5.1f%%)\n",
158 max_width
, i
, bins
[i
], 100.0 * bins
[i
] / total
);
160 for (; i
< n_bins
; i
++)
161 fprintf (file
, "%*lu-%lu\t%8u (%5.1f%%)\n",
162 max_width
- ((unsigned int) (0.30103 * (i
) + 0.9999) + 1),
166 (100.0 * bins
[i
]) / total
);
170 /* Print bitset statistics to FILE. */
172 bitset_stats_print_1 (FILE *file
, const char *name
,
173 struct bitset_type_info_struct
*stats
)
178 fprintf (file
, "%s:\n", name
);
179 fprintf (file
, _("%u bitset_allocs, %u freed (%.2f%%).\n"),
180 stats
->allocs
, stats
->frees
,
181 stats
->allocs
? 100.0 * stats
->frees
/ stats
->allocs
: 0);
182 fprintf (file
, _("%u bitset_sets, %u cached (%.2f%%)\n"),
183 stats
->sets
, stats
->cache_sets
,
184 stats
->sets
? 100.0 * stats
->cache_sets
/ stats
->sets
: 0);
185 fprintf (file
, _("%u bitset_resets, %u cached (%.2f%%)\n"),
186 stats
->resets
, stats
->cache_resets
,
187 stats
->resets
? 100.0 * stats
->cache_resets
/ stats
->resets
: 0);
188 fprintf (file
, _("%u bitset_tests, %u cached (%.2f%%)\n"),
189 stats
->tests
, stats
->cache_tests
,
190 stats
->tests
? 100.0 * stats
->cache_tests
/ stats
->tests
: 0);
192 fprintf (file
, _("%u bitset_lists\n"), stats
->lists
);
194 bitset_log_histogram_print (file
, name
, _("count log histogram\n"),
195 BITSET_LOG_COUNT_BINS
, stats
->list_counts
);
197 bitset_log_histogram_print (file
, name
, _("size log histogram\n"),
198 BITSET_LOG_SIZE_BINS
, stats
->list_sizes
);
200 bitset_percent_histogram_print (file
, name
, _("density histogram\n"),
201 BITSET_DENSITY_BINS
, stats
->list_density
);
205 /* Print all bitset statistics to FILE. */
207 bitset_stats_print (FILE *file
, bool verbose ATTRIBUTE_UNUSED
)
211 if (!bitset_stats_info
)
214 fprintf (file
, _("Bitset statistics:\n\n"));
216 if (bitset_stats_info
->runs
> 1)
217 fprintf (file
, _("Accumulated runs = %u\n"), bitset_stats_info
->runs
);
219 for (i
= 0; i
< BITSET_TYPE_NUM
; i
++)
220 bitset_stats_print_1 (file
, bitset_type_names
[i
],
221 &bitset_stats_info
->types
[i
]);
225 /* Initialise bitset statistics logging. */
227 bitset_stats_enable (void)
229 if (!bitset_stats_info
)
230 bitset_stats_info
= &bitset_stats_info_data
;
231 bitset_stats_enabled
= true;
236 bitset_stats_disable (void)
238 bitset_stats_enabled
= false;
242 /* Read bitset statistics file. */
244 bitset_stats_read (const char *filename
)
248 if (!bitset_stats_info
)
252 filename
= BITSET_STATS_FILE
;
254 file
= fopen (filename
, "r");
257 if (fread (&bitset_stats_info_data
, sizeof (bitset_stats_info_data
),
261 perror (_("Could not read stats file."));
263 fprintf (stderr
, _("Bad stats file size.\n"));
265 if (fclose (file
) != 0)
266 perror (_("Could not read stats file."));
268 bitset_stats_info_data
.runs
++;
272 /* Write bitset statistics file. */
274 bitset_stats_write (const char *filename
)
278 if (!bitset_stats_info
)
282 filename
= BITSET_STATS_FILE
;
284 file
= fopen (filename
, "w");
287 if (fwrite (&bitset_stats_info_data
, sizeof (bitset_stats_info_data
),
289 perror (_("Could not write stats file."));
290 if (fclose (file
) != 0)
291 perror (_("Could not write stats file."));
294 perror (_("Could not open stats file for writing."));
298 /* Dump bitset statistics to FILE. */
300 bitset_stats_dump (FILE *file
)
302 bitset_stats_print (file
, false);
306 /* Function to be called from debugger to print bitset stats. */
308 debug_bitset_stats (void)
310 bitset_stats_print (stderr
, true);
315 bitset_stats_set (bitset dst
, bitset_bindex bitno
)
317 bitset bset
= dst
->s
.bset
;
318 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
319 bitset_windex offset
= wordno
- bset
->b
.cindex
;
321 BITSET_STATS_SETS_INC (bset
);
323 if (offset
< bset
->b
.csize
)
325 bset
->b
.cdata
[offset
] |= (bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
);
326 BITSET_STATS_CACHE_SETS_INC (bset
);
329 BITSET_SET_ (bset
, bitno
);
334 bitset_stats_reset (bitset dst
, bitset_bindex bitno
)
336 bitset bset
= dst
->s
.bset
;
337 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
338 bitset_windex offset
= wordno
- bset
->b
.cindex
;
340 BITSET_STATS_RESETS_INC (bset
);
342 if (offset
< bset
->b
.csize
)
344 bset
->b
.cdata
[offset
] &=
345 ~((bitset_word
) 1 << (bitno
% BITSET_WORD_BITS
));
346 BITSET_STATS_CACHE_RESETS_INC (bset
);
349 BITSET_RESET_ (bset
, bitno
);
354 bitset_stats_toggle (bitset src
, bitset_bindex bitno
)
356 return BITSET_TOGGLE_ (src
->s
.bset
, bitno
);
361 bitset_stats_test (bitset src
, bitset_bindex bitno
)
363 bitset bset
= src
->s
.bset
;
364 bitset_windex wordno
= bitno
/ BITSET_WORD_BITS
;
365 bitset_windex offset
= wordno
- bset
->b
.cindex
;
367 BITSET_STATS_TESTS_INC (bset
);
369 if (offset
< bset
->b
.csize
)
371 BITSET_STATS_CACHE_TESTS_INC (bset
);
372 return (bset
->b
.cdata
[offset
] >> (bitno
% BITSET_WORD_BITS
)) & 1;
375 return BITSET_TEST_ (bset
, bitno
);
380 bitset_stats_resize (bitset src
, bitset_bindex size
)
382 return BITSET_RESIZE_ (src
->s
.bset
, size
);
387 bitset_stats_size (bitset src
)
389 return BITSET_SIZE_ (src
->s
.bset
);
394 bitset_stats_count (bitset src
)
396 return BITSET_COUNT_ (src
->s
.bset
);
401 bitset_stats_empty_p (bitset dst
)
403 return BITSET_EMPTY_P_ (dst
->s
.bset
);
408 bitset_stats_ones (bitset dst
)
410 BITSET_ONES_ (dst
->s
.bset
);
415 bitset_stats_zero (bitset dst
)
417 BITSET_ZERO_ (dst
->s
.bset
);
422 bitset_stats_copy (bitset dst
, bitset src
)
424 BITSET_CHECK2_ (dst
, src
);
425 BITSET_COPY_ (dst
->s
.bset
, src
->s
.bset
);
430 bitset_stats_disjoint_p (bitset dst
, bitset src
)
432 BITSET_CHECK2_ (dst
, src
);
433 return BITSET_DISJOINT_P_ (dst
->s
.bset
, src
->s
.bset
);
438 bitset_stats_equal_p (bitset dst
, bitset src
)
440 BITSET_CHECK2_ (dst
, src
);
441 return BITSET_EQUAL_P_ (dst
->s
.bset
, src
->s
.bset
);
446 bitset_stats_not (bitset dst
, bitset src
)
448 BITSET_CHECK2_ (dst
, src
);
449 BITSET_NOT_ (dst
->s
.bset
, src
->s
.bset
);
454 bitset_stats_subset_p (bitset dst
, bitset src
)
456 BITSET_CHECK2_ (dst
, src
);
457 return BITSET_SUBSET_P_ (dst
->s
.bset
, src
->s
.bset
);
462 bitset_stats_and (bitset dst
, bitset src1
, bitset src2
)
464 BITSET_CHECK3_ (dst
, src1
, src2
);
465 BITSET_AND_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
470 bitset_stats_and_cmp (bitset dst
, bitset src1
, bitset src2
)
472 BITSET_CHECK3_ (dst
, src1
, src2
);
473 return BITSET_AND_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
478 bitset_stats_andn (bitset dst
, bitset src1
, bitset src2
)
480 BITSET_CHECK3_ (dst
, src1
, src2
);
481 BITSET_ANDN_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
486 bitset_stats_andn_cmp (bitset dst
, bitset src1
, bitset src2
)
488 BITSET_CHECK3_ (dst
, src1
, src2
);
489 return BITSET_ANDN_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
494 bitset_stats_or (bitset dst
, bitset src1
, bitset src2
)
496 BITSET_CHECK3_ (dst
, src1
, src2
);
497 BITSET_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
502 bitset_stats_or_cmp (bitset dst
, bitset src1
, bitset src2
)
504 BITSET_CHECK3_ (dst
, src1
, src2
);
505 return BITSET_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
510 bitset_stats_xor (bitset dst
, bitset src1
, bitset src2
)
512 BITSET_CHECK3_ (dst
, src1
, src2
);
513 BITSET_XOR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
518 bitset_stats_xor_cmp (bitset dst
, bitset src1
, bitset src2
)
520 BITSET_CHECK3_ (dst
, src1
, src2
);
521 return BITSET_XOR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
);
526 bitset_stats_and_or (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
528 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
529 BITSET_AND_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
534 bitset_stats_and_or_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
536 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
537 return BITSET_AND_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
542 bitset_stats_andn_or (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
544 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
545 BITSET_ANDN_OR_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
550 bitset_stats_andn_or_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
552 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
553 return BITSET_ANDN_OR_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
558 bitset_stats_or_and (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
560 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
561 BITSET_OR_AND_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
566 bitset_stats_or_and_cmp (bitset dst
, bitset src1
, bitset src2
, bitset src3
)
568 BITSET_CHECK4_ (dst
, src1
, src2
, src3
);
569 return BITSET_OR_AND_CMP_ (dst
->s
.bset
, src1
->s
.bset
, src2
->s
.bset
, src3
->s
.bset
);
574 bitset_stats_list (bitset bset
, bitset_bindex
*list
,
575 bitset_bindex num
, bitset_bindex
*next
)
581 enum bitset_type type
;
583 count
= BITSET_LIST_ (bset
->s
.bset
, list
, num
, next
);
585 type
= BITSET_TYPE_ (bset
->s
.bset
);
586 BITSET_STATS_LISTS_INC (bset
->s
.bset
);
588 /* Log histogram of number of set bits. */
589 for (i
= 0, tmp
= count
; tmp
; tmp
>>= 1, i
++)
591 if (i
>= BITSET_LOG_COUNT_BINS
)
592 i
= BITSET_LOG_COUNT_BINS
- 1;
593 BITSET_STATS_LIST_COUNTS_INC (bset
->s
.bset
, i
);
595 /* Log histogram of number of bits in set. */
596 size
= BITSET_SIZE_ (bset
->s
.bset
);
597 for (i
= 0, tmp
= size
; tmp
; tmp
>>= 1, i
++)
599 if (i
>= BITSET_LOG_SIZE_BINS
)
600 i
= BITSET_LOG_SIZE_BINS
- 1;
601 BITSET_STATS_LIST_SIZES_INC (bset
->s
.bset
, i
);
603 /* Histogram of fraction of bits set. */
604 i
= size
? (count
* BITSET_DENSITY_BINS
) / size
: 0;
605 if (i
>= BITSET_DENSITY_BINS
)
606 i
= BITSET_DENSITY_BINS
- 1;
607 BITSET_STATS_LIST_DENSITY_INC (bset
->s
.bset
, i
);
613 bitset_stats_list_reverse (bitset bset
, bitset_bindex
*list
,
614 bitset_bindex num
, bitset_bindex
*next
)
616 return BITSET_LIST_REVERSE_ (bset
->s
.bset
, list
, num
, next
);
621 bitset_stats_free (bitset bset
)
623 BITSET_STATS_FREES_INC (bset
->s
.bset
);
624 BITSET_FREE_ (bset
->s
.bset
);
628 struct bitset_vtable bitset_stats_vtable
= {
636 bitset_stats_empty_p
,
640 bitset_stats_disjoint_p
,
641 bitset_stats_equal_p
,
643 bitset_stats_subset_p
,
645 bitset_stats_and_cmp
,
647 bitset_stats_andn_cmp
,
651 bitset_stats_xor_cmp
,
653 bitset_stats_and_or_cmp
,
654 bitset_stats_andn_or
,
655 bitset_stats_andn_or_cmp
,
657 bitset_stats_or_and_cmp
,
659 bitset_stats_list_reverse
,
665 /* Return enclosed bitset type. */
667 bitset_stats_type_get (bitset bset
)
669 return BITSET_TYPE_ (bset
->s
.bset
);
674 bitset_stats_bytes (void)
676 return sizeof (struct bitset_stats_struct
);
681 bitset_stats_init (bitset bset
, bitset_bindex n_bits
, enum bitset_type type
)
686 bset
->b
.vtable
= &bitset_stats_vtable
;
693 BITSET_NBITS_ (bset
) = n_bits
;
695 /* Set up the actual bitset implementation that
696 we are a wrapper over. */
700 bytes
= abitset_bytes (n_bits
);
701 sbset
= (bitset
) xcalloc (1, bytes
);
702 abitset_init (sbset
, n_bits
);
706 bytes
= lbitset_bytes (n_bits
);
707 sbset
= (bitset
) xcalloc (1, bytes
);
708 lbitset_init (sbset
, n_bits
);
712 bytes
= ebitset_bytes (n_bits
);
713 sbset
= (bitset
) xcalloc (1, bytes
);
714 ebitset_init (sbset
, n_bits
);
718 bytes
= vbitset_bytes (n_bits
);
719 sbset
= (bitset
) xcalloc (1, bytes
);
720 vbitset_init (sbset
, n_bits
);
727 bset
->s
.bset
= sbset
;
729 BITSET_STATS_ALLOCS_INC (type
);