VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/pldhash.c@ 11585

Last change on this file since 11585 was 11278, checked in by vboxsync, 16 years ago

xpcom/PL_DHashTableEnumerate: New fix/workaround for the IPC code deleting hash table entries during enumeartion - prevent ChangeTable from doing anything while someone is enumerating the hashtable. The previous fix had sideeffects and could cause entries to be skipped.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.5 KB
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla JavaScript code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2001
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Brendan Eich <brendan@mozilla.org> (Original Author)
24 * Chris Waterson <waterson@netscape.com>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40/*
41 * Double hashing implementation.
42 * GENERATED BY js/src/plify_jsdhash.sed -- DO NOT EDIT!!!
43 */
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include "prbit.h"
48#include "pldhash.h"
49#include "prlog.h" /* for PR_ASSERT */
50
51#ifdef PL_DHASHMETER
52# if defined MOZILLA_CLIENT && defined DEBUG_XXXbrendan
53# include "nsTraceMalloc.h"
54# endif
55# define METER(x) x
56#else
57# define METER(x) /* nothing */
58#endif
59
60PR_IMPLEMENT(void *)
61PL_DHashAllocTable(PLDHashTable *table, PRUint32 nbytes)
62{
63 return malloc(nbytes);
64}
65
66PR_IMPLEMENT(void)
67PL_DHashFreeTable(PLDHashTable *table, void *ptr)
68{
69 free(ptr);
70}
71
72PR_IMPLEMENT(PLDHashNumber)
73PL_DHashStringKey(PLDHashTable *table, const void *key)
74{
75 PLDHashNumber h;
76 const unsigned char *s;
77
78 h = 0;
79 for (s = key; *s != '\0'; s++)
80 h = (h >> (PL_DHASH_BITS - 4)) ^ (h << 4) ^ *s;
81 return h;
82}
83
84PR_IMPLEMENT(const void *)
85PL_DHashGetKeyStub(PLDHashTable *table, PLDHashEntryHdr *entry)
86{
87 PLDHashEntryStub *stub = (PLDHashEntryStub *)entry;
88
89 return stub->key;
90}
91
92PR_IMPLEMENT(PLDHashNumber)
93PL_DHashVoidPtrKeyStub(PLDHashTable *table, const void *key)
94{
95 return (PLDHashNumber)key >> 2;
96}
97
98PR_IMPLEMENT(PRBool)
99PL_DHashMatchEntryStub(PLDHashTable *table,
100 const PLDHashEntryHdr *entry,
101 const void *key)
102{
103 const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
104
105 return stub->key == key;
106}
107
108PR_IMPLEMENT(PRBool)
109PL_DHashMatchStringKey(PLDHashTable *table,
110 const PLDHashEntryHdr *entry,
111 const void *key)
112{
113 const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
114
115 /* XXX tolerate null keys on account of sloppy Mozilla callers. */
116 return stub->key == key ||
117 (stub->key && key && strcmp(stub->key, key) == 0);
118}
119
120PR_IMPLEMENT(void)
121PL_DHashMoveEntryStub(PLDHashTable *table,
122 const PLDHashEntryHdr *from,
123 PLDHashEntryHdr *to)
124{
125 memcpy(to, from, table->entrySize);
126}
127
128PR_IMPLEMENT(void)
129PL_DHashClearEntryStub(PLDHashTable *table, PLDHashEntryHdr *entry)
130{
131 memset(entry, 0, table->entrySize);
132}
133
134PR_IMPLEMENT(void)
135PL_DHashFreeStringKey(PLDHashTable *table, PLDHashEntryHdr *entry)
136{
137 const PLDHashEntryStub *stub = (const PLDHashEntryStub *)entry;
138
139 free((void *) stub->key);
140 memset(entry, 0, table->entrySize);
141}
142
143PR_IMPLEMENT(void)
144PL_DHashFinalizeStub(PLDHashTable *table)
145{
146}
147
148static const PLDHashTableOps stub_ops = {
149 PL_DHashAllocTable,
150 PL_DHashFreeTable,
151 PL_DHashGetKeyStub,
152 PL_DHashVoidPtrKeyStub,
153 PL_DHashMatchEntryStub,
154 PL_DHashMoveEntryStub,
155 PL_DHashClearEntryStub,
156 PL_DHashFinalizeStub,
157 NULL
158};
159
160PR_IMPLEMENT(const PLDHashTableOps *)
161PL_DHashGetStubOps(void)
162{
163 return &stub_ops;
164}
165
166PR_IMPLEMENT(PLDHashTable *)
167PL_NewDHashTable(const PLDHashTableOps *ops, void *data, PRUint32 entrySize,
168 PRUint32 capacity)
169{
170 PLDHashTable *table;
171
172 table = (PLDHashTable *) malloc(sizeof *table);
173 if (!table)
174 return NULL;
175 if (!PL_DHashTableInit(table, ops, data, entrySize, capacity)) {
176 free(table);
177 return NULL;
178 }
179 return table;
180}
181
182PR_IMPLEMENT(void)
183PL_DHashTableDestroy(PLDHashTable *table)
184{
185 PL_DHashTableFinish(table);
186 free(table);
187}
188
189PR_IMPLEMENT(PRBool)
190PL_DHashTableInit(PLDHashTable *table, const PLDHashTableOps *ops, void *data,
191 PRUint32 entrySize, PRUint32 capacity)
192{
193 int log2;
194 PRUint32 nbytes;
195
196#ifdef DEBUG
197 if (entrySize > 10 * sizeof(void *)) {
198 fprintf(stderr,
199 "pldhash: for the table at address %p, the given entrySize"
200 " of %lu %s favors chaining over double hashing.\n",
201 (void *)table,
202 (unsigned long) entrySize,
203 (entrySize > 16 * sizeof(void*)) ? "definitely" : "probably");
204 }
205#endif
206
207 table->ops = ops;
208 table->data = data;
209 if (capacity < PL_DHASH_MIN_SIZE)
210 capacity = PL_DHASH_MIN_SIZE;
211 log2 = PR_CeilingLog2(capacity);
212 capacity = PR_BIT(log2);
213 if (capacity >= PL_DHASH_SIZE_LIMIT)
214 return PR_FALSE;
215 table->hashShift = PL_DHASH_BITS - log2;
216 table->maxAlphaFrac = 0xC0; /* .75 */
217 table->minAlphaFrac = 0x40; /* .25 */
218 table->entrySize = entrySize;
219 table->entryCount = table->removedCount = 0;
220 table->generation = 0;
221 nbytes = capacity * entrySize;
222
223 table->entryStore = ops->allocTable(table, nbytes);
224 if (!table->entryStore)
225 return PR_FALSE;
226 memset(table->entryStore, 0, nbytes);
227 METER(memset(&table->stats, 0, sizeof table->stats));
228 return PR_TRUE;
229}
230
231/*
232 * Compute max and min load numbers (entry counts) from table params.
233 */
234#define MAX_LOAD(table, size) (((table)->maxAlphaFrac * (size)) >> 8)
235#define MIN_LOAD(table, size) (((table)->minAlphaFrac * (size)) >> 8)
236
237PR_IMPLEMENT(void)
238PL_DHashTableSetAlphaBounds(PLDHashTable *table,
239 float maxAlpha,
240 float minAlpha)
241{
242 PRUint32 size;
243
244 /*
245 * Reject obviously insane bounds, rather than trying to guess what the
246 * buggy caller intended.
247 */
248 PR_ASSERT(0.5 <= maxAlpha && maxAlpha < 1 && 0 <= minAlpha);
249 if (maxAlpha < 0.5 || 1 <= maxAlpha || minAlpha < 0)
250 return;
251
252 /*
253 * Ensure that at least one entry will always be free. If maxAlpha at
254 * minimum size leaves no entries free, reduce maxAlpha based on minimum
255 * size and the precision limit of maxAlphaFrac's fixed point format.
256 */
257 PR_ASSERT(PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) >= 1);
258 if (PL_DHASH_MIN_SIZE - (maxAlpha * PL_DHASH_MIN_SIZE) < 1) {
259 maxAlpha = (float)
260 (PL_DHASH_MIN_SIZE - PR_MAX(PL_DHASH_MIN_SIZE / 256, 1))
261 / PL_DHASH_MIN_SIZE;
262 }
263
264 /*
265 * Ensure that minAlpha is strictly less than half maxAlpha. Take care
266 * not to truncate an entry's worth of alpha when storing in minAlphaFrac
267 * (8-bit fixed point format).
268 */
269 PR_ASSERT(minAlpha < maxAlpha / 2);
270 if (minAlpha >= maxAlpha / 2) {
271 size = PL_DHASH_TABLE_SIZE(table);
272 minAlpha = (size * maxAlpha - PR_MAX(size / 256, 1)) / (2 * size);
273 }
274
275 table->maxAlphaFrac = (uint8)(maxAlpha * 256);
276 table->minAlphaFrac = (uint8)(minAlpha * 256);
277}
278
279/*
280 * Double hashing needs the second hash code to be relatively prime to table
281 * size, so we simply make hash2 odd.
282 */
283#define HASH1(hash0, shift) ((hash0) >> (shift))
284#define HASH2(hash0,log2,shift) ((((hash0) << (log2)) >> (shift)) | 1)
285
286/*
287 * Reserve keyHash 0 for free entries and 1 for removed-entry sentinels. Note
288 * that a removed-entry sentinel need be stored only if the removed entry had
289 * a colliding entry added after it. Therefore we can use 1 as the collision
290 * flag in addition to the removed-entry sentinel value. Multiplicative hash
291 * uses the high order bits of keyHash, so this least-significant reservation
292 * should not hurt the hash function's effectiveness much.
293 *
294 * If you change any of these magic numbers, also update PL_DHASH_ENTRY_IS_LIVE
295 * in pldhash.h. It used to be private to pldhash.c, but then became public to
296 * assist iterator writers who inspect table->entryStore directly.
297 */
298#define COLLISION_FLAG ((PLDHashNumber) 1)
299#define MARK_ENTRY_FREE(entry) ((entry)->keyHash = 0)
300#define MARK_ENTRY_REMOVED(entry) ((entry)->keyHash = 1)
301#define ENTRY_IS_REMOVED(entry) ((entry)->keyHash == 1)
302#define ENTRY_IS_LIVE(entry) PL_DHASH_ENTRY_IS_LIVE(entry)
303#define ENSURE_LIVE_KEYHASH(hash0) if (hash0 < 2) hash0 -= 2; else (void)0
304
305/* Match an entry's keyHash against an unstored one computed from a key. */
306#define MATCH_ENTRY_KEYHASH(entry,hash0) \
307 (((entry)->keyHash & ~COLLISION_FLAG) == (hash0))
308
309/* Compute the address of the indexed entry in table. */
310#define ADDRESS_ENTRY(table, index) \
311 ((PLDHashEntryHdr *)((table)->entryStore + (index) * (table)->entrySize))
312
313PR_IMPLEMENT(void)
314PL_DHashTableFinish(PLDHashTable *table)
315{
316 char *entryAddr, *entryLimit;
317 PRUint32 entrySize;
318 PLDHashEntryHdr *entry;
319
320#ifdef DEBUG_XXXbrendan
321 static FILE *dumpfp = NULL;
322 if (!dumpfp) dumpfp = fopen("/tmp/pldhash.bigdump", "w");
323 if (dumpfp) {
324#ifdef MOZILLA_CLIENT
325 NS_TraceStack(1, dumpfp);
326#endif
327 PL_DHashTableDumpMeter(table, NULL, dumpfp);
328 fputc('\n', dumpfp);
329 }
330#endif
331
332 /* Call finalize before clearing entries, so it can enumerate them. */
333 table->ops->finalize(table);
334
335 /* Clear any remaining live entries. */
336 entryAddr = table->entryStore;
337 entrySize = table->entrySize;
338 entryLimit = entryAddr + PL_DHASH_TABLE_SIZE(table) * entrySize;
339 while (entryAddr < entryLimit) {
340 entry = (PLDHashEntryHdr *)entryAddr;
341 if (ENTRY_IS_LIVE(entry)) {
342 METER(table->stats.removeEnums++);
343 table->ops->clearEntry(table, entry);
344 }
345 entryAddr += entrySize;
346 }
347
348 /* Free entry storage last. */
349 table->ops->freeTable(table, table->entryStore);
350}
351
352static PLDHashEntryHdr * PL_DHASH_FASTCALL
353SearchTable(PLDHashTable *table, const void *key, PLDHashNumber keyHash,
354 PLDHashOperator op)
355{
356 PLDHashNumber hash1, hash2;
357 int hashShift, sizeLog2;
358 PLDHashEntryHdr *entry, *firstRemoved;
359 PLDHashMatchEntry matchEntry;
360 PRUint32 sizeMask;
361
362 METER(table->stats.searches++);
363 PR_ASSERT(!(keyHash & COLLISION_FLAG));
364
365 /* Compute the primary hash address. */
366 hashShift = table->hashShift;
367 hash1 = HASH1(keyHash, hashShift);
368 entry = ADDRESS_ENTRY(table, hash1);
369
370 /* Miss: return space for a new entry. */
371 if (PL_DHASH_ENTRY_IS_FREE(entry)) {
372 METER(table->stats.misses++);
373 return entry;
374 }
375
376 /* Hit: return entry. */
377 matchEntry = table->ops->matchEntry;
378 if (MATCH_ENTRY_KEYHASH(entry, keyHash) && matchEntry(table, entry, key)) {
379 METER(table->stats.hits++);
380 return entry;
381 }
382
383 /* Collision: double hash. */
384 sizeLog2 = PL_DHASH_BITS - table->hashShift;
385 hash2 = HASH2(keyHash, sizeLog2, hashShift);
386 sizeMask = PR_BITMASK(sizeLog2);
387
388 /* Save the first removed entry pointer so PL_DHASH_ADD can recycle it. */
389 if (ENTRY_IS_REMOVED(entry)) {
390 firstRemoved = entry;
391 } else {
392 firstRemoved = NULL;
393 if (op == PL_DHASH_ADD)
394 entry->keyHash |= COLLISION_FLAG;
395 }
396
397 for (;;) {
398 METER(table->stats.steps++);
399 hash1 -= hash2;
400 hash1 &= sizeMask;
401
402 entry = ADDRESS_ENTRY(table, hash1);
403 if (PL_DHASH_ENTRY_IS_FREE(entry)) {
404 METER(table->stats.misses++);
405 return (firstRemoved && op == PL_DHASH_ADD) ? firstRemoved : entry;
406 }
407
408 if (MATCH_ENTRY_KEYHASH(entry, keyHash) &&
409 matchEntry(table, entry, key)) {
410 METER(table->stats.hits++);
411 return entry;
412 }
413
414 if (ENTRY_IS_REMOVED(entry)) {
415 if (!firstRemoved)
416 firstRemoved = entry;
417 } else {
418 if (op == PL_DHASH_ADD)
419 entry->keyHash |= COLLISION_FLAG;
420 }
421 }
422
423 /* NOTREACHED */
424 return NULL;
425}
426
427static PRBool
428ChangeTable(PLDHashTable *table, int deltaLog2)
429{
430 int oldLog2, newLog2;
431 PRUint32 oldCapacity, newCapacity;
432 char *newEntryStore, *oldEntryStore, *oldEntryAddr;
433 PRUint32 entrySize, i, nbytes;
434 PLDHashEntryHdr *oldEntry, *newEntry;
435 PLDHashGetKey getKey;
436 PLDHashMoveEntry moveEntry;
437
438#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
439 PR_ASSERT(table->generation != PR_UINT32_MAX);
440 if (table->generation == PR_UINT32_MAX)
441 return PR_FALSE;
442#endif
443
444 /* Look, but don't touch, until we succeed in getting new entry store. */
445 oldLog2 = PL_DHASH_BITS - table->hashShift;
446 newLog2 = oldLog2 + deltaLog2;
447 oldCapacity = PR_BIT(oldLog2);
448 newCapacity = PR_BIT(newLog2);
449 if (newCapacity >= PL_DHASH_SIZE_LIMIT)
450 return PR_FALSE;
451 entrySize = table->entrySize;
452 nbytes = newCapacity * entrySize;
453
454 newEntryStore = table->ops->allocTable(table, nbytes);
455 if (!newEntryStore)
456 return PR_FALSE;
457
458 /* We can't fail from here on, so update table parameters. */
459 table->hashShift = PL_DHASH_BITS - newLog2;
460 table->removedCount = 0;
461 table->generation++;
462#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
463 if (table->generation == PR_UINT32_MAX)
464 table->generation++;
465#endif
466
467 /* Assign the new entry store to table. */
468 memset(newEntryStore, 0, nbytes);
469 oldEntryAddr = oldEntryStore = table->entryStore;
470 table->entryStore = newEntryStore;
471 getKey = table->ops->getKey;
472 moveEntry = table->ops->moveEntry;
473
474 /* Copy only live entries, leaving removed ones behind. */
475 for (i = 0; i < oldCapacity; i++) {
476 oldEntry = (PLDHashEntryHdr *)oldEntryAddr;
477 if (ENTRY_IS_LIVE(oldEntry)) {
478 oldEntry->keyHash &= ~COLLISION_FLAG;
479 newEntry = SearchTable(table, getKey(table, oldEntry),
480 oldEntry->keyHash, PL_DHASH_ADD);
481 PR_ASSERT(PL_DHASH_ENTRY_IS_FREE(newEntry));
482 moveEntry(table, oldEntry, newEntry);
483 newEntry->keyHash = oldEntry->keyHash;
484 }
485 oldEntryAddr += entrySize;
486 }
487
488 table->ops->freeTable(table, oldEntryStore);
489 return PR_TRUE;
490}
491
492PR_IMPLEMENT(PLDHashEntryHdr *) PL_DHASH_FASTCALL
493PL_DHashTableOperate(PLDHashTable *table, const void *key, PLDHashOperator op)
494{
495 PLDHashNumber keyHash;
496 PLDHashEntryHdr *entry;
497 PRUint32 size;
498 int deltaLog2;
499
500 keyHash = table->ops->hashKey(table, key);
501 keyHash *= PL_DHASH_GOLDEN_RATIO;
502
503 /* Avoid 0 and 1 hash codes, they indicate free and removed entries. */
504 ENSURE_LIVE_KEYHASH(keyHash);
505 keyHash &= ~COLLISION_FLAG;
506
507 switch (op) {
508 case PL_DHASH_LOOKUP:
509 METER(table->stats.lookups++);
510 entry = SearchTable(table, key, keyHash, op);
511 break;
512
513 case PL_DHASH_ADD:
514 /*
515 * If alpha is >= .75, grow or compress the table. If key is already
516 * in the table, we may grow once more than necessary, but only if we
517 * are on the edge of being overloaded.
518 */
519 size = PL_DHASH_TABLE_SIZE(table);
520 if (table->entryCount + table->removedCount >= MAX_LOAD(table, size)) {
521 /* Compress if a quarter or more of all entries are removed. */
522 if (table->removedCount >= size >> 2) {
523 METER(table->stats.compresses++);
524 deltaLog2 = 0;
525 } else {
526 METER(table->stats.grows++);
527 deltaLog2 = 1;
528 }
529
530 /*
531 * Grow or compress table, returning null if ChangeTable fails and
532 * falling through might claim the last free entry.
533 */
534 if (!ChangeTable(table, deltaLog2) &&
535 table->entryCount + table->removedCount == size - 1) {
536 METER(table->stats.addFailures++);
537 return NULL;
538 }
539 }
540
541 /*
542 * Look for entry after possibly growing, so we don't have to add it,
543 * then skip it while growing the table and re-add it after.
544 */
545 entry = SearchTable(table, key, keyHash, op);
546 if (!ENTRY_IS_LIVE(entry)) {
547 /* Initialize the entry, indicating that it's no longer free. */
548 METER(table->stats.addMisses++);
549 if (ENTRY_IS_REMOVED(entry)) {
550 METER(table->stats.addOverRemoved++);
551 table->removedCount--;
552 keyHash |= COLLISION_FLAG;
553 }
554 if (table->ops->initEntry &&
555 !table->ops->initEntry(table, entry, key)) {
556 /* We haven't claimed entry yet; fail with null return. */
557 memset(entry + 1, 0, table->entrySize - sizeof *entry);
558 return NULL;
559 }
560 entry->keyHash = keyHash;
561 table->entryCount++;
562 }
563 METER(else table->stats.addHits++);
564 break;
565
566 case PL_DHASH_REMOVE:
567 entry = SearchTable(table, key, keyHash, op);
568 if (ENTRY_IS_LIVE(entry)) {
569 /* Clear this entry and mark it as "removed". */
570 METER(table->stats.removeHits++);
571 PL_DHashTableRawRemove(table, entry);
572
573 /* Shrink if alpha is <= .25 and table isn't too small already. */
574 size = PL_DHASH_TABLE_SIZE(table);
575 if (size > PL_DHASH_MIN_SIZE &&
576#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
577 /** @todo This is where IPC screws up, avoid the assertion in ChangeTable until it's fixed. */
578 table->generation != PR_UINT32_MAX &&
579#endif
580 table->entryCount <= MIN_LOAD(table, size)) {
581 METER(table->stats.shrinks++);
582 (void) ChangeTable(table, -1);
583 }
584 }
585 METER(else table->stats.removeMisses++);
586 entry = NULL;
587 break;
588
589 default:
590 PR_ASSERT(0);
591 entry = NULL;
592 }
593
594 return entry;
595}
596
597PR_IMPLEMENT(void)
598PL_DHashTableRawRemove(PLDHashTable *table, PLDHashEntryHdr *entry)
599{
600 PLDHashNumber keyHash; /* load first in case clearEntry goofs it */
601
602 PR_ASSERT(PL_DHASH_ENTRY_IS_LIVE(entry));
603 keyHash = entry->keyHash;
604 table->ops->clearEntry(table, entry);
605 if (keyHash & COLLISION_FLAG) {
606 MARK_ENTRY_REMOVED(entry);
607 table->removedCount++;
608 } else {
609 METER(table->stats.removeFrees++);
610 MARK_ENTRY_FREE(entry);
611 }
612 table->entryCount--;
613}
614
615PR_IMPLEMENT(PRUint32)
616PL_DHashTableEnumerate(PLDHashTable *table, PLDHashEnumerator etor, void *arg)
617{
618 char *entryAddr, *entryLimit;
619 PRUint32 i, capacity, entrySize;
620 PRBool didRemove;
621 PLDHashEntryHdr *entry;
622 PLDHashOperator op;
623#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
624 PRUint32 generation;
625
626 /*
627 * The hack! Set generation to PR_UINT32_MAX during the enumeration so
628 * we can prevent ChangeTable from being called.
629 *
630 * This happens during ipcDConnectService::OnClientStateChange()
631 * / ipcDConnectService::DeleteInstance() now when running
632 * java clienttest list hostinfo and vboxwebsrv crashes. It's quite
633 * likely that the IPC code isn't following the rules here, but it
634 * looks more difficult to fix that just hacking this hash code.
635 */
636 generation = table->generation;
637 table->generation = PR_UINT32_MAX;
638#endif /* VBOX */
639 entryAddr = table->entryStore;
640 entrySize = table->entrySize;
641 capacity = PL_DHASH_TABLE_SIZE(table);
642 entryLimit = entryAddr + capacity * entrySize;
643 i = 0;
644 didRemove = PR_FALSE;
645 while (entryAddr < entryLimit) {
646 entry = (PLDHashEntryHdr *)entryAddr;
647 if (ENTRY_IS_LIVE(entry)) {
648 op = etor(table, entry, i++, arg);
649#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
650 PR_ASSERT(table->generation == PR_UINT32_MAX);
651#endif
652 if (op & PL_DHASH_REMOVE) {
653 METER(table->stats.removeEnums++);
654 PL_DHashTableRawRemove(table, entry);
655 didRemove = PR_TRUE;
656 }
657 if (op & PL_DHASH_STOP)
658 break;
659 }
660 entryAddr += entrySize;
661 }
662#ifdef VBOX /* HACK ALERT! generation == PR_UINT32_MAX during enumeration. */
663 table->generation = generation;
664#endif
665
666 /*
667 * Shrink or compress if a quarter or more of all entries are removed, or
668 * if the table is underloaded according to the configured minimum alpha,
669 * and is not minimal-size already. Do this only if we removed above, so
670 * non-removing enumerations can count on stable table->entryStore until
671 * the next non-lookup-Operate or removing-Enumerate.
672 */
673 if (didRemove &&
674 (table->removedCount >= capacity >> 2 ||
675 (capacity > PL_DHASH_MIN_SIZE &&
676 table->entryCount <= MIN_LOAD(table, capacity)))) {
677 METER(table->stats.enumShrinks++);
678 capacity = table->entryCount;
679 capacity += capacity >> 1;
680 if (capacity < PL_DHASH_MIN_SIZE)
681 capacity = PL_DHASH_MIN_SIZE;
682 (void) ChangeTable(table,
683 PR_CeilingLog2(capacity)
684 - (PL_DHASH_BITS - table->hashShift));
685 }
686 return i;
687}
688
689#ifdef PL_DHASHMETER
690#include <math.h>
691
692PR_IMPLEMENT(void)
693PL_DHashTableDumpMeter(PLDHashTable *table, PLDHashEnumerator dump, FILE *fp)
694{
695 char *entryAddr;
696 PRUint32 entrySize, entryCount;
697 int hashShift, sizeLog2;
698 PRUint32 i, tableSize, sizeMask, chainLen, maxChainLen, chainCount;
699 PLDHashNumber hash1, hash2, saveHash1, maxChainHash1, maxChainHash2;
700 double sqsum, mean, variance, sigma;
701 PLDHashEntryHdr *entry, *probe;
702
703 entryAddr = table->entryStore;
704 entrySize = table->entrySize;
705 hashShift = table->hashShift;
706 sizeLog2 = PL_DHASH_BITS - hashShift;
707 tableSize = PL_DHASH_TABLE_SIZE(table);
708 sizeMask = PR_BITMASK(sizeLog2);
709 chainCount = maxChainLen = 0;
710 hash2 = 0;
711 sqsum = 0;
712
713 for (i = 0; i < tableSize; i++) {
714 entry = (PLDHashEntryHdr *)entryAddr;
715 entryAddr += entrySize;
716 if (!ENTRY_IS_LIVE(entry))
717 continue;
718 hash1 = HASH1(entry->keyHash & ~COLLISION_FLAG, hashShift);
719 saveHash1 = hash1;
720 probe = ADDRESS_ENTRY(table, hash1);
721 chainLen = 1;
722 if (probe == entry) {
723 /* Start of a (possibly unit-length) chain. */
724 chainCount++;
725 } else {
726 hash2 = HASH2(entry->keyHash & ~COLLISION_FLAG, sizeLog2,
727 hashShift);
728 do {
729 chainLen++;
730 hash1 -= hash2;
731 hash1 &= sizeMask;
732 probe = ADDRESS_ENTRY(table, hash1);
733 } while (probe != entry);
734 }
735 sqsum += chainLen * chainLen;
736 if (chainLen > maxChainLen) {
737 maxChainLen = chainLen;
738 maxChainHash1 = saveHash1;
739 maxChainHash2 = hash2;
740 }
741 }
742
743 entryCount = table->entryCount;
744 if (entryCount && chainCount) {
745 mean = (double)entryCount / chainCount;
746 variance = chainCount * sqsum - entryCount * entryCount;
747 if (variance < 0 || chainCount == 1)
748 variance = 0;
749 else
750 variance /= chainCount * (chainCount - 1);
751 sigma = sqrt(variance);
752 } else {
753 mean = sigma = 0;
754 }
755
756 fprintf(fp, "Double hashing statistics:\n");
757 fprintf(fp, " table size (in entries): %u\n", tableSize);
758 fprintf(fp, " number of entries: %u\n", table->entryCount);
759 fprintf(fp, " number of removed entries: %u\n", table->removedCount);
760 fprintf(fp, " number of searches: %u\n", table->stats.searches);
761 fprintf(fp, " number of hits: %u\n", table->stats.hits);
762 fprintf(fp, " number of misses: %u\n", table->stats.misses);
763 fprintf(fp, " mean steps per search: %g\n", table->stats.searches ?
764 (double)table->stats.steps
765 / table->stats.searches :
766 0.);
767 fprintf(fp, " mean hash chain length: %g\n", mean);
768 fprintf(fp, " standard deviation: %g\n", sigma);
769 fprintf(fp, " maximum hash chain length: %u\n", maxChainLen);
770 fprintf(fp, " number of lookups: %u\n", table->stats.lookups);
771 fprintf(fp, " adds that made a new entry: %u\n", table->stats.addMisses);
772 fprintf(fp, "adds that recycled removeds: %u\n", table->stats.addOverRemoved);
773 fprintf(fp, " adds that found an entry: %u\n", table->stats.addHits);
774 fprintf(fp, " add failures: %u\n", table->stats.addFailures);
775 fprintf(fp, " useful removes: %u\n", table->stats.removeHits);
776 fprintf(fp, " useless removes: %u\n", table->stats.removeMisses);
777 fprintf(fp, "removes that freed an entry: %u\n", table->stats.removeFrees);
778 fprintf(fp, " removes while enumerating: %u\n", table->stats.removeEnums);
779 fprintf(fp, " number of grows: %u\n", table->stats.grows);
780 fprintf(fp, " number of shrinks: %u\n", table->stats.shrinks);
781 fprintf(fp, " number of compresses: %u\n", table->stats.compresses);
782 fprintf(fp, "number of enumerate shrinks: %u\n", table->stats.enumShrinks);
783
784 if (dump && maxChainLen && hash2) {
785 fputs("Maximum hash chain:\n", fp);
786 hash1 = maxChainHash1;
787 hash2 = maxChainHash2;
788 entry = ADDRESS_ENTRY(table, hash1);
789 i = 0;
790 do {
791 if (dump(table, entry, i++, fp) != PL_DHASH_NEXT)
792 break;
793 hash1 -= hash2;
794 hash1 &= sizeMask;
795 entry = ADDRESS_ENTRY(table, hash1);
796 } while (PL_DHASH_ENTRY_IS_BUSY(entry));
797 }
798}
799#endif /* PL_DHASHMETER */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette