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. */
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. */
59 /* All the bstrings are allocated here. */
62 /* How many hash buckets we're using. */
63 unsigned int num_buckets
;
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
;
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 */
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
80 unsigned long expand_count
;
81 unsigned long expand_hash_count
;
84 /* The old hash function was stolen from SDBM. This is what DB 3.0 uses now,
85 * and is better than the old one.
89 hash(const void *addr
, int length
)
91 const unsigned char *k
, *e
;
94 k
= (const unsigned char *)addr
;
104 /* Growing the bcache's hash table. */
106 /* If the average chain length grows beyond this, then we want to
107 resize our hash table. */
108 #define CHAIN_LENGTH_THRESHOLD (5)
111 expand_hash_table (struct bcache
*bcache
)
113 /* A table of good hash table sizes. Whenever we grow, we pick the
114 next larger size from this table. sizes[i] is close to 1 << (i+10),
115 so we roughly double the table size each time. After we fall off
116 the end of this table, we just double. Don't laugh --- there have
117 been executables sighted with a gigabyte of debug info. */
118 static unsigned long sizes
[] = {
119 1021, 2053, 4099, 8191, 16381, 32771,
120 65537, 131071, 262144, 524287, 1048573, 2097143,
121 4194301, 8388617, 16777213, 33554467, 67108859, 134217757,
122 268435459, 536870923, 1073741827, 2147483659UL
124 unsigned int new_num_buckets
;
125 struct bstring
**new_buckets
;
128 /* Count the stats. Every unique item needs to be re-hashed and
130 bcache
->expand_count
++;
131 bcache
->expand_hash_count
+= bcache
->unique_count
;
133 /* Find the next size. */
134 new_num_buckets
= bcache
->num_buckets
* 2;
135 for (i
= 0; i
< (sizeof (sizes
) / sizeof (sizes
[0])); i
++)
136 if (sizes
[i
] > bcache
->num_buckets
)
138 new_num_buckets
= sizes
[i
];
142 /* Allocate the new table. */
144 size_t new_size
= new_num_buckets
* sizeof (new_buckets
[0]);
145 new_buckets
= (struct bstring
**) xmalloc (new_size
);
146 memset (new_buckets
, 0, new_size
);
148 bcache
->structure_size
-= (bcache
->num_buckets
149 * sizeof (bcache
->bucket
[0]));
150 bcache
->structure_size
+= new_size
;
153 /* Rehash all existing strings. */
154 for (i
= 0; i
< bcache
->num_buckets
; i
++)
156 struct bstring
*s
, *next
;
158 for (s
= bcache
->bucket
[i
]; s
; s
= next
)
160 struct bstring
**new_bucket
;
163 new_bucket
= &new_buckets
[(hash (&s
->d
.data
, s
->length
)
165 s
->next
= *new_bucket
;
170 /* Plug in the new table. */
172 xfree (bcache
->bucket
);
173 bcache
->bucket
= new_buckets
;
174 bcache
->num_buckets
= new_num_buckets
;
178 /* Looking up things in the bcache. */
180 /* The number of bytes needed to allocate a struct bstring whose data
182 #define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n))
184 /* Find a copy of the LENGTH bytes at ADDR in BCACHE. If BCACHE has
185 never seen those bytes before, add a copy of them to BCACHE. In
186 either case, return a pointer to BCACHE's copy of that string. */
188 bcache (const void *addr
, int length
, struct bcache
*bcache
)
193 /* If our average chain length is too high, expand the hash table. */
194 if (bcache
->unique_count
>= bcache
->num_buckets
* CHAIN_LENGTH_THRESHOLD
)
195 expand_hash_table (bcache
);
197 bcache
->total_count
++;
198 bcache
->total_size
+= length
;
200 hash_index
= hash (addr
, length
) % bcache
->num_buckets
;
202 /* Search the hash bucket for a string identical to the caller's. */
203 for (s
= bcache
->bucket
[hash_index
]; s
; s
= s
->next
)
204 if (s
->length
== length
205 && ! memcmp (&s
->d
.data
, addr
, length
))
208 /* The user's string isn't in the list. Insert it after *ps. */
211 = obstack_alloc (&bcache
->cache
, BSTRING_SIZE (length
));
212 memcpy (&new->d
.data
, addr
, length
);
213 new->length
= length
;
214 new->next
= bcache
->bucket
[hash_index
];
215 bcache
->bucket
[hash_index
] = new;
217 bcache
->unique_count
++;
218 bcache
->unique_size
+= length
;
219 bcache
->structure_size
+= BSTRING_SIZE (length
);
226 /* Allocating and freeing bcaches. */
229 bcache_xmalloc (void)
231 /* Allocate the bcache pre-zeroed. */
232 struct bcache
*b
= XCALLOC (1, struct bcache
);
233 obstack_specify_allocation (&b
->cache
, 0, 0, xmalloc
, xfree
);
237 /* Free all the storage associated with BCACHE. */
239 bcache_xfree (struct bcache
*bcache
)
243 obstack_free (&bcache
->cache
, 0);
244 xfree (bcache
->bucket
);
250 /* Printing statistics. */
253 compare_ints (const void *ap
, const void *bp
)
255 /* Because we know we're comparing two ints which are positive,
256 there's no danger of overflow here. */
257 return * (int *) ap
- * (int *) bp
;
262 print_percentage (int portion
, int total
)
265 printf_filtered ("(not applicable)\n");
267 printf_filtered ("%3d%%\n", portion
* 100 / total
);
271 /* Print statistics on BCACHE's memory usage and efficacity at
272 eliminating duplication. NAME should describe the kind of data
273 BCACHE holds. Statistics are printed using `printf_filtered' and
276 print_bcache_statistics (struct bcache
*c
, char *type
)
278 int occupied_buckets
;
279 int max_chain_length
;
280 int median_chain_length
;
282 int median_entry_size
;
284 /* Count the number of occupied buckets, tally the various string
285 lengths, and measure chain lengths. */
288 int *chain_length
= XCALLOC (c
->num_buckets
+ 1, int);
289 int *entry_size
= XCALLOC (c
->unique_count
+ 1, int);
292 occupied_buckets
= 0;
294 for (b
= 0; b
< c
->num_buckets
; b
++)
296 struct bstring
*s
= c
->bucket
[b
];
306 gdb_assert (b
< c
->num_buckets
);
308 gdb_assert (stringi
< c
->unique_count
);
309 entry_size
[stringi
++] = s
->length
;
315 /* To compute the median, we need the set of chain lengths sorted. */
316 qsort (chain_length
, c
->num_buckets
, sizeof (chain_length
[0]),
318 qsort (entry_size
, c
->unique_count
, sizeof (entry_size
[0]),
321 if (c
->num_buckets
> 0)
323 max_chain_length
= chain_length
[c
->num_buckets
- 1];
324 median_chain_length
= chain_length
[c
->num_buckets
/ 2];
328 max_chain_length
= 0;
329 median_chain_length
= 0;
331 if (c
->unique_count
> 0)
333 max_entry_size
= entry_size
[c
->unique_count
- 1];
334 median_entry_size
= entry_size
[c
->unique_count
/ 2];
339 median_entry_size
= 0;
342 xfree (chain_length
);
346 printf_filtered (" Cached '%s' statistics:\n", type
);
347 printf_filtered (" Total object count: %ld\n", c
->total_count
);
348 printf_filtered (" Unique object count: %lu\n", c
->unique_count
);
349 printf_filtered (" Percentage of duplicates, by count: ");
350 print_percentage (c
->total_count
- c
->unique_count
, c
->total_count
);
351 printf_filtered ("\n");
353 printf_filtered (" Total object size: %ld\n", c
->total_size
);
354 printf_filtered (" Unique object size: %ld\n", c
->unique_size
);
355 printf_filtered (" Percentage of duplicates, by size: ");
356 print_percentage (c
->total_size
- c
->unique_size
, c
->total_size
);
357 printf_filtered ("\n");
359 printf_filtered (" Max entry size: %d\n", max_entry_size
);
360 printf_filtered (" Average entry size: ");
361 if (c
->unique_count
> 0)
362 printf_filtered ("%ld\n", c
->unique_size
/ c
->unique_count
);
364 printf_filtered ("(not applicable)\n");
365 printf_filtered (" Median entry size: %d\n", median_entry_size
);
366 printf_filtered ("\n");
368 printf_filtered (" Total memory used by bcache, including overhead: %ld\n",
370 printf_filtered (" Percentage memory overhead: ");
371 print_percentage (c
->structure_size
- c
->unique_size
, c
->unique_size
);
372 printf_filtered (" Net memory savings: ");
373 print_percentage (c
->total_size
- c
->structure_size
, c
->total_size
);
374 printf_filtered ("\n");
376 printf_filtered (" Hash table size: %3d\n", c
->num_buckets
);
377 printf_filtered (" Hash table expands: %lu\n",
379 printf_filtered (" Hash table hashes: %lu\n",
380 c
->total_count
+ c
->expand_hash_count
);
381 printf_filtered (" Hash table population: ");
382 print_percentage (occupied_buckets
, c
->num_buckets
);
383 printf_filtered (" Median hash chain length: %3d\n",
384 median_chain_length
);
385 printf_filtered (" Average hash chain length: ");
386 if (c
->num_buckets
> 0)
387 printf_filtered ("%3lu\n", c
->unique_count
/ c
->num_buckets
);
389 printf_filtered ("(not applicable)\n");
390 printf_filtered (" Maximum hash chain length: %3d\n", max_chain_length
);
391 printf_filtered ("\n");
395 bcache_memory_used (struct bcache
*bcache
)
397 return obstack_memory_used (&bcache
->cache
);
This page took 0.036807 seconds and 4 git commands to generate.