t/io_uring: link with libaio when necessary
[fio.git] / trim.c
1 /*
2  * TRIM/DISCARD support
3  */
4 #include <string.h>
5 #include <assert.h>
6
7 #include "fio.h"
8 #include "trim.h"
9
10 #ifdef FIO_HAVE_TRIM
11 bool get_next_trim(struct thread_data *td, struct io_u *io_u)
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)
19                 return true;
20         if (flist_empty(&td->trim_list))
21                 return false;
22
23         assert(td->trim_entries);
24         ipo = flist_first_entry(&td->trim_list, struct io_piece, trim_list);
25         remove_trim_entry(td, ipo);
26
27         io_u->offset = ipo->offset;
28         io_u->buflen = ipo->len;
29         io_u->file = ipo->file;
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--;
43                 free(ipo);
44         } else
45                 ipo->flags |= IP_F_TRIMMED;
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);
53                         return false;
54                 }
55         }
56
57         get_file(io_u->file);
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
63         dprint(FD_VERIFY, "get_next_trim: ret io_u %p\n", io_u);
64         return true;
65 }
66
67 bool io_u_should_trim(struct thread_data *td, struct io_u *io_u)
68 {
69         unsigned long long val;
70         uint64_t frand_max;
71         unsigned long r;
72
73         if (!td->o.trim_percentage)
74                 return false;
75
76         frand_max = rand_max(&td->trim_state);
77         r = __rand(&td->trim_state);
78         val = (frand_max / 100ULL);
79
80         val *= (unsigned long long) td->o.trim_percentage;
81         return r <= val;
82 }
83 #endif