sg: allow fio to open and close streams for WRITE STREAM(16) commands
authorVincent Fu <vincent.fu@samsung.com>
Mon, 15 Nov 2021 20:07:17 +0000 (20:07 +0000)
committerJens Axboe <axboe@kernel.dk>
Tue, 18 Jan 2022 13:37:39 +0000 (06:37 -0700)
If --stream_id=0 then fio will open a stream for WRITE STREAM(16) commands and
close the stream when the device file is closed.

Example:
./fio --name=test --filename=/dev/sdb --ioengine=sg --number_ios=1 --debug=file,io --sg_write_mode=write_stream --rw=randwrite
fio: set debug option file
fio: set debug option io
test: (g=0): rw=randwrite, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=sg, iodepth=1
fio-3.27
Starting 1 process
file     1072297 setup files
file     1072297 get file size for 0x7f0306fa5110/0//dev/sdb
file     1072307 trying file /dev/sdb 290
file     1072307 fd open /dev/sdb
file     1072307 file not found in hash /dev/sdb
file     1072307 sgio_stream_control: opened stream 1
file     1072307 get file /dev/sdb, ref=0
io       1072307 drop page cache /dev/sdb
file     1072307 goodf=1, badf=2, ff=2b1
file     1072307 get_next_file_rr: 0x7f0306fa5110
file     1072307 get_next_file: 0x7f0306fa5110 [/dev/sdb]
file     1072307 get file /dev/sdb, ref=1
io       1072307 fill: io_u 0xb55700: off=0x35ef554000,len=0x1000,ddir=1,file=/dev/sdb
io       1072307 prep: io_u 0xb55700: off=0x35ef554000,len=0x1000,ddir=1,file=/dev/sdb
io       1072307 prep: io_u 0xb55700: ret=0
io       1072307 queue: io_u 0xb55700: off=0x35ef554000,len=0x1000,ddir=1,file=/dev/sdb
io       1072307 complete: io_u 0xb55700: off=0x35ef554000,len=0x1000,ddir=1,file=/dev/sdb
file     1072307 put file /dev/sdb, ref=2
file     1072307 close files
file     1072307 put file /dev/sdb, ref=1
file     1072307 sgio_stream_control: closed stream 1
file     1072307 fd close /dev/sdb
io       1072307 close ioengine sg
io       1072307 free ioengine sg

test: (groupid=0, jobs=1): err= 0: pid=1072307: Mon Aug 16 14:25:45 2021
  write: IOPS=200, BW=800KiB/s (819kB/s)(4096B/5msec); 0 zone resets
    clat (nsec): min=93339, max=93339, avg=93339.00, stdev= 0.00
     lat (nsec): min=96201, max=96201, avg=96201.00, stdev= 0.00
    clat percentiles (nsec):
     |  1.00th=[93696],  5.00th=[93696], 10.00th=[93696], 20.00th=[93696],
     | 30.00th=[93696], 40.00th=[93696], 50.00th=[93696], 60.00th=[93696],
     | 70.00th=[93696], 80.00th=[93696], 90.00th=[93696], 95.00th=[93696],
     | 99.00th=[93696], 99.50th=[93696], 99.90th=[93696], 99.95th=[93696],
     | 99.99th=[93696]
  lat (usec)   : 100=100.00%
  cpu          : usr=100.00%, sys=0.00%, ctx=2, majf=0, minf=20
  IO depths    : 1=100.0%, 2=0.0%, 4=0.0%, 8=0.0%, 16=0.0%, 32=0.0%, >=64=0.0%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     issued rwts: total=0,1,0,0 short=0,0,0,0 dropped=0,0,0,0
     latency   : target=0, window=0, percentile=100.00%, depth=1

Run status group 0 (all jobs):
  WRITE: bw=800KiB/s (819kB/s), 800KiB/s-800KiB/s (819kB/s-819kB/s), io=4096B (4096B), run=5-5msec

Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
Link: https://lore.kernel.org/r/20211115200807.117138-6-vincent.fu@samsung.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
engines/sg.c

index b51edb078bbb06a04ce63130bdabf1d9586ed0e9..72ee07ba4592f715618e935bf57712246bce8ac0 100644 (file)
@@ -217,6 +217,11 @@ struct sgio_data {
 #endif
 };
 
+static inline uint16_t sgio_get_be16(uint8_t *buf)
+{
+       return be16_to_cpu(*((uint16_t *) buf));
+}
+
 static inline uint32_t sgio_get_be32(uint8_t *buf)
 {
        return be32_to_cpu(*((uint32_t *) buf));
@@ -632,7 +637,7 @@ static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
                        if (o->writefua)
                                hdr->cmdp[1] |= 0x08;
                        sgio_set_be64(lba, &hdr->cmdp[2]);
-                       sgio_set_be16(o->stream_id, &hdr->cmdp[10]);
+                       sgio_set_be16((uint16_t) io_u->file->engine_pos, &hdr->cmdp[10]);
                        sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[12]);
                        break;
                case FIO_SG_VERIFY_BYTCHK_00:
@@ -1053,9 +1058,60 @@ static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
        return 0;
 }
 
+static int fio_sgio_stream_control(struct fio_file *f, bool open_stream, uint16_t *stream_id)
+{
+       struct sg_io_hdr hdr;
+       unsigned char cmd[16];
+       unsigned char sb[64];
+       unsigned char buf[8];
+       int ret;
+
+       memset(&hdr, 0, sizeof(hdr));
+       memset(cmd, 0, sizeof(cmd));
+       memset(sb, 0, sizeof(sb));
+       memset(buf, 0, sizeof(buf));
+
+       hdr.interface_id = 'S';
+       hdr.cmdp = cmd;
+       hdr.cmd_len = 16;
+       hdr.sbp = sb;
+       hdr.mx_sb_len = sizeof(sb);
+       hdr.timeout = SCSI_TIMEOUT_MS;
+       hdr.cmdp[0] = 0x9e;
+       hdr.dxfer_direction = SG_DXFER_FROM_DEV;
+       hdr.dxferp = buf;
+       hdr.dxfer_len = sizeof(buf);
+       sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
+
+       if (open_stream)
+               hdr.cmdp[1] = 0x34;
+       else {
+               hdr.cmdp[1] = 0x54;
+               sgio_set_be16(*stream_id, &hdr.cmdp[4]);
+       }
+
+       ret = ioctl(f->fd, SG_IO, &hdr);
+
+       if (ret < 0)
+               return ret;
+
+       if (hdr.info & SG_INFO_CHECK)
+               return 1;
+
+       if (open_stream) {
+               *stream_id = sgio_get_be16(&buf[4]);
+               dprint(FD_FILE, "sgio_stream_control: opened stream %u\n", (unsigned int) *stream_id);
+               assert(*stream_id != 0);
+       } else
+               dprint(FD_FILE, "sgio_stream_control: closed stream %u\n", (unsigned int) *stream_id);
+
+       return 0;
+}
+
 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
 {
        struct sgio_data *sd = td->io_ops_data;
+       struct sg_options *o = td->eo;
        int ret;
 
        ret = generic_open_file(td, f);
@@ -1067,9 +1123,33 @@ static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
                return ret;
        }
 
+       if (o->write_mode == FIO_SG_WRITE_STREAM) {
+               if (o->stream_id)
+                       f->engine_pos = o->stream_id;
+               else {
+                       ret = fio_sgio_stream_control(f, true, (uint16_t *) &f->engine_pos);
+                       if (ret)
+                               return ret;
+               }
+       }
+
        return 0;
 }
 
+int fio_sgio_close(struct thread_data *td, struct fio_file *f)
+{
+       struct sg_options *o = td->eo;
+       int ret;
+
+       if (!o->stream_id && o->write_mode == FIO_SG_WRITE_STREAM) {
+               ret = fio_sgio_stream_control(f, false, (uint16_t *) &f->engine_pos);
+               if (ret)
+                       return ret;
+       }
+
+       return generic_close_file(td, f);
+}
+
 /*
  * Build an error string with details about the driver, host or scsi
  * error contained in the sg header Caller will use as necessary.
@@ -1344,7 +1424,7 @@ static struct ioengine_ops ioengine = {
        .event          = fio_sgio_event,
        .cleanup        = fio_sgio_cleanup,
        .open_file      = fio_sgio_open,
-       .close_file     = generic_close_file,
+       .close_file     = fio_sgio_close,
        .get_file_size  = fio_sgio_get_file_size,
        .flags          = FIO_SYNCIO | FIO_RAWIO,
        .options        = options,