summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/helprun-layout.c
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-06-19 12:43:04 +0930
committerRusty Russell <rusty@rustcorp.com.au>2012-06-19 05:38:07 +0200
commitdd42962878ab7c9ddfa79d7c32094fb6748017b8 (patch)
treea614af427c5ad0d962db77a58f133cb39c9bd057 /lib/ntdb/test/helprun-layout.c
parentf986554b1e38d8dd40b4bf4748d4aeb470e27d2e (diff)
downloadsamba-dd42962878ab7c9ddfa79d7c32094fb6748017b8.tar.gz
samba-dd42962878ab7c9ddfa79d7c32094fb6748017b8.tar.bz2
samba-dd42962878ab7c9ddfa79d7c32094fb6748017b8.zip
ntdb: remove hash table trees.
TDB2 started with a top-level hash of 1024 entries, divided into 128 groups of 8 buckets. When a bucket filled, the 8 bucket group expanded into pointers into 8 new 64-entry hash tables. When these filled, they expanded in turn, etc. It's a nice idea to automatically expand the hash tables, but it doesn't pay off. Remove it for NTDB. 1) It only beats TDB performance when the database is huge and the TDB hashsize is small. We are about 20% slower on medium-size databases (1000 to 10000 records), worse on really small ones. 2) Since we're 64 bits, our hash tables are already twice as expensive as TDB. 3) Since our hash function is good, it means that all groups tend to fill at the same time, meaning the hash enlarges by a factor of 128 all at once, leading to a very large database at that point. 4) Our efficiency would improve if we enlarged the top level, but that makes our minimum db size even worse: it's already over 8k, and jumps to 1M after about 1000 entries! 5) Making the sub group size larger gives a shallower tree, which performs better, but makes the "hash explosion" problem worse. 6) The code is complicated, having to handle delete and reshuffling groups of hash buckets, and expansion of buckets. 7) We have to handle the case where all the records somehow end up with the same hash value, which requires special code to chain records for that case. On the other hand, it would be nice if we didn't degrade as badly as TDB does when the hash chains get long. This patch removes the hash-growing code, but instead of chaining like TDB does when a bucket fills, we point the bucket to an array of record pointers. Since each on-disk NTDB pointer contains some hash bits from the record (we steal the upper 8 bits of the offset), 99.5% of the time we don't need to load the record to determine if it matches. This makes an array of offsets much more cache-friendly than a linked list. Here are the times (in ns) for tdb_store of N records, tdb_store of N records the second time, and a fetch of all N records. I've also included the final database size and the smbtorture local.[n]tdb_speed results. Benchmark details: 1) Compiled with -O2. 2) assert() was disabled in TDB2 and NTDB. 3) The "optimize fetch" patch was applied to NTDB. 10 runs, using tmpfs (otherwise massive swapping as db hits ~30M, despite plenty of RAM). Insert Re-ins Fetch Size dbspeed (nsec) (nsec) (nsec) (Kb) (ops/sec) TDB (10000 hashsize): 100 records: 3882 3320 1609 53 203204 1000 records: 3651 3281 1571 115 218021 10000 records: 3404 3326 1595 880 202874 100000 records: 4317 3825 2097 8262 126811 1000000 records: 11568 11578 9320 77005 25046 TDB2 (1024 hashsize, expandable): 100 records: 3867 3329 1699 17 187100 1000 records: 4040 3249 1639 154 186255 10000 records: 4143 3300 1695 1226 185110 100000 records: 4481 3425 1800 17848 163483 1000000 records: 4055 3534 1878 106386 160774 NTDB (8192 hashsize) 100 records: 4259 3376 1692 82 190852 1000 records: 3640 3275 1566 130 195106 10000 records: 4337 3438 1614 773 188362 100000 records: 4750 5165 1746 9001 169197 1000000 records: 4897 5180 2341 83838 121901 Analysis: 1) TDB wins on small databases, beating TDB2 by ~15%, NTDB by ~10%. 2) TDB starts to lose when hash chains get 10 long (fetch 10% slower than TDB2/NTDB). 3) TDB does horribly when hash chains get 100 long (fetch 4x slower than NTDB, 5x slower than TDB2, insert about 2-3x slower). 4) TDB2 databases are 40% larger than TDB1. NTDB is about 15% larger than TDB1
Diffstat (limited to 'lib/ntdb/test/helprun-layout.c')
-rw-r--r--lib/ntdb/test/helprun-layout.c102
1 files changed, 31 insertions, 71 deletions
diff --git a/lib/ntdb/test/helprun-layout.c b/lib/ntdb/test/helprun-layout.c
index 7f1f9f9d8e..fa6fa29fce 100644
--- a/lib/ntdb/test/helprun-layout.c
+++ b/lib/ntdb/test/helprun-layout.c
@@ -94,13 +94,6 @@ static ntdb_len_t data_record_len(struct tle_used *used)
return len;
}
-static ntdb_len_t hashtable_len(struct tle_hashtable *htable)
-{
- return sizeof(struct ntdb_used_record)
- + (sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS)
- + htable->extra;
-}
-
static ntdb_len_t capability_len(struct tle_capability *cap)
{
return sizeof(struct ntdb_capability) + cap->extra;
@@ -128,25 +121,13 @@ static void set_data_record(void *mem, struct ntdb_context *ntdb,
struct ntdb_used_record *u = mem;
set_header(ntdb, u, NTDB_USED_MAGIC, used->key.dsize, used->data.dsize,
- used->key.dsize + used->data.dsize + used->extra,
- ntdb_hash(ntdb, used->key.dptr, used->key.dsize));
+ used->key.dsize + used->data.dsize + used->extra);
memcpy(u + 1, used->key.dptr, used->key.dsize);
memcpy((char *)(u + 1) + used->key.dsize,
used->data.dptr, used->data.dsize);
add_zero_pad(u, used->key.dsize + used->data.dsize, used->extra);
}
-static void set_hashtable(void *mem, struct ntdb_context *ntdb,
- struct tle_hashtable *htable)
-{
- struct ntdb_used_record *u = mem;
- ntdb_len_t len = sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS;
-
- set_header(ntdb, u, NTDB_HTABLE_MAGIC, 0, len, len + htable->extra, 0);
- memset(u + 1, 0, len);
- add_zero_pad(u, len, htable->extra);
-}
-
static void set_capability(void *mem, struct ntdb_context *ntdb,
struct tle_capability *cap, struct ntdb_header *hdr,
ntdb_off_t last_cap)
@@ -156,7 +137,7 @@ static void set_capability(void *mem, struct ntdb_context *ntdb,
c->type = cap->type;
c->next = 0;
- set_header(ntdb, &c->hdr, NTDB_CAP_MAGIC, 0, len, len, 0);
+ set_header(ntdb, &c->hdr, NTDB_CAP_MAGIC, 0, len, len);
/* Append to capability list. */
if (!last_cap) {
@@ -175,7 +156,7 @@ static void set_freetable(void *mem, struct ntdb_context *ntdb,
memset(ftable, 0, sizeof(*ftable));
set_header(ntdb, &ftable->hdr, NTDB_FTABLE_MAGIC, 0,
sizeof(*ftable) - sizeof(ftable->hdr),
- sizeof(*ftable) - sizeof(ftable->hdr), 0);
+ sizeof(*ftable) - sizeof(ftable->hdr));
if (last_ftable) {
ftable = (struct ntdb_freetable *)((char *)hdr + last_ftable);
@@ -197,12 +178,6 @@ static void add_to_freetable(struct ntdb_context *ntdb,
NTDB_LOCK_WAIT, false);
}
-static ntdb_off_t hbucket_off(ntdb_off_t group_start, unsigned ingroup)
-{
- return group_start
- + (ingroup % (1 << NTDB_HASH_GROUP_BITS)) * sizeof(ntdb_off_t);
-}
-
/* Get bits from a value. */
static uint32_t bits(uint64_t val, unsigned start, unsigned num)
{
@@ -210,22 +185,24 @@ static uint32_t bits(uint64_t val, unsigned start, unsigned num)
return (val >> start) & ((1U << num) - 1);
}
-/* We take bits from the top: that way we can lock whole sections of the hash
- * by using lock ranges. */
-static uint32_t use_bits(uint64_t h, unsigned num, unsigned *used)
+static ntdb_off_t encode_offset(const struct ntdb_context *ntdb,
+ ntdb_off_t new_off, uint32_t hash)
{
- *used += num;
- return bits(h, 64 - *used, num);
+ ntdb_off_t extra;
+
+ assert((new_off & (1ULL << NTDB_OFF_CHAIN_BIT)) == 0);
+ assert((new_off >> (64 - NTDB_OFF_UPPER_STEAL)) == 0);
+ /* We pack extra hash bits into the upper bits of the offset. */
+ extra = bits(hash, ntdb->hash_bits, NTDB_OFF_UPPER_STEAL);
+ extra <<= (64 - NTDB_OFF_UPPER_STEAL);
+
+ return new_off | extra;
}
-static ntdb_off_t encode_offset(ntdb_off_t new_off, unsigned bucket,
- uint64_t h)
+static ntdb_off_t hbucket_off(ntdb_len_t idx)
{
- return bucket
- | new_off
- | ((uint64_t)bits(h, 64 - NTDB_OFF_UPPER_STEAL_EXTRA,
- NTDB_OFF_UPPER_STEAL_EXTRA)
- << NTDB_OFF_HASH_EXTRA_BIT);
+ return sizeof(struct ntdb_header) + sizeof(struct ntdb_used_record)
+ + idx * sizeof(ntdb_off_t);
}
/* FIXME: Our hash table handling here is primitive: we don't expand! */
@@ -233,28 +210,14 @@ static void add_to_hashtable(struct ntdb_context *ntdb,
ntdb_off_t eoff,
NTDB_DATA key)
{
- uint64_t h = ntdb_hash(ntdb, key.dptr, key.dsize);
- ntdb_off_t b_off, group_start;
- unsigned i, group, in_group;
- unsigned used = 0;
+ ntdb_off_t b_off;
+ uint32_t h = ntdb_hash(ntdb, key.dptr, key.dsize);
- group = use_bits(h, NTDB_TOPLEVEL_HASH_BITS-NTDB_HASH_GROUP_BITS, &used);
- in_group = use_bits(h, NTDB_HASH_GROUP_BITS, &used);
+ b_off = hbucket_off(h & ((1 << ntdb->hash_bits)-1));
+ if (ntdb_read_off(ntdb, b_off) != 0)
+ abort();
- group_start = offsetof(struct ntdb_header, hashtable)
- + group * (sizeof(ntdb_off_t) << NTDB_HASH_GROUP_BITS);
-
- for (i = 0; i < (1 << NTDB_HASH_GROUP_BITS); i++) {
- unsigned bucket = (in_group + i) % (1 << NTDB_HASH_GROUP_BITS);
-
- b_off = hbucket_off(group_start, bucket);
- if (ntdb_read_off(ntdb, b_off) == 0) {
- ntdb_write_off(ntdb, b_off,
- encode_offset(eoff, in_group, h));
- return;
- }
- }
- abort();
+ ntdb_write_off(ntdb, b_off, encode_offset(ntdb, eoff, h));
}
static struct tle_freetable *find_ftable(struct ntdb_layout *layout, unsigned num)
@@ -277,11 +240,16 @@ struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
union ntdb_attribute *attr)
{
unsigned int i;
- ntdb_off_t off, len, last_ftable, last_cap;
+ ntdb_off_t off, hdrlen, len, last_ftable, last_cap;
char *mem;
struct ntdb_context *ntdb;
- off = sizeof(struct ntdb_header);
+ /* Now populate our header, cribbing from a real NTDB header. */
+ ntdb = ntdb_open("layout", NTDB_INTERNAL, O_RDWR, 0, attr);
+
+ off = sizeof(struct ntdb_header) + sizeof(struct ntdb_used_record)
+ + (sizeof(ntdb_off_t) << ntdb->hash_bits);
+ hdrlen = off;
/* First pass of layout: calc lengths */
for (i = 0; i < layout->num_elems; i++) {
@@ -297,9 +265,6 @@ struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
case DATA:
len = data_record_len(&e->used);
break;
- case HASHTABLE:
- len = hashtable_len(&e->hashtable);
- break;
case CAPABILITY:
len = capability_len(&e->capability);
break;
@@ -312,9 +277,7 @@ struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
mem = malloc(off);
/* Fill with some weird pattern. */
memset(mem, 0x99, off);
- /* Now populate our header, cribbing from a real NTDB header. */
- ntdb = ntdb_open("layout", NTDB_INTERNAL, O_RDWR, 0, attr);
- memcpy(mem, ntdb->file->map_ptr, sizeof(struct ntdb_header));
+ memcpy(mem, ntdb->file->map_ptr, hdrlen);
/* Mug the ntdb we have to make it use this. */
freefn(ntdb->file->map_ptr);
@@ -337,9 +300,6 @@ struct ntdb_context *ntdb_layout_get(struct ntdb_layout *layout,
case DATA:
set_data_record(mem + e->base.off, ntdb, &e->used);
break;
- case HASHTABLE:
- set_hashtable(mem + e->base.off, ntdb, &e->hashtable);
- break;
case CAPABILITY:
set_capability(mem + e->base.off, ntdb, &e->capability,
(struct ntdb_header *)mem, last_cap);