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