1 /* Implement a cached obstack.
2 Written by Fred Fish <fnf@cygnus.com>
3 Rewritten by Jim Blandy <jimb@cygnus.com>
5 Copyright 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "gdb_obstack.h"
27 #include "gdb_string.h" /* For memcpy declaration */
28 #include "gdb_assert.h"
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. */
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
;
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. */
66 /* All the bstrings are allocated here. */
69 /* How many hash buckets we're using. */
70 unsigned int num_buckets
;
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
;
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 */
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
87 unsigned long expand_count
;
88 unsigned long expand_hash_count
;
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
;
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.
100 hash(const void *addr
, int length
)
102 const unsigned char *k
, *e
;
105 k
= (const unsigned char *)addr
;
115 /* Growing the bcache's hash table. */
117 /* If the average chain length grows beyond this, then we want to
118 resize our hash table. */
119 #define CHAIN_LENGTH_THRESHOLD (5)
122 expand_hash_table (struct bcache
*bcache
)
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
135 unsigned int new_num_buckets
;
136 struct bstring
**new_buckets
;
139 /* Count the stats. Every unique item needs to be re-hashed and
141 bcache
->expand_count
++;
142 bcache
->expand_hash_count
+= bcache
->unique_count
;
144 /* Find the next size. */
145 new_num_buckets
= bcache
->num_buckets
* 2;
146 for (i
= 0; i
< (sizeof (sizes
) / sizeof (sizes
[0])); i
++)
147 if (sizes
[i
] > bcache
->num_buckets
)
149 new_num_buckets
= sizes
[i
];
153 /* Allocate the new table. */
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
);
159 bcache
->structure_size
-= (bcache
->num_buckets
160 * sizeof (bcache
->bucket
[0]));
161 bcache
->structure_size
+= new_size
;
164 /* Rehash all existing strings. */
165 for (i
= 0; i
< bcache
->num_buckets
; i
++)
167 struct bstring
*s
, *next
;
169 for (s
= bcache
->bucket
[i
]; s
; s
= next
)
171 struct bstring
**new_bucket
;
174 new_bucket
= &new_buckets
[(hash (&s
->d
.data
, s
->length
)
176 s
->next
= *new_bucket
;
181 /* Plug in the new table. */
183 xfree (bcache
->bucket
);
184 bcache
->bucket
= new_buckets
;
185 bcache
->num_buckets
= new_num_buckets
;
189 /* Looking up things in the bcache. */
191 /* The number of bytes needed to allocate a struct bstring whose data
193 #define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n))
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. */
199 bcache_data (const void *addr
, int length
, struct bcache
*bcache
)
201 unsigned long full_hash
;
202 unsigned short half_hash
;
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
);
210 bcache
->total_count
++;
211 bcache
->total_size
+= length
;
213 full_hash
= hash (addr
, length
);
214 half_hash
= (full_hash
>> 16);
215 hash_index
= full_hash
% bcache
->num_buckets
;
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
220 for (s
= bcache
->bucket
[hash_index
]; s
; s
= s
->next
)
222 if (s
->half_hash
== half_hash
)
224 if (s
->length
== length
225 && ! memcmp (&s
->d
.data
, addr
, length
))
228 bcache
->half_hash_miss_count
++;
232 /* The user's string isn't in the list. Insert it after *ps. */
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
];
239 new->half_hash
= half_hash
;
240 bcache
->bucket
[hash_index
] = new;
242 bcache
->unique_count
++;
243 bcache
->unique_size
+= length
;
244 bcache
->structure_size
+= BSTRING_SIZE (length
);
251 deprecated_bcache (const void *addr
, int length
, struct bcache
*bcache
)
253 return bcache_data (addr
, length
, bcache
);
257 bcache (const void *addr
, int length
, struct bcache
*bcache
)
259 return bcache_data (addr
, length
, bcache
);
262 /* Allocating and freeing bcaches. */
265 bcache_xmalloc (void)
267 /* Allocate the bcache pre-zeroed. */
268 struct bcache
*b
= XCALLOC (1, struct bcache
);
269 /* We could use obstack_specify_allocation here instead, but
270 gdb_obstack.h specifies the allocation/deallocation
272 obstack_init (&b
->cache
);
276 /* Free all the storage associated with BCACHE. */
278 bcache_xfree (struct bcache
*bcache
)
282 obstack_free (&bcache
->cache
, 0);
283 xfree (bcache
->bucket
);
289 /* Printing statistics. */
292 compare_ints (const void *ap
, const void *bp
)
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
;
301 print_percentage (int portion
, int total
)
304 printf_filtered ("(not applicable)\n");
306 printf_filtered ("%3d%%\n", (int) (portion
* 100.0 / total
));
310 /* Print statistics on BCACHE's memory usage and efficacity at
311 eliminating duplication. NAME should describe the kind of data
312 BCACHE holds. Statistics are printed using `printf_filtered' and
315 print_bcache_statistics (struct bcache
*c
, char *type
)
317 int occupied_buckets
;
318 int max_chain_length
;
319 int median_chain_length
;
321 int median_entry_size
;
323 /* Count the number of occupied buckets, tally the various string
324 lengths, and measure chain lengths. */
327 int *chain_length
= XCALLOC (c
->num_buckets
+ 1, int);
328 int *entry_size
= XCALLOC (c
->unique_count
+ 1, int);
331 occupied_buckets
= 0;
333 for (b
= 0; b
< c
->num_buckets
; b
++)
335 struct bstring
*s
= c
->bucket
[b
];
345 gdb_assert (b
< c
->num_buckets
);
347 gdb_assert (stringi
< c
->unique_count
);
348 entry_size
[stringi
++] = s
->length
;
354 /* To compute the median, we need the set of chain lengths sorted. */
355 qsort (chain_length
, c
->num_buckets
, sizeof (chain_length
[0]),
357 qsort (entry_size
, c
->unique_count
, sizeof (entry_size
[0]),
360 if (c
->num_buckets
> 0)
362 max_chain_length
= chain_length
[c
->num_buckets
- 1];
363 median_chain_length
= chain_length
[c
->num_buckets
/ 2];
367 max_chain_length
= 0;
368 median_chain_length
= 0;
370 if (c
->unique_count
> 0)
372 max_entry_size
= entry_size
[c
->unique_count
- 1];
373 median_entry_size
= entry_size
[c
->unique_count
/ 2];
378 median_entry_size
= 0;
381 xfree (chain_length
);
385 printf_filtered (" Cached '%s' statistics:\n", type
);
386 printf_filtered (" Total object count: %ld\n", c
->total_count
);
387 printf_filtered (" Unique object count: %lu\n", c
->unique_count
);
388 printf_filtered (" Percentage of duplicates, by count: ");
389 print_percentage (c
->total_count
- c
->unique_count
, c
->total_count
);
390 printf_filtered ("\n");
392 printf_filtered (" Total object size: %ld\n", c
->total_size
);
393 printf_filtered (" Unique object size: %ld\n", c
->unique_size
);
394 printf_filtered (" Percentage of duplicates, by size: ");
395 print_percentage (c
->total_size
- c
->unique_size
, c
->total_size
);
396 printf_filtered ("\n");
398 printf_filtered (" Max entry size: %d\n", max_entry_size
);
399 printf_filtered (" Average entry size: ");
400 if (c
->unique_count
> 0)
401 printf_filtered ("%ld\n", c
->unique_size
/ c
->unique_count
);
403 printf_filtered ("(not applicable)\n");
404 printf_filtered (" Median entry size: %d\n", median_entry_size
);
405 printf_filtered ("\n");
407 printf_filtered (" Total memory used by bcache, including overhead: %ld\n",
409 printf_filtered (" Percentage memory overhead: ");
410 print_percentage (c
->structure_size
- c
->unique_size
, c
->unique_size
);
411 printf_filtered (" Net memory savings: ");
412 print_percentage (c
->total_size
- c
->structure_size
, c
->total_size
);
413 printf_filtered ("\n");
415 printf_filtered (" Hash table size: %3d\n", c
->num_buckets
);
416 printf_filtered (" Hash table expands: %lu\n",
418 printf_filtered (" Hash table hashes: %lu\n",
419 c
->total_count
+ c
->expand_hash_count
);
420 printf_filtered (" Half hash misses: %lu\n",
421 c
->half_hash_miss_count
);
422 printf_filtered (" Hash table population: ");
423 print_percentage (occupied_buckets
, c
->num_buckets
);
424 printf_filtered (" Median hash chain length: %3d\n",
425 median_chain_length
);
426 printf_filtered (" Average hash chain length: ");
427 if (c
->num_buckets
> 0)
428 printf_filtered ("%3lu\n", c
->unique_count
/ c
->num_buckets
);
430 printf_filtered ("(not applicable)\n");
431 printf_filtered (" Maximum hash chain length: %3d\n", max_chain_length
);
432 printf_filtered ("\n");
436 bcache_memory_used (struct bcache
*bcache
)
438 return obstack_memory_used (&bcache
->cache
);
This page took 0.037394 seconds and 4 git commands to generate.