configure: add gettid() test
[fio.git] / trim.c
CommitLineData
0d29de83
JA
1/*
2 * TRIM/DISCARD support
3 */
0d29de83
JA
4#include <string.h>
5#include <assert.h>
0d29de83
JA
6
7#include "fio.h"
8#include "trim.h"
9
10#ifdef FIO_HAVE_TRIM
917c64b3 11bool get_next_trim(struct thread_data *td, struct io_u *io_u)
0d29de83
JA
12{
13 struct io_piece *ipo;
14
15 /*
16 * this io_u is from a requeue, we already filled the offsets
17 */
18 if (io_u->file)
917c64b3 19 return true;
0d29de83 20 if (flist_empty(&td->trim_list))
917c64b3 21 return false;
0d29de83
JA
22
23 assert(td->trim_entries);
9342d5f8 24 ipo = flist_first_entry(&td->trim_list, struct io_piece, trim_list);
0d29de83 25 remove_trim_entry(td, ipo);
a917a8b3
JA
26
27 io_u->offset = ipo->offset;
28 io_u->buflen = ipo->len;
29 io_u->file = ipo->file;
0d29de83
JA
30
31 /*
32 * If not verifying that trimmed ranges return zeroed data,
33 * remove this from the to-read verify lists
34 */
35 if (!td->o.trim_zero) {
36 if (ipo->flags & IP_F_ONLIST)
37 flist_del(&ipo->list);
38 else {
39 assert(ipo->flags & IP_F_ONRB);
40 rb_erase(&ipo->rb_node, &td->io_hist_tree);
41 }
42 td->io_hist_len--;
a917a8b3
JA
43 free(ipo);
44 } else
45 ipo->flags |= IP_F_TRIMMED;
0d29de83
JA
46
47 if (!fio_file_open(io_u->file)) {
48 int r = td_io_open_file(td, io_u->file);
49
50 if (r) {
51 dprint(FD_VERIFY, "failed file %s open\n",
52 io_u->file->file_name);
917c64b3 53 return false;
0d29de83
JA
54 }
55 }
56
a917a8b3 57 get_file(io_u->file);
0d29de83
JA
58 assert(fio_file_open(io_u->file));
59 io_u->ddir = DDIR_TRIM;
60 io_u->xfer_buf = NULL;
61 io_u->xfer_buflen = io_u->buflen;
62
0d29de83 63 dprint(FD_VERIFY, "get_next_trim: ret io_u %p\n", io_u);
917c64b3 64 return true;
0d29de83
JA
65}
66
917c64b3 67bool io_u_should_trim(struct thread_data *td, struct io_u *io_u)
0d29de83
JA
68{
69 unsigned long long val;
c3546b53 70 uint64_t frand_max;
1294c3ec 71 unsigned long r;
0d29de83
JA
72
73 if (!td->o.trim_percentage)
917c64b3 74 return false;
0d29de83 75
c3546b53 76 frand_max = rand_max(&td->trim_state);
d6b72507 77 r = __rand(&td->trim_state);
c3546b53 78 val = (frand_max / 100ULL);
0d29de83 79
4c07ad86 80 val *= (unsigned long long) td->o.trim_percentage;
0d29de83
JA
81 return r <= val;
82}
83#endif