]> git.ozlabs.org Git - ccan/blob - ccan/tdb/summary.c
a8f2b71bf1ff1ae4f741cc6179dd018e1afb178a
[ccan] / ccan / tdb / summary.c
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 #include <ccan/tally/tally.h>
20
21 #define SUMMARY_FORMAT \
22         "Size of file/data: %zu/%zu\n" \
23         "Number of records: %zu\n" \
24         "Smallest/average/largest keys: %zu/%zu/%zu\n%s" \
25         "Smallest/average/largest data: %zu/%zu/%zu\n%s" \
26         "Smallest/average/largest padding: %zu/%zu/%zu\n%s" \
27         "Number of dead records: %zu\n" \
28         "Smallest/average/largest dead records: %zu/%zu/%zu\n%s" \
29         "Number of free records: %zu\n" \
30         "Smallest/average/largest free records: %zu/%zu/%zu\n%s" \
31         "Number of hash chains: %zu\n" \
32         "Smallest/average/largest hash chains: %zu/%zu/%zu\n%s" \
33         "Number of uncoalesced records: %zu\n" \
34         "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n%s" \
35         "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
36
37 #define HISTO_WIDTH 70
38 #define HISTO_HEIGHT 20
39
40 /* Slow, but should be very rare. */
41 static size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
42 {
43         size_t len;
44
45         for (len = 0; off + len < tdb->map_size; len++) {
46                 char c;
47                 if (tdb->methods->tdb_read(tdb, off, &c, 1, 0))
48                         return 0;
49                 if (c != 0 && c != 0x42)
50                         break;
51         }
52         return len;
53 }
54
55 static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
56 {
57         tdb_off_t rec_ptr;
58         size_t count = 0;
59
60         if (tdb_ofs_read(tdb, TDB_HASH_TOP(i), &rec_ptr) == -1)
61                 return 0;
62
63         /* keep looking until we find the right record */
64         while (rec_ptr) {
65                 struct tdb_record r;
66                 ++count;
67                 if (tdb_rec_read(tdb, rec_ptr, &r) == -1)
68                         return 0;
69                 rec_ptr = r.next;
70         }
71         return count;
72 }
73
74 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
75 {
76         tdb_off_t off;
77         struct tally *freet, *keys, *data, *dead, *extra, *hash, *uncoal;
78         char *freeg, *keysg, *datag, *deadg, *extrag, *hashg, *uncoalg;
79         struct tdb_record rec;
80         char *ret = NULL;
81         bool locked;
82         size_t len, unc = 0;
83
84         freeg = keysg = datag = deadg = extrag = hashg = uncoalg = NULL;
85
86         /* Read-only databases use no locking at all: it's best-effort.
87          * We may have a write lock already, so skip that case too. */
88         if (tdb->read_only || tdb->allrecord_lock.count != 0) {
89                 locked = false;
90         } else {
91                 if (tdb_lockall_read(tdb) == -1)
92                         return NULL;
93                 locked = true;
94         }
95
96         freet = tally_new(HISTO_HEIGHT);
97         keys = tally_new(HISTO_HEIGHT);
98         data = tally_new(HISTO_HEIGHT);
99         dead = tally_new(HISTO_HEIGHT);
100         extra = tally_new(HISTO_HEIGHT);
101         hash = tally_new(HISTO_HEIGHT);
102         uncoal = tally_new(HISTO_HEIGHT);
103         if (!freet || !keys || !data || !dead || !extra || !hash || !uncoal) {
104                 tdb->ecode = TDB_ERR_OOM;
105                 goto unlock;
106         }
107
108         for (off = TDB_DATA_START(tdb->header.hash_size);
109              off < tdb->map_size - 1;
110              off += sizeof(rec) + rec.rec_len) {
111                 if (tdb->methods->tdb_read(tdb, off, &rec, sizeof(rec),
112                                            DOCONV()) == -1)
113                         goto unlock;
114                 switch (rec.magic) {
115                 case TDB_MAGIC:
116                         tally_add(keys, rec.key_len);
117                         tally_add(data, rec.data_len);
118                         tally_add(extra, rec.rec_len - (rec.key_len
119                                                         + rec.data_len));
120                         break;
121                 case TDB_FREE_MAGIC:
122                         tally_add(freet, rec.rec_len);
123                         unc++;
124                         break;
125                 /* If we crash after ftruncate, we can get zeroes or fill. */
126                 case TDB_RECOVERY_INVALID_MAGIC:
127                 case 0x42424242:
128                         unc++;
129                         rec.rec_len = dead_space(tdb, off) - sizeof(rec);
130                         /* Fall through */
131                 case TDB_DEAD_MAGIC:
132                         tally_add(dead, rec.rec_len);
133                         break;
134                 default:
135                         TDB_LOG((tdb, TDB_DEBUG_ERROR,
136                                  "Unexpected record magic 0x%x at offset %d\n",
137                                  rec.magic, off));
138                         goto unlock;
139                 }
140
141                 if (unc &&
142                     (rec.magic == TDB_MAGIC || rec.magic == TDB_DEAD_MAGIC)) {
143                         tally_add(uncoal, unc);
144                         unc = 0;
145                 }
146         }
147         if (unc)
148                 tally_add(uncoal, unc);
149
150         for (off = 0; off < tdb->header.hash_size; off++)
151                 tally_add(hash, get_hash_length(tdb, off));
152
153         if (flags & TDB_SUMMARY_HISTOGRAMS) {
154                 freeg = tally_histogram(freet, HISTO_WIDTH, HISTO_HEIGHT);
155                 keysg = tally_histogram(keys, HISTO_WIDTH, HISTO_HEIGHT);
156                 datag = tally_histogram(data, HISTO_WIDTH, HISTO_HEIGHT);
157                 deadg = tally_histogram(dead, HISTO_WIDTH, HISTO_HEIGHT);
158                 extrag = tally_histogram(extra, HISTO_WIDTH, HISTO_HEIGHT);
159                 hashg = tally_histogram(hash, HISTO_WIDTH, HISTO_HEIGHT);
160                 uncoalg = tally_histogram(uncoal, HISTO_WIDTH, HISTO_HEIGHT);
161         }
162
163         /* 20 is max length of a %zu. */
164         len = strlen(SUMMARY_FORMAT) + 29*20 + 1
165                 + (freeg ? strlen(freeg) : 0)
166                 + (keysg ? strlen(keysg) : 0)
167                 + (datag ? strlen(datag) : 0)
168                 + (deadg ? strlen(deadg) : 0)
169                 + (extrag ? strlen(extrag) : 0)
170                 + (hashg ? strlen(hashg) : 0)
171                 + (uncoalg ? strlen(uncoalg) : 0);
172         ret = malloc(len);
173         if (!ret)
174                 goto unlock;
175
176         sprintf(ret, SUMMARY_FORMAT,
177                 tdb->map_size, tally_total(keys, NULL)+tally_total(data, NULL),
178                 tally_num(keys),
179                 tally_min(keys), tally_mean(keys), tally_max(keys),
180                 keysg ? keysg : "",
181                 tally_min(data), tally_mean(data), tally_max(data),
182                 datag ? datag : "",
183                 tally_min(extra), tally_mean(extra), tally_max(extra),
184                 extrag ? extrag : "",
185                 tally_num(dead),
186                 tally_min(dead), tally_mean(dead), tally_max(dead),
187                 deadg ? deadg : "",
188                 tally_num(freet),
189                 tally_min(freet), tally_mean(freet), tally_max(freet),
190                 freeg ? freeg : "",
191                 tally_num(hash),
192                 tally_min(hash), tally_mean(hash), tally_max(hash),
193                 hashg ? hashg : "",
194                 tally_total(uncoal, NULL),
195                 tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal),
196                 uncoalg ? uncoalg : "",
197                 tally_total(keys, NULL) * 100.0 / tdb->map_size,
198                 tally_total(data, NULL) * 100.0 / tdb->map_size,
199                 tally_total(extra, NULL) * 100.0 / tdb->map_size,
200                 tally_total(freet, NULL) * 100.0 / tdb->map_size,
201                 tally_total(dead, NULL) * 100.0 / tdb->map_size,
202                 (tally_num(keys) + tally_num(freet) + tally_num(dead))
203                 * (sizeof(struct tdb_record) + sizeof(uint32_t))
204                 * 100.0 / tdb->map_size,
205                 tdb->header.hash_size * sizeof(tdb_off_t)
206                 * 100.0 / tdb->map_size);
207
208 unlock:
209         free(freeg);
210         free(keysg);
211         free(datag);
212         free(deadg);
213         free(extrag);
214         free(hashg);
215         free(uncoalg);
216         free(freet);
217         free(keys);
218         free(data);
219         free(dead);
220         free(extra);
221         free(hash);
222         free(uncoal);
223         if (locked) {
224                 tdb_unlockall_read(tdb);
225         }
226         return ret;
227 }