/** * Produces stat output about hashtable * * Parameters: * @param table - <usage> * <description> * * @param hashsize - <usage> * <description> * * @param nelems - <usage> * <description> * * @param longest - <usage> * <description> * * @return void * * */ void hashtable_get_stats( void* table, int* hashsize, int* nelems, int* longest) { HASHTABLE* ht; HASHENTRIES* entries; int i; int j; ht = (HASHTABLE *)table; CHK_HASHTABLE(ht); *nelems = 0; *longest = 0; hashtable_read_lock(ht); for (i = 0; i < ht->hashsize; i++) { j = 0; entries = ht->entries[i]; while (entries) { j++; entries = entries->next; } *nelems += j; if (j > *longest) { *longest = j; } } *hashsize = ht->hashsize; hashtable_read_unlock(ht); }
/** * test1 spinlock_acquire_nowait tests * * Test that spinlock_acquire_nowait returns false if the spinlock * is already taken. * * Test that spinlock_acquire_nowait returns true if the spinlock * is not taken. * * Test that spinlock_acquire_nowait does hold the spinlock. */ static bool do_hashtest( int argelems, int argsize) { bool succp = true; HASHTABLE* h; int nelems; int i; int* val_arr; int hsize; int longest; int* iter; ss_dfprintf(stderr, "testhash : creating hash table of size %d, including %d " "elements in total, at time %g.", argsize, argelems, (double)clock()-start); val_arr = (int *)malloc(sizeof(void *)*argelems); h = hashtable_alloc(argsize, hfun, cmpfun); ss_dfprintf(stderr, "\t..done\nAdd %d elements to hash table.", argelems); for (i=0; i<argelems; i++) { val_arr[i] = i; hashtable_add(h, (void *)&val_arr[i], (void *)&val_arr[i]); } if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start); ss_dfprintf(stderr, "\t..done\nRead hash table statistics."); hashtable_get_stats((void *)h, &hsize, &nelems, &longest); ss_dfprintf(stderr, "\t..done\nValidate read values."); ss_info_dassert(hsize == (argsize > 0 ? argsize: 1), "Invalid hash size"); ss_info_dassert((nelems == argelems) || (nelems == 0 && argsize == 0), "Invalid element count"); ss_info_dassert(longest <= nelems, "Too large longest list value"); if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start); ss_dfprintf(stderr, "\t..done\nValidate iterator."); HASHITERATOR *iterator = hashtable_iterator(h); read_lock(h); for (i=0; i < (argelems+1); i++) { iter = (int *)hashtable_next(iterator); if (iter == NULL) break; if (argelems < 100) ss_dfprintf(stderr, "\nNext item, iter = %d, i = %d", *iter, i); } read_unlock(h); ss_info_dassert((i == argelems) || (i == 0 && argsize == 0), "\nIncorrect number of elements from iterator"); hashtable_iterator_free(iterator); if (argelems > 1000) ss_dfprintf(stderr, "\t..done\nOperation took %g", (double)clock()-start); ss_dfprintf(stderr, "\t\t..done\n\nTest completed successfully.\n\n"); CHK_HASHTABLE(h); hashtable_free(h); free(val_arr); return succp; }