tdb: introduce tdb->hdr_ofs
[Samba.git] / lib / tdb / common / summary.c
blobe9989f676f78b83834fe10cdbd9444679b67f3ac
1 /*
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: %llu/%zu\n" \
22 "Header offset/logical size: %zu/%zu\n" \
23 "Number of records: %zu\n" \
24 "Incompatible hash: %s\n" \
25 "Active/supported feature flags: 0x%08x/0x%08x\n" \
26 "Smallest/average/largest keys: %zu/%zu/%zu\n" \
27 "Smallest/average/largest data: %zu/%zu/%zu\n" \
28 "Smallest/average/largest padding: %zu/%zu/%zu\n" \
29 "Number of dead records: %zu\n" \
30 "Smallest/average/largest dead records: %zu/%zu/%zu\n" \
31 "Number of free records: %zu\n" \
32 "Smallest/average/largest free records: %zu/%zu/%zu\n" \
33 "Number of hash chains: %zu\n" \
34 "Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
35 "Number of uncoalesced records: %zu\n" \
36 "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n" \
37 "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
39 /* We don't use tally module, to keep upstream happy. */
40 struct tally {
41 size_t min, max, total;
42 size_t num;
45 static void tally_init(struct tally *tally)
47 tally->total = 0;
48 tally->num = 0;
49 tally->min = tally->max = 0;
52 static void tally_add(struct tally *tally, size_t len)
54 if (tally->num == 0)
55 tally->max = tally->min = len;
56 else if (len > tally->max)
57 tally->max = len;
58 else if (len < tally->min)
59 tally->min = len;
60 tally->num++;
61 tally->total += len;
64 static size_t tally_mean(const struct tally *tally)
66 if (!tally->num)
67 return 0;
68 return tally->total / tally->num;
71 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
73 tdb_off_t rec_ptr;
74 size_t count = 0;
76 if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
77 return 0;
79 /* keep looking until we find the right record */
80 while (rec_ptr) {
81 struct tdb_record r;
82 ++count;
83 if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
84 return 0;
85 rec_ptr = r.next;
87 return count;
90 _PUBLIC_ char *tdb_summary(struct tdb_context *tdb)
92 off_t file_size;
93 tdb_off_t off, rec_off;
94 struct tally freet, keys, data, dead, extra, hashval, uncoal;
95 struct tdb_record rec;
96 char *ret = NULL;
97 bool locked;
98 size_t unc = 0;
99 int len;
100 struct tdb_record recovery;
102 /* Read-only databases use no locking at all: it's best-effort.
103 * We may have a write lock already, so skip that case too. */
104 if (tdb->read_only || tdb->allrecord_lock.count != 0) {
105 locked = false;
106 } else {
107 if (tdb_lockall_read(tdb) == -1)
108 return NULL;
109 locked = true;
112 if (tdb_recovery_area(tdb, tdb->methods, &rec_off, &recovery) != 0) {
113 goto unlock;
116 tally_init(&freet);
117 tally_init(&keys);
118 tally_init(&data);
119 tally_init(&dead);
120 tally_init(&extra);
121 tally_init(&hashval);
122 tally_init(&uncoal);
124 for (off = TDB_DATA_START(tdb->hash_size);
125 off < tdb->map_size - 1;
126 off += sizeof(rec) + rec.rec_len) {
127 if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
128 DOCONV()) == -1)
129 goto unlock;
130 switch (rec.magic) {
131 case TDB_MAGIC:
132 tally_add(&keys, rec.key_len);
133 tally_add(&data, rec.data_len);
134 tally_add(&extra, rec.rec_len - (rec.key_len
135 + rec.data_len));
136 if (unc > 1)
137 tally_add(&uncoal, unc - 1);
138 unc = 0;
139 break;
140 case TDB_FREE_MAGIC:
141 tally_add(&freet, rec.rec_len);
142 unc++;
143 break;
144 /* If we crash after ftruncate, we can get zeroes or fill. */
145 case TDB_RECOVERY_INVALID_MAGIC:
146 case 0x42424242:
147 unc++;
148 /* If it's a valid recovery, we can trust rec_len. */
149 if (off != rec_off) {
150 rec.rec_len = tdb_dead_space(tdb, off)
151 - sizeof(rec);
153 /* Fall through */
154 case TDB_DEAD_MAGIC:
155 tally_add(&dead, rec.rec_len);
156 break;
157 default:
158 TDB_LOG((tdb, TDB_DEBUG_ERROR,
159 "Unexpected record magic 0x%x at offset %u\n",
160 rec.magic, off));
161 goto unlock;
164 if (unc > 1)
165 tally_add(&uncoal, unc - 1);
167 for (off = 0; off < tdb->hash_size; off++)
168 tally_add(&hashval, get_hash_length(tdb, off));
170 file_size = tdb->hdr_ofs + tdb->map_size;
172 len = asprintf(&ret, SUMMARY_FORMAT,
173 (unsigned long long)file_size, keys.total+data.total,
174 (size_t)tdb->hdr_ofs, (size_t)tdb->map_size,
175 keys.num,
176 (tdb->hash_fn == tdb_jenkins_hash)?"yes":"no",
177 (unsigned)tdb->feature_flags, TDB_SUPPORTED_FEATURE_FLAGS,
178 keys.min, tally_mean(&keys), keys.max,
179 data.min, tally_mean(&data), data.max,
180 extra.min, tally_mean(&extra), extra.max,
181 dead.num,
182 dead.min, tally_mean(&dead), dead.max,
183 freet.num,
184 freet.min, tally_mean(&freet), freet.max,
185 hashval.num,
186 hashval.min, tally_mean(&hashval), hashval.max,
187 uncoal.total,
188 uncoal.min, tally_mean(&uncoal), uncoal.max,
189 keys.total * 100.0 / file_size,
190 data.total * 100.0 / file_size,
191 extra.total * 100.0 / file_size,
192 freet.total * 100.0 / file_size,
193 dead.total * 100.0 / file_size,
194 (keys.num + freet.num + dead.num)
195 * (sizeof(struct tdb_record) + sizeof(uint32_t))
196 * 100.0 / file_size,
197 tdb->hash_size * sizeof(tdb_off_t)
198 * 100.0 / file_size);
199 if (len == -1) {
200 goto unlock;
203 unlock:
204 if (locked) {
205 tdb_unlockall_read(tdb);
207 return ret;