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