1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/hash.c
38 * Implement a hash class for hash process in lustre system.
40 * Author: YuZhangyong <yzy@clusterfs.com>
42 * 2008-08-15: Brian Behlendorf <behlendorf1@llnl.gov>
43 * - Simplified API and improved documentation
44 * - Added per-hash feature flags:
45 * * CFS_HASH_DEBUG additional validation
46 * * CFS_HASH_REHASH dynamic rehashing
47 * - Added per-hash statistics
48 * - General performance enhancements
50 * 2009-07-31: Liang Zhen <zhen.liang@sun.com>
51 * - move all stuff to libcfs
52 * - don't allow cur_bits != max_bits without setting of CFS_HASH_REHASH
53 * - ignore hs_rwlock if without CFS_HASH_REHASH setting
54 * - buckets are allocated one by one(intead of contiguous memory),
55 * to avoid unnecessary cacheline conflict
58 #include <libcfs/libcfs.h>
61 cfs_hash_rlock(cfs_hash_t *hs)
63 if ((hs->hs_flags & CFS_HASH_REHASH) != 0)
64 cfs_read_lock(&hs->hs_rwlock);
68 cfs_hash_runlock(cfs_hash_t *hs)
70 if ((hs->hs_flags & CFS_HASH_REHASH) != 0)
71 cfs_read_unlock(&hs->hs_rwlock);
75 cfs_hash_wlock(cfs_hash_t *hs)
77 if ((hs->hs_flags & CFS_HASH_REHASH) != 0)
78 cfs_write_lock(&hs->hs_rwlock);
82 cfs_hash_wunlock(cfs_hash_t *hs)
84 if ((hs->hs_flags & CFS_HASH_REHASH) != 0)
85 cfs_write_unlock(&hs->hs_rwlock);
89 * Initialize new libcfs hash, where:
90 * @name - Descriptive hash name
91 * @cur_bits - Initial hash table size, in bits
92 * @max_bits - Maximum allowed hash table resize, in bits
93 * @ops - Registered hash table operations
94 * @flags - CFS_HASH_REHASH enable synamic hash resizing
95 * - CFS_HASH_SORT enable chained hash sort
98 cfs_hash_create(char *name, unsigned int cur_bits,
99 unsigned int max_bits, cfs_hash_ops_t *ops, int flags)
105 LASSERT(name != NULL);
106 LASSERT(ops != NULL);
108 LASSERT(cur_bits > 0);
109 LASSERT(max_bits >= cur_bits);
110 LASSERT(max_bits < 31);
111 LASSERT(cur_bits == max_bits || (flags & CFS_HASH_REHASH) != 0);
117 strncpy(hs->hs_name, name, sizeof(hs->hs_name));
118 hs->hs_name[sizeof(hs->hs_name) - 1] = '\0';
119 cfs_atomic_set(&hs->hs_rehash_count, 0);
120 cfs_atomic_set(&hs->hs_count, 0);
121 cfs_rwlock_init(&hs->hs_rwlock);
122 hs->hs_cur_bits = cur_bits;
123 hs->hs_cur_mask = (1 << cur_bits) - 1;
124 hs->hs_min_bits = cur_bits;
125 hs->hs_max_bits = max_bits;
126 /* XXX: need to fixup cfs_hash_rehash_bits() before this can be
127 * anything other than 0.5 and 2.0 */
128 hs->hs_min_theta = 1 << (CFS_HASH_THETA_BITS - 1);
129 hs->hs_max_theta = 1 << (CFS_HASH_THETA_BITS + 1);
131 hs->hs_flags = flags;
134 __cfs_hash_set_theta(hs, 500, 2000);
136 LIBCFS_ALLOC(hs->hs_buckets,
137 sizeof(*hs->hs_buckets) << hs->hs_cur_bits);
138 if (hs->hs_buckets == NULL) {
143 for (i = 0; i <= hs->hs_cur_mask; i++) {
144 CFS_ALLOC_PTR(hs->hs_buckets[i]);
145 if (hs->hs_buckets[i] == NULL) {
146 cfs_hash_destroy(hs);
150 CFS_INIT_HLIST_HEAD(&hs->hs_buckets[i]->hsb_head);
151 cfs_rwlock_init(&hs->hs_buckets[i]->hsb_rwlock);
152 cfs_atomic_set(&hs->hs_buckets[i]->hsb_count, 0);
157 CFS_EXPORT_SYMBOL(cfs_hash_create);
160 * Cleanup libcfs hash @hs.
163 cfs_hash_destroy(cfs_hash_t *hs)
165 cfs_hash_bucket_t *hsb;
166 cfs_hlist_node_t *hnode;
167 cfs_hlist_node_t *pos;
175 cfs_hash_for_each_bucket(hs, hsb, i) {
179 cfs_write_lock(&hsb->hsb_rwlock);
180 cfs_hlist_for_each_safe(hnode, pos, &(hsb->hsb_head)) {
181 __cfs_hash_bucket_validate(hs, hsb, hnode);
182 __cfs_hash_bucket_del(hs, hsb, hnode);
183 cfs_hash_exit(hs, hnode);
186 LASSERT(cfs_hlist_empty(&(hsb->hsb_head)));
187 LASSERT(cfs_atomic_read(&hsb->hsb_count) == 0);
188 cfs_write_unlock(&hsb->hsb_rwlock);
192 LASSERT(cfs_atomic_read(&hs->hs_count) == 0);
193 cfs_hash_wunlock(hs);
195 LIBCFS_FREE(hs->hs_buckets,
196 sizeof(*hs->hs_buckets) << hs->hs_cur_bits);
200 CFS_EXPORT_SYMBOL(cfs_hash_destroy);
202 static inline unsigned int
203 cfs_hash_rehash_bits(cfs_hash_t *hs)
205 if (!(hs->hs_flags & CFS_HASH_REHASH))
208 /* XXX: need to handle case with max_theta != 2.0
209 * and the case with min_theta != 0.5 */
210 if ((hs->hs_cur_bits < hs->hs_max_bits) &&
211 (__cfs_hash_theta(hs) > hs->hs_max_theta))
212 return hs->hs_cur_bits + 1;
214 if ((hs->hs_cur_bits > hs->hs_min_bits) &&
215 (__cfs_hash_theta(hs) < hs->hs_min_theta))
216 return hs->hs_cur_bits - 1;
222 * Add item @hnode to libcfs hash @hs using @key. The registered
223 * ops->hs_get function will be called when the item is added.
226 cfs_hash_add(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
228 cfs_hash_bucket_t *hsb;
233 __cfs_hash_key_validate(hs, key, hnode);
236 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
237 hsb = hs->hs_buckets[i];
238 LASSERT(i <= hs->hs_cur_mask);
239 LASSERT(cfs_hlist_unhashed(hnode));
241 cfs_write_lock(&hsb->hsb_rwlock);
242 __cfs_hash_bucket_add(hs, hsb, hnode);
243 cfs_write_unlock(&hsb->hsb_rwlock);
245 bits = cfs_hash_rehash_bits(hs);
246 cfs_hash_runlock(hs);
248 cfs_hash_rehash(hs, bits);
252 CFS_EXPORT_SYMBOL(cfs_hash_add);
254 static cfs_hlist_node_t *
255 cfs_hash_findadd_unique_hnode(cfs_hash_t *hs, void *key,
256 cfs_hlist_node_t *hnode)
259 cfs_hlist_node_t *ehnode;
260 cfs_hash_bucket_t *hsb;
264 __cfs_hash_key_validate(hs, key, hnode);
267 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
268 hsb = hs->hs_buckets[i];
269 LASSERT(i <= hs->hs_cur_mask);
270 LASSERT(cfs_hlist_unhashed(hnode));
272 cfs_write_lock(&hsb->hsb_rwlock);
273 ehnode = __cfs_hash_bucket_lookup(hs, hsb, key);
275 cfs_hash_get(hs, ehnode);
277 __cfs_hash_bucket_add(hs, hsb, hnode);
279 bits = cfs_hash_rehash_bits(hs);
281 cfs_write_unlock(&hsb->hsb_rwlock);
282 cfs_hash_runlock(hs);
284 cfs_hash_rehash(hs, bits);
290 * Add item @hnode to libcfs hash @hs using @key. The registered
291 * ops->hs_get function will be called if the item was added.
292 * Returns 0 on success or -EALREADY on key collisions.
295 cfs_hash_add_unique(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
297 cfs_hlist_node_t *ehnode;
300 ehnode = cfs_hash_findadd_unique_hnode(hs, key, hnode);
301 if (ehnode != hnode) {
302 cfs_hash_put(hs, ehnode);
307 CFS_EXPORT_SYMBOL(cfs_hash_add_unique);
310 * Add item @hnode to libcfs hash @hs using @key. If this @key
311 * already exists in the hash then ops->hs_get will be called on the
312 * conflicting entry and that entry will be returned to the caller.
313 * Otherwise ops->hs_get is called on the item which was added.
316 cfs_hash_findadd_unique(cfs_hash_t *hs, void *key,
317 cfs_hlist_node_t *hnode)
319 cfs_hlist_node_t *ehnode;
323 ehnode = cfs_hash_findadd_unique_hnode(hs, key, hnode);
324 obj = cfs_hash_get(hs, ehnode);
325 cfs_hash_put(hs, ehnode);
328 CFS_EXPORT_SYMBOL(cfs_hash_findadd_unique);
331 * Delete item @hnode from the libcfs hash @hs using @key. The @key
332 * is required to ensure the correct hash bucket is locked since there
333 * is no direct linkage from the item to the bucket. The object
334 * removed from the hash will be returned and obs->hs_put is called
335 * on the removed object.
338 cfs_hash_del(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
340 cfs_hash_bucket_t *hsb;
345 __cfs_hash_key_validate(hs, key, hnode);
348 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
349 hsb = hs->hs_buckets[i];
350 LASSERT(i <= hs->hs_cur_mask);
351 LASSERT(!cfs_hlist_unhashed(hnode));
353 cfs_write_lock(&hsb->hsb_rwlock);
354 obj = __cfs_hash_bucket_del(hs, hsb, hnode);
355 cfs_write_unlock(&hsb->hsb_rwlock);
356 cfs_hash_runlock(hs);
360 CFS_EXPORT_SYMBOL(cfs_hash_del);
363 * Delete item given @key in libcfs hash @hs. The first @key found in
364 * the hash will be removed, if the key exists multiple times in the hash
365 * @hs this function must be called once per key. The removed object
366 * will be returned and ops->hs_put is called on the removed object.
369 cfs_hash_del_key(cfs_hash_t *hs, void *key)
372 cfs_hlist_node_t *hnode;
373 cfs_hash_bucket_t *hsb;
378 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
379 hsb = hs->hs_buckets[i];
380 LASSERT(i <= hs->hs_cur_mask);
382 cfs_write_lock(&hsb->hsb_rwlock);
383 hnode = __cfs_hash_bucket_lookup(hs, hsb, key);
385 obj = __cfs_hash_bucket_del(hs, hsb, hnode);
387 cfs_write_unlock(&hsb->hsb_rwlock);
388 cfs_hash_runlock(hs);
392 CFS_EXPORT_SYMBOL(cfs_hash_del_key);
395 * Lookup an item using @key in the libcfs hash @hs and return it.
396 * If the @key is found in the hash hs->hs_get() is called and the
397 * matching objects is returned. It is the callers responsibility
398 * to call the counterpart ops->hs_put using the cfs_hash_put() macro
399 * when when finished with the object. If the @key was not found
400 * in the hash @hs NULL is returned.
403 cfs_hash_lookup(cfs_hash_t *hs, void *key)
406 cfs_hlist_node_t *hnode;
407 cfs_hash_bucket_t *hsb;
412 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
413 hsb = hs->hs_buckets[i];
414 LASSERT(i <= hs->hs_cur_mask);
416 cfs_read_lock(&hsb->hsb_rwlock);
417 hnode = __cfs_hash_bucket_lookup(hs, hsb, key);
419 obj = cfs_hash_get(hs, hnode);
421 cfs_read_unlock(&hsb->hsb_rwlock);
422 cfs_hash_runlock(hs);
426 CFS_EXPORT_SYMBOL(cfs_hash_lookup);
429 * For each item in the libcfs hash @hs call the passed callback @func
430 * and pass to it as an argument each hash item and the private @data.
431 * Before each callback ops->hs_get will be called, and after each
432 * callback ops->hs_put will be called. Finally, during the callback
433 * the bucket lock is held so the callback must never sleep.
436 cfs_hash_for_each(cfs_hash_t *hs,
437 cfs_hash_for_each_cb_t func, void *data)
439 cfs_hlist_node_t *hnode;
440 cfs_hash_bucket_t *hsb;
446 cfs_hash_for_each_bucket(hs, hsb, i) {
447 cfs_read_lock(&hsb->hsb_rwlock);
448 cfs_hlist_for_each(hnode, &(hsb->hsb_head)) {
449 __cfs_hash_bucket_validate(hs, hsb, hnode);
450 obj = cfs_hash_get(hs, hnode);
452 (void)cfs_hash_put(hs, hnode);
454 cfs_read_unlock(&hsb->hsb_rwlock);
456 cfs_hash_runlock(hs);
460 CFS_EXPORT_SYMBOL(cfs_hash_for_each);
463 * For each item in the libcfs hash @hs call the passed callback @func
464 * and pass to it as an argument each hash item and the private @data.
465 * Before each callback ops->hs_get will be called, and after each
466 * callback ops->hs_put will be called. During the callback the
467 * bucket lock will not be held will allows for the current item
468 * to be removed from the hash during the callback. However, care
469 * should be taken to prevent other callers from operating on the
470 * hash concurrently or list corruption may occur.
473 cfs_hash_for_each_safe(cfs_hash_t *hs,
474 cfs_hash_for_each_cb_t func, void *data)
476 cfs_hlist_node_t *hnode;
477 cfs_hlist_node_t *pos;
478 cfs_hash_bucket_t *hsb;
484 cfs_hash_for_each_bucket(hs, hsb, i) {
485 cfs_read_lock(&hsb->hsb_rwlock);
486 cfs_hlist_for_each_safe(hnode, pos, &(hsb->hsb_head)) {
487 __cfs_hash_bucket_validate(hs, hsb, hnode);
488 obj = cfs_hash_get(hs, hnode);
489 cfs_read_unlock(&hsb->hsb_rwlock);
491 cfs_read_lock(&hsb->hsb_rwlock);
492 (void)cfs_hash_put(hs, hnode);
494 cfs_read_unlock(&hsb->hsb_rwlock);
496 cfs_hash_runlock(hs);
499 CFS_EXPORT_SYMBOL(cfs_hash_for_each_safe);
502 * For each hash bucket in the libcfs hash @hs call the passed callback
503 * @func until all the hash buckets are empty. The passed callback @func
504 * or the previously registered callback hs->hs_put must remove the item
505 * from the hash. You may either use the cfs_hash_del() or hlist_del()
506 * functions. No rwlocks will be held during the callback @func it is
507 * safe to sleep if needed. This function will not terminate until the
508 * hash is empty. Note it is still possible to concurrently add new
509 * items in to the hash. It is the callers responsibility to ensure
510 * the required locking is in place to prevent concurrent insertions.
513 cfs_hash_for_each_empty(cfs_hash_t *hs,
514 cfs_hash_for_each_cb_t func, void *data)
516 cfs_hlist_node_t *hnode;
517 cfs_hash_bucket_t *hsb;
518 cfs_hash_bucket_t **hsb_last = NULL;
525 /* If the hash table has changed since we last held lh_rwlock,
526 * we need to start traversing the list from the start. */
527 if (hs->hs_buckets != hsb_last) {
529 hsb_last = hs->hs_buckets;
531 cfs_hash_for_each_bucket_restart(hs, hsb, i) {
532 cfs_write_lock(&hsb->hsb_rwlock);
533 while (!cfs_hlist_empty(&hsb->hsb_head)) {
534 hnode = hsb->hsb_head.first;
535 __cfs_hash_bucket_validate(hs, hsb, hnode);
536 obj = cfs_hash_get(hs, hnode);
537 cfs_write_unlock(&hsb->hsb_rwlock);
538 cfs_hash_runlock(hs);
540 (void)cfs_hash_put(hs, hnode);
544 cfs_write_unlock(&hsb->hsb_rwlock);
546 cfs_hash_runlock(hs);
549 CFS_EXPORT_SYMBOL(cfs_hash_for_each_empty);
552 * For each item in the libcfs hash @hs which matches the @key call
553 * the passed callback @func and pass to it as an argument each hash
554 * item and the private @data. Before each callback ops->hs_get will
555 * be called, and after each callback ops->hs_put will be called.
556 * Finally, during the callback the bucket lock is held so the
557 * callback must never sleep.
560 cfs_hash_for_each_key(cfs_hash_t *hs, void *key,
561 cfs_hash_for_each_cb_t func, void *data)
563 cfs_hlist_node_t *hnode;
564 cfs_hash_bucket_t *hsb;
569 i = cfs_hash_id(hs, key, hs->hs_cur_mask);
570 hsb = hs->hs_buckets[i];
571 LASSERT(i <= hs->hs_cur_mask);
573 cfs_read_lock(&hsb->hsb_rwlock);
574 cfs_hlist_for_each(hnode, &(hsb->hsb_head)) {
575 __cfs_hash_bucket_validate(hs, hsb, hnode);
577 if (!cfs_hash_compare(hs, key, hnode))
580 func(cfs_hash_get(hs, hnode), data);
581 (void)cfs_hash_put(hs, hnode);
584 cfs_read_unlock(&hsb->hsb_rwlock);
585 cfs_hash_runlock(hs);
589 CFS_EXPORT_SYMBOL(cfs_hash_for_each_key);
592 * Rehash the libcfs hash @hs to the given @bits. This can be used
593 * to grow the hash size when excessive chaining is detected, or to
594 * shrink the hash when it is larger than needed. When the CFS_HASH_REHASH
595 * flag is set in @hs the libcfs hash may be dynamically rehashed
596 * during addition or removal if the hash's theta value exceeds
597 * either the hs->hs_min_theta or hs->max_theta values. By default
598 * these values are tuned to keep the chained hash depth small, and
599 * this approach assumes a reasonably uniform hashing function. The
600 * theta thresholds for @hs are tunable via cfs_hash_set_theta().
603 cfs_hash_rehash(cfs_hash_t *hs, int bits)
605 cfs_hlist_node_t *hnode;
606 cfs_hlist_node_t *pos;
607 cfs_hash_bucket_t **old_buckets;
608 cfs_hash_bucket_t **rehash_buckets;
609 cfs_hash_bucket_t *hs_hsb;
610 cfs_hash_bucket_t *rehash_hsb;
615 int new_mask = (1 << bits) - 1;
620 LASSERT(!cfs_in_interrupt());
621 LASSERT(new_mask > 0);
622 LASSERT((hs->hs_flags & CFS_HASH_REHASH) != 0);
624 LIBCFS_ALLOC(rehash_buckets, sizeof(*rehash_buckets) << bits);
628 for (i = 0; i <= new_mask; i++) {
629 CFS_ALLOC_PTR(rehash_buckets[i]);
630 if (rehash_buckets[i] == NULL)
631 GOTO(free, rc = -ENOMEM);
633 CFS_INIT_HLIST_HEAD(&rehash_buckets[i]->hsb_head);
634 cfs_rwlock_init(&rehash_buckets[i]->hsb_rwlock);
635 cfs_atomic_set(&rehash_buckets[i]->hsb_count, 0);
641 * Early return for multiple concurrent racing callers,
642 * ensure we only trigger the rehash if it is still needed.
644 theta = __cfs_hash_theta(hs);
645 if ((theta >= hs->hs_min_theta) && (theta <= hs->hs_max_theta)) {
646 cfs_hash_wunlock(hs);
647 GOTO(free, rc = -EALREADY);
650 old_bits = hs->hs_cur_bits;
651 old_buckets = hs->hs_buckets;
652 old_mask = (1 << old_bits) - 1;
654 hs->hs_cur_bits = bits;
655 hs->hs_cur_mask = (1 << bits) - 1;
656 hs->hs_buckets = rehash_buckets;
657 cfs_atomic_inc(&hs->hs_rehash_count);
659 for (i = 0; i <= old_mask; i++) {
660 hs_hsb = old_buckets[i];
662 cfs_write_lock(&hs_hsb->hsb_rwlock);
663 cfs_hlist_for_each_safe(hnode, pos, &(hs_hsb->hsb_head)) {
664 key = cfs_hash_key(hs, hnode);
668 * Validate hnode is in the correct bucket.
670 if (unlikely(hs->hs_flags & CFS_HASH_DEBUG))
671 LASSERT(cfs_hash_id(hs, key, old_mask) == i);
674 * Delete from old hash bucket.
676 cfs_hlist_del(hnode);
677 LASSERT(cfs_atomic_read(&hs_hsb->hsb_count) > 0);
678 cfs_atomic_dec(&hs_hsb->hsb_count);
681 * Add to rehash bucket, ops->hs_key must be defined.
683 rehash_hsb = rehash_buckets[cfs_hash_id(hs, key,
685 cfs_hlist_add_head(hnode, &(rehash_hsb->hsb_head));
686 cfs_atomic_inc(&rehash_hsb->hsb_count);
689 LASSERT(cfs_hlist_empty(&(hs_hsb->hsb_head)));
690 LASSERT(cfs_atomic_read(&hs_hsb->hsb_count) == 0);
691 cfs_write_unlock(&hs_hsb->hsb_rwlock);
694 cfs_hash_wunlock(hs);
695 rehash_buckets = old_buckets;
700 CFS_FREE_PTR(rehash_buckets[i]);
701 LIBCFS_FREE(rehash_buckets, sizeof(*rehash_buckets) << bits);
704 CFS_EXPORT_SYMBOL(cfs_hash_rehash);
707 * Rehash the object referenced by @hnode in the libcfs hash @hs. The
708 * @old_key must be provided to locate the objects previous location
709 * in the hash, and the @new_key will be used to reinsert the object.
710 * Use this function instead of a cfs_hash_add() + cfs_hash_del()
711 * combo when it is critical that there is no window in time where the
712 * object is missing from the hash. When an object is being rehashed
713 * the registered cfs_hash_get() and cfs_hash_put() functions will
716 void cfs_hash_rehash_key(cfs_hash_t *hs, void *old_key, void *new_key,
717 cfs_hlist_node_t *hnode)
719 cfs_hash_bucket_t *old_hsb;
720 cfs_hash_bucket_t *new_hsb;
725 __cfs_hash_key_validate(hs, new_key, hnode);
726 LASSERT(!cfs_hlist_unhashed(hnode));
730 i = cfs_hash_id(hs, old_key, hs->hs_cur_mask);
731 old_hsb = hs->hs_buckets[i];
732 LASSERT(i <= hs->hs_cur_mask);
734 j = cfs_hash_id(hs, new_key, hs->hs_cur_mask);
735 new_hsb = hs->hs_buckets[j];
736 LASSERT(j <= hs->hs_cur_mask);
738 if (i < j) { /* write_lock ordering */
739 cfs_write_lock(&old_hsb->hsb_rwlock);
740 cfs_write_lock(&new_hsb->hsb_rwlock);
742 cfs_write_lock(&new_hsb->hsb_rwlock);
743 cfs_write_lock(&old_hsb->hsb_rwlock);
744 } else { /* do nothing */
745 cfs_read_unlock(&hs->hs_rwlock);
751 * Migrate item between hash buckets without calling
752 * the cfs_hash_get() and cfs_hash_put() callback functions.
754 cfs_hlist_del(hnode);
755 LASSERT(cfs_atomic_read(&old_hsb->hsb_count) > 0);
756 cfs_atomic_dec(&old_hsb->hsb_count);
757 cfs_hlist_add_head(hnode, &(new_hsb->hsb_head));
758 cfs_atomic_inc(&new_hsb->hsb_count);
760 cfs_write_unlock(&new_hsb->hsb_rwlock);
761 cfs_write_unlock(&old_hsb->hsb_rwlock);
762 cfs_hash_runlock(hs);
766 CFS_EXPORT_SYMBOL(cfs_hash_rehash_key);
768 int cfs_hash_debug_header(char *str, int size)
770 return snprintf(str, size,
771 "%-*s%6s%6s%6s%6s%6s%6s%6s%7s%6s%s\n", CFS_MAX_HASH_NAME,
772 "name", "cur", "min", "max", "theta", "t-min", "t-max",
773 "flags", "rehash", "count", " distribution");
775 CFS_EXPORT_SYMBOL(cfs_hash_debug_header);
777 int cfs_hash_debug_str(cfs_hash_t *hs, char *str, int size)
779 cfs_hash_bucket_t *hsb;
783 int dist[8] = { 0, };
785 if (str == NULL || size == 0)
789 theta = __cfs_hash_theta(hs);
791 c += snprintf(str + c, size - c, "%-*s ",
792 CFS_MAX_HASH_NAME, hs->hs_name);
793 c += snprintf(str + c, size - c, "%5d ", 1 << hs->hs_cur_bits);
794 c += snprintf(str + c, size - c, "%5d ", 1 << hs->hs_min_bits);
795 c += snprintf(str + c, size - c, "%5d ", 1 << hs->hs_max_bits);
796 c += snprintf(str + c, size - c, "%d.%03d ",
797 __cfs_hash_theta_int(theta),
798 __cfs_hash_theta_frac(theta));
799 c += snprintf(str + c, size - c, "%d.%03d ",
800 __cfs_hash_theta_int(hs->hs_min_theta),
801 __cfs_hash_theta_frac(hs->hs_min_theta));
802 c += snprintf(str + c, size - c, "%d.%03d ",
803 __cfs_hash_theta_int(hs->hs_max_theta),
804 __cfs_hash_theta_frac(hs->hs_max_theta));
805 c += snprintf(str + c, size - c, " 0x%02x ", hs->hs_flags);
806 c += snprintf(str + c, size - c, "%6d ",
807 cfs_atomic_read(&hs->hs_rehash_count));
808 c += snprintf(str + c, size - c, "%5d ",
809 cfs_atomic_read(&hs->hs_count));
812 * The distribution is a summary of the chained hash depth in
813 * each of the libcfs hash buckets. Each buckets hsb_count is
814 * divided by the hash theta value and used to generate a
815 * histogram of the hash distribution. A uniform hash will
816 * result in all hash buckets being close to the average thus
817 * only the first few entries in the histogram will be non-zero.
818 * If you hash function results in a non-uniform hash the will
819 * be observable by outlier bucks in the distribution histogram.
821 * Uniform hash distribution: 128/128/0/0/0/0/0/0
822 * Non-Uniform hash distribution: 128/125/0/0/0/0/2/1
824 cfs_hash_for_each_bucket(hs, hsb, i)
825 dist[min(__cfs_fls(cfs_atomic_read(&hsb->hsb_count)/max(theta,1)),7)]++;
827 for (i = 0; i < 8; i++)
828 c += snprintf(str + c, size - c, "%d%c", dist[i],
829 (i == 7) ? '\n' : '/');
831 cfs_hash_runlock(hs);
835 CFS_EXPORT_SYMBOL(cfs_hash_debug_str);