summary.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Trivial Database 2: human-readable summary code
  3. Copyright (C) Rusty Russell 2010
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "private.h"
  16. #include <assert.h>
  17. #include <ccan/tally/tally.h>
  18. static int count_hash(struct tdb_context *tdb,
  19. tdb_off_t hash_off, unsigned bits)
  20. {
  21. const tdb_off_t *h;
  22. unsigned int i, count = 0;
  23. h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
  24. if (!h)
  25. return -1;
  26. for (i = 0; i < (1 << bits); i++)
  27. count += (h[i] != 0);
  28. tdb_access_release(tdb, h);
  29. return count;
  30. }
  31. static bool summarize(struct tdb_context *tdb,
  32. struct tally *hashes,
  33. struct tally *ftables,
  34. struct tally *free,
  35. struct tally *keys,
  36. struct tally *data,
  37. struct tally *extra,
  38. struct tally *uncoal,
  39. struct tally *buckets,
  40. struct tally *chains)
  41. {
  42. tdb_off_t off;
  43. tdb_len_t len;
  44. tdb_len_t unc = 0;
  45. for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
  46. const union {
  47. struct tdb_used_record u;
  48. struct tdb_free_record f;
  49. struct tdb_recovery_record r;
  50. } *p;
  51. /* We might not be able to get the whole thing. */
  52. p = tdb_access_read(tdb, off, sizeof(p->f), true);
  53. if (!p)
  54. return false;
  55. if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
  56. || p->r.magic == TDB_RECOVERY_MAGIC) {
  57. if (unc) {
  58. tally_add(uncoal, unc);
  59. unc = 0;
  60. }
  61. len = sizeof(p->r) + p->r.max_len;
  62. } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
  63. len = frec_len(&p->f);
  64. tally_add(free, len);
  65. tally_add(buckets, size_to_bucket(len));
  66. len += sizeof(p->u);
  67. unc++;
  68. } else if (rec_magic(&p->u) == TDB_USED_MAGIC) {
  69. if (unc) {
  70. tally_add(uncoal, unc);
  71. unc = 0;
  72. }
  73. len = sizeof(p->u)
  74. + rec_key_length(&p->u)
  75. + rec_data_length(&p->u)
  76. + rec_extra_padding(&p->u);
  77. tally_add(keys, rec_key_length(&p->u));
  78. tally_add(data, rec_data_length(&p->u));
  79. tally_add(extra, rec_extra_padding(&p->u));
  80. } else if (rec_magic(&p->u) == TDB_HTABLE_MAGIC) {
  81. int count = count_hash(tdb,
  82. off + sizeof(p->u),
  83. TDB_SUBLEVEL_HASH_BITS);
  84. if (count == -1)
  85. return false;
  86. tally_add(hashes, count);
  87. tally_add(extra, rec_extra_padding(&p->u));
  88. len = sizeof(p->u)
  89. + rec_data_length(&p->u)
  90. + rec_extra_padding(&p->u);
  91. } else if (rec_magic(&p->u) == TDB_FTABLE_MAGIC) {
  92. len = sizeof(p->u)
  93. + rec_data_length(&p->u)
  94. + rec_extra_padding(&p->u);
  95. tally_add(ftables, rec_data_length(&p->u));
  96. tally_add(extra, rec_extra_padding(&p->u));
  97. } else if (rec_magic(&p->u) == TDB_CHAIN_MAGIC) {
  98. len = sizeof(p->u)
  99. + rec_data_length(&p->u)
  100. + rec_extra_padding(&p->u);
  101. tally_add(chains, 1);
  102. tally_add(extra, rec_extra_padding(&p->u));
  103. } else
  104. len = dead_space(tdb, off);
  105. tdb_access_release(tdb, p);
  106. }
  107. if (unc)
  108. tally_add(uncoal, unc);
  109. return true;
  110. }
  111. #define SUMMARY_FORMAT \
  112. "Size of file/data: %zu/%zu\n" \
  113. "Number of records: %zu\n" \
  114. "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
  115. "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
  116. "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
  117. "Number of free records: %zu\n" \
  118. "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
  119. "Number of uncoalesced records: %zu\n" \
  120. "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
  121. "Number of free lists: %zu\n%s" \
  122. "Toplevel hash used: %u of %u\n" \
  123. "Number of chains: %zu\n" \
  124. "Number of subhashes: %zu\n" \
  125. "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
  126. "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
  127. #define BUCKET_SUMMARY_FORMAT_A \
  128. "Free bucket %zu: total entries %zu.\n" \
  129. "Smallest/average/largest length: %zu/%zu/%zu\n%s"
  130. #define BUCKET_SUMMARY_FORMAT_B \
  131. "Free bucket %zu-%zu: total entries %zu.\n" \
  132. "Smallest/average/largest length: %zu/%zu/%zu\n%s"
  133. #define HISTO_WIDTH 70
  134. #define HISTO_HEIGHT 20
  135. char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
  136. {
  137. tdb_len_t len;
  138. struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal,
  139. *buckets, *chains;
  140. char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
  141. char *ret = NULL;
  142. hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
  143. if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
  144. return NULL;
  145. if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
  146. tdb_allrecord_unlock(tdb, F_RDLCK);
  147. return NULL;
  148. }
  149. /* Start stats off empty. */
  150. ftables = tally_new(HISTO_HEIGHT);
  151. hashes = tally_new(HISTO_HEIGHT);
  152. freet = tally_new(HISTO_HEIGHT);
  153. keys = tally_new(HISTO_HEIGHT);
  154. data = tally_new(HISTO_HEIGHT);
  155. extra = tally_new(HISTO_HEIGHT);
  156. uncoal = tally_new(HISTO_HEIGHT);
  157. buckets = tally_new(HISTO_HEIGHT);
  158. chains = tally_new(HISTO_HEIGHT);
  159. if (!ftables || !hashes || !freet || !keys || !data || !extra
  160. || !uncoal || !buckets || !chains) {
  161. tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
  162. "tdb_summary: failed to allocate tally structures");
  163. goto unlock;
  164. }
  165. if (!summarize(tdb, hashes, ftables, freet, keys, data, extra, uncoal,
  166. buckets, chains))
  167. goto unlock;
  168. if (flags & TDB_SUMMARY_HISTOGRAMS) {
  169. hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
  170. freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
  171. keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
  172. datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
  173. extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
  174. uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
  175. bucketsg = tally_histogram(buckets, HISTO_WIDTH, HISTO_HEIGHT);
  176. }
  177. /* 20 is max length of a %llu. */
  178. len = strlen(SUMMARY_FORMAT) + 33*20 + 1
  179. + (hashesg ? strlen(hashesg) : 0)
  180. + (freeg ? strlen(freeg) : 0)
  181. + (keysg ? strlen(keysg) : 0)
  182. + (datag ? strlen(datag) : 0)
  183. + (extrag ? strlen(extrag) : 0)
  184. + (uncoalg ? strlen(uncoalg) : 0)
  185. + (bucketsg ? strlen(bucketsg) : 0);
  186. ret = malloc(len);
  187. if (!ret)
  188. goto unlock;
  189. len = sprintf(ret, SUMMARY_FORMAT,
  190. (size_t)tdb->map_size,
  191. tally_num(keys) + tally_num(data),
  192. tally_num(keys),
  193. tally_min(keys), tally_mean(keys), tally_max(keys),
  194. keysg ? keysg : "",
  195. tally_min(data), tally_mean(data), tally_max(data),
  196. datag ? datag : "",
  197. tally_min(extra), tally_mean(extra), tally_max(extra),
  198. extrag ? extrag : "",
  199. tally_num(freet),
  200. tally_min(freet), tally_mean(freet), tally_max(freet),
  201. freeg ? freeg : "",
  202. tally_total(uncoal, NULL),
  203. tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
  204. uncoalg ? uncoalg : "",
  205. tally_num(buckets),
  206. bucketsg ? bucketsg : "",
  207. count_hash(tdb, offsetof(struct tdb_header, hashtable),
  208. TDB_TOPLEVEL_HASH_BITS),
  209. 1 << TDB_TOPLEVEL_HASH_BITS,
  210. tally_num(chains),
  211. tally_num(hashes),
  212. tally_min(hashes), tally_mean(hashes), tally_max(hashes),
  213. hashesg ? hashesg : "",
  214. tally_total(keys, NULL) * 100.0 / tdb->map_size,
  215. tally_total(data, NULL) * 100.0 / tdb->map_size,
  216. tally_total(extra, NULL) * 100.0 / tdb->map_size,
  217. tally_total(freet, NULL) * 100.0 / tdb->map_size,
  218. (tally_num(keys) + tally_num(freet) + tally_num(hashes))
  219. * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size,
  220. tally_num(ftables) * sizeof(struct tdb_freetable)
  221. * 100.0 / tdb->map_size,
  222. (tally_num(hashes)
  223. * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
  224. + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS)
  225. + sizeof(struct tdb_chain) * tally_num(chains))
  226. * 100.0 / tdb->map_size);
  227. unlock:
  228. free(hashesg);
  229. free(freeg);
  230. free(keysg);
  231. free(datag);
  232. free(extrag);
  233. free(uncoalg);
  234. free(bucketsg);
  235. free(hashes);
  236. free(buckets);
  237. free(freet);
  238. free(keys);
  239. free(data);
  240. free(extra);
  241. free(uncoal);
  242. free(ftables);
  243. free(chains);
  244. tdb_allrecord_unlock(tdb, F_RDLCK);
  245. tdb_unlock_expand(tdb, F_RDLCK);
  246. return ret;
  247. }