1 /* Implement a cached obstack.
2 Written by Fred Fish <fnf@cygnus.com>
3 Rewritten by Jim Blandy <jimb@cygnus.com>
5 Copyright (C) 1999-2022 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "gdbsupport/gdb_obstack.h"
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. */
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
;
57 /* Growing the bcache's hash table. */
59 /* If the average chain length grows beyond this, then we want to
60 resize our hash table. */
61 #define CHAIN_LENGTH_THRESHOLD (5)
64 bcache::expand_hash_table ()
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 const 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
77 unsigned int new_num_buckets
;
78 struct bstring
**new_buckets
;
81 /* Count the stats. Every unique item needs to be re-hashed and
84 m_expand_hash_count
+= m_unique_count
;
86 /* Find the next size. */
87 new_num_buckets
= m_num_buckets
* 2;
88 for (unsigned long a_size
: sizes
)
89 if (a_size
> m_num_buckets
)
91 new_num_buckets
= a_size
;
95 /* Allocate the new table. */
97 size_t new_size
= new_num_buckets
* sizeof (new_buckets
[0]);
99 new_buckets
= (struct bstring
**) xmalloc (new_size
);
100 memset (new_buckets
, 0, new_size
);
102 m_structure_size
-= m_num_buckets
* sizeof (m_bucket
[0]);
103 m_structure_size
+= new_size
;
106 /* Rehash all existing strings. */
107 for (i
= 0; i
< m_num_buckets
; i
++)
109 struct bstring
*s
, *next
;
111 for (s
= m_bucket
[i
]; s
; s
= next
)
113 struct bstring
**new_bucket
;
116 new_bucket
= &new_buckets
[(this->hash (&s
->d
.data
, s
->length
)
118 s
->next
= *new_bucket
;
123 /* Plug in the new table. */
125 m_bucket
= new_buckets
;
126 m_num_buckets
= new_num_buckets
;
130 /* Looking up things in the bcache. */
132 /* The number of bytes needed to allocate a struct bstring whose data
134 #define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n))
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. */
143 bcache::insert (const void *addr
, int length
, bool *added
)
145 unsigned long full_hash
;
146 unsigned short half_hash
;
150 if (added
!= nullptr)
153 /* Lazily initialize the obstack. This can save quite a bit of
154 memory in some cases. */
155 if (m_total_count
== 0)
157 /* We could use obstack_specify_allocation here instead, but
158 gdb_obstack.h specifies the allocation/deallocation
160 obstack_init (&m_cache
);
163 /* If our average chain length is too high, expand the hash table. */
164 if (m_unique_count
>= m_num_buckets
* CHAIN_LENGTH_THRESHOLD
)
165 expand_hash_table ();
168 m_total_size
+= length
;
170 full_hash
= this->hash (addr
, length
);
172 half_hash
= (full_hash
>> 16);
173 hash_index
= full_hash
% m_num_buckets
;
175 /* Search the hash m_bucket for a string identical to the caller's.
176 As a short-circuit first compare the upper part of each hash
178 for (s
= m_bucket
[hash_index
]; s
; s
= s
->next
)
180 if (s
->half_hash
== half_hash
)
182 if (s
->length
== length
183 && this->compare (&s
->d
.data
, addr
, length
))
186 m_half_hash_miss_count
++;
190 /* The user's string isn't in the list. Insert it after *ps. */
192 struct bstring
*newobj
193 = (struct bstring
*) obstack_alloc (&m_cache
,
194 BSTRING_SIZE (length
));
196 memcpy (&newobj
->d
.data
, addr
, length
);
197 newobj
->length
= length
;
198 newobj
->next
= m_bucket
[hash_index
];
199 newobj
->half_hash
= half_hash
;
200 m_bucket
[hash_index
] = newobj
;
203 m_unique_size
+= length
;
204 m_structure_size
+= BSTRING_SIZE (length
);
206 if (added
!= nullptr)
209 return &newobj
->d
.data
;
217 bcache::hash (const void *addr
, int length
)
219 return fast_hash (addr
, length
, 0);
225 bcache::compare (const void *left
, const void *right
, int length
)
227 return memcmp (left
, right
, length
) == 0;
230 /* Free all the storage associated with BCACHE. */
233 /* Only free the obstack if we actually initialized it. */
234 if (m_total_count
> 0)
235 obstack_free (&m_cache
, 0);
241 /* Printing statistics. */
244 print_percentage (int portion
, int total
)
247 /* i18n: Like "Percentage of duplicates, by count: (not applicable)". */
248 gdb_printf (_("(not applicable)\n"));
250 gdb_printf ("%3d%%\n", (int) (portion
* 100.0 / total
));
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 `gdb_printf' and
259 bcache::print_statistics (const char *type
)
261 int occupied_buckets
;
262 int max_chain_length
;
263 int median_chain_length
;
265 int median_entry_size
;
267 /* Count the number of occupied buckets, tally the various string
268 lengths, and measure chain lengths. */
271 int *chain_length
= XCNEWVEC (int, m_num_buckets
+ 1);
272 int *entry_size
= XCNEWVEC (int, m_unique_count
+ 1);
275 occupied_buckets
= 0;
277 for (b
= 0; b
< m_num_buckets
; b
++)
279 struct bstring
*s
= m_bucket
[b
];
289 gdb_assert (b
< m_num_buckets
);
291 gdb_assert (stringi
< m_unique_count
);
292 entry_size
[stringi
++] = s
->length
;
298 /* To compute the median, we need the set of chain lengths
300 std::sort (chain_length
, chain_length
+ m_num_buckets
);
301 std::sort (entry_size
, entry_size
+ m_unique_count
);
303 if (m_num_buckets
> 0)
305 max_chain_length
= chain_length
[m_num_buckets
- 1];
306 median_chain_length
= chain_length
[m_num_buckets
/ 2];
310 max_chain_length
= 0;
311 median_chain_length
= 0;
313 if (m_unique_count
> 0)
315 max_entry_size
= entry_size
[m_unique_count
- 1];
316 median_entry_size
= entry_size
[m_unique_count
/ 2];
321 median_entry_size
= 0;
324 xfree (chain_length
);
328 gdb_printf (_(" M_Cached '%s' statistics:\n"), type
);
329 gdb_printf (_(" Total object count: %ld\n"), m_total_count
);
330 gdb_printf (_(" Unique object count: %lu\n"), m_unique_count
);
331 gdb_printf (_(" Percentage of duplicates, by count: "));
332 print_percentage (m_total_count
- m_unique_count
, m_total_count
);
335 gdb_printf (_(" Total object size: %ld\n"), m_total_size
);
336 gdb_printf (_(" Unique object size: %ld\n"), m_unique_size
);
337 gdb_printf (_(" Percentage of duplicates, by size: "));
338 print_percentage (m_total_size
- m_unique_size
, m_total_size
);
341 gdb_printf (_(" Max entry size: %d\n"), max_entry_size
);
342 gdb_printf (_(" Average entry size: "));
343 if (m_unique_count
> 0)
344 gdb_printf ("%ld\n", m_unique_size
/ m_unique_count
);
346 /* i18n: "Average entry size: (not applicable)". */
347 gdb_printf (_("(not applicable)\n"));
348 gdb_printf (_(" Median entry size: %d\n"), median_entry_size
);
352 Total memory used by bcache, including overhead: %ld\n"),
354 gdb_printf (_(" Percentage memory overhead: "));
355 print_percentage (m_structure_size
- m_unique_size
, m_unique_size
);
356 gdb_printf (_(" Net memory savings: "));
357 print_percentage (m_total_size
- m_structure_size
, m_total_size
);
360 gdb_printf (_(" Hash table size: %3d\n"),
362 gdb_printf (_(" Hash table expands: %lu\n"),
364 gdb_printf (_(" Hash table hashes: %lu\n"),
365 m_total_count
+ m_expand_hash_count
);
366 gdb_printf (_(" Half hash misses: %lu\n"),
367 m_half_hash_miss_count
);
368 gdb_printf (_(" Hash table population: "));
369 print_percentage (occupied_buckets
, m_num_buckets
);
370 gdb_printf (_(" Median hash chain length: %3d\n"),
371 median_chain_length
);
372 gdb_printf (_(" Average hash chain length: "));
373 if (m_num_buckets
> 0)
374 gdb_printf ("%3lu\n", m_unique_count
/ m_num_buckets
);
376 /* i18n: "Average hash chain length: (not applicable)". */
377 gdb_printf (_("(not applicable)\n"));
378 gdb_printf (_(" Maximum hash chain length: %3d\n"),
384 bcache::memory_used ()
386 if (m_total_count
== 0)
388 return obstack_memory_used (&m_cache
);
391 } /* namespace gdb */