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 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
45 if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
48 /* keep looking until we find the right record */
52 if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
59 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
62 struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal;
63 char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg;
64 struct tdb_record rec;
69 freeg = keysg = datag = deadg = extrag = hashg = uncoalg = NULL;
71 /* Read-only databases use no locking at all: it's best-effort.
72 * We may have a write lock already, so skip that case too. */
73 if (tdb->read_only || tdb->allrecord_lock.count != 0) {
76 if (tdb_lockall_read(tdb) == -1)
81 freet = tally_new(HISTO_HEIGHT);
82 keys = tally_new(HISTO_HEIGHT);
83 data = tally_new(HISTO_HEIGHT);
84 dead = tally_new(HISTO_HEIGHT);
85 extra = tally_new(HISTO_HEIGHT);
86 hash = tally_new(HISTO_HEIGHT);
87 uncoal = tally_new(HISTO_HEIGHT);
88 if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) {
89 tdb->ecode = TDB_ERR_OOM;
93 for (off = TDB_DATA_START(tdb->header.hash_size);
94 off < tdb->map_size - 1;
95 off += sizeof(rec) + rec.rec_len) {
96 if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
101 tally_add(keys, rec.key_len);
102 tally_add(data, rec.data_len);
103 tally_add(extra, rec.rec_len - (rec.key_len
106 tally_add(uncoal, unc - 1);
110 tally_add(freet, rec.rec_len);
113 /* If we crash after ftruncate, we can get zeroes or fill. */
114 case TDB_RECOVERY_INVALID_MAGIC:
117 rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
120 tally_add(dead, rec.rec_len);
123 TDB_LOG((tdb, TDB_DEBUG_ERROR,
124 "Unexpected record magic 0x%x at offset %d\n",
130 tally_add(uncoal, unc - 1);
132 for (off = 0; off < tdb->header.hash_size; off++)
133 tally_add(hash, get_hash_length(tdb, off));
135 if (flags & TDB_SUMMARY_HISTOGRAMS) {
136 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
137 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
138 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
139 deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT);
140 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
141 hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT);
142 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
145 /* 20 is max length of a %zu. */
146 len = strlen(SUMMARY_FORMAT) + 29*20 + 1
147 + (freeg ? strlen(freeg) : 0)
148 + (keysg ? strlen(keysg) : 0)
149 + (datag ? strlen(datag) : 0)
150 + (deadg ? strlen(deadg) : 0)
151 + (extrag ? strlen(extrag) : 0)
152 + (hashg ? strlen(hashg) : 0)
153 + (uncoalg ? strlen(uncoalg) : 0);
158 sprintf(ret, SUMMARY_FORMAT,
159 tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL),
161 tally_min(keys), tally_mean(keys), tally_max(keys),
163 tally_min(data), tally_mean(data), tally_max(data),
165 tally_min(extra), tally_mean(extra), tally_max(extra),
166 extrag ? extrag : "",
168 tally_min(dead), tally_mean(dead), tally_max(dead),
171 tally_min(freet), tally_mean(freet), tally_max(freet),
174 tally_min(hash), tally_mean(hash), tally_max(hash),
176 tally_total(uncoal, NULL),
177 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
178 uncoalg ? uncoalg : "",
179 tally_total(keys, NULL) * 100.0 / tdb->map_size,
180 tally_total(data, NULL) * 100.0 / tdb->map_size,
181 tally_total(extra, NULL) * 100.0 / tdb->map_size,
182 tally_total(freet, NULL) * 100.0 / tdb->map_size,
183 tally_total(dead, NULL) * 100.0 / tdb->map_size,
184 (tally_num(keys) + tally_num(freet) + tally_num(dead))
185 * (sizeof(struct tdb_record) + sizeof(uint32_t))
186 * 100.0 / tdb->map_size,
187 tdb->header.hash_size * sizeof(tdb_off_t)
188 * 100.0 / tdb->map_size);
206 tdb_unlockall_read(tdb);