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,
53 for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
55 struct tdb_used_record u;
56 struct tdb_free_record f;
57 struct tdb_recovery_record r;
59 /* We might not be able to get the whole thing. */
60 p = tdb_access_read(tdb, off, sizeof(p->f), true);
63 if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
64 || p->r.magic == TDB_RECOVERY_MAGIC) {
66 tally_add(uncoal, unc);
69 len = sizeof(p->r) + p->r.max_len;
70 } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
71 len = frec_len(&p->f);
73 tally_add(buckets, size_to_bucket(len));
76 } else if (rec_magic(&p->u) == TDB_MAGIC) {
78 tally_add(uncoal, unc);
82 + rec_key_length(&p->u)
83 + rec_data_length(&p->u)
84 + rec_extra_padding(&p->u);
86 /* FIXME: Use different magic for hashes, flists. */
87 if (!rec_key_length(&p->u) && rec_hash(&p->u) < 3) {
88 if (rec_hash(&p->u) == 0) {
89 int count = count_hash(tdb,
91 TDB_SUBLEVEL_HASH_BITS);
94 tally_add(hashes, count);
95 } else if (rec_hash(&p->u) == 1) {
97 rec_data_length(&p->u));
98 } else if (rec_hash(&p->u) == 2) {
102 tally_add(keys, rec_key_length(&p->u));
103 tally_add(data, rec_data_length(&p->u));
105 tally_add(extra, rec_extra_padding(&p->u));
107 len = dead_space(tdb, off);
108 tdb_access_release(tdb, p);
111 tally_add(uncoal, unc);
115 #define SUMMARY_FORMAT \
116 "Size of file/data: %zu/%zu\n" \
117 "Number of records: %zu\n" \
118 "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
119 "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
120 "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
121 "Number of free records: %zu\n" \
122 "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
123 "Number of uncoalesced records: %zu\n" \
124 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
125 "Number of free lists: %zu\n%s" \
126 "Toplevel hash used: %u of %u\n" \
127 "Number of chains: %zu\n" \
128 "Number of subhashes: %zu\n" \
129 "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
130 "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
132 #define BUCKET_SUMMARY_FORMAT_A \
133 "Free bucket %zu: total entries %zu.\n" \
134 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
135 #define BUCKET_SUMMARY_FORMAT_B \
136 "Free bucket %zu-%zu: total entries %zu.\n" \
137 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
139 #define HISTO_WIDTH 70
140 #define HISTO_HEIGHT 20
142 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
145 struct tally *flists, *hashes, *freet, *keys, *data, *extra, *uncoal,
147 char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
150 hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
152 if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
155 if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
156 tdb_allrecord_unlock(tdb, F_RDLCK);
160 /* Start stats off empty. */
161 flists = tally_new(HISTO_HEIGHT);
162 hashes = tally_new(HISTO_HEIGHT);
163 freet = tally_new(HISTO_HEIGHT);
164 keys = tally_new(HISTO_HEIGHT);
165 data = tally_new(HISTO_HEIGHT);
166 extra = tally_new(HISTO_HEIGHT);
167 uncoal = tally_new(HISTO_HEIGHT);
168 buckets = tally_new(HISTO_HEIGHT);
169 chains = tally_new(HISTO_HEIGHT);
170 if (!flists || !hashes || !freet || !keys || !data || !extra
171 || !uncoal || !buckets || !chains) {
172 tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
173 "tdb_summary: failed to allocate tally structures");
177 if (!summarize(tdb, hashes, flists, freet, keys, data, extra, uncoal,
181 if (flags & TDB_SUMMARY_HISTOGRAMS) {
182 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
183 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
184 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
185 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
186 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
187 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
188 bucketsg = tally_histogram(buckets, HISTO_WIDTH, HISTO_HEIGHT);
191 /* 20 is max length of a %llu. */
192 len = strlen(SUMMARY_FORMAT) + 33*20 + 1
193 + (hashesg ? strlen(hashesg) : 0)
194 + (freeg ? strlen(freeg) : 0)
195 + (keysg ? strlen(keysg) : 0)
196 + (datag ? strlen(datag) : 0)
197 + (extrag ? strlen(extrag) : 0)
198 + (uncoalg ? strlen(uncoalg) : 0)
199 + (bucketsg ? strlen(bucketsg) : 0);
205 len = sprintf(ret, SUMMARY_FORMAT,
206 (size_t)tdb->map_size,
207 tally_num(keys) + tally_num(data),
209 tally_min(keys), tally_mean(keys), tally_max(keys),
211 tally_min(data), tally_mean(data), tally_max(data),
213 tally_min(extra), tally_mean(extra), tally_max(extra),
214 extrag ? extrag : "",
216 tally_min(freet), tally_mean(freet), tally_max(freet),
218 tally_total(uncoal, NULL),
219 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
220 uncoalg ? uncoalg : "",
222 bucketsg ? bucketsg : "",
223 count_hash(tdb, offsetof(struct tdb_header, hashtable),
224 TDB_TOPLEVEL_HASH_BITS),
225 1 << TDB_TOPLEVEL_HASH_BITS,
228 tally_min(hashes), tally_mean(hashes), tally_max(hashes),
229 hashesg ? hashesg : "",
230 tally_total(keys, NULL) * 100.0 / tdb->map_size,
231 tally_total(data, NULL) * 100.0 / tdb->map_size,
232 tally_total(extra, NULL) * 100.0 / tdb->map_size,
233 tally_total(freet, NULL) * 100.0 / tdb->map_size,
234 (tally_num(keys) + tally_num(freet) + tally_num(hashes))
235 * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size,
236 tally_num(flists) * sizeof(struct tdb_freelist)
237 * 100.0 / tdb->map_size,
239 * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
240 + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS)
241 + sizeof(struct tdb_chain) * tally_num(chains))
242 * 100.0 / tdb->map_size);
261 tdb_allrecord_unlock(tdb, F_RDLCK);
262 tdb_unlock_expand(tdb, F_RDLCK);