blob: 0d61f54cca0b2f8c526cd639f9749716069aa8ff [file] [log] [blame]
Jens Axboe380065a2008-03-01 18:56:24 +01001#include <stdlib.h>
2#include <assert.h>
3
4#include "fio.h"
Jens Axboe01743ee2008-06-02 12:19:19 +02005#include "flist.h"
Jens Axboedadf66c2011-10-06 12:37:10 +02006#include "hash.h"
Jens Axboe10aa1362014-04-01 21:10:36 -06007#include "filehash.h"
Jens Axboe380065a2008-03-01 18:56:24 +01008
9#define HASH_BUCKETS 512
10#define HASH_MASK (HASH_BUCKETS - 1)
11
Jens Axboe01743ee2008-06-02 12:19:19 +020012unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
Jens Axboe380065a2008-03-01 18:56:24 +010013
Jens Axboe01743ee2008-06-02 12:19:19 +020014static struct flist_head *file_hash;
Jens Axboeb9507812008-06-13 08:38:25 +020015static struct fio_mutex *hash_lock;
Jens Axboe380065a2008-03-01 18:56:24 +010016
Jens Axboe380065a2008-03-01 18:56:24 +010017static unsigned short hash(const char *name)
18{
Jens Axboedadf66c2011-10-06 12:37:10 +020019 return jhash(name, strlen(name), 0) & HASH_MASK;
Jens Axboe380065a2008-03-01 18:56:24 +010020}
21
Jens Axboe90426232014-04-01 12:28:47 -060022void fio_file_hash_lock(void)
23{
Jens Axboe45aeca72014-04-02 12:25:16 -060024 if (hash_lock)
25 fio_mutex_down(hash_lock);
Jens Axboe90426232014-04-01 12:28:47 -060026}
27
28void fio_file_hash_unlock(void)
29{
Jens Axboe45aeca72014-04-02 12:25:16 -060030 if (hash_lock)
31 fio_mutex_up(hash_lock);
Jens Axboe90426232014-04-01 12:28:47 -060032}
33
Jens Axboe380065a2008-03-01 18:56:24 +010034void remove_file_hash(struct fio_file *f)
35{
Jens Axboeb9507812008-06-13 08:38:25 +020036 fio_mutex_down(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010037
Jens Axboed6aed792009-06-03 08:41:15 +020038 if (fio_file_hashed(f)) {
Jens Axboe01743ee2008-06-02 12:19:19 +020039 assert(!flist_empty(&f->hash_list));
40 flist_del_init(&f->hash_list);
Jens Axboed6aed792009-06-03 08:41:15 +020041 fio_file_clear_hashed(f);
Jens Axboe380065a2008-03-01 18:56:24 +010042 }
43
Jens Axboeb9507812008-06-13 08:38:25 +020044 fio_mutex_up(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010045}
46
47static struct fio_file *__lookup_file_hash(const char *name)
48{
Jens Axboe01743ee2008-06-02 12:19:19 +020049 struct flist_head *bucket = &file_hash[hash(name)];
50 struct flist_head *n;
Jens Axboe380065a2008-03-01 18:56:24 +010051
Jens Axboe01743ee2008-06-02 12:19:19 +020052 flist_for_each(n, bucket) {
53 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
Jens Axboe380065a2008-03-01 18:56:24 +010054
Jens Axboe89541102008-09-10 09:07:55 +020055 if (!f->file_name)
56 continue;
57
Jens Axboe380065a2008-03-01 18:56:24 +010058 if (!strcmp(f->file_name, name)) {
59 assert(f->fd != -1);
60 return f;
61 }
62 }
63
Jens Axboe380065a2008-03-01 18:56:24 +010064 return NULL;
65}
66
67struct fio_file *lookup_file_hash(const char *name)
68{
69 struct fio_file *f;
70
Jens Axboeb9507812008-06-13 08:38:25 +020071 fio_mutex_down(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010072 f = __lookup_file_hash(name);
Jens Axboeb9507812008-06-13 08:38:25 +020073 fio_mutex_up(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010074 return f;
75}
76
77struct fio_file *add_file_hash(struct fio_file *f)
78{
79 struct fio_file *alias;
80
Jens Axboed6aed792009-06-03 08:41:15 +020081 if (fio_file_hashed(f))
Jens Axboe380065a2008-03-01 18:56:24 +010082 return NULL;
83
Jens Axboe01743ee2008-06-02 12:19:19 +020084 INIT_FLIST_HEAD(&f->hash_list);
Jens Axboe380065a2008-03-01 18:56:24 +010085
Jens Axboeb9507812008-06-13 08:38:25 +020086 fio_mutex_down(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010087
88 alias = __lookup_file_hash(f->file_name);
89 if (!alias) {
Jens Axboed6aed792009-06-03 08:41:15 +020090 fio_file_set_hashed(f);
Jens Axboe01743ee2008-06-02 12:19:19 +020091 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
Jens Axboe380065a2008-03-01 18:56:24 +010092 }
93
Jens Axboeb9507812008-06-13 08:38:25 +020094 fio_mutex_up(hash_lock);
Jens Axboe380065a2008-03-01 18:56:24 +010095 return alias;
96}
97
Jens Axboe5e1d3062008-05-23 11:55:53 +020098void file_hash_exit(void)
99{
100 unsigned int i, has_entries = 0;
101
Jens Axboeb9507812008-06-13 08:38:25 +0200102 fio_mutex_down(hash_lock);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200103 for (i = 0; i < HASH_BUCKETS; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200104 has_entries += !flist_empty(&file_hash[i]);
Jens Axboeb9507812008-06-13 08:38:25 +0200105 fio_mutex_up(hash_lock);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200106
107 if (has_entries)
108 log_err("fio: file hash not empty on exit\n");
109
Jens Axboeb9507812008-06-13 08:38:25 +0200110 file_hash = NULL;
111 fio_mutex_remove(hash_lock);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200112 hash_lock = NULL;
113}
114
Jens Axboe380065a2008-03-01 18:56:24 +0100115void file_hash_init(void *ptr)
116{
117 unsigned int i;
118
119 file_hash = ptr;
120 for (i = 0; i < HASH_BUCKETS; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200121 INIT_FLIST_HEAD(&file_hash[i]);
Jens Axboe380065a2008-03-01 18:56:24 +0100122
Jens Axboe521da522012-08-02 11:21:36 +0200123 hash_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
Jens Axboe380065a2008-03-01 18:56:24 +0100124}