tdb2: import TDB1 code.
[ccan] / ccan / tdb2 / tdb1_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 "tdb1_private.h"
19
20 #define SUMMARY_FORMAT1 \
21         "Size of file/data: %u/%zu\n" \
22         "Number of records: %zu\n" \
23         "Smallest/average/largest keys: %zu/%zu/%zu\n" \
24         "Smallest/average/largest data: %zu/%zu/%zu\n" \
25         "Smallest/average/largest padding: %zu/%zu/%zu\n" \
26         "Number of dead records: %zu\n" \
27         "Smallest/average/largest dead records: %zu/%zu/%zu\n" \
28         "Number of free records: %zu\n" \
29         "Smallest/average/largest free records: %zu/%zu/%zu\n" \
30         "Number of hash chains: %zu\n" \
31         "Smallest/average/largest hash chains: %zu/%zu/%zu\n" \
32         "Number of uncoalesced records: %zu\n" \
33         "Smallest/average/largest uncoalesced runs: %zu/%zu/%zu\n" \
34         "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: %.0f/%.0f/%.0f/%.0f/%.0f/%.0f/%.0f\n"
35
36 /* We don't use tally module, to keep upstream happy. */
37 struct tally {
38         size_t min, max, total;
39         size_t num;
40 };
41
42 static void tally1_init(struct tally *tally)
43 {
44         tally->total = 0;
45         tally->num = 0;
46         tally->min = tally->max = 0;
47 }
48
49 static void tally1_add(struct tally *tally, size_t len)
50 {
51         if (tally->num == 0)
52                 tally->max = tally->min = len;
53         else if (len > tally->max)
54                 tally->max = len;
55         else if (len < tally->min)
56                 tally->min = len;
57         tally->num++;
58         tally->total += len;
59 }
60
61 static size_t tally1_mean(const struct tally *tally)
62 {
63         if (!tally->num)
64                 return 0;
65         return tally->total / tally->num;
66 }
67
68 static size_t get_hash_length(struct tdb1_context *tdb, unsigned int i)
69 {
70         tdb1_off_t rec_ptr;
71         size_t count = 0;
72
73         if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(i), &rec_ptr) == -1)
74                 return 0;
75
76         /* keep looking until we find the right record */
77         while (rec_ptr) {
78                 struct tdb1_record r;
79                 ++count;
80                 if (tdb1_rec_read(tdb, rec_ptr, &r) == -1)
81                         return 0;
82                 rec_ptr = r.next;
83         }
84         return count;
85 }
86
87 _PUBLIC_ char *tdb1_summary(struct tdb1_context *tdb)
88 {
89         tdb1_off_t off, rec_off;
90         struct tally freet, keys, data, dead, extra, hash, uncoal;
91         struct tdb1_record rec;
92         char *ret = NULL;
93         bool locked;
94         size_t len, unc = 0;
95         struct tdb1_record recovery;
96
97         /* Read-only databases use no locking at all: it's best-effort.
98          * We may have a write lock already, so skip that case too. */
99         if (tdb->read_only || tdb->allrecord_lock.count != 0) {
100                 locked = false;
101         } else {
102                 if (tdb1_lockall_read(tdb) == -1)
103                         return NULL;
104                 locked = true;
105         }
106
107         if (tdb1_recovery_area(tdb, tdb->methods, &rec_off, &recovery) != 0) {
108                 goto unlock;
109         }
110
111         tally1_init(&freet);
112         tally1_init(&keys);
113         tally1_init(&data);
114         tally1_init(&dead);
115         tally1_init(&extra);
116         tally1_init(&hash);
117         tally1_init(&uncoal);
118
119         for (off = TDB1_DATA_START(tdb->header.hash_size);
120              off < tdb->map_size - 1;
121              off += sizeof(rec) + rec.rec_len) {
122                 if (tdb->methods->tdb1_read(tdb, off, &rec, sizeof(rec),
123                                            TDB1_DOCONV()) == -1)
124                         goto unlock;
125                 switch (rec.magic) {
126                 case TDB1_MAGIC:
127                         tally1_add(&keys, rec.key_len);
128                         tally1_add(&data, rec.data_len);
129                         tally1_add(&extra, rec.rec_len - (rec.key_len
130                                                          + rec.data_len));
131                         if (unc > 1)
132                                 tally1_add(&uncoal, unc - 1);
133                         unc = 0;
134                         break;
135                 case TDB1_FREE_MAGIC:
136                         tally1_add(&freet, rec.rec_len);
137                         unc++;
138                         break;
139                 /* If we crash after ftruncate, we can get zeroes or fill. */
140                 case TDB1_RECOVERY_INVALID_MAGIC:
141                 case 0x42424242:
142                         unc++;
143                         /* If it's a valid recovery, we can trust rec_len. */
144                         if (off != rec_off) {
145                                 rec.rec_len = tdb1_dead_space(tdb, off)
146                                         - sizeof(rec);
147                         }
148                         /* Fall through */
149                 case TDB1_DEAD_MAGIC:
150                         tally1_add(&dead, rec.rec_len);
151                         break;
152                 default:
153                         TDB1_LOG((tdb, TDB1_DEBUG_ERROR,
154                                  "Unexpected record magic 0x%x at offset %d\n",
155                                  rec.magic, off));
156                         goto unlock;
157                 }
158         }
159         if (unc > 1)
160                 tally1_add(&uncoal, unc - 1);
161
162         for (off = 0; off < tdb->header.hash_size; off++)
163                 tally1_add(&hash, get_hash_length(tdb, off));
164
165         /* 20 is max length of a %zu. */
166         len = strlen(SUMMARY_FORMAT1) + 35*20 + 1;
167         ret = (char *)malloc(len);
168         if (!ret)
169                 goto unlock;
170
171         snprintf(ret, len, SUMMARY_FORMAT1,
172                  tdb->map_size, keys.total+data.total,
173                  keys.num,
174                  keys.min, tally1_mean(&keys), keys.max,
175                  data.min, tally1_mean(&data), data.max,
176                  extra.min, tally1_mean(&extra), extra.max,
177                  dead.num,
178                  dead.min, tally1_mean(&dead), dead.max,
179                  freet.num,
180                  freet.min, tally1_mean(&freet), freet.max,
181                  hash.num,
182                  hash.min, tally1_mean(&hash), hash.max,
183                  uncoal.total,
184                  uncoal.min, tally1_mean(&uncoal), uncoal.max,
185                  keys.total * 100.0 / tdb->map_size,
186                  data.total * 100.0 / tdb->map_size,
187                  extra.total * 100.0 / tdb->map_size,
188                  freet.total * 100.0 / tdb->map_size,
189                  dead.total * 100.0 / tdb->map_size,
190                  (keys.num + freet.num + dead.num)
191                  * (sizeof(struct tdb1_record) + sizeof(uint32_t))
192                  * 100.0 / tdb->map_size,
193                  tdb->header.hash_size * sizeof(tdb1_off_t)
194                  * 100.0 / tdb->map_size);
195
196 unlock:
197         if (locked) {
198                 tdb1_unlockall_read(tdb);
199         }
200         return ret;
201 }