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