1 | /*
|
---|
2 | Trivial Database: 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 "tdb_private.h"
|
---|
19 |
|
---|
20 | #define SUMMARY_FORMAT \
|
---|
21 | "Size of file/data: %llu/%zu\n" \
|
---|
22 | "Header offset/logical size: %zu/%zu\n" \
|
---|
23 | "Number of records: %zu\n" \
|
---|
24 | "Incompatible hash: %s\n" \
|
---|
25 | "Active/supported feature flags: 0x%08x/0x%08x\n" \
|
---|
26 | "Robust mutexes locking: %s\n" \
|
---|
27 | "Smallest/average/largest keys: %zu/%zu/%zu\n" \
|
---|
28 | "Smallest/average/largest data: %zu/%zu/%zu\n" \
|
---|
29 | "Smallest/average/largest padding: %zu/%zu/%zu\n" \
|
---|
30 | "Number of dead records: %zu\n" \
|
---|
31 | "Smallest/average/largest dead records: %zu/%zu/%zu\n" \
|
---|
32 | "Number of free records: %zu\n" \
|
---|
33 | "Smallest/average/largest free records: %zu/%zu/%zu\n" \
|
---|
34 | "Number of hash chains: %zu\n" \
|
---|
35 | "Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
|
---|
36 | "Number of uncoalesced records: %zu\n" \
|
---|
37 | "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n" \
|
---|
38 | "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
|
---|
39 |
|
---|
40 | /* We don't use tally module, to keep upstream happy. */
|
---|
41 | struct tally {
|
---|
42 | size_t min, max, total;
|
---|
43 | size_t num;
|
---|
44 | };
|
---|
45 |
|
---|
46 | static void tally_init(struct tally *tally)
|
---|
47 | {
|
---|
48 | tally->total = 0;
|
---|
49 | tally->num = 0;
|
---|
50 | tally->min = tally->max = 0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | static void tally_add(struct tally *tally, size_t len)
|
---|
54 | {
|
---|
55 | if (tally->num == 0)
|
---|
56 | tally->max = tally->min = len;
|
---|
57 | else if (len > tally->max)
|
---|
58 | tally->max = len;
|
---|
59 | else if (len < tally->min)
|
---|
60 | tally->min = len;
|
---|
61 | tally->num++;
|
---|
62 | tally->total += len;
|
---|
63 | }
|
---|
64 |
|
---|
65 | static size_t tally_mean(const struct tally *tally)
|
---|
66 | {
|
---|
67 | if (!tally->num)
|
---|
68 | return 0;
|
---|
69 | return tally->total / tally->num;
|
---|
70 | }
|
---|
71 |
|
---|
72 | static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
|
---|
73 | {
|
---|
74 | tdb_off_t rec_ptr;
|
---|
75 | size_t count = 0;
|
---|
76 |
|
---|
77 | if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
|
---|
78 | return 0;
|
---|
79 |
|
---|
80 | /* keep looking until we find the right record */
|
---|
81 | while (rec_ptr) {
|
---|
82 | struct tdb_record r;
|
---|
83 | ++count;
|
---|
84 | if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
|
---|
85 | return 0;
|
---|
86 | rec_ptr = r.next;
|
---|
87 | }
|
---|
88 | return count;
|
---|
89 | }
|
---|
90 |
|
---|
91 | _PUBLIC_ char *tdb_summary(struct tdb_context *tdb)
|
---|
92 | {
|
---|
93 | off_t file_size;
|
---|
94 | tdb_off_t off, rec_off;
|
---|
95 | struct tally freet, keys, data, dead, extra, hashval, uncoal;
|
---|
96 | struct tdb_record rec;
|
---|
97 | char *ret = NULL;
|
---|
98 | bool locked;
|
---|
99 | size_t unc = 0;
|
---|
100 | int len;
|
---|
101 | struct tdb_record recovery;
|
---|
102 |
|
---|
103 | /* Read-only databases use no locking at all: it's best-effort.
|
---|
104 | * We may have a write lock already, so skip that case too. */
|
---|
105 | if (tdb->read_only || tdb->allrecord_lock.count != 0) {
|
---|
106 | locked = false;
|
---|
107 | } else {
|
---|
108 | if (tdb_lockall_read(tdb) == -1)
|
---|
109 | return NULL;
|
---|
110 | locked = true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (tdb_recovery_area(tdb, tdb->methods, &rec_off, &recovery) != 0) {
|
---|
114 | goto unlock;
|
---|
115 | }
|
---|
116 |
|
---|
117 | tally_init(&freet);
|
---|
118 | tally_init(&keys);
|
---|
119 | tally_init(&data);
|
---|
120 | tally_init(&dead);
|
---|
121 | tally_init(&extra);
|
---|
122 | tally_init(&hashval);
|
---|
123 | tally_init(&uncoal);
|
---|
124 |
|
---|
125 | for (off = TDB_DATA_START(tdb->hash_size);
|
---|
126 | off < tdb->map_size - 1;
|
---|
127 | off += sizeof(rec) + rec.rec_len) {
|
---|
128 | if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
|
---|
129 | DOCONV()) == -1)
|
---|
130 | goto unlock;
|
---|
131 | switch (rec.magic) {
|
---|
132 | case TDB_MAGIC:
|
---|
133 | tally_add(&keys, rec.key_len);
|
---|
134 | tally_add(&data, rec.data_len);
|
---|
135 | tally_add(&extra, rec.rec_len - (rec.key_len
|
---|
136 | + rec.data_len));
|
---|
137 | if (unc > 1)
|
---|
138 | tally_add(&uncoal, unc - 1);
|
---|
139 | unc = 0;
|
---|
140 | break;
|
---|
141 | case TDB_FREE_MAGIC:
|
---|
142 | tally_add(&freet, rec.rec_len);
|
---|
143 | unc++;
|
---|
144 | break;
|
---|
145 | /* If we crash after ftruncate, we can get zeroes or fill. */
|
---|
146 | case TDB_RECOVERY_INVALID_MAGIC:
|
---|
147 | case 0x42424242:
|
---|
148 | unc++;
|
---|
149 | /* If it's a valid recovery, we can trust rec_len. */
|
---|
150 | if (off != rec_off) {
|
---|
151 | rec.rec_len = tdb_dead_space(tdb, off)
|
---|
152 | - sizeof(rec);
|
---|
153 | }
|
---|
154 | /* Fall through */
|
---|
155 | case TDB_DEAD_MAGIC:
|
---|
156 | tally_add(&dead, rec.rec_len);
|
---|
157 | break;
|
---|
158 | default:
|
---|
159 | TDB_LOG((tdb, TDB_DEBUG_ERROR,
|
---|
160 | "Unexpected record magic 0x%x at offset %u\n",
|
---|
161 | rec.magic, off));
|
---|
162 | goto unlock;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | if (unc > 1)
|
---|
166 | tally_add(&uncoal, unc - 1);
|
---|
167 |
|
---|
168 | for (off = 0; off < tdb->hash_size; off++)
|
---|
169 | tally_add(&hashval, get_hash_length(tdb, off));
|
---|
170 |
|
---|
171 | file_size = tdb->hdr_ofs + tdb->map_size;
|
---|
172 |
|
---|
173 | len = asprintf(&ret, SUMMARY_FORMAT,
|
---|
174 | (unsigned long long)file_size, keys.total+data.total,
|
---|
175 | (size_t)tdb->hdr_ofs, (size_t)tdb->map_size,
|
---|
176 | keys.num,
|
---|
177 | (tdb->hash_fn == tdb_jenkins_hash)?"yes":"no",
|
---|
178 | (unsigned)tdb->feature_flags, TDB_SUPPORTED_FEATURE_FLAGS,
|
---|
179 | (tdb->feature_flags & TDB_FEATURE_FLAG_MUTEX)?"yes":"no",
|
---|
180 | keys.min, tally_mean(&keys), keys.max,
|
---|
181 | data.min, tally_mean(&data), data.max,
|
---|
182 | extra.min, tally_mean(&extra), extra.max,
|
---|
183 | dead.num,
|
---|
184 | dead.min, tally_mean(&dead), dead.max,
|
---|
185 | freet.num,
|
---|
186 | freet.min, tally_mean(&freet), freet.max,
|
---|
187 | hashval.num,
|
---|
188 | hashval.min, tally_mean(&hashval), hashval.max,
|
---|
189 | uncoal.total,
|
---|
190 | uncoal.min, tally_mean(&uncoal), uncoal.max,
|
---|
191 | keys.total * 100.0 / file_size,
|
---|
192 | data.total * 100.0 / file_size,
|
---|
193 | extra.total * 100.0 / file_size,
|
---|
194 | freet.total * 100.0 / file_size,
|
---|
195 | dead.total * 100.0 / file_size,
|
---|
196 | (keys.num + freet.num + dead.num)
|
---|
197 | * (sizeof(struct tdb_record) + sizeof(uint32_t))
|
---|
198 | * 100.0 / file_size,
|
---|
199 | tdb->hash_size * sizeof(tdb_off_t)
|
---|
200 | * 100.0 / file_size);
|
---|
201 | if (len == -1) {
|
---|
202 | goto unlock;
|
---|
203 | }
|
---|
204 |
|
---|
205 | unlock:
|
---|
206 | if (locked) {
|
---|
207 | tdb_unlockall_read(tdb);
|
---|
208 | }
|
---|
209 | return ret;
|
---|
210 | }
|
---|