Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Simple benchmark program that uses the various features of io_uring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * to provide fast random access to a device/file. It has various
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * options that are control how we use io_uring, see the OPTIONS section
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * below. This uses the raw io_uring interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2018-2019 Jens Axboe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <sys/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include "liburing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include "barrier.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define min(a, b)		((a < b) ? (a) : (b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct io_sq_ring {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	unsigned *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	unsigned *ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned *ring_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned *flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	unsigned *array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) struct io_cq_ring {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	unsigned *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	unsigned *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	unsigned *ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	unsigned *ring_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct io_uring_cqe *cqes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define DEPTH			128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define BATCH_SUBMIT		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define BATCH_COMPLETE		32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #define BS			4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define MAX_FDS			16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static unsigned sq_ring_mask, cq_ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) struct file {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	unsigned long max_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned pending_ios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int real_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int fixed_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) struct submitter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	pthread_t thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int ring_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct drand48_data rand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct io_sq_ring sq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct io_uring_sqe *sqes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct iovec iovecs[DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct io_cq_ring cq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int inflight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	unsigned long reaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	unsigned long done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	unsigned long calls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	volatile int finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	__s32 *fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct file files[MAX_FDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	unsigned nr_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	unsigned cur_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static struct submitter submitters[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static volatile int finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * OPTIONS: Set these to test the various features of io_uring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) static int polled = 1;		/* use IO polling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int fixedbufs = 1;	/* use fixed user buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int register_files = 1;	/* use fixed files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int buffered = 0;	/* use buffered IO, not O_DIRECT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int sq_thread_poll = 0;	/* use kernel submission/poller thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int sq_thread_cpu = -1;	/* pin above thread to this CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int do_nop = 0;		/* no-op SQ ring commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int io_uring_register_buffers(struct submitter *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (do_nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return io_uring_register(s->ring_fd, IORING_REGISTER_BUFFERS, s->iovecs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 					DEPTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int io_uring_register_files(struct submitter *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (do_nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	s->fds = calloc(s->nr_files, sizeof(__s32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	for (i = 0; i < s->nr_files; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		s->fds[i] = s->files[i].real_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		s->files[i].fixed_fd = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return io_uring_register(s->ring_fd, IORING_REGISTER_FILES, s->fds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 					s->nr_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static int lk_gettid(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return syscall(__NR_gettid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static unsigned file_depth(struct submitter *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	return (DEPTH + s->nr_files - 1) / s->nr_files;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static void init_io(struct submitter *s, unsigned index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	struct io_uring_sqe *sqe = &s->sqes[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct file *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	long r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (do_nop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		sqe->opcode = IORING_OP_NOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (s->nr_files == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		f = &s->files[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		f = &s->files[s->cur_file];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (f->pending_ios >= file_depth(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			s->cur_file++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			if (s->cur_file == s->nr_files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				s->cur_file = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			f = &s->files[s->cur_file];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	f->pending_ios++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	lrand48_r(&s->rand, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	offset = (r % (f->max_blocks - 1)) * BS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (register_files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		sqe->flags = IOSQE_FIXED_FILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		sqe->fd = f->fixed_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		sqe->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		sqe->fd = f->real_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (fixedbufs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		sqe->opcode = IORING_OP_READ_FIXED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		sqe->addr = (unsigned long) s->iovecs[index].iov_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		sqe->len = BS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		sqe->buf_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		sqe->opcode = IORING_OP_READV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		sqe->addr = (unsigned long) &s->iovecs[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		sqe->len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		sqe->buf_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	sqe->ioprio = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	sqe->off = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	sqe->user_data = (unsigned long) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int prep_more_ios(struct submitter *s, unsigned max_ios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct io_sq_ring *ring = &s->sq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	unsigned index, tail, next_tail, prepped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	next_tail = tail = *ring->tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		next_tail++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		read_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (next_tail == *ring->head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		index = tail & sq_ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		init_io(s, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		ring->array[index] = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		prepped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		tail = next_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	} while (prepped < max_ios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (*ring->tail != tail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		/* order tail store with writes to sqes above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		write_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		*ring->tail = tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		write_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return prepped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int get_file_size(struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (fstat(f->real_fd, &st) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (S_ISBLK(st.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		unsigned long long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (ioctl(f->real_fd, BLKGETSIZE64, &bytes) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		f->max_blocks = bytes / BS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	} else if (S_ISREG(st.st_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		f->max_blocks = st.st_size / BS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int reap_events(struct submitter *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct io_cq_ring *ring = &s->cq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct io_uring_cqe *cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	unsigned head, reaped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	head = *ring->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		struct file *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		read_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (head == *ring->tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		cqe = &ring->cqes[head & cq_ring_mask];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (!do_nop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			f = (struct file *) (uintptr_t) cqe->user_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			f->pending_ios--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			if (cqe->res != BS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				printf("io: unexpected ret=%d\n", cqe->res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				if (polled && cqe->res == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 					printf("Your filesystem doesn't support poll\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		reaped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		head++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	} while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	s->inflight -= reaped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	*ring->head = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	write_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return reaped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static void *submitter_fn(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct submitter *s = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	struct io_sq_ring *ring = &s->sq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	int ret, prepped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	printf("submitter=%d\n", lk_gettid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	srand48_r(pthread_self(), &s->rand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	prepped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		int to_wait, to_submit, this_reap, to_prep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (!prepped && s->inflight < DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			to_prep = min(DEPTH - s->inflight, BATCH_SUBMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			prepped = prep_more_ios(s, to_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		s->inflight += prepped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) submit_more:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		to_submit = prepped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) submit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (to_submit && (s->inflight + to_submit <= DEPTH))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			to_wait = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			to_wait = min(s->inflight + to_submit, BATCH_COMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		 * Only need to call io_uring_enter if we're not using SQ thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 * poll, or if IORING_SQ_NEED_WAKEUP is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if (!sq_thread_poll || (*ring->flags & IORING_SQ_NEED_WAKEUP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			unsigned flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			if (to_wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				flags = IORING_ENTER_GETEVENTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			if ((*ring->flags & IORING_SQ_NEED_WAKEUP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				flags |= IORING_ENTER_SQ_WAKEUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			ret = io_uring_enter(s->ring_fd, to_submit, to_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 						flags, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			s->calls++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		 * For non SQ thread poll, we already got the events we needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		 * through the io_uring_enter() above. For SQ thread poll, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 * need to loop here until we find enough events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		this_reap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			r = reap_events(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			if (r == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				s->finish = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			} else if (r > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 				this_reap += r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		} while (sq_thread_poll && this_reap < to_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		s->reaps += this_reap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				to_submit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				if (s->inflight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 					goto submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			} else if (ret < to_submit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				int diff = to_submit - ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 				s->done += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 				prepped -= diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				goto submit_more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			s->done += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			prepped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		} else if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			if (errno == EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 				if (s->finish)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				if (this_reap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 					goto submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				to_submit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				goto submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			printf("io_submit: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	} while (!s->finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	finish = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static void sig_int(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	printf("Exiting on signal %d\n", sig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	submitters[0].finish = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	finish = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static void arm_sig_int(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	memset(&act, 0, sizeof(act));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	act.sa_handler = sig_int;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	act.sa_flags = SA_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	sigaction(SIGINT, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int setup_ring(struct submitter *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct io_sq_ring *sring = &s->sq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct io_cq_ring *cring = &s->cq_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	struct io_uring_params p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	int ret, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	memset(&p, 0, sizeof(p));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (polled && !do_nop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		p.flags |= IORING_SETUP_IOPOLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (sq_thread_poll) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		p.flags |= IORING_SETUP_SQPOLL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		if (sq_thread_cpu != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			p.flags |= IORING_SETUP_SQ_AFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			p.sq_thread_cpu = sq_thread_cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	fd = io_uring_setup(DEPTH, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		perror("io_uring_setup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	s->ring_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (fixedbufs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		ret = io_uring_register_buffers(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			perror("io_uring_register_buffers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (register_files) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		ret = io_uring_register_files(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			perror("io_uring_register_files");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	ptr = mmap(0, p.sq_off.array + p.sq_entries * sizeof(__u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 			IORING_OFF_SQ_RING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	printf("sq_ring ptr = 0x%p\n", ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	sring->head = ptr + p.sq_off.head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	sring->tail = ptr + p.sq_off.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	sring->ring_mask = ptr + p.sq_off.ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	sring->ring_entries = ptr + p.sq_off.ring_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	sring->flags = ptr + p.sq_off.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	sring->array = ptr + p.sq_off.array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	sq_ring_mask = *sring->ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			IORING_OFF_SQES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	printf("sqes ptr    = 0x%p\n", s->sqes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	ptr = mmap(0, p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			IORING_OFF_CQ_RING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	printf("cq_ring ptr = 0x%p\n", ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	cring->head = ptr + p.cq_off.head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	cring->tail = ptr + p.cq_off.tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	cring->ring_mask = ptr + p.cq_off.ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	cring->ring_entries = ptr + p.cq_off.ring_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	cring->cqes = ptr + p.cq_off.cqes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	cq_ring_mask = *cring->ring_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static void file_depths(char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	struct submitter *s = &submitters[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	buf[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	for (i = 0; i < s->nr_files; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		struct file *f = &s->files[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		if (i + 1 == s->nr_files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			p += sprintf(p, "%d", f->pending_ios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			p += sprintf(p, "%d, ", f->pending_ios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	struct submitter *s = &submitters[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	unsigned long done, calls, reap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	int err, i, flags, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	char *fdepths;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (!do_nop && argc < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		printf("%s: filename\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	flags = O_RDONLY | O_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (!buffered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		flags |= O_DIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	while (!do_nop && i < argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		struct file *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		if (s->nr_files == MAX_FDS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			printf("Max number of files (%d) reached\n", MAX_FDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		fd = open(argv[i], flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		f = &s->files[s->nr_files];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		f->real_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		if (get_file_size(f)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			printf("failed getting size of device/file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		if (f->max_blocks <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			printf("Zero file/device size?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		f->max_blocks--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		printf("Added file %s\n", argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		s->nr_files++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	if (fixedbufs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		struct rlimit rlim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		rlim.rlim_cur = RLIM_INFINITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		rlim.rlim_max = RLIM_INFINITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			perror("setrlimit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	arm_sig_int();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	for (i = 0; i < DEPTH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		if (posix_memalign(&buf, BS, BS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 			printf("failed alloc\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		s->iovecs[i].iov_base = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		s->iovecs[i].iov_len = BS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	err = setup_ring(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		printf("ring setup failed: %s, %d\n", strerror(errno), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	printf("polled=%d, fixedbufs=%d, buffered=%d", polled, fixedbufs, buffered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	printf(" QD=%d, sq_ring=%d, cq_ring=%d\n", DEPTH, *s->sq_ring.ring_entries, *s->cq_ring.ring_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	pthread_create(&s->thread, NULL, submitter_fn, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	fdepths = malloc(8 * s->nr_files);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	reap = calls = done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		unsigned long this_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		unsigned long this_reap = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		unsigned long this_call = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		unsigned long rpc = 0, ipc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		this_done += s->done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		this_call += s->calls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		this_reap += s->reaps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		if (this_call - calls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			rpc = (this_done - done) / (this_call - calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			ipc = (this_reap - reap) / (this_call - calls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			rpc = ipc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		file_depths(fdepths);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		printf("IOPS=%lu, IOS/call=%ld/%ld, inflight=%u (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				this_done - done, rpc, ipc, s->inflight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 				fdepths);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		done = this_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		calls = this_call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		reap = this_reap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	} while (!finish);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	pthread_join(s->thread, &ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	close(s->ring_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	free(fdepths);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }