bcache.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Implement a cached obstack.
  2. Written by Fred Fish <fnf@cygnus.com>
  3. Rewritten by Jim Blandy <jimb@cygnus.com>
  4. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "gdbsupport/gdb_obstack.h"
  18. #include "bcache.h"
  19. #include <algorithm>
  20. namespace gdb {
  21. /* The type used to hold a single bcache string. The user data is
  22. stored in d.data. Since it can be any type, it needs to have the
  23. same alignment as the most strict alignment of any type on the host
  24. machine. I don't know of any really correct way to do this in
  25. stock ANSI C, so just do it the same way obstack.h does. */
  26. struct bstring
  27. {
  28. /* Hash chain. */
  29. struct bstring *next;
  30. /* Assume the data length is no more than 64k. */
  31. unsigned short length;
  32. /* The half hash hack. This contains the upper 16 bits of the hash
  33. value and is used as a pre-check when comparing two strings and
  34. avoids the need to do length or memcmp calls. It proves to be
  35. roughly 100% effective. */
  36. unsigned short half_hash;
  37. union
  38. {
  39. char data[1];
  40. double dummy;
  41. }
  42. d;
  43. };
  44. /* Growing the bcache's hash table. */
  45. /* If the average chain length grows beyond this, then we want to
  46. resize our hash table. */
  47. #define CHAIN_LENGTH_THRESHOLD (5)
  48. void
  49. bcache::expand_hash_table ()
  50. {
  51. /* A table of good hash table sizes. Whenever we grow, we pick the
  52. next larger size from this table. sizes[i] is close to 1 << (i+10),
  53. so we roughly double the table size each time. After we fall off
  54. the end of this table, we just double. Don't laugh --- there have
  55. been executables sighted with a gigabyte of debug info. */
  56. static const unsigned long sizes[] = {
  57. 1021, 2053, 4099, 8191, 16381, 32771,
  58. 65537, 131071, 262144, 524287, 1048573, 2097143,
  59. 4194301, 8388617, 16777213, 33554467, 67108859, 134217757,
  60. 268435459, 536870923, 1073741827, 2147483659UL
  61. };
  62. unsigned int new_num_buckets;
  63. struct bstring **new_buckets;
  64. unsigned int i;
  65. /* Count the stats. Every unique item needs to be re-hashed and
  66. re-entered. */
  67. m_expand_count++;
  68. m_expand_hash_count += m_unique_count;
  69. /* Find the next size. */
  70. new_num_buckets = m_num_buckets * 2;
  71. for (unsigned long a_size : sizes)
  72. if (a_size > m_num_buckets)
  73. {
  74. new_num_buckets = a_size;
  75. break;
  76. }
  77. /* Allocate the new table. */
  78. {
  79. size_t new_size = new_num_buckets * sizeof (new_buckets[0]);
  80. new_buckets = (struct bstring **) xmalloc (new_size);
  81. memset (new_buckets, 0, new_size);
  82. m_structure_size -= m_num_buckets * sizeof (m_bucket[0]);
  83. m_structure_size += new_size;
  84. }
  85. /* Rehash all existing strings. */
  86. for (i = 0; i < m_num_buckets; i++)
  87. {
  88. struct bstring *s, *next;
  89. for (s = m_bucket[i]; s; s = next)
  90. {
  91. struct bstring **new_bucket;
  92. next = s->next;
  93. new_bucket = &new_buckets[(this->hash (&s->d.data, s->length)
  94. % new_num_buckets)];
  95. s->next = *new_bucket;
  96. *new_bucket = s;
  97. }
  98. }
  99. /* Plug in the new table. */
  100. xfree (m_bucket);
  101. m_bucket = new_buckets;
  102. m_num_buckets = new_num_buckets;
  103. }
  104. /* Looking up things in the bcache. */
  105. /* The number of bytes needed to allocate a struct bstring whose data
  106. is N bytes long. */
  107. #define BSTRING_SIZE(n) (offsetof (struct bstring, d.data) + (n))
  108. /* Find a copy of the LENGTH bytes at ADDR in BCACHE. If BCACHE has
  109. never seen those bytes before, add a copy of them to BCACHE. In
  110. either case, return a pointer to BCACHE's copy of that string. If
  111. optional ADDED is not NULL, return 1 in case of new entry or 0 if
  112. returning an old entry. */
  113. const void *
  114. bcache::insert (const void *addr, int length, bool *added)
  115. {
  116. unsigned long full_hash;
  117. unsigned short half_hash;
  118. int hash_index;
  119. struct bstring *s;
  120. if (added != nullptr)
  121. *added = false;
  122. /* Lazily initialize the obstack. This can save quite a bit of
  123. memory in some cases. */
  124. if (m_total_count == 0)
  125. {
  126. /* We could use obstack_specify_allocation here instead, but
  127. gdb_obstack.h specifies the allocation/deallocation
  128. functions. */
  129. obstack_init (&m_cache);
  130. }
  131. /* If our average chain length is too high, expand the hash table. */
  132. if (m_unique_count >= m_num_buckets * CHAIN_LENGTH_THRESHOLD)
  133. expand_hash_table ();
  134. m_total_count++;
  135. m_total_size += length;
  136. full_hash = this->hash (addr, length);
  137. half_hash = (full_hash >> 16);
  138. hash_index = full_hash % m_num_buckets;
  139. /* Search the hash m_bucket for a string identical to the caller's.
  140. As a short-circuit first compare the upper part of each hash
  141. values. */
  142. for (s = m_bucket[hash_index]; s; s = s->next)
  143. {
  144. if (s->half_hash == half_hash)
  145. {
  146. if (s->length == length
  147. && this->compare (&s->d.data, addr, length))
  148. return &s->d.data;
  149. else
  150. m_half_hash_miss_count++;
  151. }
  152. }
  153. /* The user's string isn't in the list. Insert it after *ps. */
  154. {
  155. struct bstring *newobj
  156. = (struct bstring *) obstack_alloc (&m_cache,
  157. BSTRING_SIZE (length));
  158. memcpy (&newobj->d.data, addr, length);
  159. newobj->length = length;
  160. newobj->next = m_bucket[hash_index];
  161. newobj->half_hash = half_hash;
  162. m_bucket[hash_index] = newobj;
  163. m_unique_count++;
  164. m_unique_size += length;
  165. m_structure_size += BSTRING_SIZE (length);
  166. if (added != nullptr)
  167. *added = true;
  168. return &newobj->d.data;
  169. }
  170. }
  171. /* See bcache.h. */
  172. unsigned long
  173. bcache::hash (const void *addr, int length)
  174. {
  175. return fast_hash (addr, length, 0);
  176. }
  177. /* See bcache.h. */
  178. int
  179. bcache::compare (const void *left, const void *right, int length)
  180. {
  181. return memcmp (left, right, length) == 0;
  182. }
  183. /* Free all the storage associated with BCACHE. */
  184. bcache::~bcache ()
  185. {
  186. /* Only free the obstack if we actually initialized it. */
  187. if (m_total_count > 0)
  188. obstack_free (&m_cache, 0);
  189. xfree (m_bucket);
  190. }
  191. /* Printing statistics. */
  192. static void
  193. print_percentage (int portion, int total)
  194. {
  195. if (total == 0)
  196. /* i18n: Like "Percentage of duplicates, by count: (not applicable)". */
  197. gdb_printf (_("(not applicable)\n"));
  198. else
  199. gdb_printf ("%3d%%\n", (int) (portion * 100.0 / total));
  200. }
  201. /* Print statistics on BCACHE's memory usage and efficacity at
  202. eliminating duplication. NAME should describe the kind of data
  203. BCACHE holds. Statistics are printed using `gdb_printf' and
  204. its ilk. */
  205. void
  206. bcache::print_statistics (const char *type)
  207. {
  208. int occupied_buckets;
  209. int max_chain_length;
  210. int median_chain_length;
  211. int max_entry_size;
  212. int median_entry_size;
  213. /* Count the number of occupied buckets, tally the various string
  214. lengths, and measure chain lengths. */
  215. {
  216. unsigned int b;
  217. int *chain_length = XCNEWVEC (int, m_num_buckets + 1);
  218. int *entry_size = XCNEWVEC (int, m_unique_count + 1);
  219. int stringi = 0;
  220. occupied_buckets = 0;
  221. for (b = 0; b < m_num_buckets; b++)
  222. {
  223. struct bstring *s = m_bucket[b];
  224. chain_length[b] = 0;
  225. if (s)
  226. {
  227. occupied_buckets++;
  228. while (s)
  229. {
  230. gdb_assert (b < m_num_buckets);
  231. chain_length[b]++;
  232. gdb_assert (stringi < m_unique_count);
  233. entry_size[stringi++] = s->length;
  234. s = s->next;
  235. }
  236. }
  237. }
  238. /* To compute the median, we need the set of chain lengths
  239. sorted. */
  240. std::sort (chain_length, chain_length + m_num_buckets);
  241. std::sort (entry_size, entry_size + m_unique_count);
  242. if (m_num_buckets > 0)
  243. {
  244. max_chain_length = chain_length[m_num_buckets - 1];
  245. median_chain_length = chain_length[m_num_buckets / 2];
  246. }
  247. else
  248. {
  249. max_chain_length = 0;
  250. median_chain_length = 0;
  251. }
  252. if (m_unique_count > 0)
  253. {
  254. max_entry_size = entry_size[m_unique_count - 1];
  255. median_entry_size = entry_size[m_unique_count / 2];
  256. }
  257. else
  258. {
  259. max_entry_size = 0;
  260. median_entry_size = 0;
  261. }
  262. xfree (chain_length);
  263. xfree (entry_size);
  264. }
  265. gdb_printf (_(" M_Cached '%s' statistics:\n"), type);
  266. gdb_printf (_(" Total object count: %ld\n"), m_total_count);
  267. gdb_printf (_(" Unique object count: %lu\n"), m_unique_count);
  268. gdb_printf (_(" Percentage of duplicates, by count: "));
  269. print_percentage (m_total_count - m_unique_count, m_total_count);
  270. gdb_printf ("\n");
  271. gdb_printf (_(" Total object size: %ld\n"), m_total_size);
  272. gdb_printf (_(" Unique object size: %ld\n"), m_unique_size);
  273. gdb_printf (_(" Percentage of duplicates, by size: "));
  274. print_percentage (m_total_size - m_unique_size, m_total_size);
  275. gdb_printf ("\n");
  276. gdb_printf (_(" Max entry size: %d\n"), max_entry_size);
  277. gdb_printf (_(" Average entry size: "));
  278. if (m_unique_count > 0)
  279. gdb_printf ("%ld\n", m_unique_size / m_unique_count);
  280. else
  281. /* i18n: "Average entry size: (not applicable)". */
  282. gdb_printf (_("(not applicable)\n"));
  283. gdb_printf (_(" Median entry size: %d\n"), median_entry_size);
  284. gdb_printf ("\n");
  285. gdb_printf (_(" \
  286. Total memory used by bcache, including overhead: %ld\n"),
  287. m_structure_size);
  288. gdb_printf (_(" Percentage memory overhead: "));
  289. print_percentage (m_structure_size - m_unique_size, m_unique_size);
  290. gdb_printf (_(" Net memory savings: "));
  291. print_percentage (m_total_size - m_structure_size, m_total_size);
  292. gdb_printf ("\n");
  293. gdb_printf (_(" Hash table size: %3d\n"),
  294. m_num_buckets);
  295. gdb_printf (_(" Hash table expands: %lu\n"),
  296. m_expand_count);
  297. gdb_printf (_(" Hash table hashes: %lu\n"),
  298. m_total_count + m_expand_hash_count);
  299. gdb_printf (_(" Half hash misses: %lu\n"),
  300. m_half_hash_miss_count);
  301. gdb_printf (_(" Hash table population: "));
  302. print_percentage (occupied_buckets, m_num_buckets);
  303. gdb_printf (_(" Median hash chain length: %3d\n"),
  304. median_chain_length);
  305. gdb_printf (_(" Average hash chain length: "));
  306. if (m_num_buckets > 0)
  307. gdb_printf ("%3lu\n", m_unique_count / m_num_buckets);
  308. else
  309. /* i18n: "Average hash chain length: (not applicable)". */
  310. gdb_printf (_("(not applicable)\n"));
  311. gdb_printf (_(" Maximum hash chain length: %3d\n"),
  312. max_chain_length);
  313. gdb_printf ("\n");
  314. }
  315. int
  316. bcache::memory_used ()
  317. {
  318. if (m_total_count == 0)
  319. return 0;
  320. return obstack_memory_used (&m_cache);
  321. }
  322. } /* namespace gdb */