Add verify trim support
[fio.git] / trim.c
CommitLineData
0d29de83
JA
1/*
2 * TRIM/DISCARD support
3 */
4#include <unistd.h>
5#include <fcntl.h>
6#include <string.h>
7#include <assert.h>
8#include <pthread.h>
9
10#include "fio.h"
11#include "trim.h"
12
13#ifdef FIO_HAVE_TRIM
14int get_next_trim(struct thread_data *td, struct io_u *io_u)
15{
16 struct io_piece *ipo;
17
18 /*
19 * this io_u is from a requeue, we already filled the offsets
20 */
21 if (io_u->file)
22 return 0;
23 if (flist_empty(&td->trim_list))
24 return 0;
25
26 assert(td->trim_entries);
27 ipo = flist_entry(td->trim_list.next, struct io_piece, trim_list);
28 remove_trim_entry(td, ipo);
29 ipo->flags |= IP_F_TRIMMED;
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 }
44
45 io_u->offset = ipo->offset;
46 io_u->buflen = ipo->len;
47 io_u->file = ipo->file;
48
49 if (!fio_file_open(io_u->file)) {
50 int r = td_io_open_file(td, io_u->file);
51
52 if (r) {
53 dprint(FD_VERIFY, "failed file %s open\n",
54 io_u->file->file_name);
55 return 1;
56 }
57 }
58
59 get_file(ipo->file);
60 assert(fio_file_open(io_u->file));
61 io_u->ddir = DDIR_TRIM;
62 io_u->xfer_buf = NULL;
63 io_u->xfer_buflen = io_u->buflen;
64
65 free(ipo);
66 dprint(FD_VERIFY, "get_next_trim: ret io_u %p\n", io_u);
67 return 0;
68}
69
70int io_u_should_trim(struct thread_data *td, struct io_u *io_u)
71{
72 unsigned long long val;
73 long r;
74
75 if (!td->o.trim_percentage)
76 return 0;
77
78 r = os_random_long(&td->trim_state);
79 val = (OS_RAND_MAX / 100ULL);
80 val *= (unsigned long long) td->o.trim_percentage;
81
82 return r <= val;
83}
84#endif