summary.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 tdb_off_t count_hash(struct tdb_context *tdb,
  19. tdb_off_t hash_off, unsigned bits)
  20. {
  21. const tdb_off_t *h;
  22. tdb_off_t count = 0;
  23. unsigned int i;
  24. h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
  25. if (TDB_PTR_IS_ERR(h)) {
  26. return TDB_PTR_ERR(h);
  27. }
  28. for (i = 0; i < (1 << bits); i++)
  29. count += (h[i] != 0);
  30. tdb_access_release(tdb, h);
  31. return count;
  32. }
  33. static enum TDB_ERROR summarize(struct tdb_context *tdb,
  34. struct tally *hashes,
  35. struct tally *ftables,
  36. struct tally *fr,
  37. struct tally *keys,
  38. struct tally *data,
  39. struct tally *extra,
  40. struct tally *uncoal,
  41. struct tally *chains)
  42. {
  43. tdb_off_t off;
  44. tdb_len_t len;
  45. tdb_len_t unc = 0;
  46. for (off = sizeof(struct tdb_header);
  47. off < tdb->file->map_size;
  48. off += len) {
  49. const union {
  50. struct tdb_used_record u;
  51. struct tdb_free_record f;
  52. struct tdb_recovery_record r;
  53. } *p;
  54. /* We might not be able to get the whole thing. */
  55. p = tdb_access_read(tdb, off, sizeof(p->f), true);
  56. if (TDB_PTR_IS_ERR(p)) {
  57. return TDB_PTR_ERR(p);
  58. }
  59. if (frec_magic(&p->f) != TDB_FREE_MAGIC) {
  60. if (unc > 1) {
  61. tally_add(uncoal, unc);
  62. unc = 0;
  63. }
  64. }
  65. if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
  66. || p->r.magic == TDB_RECOVERY_MAGIC) {
  67. len = sizeof(p->r) + p->r.max_len;
  68. } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
  69. len = frec_len(&p->f);
  70. tally_add(fr, len);
  71. len += sizeof(p->u);
  72. unc++;
  73. } else if (rec_magic(&p->u) == TDB_USED_MAGIC) {
  74. len = sizeof(p->u)
  75. + rec_key_length(&p->u)
  76. + rec_data_length(&p->u)
  77. + rec_extra_padding(&p->u);
  78. tally_add(keys, rec_key_length(&p->u));
  79. tally_add(data, rec_data_length(&p->u));
  80. tally_add(extra, rec_extra_padding(&p->u));
  81. } else if (rec_magic(&p->u) == TDB_HTABLE_MAGIC) {
  82. tdb_off_t count = count_hash(tdb,
  83. off + sizeof(p->u),
  84. TDB_SUBLEVEL_HASH_BITS);
  85. if (TDB_OFF_IS_ERR(count)) {
  86. return count;
  87. }
  88. tally_add(hashes, count);
  89. tally_add(extra, rec_extra_padding(&p->u));
  90. len = sizeof(p->u)
  91. + rec_data_length(&p->u)
  92. + rec_extra_padding(&p->u);
  93. } else if (rec_magic(&p->u) == TDB_FTABLE_MAGIC) {
  94. len = sizeof(p->u)
  95. + rec_data_length(&p->u)
  96. + rec_extra_padding(&p->u);
  97. tally_add(ftables, rec_data_length(&p->u));
  98. tally_add(extra, rec_extra_padding(&p->u));
  99. } else if (rec_magic(&p->u) == TDB_CHAIN_MAGIC) {
  100. len = sizeof(p->u)
  101. + rec_data_length(&p->u)
  102. + rec_extra_padding(&p->u);
  103. tally_add(chains, 1);
  104. tally_add(extra, rec_extra_padding(&p->u));
  105. } else {
  106. len = dead_space(tdb, off);
  107. if (TDB_OFF_IS_ERR(len)) {
  108. return len;
  109. }
  110. }
  111. tdb_access_release(tdb, p);
  112. }
  113. if (unc)
  114. tally_add(uncoal, unc);
  115. return TDB_SUCCESS;
  116. }
  117. #define SUMMARY_FORMAT \
  118. "Size of file/data: %zu/%zu\n" \
  119. "Number of records: %zu\n" \
  120. "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
  121. "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
  122. "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
  123. "Number of free records: %zu\n" \
  124. "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
  125. "Number of uncoalesced records: %zu\n" \
  126. "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
  127. "Toplevel hash used: %u of %u\n" \
  128. "Number of chains: %zu\n" \
  129. "Number of subhashes: %zu\n" \
  130. "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
  131. "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
  132. #define BUCKET_SUMMARY_FORMAT_A \
  133. "Free bucket %zu: total entries %zu.\n" \
  134. "Smallest/average/largest length: %zu/%zu/%zu\n%s"
  135. #define BUCKET_SUMMARY_FORMAT_B \
  136. "Free bucket %zu-%zu: total entries %zu.\n" \
  137. "Smallest/average/largest length: %zu/%zu/%zu\n%s"
  138. #define HISTO_WIDTH 70
  139. #define HISTO_HEIGHT 20
  140. enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
  141. enum tdb_summary_flags flags,
  142. char **summary)
  143. {
  144. tdb_len_t len;
  145. struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal,
  146. *chains;
  147. char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg;
  148. enum TDB_ERROR ecode;
  149. if (tdb->flags & TDB_VERSION1) {
  150. /* tdb1 doesn't do graphs. */
  151. *summary = tdb1_summary(tdb);
  152. if (!*summary)
  153. return tdb->last_error;
  154. return TDB_SUCCESS;
  155. }
  156. hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
  157. ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
  158. if (ecode != TDB_SUCCESS) {
  159. return tdb->last_error = ecode;
  160. }
  161. ecode = tdb_lock_expand(tdb, F_RDLCK);
  162. if (ecode != TDB_SUCCESS) {
  163. tdb_allrecord_unlock(tdb, F_RDLCK);
  164. return tdb->last_error = ecode;
  165. }
  166. /* Start stats off empty. */
  167. ftables = tally_new(HISTO_HEIGHT);
  168. hashes = tally_new(HISTO_HEIGHT);
  169. freet = tally_new(HISTO_HEIGHT);
  170. keys = tally_new(HISTO_HEIGHT);
  171. data = tally_new(HISTO_HEIGHT);
  172. extra = tally_new(HISTO_HEIGHT);
  173. uncoal = tally_new(HISTO_HEIGHT);
  174. chains = tally_new(HISTO_HEIGHT);
  175. if (!ftables || !hashes || !freet || !keys || !data || !extra
  176. || !uncoal || !chains) {
  177. ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
  178. "tdb_summary: failed to allocate"
  179. " tally structures");
  180. goto unlock;
  181. }
  182. ecode = summarize(tdb, hashes, ftables, freet, keys, data, extra,
  183. uncoal, chains);
  184. if (ecode != TDB_SUCCESS) {
  185. goto unlock;
  186. }
  187. if (flags & TDB_SUMMARY_HISTOGRAMS) {
  188. hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
  189. freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
  190. keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
  191. datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
  192. extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
  193. uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
  194. }
  195. /* 20 is max length of a %llu. */
  196. len = strlen(SUMMARY_FORMAT) + 33*20 + 1
  197. + (hashesg ? strlen(hashesg) : 0)
  198. + (freeg ? strlen(freeg) : 0)
  199. + (keysg ? strlen(keysg) : 0)
  200. + (datag ? strlen(datag) : 0)
  201. + (extrag ? strlen(extrag) : 0)
  202. + (uncoalg ? strlen(uncoalg) : 0);
  203. *summary = malloc(len);
  204. if (!*summary) {
  205. ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
  206. "tdb_summary: failed to allocate string");
  207. goto unlock;
  208. }
  209. sprintf(*summary, SUMMARY_FORMAT,
  210. (size_t)tdb->file->map_size,
  211. tally_total(keys, NULL) + tally_total(data, NULL),
  212. tally_num(keys),
  213. tally_min(keys), tally_mean(keys), tally_max(keys),
  214. keysg ? keysg : "",
  215. tally_min(data), tally_mean(data), tally_max(data),
  216. datag ? datag : "",
  217. tally_min(extra), tally_mean(extra), tally_max(extra),
  218. extrag ? extrag : "",
  219. tally_num(freet),
  220. tally_min(freet), tally_mean(freet), tally_max(freet),
  221. freeg ? freeg : "",
  222. tally_total(uncoal, NULL),
  223. tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
  224. uncoalg ? uncoalg : "",
  225. (unsigned)count_hash(tdb, offsetof(struct tdb_header,
  226. hashtable),
  227. TDB_TOPLEVEL_HASH_BITS),
  228. 1 << TDB_TOPLEVEL_HASH_BITS,
  229. tally_num(chains),
  230. tally_num(hashes),
  231. tally_min(hashes), tally_mean(hashes), tally_max(hashes),
  232. hashesg ? hashesg : "",
  233. tally_total(keys, NULL) * 100.0 / tdb->file->map_size,
  234. tally_total(data, NULL) * 100.0 / tdb->file->map_size,
  235. tally_total(extra, NULL) * 100.0 / tdb->file->map_size,
  236. tally_total(freet, NULL) * 100.0 / tdb->file->map_size,
  237. (tally_num(keys) + tally_num(freet) + tally_num(hashes))
  238. * sizeof(struct tdb_used_record) * 100.0 / tdb->file->map_size,
  239. tally_num(ftables) * sizeof(struct tdb_freetable)
  240. * 100.0 / tdb->file->map_size,
  241. (tally_num(hashes)
  242. * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
  243. + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS)
  244. + sizeof(struct tdb_chain) * tally_num(chains))
  245. * 100.0 / tdb->file->map_size);
  246. unlock:
  247. free(hashesg);
  248. free(freeg);
  249. free(keysg);
  250. free(datag);
  251. free(extrag);
  252. free(uncoalg);
  253. free(hashes);
  254. free(freet);
  255. free(keys);
  256. free(data);
  257. free(extra);
  258. free(uncoal);
  259. free(ftables);
  260. free(chains);
  261. tdb_allrecord_unlock(tdb, F_RDLCK);
  262. tdb_unlock_expand(tdb, F_RDLCK);
  263. return tdb->last_error = ecode;
  264. }