2 Trivial Database 2: 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/>.
20 #include <ccan/tally/tally.h>
22 static int count_hash(struct tdb_context *tdb,
23 tdb_off_t hash_off, unsigned bits)
26 unsigned int i, count = 0;
28 h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
31 for (i = 0; i < (1 << bits); i++)
34 tdb_access_release(tdb, h);
38 static bool summarize(struct tdb_context *tdb,
46 struct tally *buckets)
52 for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
54 struct tdb_used_record u;
55 struct tdb_free_record f;
56 struct tdb_recovery_record r;
58 /* We might not be able to get the whole thing. */
59 p = tdb_access_read(tdb, off, sizeof(p->f), true);
62 if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
63 || p->r.magic == TDB_RECOVERY_MAGIC) {
65 tally_add(uncoal, unc);
68 len = sizeof(p->r) + p->r.max_len;
69 } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
70 len = frec_len(&p->f);
72 tally_add(buckets, size_to_bucket(len));
75 } else if (rec_magic(&p->u) == TDB_MAGIC) {
77 tally_add(uncoal, unc);
81 + rec_key_length(&p->u)
82 + rec_data_length(&p->u)
83 + rec_extra_padding(&p->u);
85 /* FIXME: Use different magic for hashes, flists. */
86 if (!rec_key_length(&p->u) && rec_hash(&p->u) < 2) {
87 if (rec_hash(&p->u) == 0) {
88 int count = count_hash(tdb,
90 TDB_SUBLEVEL_HASH_BITS);
93 tally_add(hashes, count);
96 rec_data_length(&p->u));
99 tally_add(keys, rec_key_length(&p->u));
100 tally_add(data, rec_data_length(&p->u));
102 tally_add(extra, rec_extra_padding(&p->u));
104 len = dead_space(tdb, off);
105 tdb_access_release(tdb, p);
108 tally_add(uncoal, unc);
112 #define SUMMARY_FORMAT \
113 "Size of file/data: %zu/%zu\n" \
114 "Number of records: %zu\n" \
115 "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
116 "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
117 "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
118 "Number of free records: %zu\n" \
119 "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
120 "Number of uncoalesced records: %zu\n" \
121 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
122 "Number of free lists: %zu\n%s" \
123 "Toplevel hash used: %u of %u\n" \
124 "Number of subhashes: %zu\n" \
125 "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
126 "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
128 #define BUCKET_SUMMARY_FORMAT_A \
129 "Free bucket %zu: total entries %zu.\n" \
130 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
131 #define BUCKET_SUMMARY_FORMAT_B \
132 "Free bucket %zu-%zu: total entries %zu.\n" \
133 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
135 #define HISTO_WIDTH 70
136 #define HISTO_HEIGHT 20
138 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
141 struct tally *flists, *hashes, *freet, *keys, *data, *extra, *uncoal,
143 char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
146 hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
148 if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
151 if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
152 tdb_allrecord_unlock(tdb, F_RDLCK);
156 /* Start stats off empty. */
157 flists = tally_new(HISTO_HEIGHT);
158 hashes = tally_new(HISTO_HEIGHT);
159 freet = tally_new(HISTO_HEIGHT);
160 keys = tally_new(HISTO_HEIGHT);
161 data = tally_new(HISTO_HEIGHT);
162 extra = tally_new(HISTO_HEIGHT);
163 uncoal = tally_new(HISTO_HEIGHT);
164 buckets = tally_new(HISTO_HEIGHT);
165 if (!flists || !hashes || !freet || !keys || !data || !extra
166 || !uncoal || !buckets) {
167 tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
168 "tdb_summary: failed to allocate tally structures");
172 if (!summarize(tdb, hashes, flists, freet, keys, data, extra, uncoal,
176 if (flags & TDB_SUMMARY_HISTOGRAMS) {
177 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
178 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
179 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
180 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
181 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
182 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
183 bucketsg = tally_histogram(buckets, HISTO_WIDTH, HISTO_HEIGHT);
186 /* 20 is max length of a %llu. */
187 len = strlen(SUMMARY_FORMAT) + 33*20 + 1
188 + (hashesg ? strlen(hashesg) : 0)
189 + (freeg ? strlen(freeg) : 0)
190 + (keysg ? strlen(keysg) : 0)
191 + (datag ? strlen(datag) : 0)
192 + (extrag ? strlen(extrag) : 0)
193 + (uncoalg ? strlen(uncoalg) : 0)
194 + (bucketsg ? strlen(bucketsg) : 0);
200 len = sprintf(ret, SUMMARY_FORMAT,
201 (size_t)tdb->map_size,
202 tally_num(keys) + tally_num(data),
204 tally_min(keys), tally_mean(keys), tally_max(keys),
206 tally_min(data), tally_mean(data), tally_max(data),
208 tally_min(extra), tally_mean(extra), tally_max(extra),
209 extrag ? extrag : "",
211 tally_min(freet), tally_mean(freet), tally_max(freet),
213 tally_total(uncoal, NULL),
214 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
215 uncoalg ? uncoalg : "",
217 bucketsg ? bucketsg : "",
218 count_hash(tdb, offsetof(struct tdb_header, hashtable),
219 TDB_TOPLEVEL_HASH_BITS),
220 1 << TDB_TOPLEVEL_HASH_BITS,
222 tally_min(hashes), tally_mean(hashes), tally_max(hashes),
223 hashesg ? hashesg : "",
224 tally_total(keys, NULL) * 100.0 / tdb->map_size,
225 tally_total(data, NULL) * 100.0 / tdb->map_size,
226 tally_total(extra, NULL) * 100.0 / tdb->map_size,
227 tally_total(freet, NULL) * 100.0 / tdb->map_size,
228 (tally_num(keys) + tally_num(freet) + tally_num(hashes))
229 * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size,
230 tally_num(flists) * sizeof(struct tdb_freelist)
231 * 100.0 / tdb->map_size,
233 * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
234 + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS))
235 * 100.0 / tdb->map_size);
253 tdb_allrecord_unlock(tdb, F_RDLCK);
254 tdb_unlock_expand(tdb, F_RDLCK);