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