Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 1 | #include <stdlib.h> |
2 | #include <assert.h> | ||||
3 | |||||
4 | #include "fio.h" | ||||
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 5 | #include "flist.h" |
Jens Axboe | dadf66c | 2011-10-06 12:37:10 +0200 | [diff] [blame] | 6 | #include "hash.h" |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 7 | |
8 | #define HASH_BUCKETS 512 | ||||
9 | #define HASH_MASK (HASH_BUCKETS - 1) | ||||
10 | |||||
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 11 | unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 12 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 13 | static struct flist_head *file_hash; |
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 14 | static struct fio_mutex *hash_lock; |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 15 | |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 16 | static unsigned short hash(const char *name) |
17 | { | ||||
Jens Axboe | dadf66c | 2011-10-06 12:37:10 +0200 | [diff] [blame] | 18 | return jhash(name, strlen(name), 0) & HASH_MASK; |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 19 | } |
20 | |||||
21 | void remove_file_hash(struct fio_file *f) | ||||
22 | { | ||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 23 | fio_mutex_down(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 24 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 25 | if (fio_file_hashed(f)) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 26 | assert(!flist_empty(&f->hash_list)); |
27 | flist_del_init(&f->hash_list); | ||||
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 28 | fio_file_clear_hashed(f); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 29 | } |
30 | |||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 31 | fio_mutex_up(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 32 | } |
33 | |||||
34 | static struct fio_file *__lookup_file_hash(const char *name) | ||||
35 | { | ||||
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 36 | struct flist_head *bucket = &file_hash[hash(name)]; |
37 | struct flist_head *n; | ||||
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 38 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 39 | flist_for_each(n, bucket) { |
40 | struct fio_file *f = flist_entry(n, struct fio_file, hash_list); | ||||
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 41 | |
Jens Axboe | 8954110 | 2008-09-10 09:07:55 +0200 | [diff] [blame] | 42 | if (!f->file_name) |
43 | continue; | ||||
44 | |||||
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 45 | if (!strcmp(f->file_name, name)) { |
46 | assert(f->fd != -1); | ||||
47 | return f; | ||||
48 | } | ||||
49 | } | ||||
50 | |||||
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 51 | return NULL; |
52 | } | ||||
53 | |||||
54 | struct fio_file *lookup_file_hash(const char *name) | ||||
55 | { | ||||
56 | struct fio_file *f; | ||||
57 | |||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 58 | fio_mutex_down(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 59 | f = __lookup_file_hash(name); |
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 60 | fio_mutex_up(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 61 | return f; |
62 | } | ||||
63 | |||||
64 | struct fio_file *add_file_hash(struct fio_file *f) | ||||
65 | { | ||||
66 | struct fio_file *alias; | ||||
67 | |||||
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 68 | if (fio_file_hashed(f)) |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 69 | return NULL; |
70 | |||||
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 71 | INIT_FLIST_HEAD(&f->hash_list); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 72 | |
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 73 | fio_mutex_down(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 74 | |
75 | alias = __lookup_file_hash(f->file_name); | ||||
76 | if (!alias) { | ||||
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 77 | fio_file_set_hashed(f); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 78 | flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 79 | } |
80 | |||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 81 | fio_mutex_up(hash_lock); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 82 | return alias; |
83 | } | ||||
84 | |||||
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 85 | void file_hash_exit(void) |
86 | { | ||||
87 | unsigned int i, has_entries = 0; | ||||
88 | |||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 89 | fio_mutex_down(hash_lock); |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 90 | for (i = 0; i < HASH_BUCKETS; i++) |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 91 | has_entries += !flist_empty(&file_hash[i]); |
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 92 | fio_mutex_up(hash_lock); |
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 93 | |
94 | if (has_entries) | ||||
95 | log_err("fio: file hash not empty on exit\n"); | ||||
96 | |||||
Jens Axboe | b950781 | 2008-06-13 08:38:25 +0200 | [diff] [blame] | 97 | file_hash = NULL; |
98 | fio_mutex_remove(hash_lock); | ||||
Jens Axboe | 5e1d306 | 2008-05-23 11:55:53 +0200 | [diff] [blame] | 99 | hash_lock = NULL; |
100 | } | ||||
101 | |||||
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 102 | void file_hash_init(void *ptr) |
103 | { | ||||
104 | unsigned int i; | ||||
105 | |||||
106 | file_hash = ptr; | ||||
107 | for (i = 0; i < HASH_BUCKETS; i++) | ||||
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 108 | INIT_FLIST_HEAD(&file_hash[i]); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 109 | |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 110 | hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | 380065a | 2008-03-01 18:56:24 +0100 | [diff] [blame] | 111 | } |