TDB2: Goodbye TDB2, Hello NTDB.
[obnox/samba/samba-obnox.git] / lib / ntdb / 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 #define SUMMARY_FORMAT \
23         "Size of file/data: %zu/%zu\n" \
24         "Number of records: %zu\n" \
25         "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
26         "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
27         "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
28         "Number of free records: %zu\n" \
29         "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
30         "Number of uncoalesced records: %zu\n" \
31         "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
32         "Toplevel hash used: %u of %u\n" \
33         "Number of chains: %zu\n" \
34         "Number of subhashes: %zu\n" \
35         "Smallest/average/largest subhash entries: %zu/%zu/%zu\n%s" \
36         "Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
37
38 #define BUCKET_SUMMARY_FORMAT_A                                 \
39         "Free bucket %zu: total entries %zu.\n"                 \
40         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
41 #define BUCKET_SUMMARY_FORMAT_B                                 \
42         "Free bucket %zu-%zu: total entries %zu.\n"             \
43         "Smallest/average/largest length: %zu/%zu/%zu\n%s"
44 #define CAPABILITY_FORMAT                                       \
45         "Capability %llu%s\n"
46
47 #define HISTO_WIDTH 70
48 #define HISTO_HEIGHT 20
49
50 static ntdb_off_t count_hash(struct ntdb_context *ntdb,
51                             ntdb_off_t hash_off, unsigned bits)
52 {
53         const ntdb_off_t *h;
54         ntdb_off_t count = 0;
55         unsigned int i;
56
57         h = ntdb_access_read(ntdb, hash_off, sizeof(*h) << bits, true);
58         if (NTDB_PTR_IS_ERR(h)) {
59                 return NTDB_ERR_TO_OFF(NTDB_PTR_ERR(h));
60         }
61         for (i = 0; i < (1 << bits); i++)
62                 count += (h[i] != 0);
63
64         ntdb_access_release(ntdb, h);
65         return count;
66 }
67
68 static enum NTDB_ERROR summarize(struct ntdb_context *ntdb,
69                                 struct tally *hashes,
70                                 struct tally *ftables,
71                                 struct tally *fr,
72                                 struct tally *keys,
73                                 struct tally *data,
74                                 struct tally *extra,
75                                 struct tally *uncoal,
76                                 struct tally *chains,
77                                 size_t *num_caps)
78 {
79         ntdb_off_t off;
80         ntdb_len_t len;
81         ntdb_len_t unc = 0;
82
83         for (off = sizeof(struct ntdb_header);
84              off < ntdb->file->map_size;
85              off += len) {
86                 const union {
87                         struct ntdb_used_record u;
88                         struct ntdb_free_record f;
89                         struct ntdb_recovery_record r;
90                 } *p;
91                 /* We might not be able to get the whole thing. */
92                 p = ntdb_access_read(ntdb, off, sizeof(p->f), true);
93                 if (NTDB_PTR_IS_ERR(p)) {
94                         return NTDB_PTR_ERR(p);
95                 }
96                 if (frec_magic(&p->f) != NTDB_FREE_MAGIC) {
97                         if (unc > 1) {
98                                 tally_add(uncoal, unc);
99                                 unc = 0;
100                         }
101                 }
102
103                 if (p->r.magic == NTDB_RECOVERY_INVALID_MAGIC
104                     || p->r.magic == NTDB_RECOVERY_MAGIC) {
105                         len = sizeof(p->r) + p->r.max_len;
106                 } else if (frec_magic(&p->f) == NTDB_FREE_MAGIC) {
107                         len = frec_len(&p->f);
108                         tally_add(fr, len);
109                         len += sizeof(p->u);
110                         unc++;
111                 } else if (rec_magic(&p->u) == NTDB_USED_MAGIC) {
112                         len = sizeof(p->u)
113                                 + rec_key_length(&p->u)
114                                 + rec_data_length(&p->u)
115                                 + rec_extra_padding(&p->u);
116
117                         tally_add(keys, rec_key_length(&p->u));
118                         tally_add(data, rec_data_length(&p->u));
119                         tally_add(extra, rec_extra_padding(&p->u));
120                 } else if (rec_magic(&p->u) == NTDB_HTABLE_MAGIC) {
121                         ntdb_off_t count = count_hash(ntdb,
122                                                      off + sizeof(p->u),
123                                                      NTDB_SUBLEVEL_HASH_BITS);
124                         if (NTDB_OFF_IS_ERR(count)) {
125                                 return NTDB_OFF_TO_ERR(count);
126                         }
127                         tally_add(hashes, count);
128                         tally_add(extra, rec_extra_padding(&p->u));
129                         len = sizeof(p->u)
130                                 + rec_data_length(&p->u)
131                                 + rec_extra_padding(&p->u);
132                 } else if (rec_magic(&p->u) == NTDB_FTABLE_MAGIC) {
133                         len = sizeof(p->u)
134                                 + rec_data_length(&p->u)
135                                 + rec_extra_padding(&p->u);
136                         tally_add(ftables, rec_data_length(&p->u));
137                         tally_add(extra, rec_extra_padding(&p->u));
138                 } else if (rec_magic(&p->u) == NTDB_CHAIN_MAGIC) {
139                         len = sizeof(p->u)
140                                 + rec_data_length(&p->u)
141                                 + rec_extra_padding(&p->u);
142                         tally_add(chains, 1);
143                         tally_add(extra, rec_extra_padding(&p->u));
144                 } else if (rec_magic(&p->u) == NTDB_CAP_MAGIC) {
145                         len = sizeof(p->u)
146                                 + rec_data_length(&p->u)
147                                 + rec_extra_padding(&p->u);
148                         (*num_caps)++;
149                 } else {
150                         len = dead_space(ntdb, off);
151                         if (NTDB_OFF_IS_ERR(len)) {
152                                 return NTDB_OFF_TO_ERR(len);
153                         }
154                 }
155                 ntdb_access_release(ntdb, p);
156         }
157         if (unc)
158                 tally_add(uncoal, unc);
159         return NTDB_SUCCESS;
160 }
161
162 static void add_capabilities(struct ntdb_context *ntdb, char *summary)
163 {
164         ntdb_off_t off, next;
165         const struct ntdb_capability *cap;
166         size_t count = 0;
167
168         /* Append to summary. */
169         summary += strlen(summary);
170
171         off = ntdb_read_off(ntdb, offsetof(struct ntdb_header, capabilities));
172         if (NTDB_OFF_IS_ERR(off))
173                 return;
174
175         /* Walk capability list. */
176         for (; off; off = next) {
177                 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
178                 if (NTDB_PTR_IS_ERR(cap)) {
179                         break;
180                 }
181                 count++;
182                 sprintf(summary, CAPABILITY_FORMAT,
183                         cap->type & NTDB_CAP_TYPE_MASK,
184                         /* Noopen?  How did we get here? */
185                         (cap->type & NTDB_CAP_NOOPEN) ? " (unopenable)"
186                         : ((cap->type & NTDB_CAP_NOWRITE)
187                            && (cap->type & NTDB_CAP_NOCHECK)) ? " (uncheckable,read-only)"
188                         : (cap->type & NTDB_CAP_NOWRITE) ? " (read-only)"
189                         : (cap->type & NTDB_CAP_NOCHECK) ? " (uncheckable)"
190                         : "");
191                 summary += strlen(summary);
192                 next = cap->next;
193                 ntdb_access_release(ntdb, cap);
194         }
195 }
196
197 _PUBLIC_ enum NTDB_ERROR ntdb_summary(struct ntdb_context *ntdb,
198                            enum ntdb_summary_flags flags,
199                            char **summary)
200 {
201         ntdb_len_t len;
202         size_t num_caps = 0;
203         struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal,
204                 *chains;
205         char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg;
206         enum NTDB_ERROR ecode;
207
208         hashesg = freeg = keysg = datag = extrag = uncoalg = NULL;
209
210         ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
211         if (ecode != NTDB_SUCCESS) {
212                 return ntdb->last_error = ecode;
213         }
214
215         ecode = ntdb_lock_expand(ntdb, F_RDLCK);
216         if (ecode != NTDB_SUCCESS) {
217                 ntdb_allrecord_unlock(ntdb, F_RDLCK);
218                 return ntdb->last_error = ecode;
219         }
220
221         /* Start stats off empty. */
222         ftables = tally_new(HISTO_HEIGHT);
223         hashes = tally_new(HISTO_HEIGHT);
224         freet = tally_new(HISTO_HEIGHT);
225         keys = tally_new(HISTO_HEIGHT);
226         data = tally_new(HISTO_HEIGHT);
227         extra = tally_new(HISTO_HEIGHT);
228         uncoal = tally_new(HISTO_HEIGHT);
229         chains = tally_new(HISTO_HEIGHT);
230         if (!ftables || !hashes || !freet || !keys || !data || !extra
231             || !uncoal || !chains) {
232                 ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
233                                    "ntdb_summary: failed to allocate"
234                                    " tally structures");
235                 goto unlock;
236         }
237
238         ecode = summarize(ntdb, hashes, ftables, freet, keys, data, extra,
239                           uncoal, chains, &num_caps);
240         if (ecode != NTDB_SUCCESS) {
241                 goto unlock;
242         }
243
244         if (flags & NTDB_SUMMARY_HISTOGRAMS) {
245                 hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
246                 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
247                 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
248                 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
249                 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
250                 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
251         }
252
253         /* 20 is max length of a %llu. */
254         len = strlen(SUMMARY_FORMAT) + 33*20 + 1
255                 + (hashesg ? strlen(hashesg) : 0)
256                 + (freeg ? strlen(freeg) : 0)
257                 + (keysg ? strlen(keysg) : 0)
258                 + (datag ? strlen(datag) : 0)
259                 + (extrag ? strlen(extrag) : 0)
260                 + (uncoalg ? strlen(uncoalg) : 0)
261                 + num_caps * (strlen(CAPABILITY_FORMAT) + 20
262                               + strlen(" (uncheckable,read-only)"));
263
264         *summary = malloc(len);
265         if (!*summary) {
266                 ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
267                                    "ntdb_summary: failed to allocate string");
268                 goto unlock;
269         }
270
271         sprintf(*summary, SUMMARY_FORMAT,
272                 (size_t)ntdb->file->map_size,
273                 tally_total(keys, NULL) + tally_total(data, NULL),
274                 tally_num(keys),
275                 tally_min(keys), tally_mean(keys), tally_max(keys),
276                 keysg ? keysg : "",
277                 tally_min(data), tally_mean(data), tally_max(data),
278                 datag ? datag : "",
279                 tally_min(extra), tally_mean(extra), tally_max(extra),
280                 extrag ? extrag : "",
281                 tally_num(freet),
282                 tally_min(freet), tally_mean(freet), tally_max(freet),
283                 freeg ? freeg : "",
284                 tally_total(uncoal, NULL),
285                 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
286                 uncoalg ? uncoalg : "",
287                 (unsigned)count_hash(ntdb, offsetof(struct ntdb_header,
288                                                    hashtable),
289                                      NTDB_TOPLEVEL_HASH_BITS),
290                 1 << NTDB_TOPLEVEL_HASH_BITS,
291                 tally_num(chains),
292                 tally_num(hashes),
293                 tally_min(hashes), tally_mean(hashes), tally_max(hashes),
294                 hashesg ? hashesg : "",
295                 tally_total(keys, NULL) * 100.0 / ntdb->file->map_size,
296                 tally_total(data, NULL) * 100.0 / ntdb->file->map_size,
297                 tally_total(extra, NULL) * 100.0 / ntdb->file->map_size,
298                 tally_total(freet, NULL) * 100.0 / ntdb->file->map_size,
299                 (tally_num(keys) + tally_num(freet) + tally_num(hashes))
300                 * sizeof(struct ntdb_used_record) * 100.0 / ntdb->file->map_size,
301                 tally_num(ftables) * sizeof(struct ntdb_freetable)
302                 * 100.0 / ntdb->file->map_size,
303                 (tally_num(hashes)
304                  * (sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS)
305                  + (sizeof(ntdb_off_t) << NTDB_TOPLEVEL_HASH_BITS)
306                  + sizeof(struct ntdb_chain) * tally_num(chains))
307                 * 100.0 / ntdb->file->map_size);
308
309         add_capabilities(ntdb, *summary);
310
311 unlock:
312         free(hashesg);
313         free(freeg);
314         free(keysg);
315         free(datag);
316         free(extrag);
317         free(uncoalg);
318         free(hashes);
319         free(freet);
320         free(keys);
321         free(data);
322         free(extra);
323         free(uncoal);
324         free(ftables);
325         free(chains);
326
327         ntdb_allrecord_unlock(ntdb, F_RDLCK);
328         ntdb_unlock_expand(ntdb, F_RDLCK);
329         return ntdb->last_error = ecode;
330 }