9 #define HASH_BUCKETS 512
10 #define HASH_MASK (HASH_BUCKETS - 1)
12 unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
14 static struct flist_head *file_hash;
15 static struct fio_mutex *hash_lock;
17 static unsigned short hash(const char *name)
19 return jhash(name, strlen(name), 0) & HASH_MASK;
22 void fio_file_hash_lock(void)
24 fio_mutex_down(hash_lock);
27 void fio_file_hash_unlock(void)
29 fio_mutex_up(hash_lock);
32 void remove_file_hash(struct fio_file *f)
34 fio_mutex_down(hash_lock);
36 if (fio_file_hashed(f)) {
37 assert(!flist_empty(&f->hash_list));
38 flist_del_init(&f->hash_list);
39 fio_file_clear_hashed(f);
42 fio_mutex_up(hash_lock);
45 static struct fio_file *__lookup_file_hash(const char *name)
47 struct flist_head *bucket = &file_hash[hash(name)];
50 flist_for_each(n, bucket) {
51 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
56 if (!strcmp(f->file_name, name)) {
65 struct fio_file *lookup_file_hash(const char *name)
69 fio_mutex_down(hash_lock);
70 f = __lookup_file_hash(name);
71 fio_mutex_up(hash_lock);
75 struct fio_file *add_file_hash(struct fio_file *f)
77 struct fio_file *alias;
79 if (fio_file_hashed(f))
82 INIT_FLIST_HEAD(&f->hash_list);
84 fio_mutex_down(hash_lock);
86 alias = __lookup_file_hash(f->file_name);
88 fio_file_set_hashed(f);
89 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
92 fio_mutex_up(hash_lock);
96 void file_hash_exit(void)
98 unsigned int i, has_entries = 0;
100 fio_mutex_down(hash_lock);
101 for (i = 0; i < HASH_BUCKETS; i++)
102 has_entries += !flist_empty(&file_hash[i]);
103 fio_mutex_up(hash_lock);
106 log_err("fio: file hash not empty on exit\n");
109 fio_mutex_remove(hash_lock);
113 void file_hash_init(void *ptr)
118 for (i = 0; i < HASH_BUCKETS; i++)
119 INIT_FLIST_HEAD(&file_hash[i]);
121 hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);