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 #define SUMMARY_FORMAT \
23 "Size of file/data: %zu/%zu\n" \
24 "Number of records: %zu\n" \
25 "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
26 "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
27 "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
28 "Number of free records: %zu\n" \
29 "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
30 "Number of uncoalesced records: %zu\n" \
31 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
32 "Toplevel hash used: %u of %u\n" \
33 "Number of chains: %zu\n" \
34 "Number of subhashes: %zu\n" \
35 "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
36 "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
38 #define BUCKET_SUMMARY_FORMAT_A \
39 "Free bucket %zu: total entries %zu.\n" \
40 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
41 #define BUCKET_SUMMARY_FORMAT_B \
42 "Free bucket %zu-%zu: total entries %zu.\n" \
43 "Smallest/average/largest length: %zu/%zu/%zu\n%s"
44 #define CAPABILITY_FORMAT \
47 #define HISTO_WIDTH 70
48 #define HISTO_HEIGHT 20
50 static ntdb_off_t count_hash(struct ntdb_context *ntdb,
51 ntdb_off_t hash_off, unsigned bits)
57 h = ntdb_access_read(ntdb, hash_off, sizeof(*h) << bits, true);
58 if (NTDB_PTR_IS_ERR(h)) {
59 return NTDB_ERR_TO_OFF(NTDB_PTR_ERR(h));
61 for (i = 0; i < (1 << bits); i++)
64 ntdb_access_release(ntdb, h);
68 static enum NTDB_ERROR summarize(struct ntdb_context *ntdb,
70 struct tally *ftables,
83 for (off = sizeof(struct ntdb_header);
84 off < ntdb->file->map_size;
87 struct ntdb_used_record u;
88 struct ntdb_free_record f;
89 struct ntdb_recovery_record r;
91 /* We might not be able to get the whole thing. */
92 p = ntdb_access_read(ntdb, off, sizeof(p->f), true);
93 if (NTDB_PTR_IS_ERR(p)) {
94 return NTDB_PTR_ERR(p);
96 if (frec_magic(&p->f) != NTDB_FREE_MAGIC) {
98 tally_add(uncoal, unc);
103 if (p->r.magic == NTDB_RECOVERY_INVALID_MAGIC
104 || p->r.magic == NTDB_RECOVERY_MAGIC) {
105 len = sizeof(p->r) + p->r.max_len;
106 } else if (frec_magic(&p->f) == NTDB_FREE_MAGIC) {
107 len = frec_len(&p->f);
111 } else if (rec_magic(&p->u) == NTDB_USED_MAGIC) {
113 + rec_key_length(&p->u)
114 + rec_data_length(&p->u)
115 + rec_extra_padding(&p->u);
117 tally_add(keys, rec_key_length(&p->u));
118 tally_add(data, rec_data_length(&p->u));
119 tally_add(extra, rec_extra_padding(&p->u));
120 } else if (rec_magic(&p->u) == NTDB_HTABLE_MAGIC) {
121 ntdb_off_t count = count_hash(ntdb,
123 NTDB_SUBLEVEL_HASH_BITS);
124 if (NTDB_OFF_IS_ERR(count)) {
125 return NTDB_OFF_TO_ERR(count);
127 tally_add(hashes, count);
128 tally_add(extra, rec_extra_padding(&p->u));
130 + rec_data_length(&p->u)
131 + rec_extra_padding(&p->u);
132 } else if (rec_magic(&p->u) == NTDB_FTABLE_MAGIC) {
134 + rec_data_length(&p->u)
135 + rec_extra_padding(&p->u);
136 tally_add(ftables, rec_data_length(&p->u));
137 tally_add(extra, rec_extra_padding(&p->u));
138 } else if (rec_magic(&p->u) == NTDB_CHAIN_MAGIC) {
140 + rec_data_length(&p->u)
141 + rec_extra_padding(&p->u);
142 tally_add(chains, 1);
143 tally_add(extra, rec_extra_padding(&p->u));
144 } else if (rec_magic(&p->u) == NTDB_CAP_MAGIC) {
146 + rec_data_length(&p->u)
147 + rec_extra_padding(&p->u);
150 len = dead_space(ntdb, off);
151 if (NTDB_OFF_IS_ERR(len)) {
152 return NTDB_OFF_TO_ERR(len);
155 ntdb_access_release(ntdb, p);
158 tally_add(uncoal, unc);
162 static void add_capabilities(struct ntdb_context *ntdb, char *summary)
164 ntdb_off_t off, next;
165 const struct ntdb_capability *cap;
168 /* Append to summary. */
169 summary += strlen(summary);
171 off = ntdb_read_off(ntdb, offsetof(struct ntdb_header, capabilities));
172 if (NTDB_OFF_IS_ERR(off))
175 /* Walk capability list. */
176 for (; off; off = next) {
177 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
178 if (NTDB_PTR_IS_ERR(cap)) {
182 sprintf(summary, CAPABILITY_FORMAT,
183 cap->type & NTDB_CAP_TYPE_MASK,
184 /* Noopen? How did we get here? */
185 (cap->type & NTDB_CAP_NOOPEN) ? " (unopenable)"
186 : ((cap->type & NTDB_CAP_NOWRITE)
187 && (cap->type & NTDB_CAP_NOCHECK)) ? " (uncheckable,read-only)"
188 : (cap->type & NTDB_CAP_NOWRITE) ? " (read-only)"
189 : (cap->type & NTDB_CAP_NOCHECK) ? " (uncheckable)"
191 summary += strlen(summary);
193 ntdb_access_release(ntdb, cap);
197 _PUBLIC_ enum NTDB_ERROR ntdb_summary(struct ntdb_context *ntdb,
198 enum ntdb_summary_flags flags,
203 struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal,
205 char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg;
206 enum NTDB_ERROR ecode;
208 hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
210 ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
211 if (ecode != NTDB_SUCCESS) {
212 return ntdb->last_error = ecode;
215 ecode = ntdb_lock_expand(ntdb, F_RDLCK);
216 if (ecode != NTDB_SUCCESS) {
217 ntdb_allrecord_unlock(ntdb, F_RDLCK);
218 return ntdb->last_error = ecode;
221 /* Start stats off empty. */
222 ftables = tally_new(HISTO_HEIGHT);
223 hashes = tally_new(HISTO_HEIGHT);
224 freet = tally_new(HISTO_HEIGHT);
225 keys = tally_new(HISTO_HEIGHT);
226 data = tally_new(HISTO_HEIGHT);
227 extra = tally_new(HISTO_HEIGHT);
228 uncoal = tally_new(HISTO_HEIGHT);
229 chains = tally_new(HISTO_HEIGHT);
230 if (!ftables || !hashes || !freet || !keys || !data || !extra
231 || !uncoal || !chains) {
232 ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
233 "ntdb_summary: failed to allocate"
234 " tally structures");
238 ecode = summarize(ntdb, hashes, ftables, freet, keys, data, extra,
239 uncoal, chains, &num_caps);
240 if (ecode != NTDB_SUCCESS) {
244 if (flags & NTDB_SUMMARY_HISTOGRAMS) {
245 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
246 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
247 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
248 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
249 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
250 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
253 /* 20 is max length of a %llu. */
254 len = strlen(SUMMARY_FORMAT) + 33*20 + 1
255 + (hashesg ? strlen(hashesg) : 0)
256 + (freeg ? strlen(freeg) : 0)
257 + (keysg ? strlen(keysg) : 0)
258 + (datag ? strlen(datag) : 0)
259 + (extrag ? strlen(extrag) : 0)
260 + (uncoalg ? strlen(uncoalg) : 0)
261 + num_caps * (strlen(CAPABILITY_FORMAT) + 20
262 + strlen(" (uncheckable,read-only)"));
264 *summary = malloc(len);
266 ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
267 "ntdb_summary: failed to allocate string");
271 sprintf(*summary, SUMMARY_FORMAT,
272 (size_t)ntdb->file->map_size,
273 tally_total(keys, NULL) + tally_total(data, NULL),
275 tally_min(keys), tally_mean(keys), tally_max(keys),
277 tally_min(data), tally_mean(data), tally_max(data),
279 tally_min(extra), tally_mean(extra), tally_max(extra),
280 extrag ? extrag : "",
282 tally_min(freet), tally_mean(freet), tally_max(freet),
284 tally_total(uncoal, NULL),
285 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
286 uncoalg ? uncoalg : "",
287 (unsigned)count_hash(ntdb, offsetof(struct ntdb_header,
289 NTDB_TOPLEVEL_HASH_BITS),
290 1 << NTDB_TOPLEVEL_HASH_BITS,
293 tally_min(hashes), tally_mean(hashes), tally_max(hashes),
294 hashesg ? hashesg : "",
295 tally_total(keys, NULL) * 100.0 / ntdb->file->map_size,
296 tally_total(data, NULL) * 100.0 / ntdb->file->map_size,
297 tally_total(extra, NULL) * 100.0 / ntdb->file->map_size,
298 tally_total(freet, NULL) * 100.0 / ntdb->file->map_size,
299 (tally_num(keys) + tally_num(freet) + tally_num(hashes))
300 * sizeof(struct ntdb_used_record) * 100.0 / ntdb->file->map_size,
301 tally_num(ftables) * sizeof(struct ntdb_freetable)
302 * 100.0 / ntdb->file->map_size,
304 * (sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS)
305 + (sizeof(ntdb_off_t) << NTDB_TOPLEVEL_HASH_BITS)
306 + sizeof(struct ntdb_chain) * tally_num(chains))
307 * 100.0 / ntdb->file->map_size);
309 add_capabilities(ntdb, *summary);
327 ntdb_allrecord_unlock(ntdb, F_RDLCK);
328 ntdb_unlock_expand(ntdb, F_RDLCK);
329 return ntdb->last_error = ecode;