~ubuntu-branches/ubuntu/quantal/memcached/quantal

« back to all changes in this revision

Viewing changes to assoc.c

  • Committer: Bazaar Package Importer
  • Author(s): David Martínez Moreno
  • Date: 2009-10-16 15:09:43 UTC
  • mfrom: (1.1.6 upstream) (3.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20091016150943-0rhh8x206ebgwzeu
Tags: 1.4.2-1
* New upstream release, primarily bugfixes, some of them critical, hence
  the urgency:
  - Reject keys larger than 250 bytes in the binary protocol.
  - Bounds checking on stats cachedump.
  - Binary protocol set+cas wasn't returning a new cas ID.
  - Binary quitq didn't actually close the connection
  - Slab boundary checking cleanup (bad logic in unreachable code)
  - Get hit memory optimizations
  - Disallow -t options that cause the server to not work
  - Killed off incomplete slab rebalance feature.
* debian/patches:
  - 01_init_script_compliant_with_LSB.patch: Remade as upstream applied a
    whitespace cleanup script that broke the patch.
  - 02_manpage_additions.patch: Added missing parameters to the memcached
    manpage.
* Removed TODO from debian/docs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 *       or commercial.  It's free."
10
10
 *
11
11
 * The rest of the file is licensed under the BSD license.  See LICENSE.
12
 
 *
13
 
 * $Id$
14
12
 */
15
13
 
16
14
#include "memcached.h"
25
23
#include <stdio.h>
26
24
#include <string.h>
27
25
#include <assert.h>
28
 
 
29
 
/*
30
 
 * Since the hash function does bit manipulation, it needs to know
31
 
 * whether it's big or little-endian. ENDIAN_LITTLE and ENDIAN_BIG
32
 
 * are set in the configure script.
33
 
 */
34
 
#if ENDIAN_BIG == 1
35
 
# define HASH_LITTLE_ENDIAN 0
36
 
# define HASH_BIG_ENDIAN 1
37
 
#else
38
 
# if ENDIAN_LITTLE == 1
39
 
#  define HASH_LITTLE_ENDIAN 1
40
 
#  define HASH_BIG_ENDIAN 0
41
 
# else
42
 
#  define HASH_LITTLE_ENDIAN 0
43
 
#  define HASH_BIG_ENDIAN 0
44
 
# endif
45
 
#endif
46
 
 
47
 
#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
48
 
 
49
 
/*
50
 
-------------------------------------------------------------------------------
51
 
mix -- mix 3 32-bit values reversibly.
52
 
 
53
 
This is reversible, so any information in (a,b,c) before mix() is
54
 
still in (a,b,c) after mix().
55
 
 
56
 
If four pairs of (a,b,c) inputs are run through mix(), or through
57
 
mix() in reverse, there are at least 32 bits of the output that
58
 
are sometimes the same for one pair and different for another pair.
59
 
This was tested for:
60
 
* pairs that differed by one bit, by two bits, in any combination
61
 
  of top bits of (a,b,c), or in any combination of bottom bits of
62
 
  (a,b,c).
63
 
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
64
 
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
65
 
  is commonly produced by subtraction) look like a single 1-bit
66
 
  difference.
67
 
* the base values were pseudorandom, all zero but one bit set, or
68
 
  all zero plus a counter that starts at zero.
69
 
 
70
 
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
71
 
satisfy this are
72
 
    4  6  8 16 19  4
73
 
    9 15  3 18 27 15
74
 
   14  9  3  7 17  3
75
 
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
76
 
for "differ" defined as + with a one-bit base and a two-bit delta.  I
77
 
used http://burtleburtle.net/bob/hash/avalanche.html to choose
78
 
the operations, constants, and arrangements of the variables.
79
 
 
80
 
This does not achieve avalanche.  There are input bits of (a,b,c)
81
 
that fail to affect some output bits of (a,b,c), especially of a.  The
82
 
most thoroughly mixed value is c, but it doesn't really even achieve
83
 
avalanche in c.
84
 
 
85
 
This allows some parallelism.  Read-after-writes are good at doubling
86
 
the number of bits affected, so the goal of mixing pulls in the opposite
87
 
direction as the goal of parallelism.  I did what I could.  Rotates
88
 
seem to cost as much as shifts on every machine I could lay my hands
89
 
on, and rotates are much kinder to the top and bottom bits, so I used
90
 
rotates.
91
 
-------------------------------------------------------------------------------
92
 
*/
93
 
#define mix(a,b,c) \
94
 
{ \
95
 
  a -= c;  a ^= rot(c, 4);  c += b; \
96
 
  b -= a;  b ^= rot(a, 6);  a += c; \
97
 
  c -= b;  c ^= rot(b, 8);  b += a; \
98
 
  a -= c;  a ^= rot(c,16);  c += b; \
99
 
  b -= a;  b ^= rot(a,19);  a += c; \
100
 
  c -= b;  c ^= rot(b, 4);  b += a; \
101
 
}
102
 
 
103
 
/*
104
 
-------------------------------------------------------------------------------
105
 
final -- final mixing of 3 32-bit values (a,b,c) into c
106
 
 
107
 
Pairs of (a,b,c) values differing in only a few bits will usually
108
 
produce values of c that look totally different.  This was tested for
109
 
* pairs that differed by one bit, by two bits, in any combination
110
 
  of top bits of (a,b,c), or in any combination of bottom bits of
111
 
  (a,b,c).
112
 
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
113
 
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
114
 
  is commonly produced by subtraction) look like a single 1-bit
115
 
  difference.
116
 
* the base values were pseudorandom, all zero but one bit set, or
117
 
  all zero plus a counter that starts at zero.
118
 
 
119
 
These constants passed:
120
 
 14 11 25 16 4 14 24
121
 
 12 14 25 16 4 14 24
122
 
and these came close:
123
 
  4  8 15 26 3 22 24
124
 
 10  8 15 26 3 22 24
125
 
 11  8 15 26 3 22 24
126
 
-------------------------------------------------------------------------------
127
 
*/
128
 
#define final(a,b,c) \
129
 
{ \
130
 
  c ^= b; c -= rot(b,14); \
131
 
  a ^= c; a -= rot(c,11); \
132
 
  b ^= a; b -= rot(a,25); \
133
 
  c ^= b; c -= rot(b,16); \
134
 
  a ^= c; a -= rot(c,4);  \
135
 
  b ^= a; b -= rot(a,14); \
136
 
  c ^= b; c -= rot(b,24); \
137
 
}
138
 
 
139
 
#if HASH_LITTLE_ENDIAN == 1
140
 
uint32_t hash(
141
 
  const void *key,       /* the key to hash */
142
 
  size_t      length,    /* length of the key */
143
 
  const uint32_t    initval)   /* initval */
144
 
{
145
 
  uint32_t a,b,c;                                          /* internal state */
146
 
  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
147
 
 
148
 
  /* Set up the internal state */
149
 
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
150
 
 
151
 
  u.ptr = key;
152
 
  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
153
 
    const uint32_t *k = key;                           /* read 32-bit chunks */
154
 
#ifdef VALGRIND
155
 
    const uint8_t  *k8;
156
 
#endif /* ifdef VALGRIND */
157
 
 
158
 
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
159
 
    while (length > 12)
160
 
    {
161
 
      a += k[0];
162
 
      b += k[1];
163
 
      c += k[2];
164
 
      mix(a,b,c);
165
 
      length -= 12;
166
 
      k += 3;
167
 
    }
168
 
 
169
 
    /*----------------------------- handle the last (probably partial) block */
170
 
    /*
171
 
     * "k[2]&0xffffff" actually reads beyond the end of the string, but
172
 
     * then masks off the part it's not allowed to read.  Because the
173
 
     * string is aligned, the masked-off tail is in the same word as the
174
 
     * rest of the string.  Every machine with memory protection I've seen
175
 
     * does it on word boundaries, so is OK with this.  But VALGRIND will
176
 
     * still catch it and complain.  The masking trick does make the hash
177
 
     * noticably faster for short strings (like English words).
178
 
     */
179
 
#ifndef VALGRIND
180
 
 
181
 
    switch(length)
182
 
    {
183
 
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
184
 
    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
185
 
    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
186
 
    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
187
 
    case 8 : b+=k[1]; a+=k[0]; break;
188
 
    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
189
 
    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
190
 
    case 5 : b+=k[1]&0xff; a+=k[0]; break;
191
 
    case 4 : a+=k[0]; break;
192
 
    case 3 : a+=k[0]&0xffffff; break;
193
 
    case 2 : a+=k[0]&0xffff; break;
194
 
    case 1 : a+=k[0]&0xff; break;
195
 
    case 0 : return c;  /* zero length strings require no mixing */
196
 
    }
197
 
 
198
 
#else /* make valgrind happy */
199
 
 
200
 
    k8 = (const uint8_t *)k;
201
 
    switch(length)
202
 
    {
203
 
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
204
 
    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
205
 
    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
206
 
    case 9 : c+=k8[8];                   /* fall through */
207
 
    case 8 : b+=k[1]; a+=k[0]; break;
208
 
    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
209
 
    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
210
 
    case 5 : b+=k8[4];                   /* fall through */
211
 
    case 4 : a+=k[0]; break;
212
 
    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
213
 
    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
214
 
    case 1 : a+=k8[0]; break;
215
 
    case 0 : return c;  /* zero length strings require no mixing */
216
 
    }
217
 
 
218
 
#endif /* !valgrind */
219
 
 
220
 
  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
221
 
    const uint16_t *k = key;                           /* read 16-bit chunks */
222
 
    const uint8_t  *k8;
223
 
 
224
 
    /*--------------- all but last block: aligned reads and different mixing */
225
 
    while (length > 12)
226
 
    {
227
 
      a += k[0] + (((uint32_t)k[1])<<16);
228
 
      b += k[2] + (((uint32_t)k[3])<<16);
229
 
      c += k[4] + (((uint32_t)k[5])<<16);
230
 
      mix(a,b,c);
231
 
      length -= 12;
232
 
      k += 6;
233
 
    }
234
 
 
235
 
    /*----------------------------- handle the last (probably partial) block */
236
 
    k8 = (const uint8_t *)k;
237
 
    switch(length)
238
 
    {
239
 
    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
240
 
             b+=k[2]+(((uint32_t)k[3])<<16);
241
 
             a+=k[0]+(((uint32_t)k[1])<<16);
242
 
             break;
243
 
    case 11: c+=((uint32_t)k8[10])<<16;     /* @fallthrough */
244
 
    case 10: c+=k[4];                       /* @fallthrough@ */
245
 
             b+=k[2]+(((uint32_t)k[3])<<16);
246
 
             a+=k[0]+(((uint32_t)k[1])<<16);
247
 
             break;
248
 
    case 9 : c+=k8[8];                      /* @fallthrough */
249
 
    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
250
 
             a+=k[0]+(((uint32_t)k[1])<<16);
251
 
             break;
252
 
    case 7 : b+=((uint32_t)k8[6])<<16;      /* @fallthrough */
253
 
    case 6 : b+=k[2];
254
 
             a+=k[0]+(((uint32_t)k[1])<<16);
255
 
             break;
256
 
    case 5 : b+=k8[4];                      /* @fallthrough */
257
 
    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
258
 
             break;
259
 
    case 3 : a+=((uint32_t)k8[2])<<16;      /* @fallthrough */
260
 
    case 2 : a+=k[0];
261
 
             break;
262
 
    case 1 : a+=k8[0];
263
 
             break;
264
 
    case 0 : return c;  /* zero length strings require no mixing */
265
 
    }
266
 
 
267
 
  } else {                        /* need to read the key one byte at a time */
268
 
    const uint8_t *k = key;
269
 
 
270
 
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
271
 
    while (length > 12)
272
 
    {
273
 
      a += k[0];
274
 
      a += ((uint32_t)k[1])<<8;
275
 
      a += ((uint32_t)k[2])<<16;
276
 
      a += ((uint32_t)k[3])<<24;
277
 
      b += k[4];
278
 
      b += ((uint32_t)k[5])<<8;
279
 
      b += ((uint32_t)k[6])<<16;
280
 
      b += ((uint32_t)k[7])<<24;
281
 
      c += k[8];
282
 
      c += ((uint32_t)k[9])<<8;
283
 
      c += ((uint32_t)k[10])<<16;
284
 
      c += ((uint32_t)k[11])<<24;
285
 
      mix(a,b,c);
286
 
      length -= 12;
287
 
      k += 12;
288
 
    }
289
 
 
290
 
    /*-------------------------------- last block: affect all 32 bits of (c) */
291
 
    switch(length)                   /* all the case statements fall through */
292
 
    {
293
 
    case 12: c+=((uint32_t)k[11])<<24;
294
 
    case 11: c+=((uint32_t)k[10])<<16;
295
 
    case 10: c+=((uint32_t)k[9])<<8;
296
 
    case 9 : c+=k[8];
297
 
    case 8 : b+=((uint32_t)k[7])<<24;
298
 
    case 7 : b+=((uint32_t)k[6])<<16;
299
 
    case 6 : b+=((uint32_t)k[5])<<8;
300
 
    case 5 : b+=k[4];
301
 
    case 4 : a+=((uint32_t)k[3])<<24;
302
 
    case 3 : a+=((uint32_t)k[2])<<16;
303
 
    case 2 : a+=((uint32_t)k[1])<<8;
304
 
    case 1 : a+=k[0];
305
 
             break;
306
 
    case 0 : return c;  /* zero length strings require no mixing */
307
 
    }
308
 
  }
309
 
 
310
 
  final(a,b,c);
311
 
  return c;             /* zero length strings require no mixing */
312
 
}
313
 
 
314
 
#elif HASH_BIG_ENDIAN == 1
315
 
/*
316
 
 * hashbig():
317
 
 * This is the same as hashword() on big-endian machines.  It is different
318
 
 * from hashlittle() on all machines.  hashbig() takes advantage of
319
 
 * big-endian byte ordering.
320
 
 */
321
 
uint32_t hash( const void *key, size_t length, const uint32_t initval)
322
 
{
323
 
  uint32_t a,b,c;
324
 
  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
325
 
 
326
 
  /* Set up the internal state */
327
 
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
328
 
 
329
 
  u.ptr = key;
330
 
  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
331
 
    const uint32_t *k = key;                           /* read 32-bit chunks */
332
 
#ifdef VALGRIND
333
 
    const uint8_t  *k8;
334
 
#endif /* ifdef VALGRIND */
335
 
 
336
 
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
337
 
    while (length > 12)
338
 
    {
339
 
      a += k[0];
340
 
      b += k[1];
341
 
      c += k[2];
342
 
      mix(a,b,c);
343
 
      length -= 12;
344
 
      k += 3;
345
 
    }
346
 
 
347
 
    /*----------------------------- handle the last (probably partial) block */
348
 
    /*
349
 
     * "k[2]<<8" actually reads beyond the end of the string, but
350
 
     * then shifts out the part it's not allowed to read.  Because the
351
 
     * string is aligned, the illegal read is in the same word as the
352
 
     * rest of the string.  Every machine with memory protection I've seen
353
 
     * does it on word boundaries, so is OK with this.  But VALGRIND will
354
 
     * still catch it and complain.  The masking trick does make the hash
355
 
     * noticably faster for short strings (like English words).
356
 
     */
357
 
#ifndef VALGRIND
358
 
 
359
 
    switch(length)
360
 
    {
361
 
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
362
 
    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
363
 
    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
364
 
    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
365
 
    case 8 : b+=k[1]; a+=k[0]; break;
366
 
    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
367
 
    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
368
 
    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
369
 
    case 4 : a+=k[0]; break;
370
 
    case 3 : a+=k[0]&0xffffff00; break;
371
 
    case 2 : a+=k[0]&0xffff0000; break;
372
 
    case 1 : a+=k[0]&0xff000000; break;
373
 
    case 0 : return c;              /* zero length strings require no mixing */
374
 
    }
375
 
 
376
 
#else  /* make valgrind happy */
377
 
 
378
 
    k8 = (const uint8_t *)k;
379
 
    switch(length)                   /* all the case statements fall through */
380
 
    {
381
 
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
382
 
    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
383
 
    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
384
 
    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
385
 
    case 8 : b+=k[1]; a+=k[0]; break;
386
 
    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
387
 
    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
388
 
    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
389
 
    case 4 : a+=k[0]; break;
390
 
    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
391
 
    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
392
 
    case 1 : a+=((uint32_t)k8[0])<<24; break;
393
 
    case 0 : return c;
394
 
    }
395
 
 
396
 
#endif /* !VALGRIND */
397
 
 
398
 
  } else {                        /* need to read the key one byte at a time */
399
 
    const uint8_t *k = key;
400
 
 
401
 
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
402
 
    while (length > 12)
403
 
    {
404
 
      a += ((uint32_t)k[0])<<24;
405
 
      a += ((uint32_t)k[1])<<16;
406
 
      a += ((uint32_t)k[2])<<8;
407
 
      a += ((uint32_t)k[3]);
408
 
      b += ((uint32_t)k[4])<<24;
409
 
      b += ((uint32_t)k[5])<<16;
410
 
      b += ((uint32_t)k[6])<<8;
411
 
      b += ((uint32_t)k[7]);
412
 
      c += ((uint32_t)k[8])<<24;
413
 
      c += ((uint32_t)k[9])<<16;
414
 
      c += ((uint32_t)k[10])<<8;
415
 
      c += ((uint32_t)k[11]);
416
 
      mix(a,b,c);
417
 
      length -= 12;
418
 
      k += 12;
419
 
    }
420
 
 
421
 
    /*-------------------------------- last block: affect all 32 bits of (c) */
422
 
    switch(length)                   /* all the case statements fall through */
423
 
    {
424
 
    case 12: c+=k[11];
425
 
    case 11: c+=((uint32_t)k[10])<<8;
426
 
    case 10: c+=((uint32_t)k[9])<<16;
427
 
    case 9 : c+=((uint32_t)k[8])<<24;
428
 
    case 8 : b+=k[7];
429
 
    case 7 : b+=((uint32_t)k[6])<<8;
430
 
    case 6 : b+=((uint32_t)k[5])<<16;
431
 
    case 5 : b+=((uint32_t)k[4])<<24;
432
 
    case 4 : a+=k[3];
433
 
    case 3 : a+=((uint32_t)k[2])<<8;
434
 
    case 2 : a+=((uint32_t)k[1])<<16;
435
 
    case 1 : a+=((uint32_t)k[0])<<24;
436
 
             break;
437
 
    case 0 : return c;
438
 
    }
439
 
  }
440
 
 
441
 
  final(a,b,c);
442
 
  return c;
443
 
}
444
 
#else /* HASH_XXX_ENDIAN == 1 */
445
 
#error Must define HASH_BIG_ENDIAN or HASH_LITTLE_ENDIAN
446
 
#endif /* HASH_XXX_ENDIAN == 1 */
 
26
#include <pthread.h>
 
27
 
 
28
static pthread_cond_t maintenance_cond = PTHREAD_COND_INITIALIZER;
 
29
 
447
30
 
448
31
typedef  unsigned long  int  ub4;   /* unsigned 4-byte quantities */
449
32
typedef  unsigned       char ub1;   /* unsigned 1-byte quantities */
506
89
        it = it->h_next;
507
90
        ++depth;
508
91
    }
509
 
    MEMCACHED_ASSOC_FIND(key, depth);
 
92
    MEMCACHED_ASSOC_FIND(key, nkey, depth);
510
93
    return ret;
511
94
}
512
95
 
543
126
        hashpower++;
544
127
        expanding = true;
545
128
        expand_bucket = 0;
546
 
        do_assoc_move_next_bucket();
 
129
        pthread_cond_signal(&maintenance_cond);
547
130
    } else {
548
131
        primary_hashtable = old_hashtable;
549
132
        /* Bad news, but we can keep running. */
550
133
    }
551
134
}
552
135
 
553
 
/* migrates the next bucket to the primary hashtable if we're expanding. */
554
 
void do_assoc_move_next_bucket(void) {
555
 
    item *it, *next;
556
 
    int bucket;
557
 
 
558
 
    if (expanding) {
559
 
        for (it = old_hashtable[expand_bucket]; NULL != it; it = next) {
560
 
            next = it->h_next;
561
 
 
562
 
            bucket = hash(ITEM_key(it), it->nkey, 0) & hashmask(hashpower);
563
 
            it->h_next = primary_hashtable[bucket];
564
 
            primary_hashtable[bucket] = it;
565
 
        }
566
 
 
567
 
        old_hashtable[expand_bucket] = NULL;
568
 
 
569
 
        expand_bucket++;
570
 
        if (expand_bucket == hashsize(hashpower - 1)) {
571
 
            expanding = false;
572
 
            free(old_hashtable);
573
 
            if (settings.verbose > 1)
574
 
                fprintf(stderr, "Hash table expansion done\n");
575
 
        }
576
 
    }
577
 
}
578
 
 
579
136
/* Note: this isn't an assoc_update.  The key must not already exist to call this */
580
137
int assoc_insert(item *it) {
581
138
    uint32_t hv;
599
156
        assoc_expand();
600
157
    }
601
158
 
602
 
    MEMCACHED_ASSOC_INSERT(ITEM_key(it), hash_items);
 
159
    MEMCACHED_ASSOC_INSERT(ITEM_key(it), it->nkey, hash_items);
603
160
    return 1;
604
161
}
605
162
 
612
169
        /* The DTrace probe cannot be triggered as the last instruction
613
170
         * due to possible tail-optimization by the compiler
614
171
         */
615
 
        MEMCACHED_ASSOC_DELETE(key, hash_items);
 
172
        MEMCACHED_ASSOC_DELETE(key, nkey, hash_items);
616
173
        nxt = (*before)->h_next;
617
174
        (*before)->h_next = 0;   /* probably pointless, but whatever. */
618
175
        *before = nxt;
622
179
       they can't find. */
623
180
    assert(*before != 0);
624
181
}
 
182
 
 
183
 
 
184
static volatile int do_run_maintenance_thread = 1;
 
185
 
 
186
#define DEFAULT_HASH_BULK_MOVE 1
 
187
int hash_bulk_move = DEFAULT_HASH_BULK_MOVE;
 
188
 
 
189
static void *assoc_maintenance_thread(void *arg) {
 
190
 
 
191
    while (do_run_maintenance_thread) {
 
192
        int ii = 0;
 
193
 
 
194
        /* Lock the cache, and bulk move multiple buckets to the new
 
195
         * hash table. */
 
196
        pthread_mutex_lock(&cache_lock);
 
197
 
 
198
        for (ii = 0; ii < hash_bulk_move && expanding; ++ii) {
 
199
            item *it, *next;
 
200
            int bucket;
 
201
 
 
202
            for (it = old_hashtable[expand_bucket]; NULL != it; it = next) {
 
203
                next = it->h_next;
 
204
 
 
205
                bucket = hash(ITEM_key(it), it->nkey, 0) & hashmask(hashpower);
 
206
                it->h_next = primary_hashtable[bucket];
 
207
                primary_hashtable[bucket] = it;
 
208
            }
 
209
 
 
210
            old_hashtable[expand_bucket] = NULL;
 
211
 
 
212
            expand_bucket++;
 
213
            if (expand_bucket == hashsize(hashpower - 1)) {
 
214
                expanding = false;
 
215
                free(old_hashtable);
 
216
                if (settings.verbose > 1)
 
217
                    fprintf(stderr, "Hash table expansion done\n");
 
218
            }
 
219
        }
 
220
 
 
221
        if (!expanding) {
 
222
            /* We are done expanding.. just wait for next invocation */
 
223
            pthread_cond_wait(&maintenance_cond, &cache_lock);
 
224
        }
 
225
 
 
226
        pthread_mutex_unlock(&cache_lock);
 
227
    }
 
228
    return NULL;
 
229
}
 
230
 
 
231
static pthread_t maintenance_tid;
 
232
 
 
233
int start_assoc_maintenance_thread() {
 
234
    int ret;
 
235
    char *env = getenv("MEMCACHED_HASH_BULK_MOVE");
 
236
    if (env != NULL) {
 
237
        hash_bulk_move = atoi(env);
 
238
        if (hash_bulk_move == 0) {
 
239
            hash_bulk_move = DEFAULT_HASH_BULK_MOVE;
 
240
        }
 
241
    }
 
242
    if ((ret = pthread_create(&maintenance_tid, NULL,
 
243
                              assoc_maintenance_thread, NULL)) != 0) {
 
244
        fprintf(stderr, "Can't create thread: %s\n", strerror(ret));
 
245
        return -1;
 
246
    }
 
247
    return 0;
 
248
}
 
249
 
 
250
void stop_assoc_maintenance_thread() {
 
251
    pthread_mutex_lock(&cache_lock);
 
252
    do_run_maintenance_thread = 0;
 
253
    pthread_cond_signal(&maintenance_cond);
 
254
    pthread_mutex_unlock(&cache_lock);
 
255
 
 
256
    /* Wait for the maintenance thread to stop */
 
257
    pthread_join(maintenance_tid, NULL);
 
258
}
 
259
 
 
260