]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/summary.c
tdb2: use expansion heuristics from tdb1
[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 int count_hash(struct tdb_context *tdb,
23                       tdb_off_t hash_off, unsigned bits)
24 {
25         const tdb_off_t *h;
26         unsigned int i, count = 0;
27
28         h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
29         if (!h)
30                 return -1;
31         for (i = 0; i < (1 << bits); i++)
32                 count += (h[i] != 0);
33
34         tdb_access_release(tdb, h);
35         return count;
36 }
37
38 static bool summarize(struct tdb_context *tdb,
39                       struct tally *hashes,
40                       struct tally *flists,
41                       struct tally *free,
42                       struct tally *keys,
43                       struct tally *data,
44                       struct tally *extra,
45                       struct tally *uncoal,
46                       struct tally *buckets)
47 {
48         tdb_off_t off;
49         tdb_len_t len;
50         tdb_len_t unc = 0;
51
52         for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
53                 union {
54                         struct tdb_used_record u;
55                         struct tdb_free_record f;
56                         struct tdb_recovery_record r;
57                 } pad, *p;
58                 /* We might not be able to get the whole thing. */
59                 p = tdb_get(tdb, off, &pad, sizeof(p->f));
60                 if (!p)
61                         return false;
62                 if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
63                     || p->r.magic == TDB_RECOVERY_MAGIC) {
64                         len = sizeof(p->r) + p->r.max_len;
65                 } else if (rec_magic(&p->u) != TDB_MAGIC) {
66                         len = frec_len(&p->f);
67                         tally_add(free, len);
68                         tally_add(buckets, size_to_bucket(len));
69                         len += sizeof(p->u);
70                         unc++;
71                 } else if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
72                         if (unc) {
73                                 tally_add(uncoal, unc);
74                                 unc = 0;
75                         }
76                         len = sizeof(p->u)
77                                 + rec_key_length(&p->u)
78                                 + rec_data_length(&p->u)
79                                 + rec_extra_padding(&p->u);
80
81                         /* FIXME: Use different magic for hashes, flists. */
82                         if (!rec_key_length(&p->u) && rec_hash(&p->u) < 2) {
83                                 if (rec_hash(&p->u) == 0) {
84                                         int count = count_hash(tdb,
85                                                         off + sizeof(p->u),
86                                                         TDB_SUBLEVEL_HASH_BITS);
87                                         if (count == -1)
88                                                 return false;
89                                         tally_add(hashes, count);
90                                 } else {
91                                         tally_add(flists,
92                                                   rec_data_length(&p->u));
93                                 }
94                         } else {
95                                 tally_add(keys, rec_key_length(&p->u));
96                                 tally_add(data, rec_data_length(&p->u));
97                         }
98                         tally_add(extra, rec_extra_padding(&p->u));
99                 } else
100                         len = dead_space(tdb, off);
101         }
102         if (unc)
103                 tally_add(uncoal, unc);
104         return true;
105 }
106
107 #define SUMMARY_FORMAT \
108         "Size of file/data: %zu/%zu\n" \
109         "Number of records: %zu\n" \
110         "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
111         "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
112         "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
113         "Number of free records: %zu\n" \
114         "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
115         "Number of uncoalesced records: %zu\n" \
116         "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
117         "Number of free lists: %zu\n%s" \
118         "Toplevel hash used: %u of %u\n" \
119         "Number of subhashes: %zu\n" \
120         "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
121         "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
122
123 #define BUCKET_SUMMARY_FORMAT_A                                 \
124         "Free bucket %zu: total entries %zu.\n"                 \
125         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
126 #define BUCKET_SUMMARY_FORMAT_B                                 \
127         "Free bucket %zu-%zu: total entries %zu.\n"             \
128         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
129
130 #define HISTO_WIDTH 70
131 #define HISTO_HEIGHT 20
132
133 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
134 {
135         tdb_len_t len;
136         struct tally *flists, *hashes, *freet, *keys, *data, *extra, *uncoal,
137                 *buckets;
138         char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg;
139         char *ret = NULL;
140
141         hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL;
142
143         if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
144                 return NULL;
145
146         if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
147                 tdb_allrecord_unlock(tdb, F_RDLCK);
148                 return NULL;
149         }
150
151         /* Start stats off empty. */
152         flists = tally_new(HISTO_HEIGHT);
153         hashes = tally_new(HISTO_HEIGHT);
154         freet = tally_new(HISTO_HEIGHT);
155         keys = tally_new(HISTO_HEIGHT);
156         data = tally_new(HISTO_HEIGHT);
157         extra = tally_new(HISTO_HEIGHT);
158         uncoal = tally_new(HISTO_HEIGHT);
159         buckets = tally_new(HISTO_HEIGHT);
160         if (!flists || !hashes || !freet || !keys || !data || !extra
161             || !uncoal || !buckets) {
162                 tdb->ecode = TDB_ERR_OOM;
163                 goto unlock;
164         }
165
166         if (!summarize(tdb, hashes, flists, freet, keys, data, extra, uncoal,
167                        buckets))
168                 goto unlock;
169
170         if (flags & TDB_SUMMARY_HISTOGRAMS) {
171                 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
172                 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
173                 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
174                 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
175                 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
176                 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
177                 bucketsg = tally_histogram(buckets, HISTO_WIDTH, HISTO_HEIGHT);
178         }
179
180         /* 20 is max length of a %llu. */
181         len = strlen(SUMMARY_FORMAT) + 33*20 + 1
182                 + (hashesg ? strlen(hashesg) : 0)
183                 + (freeg ? strlen(freeg) : 0)
184                 + (keysg ? strlen(keysg) : 0)
185                 + (datag ? strlen(datag) : 0)
186                 + (extrag ? strlen(extrag) : 0)
187                 + (uncoalg ? strlen(uncoalg) : 0)
188                 + (bucketsg ? strlen(bucketsg) : 0);
189
190         ret = malloc(len);
191         if (!ret)
192                 goto unlock;
193
194         len = sprintf(ret, SUMMARY_FORMAT,
195                       (size_t)tdb->map_size,
196                       tally_num(keys) + tally_num(data),
197                       tally_num(keys),
198                       tally_min(keys), tally_mean(keys), tally_max(keys),
199                       keysg ? keysg : "",
200                       tally_min(data), tally_mean(data), tally_max(data),
201                       datag ? datag : "",
202                       tally_min(extra), tally_mean(extra), tally_max(extra),
203                       extrag ? extrag : "",
204                       tally_num(freet),
205                       tally_min(freet), tally_mean(freet), tally_max(freet),
206                       freeg ? freeg : "",
207                       tally_total(uncoal, NULL),
208                       tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
209                       uncoalg ? uncoalg : "",
210                       tally_num(buckets),
211                       bucketsg ? bucketsg : "",
212                       count_hash(tdb, offsetof(struct tdb_header, hashtable),
213                                  TDB_TOPLEVEL_HASH_BITS),
214                       1 << TDB_TOPLEVEL_HASH_BITS,
215                       tally_num(hashes),
216                       tally_min(hashes), tally_mean(hashes), tally_max(hashes),
217                       hashesg ? hashesg : "",
218                       tally_total(keys, NULL) * 100.0 / tdb->map_size,
219                       tally_total(data, NULL) * 100.0 / tdb->map_size,
220                       tally_total(extra, NULL) * 100.0 / tdb->map_size,
221                       tally_total(freet, NULL) * 100.0 / tdb->map_size,
222                       (tally_num(keys) + tally_num(freet) + tally_num(hashes))
223                       * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size,
224                       tally_num(flists) * sizeof(struct tdb_freelist)
225                       * 100.0 / tdb->map_size,
226                       (tally_num(hashes)
227                        * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
228                        + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS))
229                       * 100.0 / tdb->map_size);
230
231 unlock:
232         free(hashesg);
233         free(freeg);
234         free(keysg);
235         free(datag);
236         free(extrag);
237         free(uncoalg);
238         free(bucketsg);
239         free(hashes);
240         free(buckets);
241         free(freet);
242         free(keys);
243         free(data);
244         free(extra);
245         free(uncoal);
246
247         tdb_allrecord_unlock(tdb, F_RDLCK);
248         tdb_unlock_expand(tdb, F_RDLCK);
249         return ret;
250 }