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"
20 #define SUMMARY_FORMAT \
21 "Size of file/data: %u/%zu\n" \
22 "Number of records: %zu\n" \
23 "Smallest/average/largest keys: %zu/%zu/%zu\n" \
24 "Smallest/average/largest data: %zu/%zu/%zu\n" \
25 "Smallest/average/largest padding: %zu/%zu/%zu\n" \
26 "Number of dead records: %zu\n" \
27 "Smallest/average/largest dead records: %zu/%zu/%zu\n" \
28 "Number of free records: %zu\n" \
29 "Smallest/average/largest free records: %zu/%zu/%zu\n" \
30 "Number of hash chains: %zu\n" \
31 "Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
32 "Number of uncoalesced records: %zu\n" \
33 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n" \
34 "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
36 /* We don't use tally module, to keep upstream happy. */
38 size_t min, max, total;
42 static void tally_init(struct tally *tally)
46 tally->min = tally->max = 0;
49 static void tally_add(struct tally *tally, size_t len)
52 tally->max = tally->min = len;
53 else if (len > tally->max)
55 else if (len < tally->min)
61 static size_t tally_mean(const struct tally *tally)
65 return tally->total / tally->num;
68 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
73 if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
76 /* keep looking until we find the right record */
80 if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
87 char *tdb_summary(struct tdb_context *tdb)
90 struct tally freet, keys, data, dead, extra, hash, uncoal;
91 struct tdb_record rec;
96 /* Read-only databases use no locking at all: it's best-effort.
97 * We may have a write lock already, so skip that case too. */
98 if (tdb->read_only || tdb->allrecord_lock.count != 0) {
101 if (tdb_lockall_read(tdb) == -1)
114 for (off = TDB_DATA_START(tdb->header.hash_size);
115 off < tdb->map_size - 1;
116 off += sizeof(rec) + rec.rec_len) {
117 if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
122 tally_add(&keys, rec.key_len);
123 tally_add(&data, rec.data_len);
124 tally_add(&extra, rec.rec_len - (rec.key_len
127 tally_add(&uncoal, unc - 1);
131 tally_add(&freet, rec.rec_len);
134 /* If we crash after ftruncate, we can get zeroes or fill. */
135 case TDB_RECOVERY_INVALID_MAGIC:
138 rec.rec_len = tdb_dead_space(tdb, off) - sizeof(rec);
141 tally_add(&dead, rec.rec_len);
144 TDB_LOG((tdb, TDB_DEBUG_ERROR,
145 "Unexpected record magic 0x%x at offset %d\n",
151 tally_add(&uncoal, unc - 1);
153 for (off = 0; off < tdb->header.hash_size; off++)
154 tally_add(&hash, get_hash_length(tdb, off));
156 /* 20 is max length of a %zu. */
157 len = strlen(SUMMARY_FORMAT) + 35*20 + 1;
162 sprintf(ret, SUMMARY_FORMAT,
163 tdb->map_size, keys.total+data.total,
165 keys.min, tally_mean(&keys), keys.max,
166 data.min, tally_mean(&data), data.max,
167 extra.min, tally_mean(&extra), extra.max,
169 dead.min, tally_mean(&dead), dead.max,
171 freet.min, tally_mean(&freet), freet.max,
173 hash.min, tally_mean(&hash), hash.max,
175 uncoal.min, tally_mean(&uncoal), uncoal.max,
176 keys.total * 100.0 / tdb->map_size,
177 data.total * 100.0 / tdb->map_size,
178 extra.total * 100.0 / tdb->map_size,
179 freet.total * 100.0 / tdb->map_size,
180 dead.total * 100.0 / tdb->map_size,
181 (keys.num + freet.num + dead.num)
182 * (sizeof(struct tdb_record) + sizeof(uint32_t))
183 * 100.0 / tdb->map_size,
184 tdb->header.hash_size * sizeof(tdb_off_t)
185 * 100.0 / tdb->map_size);
189 tdb_unlockall_read(tdb);