summaryrefslogtreecommitdiff
path: root/debian/patches/0014-libdiskfs-use-a-hash-table-for-the-name-cache.patch
blob: ef8f720afd8eac27d3ad45543ef0094ed479da96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
From 66dce065442599ed9713b06ce5ef901e212b2d03 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Thu, 29 May 2014 02:03:03 +0200
Subject: [PATCH 14/14] libdiskfs: use a hash table for the name cache

* libdiskfs/name-cache.c: XXX.
---
 libdiskfs/name-cache.c | 344 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 247 insertions(+), 97 deletions(-)

diff --git a/libdiskfs/name-cache.c b/libdiskfs/name-cache.c
index 25b5d0d..25b230a 100644
--- a/libdiskfs/name-cache.c
+++ b/libdiskfs/name-cache.c
@@ -1,6 +1,6 @@
 /* Directory name lookup caching
 
-   Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998, 2014 Free Software Foundation, Inc.
    Written by Michael I. Bushnell, p/BSG, & Miles Bader.
 
    This file is part of the GNU Hurd.
@@ -20,118 +20,264 @@
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA. */
 
 #include "priv.h"
+#include <assert.h>
 #include <string.h>
-#include <cacheq.h>
 
-/* Maximum number of names to cache at once */
-#define MAXCACHE 200
+/* The name-cache is implemented using a hash table.
 
-/* Maximum length of file name we bother caching */
-#define CACHE_NAME_LEN 100
+   Some design choices:
 
-/* Cache entry */
-struct lookup_cache
+   XXX
+ */
+
+/* Maximum number of names to cache.  Must be a power of two. */
+#define CACHE_SIZE	256
+#define CACHE_MASK	(CACHE_SIZE - 1)
+
+#define BUCKET_SIZE	4
+
+/* Cache bucket with BUCKET_SIZE entries. */
+struct cache_bucket
 {
-  struct cacheq_hdr hdr;
+  /* Name of the node NODE_CACHE_ID in the directory DIR_CACHE_ID.  If
+     NULL, the entry is unused.  */
+  unsigned long name[BUCKET_SIZE];
 
-  /* Used to indentify nodes to the fs dependent code.  0 for NODE_CACHE_ID
-     means a `negative' entry -- recording that there's definitely no node with
-     this name.  */
-  ino64_t dir_cache_id, node_cache_id;
+  /* The key.  */
+  unsigned long key[BUCKET_SIZE];
 
-  /* Name of the node NODE_CACHE_ID in the directory DIR_CACHE_ID.  Entries
-     with names too long to fit in this buffer aren't cached at all.  */
-  char name[CACHE_NAME_LEN];
+  /* Used to indentify nodes to the fs dependent code.  */
+  ino64_t dir_cache_id[BUCKET_SIZE];
 
-  /* Strlen of NAME.  If this is zero, it's an unused entry. */
-  size_t name_len;
+  /* 0 for NODE_CACHE_ID means a `negative' entry -- recording that
+     there's definitely no node with this name.  */
+  ino64_t node_cache_id[BUCKET_SIZE];
 };
 
-/* The contents of the cache in no particular order */
-static struct cacheq lookup_cache = { sizeof (struct lookup_cache) };
+static struct cache_bucket name_cache[CACHE_SIZE];
 
-static pthread_spinlock_t cache_lock = PTHREAD_SPINLOCK_INITIALIZER;
+static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
 
-/* If there's an entry for NAME, of length NAME_LEN, in directory DIR in the
-   cache, return its entry, otherwise 0.  CACHE_LOCK must be held.  */
-static struct lookup_cache *
-find_cache (struct node *dir, const char *name, size_t name_len)
+static inline char *
+charp (unsigned long value)
 {
-  struct lookup_cache *c;
-  int i;
-
-  /* Search the list.  All unused entries are contiguous at the end of the
-     list, so we can stop searching when we see the first one.  */
-  for (i = 0, c = lookup_cache.mru;
-       c && c->name_len;
-       c = c->hdr.next, i++)
-    if (c->name_len == name_len
-	&& c->dir_cache_id == dir->cache_id
-	&& c->name[0] == name[0] && strcmp (c->name, name) == 0)
-      return c;
+  return (char *) (value & ~3L);
+}
 
-  return 0;
+static inline unsigned long
+frequ (unsigned long value)
+{
+  return value & 3;
 }
 
-/* Node NP has just been found in DIR with NAME.  If NP is null, that
-   means that this name has been confirmed as absent in the directory. */
-void
-diskfs_enter_lookup_cache (struct node *dir, struct node *np, const char *name)
+static inline void
+add_entry (struct cache_bucket *b, int i,
+	   const char *name, unsigned long key,
+	   ino64_t dir_cache_id, ino64_t node_cache_id)
 {
-  struct lookup_cache *c;
-  size_t name_len = strlen (name);
+  if (b->name[i])
+    free (charp (b->name[i]));
 
-  if (name_len > CACHE_NAME_LEN - 1)
+  b->name[i] = (unsigned long) strdup (name);
+  assert ((b->name[i] & 3) == 0);
+  if (b->name[i] == 0)
     return;
 
-  pthread_spin_lock (&cache_lock);
+  b->key[i] = key;
+  b->dir_cache_id[i] = dir_cache_id;
+  b->node_cache_id[i] = node_cache_id;
+}
+
+static inline void
+remove_entry (struct cache_bucket *b, int i)
+{
+  if (b->name[i])
+    free (charp (b->name[i]));
+  b->name[i] = 0;
+}
+
+static inline int
+valid_entry (struct cache_bucket *b, int i)
+{
+  return b->name[i] != 0;
+}
+
+/* This is the Murmur3 hash algorithm.  */
+
+#define FORCE_INLINE inline __attribute__((always_inline))
+
+inline uint32_t rotl32 ( uint32_t x, int8_t r )
+{
+  return (x << r) | (x >> (32 - r));
+}
+
+#define ROTL32(x,y)     rotl32(x,y)
+
+/* Block read - if your platform needs to do endian-swapping or can
+   only handle aligned reads, do the conversion here.  */
+
+FORCE_INLINE uint32_t getblock32 ( const uint32_t * p, int i )
+{
+  return p[i];
+}
+
+/* Finalization mix - force all bits of a hash block to avalanche.  */
+
+FORCE_INLINE uint32_t fmix32 ( uint32_t h )
+{
+  h ^= h >> 16;
+  h *= 0x85ebca6b;
+  h ^= h >> 13;
+  h *= 0xc2b2ae35;
+  h ^= h >> 16;
+
+  return h;
+}
+
+/* The Murmur3 hash function.  */
+void MurmurHash3_x86_32 ( const void * key, int len,
+                          uint32_t seed, void * out )
+{
+  const uint8_t * data = (const uint8_t*)key;
+  const int nblocks = len / 4;
+
+  uint32_t h1 = seed;
+
+  const uint32_t c1 = 0xcc9e2d51;
+  const uint32_t c2 = 0x1b873593;
+
+  /* body */
+
+  const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
+
+  for(int i = -nblocks; i; i++)
+  {
+    uint32_t k1 = getblock32(blocks,i);
+
+    k1 *= c1;
+    k1 = ROTL32(k1,15);
+    k1 *= c2;
+
+    h1 ^= k1;
+    h1 = ROTL32(h1,13);
+    h1 = h1*5+0xe6546b64;
+  }
+
+  /* tail */
 
-  if (lookup_cache.length == 0)
-    /* There should always be an lru_cache; this being zero means that the
-       cache hasn't been initialized yet.  Do so.  */
-    cacheq_set_length (&lookup_cache, MAXCACHE);
+  const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
 
-  /* See if there's an old entry for NAME in DIR.  If not, replace the least
-     recently used entry.  */
-  c = find_cache (dir, name, name_len) ?: lookup_cache.lru;
+  uint32_t k1 = 0;
 
-  /* Fill C with the new entry.  */
-  c->dir_cache_id = dir->cache_id;
-  c->node_cache_id = np ? np->cache_id : 0;
-  strcpy (c->name, name);
-  c->name_len = name_len;
+  switch(len & 3)
+  {
+  case 3: k1 ^= tail[2] << 16;
+  case 2: k1 ^= tail[1] << 8;
+  case 1: k1 ^= tail[0];
+          k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
+  };
 
-  /* Now C becomes the MRU entry!  */
-  cacheq_make_mru (&lookup_cache, c);
+  /* finalization */
 
-  pthread_spin_unlock (&cache_lock);
+  h1 ^= len;
+
+  h1 = fmix32(h1);
+
+  *(uint32_t*)out = h1;
 }
 
-/* Purge all references in the cache to NP as a node inside
-   directory DP. */
-void
-diskfs_purge_lookup_cache (struct node *dp, struct node *np)
+static int replace;
+
+static inline int
+lookup (ino64_t dir_cache_id, const char *name, unsigned long key,
+	struct cache_bucket **bucket, int *index)
 {
-  struct lookup_cache *c, *next;
+  int i;
+  unsigned long best = 3;
+  struct cache_bucket *b = *bucket = &name_cache[key & CACHE_MASK];
 
-  pthread_spin_lock (&cache_lock);
-  for (c = lookup_cache.mru; c; c = next)
+  for (i = 0; i < BUCKET_SIZE; i++)
     {
-      /* Save C->hdr.next, since we may move C from this position. */
-      next = c->hdr.next;
+      unsigned long f = frequ (b->name[i]);
+
+      if (valid_entry (b, i)
+	  && b->key[i] == key
+	  && b->dir_cache_id[i] == dir_cache_id
+	  && strcmp (charp (b->name[i]), name) == 0)
+	{
+	  if (f < 3)
+	    b->name[i] += 1;
+
+	  *index = i;
+	  return 1;
+	}
 
-      if (c->name_len
-	  && c->dir_cache_id == dp->cache_id
-	  && c->node_cache_id == np->cache_id)
+      if (f < best)
 	{
-	  c->name_len = 0;
-	  cacheq_make_lru (&lookup_cache, c); /* Use C as the next free
-						 entry. */
+	  best = f;
+	  *index = i;
 	}
     }
-  pthread_spin_unlock (&cache_lock);
+
+  if (best == 3)
+    {
+      *index = replace;
+      replace = (replace + 1) & (BUCKET_SIZE - 1);
+    }
+
+  return 0;
+}
+
+/* Hash the directory cache_id and the name.  */
+static inline unsigned long
+hash (ino64_t dir_cache_id, const char *name)
+{
+  unsigned long h;
+  MurmurHash3_x86_32 (&dir_cache_id, sizeof dir_cache_id, 0, &h);
+  MurmurHash3_x86_32 (name, strlen (name), hash, &h);
+  return h;
+}
+
+/* Node NP has just been found in DIR with NAME.  If NP is null, that
+   means that this name has been confirmed as absent in the directory. */
+void
+diskfs_enter_lookup_cache (struct node *dir, struct node *np, const char *name)
+{
+  unsigned long key = hash (dir->cache_id, name);
+  ino64_t value = np ? np->cache_id : 0;
+  struct cache_bucket *bucket;
+  int i = 0, found;
+
+  pthread_mutex_lock (&cache_lock);
+  found = lookup (dir->cache_id, name, key, &bucket, &i);
+  if (! found)
+    add_entry (bucket, i, name, key, dir->cache_id, value);
+  else
+    if (bucket->node_cache_id[i] != value)
+      bucket->node_cache_id[i] = value;
+
+  pthread_mutex_unlock (&cache_lock);
 }
 
+/* Purge all references in the cache to NP as a node inside
+   directory DP. */
+void
+diskfs_purge_lookup_cache (struct node *dp, struct node *np)
+{
+  int i;
+  struct cache_bucket *b;
+
+  pthread_mutex_lock (&cache_lock);
+
+  for (b = &name_cache[0]; b < &name_cache[CACHE_SIZE]; b++)
+    for (i = 0; i < BUCKET_SIZE; i++)
+      if (valid_entry (b, i)
+	  && b->dir_cache_id[i] == dp->cache_id
+	  && b->node_cache_id[i] == np->cache_id)
+	remove_entry (b, i);
+
+  pthread_mutex_unlock (&cache_lock);
+}
 
 /* Scan the cache looking for NAME inside DIR.  If we don't know
    anything entry at all, then return 0.  If the entry is confirmed to
@@ -140,27 +286,28 @@ diskfs_purge_lookup_cache (struct node *dp, struct node *np)
 struct node *
 diskfs_check_lookup_cache (struct node *dir, const char *name)
 {
-  struct lookup_cache *c;
-
-  pthread_spin_lock (&cache_lock);
-
-  c = find_cache (dir, name, strlen (name));
-  if (c)
+  unsigned long key = hash (dir->cache_id, name);
+  int lookup_parent = name[0] == '.' && name[1] == '.' && name[2] == '\0';
+  struct cache_bucket *bucket;
+  int i, found;
+
+  if (lookup_parent && dir == diskfs_root_node)
+    /* This is outside our file system, return cache miss.  */
+    return NULL;
+
+  pthread_mutex_lock (&cache_lock);
+  found = lookup (dir->cache_id, name, key, &bucket, &i);
+  if (found)
     {
-      int id = c->node_cache_id;
-
-      cacheq_make_mru (&lookup_cache, c); /* Record C as recently used.  */
+      ino64_t id = bucket->node_cache_id[i];
+      pthread_mutex_unlock (&cache_lock);
 
       if (id == 0)
 	/* A negative cache entry.  */
-	{
-	  pthread_spin_unlock (&cache_lock);
-	  return (struct node *)-1;
-	}
+	return (struct node *) -1;
       else if (id == dir->cache_id)
 	/* The cached node is the same as DIR.  */
 	{
-	  pthread_spin_unlock (&cache_lock);
 	  diskfs_nref (dir);
 	  return dir;
 	}
@@ -170,9 +317,7 @@ diskfs_check_lookup_cache (struct node *dir, const char *name)
 	  struct node *np;
 	  error_t err;
 
-	  pthread_spin_unlock (&cache_lock);
-
-	  if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
+	  if (lookup_parent)
 	    {
 	      pthread_mutex_unlock (&dir->lock);
 	      err = diskfs_cached_lookup (id, &np);
@@ -181,14 +326,19 @@ diskfs_check_lookup_cache (struct node *dir, const char *name)
 	      /* In the window where DP was unlocked, we might
 		 have lost.  So check the cache again, and see
 		 if it's still there; if so, then we win. */
-	      c = find_cache (dir, "..", 2);
-	      if (!c || c->node_cache_id != id)
+	      pthread_mutex_lock (&cache_lock);
+	      found = lookup (dir->cache_id, name, key, &bucket, &i);
+	      if (! found
+		  || ! bucket->node_cache_id[i] != id)
 		{
+		  pthread_mutex_unlock (&cache_lock);
+
 		  /* Lose */
 		  pthread_mutex_unlock (&np->lock);
 		  diskfs_nrele (np);
 		  return 0;
 		}
+	      pthread_mutex_unlock (&cache_lock);
 	    }
 	  else
 	    err = diskfs_cached_lookup (id, &np);
@@ -196,7 +346,7 @@ diskfs_check_lookup_cache (struct node *dir, const char *name)
 	}
     }
 
-  pthread_spin_unlock (&cache_lock);
+  pthread_mutex_unlock (&cache_lock);
 
   return 0;
 }
-- 
2.0.0.rc2