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