blob: 78cf67244efa22534604fd6b6652379457a571a2 [file] [log] [blame]
Jens Axboe0d29de82010-09-01 13:54:15 +02001/*
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
Elliott Hugheseda3a602017-05-19 18:53:02 -070014bool get_next_trim(struct thread_data *td, struct io_u *io_u)
Jens Axboe0d29de82010-09-01 13:54:15 +020015{
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)
Elliott Hugheseda3a602017-05-19 18:53:02 -070022 return true;
Jens Axboe0d29de82010-09-01 13:54:15 +020023 if (flist_empty(&td->trim_list))
Elliott Hugheseda3a602017-05-19 18:53:02 -070024 return false;
Jens Axboe0d29de82010-09-01 13:54:15 +020025
26 assert(td->trim_entries);
Jens Axboe12dbd062014-07-03 21:19:57 -060027 ipo = flist_first_entry(&td->trim_list, struct io_piece, trim_list);
Jens Axboe0d29de82010-09-01 13:54:15 +020028 remove_trim_entry(td, ipo);
Jens Axboea917a8b2010-09-02 13:23:20 +020029
30 io_u->offset = ipo->offset;
31 io_u->buflen = ipo->len;
32 io_u->file = ipo->file;
Jens Axboe0d29de82010-09-01 13:54:15 +020033
34 /*
35 * If not verifying that trimmed ranges return zeroed data,
36 * remove this from the to-read verify lists
37 */
38 if (!td->o.trim_zero) {
39 if (ipo->flags & IP_F_ONLIST)
40 flist_del(&ipo->list);
41 else {
42 assert(ipo->flags & IP_F_ONRB);
43 rb_erase(&ipo->rb_node, &td->io_hist_tree);
44 }
45 td->io_hist_len--;
Jens Axboea917a8b2010-09-02 13:23:20 +020046 free(ipo);
47 } else
48 ipo->flags |= IP_F_TRIMMED;
Jens Axboe0d29de82010-09-01 13:54:15 +020049
50 if (!fio_file_open(io_u->file)) {
51 int r = td_io_open_file(td, io_u->file);
52
53 if (r) {
54 dprint(FD_VERIFY, "failed file %s open\n",
55 io_u->file->file_name);
Elliott Hugheseda3a602017-05-19 18:53:02 -070056 return false;
Jens Axboe0d29de82010-09-01 13:54:15 +020057 }
58 }
59
Jens Axboea917a8b2010-09-02 13:23:20 +020060 get_file(io_u->file);
Jens Axboe0d29de82010-09-01 13:54:15 +020061 assert(fio_file_open(io_u->file));
62 io_u->ddir = DDIR_TRIM;
63 io_u->xfer_buf = NULL;
64 io_u->xfer_buflen = io_u->buflen;
65
Jens Axboe0d29de82010-09-01 13:54:15 +020066 dprint(FD_VERIFY, "get_next_trim: ret io_u %p\n", io_u);
Elliott Hugheseda3a602017-05-19 18:53:02 -070067 return true;
Jens Axboe0d29de82010-09-01 13:54:15 +020068}
69
Elliott Hugheseda3a602017-05-19 18:53:02 -070070bool io_u_should_trim(struct thread_data *td, struct io_u *io_u)
Jens Axboe0d29de82010-09-01 13:54:15 +020071{
72 unsigned long long val;
Elliott Hugheseda3a602017-05-19 18:53:02 -070073 uint64_t frand_max;
Jens Axboe1294c3e2011-05-11 08:15:18 +020074 unsigned long r;
Jens Axboe0d29de82010-09-01 13:54:15 +020075
76 if (!td->o.trim_percentage)
Elliott Hugheseda3a602017-05-19 18:53:02 -070077 return false;
Jens Axboe0d29de82010-09-01 13:54:15 +020078
Elliott Hugheseda3a602017-05-19 18:53:02 -070079 frand_max = rand_max(&td->trim_state);
Jens Axboef6787012014-11-05 18:39:23 -070080 r = __rand(&td->trim_state);
Elliott Hugheseda3a602017-05-19 18:53:02 -070081 val = (frand_max / 100ULL);
Jens Axboe0d29de82010-09-01 13:54:15 +020082
Jens Axboe4c07ad82011-03-28 09:51:09 +020083 val *= (unsigned long long) td->o.trim_percentage;
Jens Axboe0d29de82010-09-01 13:54:15 +020084 return r <= val;
85}
86#endif