summary.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. if (unc > 1)
  93. tally_add(uncoal, unc - 1);
  94. unc = 0;
  95. break;
  96. case TDB_FREE_MAGIC:
  97. tally_add(freet, rec.rec_len);
  98. unc++;
  99. break;
  100. /* If we crash after ftruncate, we can get zeroes or fill. */
  101. case TDB_RECOVERY_INVALID_MAGIC:
  102. case 0x42424242:
  103. unc++;
  104. rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
  105. /* Fall through */
  106. case TDB_DEAD_MAGIC:
  107. tally_add(dead, rec.rec_len);
  108. break;
  109. default:
  110. TDB_LOG((tdb, TDB_DEBUG_ERROR,
  111. "Unexpected record magic 0x%x at offset %d\n",
  112. rec.magic, off));
  113. goto unlock;
  114. }
  115. }
  116. if (unc > 1)
  117. tally_add(uncoal, unc - 1);
  118. for (off = 0; off < tdb->header.hash_size; off++)
  119. tally_add(hash, get_hash_length(tdb, off));
  120. if (flags & TDB_SUMMARY_HISTOGRAMS) {
  121. freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
  122. keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
  123. datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
  124. deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT);
  125. extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
  126. hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT);
  127. uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
  128. }
  129. /* 20 is max length of a %zu. */
  130. len = strlen(SUMMARY_FORMAT) + 29*20 + 1
  131. + (freeg ? strlen(freeg) : 0)
  132. + (keysg ? strlen(keysg) : 0)
  133. + (datag ? strlen(datag) : 0)
  134. + (deadg ? strlen(deadg) : 0)
  135. + (extrag ? strlen(extrag) : 0)
  136. + (hashg ? strlen(hashg) : 0)
  137. + (uncoalg ? strlen(uncoalg) : 0);
  138. ret = malloc(len);
  139. if (!ret)
  140. goto unlock;
  141. sprintf(ret, SUMMARY_FORMAT,
  142. tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL),
  143. tally_num(keys),
  144. tally_min(keys), tally_mean(keys), tally_max(keys),
  145. keysg ? keysg : "",
  146. tally_min(data), tally_mean(data), tally_max(data),
  147. datag ? datag : "",
  148. tally_min(extra), tally_mean(extra), tally_max(extra),
  149. extrag ? extrag : "",
  150. tally_num(dead),
  151. tally_min(dead), tally_mean(dead), tally_max(dead),
  152. deadg ? deadg : "",
  153. tally_num(freet),
  154. tally_min(freet), tally_mean(freet), tally_max(freet),
  155. freeg ? freeg : "",
  156. tally_num(hash),
  157. tally_min(hash), tally_mean(hash), tally_max(hash),
  158. hashg ? hashg : "",
  159. tally_total(uncoal, NULL),
  160. tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
  161. uncoalg ? uncoalg : "",
  162. tally_total(keys, NULL) * 100.0 / tdb->map_size,
  163. tally_total(data, NULL) * 100.0 / tdb->map_size,
  164. tally_total(extra, NULL) * 100.0 / tdb->map_size,
  165. tally_total(freet, NULL) * 100.0 / tdb->map_size,
  166. tally_total(dead, NULL) * 100.0 / tdb->map_size,
  167. (tally_num(keys) + tally_num(freet) + tally_num(dead))
  168. * (sizeof(struct tdb_record) + sizeof(uint32_t))
  169. * 100.0 / tdb->map_size,
  170. tdb->header.hash_size * sizeof(tdb_off_t)
  171. * 100.0 / tdb->map_size);
  172. unlock:
  173. free(freeg);
  174. free(keysg);
  175. free(datag);
  176. free(deadg);
  177. free(extrag);
  178. free(hashg);
  179. free(uncoalg);
  180. free(freet);
  181. free(keys);
  182. free(data);
  183. free(dead);
  184. free(extra);
  185. free(hash);
  186. free(uncoal);
  187. if (locked) {
  188. tdb_unlockall_read(tdb);
  189. }
  190. return ret;
  191. }