2 Trivial Database 2: free list/block handling
3 Copyright (C) Rusty Russell 2010
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.
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.
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/>.
19 #include <ccan/likely/likely.h>
24 static unsigned fls64(uint64_t val)
27 if (val <= ULONG_MAX) {
28 /* This is significantly faster! */
29 return val ? sizeof(long) * CHAR_BIT - __builtin_clzl(val) : 0;
36 if (!(val & 0xffffffff00000000ull)) {
40 if (!(val & 0xffff000000000000ull)) {
44 if (!(val & 0xff00000000000000ull)) {
48 if (!(val & 0xf000000000000000ull)) {
52 if (!(val & 0xc000000000000000ull)) {
56 if (!(val & 0x8000000000000000ull)) {
66 /* In which bucket would we find a particular record size? (ignoring header) */
67 unsigned int size_to_bucket(unsigned int zone_bits, tdb_len_t data_len)
71 /* We can't have records smaller than this. */
72 assert(data_len >= TDB_MIN_DATA_LEN);
74 /* Ignoring the header... */
75 if (data_len - TDB_MIN_DATA_LEN <= 64) {
76 /* 0 in bucket 0, 8 in bucket 1... 64 in bucket 8. */
77 bucket = (data_len - TDB_MIN_DATA_LEN) / 8;
79 /* After that we go power of 2. */
80 bucket = fls64(data_len - TDB_MIN_DATA_LEN) + 2;
83 if (unlikely(bucket > BUCKETS_FOR_ZONE(zone_bits)))
84 bucket = BUCKETS_FOR_ZONE(zone_bits);
88 /* Subtract 1-byte tailer and header. Then round up to next power of 2. */
89 static unsigned max_zone_bits(struct tdb_context *tdb)
91 return fls64(tdb->map_size-1-sizeof(struct tdb_header)-1) + 1;
94 /* Start by using a random zone to spread the load: returns the offset. */
95 static uint64_t random_zone(struct tdb_context *tdb)
97 struct free_zone_header zhdr;
98 tdb_off_t off = sizeof(struct tdb_header);
100 uint64_t randbits = 0;
103 for (i = 0; i < 64; i += fls64(RAND_MAX))
104 randbits ^= ((uint64_t)random()) << i;
106 /* FIXME: Does this work? Test! */
107 half_bits = max_zone_bits(tdb) - 1;
109 /* Pick left or right side (not outside file) */
111 && !tdb->methods->oob(tdb, off + (1ULL << half_bits)
112 + sizeof(zhdr), true)) {
113 off += 1ULL << half_bits;
117 if (tdb_read_convert(tdb, off, &zhdr, sizeof(zhdr)) == -1)
120 if (zhdr.zone_bits == half_bits)
124 } while (half_bits >= INITIAL_ZONE_BITS);
126 tdb->ecode = TDB_ERR_CORRUPT;
127 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
128 "random_zone: zone at %llu smaller than %u bits?",
129 (long long)off, INITIAL_ZONE_BITS);
133 int tdb_zone_init(struct tdb_context *tdb)
135 tdb->zone_off = random_zone(tdb);
136 if (tdb->zone_off == TDB_OFF_ERR)
138 if (tdb_read_convert(tdb, tdb->zone_off,
139 &tdb->zhdr, sizeof(tdb->zhdr)) == -1)
144 /* Where's the header, given a zone size of 1 << zone_bits? */
145 static tdb_off_t zone_off(tdb_off_t off, unsigned int zone_bits)
147 off -= sizeof(struct tdb_header);
148 return (off & ~((1ULL << zone_bits) - 1)) + sizeof(struct tdb_header);
151 /* Offset of a given bucket. */
152 /* FIXME: bucket can be "unsigned" everywhere, or even uint8/16. */
153 tdb_off_t bucket_off(tdb_off_t zone_off, tdb_off_t bucket)
156 + sizeof(struct free_zone_header)
157 + bucket * sizeof(tdb_off_t);
160 /* Returns free_buckets + 1, or list number to search. */
161 static tdb_off_t find_free_head(struct tdb_context *tdb, tdb_off_t bucket)
163 /* Speculatively search for a non-zero bucket. */
164 return tdb_find_nonzero_off(tdb, bucket_off(tdb->zone_off, 0),
166 BUCKETS_FOR_ZONE(tdb->zhdr.zone_bits) + 1);
169 /* Remove from free bucket. */
170 static int remove_from_list(struct tdb_context *tdb,
171 tdb_off_t b_off, struct tdb_free_record *r)
179 off = r->prev + offsetof(struct tdb_free_record, next);
181 /* r->prev->next = r->next */
182 if (tdb_write_off(tdb, off, r->next)) {
187 off = r->next + offsetof(struct tdb_free_record, prev);
188 /* r->next->prev = r->prev */
189 if (tdb_write_off(tdb, off, r->prev)) {
196 /* Enqueue in this free bucket. */
197 static int enqueue_in_free(struct tdb_context *tdb,
200 struct tdb_free_record *new)
203 /* new->next = head. */
204 new->next = tdb_read_off(tdb, b_off);
205 if (new->next == TDB_OFF_ERR)
209 /* next->prev = new. */
210 if (tdb_write_off(tdb, new->next
211 + offsetof(struct tdb_free_record, prev),
216 if (tdb_write_off(tdb, b_off, off) != 0)
219 return tdb_write_convert(tdb, off, new, sizeof(*new));
222 /* List need not be locked. */
223 int add_free_record(struct tdb_context *tdb,
224 unsigned int zone_bits,
225 tdb_off_t off, tdb_len_t len_with_header)
227 struct tdb_free_record new;
231 assert(len_with_header >= sizeof(new));
232 assert(zone_bits < (1 << 6));
234 new.magic_and_meta = TDB_FREE_MAGIC | zone_bits;
235 new.data_len = len_with_header - sizeof(struct tdb_used_record);
237 b_off = bucket_off(zone_off(off, zone_bits),
238 size_to_bucket(zone_bits, new.data_len));
239 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) != 0)
242 ret = enqueue_in_free(tdb, b_off, off, &new);
243 tdb_unlock_free_bucket(tdb, b_off);
247 /* If we have enough left over to be useful, split that off. */
248 static int to_used_record(struct tdb_context *tdb,
249 unsigned int zone_bits,
255 struct tdb_used_record used;
258 leftover = total_len - needed;
259 if (leftover < sizeof(struct tdb_free_record))
262 *actual = total_len - leftover;
265 if (add_free_record(tdb, zone_bits,
266 off + sizeof(used) + *actual,
273 /* Note: we unlock the current bucket if we coalesce or fail. */
274 static int coalesce(struct tdb_context *tdb,
275 tdb_off_t zone_off, unsigned zone_bits,
276 tdb_off_t off, tdb_off_t b_off, tdb_len_t data_len)
278 struct tdb_free_record pad, *r;
279 tdb_off_t end = off + sizeof(struct tdb_used_record) + data_len;
281 while (end < (zone_off + (1ULL << zone_bits))) {
284 /* FIXME: do tdb_get here and below really win? */
285 r = tdb_get(tdb, end, &pad, sizeof(pad));
289 if (frec_magic(r) != TDB_FREE_MAGIC)
292 nb_off = bucket_off(zone_off,
293 size_to_bucket(zone_bits, r->data_len));
295 /* We may be violating lock order here, so best effort. */
296 if (tdb_lock_free_bucket(tdb, nb_off, TDB_LOCK_NOWAIT) == -1)
299 /* Now we have lock, re-check. */
300 r = tdb_get(tdb, end, &pad, sizeof(pad));
302 tdb_unlock_free_bucket(tdb, nb_off);
306 if (unlikely(frec_magic(r) != TDB_FREE_MAGIC)) {
307 tdb_unlock_free_bucket(tdb, nb_off);
311 if (remove_from_list(tdb, nb_off, r) == -1) {
312 tdb_unlock_free_bucket(tdb, nb_off);
316 end += sizeof(struct tdb_used_record) + r->data_len;
317 tdb_unlock_free_bucket(tdb, nb_off);
320 /* Didn't find any adjacent free? */
321 if (end == off + sizeof(struct tdb_used_record) + data_len)
324 /* OK, expand record */
325 r = tdb_get(tdb, off, &pad, sizeof(pad));
329 if (r->data_len != data_len) {
330 tdb->ecode = TDB_ERR_CORRUPT;
331 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
332 "coalesce: expected data len %llu not %llu\n",
333 (long long)data_len, (long long)r->data_len);
337 if (remove_from_list(tdb, b_off, r) == -1)
340 /* We have to drop this to avoid deadlocks. */
341 tdb_unlock_free_bucket(tdb, b_off);
343 if (add_free_record(tdb, zone_bits, off, end - off) == -1)
348 /* To unify error paths, we *always* unlock bucket on error. */
349 tdb_unlock_free_bucket(tdb, b_off);
353 /* We need size bytes to put our key and data in. */
354 static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
361 tdb_off_t off, b_off,best_off;
362 struct tdb_free_record pad, best = { 0 }, *r;
366 b_off = bucket_off(zone_off, bucket);
368 /* FIXME: Try non-blocking wait first, to measure contention.
369 * If we're contented, try switching zones, and don't enlarge zone
370 * next time (we want more zones). */
371 /* Lock this bucket. */
372 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == -1) {
376 best.data_len = -1ULL;
378 /* FIXME: Start with larger multiplier if we're growing. */
381 /* Walk the list to see if any are large enough, getting less fussy
383 off = tdb_read_off(tdb, b_off);
384 if (unlikely(off == TDB_OFF_ERR))
388 /* FIXME: Does tdb_get win anything here? */
389 r = tdb_get(tdb, off, &pad, sizeof(*r));
393 if (frec_magic(r) != TDB_FREE_MAGIC) {
394 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
395 "lock_and_alloc: %llu non-free 0x%llx\n",
396 (long long)off, (long long)r->magic_and_meta);
400 if (r->data_len >= size && r->data_len < best.data_len) {
405 if (best.data_len < size * multiplier && best_off)
410 /* Since we're going slow anyway, try coalescing here. */
411 switch (coalesce(tdb, zone_off, zone_bits, off, b_off,
414 /* This has already unlocked on error. */
417 /* This has unlocked list, restart. */
423 /* If we found anything at all, use it. */
426 /* We're happy with this size: take it. */
427 if (remove_from_list(tdb, b_off, &best) != 0)
429 tdb_unlock_free_bucket(tdb, b_off);
431 if (to_used_record(tdb, zone_bits, best_off, size,
432 best.data_len, actual)) {
438 tdb_unlock_free_bucket(tdb, b_off);
442 tdb_unlock_free_bucket(tdb, b_off);
446 static bool next_zone(struct tdb_context *tdb)
448 tdb_off_t next = tdb->zone_off + (1ULL << tdb->zhdr.zone_bits);
450 /* We must have a header. */
451 if (tdb->methods->oob(tdb, next + sizeof(tdb->zhdr), true))
454 tdb->zone_off = next;
455 return tdb_read_convert(tdb, next, &tdb->zhdr, sizeof(tdb->zhdr)) == 0;
458 /* Offset returned is within current zone (which it may alter). */
459 static tdb_off_t get_free(struct tdb_context *tdb, size_t size,
462 tdb_off_t start_zone = tdb->zone_off, off;
463 bool wrapped = false;
465 /* FIXME: If we don't get a hit in the first bucket we want,
466 * try changing zones for next time. That should help wear
467 * zones evenly, so we don't need to search all of them before
469 while (!wrapped || tdb->zone_off != start_zone) {
472 /* Shortcut for really huge allocations... */
473 if ((size >> tdb->zhdr.zone_bits) != 0)
476 /* Start at exact size bucket, and search up... */
477 b = size_to_bucket(tdb->zhdr.zone_bits, size);
478 for (b = find_free_head(tdb, b);
479 b <= BUCKETS_FOR_ZONE(tdb->zhdr.zone_bits);
480 b += find_free_head(tdb, b + 1)) {
481 /* Try getting one from list. */
482 off = lock_and_alloc(tdb, tdb->zone_off,
485 if (off == TDB_OFF_ERR)
489 /* Didn't work. Try next bucket. */
492 /* Didn't work, try next zone, if it exists. */
493 if (!next_zone(tdb)) {
495 tdb->zone_off = sizeof(struct tdb_header);
496 if (tdb_read_convert(tdb, tdb->zone_off,
497 &tdb->zhdr, sizeof(tdb->zhdr))) {
505 int set_header(struct tdb_context *tdb,
506 struct tdb_used_record *rec,
507 uint64_t keylen, uint64_t datalen,
508 uint64_t actuallen, uint64_t hash,
509 unsigned int zone_bits)
511 uint64_t keybits = (fls64(keylen) + 1) / 2;
513 /* Use bottom bits of hash, so it's independent of hash table size. */
516 | ((hash & ((1 << 5)-1)) << 6)
517 | ((actuallen - (keylen + datalen)) << 11)
520 rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
522 /* Encoding can fail on big values. */
523 if (rec_key_length(rec) != keylen
524 || rec_data_length(rec) != datalen
525 || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
526 tdb->ecode = TDB_ERR_IO;
527 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
528 "Could not encode k=%llu,d=%llu,a=%llu\n",
529 (long long)keylen, (long long)datalen,
530 (long long)actuallen);
536 static bool zones_happy(struct tdb_context *tdb)
538 /* FIXME: look at distribution of zones. */
542 /* Assume we want buckets up to the comfort factor. */
543 static tdb_len_t overhead(unsigned int zone_bits)
545 return sizeof(struct free_zone_header)
546 + (BUCKETS_FOR_ZONE(zone_bits) + 1) * sizeof(tdb_off_t);
549 /* Expand the database (by adding a zone). */
550 static int tdb_expand(struct tdb_context *tdb, tdb_len_t size)
555 unsigned int num_buckets;
557 struct free_zone_header zhdr;
560 /* We need room for the record header too. */
561 wanted = sizeof(struct tdb_used_record) + size;
563 /* Only one person can expand file at a time. */
564 if (tdb_lock_expand(tdb, F_WRLCK) != 0)
567 /* Someone else may have expanded the file, so retry. */
568 old_size = tdb->map_size;
569 tdb->methods->oob(tdb, tdb->map_size + 1, true);
570 if (tdb->map_size != old_size)
573 /* FIXME: Tailer is a bogus optimization, remove it. */
574 /* zone bits tailer char is protected by EXPAND lock. */
575 if (tdb->methods->read(tdb, old_size - 1, &zone_bits, 1) == -1)
578 /* If zones aren't working well, add larger zone if possible. */
579 enlarge_zone = !zones_happy(tdb);
581 /* New zone can be between zone_bits or larger if we're on the right
584 /* Does this fit the allocation comfortably? */
585 if ((1ULL << zone_bits) >= overhead(zone_bits) + wanted) {
586 /* Only let enlarge_zone enlarge us once. */
589 enlarge_zone = false;
591 if ((old_size - 1 - sizeof(struct tdb_header))
597 zhdr.zone_bits = zone_bits;
598 num_buckets = BUCKETS_FOR_ZONE(zone_bits);
600 /* FIXME: I don't think we need to expand to full zone, do we? */
601 if (tdb->methods->expand_file(tdb, 1ULL << zone_bits) == -1)
604 /* Write new tailer. */
605 if (tdb->methods->write(tdb, tdb->map_size - 1, &zone_bits, 1) == -1)
608 /* Write new zone header (just before old tailer). */
610 if (tdb_write_convert(tdb, off, &zhdr, sizeof(zhdr)) == -1)
613 /* Now write empty buckets. */
615 if (zero_out(tdb, off, (num_buckets+1) * sizeof(tdb_off_t)) == -1)
617 off += (num_buckets+1) * sizeof(tdb_off_t);
619 /* Now add the rest as our free record. */
620 if (add_free_record(tdb, zone_bits, off, tdb->map_size-1-off) == -1)
623 /* Try allocating from this zone now. */
624 tdb->zone_off = old_size - 1;
628 tdb_unlock_expand(tdb, F_WRLCK);
632 tdb_unlock_expand(tdb, F_WRLCK);
636 static tdb_len_t adjust_size(size_t keylen, size_t datalen, bool growing)
638 tdb_len_t size = keylen + datalen;
640 if (size < TDB_MIN_DATA_LEN)
641 size = TDB_MIN_DATA_LEN;
643 /* Overallocate if this is coming from an enlarging store. */
647 /* Round to next uint64_t boundary. */
648 return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
651 /* This won't fail: it will expand the database if it has to. */
652 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
653 uint64_t hash, bool growing)
656 tdb_len_t size, actual;
657 struct tdb_used_record rec;
659 /* We can't hold pointers during this: we could unmap! */
660 assert(!tdb->direct_access);
662 size = adjust_size(keylen, datalen, growing);
665 off = get_free(tdb, size, &actual);
666 if (unlikely(off == TDB_OFF_ERR))
669 if (unlikely(off == 0)) {
670 if (tdb_expand(tdb, size) == -1)
675 /* Some supergiant values can't be encoded. */
676 /* FIXME: Check before, and limit actual in get_free. */
677 if (set_header(tdb, &rec, keylen, datalen, actual, hash,
678 tdb->zhdr.zone_bits) != 0) {
679 add_free_record(tdb, tdb->zhdr.zone_bits, off,
680 sizeof(rec) + actual);
684 if (tdb_write_convert(tdb, off, &rec, sizeof(rec)) != 0)