summary.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Trivial Database: 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 "tdb_private.h"
  16. #include <ccan/tally/tally.h>
  17. #define SUMMARY_FORMAT \
  18. "Size of file/data: %u/%zu\n" \
  19. "Number of records: %zu\n" \
  20. "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
  21. "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
  22. "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
  23. "Number of dead records: %zu\n" \
  24. "Smallest/average/largest dead records: %zu/%zu/%zu\n%s" \
  25. "Number of free records: %zu\n" \
  26. "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
  27. "Number of hash chains: %zu\n" \
  28. "Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \
  29. "Number of uncoalesced records: %zu\n" \
  30. "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
  31. "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
  32. #define HISTO_WIDTH 70
  33. #define HISTO_HEIGHT 20
  34. static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
  35. {
  36. tdb_off_t rec_ptr;
  37. size_t count = 0;
  38. if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
  39. return 0;
  40. /* keep looking until we find the right record */
  41. while (rec_ptr) {
  42. struct tdb_record r;
  43. ++count;
  44. if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
  45. return 0;
  46. rec_ptr = r.next;
  47. }
  48. return count;
  49. }
  50. char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
  51. {
  52. tdb_off_t off;
  53. struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal;
  54. char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg;
  55. struct tdb_record rec;
  56. char *ret = NULL;
  57. bool locked;
  58. size_t len, unc = 0;
  59. freeg = keysg = datag = deadg = extrag = hashg = uncoalg = NULL;
  60. /* Read-only databases use no locking at all: it's best-effort.
  61. * We may have a write lock already, so skip that case too. */
  62. if (tdb->read_only || tdb->allrecord_lock.count != 0) {
  63. locked = false;
  64. } else {
  65. if (tdb_lockall_read(tdb) == -1)
  66. return NULL;
  67. locked = true;
  68. }
  69. freet = tally_new(HISTO_HEIGHT);
  70. keys = tally_new(HISTO_HEIGHT);
  71. data = tally_new(HISTO_HEIGHT);
  72. dead = tally_new(HISTO_HEIGHT);
  73. extra = tally_new(HISTO_HEIGHT);
  74. hash = tally_new(HISTO_HEIGHT);
  75. uncoal = tally_new(HISTO_HEIGHT);
  76. if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) {
  77. tdb->ecode = TDB_ERR_OOM;
  78. goto unlock;
  79. }
  80. for (off = TDB_DATA_START(tdb->header.hash_size);
  81. off < tdb->map_size - 1;
  82. off += sizeof(rec) + rec.rec_len) {
  83. if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
  84. DOCONV()) == -1)
  85. goto unlock;
  86. switch (rec.magic) {
  87. case TDB_MAGIC:
  88. tally_add(keys, rec.key_len);
  89. tally_add(data, rec.data_len);
  90. tally_add(extra, rec.rec_len - (rec.key_len
  91. + rec.data_len));
  92. break;
  93. case TDB_FREE_MAGIC:
  94. tally_add(freet, rec.rec_len);
  95. unc++;
  96. break;
  97. /* If we crash after ftruncate, we can get zeroes or fill. */
  98. case TDB_RECOVERY_INVALID_MAGIC:
  99. case 0x42424242:
  100. unc++;
  101. rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
  102. /* Fall through */
  103. case TDB_DEAD_MAGIC:
  104. tally_add(dead, rec.rec_len);
  105. break;
  106. default:
  107. TDB_LOG((tdb, TDB_DEBUG_ERROR,
  108. "Unexpected record magic 0x%x at offset %d\n",
  109. rec.magic, off));
  110. goto unlock;
  111. }
  112. if (unc &&
  113. (rec.magic == TDB_MAGIC || rec.magic == TDB_DEAD_MAGIC)) {
  114. tally_add(uncoal, unc);
  115. unc = 0;
  116. }
  117. }
  118. if (unc)
  119. tally_add(uncoal, unc);
  120. for (off = 0; off < tdb->header.hash_size; off++)
  121. tally_add(hash, get_hash_length(tdb, off));
  122. if (flags & TDB_SUMMARY_HISTOGRAMS) {
  123. freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
  124. keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
  125. datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
  126. deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT);
  127. extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
  128. hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT);
  129. uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
  130. }
  131. /* 20 is max length of a %zu. */
  132. len = strlen(SUMMARY_FORMAT) + 29*20 + 1
  133. + (freeg ? strlen(freeg) : 0)
  134. + (keysg ? strlen(keysg) : 0)
  135. + (datag ? strlen(datag) : 0)
  136. + (deadg ? strlen(deadg) : 0)
  137. + (extrag ? strlen(extrag) : 0)
  138. + (hashg ? strlen(hashg) : 0)
  139. + (uncoalg ? strlen(uncoalg) : 0);
  140. ret = malloc(len);
  141. if (!ret)
  142. goto unlock;
  143. sprintf(ret, SUMMARY_FORMAT,
  144. tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL),
  145. tally_num(keys),
  146. tally_min(keys), tally_mean(keys), tally_max(keys),
  147. keysg ? keysg : "",
  148. tally_min(data), tally_mean(data), tally_max(data),
  149. datag ? datag : "",
  150. tally_min(extra), tally_mean(extra), tally_max(extra),
  151. extrag ? extrag : "",
  152. tally_num(dead),
  153. tally_min(dead), tally_mean(dead), tally_max(dead),
  154. deadg ? deadg : "",
  155. tally_num(freet),
  156. tally_min(freet), tally_mean(freet), tally_max(freet),
  157. freeg ? freeg : "",
  158. tally_num(hash),
  159. tally_min(hash), tally_mean(hash), tally_max(hash),
  160. hashg ? hashg : "",
  161. tally_total(uncoal, NULL),
  162. tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
  163. uncoalg ? uncoalg : "",
  164. tally_total(keys, NULL) * 100.0 / tdb->map_size,
  165. tally_total(data, NULL) * 100.0 / tdb->map_size,
  166. tally_total(extra, NULL) * 100.0 / tdb->map_size,
  167. tally_total(freet, NULL) * 100.0 / tdb->map_size,
  168. tally_total(dead, NULL) * 100.0 / tdb->map_size,
  169. (tally_num(keys) + tally_num(freet) + tally_num(dead))
  170. * (sizeof(struct tdb_record) + sizeof(uint32_t))
  171. * 100.0 / tdb->map_size,
  172. tdb->header.hash_size * sizeof(tdb_off_t)
  173. * 100.0 / tdb->map_size);
  174. unlock:
  175. free(freeg);
  176. free(keysg);
  177. free(datag);
  178. free(deadg);
  179. free(extrag);
  180. free(hashg);
  181. free(uncoalg);
  182. free(freet);
  183. free(keys);
  184. free(data);
  185. free(dead);
  186. free(extra);
  187. free(hash);
  188. free(uncoal);
  189. if (locked) {
  190. tdb_unlockall_read(tdb);
  191. }
  192. return ret;
  193. }