]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/bcache.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / bcache.c
CommitLineData
c906108c 1/* Implement a cached obstack.
c2d11a7d
JM
2 Written by Fred Fish <fnf@cygnus.com>
3 Rewritten by Jim Blandy <jimb@cygnus.com>
2c7ef074 4
6aba47ca 5 Copyright (C) 1999, 2000, 2002, 2003, 2007 Free Software Foundation, Inc.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
197e01b6
EZ
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
c906108c
SS
23
24#include "defs.h"
04ea0df1 25#include "gdb_obstack.h"
c906108c
SS
26#include "bcache.h"
27#include "gdb_string.h" /* For memcpy declaration */
2160782c 28#include "gdb_assert.h"
c906108c 29
2c7ef074
AC
30#include <stddef.h>
31#include <stdlib.h>
32
af5f3db6
AC
33/* The type used to hold a single bcache string. The user data is
34 stored in d.data. Since it can be any type, it needs to have the
35 same alignment as the most strict alignment of any type on the host
36 machine. I don't know of any really correct way to do this in
37 stock ANSI C, so just do it the same way obstack.h does. */
38
39struct bstring
40{
49df298f 41 /* Hash chain. */
af5f3db6 42 struct bstring *next;
49df298f
AC
43 /* Assume the data length is no more than 64k. */
44 unsigned short length;
45 /* The half hash hack. This contains the upper 16 bits of the hash
46 value and is used as a pre-check when comparing two strings and
47 avoids the need to do length or memcmp calls. It proves to be
48 roughly 100% effective. */
49 unsigned short half_hash;
af5f3db6
AC
50
51 union
52 {
53 char data[1];
54 double dummy;
55 }
56 d;
57};
58
59
60/* The structure for a bcache itself. The bcache is initialized, in
61 bcache_xmalloc(), by filling it with zeros and then setting the
62 corresponding obstack's malloc() and free() methods. */
63
64struct bcache
65{
66 /* All the bstrings are allocated here. */
67 struct obstack cache;
68
69 /* How many hash buckets we're using. */
70 unsigned int num_buckets;
71
72 /* Hash buckets. This table is allocated using malloc, so when we
73 grow the table we can return the old table to the system. */
74 struct bstring **bucket;
75
76 /* Statistics. */
77 unsigned long unique_count; /* number of unique strings */
78 long total_count; /* total number of strings cached, including dups */
79 long unique_size; /* size of unique strings, in bytes */
80 long total_size; /* total number of bytes cached, including dups */
81 long structure_size; /* total size of bcache, including infrastructure */
2160782c
AC
82 /* Number of times that the hash table is expanded and hence
83 re-built, and the corresponding number of times that a string is
84 [re]hashed as part of entering it into the expanded table. The
85 total number of hashes can be computed by adding TOTAL_COUNT to
86 expand_hash_count. */
87 unsigned long expand_count;
88 unsigned long expand_hash_count;
49df298f
AC
89 /* Number of times that the half-hash compare hit (compare the upper
90 16 bits of hash values) hit, but the corresponding combined
91 length/data compare missed. */
92 unsigned long half_hash_miss_count;
af5f3db6
AC
93};
94
357e46e7
DB
95/* The old hash function was stolen from SDBM. This is what DB 3.0 uses now,
96 * and is better than the old one.
97 */
c2d11a7d 98\f
c2d11a7d 99unsigned long
d85a5daf 100hash(const void *addr, int length)
c2d11a7d 101{
357e46e7
DB
102 const unsigned char *k, *e;
103 unsigned long h;
104
105 k = (const unsigned char *)addr;
106 e = k+length;
107 for (h=0; k< e;++k)
108 {
109 h *=16777619;
110 h ^= *k;
111 }
112 return (h);
c906108c 113}
c2d11a7d
JM
114\f
115/* Growing the bcache's hash table. */
116
117/* If the average chain length grows beyond this, then we want to
118 resize our hash table. */
119#define CHAIN_LENGTH_THRESHOLD (5)
120
121static void
122expand_hash_table (struct bcache *bcache)
c906108c 123{
c2d11a7d
JM
124 /* A table of good hash table sizes. Whenever we grow, we pick the
125 next larger size from this table. sizes[i] is close to 1 << (i+10),
126 so we roughly double the table size each time. After we fall off
127 the end of this table, we just double. Don't laugh --- there have
128 been executables sighted with a gigabyte of debug info. */
129 static unsigned long sizes[] = {
130 1021, 2053, 4099, 8191, 16381, 32771,
131 65537, 131071, 262144, 524287, 1048573, 2097143,
132 4194301, 8388617, 16777213, 33554467, 67108859, 134217757,
133 268435459, 536870923, 1073741827, 2147483659UL
134 };
745b8ca0 135 unsigned int new_num_buckets;
c2d11a7d 136 struct bstring **new_buckets;
745b8ca0 137 unsigned int i;
c2d11a7d 138
2160782c
AC
139 /* Count the stats. Every unique item needs to be re-hashed and
140 re-entered. */
141 bcache->expand_count++;
142 bcache->expand_hash_count += bcache->unique_count;
143
c2d11a7d 144 /* Find the next size. */
745b8ca0 145 new_num_buckets = bcache->num_buckets * 2;
c2d11a7d
JM
146 for (i = 0; i < (sizeof (sizes) / sizeof (sizes[0])); i++)
147 if (sizes[i] > bcache->num_buckets)
148 {
149 new_num_buckets = sizes[i];
150 break;
151 }
c2d11a7d
JM
152
153 /* Allocate the new table. */
154 {
155 size_t new_size = new_num_buckets * sizeof (new_buckets[0]);
156 new_buckets = (struct bstring **) xmalloc (new_size);
157 memset (new_buckets, 0, new_size);
158
159 bcache->structure_size -= (bcache->num_buckets
160 * sizeof (bcache->bucket[0]));
161 bcache->structure_size += new_size;
162 }
c906108c 163
c2d11a7d
JM
164 /* Rehash all existing strings. */
165 for (i = 0; i < bcache->num_buckets; i++)
c906108c 166 {
c2d11a7d
JM
167 struct bstring *s, *next;
168
169 for (s = bcache->bucket[i]; s; s = next)
c906108c 170 {
c2d11a7d
JM
171 struct bstring **new_bucket;
172 next = s->next;
173
174 new_bucket = &new_buckets[(hash (&s->d.data, s->length)
175 % new_num_buckets)];
176 s->next = *new_bucket;
177 *new_bucket = s;
c906108c
SS
178 }
179 }
c2d11a7d
JM
180
181 /* Plug in the new table. */
182 if (bcache->bucket)
b8c9b27d 183 xfree (bcache->bucket);
c2d11a7d
JM
184 bcache->bucket = new_buckets;
185 bcache->num_buckets = new_num_buckets;
c906108c
SS
186}
187
c2d11a7d
JM
188\f
189/* Looking up things in the bcache. */
190
191/* The number of bytes needed to allocate a struct bstring whose data
192 is N bytes long. */
193#define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n))
194
195/* Find a copy of the LENGTH bytes at ADDR in BCACHE. If BCACHE has
196 never seen those bytes before, add a copy of them to BCACHE. In
197 either case, return a pointer to BCACHE's copy of that string. */
3a16a68c
AC
198static void *
199bcache_data (const void *addr, int length, struct bcache *bcache)
c906108c 200{
49df298f
AC
201 unsigned long full_hash;
202 unsigned short half_hash;
c2d11a7d
JM
203 int hash_index;
204 struct bstring *s;
c906108c 205
c2d11a7d
JM
206 /* If our average chain length is too high, expand the hash table. */
207 if (bcache->unique_count >= bcache->num_buckets * CHAIN_LENGTH_THRESHOLD)
208 expand_hash_table (bcache);
209
210 bcache->total_count++;
211 bcache->total_size += length;
212
49df298f
AC
213 full_hash = hash (addr, length);
214 half_hash = (full_hash >> 16);
215 hash_index = full_hash % bcache->num_buckets;
c2d11a7d 216
49df298f
AC
217 /* Search the hash bucket for a string identical to the caller's.
218 As a short-circuit first compare the upper part of each hash
219 values. */
c2d11a7d 220 for (s = bcache->bucket[hash_index]; s; s = s->next)
49df298f
AC
221 {
222 if (s->half_hash == half_hash)
223 {
224 if (s->length == length
225 && ! memcmp (&s->d.data, addr, length))
226 return &s->d.data;
227 else
228 bcache->half_hash_miss_count++;
229 }
230 }
c2d11a7d
JM
231
232 /* The user's string isn't in the list. Insert it after *ps. */
233 {
234 struct bstring *new
235 = obstack_alloc (&bcache->cache, BSTRING_SIZE (length));
236 memcpy (&new->d.data, addr, length);
237 new->length = length;
238 new->next = bcache->bucket[hash_index];
49df298f 239 new->half_hash = half_hash;
c2d11a7d
JM
240 bcache->bucket[hash_index] = new;
241
242 bcache->unique_count++;
243 bcache->unique_size += length;
244 bcache->structure_size += BSTRING_SIZE (length);
245
246 return &new->d.data;
247 }
c906108c
SS
248}
249
3a16a68c
AC
250void *
251deprecated_bcache (const void *addr, int length, struct bcache *bcache)
252{
253 return bcache_data (addr, length, bcache);
254}
255
256const void *
257bcache (const void *addr, int length, struct bcache *bcache)
258{
259 return bcache_data (addr, length, bcache);
260}
c2d11a7d 261\f
af5f3db6
AC
262/* Allocating and freeing bcaches. */
263
264struct bcache *
265bcache_xmalloc (void)
266{
267 /* Allocate the bcache pre-zeroed. */
268 struct bcache *b = XCALLOC (1, struct bcache);
1ab21617
EZ
269 /* We could use obstack_specify_allocation here instead, but
270 gdb_obstack.h specifies the allocation/deallocation
271 functions. */
272 obstack_init (&b->cache);
af5f3db6
AC
273 return b;
274}
c2d11a7d
JM
275
276/* Free all the storage associated with BCACHE. */
c906108c 277void
af5f3db6 278bcache_xfree (struct bcache *bcache)
c906108c 279{
af5f3db6
AC
280 if (bcache == NULL)
281 return;
c2d11a7d 282 obstack_free (&bcache->cache, 0);
af5f3db6
AC
283 xfree (bcache->bucket);
284 xfree (bcache);
c2d11a7d
JM
285}
286
287
288\f
289/* Printing statistics. */
290
291static int
292compare_ints (const void *ap, const void *bp)
293{
294 /* Because we know we're comparing two ints which are positive,
295 there's no danger of overflow here. */
296 return * (int *) ap - * (int *) bp;
297}
298
299
300static void
301print_percentage (int portion, int total)
302{
303 if (total == 0)
3d263c1d
BI
304 /* i18n: Like "Percentage of duplicates, by count: (not applicable)" */
305 printf_filtered (_("(not applicable)\n"));
c906108c 306 else
b2ba182e 307 printf_filtered ("%3d%%\n", (int) (portion * 100.0 / total));
c2d11a7d
JM
308}
309
310
311/* Print statistics on BCACHE's memory usage and efficacity at
312 eliminating duplication. NAME should describe the kind of data
313 BCACHE holds. Statistics are printed using `printf_filtered' and
314 its ilk. */
315void
316print_bcache_statistics (struct bcache *c, char *type)
317{
318 int occupied_buckets;
319 int max_chain_length;
320 int median_chain_length;
2160782c
AC
321 int max_entry_size;
322 int median_entry_size;
c2d11a7d 323
2160782c
AC
324 /* Count the number of occupied buckets, tally the various string
325 lengths, and measure chain lengths. */
c2d11a7d 326 {
745b8ca0 327 unsigned int b;
2160782c
AC
328 int *chain_length = XCALLOC (c->num_buckets + 1, int);
329 int *entry_size = XCALLOC (c->unique_count + 1, int);
330 int stringi = 0;
c2d11a7d
JM
331
332 occupied_buckets = 0;
333
334 for (b = 0; b < c->num_buckets; b++)
335 {
336 struct bstring *s = c->bucket[b];
337
338 chain_length[b] = 0;
339
340 if (s)
341 {
342 occupied_buckets++;
343
344 while (s)
345 {
2160782c 346 gdb_assert (b < c->num_buckets);
c2d11a7d 347 chain_length[b]++;
2160782c
AC
348 gdb_assert (stringi < c->unique_count);
349 entry_size[stringi++] = s->length;
c2d11a7d
JM
350 s = s->next;
351 }
352 }
353 }
354
355 /* To compute the median, we need the set of chain lengths sorted. */
356 qsort (chain_length, c->num_buckets, sizeof (chain_length[0]),
357 compare_ints);
2160782c
AC
358 qsort (entry_size, c->unique_count, sizeof (entry_size[0]),
359 compare_ints);
c2d11a7d
JM
360
361 if (c->num_buckets > 0)
362 {
363 max_chain_length = chain_length[c->num_buckets - 1];
364 median_chain_length = chain_length[c->num_buckets / 2];
365 }
366 else
367 {
368 max_chain_length = 0;
369 median_chain_length = 0;
370 }
2160782c
AC
371 if (c->unique_count > 0)
372 {
373 max_entry_size = entry_size[c->unique_count - 1];
374 median_entry_size = entry_size[c->unique_count / 2];
375 }
376 else
377 {
378 max_entry_size = 0;
379 median_entry_size = 0;
380 }
381
382 xfree (chain_length);
383 xfree (entry_size);
c2d11a7d
JM
384 }
385
3d263c1d
BI
386 printf_filtered (_(" Cached '%s' statistics:\n"), type);
387 printf_filtered (_(" Total object count: %ld\n"), c->total_count);
388 printf_filtered (_(" Unique object count: %lu\n"), c->unique_count);
389 printf_filtered (_(" Percentage of duplicates, by count: "));
c2d11a7d
JM
390 print_percentage (c->total_count - c->unique_count, c->total_count);
391 printf_filtered ("\n");
392
3d263c1d
BI
393 printf_filtered (_(" Total object size: %ld\n"), c->total_size);
394 printf_filtered (_(" Unique object size: %ld\n"), c->unique_size);
395 printf_filtered (_(" Percentage of duplicates, by size: "));
c2d11a7d
JM
396 print_percentage (c->total_size - c->unique_size, c->total_size);
397 printf_filtered ("\n");
398
3d263c1d
BI
399 printf_filtered (_(" Max entry size: %d\n"), max_entry_size);
400 printf_filtered (_(" Average entry size: "));
2160782c
AC
401 if (c->unique_count > 0)
402 printf_filtered ("%ld\n", c->unique_size / c->unique_count);
403 else
3d263c1d
BI
404 /* i18n: "Average entry size: (not applicable)" */
405 printf_filtered (_("(not applicable)\n"));
406 printf_filtered (_(" Median entry size: %d\n"), median_entry_size);
2160782c
AC
407 printf_filtered ("\n");
408
3d263c1d 409 printf_filtered (_(" Total memory used by bcache, including overhead: %ld\n"),
c2d11a7d 410 c->structure_size);
3d263c1d 411 printf_filtered (_(" Percentage memory overhead: "));
c2d11a7d 412 print_percentage (c->structure_size - c->unique_size, c->unique_size);
3d263c1d 413 printf_filtered (_(" Net memory savings: "));
c2d11a7d
JM
414 print_percentage (c->total_size - c->structure_size, c->total_size);
415 printf_filtered ("\n");
416
3d263c1d
BI
417 printf_filtered (_(" Hash table size: %3d\n"), c->num_buckets);
418 printf_filtered (_(" Hash table expands: %lu\n"),
2160782c 419 c->expand_count);
3d263c1d 420 printf_filtered (_(" Hash table hashes: %lu\n"),
2160782c 421 c->total_count + c->expand_hash_count);
3d263c1d 422 printf_filtered (_(" Half hash misses: %lu\n"),
49df298f 423 c->half_hash_miss_count);
3d263c1d 424 printf_filtered (_(" Hash table population: "));
c2d11a7d 425 print_percentage (occupied_buckets, c->num_buckets);
3d263c1d 426 printf_filtered (_(" Median hash chain length: %3d\n"),
c2d11a7d 427 median_chain_length);
3d263c1d 428 printf_filtered (_(" Average hash chain length: "));
c2d11a7d 429 if (c->num_buckets > 0)
745b8ca0 430 printf_filtered ("%3lu\n", c->unique_count / c->num_buckets);
c906108c 431 else
3d263c1d
BI
432 /* i18n: "Average hash chain length: (not applicable)" */
433 printf_filtered (_("(not applicable)\n"));
434 printf_filtered (_(" Maximum hash chain length: %3d\n"), max_chain_length);
c2d11a7d 435 printf_filtered ("\n");
c906108c 436}
af5f3db6
AC
437
438int
439bcache_memory_used (struct bcache *bcache)
440{
441 return obstack_memory_used (&bcache->cache);
442}