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 * lustre/obdclass/class_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 * * LH_DEBUG additional validation
46 * * LH_REHASH dynamic rehashing
47 * - Added per-hash statistics
48 * - General performance enhancements
52 #include <liblustre.h>
56 #include <class_hash.h>
59 lh_read_lock(lustre_hash_t *lh)
61 if ((lh->lh_flags & LH_REHASH) != 0)
62 read_lock(&lh->lh_rwlock);
66 lh_read_unlock(lustre_hash_t *lh)
68 if ((lh->lh_flags & LH_REHASH) != 0)
69 read_unlock(&lh->lh_rwlock);
73 lh_write_lock(lustre_hash_t *lh)
75 if ((lh->lh_flags & LH_REHASH) != 0)
76 write_lock(&lh->lh_rwlock);
80 lh_write_unlock(lustre_hash_t *lh)
82 if ((lh->lh_flags & LH_REHASH) != 0)
83 write_unlock(&lh->lh_rwlock);
87 * Initialize new lustre hash, where:
88 * @name - Descriptive hash name
89 * @cur_bits - Initial hash table size, in bits
90 * @max_bits - Maximum allowed hash table resize, in bits
91 * @ops - Registered hash table operations
92 * @flags - LH_REHASH enable synamic hash resizing
93 * - LH_SORT enable chained hash sort
96 lustre_hash_init(char *name, unsigned int cur_bits, unsigned int max_bits,
97 lustre_hash_ops_t *ops, int flags)
103 LASSERT(name != NULL);
104 LASSERT(ops != NULL);
106 LASSERT(cur_bits > 0);
107 LASSERT(max_bits >= cur_bits);
108 LASSERT(max_bits < 31);
110 LIBCFS_ALLOC_PTR(lh);
114 strncpy(lh->lh_name, name, sizeof(lh->lh_name));
115 lh->lh_name[sizeof(lh->lh_name) - 1] = '\0';
116 atomic_set(&lh->lh_rehash_count, 0);
117 atomic_set(&lh->lh_count, 0);
118 rwlock_init(&lh->lh_rwlock);
119 lh->lh_cur_bits = cur_bits;
120 lh->lh_cur_mask = (1 << cur_bits) - 1;
121 lh->lh_min_bits = cur_bits;
122 lh->lh_max_bits = max_bits;
123 /* XXX: need to fixup lustre_hash_rehash_bits() before this can be
124 * anything other than 0.5 and 2.0 */
125 lh->lh_min_theta = 1 << (LH_THETA_BITS - 1);
126 lh->lh_max_theta = 1 << (LH_THETA_BITS + 1);
128 lh->lh_flags = flags;
129 if (cur_bits != max_bits && (lh->lh_flags & LH_REHASH) == 0)
130 CERROR("Rehash is disabled for %s, ignore max_bits %d\n",
134 __lustre_hash_set_theta(lh, 500, 2000);
136 LIBCFS_ALLOC(lh->lh_buckets, sizeof(*lh->lh_buckets) << lh->lh_cur_bits);
137 if (!lh->lh_buckets) {
142 for (i = 0; i <= lh->lh_cur_mask; i++) {
143 LIBCFS_ALLOC(lh->lh_buckets[i], sizeof(lustre_hash_bucket_t));
144 if (lh->lh_buckets[i] == NULL) {
145 lustre_hash_exit(lh);
149 INIT_HLIST_HEAD(&lh->lh_buckets[i]->lhb_head);
150 rwlock_init(&lh->lh_buckets[i]->lhb_rwlock);
151 atomic_set(&lh->lh_buckets[i]->lhb_count, 0);
156 EXPORT_SYMBOL(lustre_hash_init);
159 * Cleanup lustre hash @lh.
162 lustre_hash_exit(lustre_hash_t *lh)
164 lustre_hash_bucket_t *lhb;
165 struct hlist_node *hnode;
166 struct hlist_node *pos;
174 lh_for_each_bucket(lh, lhb, i) {
178 write_lock(&lhb->lhb_rwlock);
179 hlist_for_each_safe(hnode, pos, &(lhb->lhb_head)) {
180 __lustre_hash_bucket_validate(lh, lhb, hnode);
181 __lustre_hash_bucket_del(lh, lhb, hnode);
185 LASSERTF(hlist_empty(&(lhb->lhb_head)),
186 "hash bucket %d from %s is not empty\n", i, lh->lh_name);
187 LASSERTF(atomic_read(&lhb->lhb_count) == 0,
188 "hash bucket %d from %s has #entries > 0 (%d)\n", i,
189 lh->lh_name, atomic_read(&lhb->lhb_count));
190 write_unlock(&lhb->lhb_rwlock);
191 LIBCFS_FREE_PTR(lhb);
194 LASSERTF(atomic_read(&lh->lh_count) == 0,
195 "hash %s still has #entries > 0 (%d)\n", lh->lh_name,
196 atomic_read(&lh->lh_count));
199 LIBCFS_FREE(lh->lh_buckets, sizeof(*lh->lh_buckets) << lh->lh_cur_bits);
203 EXPORT_SYMBOL(lustre_hash_exit);
205 static inline unsigned int lustre_hash_rehash_bits(lustre_hash_t *lh)
207 if (!(lh->lh_flags & LH_REHASH))
210 /* XXX: need to handle case with max_theta != 2.0
211 * and the case with min_theta != 0.5 */
212 if ((lh->lh_cur_bits < lh->lh_max_bits) &&
213 (__lustre_hash_theta(lh) > lh->lh_max_theta))
214 return lh->lh_cur_bits + 1;
216 if ((lh->lh_cur_bits > lh->lh_min_bits) &&
217 (__lustre_hash_theta(lh) < lh->lh_min_theta))
218 return lh->lh_cur_bits - 1;
224 * Add item @hnode to lustre hash @lh using @key. The registered
225 * ops->lh_get function will be called when the item is added.
228 lustre_hash_add(lustre_hash_t *lh, void *key, struct hlist_node *hnode)
230 lustre_hash_bucket_t *lhb;
235 __lustre_hash_key_validate(lh, key, hnode);
238 i = lh_hash(lh, key, lh->lh_cur_mask);
239 lhb = lh->lh_buckets[i];
240 LASSERT(i <= lh->lh_cur_mask);
241 LASSERT(hlist_unhashed(hnode));
243 write_lock(&lhb->lhb_rwlock);
244 __lustre_hash_bucket_add(lh, lhb, hnode);
245 write_unlock(&lhb->lhb_rwlock);
247 bits = lustre_hash_rehash_bits(lh);
250 lustre_hash_rehash(lh, bits);
254 EXPORT_SYMBOL(lustre_hash_add);
256 static struct hlist_node *
257 lustre_hash_findadd_unique_hnode(lustre_hash_t *lh, void *key,
258 struct hlist_node *hnode)
261 struct hlist_node *ehnode;
262 lustre_hash_bucket_t *lhb;
266 __lustre_hash_key_validate(lh, key, hnode);
269 i = lh_hash(lh, key, lh->lh_cur_mask);
270 lhb = lh->lh_buckets[i];
271 LASSERT(i <= lh->lh_cur_mask);
272 LASSERT(hlist_unhashed(hnode));
274 write_lock(&lhb->lhb_rwlock);
275 ehnode = __lustre_hash_bucket_lookup(lh, lhb, key);
279 __lustre_hash_bucket_add(lh, lhb, hnode);
281 bits = lustre_hash_rehash_bits(lh);
283 write_unlock(&lhb->lhb_rwlock);
286 lustre_hash_rehash(lh, bits);
292 * Add item @hnode to lustre hash @lh using @key. The registered
293 * ops->lh_get function will be called if the item was added.
294 * Returns 0 on success or -EALREADY on key collisions.
297 lustre_hash_add_unique(lustre_hash_t *lh, void *key, struct hlist_node *hnode)
299 struct hlist_node *ehnode;
302 ehnode = lustre_hash_findadd_unique_hnode(lh, key, hnode);
303 if (ehnode != hnode) {
309 EXPORT_SYMBOL(lustre_hash_add_unique);
312 * Add item @hnode to lustre hash @lh using @key. If this @key
313 * already exists in the hash then ops->lh_get will be called on the
314 * conflicting entry and that entry will be returned to the caller.
315 * Otherwise ops->lh_get is called on the item which was added.
318 lustre_hash_findadd_unique(lustre_hash_t *lh, void *key,
319 struct hlist_node *hnode)
321 struct hlist_node *ehnode;
325 ehnode = lustre_hash_findadd_unique_hnode(lh, key, hnode);
326 obj = lh_get(lh, ehnode);
330 EXPORT_SYMBOL(lustre_hash_findadd_unique);
333 * Delete item @hnode from the lustre hash @lh using @key. The @key
334 * is required to ensure the correct hash bucket is locked since there
335 * is no direct linkage from the item to the bucket. The object
336 * removed from the hash will be returned and obs->lh_put is called
337 * on the removed object.
340 lustre_hash_del(lustre_hash_t *lh, void *key, struct hlist_node *hnode)
342 lustre_hash_bucket_t *lhb;
347 __lustre_hash_key_validate(lh, key, hnode);
350 i = lh_hash(lh, key, lh->lh_cur_mask);
351 lhb = lh->lh_buckets[i];
352 LASSERT(i <= lh->lh_cur_mask);
354 write_lock(&lhb->lhb_rwlock);
355 LASSERT(!hlist_unhashed(hnode));
356 obj = __lustre_hash_bucket_del(lh, lhb, hnode);
357 write_unlock(&lhb->lhb_rwlock);
362 EXPORT_SYMBOL(lustre_hash_del);
365 * Delete item given @key in lustre hash @lh. The first @key found in
366 * the hash will be removed, if the key exists multiple times in the hash
367 * @lh this function must be called once per key. The removed object
368 * will be returned and ops->lh_put is called on the removed object.
371 lustre_hash_del_key(lustre_hash_t *lh, void *key)
373 struct hlist_node *hnode;
374 lustre_hash_bucket_t *lhb;
380 i = lh_hash(lh, key, lh->lh_cur_mask);
381 lhb = lh->lh_buckets[i];
382 LASSERT(i <= lh->lh_cur_mask);
384 write_lock(&lhb->lhb_rwlock);
385 hnode = __lustre_hash_bucket_lookup(lh, lhb, key);
387 obj = __lustre_hash_bucket_del(lh, lhb, hnode);
389 write_unlock(&lhb->lhb_rwlock);
394 EXPORT_SYMBOL(lustre_hash_del_key);
397 * Lookup an item using @key in the lustre hash @lh and return it.
398 * If the @key is found in the hash lh->lh_get() is called and the
399 * matching objects is returned. It is the callers responsibility
400 * to call the counterpart ops->lh_put using the lh_put() macro
401 * when when finished with the object. If the @key was not found
402 * in the hash @lh NULL is returned.
405 lustre_hash_lookup(lustre_hash_t *lh, void *key)
407 struct hlist_node *hnode;
408 lustre_hash_bucket_t *lhb;
414 i = lh_hash(lh, key, lh->lh_cur_mask);
415 lhb = lh->lh_buckets[i];
416 LASSERT(i <= lh->lh_cur_mask);
418 read_lock(&lhb->lhb_rwlock);
419 hnode = __lustre_hash_bucket_lookup(lh, lhb, key);
421 obj = lh_get(lh, hnode);
423 read_unlock(&lhb->lhb_rwlock);
428 EXPORT_SYMBOL(lustre_hash_lookup);
431 * For each item in the lustre hash @lh call the passed callback @func
432 * and pass to it as an argument each hash item and the private @data.
433 * Before each callback ops->lh_get will be called, and after each
434 * callback ops->lh_put will be called. Finally, during the callback
435 * the bucket lock is held so the callback must never sleep.
438 lustre_hash_for_each(lustre_hash_t *lh, lh_for_each_cb func, void *data)
440 struct hlist_node *hnode;
441 lustre_hash_bucket_t *lhb;
447 lh_for_each_bucket(lh, lhb, i) {
448 read_lock(&lhb->lhb_rwlock);
449 hlist_for_each(hnode, &(lhb->lhb_head)) {
450 __lustre_hash_bucket_validate(lh, lhb, hnode);
451 obj = lh_get(lh, hnode);
453 (void)lh_put(lh, hnode);
455 read_unlock(&lhb->lhb_rwlock);
461 EXPORT_SYMBOL(lustre_hash_for_each);
464 * For each item in the lustre hash @lh call the passed callback @func
465 * and pass to it as an argument each hash item and the private @data.
466 * Before each callback ops->lh_get will be called, and after each
467 * callback ops->lh_put will be called. During the callback the
468 * bucket lock will not be held will allows for the current item
469 * to be removed from the hash during the callback. However, care
470 * should be taken to prevent other callers from operating on the
471 * hash concurrently or list corruption may occur.
474 lustre_hash_for_each_safe(lustre_hash_t *lh, lh_for_each_cb func, void *data)
476 struct hlist_node *hnode;
477 struct hlist_node *pos;
478 lustre_hash_bucket_t *lhb;
484 lh_for_each_bucket(lh, lhb, i) {
485 read_lock(&lhb->lhb_rwlock);
486 hlist_for_each_safe(hnode, pos, &(lhb->lhb_head)) {
487 __lustre_hash_bucket_validate(lh, lhb, hnode);
488 obj = lh_get(lh, hnode);
489 read_unlock(&lhb->lhb_rwlock);
491 read_lock(&lhb->lhb_rwlock);
492 (void)lh_put(lh, hnode);
494 read_unlock(&lhb->lhb_rwlock);
499 EXPORT_SYMBOL(lustre_hash_for_each_safe);
502 * For each hash bucket in the lustre hash @lh call the passed callback
503 * @func until all the hash buckets are empty. The passed callback @func
504 * or the previously registered callback lh->lh_put must remove the item
505 * from the hash. You may either use the lustre_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 lustre_hash_for_each_empty(lustre_hash_t *lh, lh_for_each_cb func, void *data)
515 struct hlist_node *hnode;
516 lustre_hash_bucket_t *lhb;
517 lustre_hash_bucket_t **lhb_last = NULL;
524 /* If the hash table has changed since we last held lh_rwlock,
525 * we need to start traversing the list from the start. */
526 if (lh->lh_buckets != lhb_last) {
528 lhb_last = lh->lh_buckets;
530 lh_for_each_bucket_restart(lh, lhb, i) {
531 write_lock(&lhb->lhb_rwlock);
532 while (!hlist_empty(&lhb->lhb_head)) {
533 hnode = lhb->lhb_head.first;
534 __lustre_hash_bucket_validate(lh, lhb, hnode);
535 obj = lh_get(lh, hnode);
536 write_unlock(&lhb->lhb_rwlock);
539 (void)lh_put(lh, hnode);
543 write_unlock(&lhb->lhb_rwlock);
548 EXPORT_SYMBOL(lustre_hash_for_each_empty);
551 * For each item in the lustre hash @lh which matches the @key call
552 * the passed callback @func and pass to it as an argument each hash
553 * item and the private @data. Before each callback ops->lh_get will
554 * be called, and after each callback ops->lh_put will be called.
555 * Finally, during the callback the bucket lock is held so the
556 * callback must never sleep.
559 lustre_hash_for_each_key(lustre_hash_t *lh, void *key,
560 lh_for_each_cb func, void *data)
562 struct hlist_node *hnode;
563 lustre_hash_bucket_t *lhb;
568 i = lh_hash(lh, key, lh->lh_cur_mask);
569 lhb = lh->lh_buckets[i];
570 LASSERT(i <= lh->lh_cur_mask);
572 read_lock(&lhb->lhb_rwlock);
573 hlist_for_each(hnode, &(lhb->lhb_head)) {
574 __lustre_hash_bucket_validate(lh, lhb, hnode);
576 if (!lh_compare(lh, key, hnode))
579 func(lh_get(lh, hnode), data);
580 (void)lh_put(lh, hnode);
583 read_unlock(&lhb->lhb_rwlock);
588 EXPORT_SYMBOL(lustre_hash_for_each_key);
591 * Rehash the lustre hash @lh to the given @bits. This can be used
592 * to grow the hash size when excessive chaining is detected, or to
593 * shrink the hash when it is larger than needed. When the LH_REHASH
594 * flag is set in @lh the lustre hash may be dynamically rehashed
595 * during addition or removal if the hash's theta value exceeds
596 * either the lh->lh_min_theta or lh->max_theta values. By default
597 * these values are tuned to keep the chained hash depth small, and
598 * this approach assumes a reasonably uniform hashing function. The
599 * theta thresholds for @lh are tunable via lustre_hash_set_theta().
602 lustre_hash_rehash(lustre_hash_t *lh, int bits)
604 struct hlist_node *hnode;
605 struct hlist_node *pos;
606 lustre_hash_bucket_t **lh_buckets;
607 lustre_hash_bucket_t **rehash_buckets;
608 lustre_hash_bucket_t *lh_lhb;
609 lustre_hash_bucket_t *rehash_lhb;
614 int mask = (1 << bits) - 1;
619 LASSERT(!in_interrupt());
621 LASSERT((lh->lh_flags & LH_REHASH) != 0);
623 LIBCFS_ALLOC(rehash_buckets, sizeof(*rehash_buckets) << bits);
627 for (i = 0; i <= mask; i++) {
628 LIBCFS_ALLOC(rehash_buckets[i], sizeof(*rehash_buckets[i]));
629 if (rehash_buckets[i] == NULL)
630 GOTO(free, rc = -ENOMEM);
632 INIT_HLIST_HEAD(&rehash_buckets[i]->lhb_head);
633 rwlock_init(&rehash_buckets[i]->lhb_rwlock);
634 atomic_set(&rehash_buckets[i]->lhb_count, 0);
640 * Early return for multiple concurrent racing callers,
641 * ensure we only trigger the rehash if it is still needed.
643 theta = __lustre_hash_theta(lh);
644 if ((theta >= lh->lh_min_theta) && (theta <= lh->lh_max_theta)) {
646 GOTO(free, rc = -EALREADY);
649 lh_bits = lh->lh_cur_bits;
650 lh_buckets = lh->lh_buckets;
651 lh_mask = (1 << lh_bits) - 1;
653 lh->lh_cur_bits = bits;
654 lh->lh_cur_mask = (1 << bits) - 1;
655 lh->lh_buckets = rehash_buckets;
656 atomic_inc(&lh->lh_rehash_count);
658 for (i = 0; i <= lh_mask; i++) {
659 lh_lhb = lh_buckets[i];
661 write_lock(&lh_lhb->lhb_rwlock);
662 hlist_for_each_safe(hnode, pos, &(lh_lhb->lhb_head)) {
663 key = lh_key(lh, hnode);
667 * Validate hnode is in the correct bucket.
669 if (unlikely(lh->lh_flags & LH_DEBUG))
670 LASSERT(lh_hash(lh, key, lh_mask) == i);
673 * Delete from old hash bucket.
676 LASSERT(atomic_read(&lh_lhb->lhb_count) > 0);
677 atomic_dec(&lh_lhb->lhb_count);
680 * Add to rehash bucket, ops->lh_key must be defined.
682 rehash_lhb = rehash_buckets[lh_hash(lh, key, mask)];
683 hlist_add_head(hnode, &(rehash_lhb->lhb_head));
684 atomic_inc(&rehash_lhb->lhb_count);
687 LASSERT(hlist_empty(&(lh_lhb->lhb_head)));
688 LASSERT(atomic_read(&lh_lhb->lhb_count) == 0);
689 write_unlock(&lh_lhb->lhb_rwlock);
693 rehash_buckets = lh_buckets;
698 LIBCFS_FREE(rehash_buckets[i], sizeof(*rehash_buckets[i]));
699 LIBCFS_FREE(rehash_buckets, sizeof(*rehash_buckets) << bits);
703 EXPORT_SYMBOL(lustre_hash_rehash);
706 * Rehash the object referenced by @hnode in the lustre hash @lh. The
707 * @old_key must be provided to locate the objects previous location
708 * in the hash, and the @new_key will be used to reinsert the object.
709 * Use this function instead of a lustre_hash_add() + lustre_hash_del()
710 * combo when it is critical that there is no window in time where the
711 * object is missing from the hash. When an object is being rehashed
712 * the registered lh_get() and lh_put() functions will not be called.
714 void lustre_hash_rehash_key(lustre_hash_t *lh, void *old_key, void *new_key,
715 struct hlist_node *hnode)
717 lustre_hash_bucket_t *old_lhb;
718 lustre_hash_bucket_t *new_lhb;
723 __lustre_hash_key_validate(lh, new_key, hnode);
724 LASSERT(!hlist_unhashed(hnode));
728 i = lh_hash(lh, old_key, lh->lh_cur_mask);
729 old_lhb = lh->lh_buckets[i];
730 LASSERT(i <= lh->lh_cur_mask);
732 j = lh_hash(lh, new_key, lh->lh_cur_mask);
733 new_lhb = lh->lh_buckets[j];
734 LASSERT(j <= lh->lh_cur_mask);
736 if (i < j) { /* write_lock ordering */
737 write_lock(&old_lhb->lhb_rwlock);
738 write_lock(&new_lhb->lhb_rwlock);
740 write_lock(&new_lhb->lhb_rwlock);
741 write_lock(&old_lhb->lhb_rwlock);
742 } else { /* do nothing */
749 * Migrate item between hash buckets without calling
750 * the lh_get() and lh_put() callback functions.
753 LASSERT(atomic_read(&old_lhb->lhb_count) > 0);
754 atomic_dec(&old_lhb->lhb_count);
755 hlist_add_head(hnode, &(new_lhb->lhb_head));
756 atomic_inc(&new_lhb->lhb_count);
758 write_unlock(&new_lhb->lhb_rwlock);
759 write_unlock(&old_lhb->lhb_rwlock);
764 EXPORT_SYMBOL(lustre_hash_rehash_key);
766 int lustre_hash_debug_header(char *str, int size)
768 return snprintf(str, size,
769 "%-*s%6s%6s%6s%6s%6s%6s%6s%7s%6s%s\n", LUSTRE_MAX_HASH_NAME,
770 "name", "cur", "min", "max", "theta", "t-min", "t-max",
771 "flags", "rehash", "count", " distribution");
773 EXPORT_SYMBOL(lustre_hash_debug_header);
775 int lustre_hash_debug_str(lustre_hash_t *lh, char *str, int size)
777 lustre_hash_bucket_t *lhb;
781 int dist[8] = { 0, };
783 if (str == NULL || size == 0)
787 theta = __lustre_hash_theta(lh);
789 c += snprintf(str + c, size - c, "%-*s ",
790 LUSTRE_MAX_HASH_NAME, lh->lh_name);
791 c += snprintf(str + c, size - c, "%5d ", 1 << lh->lh_cur_bits);
792 c += snprintf(str + c, size - c, "%5d ", 1 << lh->lh_min_bits);
793 c += snprintf(str + c, size - c, "%5d ", 1 << lh->lh_max_bits);
794 c += snprintf(str + c, size - c, "%d.%03d ",
795 __lustre_hash_theta_int(theta),
796 __lustre_hash_theta_frac(theta));
797 c += snprintf(str + c, size - c, "%d.%03d ",
798 __lustre_hash_theta_int(lh->lh_min_theta),
799 __lustre_hash_theta_frac(lh->lh_min_theta));
800 c += snprintf(str + c, size - c, "%d.%03d ",
801 __lustre_hash_theta_int(lh->lh_max_theta),
802 __lustre_hash_theta_frac(lh->lh_max_theta));
803 c += snprintf(str + c, size - c, " 0x%02x ", lh->lh_flags);
804 c += snprintf(str + c, size - c, "%6d ",
805 atomic_read(&lh->lh_rehash_count));
806 c += snprintf(str + c, size - c, "%5d ",
807 atomic_read(&lh->lh_count));
810 * The distribution is a summary of the chained hash depth in
811 * each of the lustre hash buckets. Each buckets lhb_count is
812 * divided by the hash theta value and used to generate a
813 * histogram of the hash distribution. A uniform hash will
814 * result in all hash buckets being close to the average thus
815 * only the first few entries in the histogram will be non-zero.
816 * If you hash function results in a non-uniform hash the will
817 * be observable by outlier bucks in the distribution histogram.
819 * Uniform hash distribution: 128/128/0/0/0/0/0/0
820 * Non-Uniform hash distribution: 128/125/0/0/0/0/2/1
822 lh_for_each_bucket(lh, lhb, i)
823 dist[min(__fls(atomic_read(&lhb->lhb_count)/max(theta,1)),7)]++;
825 for (i = 0; i < 8; i++)
826 c += snprintf(str + c, size - c, "%d%c", dist[i],
827 (i == 7) ? '\n' : '/');
833 EXPORT_SYMBOL(lustre_hash_debug_str);