61aa6da32e570666bfe61108341222190bb61793
[fio.git] / filehash.c
1 #include <stdlib.h>
2 #include <assert.h>
3
4 #include "fio.h"
5 #include "flist.h"
6 #include "hash.h"
7 #include "filehash.h"
8 #include "lib/bloom.h"
9
10 #define HASH_BUCKETS    512
11 #define HASH_MASK       (HASH_BUCKETS - 1)
12
13 #define BLOOM_SIZE      16*1024*1024
14
15 unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
16
17 static struct flist_head *file_hash;
18 static struct fio_mutex *hash_lock;
19 static struct bloom *file_bloom;
20
21 static unsigned short hash(const char *name)
22 {
23         return jhash(name, strlen(name), 0) & HASH_MASK;
24 }
25
26 void fio_file_hash_lock(void)
27 {
28         if (hash_lock)
29                 fio_mutex_down(hash_lock);
30 }
31
32 void fio_file_hash_unlock(void)
33 {
34         if (hash_lock)
35                 fio_mutex_up(hash_lock);
36 }
37
38 void remove_file_hash(struct fio_file *f)
39 {
40         fio_mutex_down(hash_lock);
41
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);
46         }
47
48         fio_mutex_up(hash_lock);
49 }
50
51 static struct fio_file *__lookup_file_hash(const char *name)
52 {
53         struct flist_head *bucket = &file_hash[hash(name)];
54         struct flist_head *n;
55
56         flist_for_each(n, bucket) {
57                 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
58
59                 if (!f->file_name)
60                         continue;
61
62                 if (!strcmp(f->file_name, name)) {
63                         assert(f->fd != -1);
64                         return f;
65                 }
66         }
67
68         return NULL;
69 }
70
71 struct fio_file *lookup_file_hash(const char *name)
72 {
73         struct fio_file *f;
74
75         fio_mutex_down(hash_lock);
76         f = __lookup_file_hash(name);
77         fio_mutex_up(hash_lock);
78         return f;
79 }
80
81 struct fio_file *add_file_hash(struct fio_file *f)
82 {
83         struct fio_file *alias;
84
85         if (fio_file_hashed(f))
86                 return NULL;
87
88         INIT_FLIST_HEAD(&f->hash_list);
89
90         fio_mutex_down(hash_lock);
91
92         alias = __lookup_file_hash(f->file_name);
93         if (!alias) {
94                 fio_file_set_hashed(f);
95                 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
96         }
97
98         fio_mutex_up(hash_lock);
99         return alias;
100 }
101
102 bool file_bloom_exists(const char *fname, bool set)
103 {
104         return bloom_string(file_bloom, fname, strlen(fname), set);
105 }
106
107 void file_hash_exit(void)
108 {
109         unsigned int i, has_entries = 0;
110
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);
115
116         if (has_entries)
117                 log_err("fio: file hash not empty on exit\n");
118
119         file_hash = NULL;
120         fio_mutex_remove(hash_lock);
121         hash_lock = NULL;
122         bloom_free(file_bloom);
123         file_bloom = NULL;
124 }
125
126 void file_hash_init(void *ptr)
127 {
128         unsigned int i;
129
130         file_hash = ptr;
131         for (i = 0; i < HASH_BUCKETS; i++)
132                 INIT_FLIST_HEAD(&file_hash[i]);
133
134         hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
135         file_bloom = bloom_new(BLOOM_SIZE);
136 }