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