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