]> git.saurik.com Git - bison.git/blame_incremental - lib/bitset_stats.c
Log recent changes.
[bison.git] / lib / bitset_stats.c
... / ...
CommitLineData
1/* Bitset statistics.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
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
25 routines.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "bbitset.h"
33#include "abitset.h"
34#include "ebitset.h"
35#include "lbitset.h"
36#include "vbitset.h"
37#include "bitset_stats.h"
38#include <stdlib.h>
39#include <string.h>
40#include <stdio.h>
41
42#include "gettext.h"
43#define _(Msgid) gettext (Msgid)
44
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
50
51
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)]++
77
78
79struct bitset_type_info_struct
80{
81 unsigned int allocs;
82 unsigned int frees;
83 unsigned int lists;
84 unsigned int sets;
85 unsigned int cache_sets;
86 unsigned int resets;
87 unsigned int cache_resets;
88 unsigned int tests;
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];
93};
94
95struct bitset_stats_info_struct
96{
97 unsigned int runs;
98 struct bitset_type_info_struct types[BITSET_TYPE_NUM];
99};
100
101
102struct bitset_stats_info_struct bitset_stats_info_data;
103struct bitset_stats_info_struct *bitset_stats_info;
104bool bitset_stats_enabled = false;
105
106
107/* Print a percentage histogram with message MSG to FILE. */
108static void
109bitset_percent_histogram_print (FILE *file, const char *name, const char *msg,
110 unsigned int n_bins, unsigned int *bins)
111{
112 unsigned int i;
113 unsigned int total;
114
115 total = 0;
116 for (i = 0; i < n_bins; i++)
117 total += bins[i];
118
119 if (!total)
120 return;
121
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",
125 i * 100.0 / n_bins,
126 (i + 1) * 100.0 / n_bins, bins[i],
127 (100.0 * bins[i]) / total);
128}
129
130
131/* Print a log histogram with message MSG to FILE. */
132static void
133bitset_log_histogram_print (FILE *file, const char *name, const char *msg,
134 unsigned int n_bins, unsigned int *bins)
135{
136 unsigned int i;
137 unsigned int total;
138 unsigned int max_width;
139
140 total = 0;
141 for (i = 0; i < n_bins; i++)
142 total += bins[i];
143
144 if (!total)
145 return;
146
147 /* Determine number of useful bins. */
148 for (i = n_bins; i > 3 && ! bins[i - 1]; i--)
149 continue;
150 n_bins = i;
151
152 /* 2 * ceil (log10 (2) * (N - 1)) + 1. */
153 max_width = 2 * (unsigned int) (0.30103 * (n_bins - 1) + 0.9999) + 1;
154
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);
159
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),
163 1UL << (i - 1),
164 (1UL << i) - 1,
165 bins[i],
166 (100.0 * bins[i]) / total);
167}
168
169
170/* Print bitset statistics to FILE. */
171static void
172bitset_stats_print_1 (FILE *file, const char *name,
173 struct bitset_type_info_struct *stats)
174{
175 if (!stats)
176 return;
177
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);
191
192 fprintf (file, _("%u bitset_lists\n"), stats->lists);
193
194 bitset_log_histogram_print (file, name, _("count log histogram\n"),
195 BITSET_LOG_COUNT_BINS, stats->list_counts);
196
197 bitset_log_histogram_print (file, name, _("size log histogram\n"),
198 BITSET_LOG_SIZE_BINS, stats->list_sizes);
199
200 bitset_percent_histogram_print (file, name, _("density histogram\n"),
201 BITSET_DENSITY_BINS, stats->list_density);
202}
203
204
205/* Print all bitset statistics to FILE. */
206static void
207bitset_stats_print (FILE *file, bool verbose ATTRIBUTE_UNUSED)
208{
209 int i;
210
211 if (!bitset_stats_info)
212 return;
213
214 fprintf (file, _("Bitset statistics:\n\n"));
215
216 if (bitset_stats_info->runs > 1)
217 fprintf (file, _("Accumulated runs = %u\n"), bitset_stats_info->runs);
218
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]);
222}
223
224
225/* Initialise bitset statistics logging. */
226void
227bitset_stats_enable (void)
228{
229 if (!bitset_stats_info)
230 bitset_stats_info = &bitset_stats_info_data;
231 bitset_stats_enabled = true;
232}
233
234
235void
236bitset_stats_disable (void)
237{
238 bitset_stats_enabled = false;
239}
240
241
242/* Read bitset statistics file. */
243void
244bitset_stats_read (const char *filename)
245{
246 FILE *file;
247
248 if (!bitset_stats_info)
249 return;
250
251 if (!filename)
252 filename = BITSET_STATS_FILE;
253
254 file = fopen (filename, "r");
255 if (file)
256 {
257 if (fread (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
258 1, file) != 1)
259 {
260 if (ferror (file))
261 perror (_("Could not read stats file."));
262 else
263 fprintf (stderr, _("Bad stats file size.\n"));
264 }
265 if (fclose (file) != 0)
266 perror (_("Could not read stats file."));
267 }
268 bitset_stats_info_data.runs++;
269}
270
271
272/* Write bitset statistics file. */
273void
274bitset_stats_write (const char *filename)
275{
276 FILE *file;
277
278 if (!bitset_stats_info)
279 return;
280
281 if (!filename)
282 filename = BITSET_STATS_FILE;
283
284 file = fopen (filename, "w");
285 if (file)
286 {
287 if (fwrite (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
288 1, file) != 1)
289 perror (_("Could not write stats file."));
290 if (fclose (file) != 0)
291 perror (_("Could not write stats file."));
292 }
293 else
294 perror (_("Could not open stats file for writing."));
295}
296
297
298/* Dump bitset statistics to FILE. */
299void
300bitset_stats_dump (FILE *file)
301{
302 bitset_stats_print (file, false);
303}
304
305
306/* Function to be called from debugger to print bitset stats. */
307void
308debug_bitset_stats (void)
309{
310 bitset_stats_print (stderr, true);
311}
312
313
314static void
315bitset_stats_set (bitset dst, bitset_bindex bitno)
316{
317 bitset bset = dst->s.bset;
318 bitset_windex wordno = bitno / BITSET_WORD_BITS;
319 bitset_windex offset = wordno - bset->b.cindex;
320
321 BITSET_STATS_SETS_INC (bset);
322
323 if (offset < bset->b.csize)
324 {
325 bset->b.cdata[offset] |= (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
326 BITSET_STATS_CACHE_SETS_INC (bset);
327 }
328 else
329 BITSET_SET_ (bset, bitno);
330}
331
332
333static void
334bitset_stats_reset (bitset dst, bitset_bindex bitno)
335{
336 bitset bset = dst->s.bset;
337 bitset_windex wordno = bitno / BITSET_WORD_BITS;
338 bitset_windex offset = wordno - bset->b.cindex;
339
340 BITSET_STATS_RESETS_INC (bset);
341
342 if (offset < bset->b.csize)
343 {
344 bset->b.cdata[offset] &=
345 ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
346 BITSET_STATS_CACHE_RESETS_INC (bset);
347 }
348 else
349 BITSET_RESET_ (bset, bitno);
350}
351
352
353static bool
354bitset_stats_toggle (bitset src, bitset_bindex bitno)
355{
356 return BITSET_TOGGLE_ (src->s.bset, bitno);
357}
358
359
360static bool
361bitset_stats_test (bitset src, bitset_bindex bitno)
362{
363 bitset bset = src->s.bset;
364 bitset_windex wordno = bitno / BITSET_WORD_BITS;
365 bitset_windex offset = wordno - bset->b.cindex;
366
367 BITSET_STATS_TESTS_INC (bset);
368
369 if (offset < bset->b.csize)
370 {
371 BITSET_STATS_CACHE_TESTS_INC (bset);
372 return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
373 }
374 else
375 return BITSET_TEST_ (bset, bitno);
376}
377
378
379static bitset_bindex
380bitset_stats_resize (bitset src, bitset_bindex size)
381{
382 return BITSET_RESIZE_ (src->s.bset, size);
383}
384
385
386static bitset_bindex
387bitset_stats_size (bitset src)
388{
389 return BITSET_SIZE_ (src->s.bset);
390}
391
392
393static bitset_bindex
394bitset_stats_count (bitset src)
395{
396 return BITSET_COUNT_ (src->s.bset);
397}
398
399
400static bool
401bitset_stats_empty_p (bitset dst)
402{
403 return BITSET_EMPTY_P_ (dst->s.bset);
404}
405
406
407static void
408bitset_stats_ones (bitset dst)
409{
410 BITSET_ONES_ (dst->s.bset);
411}
412
413
414static void
415bitset_stats_zero (bitset dst)
416{
417 BITSET_ZERO_ (dst->s.bset);
418}
419
420
421static void
422bitset_stats_copy (bitset dst, bitset src)
423{
424 BITSET_CHECK2_ (dst, src);
425 BITSET_COPY_ (dst->s.bset, src->s.bset);
426}
427
428
429static bool
430bitset_stats_disjoint_p (bitset dst, bitset src)
431{
432 BITSET_CHECK2_ (dst, src);
433 return BITSET_DISJOINT_P_ (dst->s.bset, src->s.bset);
434}
435
436
437static bool
438bitset_stats_equal_p (bitset dst, bitset src)
439{
440 BITSET_CHECK2_ (dst, src);
441 return BITSET_EQUAL_P_ (dst->s.bset, src->s.bset);
442}
443
444
445static void
446bitset_stats_not (bitset dst, bitset src)
447{
448 BITSET_CHECK2_ (dst, src);
449 BITSET_NOT_ (dst->s.bset, src->s.bset);
450}
451
452
453static bool
454bitset_stats_subset_p (bitset dst, bitset src)
455{
456 BITSET_CHECK2_ (dst, src);
457 return BITSET_SUBSET_P_ (dst->s.bset, src->s.bset);
458}
459
460
461static void
462bitset_stats_and (bitset dst, bitset src1, bitset src2)
463{
464 BITSET_CHECK3_ (dst, src1, src2);
465 BITSET_AND_ (dst->s.bset, src1->s.bset, src2->s.bset);
466}
467
468
469static bool
470bitset_stats_and_cmp (bitset dst, bitset src1, bitset src2)
471{
472 BITSET_CHECK3_ (dst, src1, src2);
473 return BITSET_AND_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
474}
475
476
477static void
478bitset_stats_andn (bitset dst, bitset src1, bitset src2)
479{
480 BITSET_CHECK3_ (dst, src1, src2);
481 BITSET_ANDN_ (dst->s.bset, src1->s.bset, src2->s.bset);
482}
483
484
485static bool
486bitset_stats_andn_cmp (bitset dst, bitset src1, bitset src2)
487{
488 BITSET_CHECK3_ (dst, src1, src2);
489 return BITSET_ANDN_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
490}
491
492
493static void
494bitset_stats_or (bitset dst, bitset src1, bitset src2)
495{
496 BITSET_CHECK3_ (dst, src1, src2);
497 BITSET_OR_ (dst->s.bset, src1->s.bset, src2->s.bset);
498}
499
500
501static bool
502bitset_stats_or_cmp (bitset dst, bitset src1, bitset src2)
503{
504 BITSET_CHECK3_ (dst, src1, src2);
505 return BITSET_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
506}
507
508
509static void
510bitset_stats_xor (bitset dst, bitset src1, bitset src2)
511{
512 BITSET_CHECK3_ (dst, src1, src2);
513 BITSET_XOR_ (dst->s.bset, src1->s.bset, src2->s.bset);
514}
515
516
517static bool
518bitset_stats_xor_cmp (bitset dst, bitset src1, bitset src2)
519{
520 BITSET_CHECK3_ (dst, src1, src2);
521 return BITSET_XOR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
522}
523
524
525static void
526bitset_stats_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
527{
528 BITSET_CHECK4_ (dst, src1, src2, src3);
529 BITSET_AND_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
530}
531
532
533static bool
534bitset_stats_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
535{
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);
538}
539
540
541static void
542bitset_stats_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
543{
544 BITSET_CHECK4_ (dst, src1, src2, src3);
545 BITSET_ANDN_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
546}
547
548
549static bool
550bitset_stats_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
551{
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);
554}
555
556
557static void
558bitset_stats_or_and (bitset dst, bitset src1, bitset src2, bitset src3)
559{
560 BITSET_CHECK4_ (dst, src1, src2, src3);
561 BITSET_OR_AND_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
562}
563
564
565static bool
566bitset_stats_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
567{
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);
570}
571
572
573static bitset_bindex
574bitset_stats_list (bitset bset, bitset_bindex *list,
575 bitset_bindex num, bitset_bindex *next)
576{
577 bitset_bindex count;
578 bitset_bindex tmp;
579 bitset_bindex size;
580 bitset_bindex i;
581 enum bitset_type type;
582
583 count = BITSET_LIST_ (bset->s.bset, list, num, next);
584
585 type = BITSET_TYPE_ (bset->s.bset);
586 BITSET_STATS_LISTS_INC (bset->s.bset);
587
588 /* Log histogram of number of set bits. */
589 for (i = 0, tmp = count; tmp; tmp >>= 1, i++)
590 continue;
591 if (i >= BITSET_LOG_COUNT_BINS)
592 i = BITSET_LOG_COUNT_BINS - 1;
593 BITSET_STATS_LIST_COUNTS_INC (bset->s.bset, i);
594
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++)
598 continue;
599 if (i >= BITSET_LOG_SIZE_BINS)
600 i = BITSET_LOG_SIZE_BINS - 1;
601 BITSET_STATS_LIST_SIZES_INC (bset->s.bset, i);
602
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);
608 return count;
609}
610
611
612static bitset_bindex
613bitset_stats_list_reverse (bitset bset, bitset_bindex *list,
614 bitset_bindex num, bitset_bindex *next)
615{
616 return BITSET_LIST_REVERSE_ (bset->s.bset, list, num, next);
617}
618
619
620static void
621bitset_stats_free (bitset bset)
622{
623 BITSET_STATS_FREES_INC (bset->s.bset);
624 BITSET_FREE_ (bset->s.bset);
625}
626
627
628struct bitset_vtable bitset_stats_vtable = {
629 bitset_stats_set,
630 bitset_stats_reset,
631 bitset_stats_toggle,
632 bitset_stats_test,
633 bitset_stats_resize,
634 bitset_stats_size,
635 bitset_stats_count,
636 bitset_stats_empty_p,
637 bitset_stats_ones,
638 bitset_stats_zero,
639 bitset_stats_copy,
640 bitset_stats_disjoint_p,
641 bitset_stats_equal_p,
642 bitset_stats_not,
643 bitset_stats_subset_p,
644 bitset_stats_and,
645 bitset_stats_and_cmp,
646 bitset_stats_andn,
647 bitset_stats_andn_cmp,
648 bitset_stats_or,
649 bitset_stats_or_cmp,
650 bitset_stats_xor,
651 bitset_stats_xor_cmp,
652 bitset_stats_and_or,
653 bitset_stats_and_or_cmp,
654 bitset_stats_andn_or,
655 bitset_stats_andn_or_cmp,
656 bitset_stats_or_and,
657 bitset_stats_or_and_cmp,
658 bitset_stats_list,
659 bitset_stats_list_reverse,
660 bitset_stats_free,
661 BITSET_STATS
662};
663
664
665/* Return enclosed bitset type. */
666enum bitset_type
667bitset_stats_type_get (bitset bset)
668{
669 return BITSET_TYPE_ (bset->s.bset);
670}
671
672
673size_t
674bitset_stats_bytes (void)
675{
676 return sizeof (struct bitset_stats_struct);
677}
678
679
680bitset
681bitset_stats_init (bitset bset, bitset_bindex n_bits, enum bitset_type type)
682{
683 size_t bytes;
684 bitset sbset;
685
686 bset->b.vtable = &bitset_stats_vtable;
687
688 /* Disable cache. */
689 bset->b.cindex = 0;
690 bset->b.csize = 0;
691 bset->b.cdata = 0;
692
693 BITSET_NBITS_ (bset) = n_bits;
694
695 /* Set up the actual bitset implementation that
696 we are a wrapper over. */
697 switch (type)
698 {
699 case BITSET_ARRAY:
700 bytes = abitset_bytes (n_bits);
701 sbset = xcalloc (1, bytes);
702 abitset_init (sbset, n_bits);
703 break;
704
705 case BITSET_LIST:
706 bytes = lbitset_bytes (n_bits);
707 sbset = xcalloc (1, bytes);
708 lbitset_init (sbset, n_bits);
709 break;
710
711 case BITSET_TABLE:
712 bytes = ebitset_bytes (n_bits);
713 sbset = xcalloc (1, bytes);
714 ebitset_init (sbset, n_bits);
715 break;
716
717 case BITSET_VARRAY:
718 bytes = vbitset_bytes (n_bits);
719 sbset = xcalloc (1, bytes);
720 vbitset_init (sbset, n_bits);
721 break;
722
723 default:
724 abort ();
725 }
726
727 bset->s.bset = sbset;
728
729 BITSET_STATS_ALLOCS_INC (type);
730
731 return bset;
732}