10 #define HASH_BUCKETS 512
11 #define HASH_MASK (HASH_BUCKETS - 1)
13 #define BLOOM_SIZE 16*1024*1024
15 unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
17 static struct flist_head *file_hash;
18 static struct fio_mutex *hash_lock;
19 static struct bloom *file_bloom;
21 static unsigned short hash(const char *name)
23 return jhash(name, strlen(name), 0) & HASH_MASK;
26 void fio_file_hash_lock(void)
29 fio_mutex_down(hash_lock);
32 void fio_file_hash_unlock(void)
35 fio_mutex_up(hash_lock);
38 void remove_file_hash(struct fio_file *f)
40 fio_mutex_down(hash_lock);
42 if (fio_file_hashed(f)) {
43 assert(!flist_empty(&f->hash_list));
44 flist_del_init(&f->hash_list);
45 fio_file_clear_hashed(f);
48 fio_mutex_up(hash_lock);
51 static struct fio_file *__lookup_file_hash(const char *name)
53 struct flist_head *bucket = &file_hash[hash(name)];
56 flist_for_each(n, bucket) {
57 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
62 if (!strcmp(f->file_name, name)) {
71 struct fio_file *lookup_file_hash(const char *name)
75 fio_mutex_down(hash_lock);
76 f = __lookup_file_hash(name);
77 fio_mutex_up(hash_lock);
81 struct fio_file *add_file_hash(struct fio_file *f)
83 struct fio_file *alias;
85 if (fio_file_hashed(f))
88 INIT_FLIST_HEAD(&f->hash_list);
90 fio_mutex_down(hash_lock);
92 alias = __lookup_file_hash(f->file_name);
94 fio_file_set_hashed(f);
95 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
98 fio_mutex_up(hash_lock);
102 bool file_bloom_exists(const char *fname, bool set)
104 return bloom_string(file_bloom, fname, strlen(fname), set);
107 void file_hash_exit(void)
109 unsigned int i, has_entries = 0;
111 fio_mutex_down(hash_lock);
112 for (i = 0; i < HASH_BUCKETS; i++)
113 has_entries += !flist_empty(&file_hash[i]);
114 fio_mutex_up(hash_lock);
117 log_err("fio: file hash not empty on exit\n");
120 fio_mutex_remove(hash_lock);
122 bloom_free(file_bloom);
126 void file_hash_init(void *ptr)
131 for (i = 0; i < HASH_BUCKETS; i++)
132 INIT_FLIST_HEAD(&file_hash[i]);
134 hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
135 file_bloom = bloom_new(BLOOM_SIZE);