public inbox for [email protected]
 help / color / mirror / Atom feed
From: Miklos Szeredi <[email protected]>
To: Jens Axboe <[email protected]>
Cc: [email protected]
Subject: Re: io_uring_prep_openat_direct() and link/drain
Date: Wed, 30 Mar 2022 16:58:06 +0200	[thread overview]
Message-ID: <CAJfpegsADrdURSUOrGTjbu1DoRr7-8itGx23Tn0wf6gNdO5dWA@mail.gmail.com> (raw)
In-Reply-To: <CAJfpeguZji8x+zXSADJ4m6VKbdmTb6ZQd5zA=HCt8acxvGSr3w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 732 bytes --]

Next issue:  seems like file slot reuse is not working correctly.
Attached program compares reads using io_uring with plain reads of
proc files.

In the below example it is using two slots alternately but the number
of slots does not seem to matter, read is apparently always using a
stale file (the prior one to the most recent open on that slot).  See
how the sizes of the files lag by two lines:

root@kvm:~# ./procreads
procreads: /proc/1/stat: ok (313)
procreads: /proc/2/stat: ok (149)
procreads: /proc/3/stat: read size mismatch 313/150
procreads: /proc/4/stat: read size mismatch 149/154
procreads: /proc/5/stat: read size mismatch 150/161
procreads: /proc/6/stat: read size mismatch 154/171
...

Any ideas?

Thanks,
Miklos

[-- Attachment #2: procreads.c --]
[-- Type: text/x-csrc, Size: 2729 bytes --]

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <err.h>
#include "liburing.h"

#define CHECK_NEGERR(_expr) \
	({ typeof(_expr) _ret = (_expr); if (_ret < 0) { errno = -_ret; err(1, #_expr); } _ret; })
#define CHECK_NULL(_expr) \
	({ typeof(_expr) _ret = (_expr); if (_ret == NULL) { errx(1, #_expr " returned NULL"); } _ret; })

ssize_t readfile_uring(struct io_uring *ring, int slot,
		       const char *path, char *buf, size_t size)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret, i;
	unsigned int sum = 0;

	for (i = 0; path[i]; i++)
		sum += path[i];

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_openat_direct(sqe, AT_FDCWD, path, O_RDONLY, 0, slot);
	sqe->flags = IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS;
	sqe->user_data = 0xdead0000 + sum;

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_read(sqe, slot, buf, size, 0);
	sqe->flags = IOSQE_FIXED_FILE;
	sqe->user_data = 0xfeed0000 + sum;

	ret = CHECK_NEGERR(io_uring_submit_and_wait(ring, 1));
	if (ret < 2)
		warnx("short submit count: %i", ret);

	ret = CHECK_NEGERR(io_uring_wait_cqe(ring, &cqe));

	if ((cqe->user_data & 0xffff) != sum)
		warnx("wrong sum: %x (should be %x)",(unsigned int) cqe->user_data, sum);
	if (cqe->res >= 0 && (cqe->user_data & 0xffff0000) != 0xfeed0000)
		warnx("not skipped: %x", (unsigned int) cqe->user_data);

	ret = cqe->res;
	io_uring_cqe_seen(ring, cqe);
	if (ret < 0) {
		errno = -ret;
		warn("failed to open or read %s", path);
	}

	return ret;
}

static ssize_t readfile_plain(const char *path, char *buf, size_t size)
{
	int fd;
	ssize_t ret;

	fd = open(path, O_RDONLY);
	if (fd == -1)
		return -errno;

	ret = read(fd, buf, size);
	if (ret == -1)
		return -errno;

	close(fd);

	return ret;
}

int main(void)
{
	int fds[] = { -1, -1 };
	struct io_uring ring;
	char *name, path[4096], buf1[4096], buf2[4096];
	DIR *dp;
	struct dirent *de;
	ssize_t ret1, ret2;
	int slot = 0;
	unsigned int numslots = sizeof(fds)/sizeof(fds[0]);

	CHECK_NEGERR(io_uring_queue_init(32, &ring, 0));
	CHECK_NEGERR(io_uring_register_files(&ring, fds, numslots));

	dp = CHECK_NULL(opendir("/proc"));
	while ((de = readdir(dp))) {
		name = de->d_name;
		if (name[0] > '0' && name[0] <= '9') {
			sprintf(path, "/proc/%s/stat", name);
			ret1 = readfile_uring(&ring, slot, path, buf1, sizeof(buf1));
			ret2 = readfile_plain(path, buf2, sizeof(buf2));
			if (ret1 != ret2)
				warnx("%s: read size mismatch %zi/%zi",
				      path, ret1, ret2);
			else if (ret1 > 0 && memcmp(buf1, buf2, ret1))
				warnx("%s: data mismatch", path);
			else {
				warnx("%s: ok (%zi)", path, ret1);
			}

			slot = (slot + 1) % numslots;
		}
	}
	closedir(dp);
	return 0;
}

  reply	other threads:[~2022-03-30 14:58 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29 13:20 io_uring_prep_openat_direct() and link/drain Miklos Szeredi
2022-03-29 16:08 ` Jens Axboe
2022-03-29 17:04   ` Jens Axboe
2022-03-29 18:21     ` Miklos Szeredi
2022-03-29 18:26       ` Jens Axboe
2022-03-29 18:31         ` Miklos Szeredi
2022-03-29 18:40           ` Jens Axboe
2022-03-29 19:30             ` Miklos Szeredi
2022-03-29 20:03               ` Jens Axboe
2022-03-30  8:18                 ` Miklos Szeredi
2022-03-30 12:35                   ` Jens Axboe
2022-03-30 12:43                     ` Miklos Szeredi
2022-03-30 12:48                       ` Jens Axboe
2022-03-30 12:51                         ` Miklos Szeredi
2022-03-30 14:58                           ` Miklos Szeredi [this message]
2022-03-30 15:05                             ` Jens Axboe
2022-03-30 15:12                               ` Miklos Szeredi
2022-03-30 15:17                                 ` Jens Axboe
2022-03-30 15:53                                   ` Jens Axboe
2022-03-30 17:49                                     ` Jens Axboe
2022-04-01  8:40                                       ` Miklos Szeredi
2022-04-01 15:36                                         ` Jens Axboe
2022-04-01 16:02                                           ` Miklos Szeredi
2022-04-01 16:21                                             ` Jens Axboe
2022-04-02  1:17                                               ` Jens Axboe
2022-04-05  7:45                                                 ` Miklos Szeredi
2022-04-05 14:44                                                   ` Jens Axboe
2022-04-21 12:31                                                     ` Miklos Szeredi
2022-04-21 12:34                                                       ` Jens Axboe
2022-04-21 12:39                                                         ` Miklos Szeredi
2022-04-21 12:41                                                           ` Jens Axboe
2022-04-21 13:10                                                             ` Miklos Szeredi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJfpegsADrdURSUOrGTjbu1DoRr7-8itGx23Tn0wf6gNdO5dWA@mail.gmail.com \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox