Revert "Remove unused define"
[fio.git] / filehash.c
... / ...
CommitLineData
1#include <stdlib.h>
2#include <assert.h>
3
4#include "fio.h"
5#include "flist.h"
6#include "crc/crc16.h"
7
8#include "spinlock.h"
9
10#define HASH_BUCKETS 512
11#define HASH_MASK (HASH_BUCKETS - 1)
12
13unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
14
15static struct flist_head *file_hash;
16static struct fio_spinlock *hash_lock;
17
18static unsigned short hash(const char *name)
19{
20 return crc16((const unsigned char *) name, strlen(name)) & HASH_MASK;
21}
22
23void remove_file_hash(struct fio_file *f)
24{
25 fio_spin_lock(hash_lock);
26
27 if (f->flags & FIO_FILE_HASHED) {
28 assert(!flist_empty(&f->hash_list));
29 flist_del_init(&f->hash_list);
30 f->flags &= ~FIO_FILE_HASHED;
31 }
32
33 fio_spin_unlock(hash_lock);
34}
35
36static struct fio_file *__lookup_file_hash(const char *name)
37{
38 struct flist_head *bucket = &file_hash[hash(name)];
39 struct flist_head *n;
40
41 flist_for_each(n, bucket) {
42 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
43
44 if (!strcmp(f->file_name, name)) {
45 assert(f->fd != -1);
46 return f;
47 }
48 }
49
50 return NULL;
51}
52
53struct fio_file *lookup_file_hash(const char *name)
54{
55 struct fio_file *f;
56
57 fio_spin_lock(hash_lock);
58 f = __lookup_file_hash(name);
59 fio_spin_unlock(hash_lock);
60 return f;
61}
62
63struct fio_file *add_file_hash(struct fio_file *f)
64{
65 struct fio_file *alias;
66
67 if (f->flags & FIO_FILE_HASHED)
68 return NULL;
69
70 INIT_FLIST_HEAD(&f->hash_list);
71
72 fio_spin_lock(hash_lock);
73
74 alias = __lookup_file_hash(f->file_name);
75 if (!alias) {
76 f->flags |= FIO_FILE_HASHED;
77 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
78 }
79
80 fio_spin_unlock(hash_lock);
81 return alias;
82}
83
84void file_hash_exit(void)
85{
86 unsigned int i, has_entries = 0;
87
88 fio_spin_lock(hash_lock);
89 for (i = 0; i < HASH_BUCKETS; i++)
90 has_entries += !flist_empty(&file_hash[i]);
91
92 file_hash = NULL;
93 fio_spin_unlock(hash_lock);
94
95 if (has_entries)
96 log_err("fio: file hash not empty on exit\n");
97
98 fio_spinlock_remove(hash_lock);
99 hash_lock = NULL;
100}
101
102void file_hash_init(void *ptr)
103{
104 unsigned int i;
105
106 file_hash = ptr;
107 for (i = 0; i < HASH_BUCKETS; i++)
108 INIT_FLIST_HEAD(&file_hash[i]);
109
110 hash_lock = fio_spinlock_init();
111}