summaryrefslogtreecommitdiff
path: root/lib/ntdb/summary.c
blob: 5a75dc5b117083030bd6c47bcc2295a4923d8e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
 /*
   Trivial Database 2: human-readable summary code
   Copyright (C) Rusty Russell 2010

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "private.h"
#include <ccan/tally/tally.h>

#define SUMMARY_FORMAT \
	"Size of file/data: %zu/%zu\n" \
	"Number of records: %zu\n" \
	"Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
	"Smallest/average/largest data: %zu/%zu/%zu\n%s" \
	"Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
	"Number of free records: %zu\n" \
	"Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
	"Number of uncoalesced records: %zu\n" \
	"Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
	"Toplevel hash used: %u of %u\n" \
	"Number of hashes: %zu\n" \
	"Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \
	"Percentage keys/data/padding/free/rechdrs/freehdrs/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"

#define BUCKET_SUMMARY_FORMAT_A					\
	"Free bucket %zu: total entries %zu.\n"			\
	"Smallest/average/largest length: %zu/%zu/%zu\n%s"
#define BUCKET_SUMMARY_FORMAT_B					\
	"Free bucket %zu-%zu: total entries %zu.\n"		\
	"Smallest/average/largest length: %zu/%zu/%zu\n%s"
#define CAPABILITY_FORMAT					\
	"Capability %llu%s\n"

#define HISTO_WIDTH 70
#define HISTO_HEIGHT 20

static ntdb_off_t count_hash(struct ntdb_context *ntdb,
			     ntdb_off_t hash_off,
			     ntdb_off_t num)
{
	const ntdb_off_t *h;
	ntdb_off_t i, count = 0;

	h = ntdb_access_read(ntdb, hash_off, sizeof(*h) * num, true);
	if (NTDB_PTR_IS_ERR(h)) {
		return NTDB_ERR_TO_OFF(NTDB_PTR_ERR(h));
	}
	for (i = 0; i < num; i++)
		count += (h[i] != 0);

	ntdb_access_release(ntdb, h);
	return count;
}

static enum NTDB_ERROR summarize(struct ntdb_context *ntdb,
				struct tally *ftables,
				struct tally *fr,
				struct tally *keys,
				struct tally *data,
				struct tally *extra,
				struct tally *uncoal,
				struct tally *hashes,
				size_t *num_caps)
{
	ntdb_off_t off;
	ntdb_len_t len;
	ntdb_len_t unc = 0;

	for (off = sizeof(struct ntdb_header);
	     off < ntdb->file->map_size;
	     off += len) {
		const union {
			struct ntdb_used_record u;
			struct ntdb_free_record f;
			struct ntdb_recovery_record r;
		} *p;
		/* We might not be able to get the whole thing. */
		p = ntdb_access_read(ntdb, off, sizeof(p->f), true);
		if (NTDB_PTR_IS_ERR(p)) {
			return NTDB_PTR_ERR(p);
		}
		if (frec_magic(&p->f) != NTDB_FREE_MAGIC) {
			if (unc > 1) {
				tally_add(uncoal, unc);
				unc = 0;
			}
		}

		if (p->r.magic == NTDB_RECOVERY_INVALID_MAGIC
		    || p->r.magic == NTDB_RECOVERY_MAGIC) {
			len = sizeof(p->r) + p->r.max_len;
		} else if (frec_magic(&p->f) == NTDB_FREE_MAGIC) {
			len = frec_len(&p->f);
			tally_add(fr, len);
			len += sizeof(p->u);
			unc++;
		} else if (rec_magic(&p->u) == NTDB_USED_MAGIC) {
			len = sizeof(p->u)
				+ rec_key_length(&p->u)
				+ rec_data_length(&p->u)
				+ rec_extra_padding(&p->u);

			tally_add(keys, rec_key_length(&p->u));
			tally_add(data, rec_data_length(&p->u));
			tally_add(extra, rec_extra_padding(&p->u));
		} else if (rec_magic(&p->u) == NTDB_HTABLE_MAGIC) {
			ntdb_off_t count = count_hash(ntdb,
						      off + sizeof(p->u),
						      1 << ntdb->hash_bits);
			if (NTDB_OFF_IS_ERR(count)) {
				return NTDB_OFF_TO_ERR(count);
			}
			tally_add(hashes, count);
			tally_add(extra, rec_extra_padding(&p->u));
			len = sizeof(p->u)
				+ rec_data_length(&p->u)
				+ rec_extra_padding(&p->u);
		} else if (rec_magic(&p->u) == NTDB_FTABLE_MAGIC) {
			len = sizeof(p->u)
				+ rec_data_length(&p->u)
				+ rec_extra_padding(&p->u);
			tally_add(ftables, rec_data_length(&p->u));
			tally_add(extra, rec_extra_padding(&p->u));
		} else if (rec_magic(&p->u) == NTDB_CHAIN_MAGIC) {
			len = sizeof(p->u)
				+ rec_data_length(&p->u)
				+ rec_extra_padding(&p->u);
			tally_add(hashes,
				  rec_data_length(&p->u)/sizeof(ntdb_off_t));
			tally_add(extra, rec_extra_padding(&p->u));
		} else if (rec_magic(&p->u) == NTDB_CAP_MAGIC) {
			len = sizeof(p->u)
				+ rec_data_length(&p->u)
				+ rec_extra_padding(&p->u);
			(*num_caps)++;
		} else {
			len = dead_space(ntdb, off);
			if (NTDB_OFF_IS_ERR(len)) {
				return NTDB_OFF_TO_ERR(len);
			}
		}
		ntdb_access_release(ntdb, p);
	}
	if (unc)
		tally_add(uncoal, unc);
	return NTDB_SUCCESS;
}

static void add_capabilities(struct ntdb_context *ntdb, char *summary)
{
	ntdb_off_t off, next;
	const struct ntdb_capability *cap;
	size_t count = 0;

	/* Append to summary. */
	summary += strlen(summary);

	off = ntdb_read_off(ntdb, offsetof(struct ntdb_header, capabilities));
	if (NTDB_OFF_IS_ERR(off))
		return;

	/* Walk capability list. */
	for (; off; off = next) {
		cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
		if (NTDB_PTR_IS_ERR(cap)) {
			break;
		}
		count++;
		sprintf(summary, CAPABILITY_FORMAT,
			cap->type & NTDB_CAP_TYPE_MASK,
			/* Noopen?  How did we get here? */
			(cap->type & NTDB_CAP_NOOPEN) ? " (unopenable)"
			: ((cap->type & NTDB_CAP_NOWRITE)
			   && (cap->type & NTDB_CAP_NOCHECK)) ? " (uncheckable,read-only)"
			: (cap->type & NTDB_CAP_NOWRITE) ? " (read-only)"
			: (cap->type & NTDB_CAP_NOCHECK) ? " (uncheckable)"
			: "");
		summary += strlen(summary);
		next = cap->next;
		ntdb_access_release(ntdb, cap);
	}
}

_PUBLIC_ enum NTDB_ERROR ntdb_summary(struct ntdb_context *ntdb,
			   enum ntdb_summary_flags flags,
			   char **summary)
{
	ntdb_len_t len;
	size_t num_caps = 0;
	struct tally *ftables, *freet, *keys, *data, *extra, *uncoal, *hashes;
	char *freeg, *keysg, *datag, *extrag, *uncoalg, *hashesg;
	enum NTDB_ERROR ecode;

	freeg = keysg = datag = extrag = uncoalg = hashesg = NULL;

	ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
	if (ecode != NTDB_SUCCESS) {
		return ecode;
	}

	ecode = ntdb_lock_expand(ntdb, F_RDLCK);
	if (ecode != NTDB_SUCCESS) {
		ntdb_allrecord_unlock(ntdb, F_RDLCK);
		return ecode;
	}

	/* Start stats off empty. */
	ftables = tally_new(HISTO_HEIGHT);
	freet = tally_new(HISTO_HEIGHT);
	keys = tally_new(HISTO_HEIGHT);
	data = tally_new(HISTO_HEIGHT);
	extra = tally_new(HISTO_HEIGHT);
	uncoal = tally_new(HISTO_HEIGHT);
	hashes = tally_new(HISTO_HEIGHT);
	if (!ftables || !freet || !keys || !data || !extra
	    || !uncoal || !hashes) {
		ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
				   "ntdb_summary: failed to allocate"
				   " tally structures");
		goto unlock;
	}

	ecode = summarize(ntdb, ftables, freet, keys, data, extra,
			  uncoal, hashes, &num_caps);
	if (ecode != NTDB_SUCCESS) {
		goto unlock;
	}

	if (flags & NTDB_SUMMARY_HISTOGRAMS) {
		freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
		keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
		datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
		extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
		uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
		hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT);
	}

	/* 20 is max length of a %llu. */
	len = strlen(SUMMARY_FORMAT) + 33*20 + 1
		+ (freeg ? strlen(freeg) : 0)
		+ (keysg ? strlen(keysg) : 0)
		+ (datag ? strlen(datag) : 0)
		+ (extrag ? strlen(extrag) : 0)
		+ (uncoalg ? strlen(uncoalg) : 0)
		+ (hashesg ? strlen(hashesg) : 0)
		+ num_caps * (strlen(CAPABILITY_FORMAT) + 20
			      + strlen(" (uncheckable,read-only)"));

	*summary = ntdb->alloc_fn(ntdb, len, ntdb->alloc_data);
	if (!*summary) {
		ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
				   "ntdb_summary: failed to allocate string");
		goto unlock;
	}

	sprintf(*summary, SUMMARY_FORMAT,
		(size_t)ntdb->file->map_size,
		tally_total(keys, NULL) + tally_total(data, NULL),
		tally_num(keys),
		tally_min(keys), tally_mean(keys), tally_max(keys),
		keysg ? keysg : "",
		tally_min(data), tally_mean(data), tally_max(data),
		datag ? datag : "",
		tally_min(extra), tally_mean(extra), tally_max(extra),
		extrag ? extrag : "",
		tally_num(freet),
		tally_min(freet), tally_mean(freet), tally_max(freet),
		freeg ? freeg : "",
		tally_total(uncoal, NULL),
		tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
		uncoalg ? uncoalg : "",
		(unsigned)count_hash(ntdb, sizeof(struct ntdb_header),
				     1 << ntdb->hash_bits),
		1 << ntdb->hash_bits,
		tally_num(hashes),
		tally_min(hashes), tally_mean(hashes), tally_max(hashes),
		hashesg ? hashesg : "",
		tally_total(keys, NULL) * 100.0 / ntdb->file->map_size,
		tally_total(data, NULL) * 100.0 / ntdb->file->map_size,
		tally_total(extra, NULL) * 100.0 / ntdb->file->map_size,
		tally_total(freet, NULL) * 100.0 / ntdb->file->map_size,
		(tally_num(keys) + tally_num(freet) + tally_num(hashes))
		* sizeof(struct ntdb_used_record) * 100.0 / ntdb->file->map_size,
		tally_num(ftables) * sizeof(struct ntdb_freetable)
		* 100.0 / ntdb->file->map_size,
		(tally_total(hashes, NULL) * sizeof(ntdb_off_t)
		 + (sizeof(ntdb_off_t) << ntdb->hash_bits))
		* 100.0 / ntdb->file->map_size);

	add_capabilities(ntdb, *summary);

unlock:
	ntdb->free_fn(freeg, ntdb->alloc_data);
	ntdb->free_fn(keysg, ntdb->alloc_data);
	ntdb->free_fn(datag, ntdb->alloc_data);
	ntdb->free_fn(extrag, ntdb->alloc_data);
	ntdb->free_fn(uncoalg, ntdb->alloc_data);
	ntdb->free_fn(hashesg, ntdb->alloc_data);
	ntdb->free_fn(freet, ntdb->alloc_data);
	ntdb->free_fn(keys, ntdb->alloc_data);
	ntdb->free_fn(data, ntdb->alloc_data);
	ntdb->free_fn(extra, ntdb->alloc_data);
	ntdb->free_fn(uncoal, ntdb->alloc_data);
	ntdb->free_fn(ftables, ntdb->alloc_data);
	ntdb->free_fn(hashes, ntdb->alloc_data);

	ntdb_allrecord_unlock(ntdb, F_RDLCK);
	ntdb_unlock_expand(ntdb, F_RDLCK);
	return ecode;
}