summaryrefslogtreecommitdiff
path: root/lib/ntdb/lock.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/lock.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/lock.c')
-rw-r--r--lib/ntdb/lock.c56
1 files changed, 25 insertions, 31 deletions
diff --git a/lib/ntdb/lock.c b/lib/ntdb/lock.c
index 95824db742..f6a811a3fa 100644
--- a/lib/ntdb/lock.c
+++ b/lib/ntdb/lock.c
@@ -26,7 +26,6 @@
*/
#include "private.h"
-#include <assert.h>
#include <ccan/build_assert/build_assert.h>
/* If we were threaded, we could wait for unlock, but we're not, so fail. */
@@ -346,12 +345,8 @@ static enum NTDB_ERROR ntdb_nest_lock(struct ntdb_context *ntdb,
struct ntdb_lock *new_lck;
enum NTDB_ERROR ecode;
- if (offset > (NTDB_HASH_LOCK_START + NTDB_HASH_LOCK_RANGE
- + ntdb->file->map_size / 8)) {
- return ntdb_logerr(ntdb, NTDB_ERR_LOCK, NTDB_LOG_ERROR,
- "ntdb_nest_lock: invalid offset %zu ltype=%d",
- (size_t)offset, ltype);
- }
+ assert(offset <= (NTDB_HASH_LOCK_START + (1 << ntdb->hash_bits)
+ + ntdb->file->map_size / 8));
if (ntdb->flags & NTDB_NOLOCK)
return NTDB_SUCCESS;
@@ -581,17 +576,17 @@ enum NTDB_ERROR ntdb_allrecord_lock(struct ntdb_context *ntdb, int ltype,
again:
/* Lock hashes, gradually. */
ecode = ntdb_lock_gradual(ntdb, ltype, flags, NTDB_HASH_LOCK_START,
- NTDB_HASH_LOCK_RANGE);
+ 1 << ntdb->hash_bits);
if (ecode != NTDB_SUCCESS)
return ecode;
/* Lock free tables: there to end of file. */
ecode = ntdb_brlock(ntdb, ltype,
- NTDB_HASH_LOCK_START + NTDB_HASH_LOCK_RANGE,
- 0, flags);
+ NTDB_HASH_LOCK_START + (1 << ntdb->hash_bits),
+ 0, flags);
if (ecode != NTDB_SUCCESS) {
ntdb_brunlock(ntdb, ltype, NTDB_HASH_LOCK_START,
- NTDB_HASH_LOCK_RANGE);
+ 1 << ntdb->hash_bits);
return ecode;
}
@@ -700,7 +695,7 @@ bool ntdb_has_hash_locks(struct ntdb_context *ntdb)
for (i=0; i<ntdb->file->num_lockrecs; i++) {
if (ntdb->file->lockrecs[i].off >= NTDB_HASH_LOCK_START
&& ntdb->file->lockrecs[i].off < (NTDB_HASH_LOCK_START
- + NTDB_HASH_LOCK_RANGE))
+ + (1 << ntdb->hash_bits)))
return true;
}
return false;
@@ -715,20 +710,19 @@ static bool ntdb_has_free_lock(struct ntdb_context *ntdb)
for (i=0; i<ntdb->file->num_lockrecs; i++) {
if (ntdb->file->lockrecs[i].off
- > NTDB_HASH_LOCK_START + NTDB_HASH_LOCK_RANGE)
+ > NTDB_HASH_LOCK_START + (1 << ntdb->hash_bits))
return true;
}
return false;
}
-enum NTDB_ERROR ntdb_lock_hashes(struct ntdb_context *ntdb,
- ntdb_off_t hash_lock,
- ntdb_len_t hash_range,
- int ltype, enum ntdb_lock_flags waitflag)
+enum NTDB_ERROR ntdb_lock_hash(struct ntdb_context *ntdb,
+ unsigned int h,
+ int ltype)
{
- /* FIXME: Do this properly, using hlock_range */
- unsigned l = NTDB_HASH_LOCK_START
- + (hash_lock >> (64 - NTDB_HASH_LOCK_RANGE_BITS));
+ unsigned l = NTDB_HASH_LOCK_START + h;
+
+ assert(h < (1 << ntdb->hash_bits));
/* a allrecord lock allows us to avoid per chain locks */
if (ntdb->file->allrecord_lock.count) {
@@ -760,15 +754,13 @@ enum NTDB_ERROR ntdb_lock_hashes(struct ntdb_context *ntdb,
" already have expansion lock");
}
- return ntdb_nest_lock(ntdb, l, ltype, waitflag);
+ return ntdb_nest_lock(ntdb, l, ltype, NTDB_LOCK_WAIT);
}
-enum NTDB_ERROR ntdb_unlock_hashes(struct ntdb_context *ntdb,
- ntdb_off_t hash_lock,
- ntdb_len_t hash_range, int ltype)
+enum NTDB_ERROR ntdb_unlock_hash(struct ntdb_context *ntdb,
+ unsigned int h, int ltype)
{
- unsigned l = NTDB_HASH_LOCK_START
- + (hash_lock >> (64 - NTDB_HASH_LOCK_RANGE_BITS));
+ unsigned l = NTDB_HASH_LOCK_START + (h & ((1 << ntdb->hash_bits)-1));
if (ntdb->flags & NTDB_NOLOCK)
return 0;
@@ -791,14 +783,15 @@ enum NTDB_ERROR ntdb_unlock_hashes(struct ntdb_context *ntdb,
return ntdb_nest_unlock(ntdb, l, ltype);
}
-/* Hash locks use NTDB_HASH_LOCK_START + the next 30 bits.
+/* Hash locks use NTDB_HASH_LOCK_START + <number of hash entries>..
* Then we begin; bucket offsets are sizeof(ntdb_len_t) apart, so we divide.
* The result is that on 32 bit systems we don't use lock values > 2^31 on
* files that are less than 4GB.
*/
-static ntdb_off_t free_lock_off(ntdb_off_t b_off)
+static ntdb_off_t free_lock_off(const struct ntdb_context *ntdb,
+ ntdb_off_t b_off)
{
- return NTDB_HASH_LOCK_START + NTDB_HASH_LOCK_RANGE
+ return NTDB_HASH_LOCK_START + (1 << ntdb->hash_bits)
+ b_off / sizeof(ntdb_off_t);
}
@@ -834,7 +827,8 @@ enum NTDB_ERROR ntdb_lock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_of
}
#endif
- return ntdb_nest_lock(ntdb, free_lock_off(b_off), F_WRLCK, waitflag);
+ return ntdb_nest_lock(ntdb, free_lock_off(ntdb, b_off), F_WRLCK,
+ waitflag);
}
void ntdb_unlock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off)
@@ -842,7 +836,7 @@ void ntdb_unlock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off)
if (ntdb->file->allrecord_lock.count)
return;
- ntdb_nest_unlock(ntdb, free_lock_off(b_off), F_WRLCK);
+ ntdb_nest_unlock(ntdb, free_lock_off(ntdb, b_off), F_WRLCK);
}
_PUBLIC_ enum NTDB_ERROR ntdb_lockall(struct ntdb_context *ntdb)