tdb2: add TDB_COALESCING_MAGIC to solve coalescing race.
[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 void sizes_for_bucket(unsigned bucket, size_t *min, size_t *max)
23 {
24         if (bucket <= 8) {
25                 *min = *max = TDB_MIN_DATA_LEN + bucket * 8;
26         } else if (bucket == 9) {
27                 /* FIXME: This is twisted; fix size_to_bucket. */
28                 *min = TDB_MIN_DATA_LEN + (1ULL << (bucket - 3)) + 8;
29                 *max = TDB_MIN_DATA_LEN + (1ULL << (bucket - 2)) - 8;
30         } else {
31                 *min = TDB_MIN_DATA_LEN + (1ULL << (bucket - 3));
32                 *max = TDB_MIN_DATA_LEN + (1ULL << (bucket - 2)) - 8;
33         }
34         assert(size_to_bucket(63, *min) == bucket);
35         assert(size_to_bucket(63, *max) == bucket);
36         if (bucket > 8)
37                 assert(size_to_bucket(63, *min - 8) == bucket - 1);
38         assert(size_to_bucket(63, *max + 8) == bucket + 1);
39 }
40
41 static int count_hash(struct tdb_context *tdb,
42                       tdb_off_t hash_off, unsigned bits)
43 {
44         const tdb_off_t *h;
45         unsigned int i, count = 0;
46
47         h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true);
48         if (!h)
49                 return -1;
50         for (i = 0; i < (1 << bits); i++)
51                 count += (h[i] != 0);
52
53         tdb_access_release(tdb, h);
54         return count;
55 }
56
57 static tdb_len_t summarize_zone(struct tdb_context *tdb, tdb_off_t zone_off,
58                                 struct tally *zones,
59                                 struct tally *hashes,
60                                 struct tally *free,
61                                 struct tally *keys,
62                                 struct tally *data,
63                                 struct tally *extra,
64                                 struct tally *uncoal,
65                                 uint64_t bucketlen[],
66                                 unsigned int *num_buckets)
67 {
68         struct free_zone_header zhdr;
69         tdb_off_t off;
70         tdb_len_t len;
71         unsigned int hdrlen;
72         tdb_len_t unc = 0;
73
74         if (tdb_read_convert(tdb, zone_off, &zhdr, sizeof(zhdr)) == -1)
75                 return TDB_OFF_ERR;
76
77         tally_add(zones, 1ULL << zhdr.zone_bits);
78         *num_buckets = BUCKETS_FOR_ZONE(zhdr.zone_bits);
79
80         hdrlen = sizeof(zhdr)
81                 + (BUCKETS_FOR_ZONE(zhdr.zone_bits) + 1) * sizeof(tdb_off_t);
82         for (off = zone_off + hdrlen;
83              off < zone_off + (1ULL << zhdr.zone_bits);
84              off += len) {
85                 union {
86                         struct tdb_used_record u;
87                         struct tdb_free_record f;
88                 } pad, *p;
89                 p = tdb_get(tdb, off, &pad, sizeof(pad));
90                 if (!p)
91                         return TDB_OFF_ERR;
92                 if (rec_magic(&p->u) != TDB_MAGIC) {
93                         len = p->f.data_len;
94                         tally_add(free, len);
95                         bucketlen[size_to_bucket(frec_zone_bits(&p->f), len)]++;
96                         len += sizeof(p->u);
97                         unc++;
98                 } else {
99                         if (unc) {
100                                 tally_add(uncoal, unc);
101                                 unc = 0;
102                         }
103                         len = sizeof(p->u)
104                                 + rec_key_length(&p->u)
105                                 + rec_data_length(&p->u)
106                                 + rec_extra_padding(&p->u);
107
108                         /* FIXME: Use different magic for hashes? */
109                         if (!rec_key_length(&p->u) && !rec_hash(&p->u)) {
110                                 int count = count_hash(tdb, off + sizeof(p->u),
111                                                        TDB_SUBLEVEL_HASH_BITS);
112                                 if (count == -1)
113                                         return TDB_OFF_ERR;
114                                 tally_add(hashes, count);
115                         } else {
116                                 tally_add(keys, rec_key_length(&p->u));
117                                 tally_add(data, rec_data_length(&p->u));
118                         }
119                         tally_add(extra, rec_extra_padding(&p->u));
120                 }
121         }
122         if (unc)
123                 tally_add(uncoal, unc);
124         return 1ULL << zhdr.zone_bits;
125 }
126
127 #define SUMMARY_FORMAT \
128         "Size of file/data: %zu/%zu\n" \
129         "Number of zones: %zu\n" \
130         "Smallest/average/largest zone size: %zu/%zu/%zu\n%s" \
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         "Toplevel hash used: %u of %u\n" \
140         "Number of subhashes: %zu\n" \
141         "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
142         "Percentage keys/data/padding/free/rechdrs/zonehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
143
144 #define BUCKET_SUMMARY_FORMAT_A                                 \
145         "Free bucket %zu: total entries %zu.\n"                 \
146         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
147 #define BUCKET_SUMMARY_FORMAT_B                                 \
148         "Free bucket %zu-%zu: total entries %zu.\n"             \
149         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
150
151 #define HISTO_WIDTH 70
152 #define HISTO_HEIGHT 20
153
154 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
155 {
156         tdb_off_t off;
157         tdb_len_t len;
158         unsigned int i, num_buckets, max_bucket = 0;
159         uint64_t total_buckets = 0;
160         struct tally *zones, *hashes, *freet, *keys, *data, *extra, *uncoal,
161                 *buckets[BUCKETS_FOR_ZONE(63)+1] = { NULL };
162         char *zonesg, *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg,
163                 *bucketsg[BUCKETS_FOR_ZONE(63)+1] = { NULL };
164         char *ret = NULL;
165
166         zonesg = hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
167
168         if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
169                 return NULL;
170
171         if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
172                 tdb_allrecord_unlock(tdb, F_RDLCK);
173                 return NULL;
174         }
175
176         /* Start stats off empty. */
177         zones = tally_new(HISTO_HEIGHT);
178         hashes = tally_new(HISTO_HEIGHT);
179         freet = tally_new(HISTO_HEIGHT);
180         keys = tally_new(HISTO_HEIGHT);
181         data = tally_new(HISTO_HEIGHT);
182         extra = tally_new(HISTO_HEIGHT);
183         uncoal = tally_new(HISTO_HEIGHT);
184         if (!zones || !hashes || !freet || !keys || !data || !extra
185             || !uncoal) {
186                 tdb->ecode = TDB_ERR_OOM;
187                 goto unlock;
188         }
189
190         for (i = 0; i < sizeof(buckets)/sizeof(buckets[0]); i++) {
191                 buckets[i] = tally_new(HISTO_HEIGHT);
192                 if (!buckets[i]) {
193                         tdb->ecode = TDB_ERR_OOM;
194                         goto unlock;
195                 }
196         }
197
198         for (off = sizeof(struct tdb_header);
199              off < tdb->map_size - 1;
200              off += len) {
201                 uint64_t bucketlen[BUCKETS_FOR_ZONE(63)+1] = { 0 };
202                 len = summarize_zone(tdb, off, zones, hashes, freet, keys,
203                                      data, extra, uncoal, bucketlen,
204                                      &num_buckets);
205                 if (len == TDB_OFF_ERR)
206                         goto unlock;
207                 for (i = 0; i < num_buckets; i++)
208                         tally_add(buckets[i], bucketlen[i]);
209                 if (num_buckets > max_bucket)
210                         max_bucket = num_buckets;
211                 total_buckets += num_buckets;
212         }
213
214         if (flags & TDB_SUMMARY_HISTOGRAMS) {
215                 zonesg = tally_histogram(zones, HISTO_WIDTH, HISTO_HEIGHT);
216                 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
217                 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
218                 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
219                 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
220                 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
221                 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
222                 for (i = 0; i < sizeof(buckets)/sizeof(buckets[0]); i++) {
223                         bucketsg[i] = tally_histogram(buckets[i],
224                                                       HISTO_WIDTH,
225                                                       HISTO_HEIGHT);
226                 }
227         }
228
229         /* 20 is max length of a %llu. */
230         len = strlen(SUMMARY_FORMAT) + 33*20 + 1
231                 + (zonesg ? strlen(zonesg) : 0)
232                 + (hashesg ? strlen(hashesg) : 0)
233                 + (freeg ? strlen(freeg) : 0)
234                 + (keysg ? strlen(keysg) : 0)
235                 + (datag ? strlen(datag) : 0)
236                 + (extrag ? strlen(extrag) : 0)
237                 + (uncoalg ? strlen(uncoalg) : 0);
238         for (i = 0; i < max_bucket; i++) {
239                 len += strlen(BUCKET_SUMMARY_FORMAT_B) + 6 * 20
240                         + (bucketsg[i] ? strlen(bucketsg[i]) : 0);
241         }
242
243         ret = malloc(len);
244         if (!ret)
245                 goto unlock;
246
247         len = sprintf(ret, SUMMARY_FORMAT,
248                       (size_t)tdb->map_size,
249                       tally_num(keys) + tally_num(data),
250                       tally_num(zones),
251                       tally_min(zones), tally_mean(zones), tally_max(zones),
252                       zonesg ? zonesg : "",
253                       tally_num(keys),
254                       tally_min(keys), tally_mean(keys), tally_max(keys),
255                       keysg ? keysg : "",
256                       tally_min(data), tally_mean(data), tally_max(data),
257                       datag ? datag : "",
258                       tally_min(extra), tally_mean(extra), tally_max(extra),
259                       extrag ? extrag : "",
260                       tally_num(freet),
261                       tally_min(freet), tally_mean(freet), tally_max(freet),
262                       freeg ? freeg : "",
263                       tally_total(uncoal, NULL),
264                       tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
265                       uncoalg ? uncoalg : "",
266                       count_hash(tdb, offsetof(struct tdb_header, hashtable),
267                                  TDB_TOPLEVEL_HASH_BITS),
268                       1 << TDB_TOPLEVEL_HASH_BITS,
269                       tally_num(hashes),
270                       tally_min(hashes), tally_mean(hashes), tally_max(hashes),
271                       hashesg ? hashesg : "",
272                       tally_total(keys, NULL) * 100.0 / tdb->map_size,
273                       tally_total(data, NULL) * 100.0 / tdb->map_size,
274                       tally_total(extra, NULL) * 100.0 / tdb->map_size,
275                       tally_total(freet, NULL) * 100.0 / tdb->map_size,
276                       (tally_num(keys) + tally_num(freet) + tally_num(hashes))
277                       * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size,
278                       (tally_num(zones) * sizeof(struct free_zone_header)
279                        + total_buckets * sizeof(tdb_off_t))
280                       * 100.0 / tdb->map_size,
281                       (tally_num(hashes)
282                        * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS)
283                        + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS))
284                       * 100.0 / tdb->map_size);
285
286         for (i = 0; i < max_bucket; i++) {
287                 size_t min, max;
288                 sizes_for_bucket(i, &min, &max);
289                 if (min == max) {
290                         len += sprintf(ret + len, BUCKET_SUMMARY_FORMAT_A,
291                                        min, tally_total(buckets[i], NULL),
292                                        tally_min(buckets[i]),
293                                        tally_mean(buckets[i]),
294                                        tally_max(buckets[i]),
295                                        bucketsg[i] ? bucketsg[i] : "");
296                 } else {
297                         len += sprintf(ret + len, BUCKET_SUMMARY_FORMAT_B,
298                                        min, max, tally_total(buckets[i], NULL),
299                                        tally_min(buckets[i]),
300                                        tally_mean(buckets[i]),
301                                        tally_max(buckets[i]),
302                                        bucketsg[i] ? bucketsg[i] : "");
303                 }
304         }
305
306 unlock:
307         free(zonesg);
308         free(hashesg);
309         free(freeg);
310         free(keysg);
311         free(datag);
312         free(extrag);
313         free(uncoalg);
314         free(zones);
315         free(hashes);
316         free(freet);
317         free(keys);
318         free(data);
319         free(extra);
320         free(uncoal);
321         for (i = 0; i < sizeof(buckets)/sizeof(buckets[0]); i++) {
322                 free(buckets[i]);
323                 free(bucketsg[i]);
324         }
325
326         tdb_allrecord_unlock(tdb, F_RDLCK);
327         tdb_unlock_expand(tdb, F_RDLCK);
328         return ret;
329 }