11 #define HASH_BUCKETS 512
12 #define HASH_MASK (HASH_BUCKETS - 1)
14 #define BLOOM_SIZE 16*1024*1024
16 static unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
18 static struct flist_head *file_hash;
19 static struct fio_sem *hash_lock;
20 static struct bloom *file_bloom;
22 static unsigned short hash(const char *name)
24 return jhash(name, strlen(name), 0) & HASH_MASK;
27 void fio_file_hash_lock(void)
30 fio_sem_down(hash_lock);
33 void fio_file_hash_unlock(void)
36 fio_sem_up(hash_lock);
39 void remove_file_hash(struct fio_file *f)
41 fio_sem_down(hash_lock);
43 if (fio_file_hashed(f)) {
44 assert(!flist_empty(&f->hash_list));
45 flist_del_init(&f->hash_list);
46 fio_file_clear_hashed(f);
49 fio_sem_up(hash_lock);
52 static struct fio_file *__lookup_file_hash(const char *name)
54 struct flist_head *bucket = &file_hash[hash(name)];
57 flist_for_each(n, bucket) {
58 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
63 if (!strcmp(f->file_name, name))
70 struct fio_file *lookup_file_hash(const char *name)
74 fio_sem_down(hash_lock);
75 f = __lookup_file_hash(name);
76 fio_sem_up(hash_lock);
80 struct fio_file *add_file_hash(struct fio_file *f)
82 struct fio_file *alias;
84 if (fio_file_hashed(f))
87 INIT_FLIST_HEAD(&f->hash_list);
89 fio_sem_down(hash_lock);
91 alias = __lookup_file_hash(f->file_name);
93 fio_file_set_hashed(f);
94 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
97 fio_sem_up(hash_lock);
101 bool file_bloom_exists(const char *fname, bool set)
103 return bloom_string(file_bloom, fname, strlen(fname), set);
106 void file_hash_exit(void)
108 unsigned int i, has_entries = 0;
110 fio_sem_down(hash_lock);
111 for (i = 0; i < HASH_BUCKETS; i++)
112 has_entries += !flist_empty(&file_hash[i]);
113 fio_sem_up(hash_lock);
116 log_err("fio: file hash not empty on exit\n");
120 fio_sem_remove(hash_lock);
122 bloom_free(file_bloom);
126 void file_hash_init(void)
130 file_hash = smalloc(file_hash_size);
132 for (i = 0; i < HASH_BUCKETS; i++)
133 INIT_FLIST_HEAD(&file_hash[i]);
135 hash_lock = fio_sem_init(FIO_SEM_UNLOCKED);
136 file_bloom = bloom_new(BLOOM_SIZE);