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