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