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