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