tdb2: move mmap into struct tdb_file
[ccan] / ccan / tdb2 / summary.c
1  /*
2    Trivial Database 2: human-readable summary code
3    Copyright (C) Rusty Russell 2010
4
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.
9
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.
14
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/>.
17 */
18 #include "private.h"
19 #include <assert.h>
20 #include <ccan/tally/tally.h>
21
22 static tdb_off_t count_hash(struct tdb_context *tdb,
23                             tdb_off_t hash_off, unsigned bits)
24 {
25         const tdb_off_t *h;
26         tdb_off_t count = 0;
27         unsigned int i;
28
29         h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
30         if (TDB_PTR_IS_ERR(h)) {
31                 return TDB_PTR_ERR(h);
32         }
33         for (i = 0; i < (1 << bits); i++)
34                 count += (h[i] != 0);
35
36         tdb_access_release(tdb, h);
37         return count;
38 }
39
40 static enum TDB_ERROR summarize(struct tdb_context *tdb,
41                                 struct tally *hashes,
42                                 struct tally *ftables,
43                                 struct tally *fr,
44                                 struct tally *keys,
45                                 struct tally *data,
46                                 struct tally *extra,
47                                 struct tally *uncoal,
48                                 struct tally *buckets,
49                                 struct tally *chains)
50 {
51         tdb_off_t off;
52         tdb_len_t len;
53         tdb_len_t unc = 0;
54
55         for (off = sizeof(struct tdb_header);
56              off < tdb->file->map_size;
57              off += len) {
58                 const union {
59                         struct tdb_used_record u;
60                         struct tdb_free_record f;
61                         struct tdb_recovery_record r;
62                 } *p;
63                 /* We might not be able to get the whole thing. */
64                 p = tdb_access_read(tdb, off, sizeof(p->f), true);
65                 if (TDB_PTR_IS_ERR(p)) {
66                         return TDB_PTR_ERR(p);
67                 }
68                 if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
69                     || p->r.magic == TDB_RECOVERY_MAGIC) {
70                         if (unc) {
71                                 tally_add(uncoal, unc);
72                                 unc = 0;
73                         }
74                         len = sizeof(p->r) + p->r.max_len;
75                 } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
76                         len = frec_len(&p->f);
77                         tally_add(fr, len);
78                         tally_add(buckets, size_to_bucket(len));
79                         len += sizeof(p->u);
80                         unc++;
81                 } else if (rec_magic(&p->u) == TDB_USED_MAGIC) {
82                         if (unc) {
83                                 tally_add(uncoal, unc);
84                                 unc = 0;
85                         }
86                         len = sizeof(p->u)
87                                 + rec_key_length(&p->u)
88                                 + rec_data_length(&p->u)
89                                 + rec_extra_padding(&p->u);
90
91                         tally_add(keys, rec_key_length(&p->u));
92                         tally_add(data, rec_data_length(&p->u));
93                         tally_add(extra, rec_extra_padding(&p->u));
94                 } else if (rec_magic(&p->u) == TDB_HTABLE_MAGIC) {
95                         tdb_off_t count = count_hash(tdb,
96                                                      off + sizeof(p->u),
97                                                      TDB_SUBLEVEL_HASH_BITS);
98                         if (TDB_OFF_IS_ERR(count)) {
99                                 return count;
100                         }
101                         tally_add(hashes, count);
102                         tally_add(extra, rec_extra_padding(&p->u));
103                         len = sizeof(p->u)
104                                 + rec_data_length(&p->u)
105                                 + rec_extra_padding(&p->u);
106                 } else if (rec_magic(&p->u) == TDB_FTABLE_MAGIC) {
107                         len = sizeof(p->u)
108                                 + rec_data_length(&p->u)
109                                 + rec_extra_padding(&p->u);
110                         tally_add(ftables, rec_data_length(&p->u));
111                         tally_add(extra, rec_extra_padding(&p->u));
112                 } else if (rec_magic(&p->u) == TDB_CHAIN_MAGIC) {
113                         len = sizeof(p->u)
114                                 + rec_data_length(&p->u)
115                                 + rec_extra_padding(&p->u);
116                         tally_add(chains, 1);
117                         tally_add(extra, rec_extra_padding(&p->u));
118                 } else {
119                         len = dead_space(tdb, off);
120                         if (TDB_OFF_IS_ERR(len)) {
121                                 return len;
122                         }
123                 }
124                 tdb_access_release(tdb, p);
125         }
126         if (unc)
127                 tally_add(uncoal, unc);
128         return TDB_SUCCESS;
129 }
130
131 #define SUMMARY_FORMAT \
132         "Size of file/data: %zu/%zu\n" \
133         "Number of records: %zu\n" \
134         "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
135         "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
136         "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
137         "Number of free records: %zu\n" \
138         "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
139         "Number of uncoalesced records: %zu\n" \
140         "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
141         "Number of free lists: %zu\n%s" \
142         "Toplevel hash used: %u of %u\n" \
143         "Number of chains: %zu\n" \
144         "Number of subhashes: %zu\n" \
145         "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
146         "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
147
148 #define BUCKET_SUMMARY_FORMAT_A                                 \
149         "Free bucket %zu: total entries %zu.\n"                 \
150         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
151 #define BUCKET_SUMMARY_FORMAT_B                                 \
152         "Free bucket %zu-%zu: total entries %zu.\n"             \
153         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
154
155 #define HISTO_WIDTH 70
156 #define HISTO_HEIGHT 20
157
158 enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
159                            enum tdb_summary_flags flags,
160                            char **summary)
161 {
162         tdb_len_t len;
163         struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal,
164                 *buckets, *chains;
165         char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
166         enum TDB_ERROR ecode;
167
168         hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
169
170         ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
171         if (ecode != TDB_SUCCESS) {
172                 return ecode;
173         }
174
175         ecode = tdb_lock_expand(tdb, F_RDLCK);
176         if (ecode != TDB_SUCCESS) {
177                 tdb_allrecord_unlock(tdb, F_RDLCK);
178                 return ecode;
179         }
180
181         /* Start stats off empty. */
182         ftables = tally_new(HISTO_HEIGHT);
183         hashes = tally_new(HISTO_HEIGHT);
184         freet = tally_new(HISTO_HEIGHT);
185         keys = tally_new(HISTO_HEIGHT);
186         data = tally_new(HISTO_HEIGHT);
187         extra = tally_new(HISTO_HEIGHT);
188         uncoal = tally_new(HISTO_HEIGHT);
189         buckets = tally_new(HISTO_HEIGHT);
190         chains = tally_new(HISTO_HEIGHT);
191         if (!ftables || !hashes || !freet || !keys || !data || !extra
192             || !uncoal || !buckets || !chains) {
193                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
194                                    "tdb_summary: failed to allocate"
195                                    " tally structures");
196                 goto unlock;
197         }
198
199         ecode = summarize(tdb, hashes, ftables, freet, keys, data, extra,
200                           uncoal, buckets, chains);
201         if (ecode != TDB_SUCCESS) {
202                 goto unlock;
203         }
204
205         if (flags & TDB_SUMMARY_HISTOGRAMS) {
206                 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
207                 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
208                 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
209                 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
210                 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
211                 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
212                 bucketsg = tally_histogram(buckets, HISTO_WIDTH, HISTO_HEIGHT);
213         }
214
215         /* 20 is max length of a %llu. */
216         len = strlen(SUMMARY_FORMAT) + 33*20 + 1
217                 + (hashesg ? strlen(hashesg) : 0)
218                 + (freeg ? strlen(freeg) : 0)
219                 + (keysg ? strlen(keysg) : 0)
220                 + (datag ? strlen(datag) : 0)
221                 + (extrag ? strlen(extrag) : 0)
222                 + (uncoalg ? strlen(uncoalg) : 0)
223                 + (bucketsg ? strlen(bucketsg) : 0);
224
225         *summary = malloc(len);
226         if (!*summary) {
227                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
228                                    "tdb_summary: failed to allocate string");
229                 goto unlock;
230         }
231
232         sprintf(*summary, SUMMARY_FORMAT,
233                 (size_t)tdb->file->map_size,
234                 tally_num(keys) + tally_num(data),
235                 tally_num(keys),
236                 tally_min(keys), tally_mean(keys), tally_max(keys),
237                 keysg ? keysg : "",
238                 tally_min(data), tally_mean(data), tally_max(data),
239                 datag ? datag : "",
240                 tally_min(extra), tally_mean(extra), tally_max(extra),
241                 extrag ? extrag : "",
242                 tally_num(freet),
243                 tally_min(freet), tally_mean(freet), tally_max(freet),
244                 freeg ? freeg : "",
245                 tally_total(uncoal, NULL),
246                 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
247                 uncoalg ? uncoalg : "",
248                 tally_num(buckets),
249                 bucketsg ? bucketsg : "",
250                 (unsigned)count_hash(tdb, offsetof(struct tdb_header,
251                                                    hashtable),
252                                      TDB_TOPLEVEL_HASH_BITS),
253                 1 << TDB_TOPLEVEL_HASH_BITS,
254                 tally_num(chains),
255                 tally_num(hashes),
256                 tally_min(hashes), tally_mean(hashes), tally_max(hashes),
257                 hashesg ? hashesg : "",
258                 tally_total(keys, NULL) * 100.0 / tdb->file->map_size,
259                 tally_total(data, NULL) * 100.0 / tdb->file->map_size,
260                 tally_total(extra, NULL) * 100.0 / tdb->file->map_size,
261                 tally_total(freet, NULL) * 100.0 / tdb->file->map_size,
262                 (tally_num(keys) + tally_num(freet) + tally_num(hashes))
263                 * sizeof(struct tdb_used_record) * 100.0 / tdb->file->map_size,
264                 tally_num(ftables) * sizeof(struct tdb_freetable)
265                 * 100.0 / tdb->file->map_size,
266                 (tally_num(hashes)
267                  * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
268                  + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS)
269                  + sizeof(struct tdb_chain) * tally_num(chains))
270                 * 100.0 / tdb->file->map_size);
271
272 unlock:
273         free(hashesg);
274         free(freeg);
275         free(keysg);
276         free(datag);
277         free(extrag);
278         free(uncoalg);
279         free(bucketsg);
280         free(hashes);
281         free(buckets);
282         free(freet);
283         free(keys);
284         free(data);
285         free(extra);
286         free(uncoal);
287         free(ftables);
288         free(chains);
289
290         tdb_allrecord_unlock(tdb, F_RDLCK);
291         tdb_unlock_expand(tdb, F_RDLCK);
292         return ecode;
293 }