From d51feaac43c2c12d60dc67f73dd18bdad2454123 Mon Sep 17 00:00:00 2001 From: Justus Winter <4winter@informatik.uni-hamburg.de> Date: Wed, 24 Jun 2015 02:30:01 +0200 Subject: [PATCH hurd] libdiskfs: use ihash for the node cache Replace the hand-written hash table in the node cache with libihash. Libihash is a self-tuning hash table, whereas the previous code used a fixed number of buckets. * libdiskfs/Makefile (HURDLIBS): Link to `ihash'. * libdiskfs/diskfs.h (struct node): Remove bucket list, add slot pointer. * libdiskfs/node-cache.c (nodecache): New ihash table replacing the old `nodehash'. (lookup): Drop function. (diskfs_cached_lookup_context): Adapt accordingly. (diskfs_cached_ifind): Likewise. (diskfs_try_dropping_softrefs): Likewise. (diskfs_node_iterate): Likewise. --- libdiskfs/Makefile | 2 +- libdiskfs/diskfs.h | 5 ++-- libdiskfs/node-cache.c | 80 +++++++++++++++++--------------------------------- 3 files changed, 31 insertions(+), 56 deletions(-) diff --git a/libdiskfs/Makefile b/libdiskfs/Makefile index 47b9339..803761d 100644 --- a/libdiskfs/Makefile +++ b/libdiskfs/Makefile @@ -61,7 +61,7 @@ MIGSTUBS = fsServer.o ioServer.o fsysServer.o exec_startupServer.o \ startup_notifyServer.o OBJS = $(sort $(SRCS:.c=.o) $(MIGSTUBS)) -HURDLIBS = fshelp iohelp store ports shouldbeinlibc pager +HURDLIBS = fshelp iohelp store ports shouldbeinlibc pager ihash LDLIBS += -lpthread fsys-MIGSFLAGS = -imacros $(srcdir)/fsmutations.h -DREPLY_PORTS diff --git a/libdiskfs/diskfs.h b/libdiskfs/diskfs.h index 11fb0ad..106aeb0 100644 --- a/libdiskfs/diskfs.h +++ b/libdiskfs/diskfs.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -80,8 +81,8 @@ struct peropen filesystem. */ struct node { - /* Links on hash list. */ - struct node *hnext, **hprevp; + /* The slot we occupy in the node cache. */ + hurd_ihash_locp_t slot; struct disknode *dn; diff --git a/libdiskfs/node-cache.c b/libdiskfs/node-cache.c index a76474a..07cd879 100644 --- a/libdiskfs/node-cache.c +++ b/libdiskfs/node-cache.c @@ -17,15 +17,10 @@ You should have received a copy of the GNU General Public License along with the GNU Hurd. If not, see . */ -#include "priv.h" - -#define INOHSZ 8192 -#if ((INOHSZ&(INOHSZ-1)) == 0) -#define INOHASH(ino) ((ino)&(INOHSZ-1)) -#else -#define INOHASH(ino) (((unsigned)(ino))%INOHSZ) -#endif +#include +#include "priv.h" +//XXX /* The nodehash is a cache of nodes. Access to nodehash and nodehash_nr_items is protected by @@ -35,8 +30,8 @@ asked to give up that light reference, we reacquire our lock momentarily to check whether someone else reacquired a reference through the nodehash. */ -static struct node *nodehash[INOHSZ]; -static size_t nodehash_nr_items; +static struct hurd_ihash nodecache = + HURD_IHASH_INITIALIZER (offsetof (struct node, slot)); static pthread_rwlock_t nodecache_lock = PTHREAD_RWLOCK_INITIALIZER; /* Initialize the inode hash table. */ @@ -45,18 +40,6 @@ nodecache_init () { } -/* Lookup node with inode number INUM. Returns NULL if the node is - not found in the node cache. */ -static struct node * -lookup (ino_t inum) -{ - struct node *np; - for (np = nodehash[INOHASH(inum)]; np; np = np->hnext) - if (np->cache_id == inum) - return np; - return NULL; -} - /* Fetch inode INUM, set *NPP to the node structure; gain one user reference and lock the node. */ error_t __attribute__ ((weak)) @@ -72,10 +55,12 @@ diskfs_cached_lookup_context (ino_t inum, struct node **npp, struct lookup_context *ctx) { error_t err; - struct node *np, *tmp; + struct node *np; + hurd_ihash_locp_t slot; pthread_rwlock_rdlock (&nodecache_lock); - np = lookup (inum); + slot = hurd_ihash_locp_find (&nodecache, inum); + np = hurd_ihash_locp_value (slot); if (np) goto gotit; pthread_rwlock_unlock (&nodecache_lock); @@ -89,22 +74,18 @@ diskfs_cached_lookup_context (ino_t inum, struct node **npp, /* Put NP in NODEHASH. */ pthread_rwlock_wrlock (&nodecache_lock); - tmp = lookup (inum); - if (tmp) + slot = hurd_ihash_locp_find (&nodecache, inum); + if (hurd_ihash_locp_value (slot)) { /* We lost a race. */ diskfs_nput (np); - np = tmp; + np = hurd_ihash_locp_value (slot); goto gotit; } - np->hnext = nodehash[INOHASH(inum)]; - if (np->hnext) - np->hnext->hprevp = &np->hnext; - np->hprevp = &nodehash[INOHASH(inum)]; - nodehash[INOHASH(inum)] = np; + err = hurd_ihash_locp_add (&nodecache, slot, inum, np); + assert_perror (err); diskfs_nref_light (np); - nodehash_nr_items += 1; pthread_rwlock_unlock (&nodecache_lock); /* Get the contents of NP off disk. */ @@ -133,7 +114,7 @@ diskfs_cached_ifind (ino_t inum) struct node *np; pthread_rwlock_rdlock (&nodecache_lock); - np = lookup (inum); + np = hurd_ihash_find (&nodecache, inum); pthread_rwlock_unlock (&nodecache_lock); assert (np); @@ -144,7 +125,7 @@ void __attribute__ ((weak)) diskfs_try_dropping_softrefs (struct node *np) { pthread_rwlock_wrlock (&nodecache_lock); - if (np->hprevp != NULL) + if (np->slot != NULL) { /* Check if someone reacquired a reference through the nodehash. */ @@ -159,12 +140,8 @@ diskfs_try_dropping_softrefs (struct node *np) return; } - *np->hprevp = np->hnext; - if (np->hnext) - np->hnext->hprevp = np->hprevp; - np->hnext = NULL; - np->hprevp = NULL; - nodehash_nr_items -= 1; + hurd_ihash_locp_remove (&nodecache, np->slot); + np->slot = NULL; diskfs_nrele_light (np); } pthread_rwlock_unlock (&nodecache_lock); @@ -179,7 +156,6 @@ error_t __attribute__ ((weak)) diskfs_node_iterate (error_t (*fun)(struct node *)) { error_t err = 0; - int n; size_t num_nodes; struct node *node, **node_list, **p; @@ -191,7 +167,7 @@ diskfs_node_iterate (error_t (*fun)(struct node *)) nodecache_lock, but we can't hold this while locking the individual node locks). */ /* XXX: Can we? */ - num_nodes = nodehash_nr_items; + num_nodes = nodecache.nr_items; /* TODO This method doesn't scale beyond a few dozen nodes and should be replaced. */ @@ -203,17 +179,15 @@ diskfs_node_iterate (error_t (*fun)(struct node *)) } p = node_list; - for (n = 0; n < INOHSZ; n++) - for (node = nodehash[n]; node; node = node->hnext) - { - *p++ = node; - - /* We acquire a hard reference for node, but without using - diskfs_nref. We do this so that diskfs_new_hardrefs will not - get called. */ - refcounts_ref (&node->refcounts, NULL); - } + HURD_IHASH_ITERATE (&nodecache, i) + { + *p++ = node = i; + /* We acquire a hard reference for node, but without using + diskfs_nref. We do this so that diskfs_new_hardrefs will not + get called. */ + refcounts_ref (&node->refcounts, NULL); + } pthread_rwlock_unlock (&nodecache_lock); p = node_list; -- 2.1.4