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