2 Trivial Database: human-readable summary code
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 #include "tdb_private.h"
19 #include <ccan/tally/tally.h>
21 #define SUMMARY_FORMAT \
22 "Size of file/data: %u/%zu\n" \
23 "Number of records: %zu\n" \
24 "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
25 "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
26 "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
27 "Number of dead records: %zu\n" \
28 "Smallest/average/largest dead records: %zu/%zu/%zu\n%s" \
29 "Number of free records: %zu\n" \
30 "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
31 "Number of hash chains: %zu\n" \
32 "Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \
33 "Number of uncoalesced records: %zu\n" \
34 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
35 "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
37 #define HISTO_WIDTH 70
38 #define HISTO_HEIGHT 20
40 /* Slow, but should be very rare. */
41 static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
45 for (len = 0; off + len < tdb->map_size; len++) {
47 if (tdb->methods->tdb_read(tdb, off, &c, 1, 0))
49 if (c != 0 && c != 0x42)
55 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
60 if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
63 /* keep looking until we find the right record */
67 if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
74 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
77 struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal;
78 char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg;
79 struct tdb_record rec;
84 freeg = keysg = datag = deadg = extrag = hashg = uncoalg = NULL;
86 /* Read-only databases use no locking at all: it's best-effort.
87 * We may have a write lock already, so skip that case too. */
88 if (tdb->read_only || tdb->allrecord_lock.count != 0) {
91 if (tdb_lockall_read(tdb) == -1)
96 freet = tally_new(HISTO_HEIGHT);
97 keys = tally_new(HISTO_HEIGHT);
98 data = tally_new(HISTO_HEIGHT);
99 dead = tally_new(HISTO_HEIGHT);
100 extra = tally_new(HISTO_HEIGHT);
101 hash = tally_new(HISTO_HEIGHT);
102 uncoal = tally_new(HISTO_HEIGHT);
103 if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) {
104 tdb->ecode = TDB_ERR_OOM;
108 for (off = TDB_DATA_START(tdb->header.hash_size);
109 off < tdb->map_size - 1;
110 off += sizeof(rec) + rec.rec_len) {
111 if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
116 tally_add(keys, rec.key_len);
117 tally_add(data, rec.data_len);
118 tally_add(extra, rec.rec_len - (rec.key_len
122 tally_add(freet, rec.rec_len);
125 /* If we crash after ftruncate, we can get zeroes or fill. */
126 case TDB_RECOVERY_INVALID_MAGIC:
129 rec.rec_len = dead_space(tdb, off) - sizeof(rec);
132 tally_add(dead, rec.rec_len);
135 TDB_LOG((tdb, TDB_DEBUG_ERROR,
136 "Unexpected record magic 0x%x at offset %d\n",
142 (rec.magic == TDB_MAGIC || rec.magic == TDB_DEAD_MAGIC)) {
143 tally_add(uncoal, unc);
148 tally_add(uncoal, unc);
150 for (off = 0; off < tdb->header.hash_size; off++)
151 tally_add(hash, get_hash_length(tdb, off));
153 if (flags & TDB_SUMMARY_HISTOGRAMS) {
154 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
155 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
156 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
157 deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT);
158 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
159 hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT);
160 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
163 /* 20 is max length of a %zu. */
164 len = strlen(SUMMARY_FORMAT) + 29*20 + 1
165 + (freeg ? strlen(freeg) : 0)
166 + (keysg ? strlen(keysg) : 0)
167 + (datag ? strlen(datag) : 0)
168 + (deadg ? strlen(deadg) : 0)
169 + (extrag ? strlen(extrag) : 0)
170 + (hashg ? strlen(hashg) : 0)
171 + (uncoalg ? strlen(uncoalg) : 0);
176 sprintf(ret, SUMMARY_FORMAT,
177 tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL),
179 tally_min(keys), tally_mean(keys), tally_max(keys),
181 tally_min(data), tally_mean(data), tally_max(data),
183 tally_min(extra), tally_mean(extra), tally_max(extra),
184 extrag ? extrag : "",
186 tally_min(dead), tally_mean(dead), tally_max(dead),
189 tally_min(freet), tally_mean(freet), tally_max(freet),
192 tally_min(hash), tally_mean(hash), tally_max(hash),
194 tally_total(uncoal, NULL),
195 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
196 uncoalg ? uncoalg : "",
197 tally_total(keys, NULL) * 100.0 / tdb->map_size,
198 tally_total(data, NULL) * 100.0 / tdb->map_size,
199 tally_total(extra, NULL) * 100.0 / tdb->map_size,
200 tally_total(freet, NULL) * 100.0 / tdb->map_size,
201 tally_total(dead, NULL) * 100.0 / tdb->map_size,
202 (tally_num(keys) + tally_num(freet) + tally_num(dead))
203 * (sizeof(struct tdb_record) + sizeof(uint32_t))
204 * 100.0 / tdb->map_size,
205 tdb->header.hash_size * sizeof(tdb_off_t)
206 * 100.0 / tdb->map_size);
224 tdb_unlockall_read(tdb);