Commit | Line | Data |
---|---|---|
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 | |
42a4f53d | 5 | Copyright (C) 1999-2019 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" | |
d55e5aa6 | 23 | #include "gdb_obstack.h" |
4de283e4 | 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 | ||
32 | struct 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 | ||
4a64f543 MS |
52 | /* The old hash function was stolen from SDBM. This is what DB 3.0 |
53 | uses now, and is better than the old one. */ | |
c2d11a7d | 54 | \f |
c2d11a7d | 55 | unsigned long |
d85a5daf | 56 | hash(const void *addr, int length) |
cbd70537 SW |
57 | { |
58 | return hash_continue (addr, length, 0); | |
59 | } | |
60 | ||
61 | /* Continue the calculation of the hash H at the given address. */ | |
62 | ||
63 | unsigned long | |
64 | hash_continue (const void *addr, int length, unsigned long h) | |
c2d11a7d | 65 | { |
b6de9da4 | 66 | const unsigned char *k, *e; |
b6de9da4 MS |
67 | |
68 | k = (const unsigned char *)addr; | |
69 | e = k+length; | |
cbd70537 | 70 | for (; k< e;++k) |
b6de9da4 MS |
71 | { |
72 | h *=16777619; | |
73 | h ^= *k; | |
74 | } | |
75 | return (h); | |
c906108c | 76 | } |
c2d11a7d JM |
77 | \f |
78 | /* Growing the bcache's hash table. */ | |
79 | ||
80 | /* If the average chain length grows beyond this, then we want to | |
81 | resize our hash table. */ | |
82 | #define CHAIN_LENGTH_THRESHOLD (5) | |
83 | ||
25629dfd TT |
84 | void |
85 | bcache::expand_hash_table () | |
c906108c | 86 | { |
c2d11a7d JM |
87 | /* A table of good hash table sizes. Whenever we grow, we pick the |
88 | next larger size from this table. sizes[i] is close to 1 << (i+10), | |
89 | so we roughly double the table size each time. After we fall off | |
90 | the end of this table, we just double. Don't laugh --- there have | |
91 | been executables sighted with a gigabyte of debug info. */ | |
92 | static unsigned long sizes[] = { | |
93 | 1021, 2053, 4099, 8191, 16381, 32771, | |
94 | 65537, 131071, 262144, 524287, 1048573, 2097143, | |
95 | 4194301, 8388617, 16777213, 33554467, 67108859, 134217757, | |
96 | 268435459, 536870923, 1073741827, 2147483659UL | |
97 | }; | |
745b8ca0 | 98 | unsigned int new_num_buckets; |
c2d11a7d | 99 | struct bstring **new_buckets; |
745b8ca0 | 100 | unsigned int i; |
c2d11a7d | 101 | |
2160782c AC |
102 | /* Count the stats. Every unique item needs to be re-hashed and |
103 | re-entered. */ | |
25629dfd TT |
104 | m_expand_count++; |
105 | m_expand_hash_count += m_unique_count; | |
2160782c | 106 | |
c2d11a7d | 107 | /* Find the next size. */ |
25629dfd | 108 | new_num_buckets = m_num_buckets * 2; |
c2d11a7d | 109 | for (i = 0; i < (sizeof (sizes) / sizeof (sizes[0])); i++) |
25629dfd | 110 | if (sizes[i] > m_num_buckets) |
c2d11a7d JM |
111 | { |
112 | new_num_buckets = sizes[i]; | |
113 | break; | |
114 | } | |
c2d11a7d JM |
115 | |
116 | /* Allocate the new table. */ | |
117 | { | |
118 | size_t new_size = new_num_buckets * sizeof (new_buckets[0]); | |
b6de9da4 | 119 | |
c2d11a7d JM |
120 | new_buckets = (struct bstring **) xmalloc (new_size); |
121 | memset (new_buckets, 0, new_size); | |
122 | ||
25629dfd TT |
123 | m_structure_size -= m_num_buckets * sizeof (m_bucket[0]); |
124 | m_structure_size += new_size; | |
c2d11a7d | 125 | } |
c906108c | 126 | |
c2d11a7d | 127 | /* Rehash all existing strings. */ |
25629dfd | 128 | for (i = 0; i < m_num_buckets; i++) |
c906108c | 129 | { |
c2d11a7d JM |
130 | struct bstring *s, *next; |
131 | ||
25629dfd | 132 | for (s = m_bucket[i]; s; s = next) |
c906108c | 133 | { |
c2d11a7d JM |
134 | struct bstring **new_bucket; |
135 | next = s->next; | |
136 | ||
25629dfd | 137 | new_bucket = &new_buckets[(m_hash_function (&s->d.data, s->length) |
c2d11a7d JM |
138 | % new_num_buckets)]; |
139 | s->next = *new_bucket; | |
140 | *new_bucket = s; | |
c906108c SS |
141 | } |
142 | } | |
c2d11a7d JM |
143 | |
144 | /* Plug in the new table. */ | |
25629dfd TT |
145 | xfree (m_bucket); |
146 | m_bucket = new_buckets; | |
147 | m_num_buckets = new_num_buckets; | |
c906108c SS |
148 | } |
149 | ||
c2d11a7d JM |
150 | \f |
151 | /* Looking up things in the bcache. */ | |
152 | ||
153 | /* The number of bytes needed to allocate a struct bstring whose data | |
154 | is N bytes long. */ | |
155 | #define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n)) | |
156 | ||
2e618c13 AR |
157 | /* Find a copy of the LENGTH bytes at ADDR in BCACHE. If BCACHE has |
158 | never seen those bytes before, add a copy of them to BCACHE. In | |
159 | either case, return a pointer to BCACHE's copy of that string. If | |
160 | optional ADDED is not NULL, return 1 in case of new entry or 0 if | |
161 | returning an old entry. */ | |
162 | ||
11d31d94 | 163 | const void * |
25629dfd | 164 | bcache::insert (const void *addr, int length, int *added) |
c906108c | 165 | { |
49df298f AC |
166 | unsigned long full_hash; |
167 | unsigned short half_hash; | |
c2d11a7d JM |
168 | int hash_index; |
169 | struct bstring *s; | |
c906108c | 170 | |
2e618c13 AR |
171 | if (added) |
172 | *added = 0; | |
173 | ||
e6ad058a TT |
174 | /* Lazily initialize the obstack. This can save quite a bit of |
175 | memory in some cases. */ | |
25629dfd | 176 | if (m_total_count == 0) |
e6ad058a TT |
177 | { |
178 | /* We could use obstack_specify_allocation here instead, but | |
179 | gdb_obstack.h specifies the allocation/deallocation | |
180 | functions. */ | |
25629dfd | 181 | obstack_init (&m_cache); |
e6ad058a TT |
182 | } |
183 | ||
c2d11a7d | 184 | /* If our average chain length is too high, expand the hash table. */ |
25629dfd TT |
185 | if (m_unique_count >= m_num_buckets * CHAIN_LENGTH_THRESHOLD) |
186 | expand_hash_table (); | |
c2d11a7d | 187 | |
25629dfd TT |
188 | m_total_count++; |
189 | m_total_size += length; | |
c2d11a7d | 190 | |
25629dfd | 191 | full_hash = m_hash_function (addr, length); |
cbd70537 | 192 | |
49df298f | 193 | half_hash = (full_hash >> 16); |
25629dfd | 194 | hash_index = full_hash % m_num_buckets; |
c2d11a7d | 195 | |
25629dfd | 196 | /* Search the hash m_bucket for a string identical to the caller's. |
49df298f AC |
197 | As a short-circuit first compare the upper part of each hash |
198 | values. */ | |
25629dfd | 199 | for (s = m_bucket[hash_index]; s; s = s->next) |
49df298f AC |
200 | { |
201 | if (s->half_hash == half_hash) | |
202 | { | |
203 | if (s->length == length | |
25629dfd | 204 | && m_compare_function (&s->d.data, addr, length)) |
49df298f AC |
205 | return &s->d.data; |
206 | else | |
25629dfd | 207 | m_half_hash_miss_count++; |
49df298f AC |
208 | } |
209 | } | |
c2d11a7d JM |
210 | |
211 | /* The user's string isn't in the list. Insert it after *ps. */ | |
212 | { | |
fe978cb0 | 213 | struct bstring *newobj |
25629dfd | 214 | = (struct bstring *) obstack_alloc (&m_cache, |
224c3ddb | 215 | BSTRING_SIZE (length)); |
b6de9da4 | 216 | |
fe978cb0 PA |
217 | memcpy (&newobj->d.data, addr, length); |
218 | newobj->length = length; | |
25629dfd | 219 | newobj->next = m_bucket[hash_index]; |
fe978cb0 | 220 | newobj->half_hash = half_hash; |
25629dfd | 221 | m_bucket[hash_index] = newobj; |
c2d11a7d | 222 | |
25629dfd TT |
223 | m_unique_count++; |
224 | m_unique_size += length; | |
225 | m_structure_size += BSTRING_SIZE (length); | |
c2d11a7d | 226 | |
2e618c13 AR |
227 | if (added) |
228 | *added = 1; | |
229 | ||
fe978cb0 | 230 | return &newobj->d.data; |
c2d11a7d | 231 | } |
c906108c | 232 | } |
c2d11a7d | 233 | \f |
cbd70537 SW |
234 | |
235 | /* Compare the byte string at ADDR1 of lenght LENGHT to the | |
236 | string at ADDR2. Return 1 if they are equal. */ | |
237 | ||
25629dfd TT |
238 | int |
239 | bcache::compare (const void *addr1, const void *addr2, int length) | |
cbd70537 SW |
240 | { |
241 | return memcmp (addr1, addr2, length) == 0; | |
242 | } | |
243 | ||
c2d11a7d | 244 | /* Free all the storage associated with BCACHE. */ |
25629dfd | 245 | bcache::~bcache () |
c906108c | 246 | { |
e6ad058a | 247 | /* Only free the obstack if we actually initialized it. */ |
25629dfd TT |
248 | if (m_total_count > 0) |
249 | obstack_free (&m_cache, 0); | |
250 | xfree (m_bucket); | |
c2d11a7d JM |
251 | } |
252 | ||
253 | ||
254 | \f | |
255 | /* Printing statistics. */ | |
256 | ||
c2d11a7d JM |
257 | static void |
258 | print_percentage (int portion, int total) | |
259 | { | |
260 | if (total == 0) | |
4a64f543 | 261 | /* i18n: Like "Percentage of duplicates, by count: (not applicable)". */ |
3d263c1d | 262 | printf_filtered (_("(not applicable)\n")); |
c906108c | 263 | else |
b2ba182e | 264 | printf_filtered ("%3d%%\n", (int) (portion * 100.0 / total)); |
c2d11a7d JM |
265 | } |
266 | ||
267 | ||
268 | /* Print statistics on BCACHE's memory usage and efficacity at | |
269 | eliminating duplication. NAME should describe the kind of data | |
270 | BCACHE holds. Statistics are printed using `printf_filtered' and | |
271 | its ilk. */ | |
272 | void | |
25629dfd | 273 | bcache::print_statistics (const char *type) |
c2d11a7d JM |
274 | { |
275 | int occupied_buckets; | |
276 | int max_chain_length; | |
277 | int median_chain_length; | |
2160782c AC |
278 | int max_entry_size; |
279 | int median_entry_size; | |
c2d11a7d | 280 | |
2160782c AC |
281 | /* Count the number of occupied buckets, tally the various string |
282 | lengths, and measure chain lengths. */ | |
c2d11a7d | 283 | { |
745b8ca0 | 284 | unsigned int b; |
25629dfd TT |
285 | int *chain_length = XCNEWVEC (int, m_num_buckets + 1); |
286 | int *entry_size = XCNEWVEC (int, m_unique_count + 1); | |
2160782c | 287 | int stringi = 0; |
c2d11a7d JM |
288 | |
289 | occupied_buckets = 0; | |
290 | ||
25629dfd | 291 | for (b = 0; b < m_num_buckets; b++) |
c2d11a7d | 292 | { |
25629dfd | 293 | struct bstring *s = m_bucket[b]; |
c2d11a7d JM |
294 | |
295 | chain_length[b] = 0; | |
296 | ||
297 | if (s) | |
298 | { | |
299 | occupied_buckets++; | |
300 | ||
301 | while (s) | |
302 | { | |
25629dfd | 303 | gdb_assert (b < m_num_buckets); |
c2d11a7d | 304 | chain_length[b]++; |
25629dfd | 305 | gdb_assert (stringi < m_unique_count); |
2160782c | 306 | entry_size[stringi++] = s->length; |
c2d11a7d JM |
307 | s = s->next; |
308 | } | |
309 | } | |
310 | } | |
311 | ||
4a64f543 MS |
312 | /* To compute the median, we need the set of chain lengths |
313 | sorted. */ | |
25629dfd | 314 | qsort (chain_length, m_num_buckets, sizeof (chain_length[0]), |
dc146f7c | 315 | compare_positive_ints); |
25629dfd | 316 | qsort (entry_size, m_unique_count, sizeof (entry_size[0]), |
dc146f7c | 317 | compare_positive_ints); |
c2d11a7d | 318 | |
25629dfd | 319 | if (m_num_buckets > 0) |
c2d11a7d | 320 | { |
25629dfd TT |
321 | max_chain_length = chain_length[m_num_buckets - 1]; |
322 | median_chain_length = chain_length[m_num_buckets / 2]; | |
c2d11a7d JM |
323 | } |
324 | else | |
325 | { | |
326 | max_chain_length = 0; | |
327 | median_chain_length = 0; | |
328 | } | |
25629dfd | 329 | if (m_unique_count > 0) |
2160782c | 330 | { |
25629dfd TT |
331 | max_entry_size = entry_size[m_unique_count - 1]; |
332 | median_entry_size = entry_size[m_unique_count / 2]; | |
2160782c AC |
333 | } |
334 | else | |
335 | { | |
336 | max_entry_size = 0; | |
337 | median_entry_size = 0; | |
338 | } | |
339 | ||
340 | xfree (chain_length); | |
341 | xfree (entry_size); | |
c2d11a7d JM |
342 | } |
343 | ||
25629dfd TT |
344 | printf_filtered (_(" M_Cached '%s' statistics:\n"), type); |
345 | printf_filtered (_(" Total object count: %ld\n"), m_total_count); | |
346 | printf_filtered (_(" Unique object count: %lu\n"), m_unique_count); | |
3d263c1d | 347 | printf_filtered (_(" Percentage of duplicates, by count: ")); |
25629dfd | 348 | print_percentage (m_total_count - m_unique_count, m_total_count); |
c2d11a7d JM |
349 | printf_filtered ("\n"); |
350 | ||
25629dfd TT |
351 | printf_filtered (_(" Total object size: %ld\n"), m_total_size); |
352 | printf_filtered (_(" Unique object size: %ld\n"), m_unique_size); | |
3d263c1d | 353 | printf_filtered (_(" Percentage of duplicates, by size: ")); |
25629dfd | 354 | print_percentage (m_total_size - m_unique_size, m_total_size); |
c2d11a7d JM |
355 | printf_filtered ("\n"); |
356 | ||
3d263c1d BI |
357 | printf_filtered (_(" Max entry size: %d\n"), max_entry_size); |
358 | printf_filtered (_(" Average entry size: ")); | |
25629dfd TT |
359 | if (m_unique_count > 0) |
360 | printf_filtered ("%ld\n", m_unique_size / m_unique_count); | |
2160782c | 361 | else |
4a64f543 | 362 | /* i18n: "Average entry size: (not applicable)". */ |
3d263c1d BI |
363 | printf_filtered (_("(not applicable)\n")); |
364 | printf_filtered (_(" Median entry size: %d\n"), median_entry_size); | |
2160782c AC |
365 | printf_filtered ("\n"); |
366 | ||
3e43a32a MS |
367 | printf_filtered (_(" \ |
368 | Total memory used by bcache, including overhead: %ld\n"), | |
25629dfd | 369 | m_structure_size); |
3d263c1d | 370 | printf_filtered (_(" Percentage memory overhead: ")); |
25629dfd | 371 | print_percentage (m_structure_size - m_unique_size, m_unique_size); |
3d263c1d | 372 | printf_filtered (_(" Net memory savings: ")); |
25629dfd | 373 | print_percentage (m_total_size - m_structure_size, m_total_size); |
c2d11a7d JM |
374 | printf_filtered ("\n"); |
375 | ||
4a64f543 | 376 | printf_filtered (_(" Hash table size: %3d\n"), |
25629dfd | 377 | m_num_buckets); |
3d263c1d | 378 | printf_filtered (_(" Hash table expands: %lu\n"), |
25629dfd | 379 | m_expand_count); |
3d263c1d | 380 | printf_filtered (_(" Hash table hashes: %lu\n"), |
25629dfd | 381 | m_total_count + m_expand_hash_count); |
3d263c1d | 382 | printf_filtered (_(" Half hash misses: %lu\n"), |
25629dfd | 383 | m_half_hash_miss_count); |
3d263c1d | 384 | printf_filtered (_(" Hash table population: ")); |
25629dfd | 385 | print_percentage (occupied_buckets, m_num_buckets); |
3d263c1d | 386 | printf_filtered (_(" Median hash chain length: %3d\n"), |
c2d11a7d | 387 | median_chain_length); |
3d263c1d | 388 | printf_filtered (_(" Average hash chain length: ")); |
25629dfd TT |
389 | if (m_num_buckets > 0) |
390 | printf_filtered ("%3lu\n", m_unique_count / m_num_buckets); | |
c906108c | 391 | else |
4a64f543 | 392 | /* i18n: "Average hash chain length: (not applicable)". */ |
3d263c1d | 393 | printf_filtered (_("(not applicable)\n")); |
4a64f543 MS |
394 | printf_filtered (_(" Maximum hash chain length: %3d\n"), |
395 | max_chain_length); | |
c2d11a7d | 396 | printf_filtered ("\n"); |
c906108c | 397 | } |
af5f3db6 AC |
398 | |
399 | int | |
25629dfd | 400 | bcache::memory_used () |
af5f3db6 | 401 | { |
25629dfd | 402 | if (m_total_count == 0) |
e6ad058a | 403 | return 0; |
25629dfd | 404 | return obstack_memory_used (&m_cache); |
af5f3db6 | 405 | } |