From owner-freebsd-hackers@freebsd.org Fri Dec 28 03:21:43 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 68E62142C16F for ; Fri, 28 Dec 2018 03:21:43 +0000 (UTC) (envelope-from asomers@gmail.com) Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 59A8570ADC for ; Fri, 28 Dec 2018 03:21:42 +0000 (UTC) (envelope-from asomers@gmail.com) Received: by mail-lj1-f194.google.com with SMTP id t9-v6so17698681ljh.6 for ; Thu, 27 Dec 2018 19:21:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=+YYO6KDdIABnfPlBZ0rlJTb6afT6wOgVwm+HZiRL6aI=; b=ju7mMD34jhzVwW1tGS+/MdXPcaGXxBNXOhgQryOzD3JKS6b+Xq3HDUIOfJLx6/IKL5 evn8q855352SvNoTD9tKnezGorICmZfV5TGimBXs+Me4LIYpD9BE8Cvlgwl35R0B7yH5 LKBisbL3ESo1CW7j3G76FQ2CHNS1YkcGS1908HT2SsudwZ9GqkfDBcDFRt5mOYj3VPgx DOqwzZvBPTmvzg41sB+s8kTTbceNzXcM9Te2jgk9NhlUx8MAuki0AKSNDiy6Tfa3elgm CnsFUVyjd+mpUhwPRtqJ/Bu1rNMDdDl7ryOj2TZzrej3+tg/3+WzdcQCV6xfyQAQ2TmI pVZA== X-Gm-Message-State: AJcUuken9ZuRoDh6v5N+mVvgHPKIV5CtbplMIfvP+B0fRC32Pi/IVPV0 G6ktgIEjXLgMVTXsjo+hUNtjqZTqArlGp9V4AY5PsQ== X-Google-Smtp-Source: ALg8bN6sEKeZHM8RuWRRczwjBtVcvXJrrRsXBluCkPDnOC5RYxi5UV3krWPJQtTvpevEfuGpyGBgnHPQpKapL0MQSLw= X-Received: by 2002:a2e:9356:: with SMTP id m22-v6mr14890911ljh.135.1545961646181; Thu, 27 Dec 2018 17:47:26 -0800 (PST) MIME-Version: 1.0 References: <8753521a-4555-ec2a-5efc-dee2660b4d9b@digiware.nl> In-Reply-To: <8753521a-4555-ec2a-5efc-dee2660b4d9b@digiware.nl> From: Alan Somers Date: Thu, 27 Dec 2018 18:47:14 -0700 Message-ID: Subject: Re: Using kqueue with aio_read/write To: Willem Jan Withagen Cc: FreeBSD Hackers Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 59A8570ADC X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; spf=pass (mx1.freebsd.org: domain of asomers@gmail.com designates 209.85.208.194 as permitted sender) smtp.mailfrom=asomers@gmail.com X-Spamd-Result: default: False [-3.91 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.99)[-0.993,0]; FROM_HAS_DN(0.00)[]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17]; NEURAL_HAM_LONG(-1.00)[-0.999,0]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; MIME_TRACE(0.00)[0:+]; TO_DN_ALL(0.00)[]; MX_GOOD(-0.01)[cached: alt3.gmail-smtp-in.l.google.com]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[194.208.85.209.list.dnswl.org : 127.0.5.0]; NEURAL_HAM_SHORT(-0.85)[-0.852,0]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_TLS_LAST(0.00)[]; FORGED_SENDER(0.30)[asomers@freebsd.org,asomers@gmail.com]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US]; FROM_NEQ_ENVFROM(0.00)[asomers@freebsd.org,asomers@gmail.com]; IP_SCORE(-1.06)[ipnet: 209.85.128.0/17(-3.70), asn: 15169(-1.51), country: US(-0.08)]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Dec 2018 03:21:43 -0000 On Thu, Dec 27, 2018 at 6:15 PM Willem Jan Withagen wrote: > > Hi, > > Im trying to understand why I cannot get so code to work. > This is the smallest extract I can make to show my problem. > > I would expect the kevent() call to return every timeo tick. > Even if I tell it NOT to time-out I get these spurts of errors > > Since there is nothing to trigger the AIO-event, I would expect kqueue > to hold indefinitly. > > But it does not generate anything other than errors > And instead it repeatedly complains that there is a permission error: > get_events_kevent: EV_Error(1) kevent(): Operation not permitted > > But I'm not getting where that would the case... > > Surely a pilot error, but I do overlook it al the time. > So suggestions are welcome. > > Thanx, > --WjW > > #include > #include > #include > #include > #include > #include > #include > #include > #include > > #define BUFFER_SIZE 512 > #define MAX_EVENTS 32 > > #define FILENAME "/tmp/aio_test" > char filename[256]; > int fd; > int done = 0; > > void get_events_kevent(int fd, int kq) > { > printf("get_events function fd = %d, kq = %d\n", fd, kq); > int i = 0, errcnt = 0, err, ret, reterr, rev; > int search = 1; > > int timeout_ms = 10; > struct timespec timeo = { > timeout_ms / 1000, > (timeout_ms % 1000) * 1000 * 1000 > }; > struct kevent filter[16]; > struct kevent changed[16]; > > EV_SET(&filter[0], fd, EVFILT_AIO, > EV_ADD, > 0, 0, 0 ); This is the first problem. There's no need to explicitly set EVFILT_AIO on the kqueue. It gets set by the aio_read(2) or similar syscall. And this invocation wouldn't be correct anyway, because for AIO the ident field refers to the address of the struct aiocb, not the file descriptor. If the only events you care about are AIO, then you can pass NULL as the filter argument to kevent. I suspect this is the cause of your problem. The kernel probably thinks you're trying to register for an aiocb that's outside of your address space or something like that. > while (!done) { > printf("+"); > rev = kevent(kq, filter, 1, changed, 16, 0); //&timeo); > if (rev < 0) { > perror("kevent error"); > } else if (rev == 0) { > printf("T"); > } else { > printf("rev(%d)\n", rev); > if (changed[0].flags == EV_ERROR) { > errno = changed[0].data; > printf( "%s: EV_Error(%d) kevent(): %s\n", __func__, errno, > strerror(errno)); > memset(&changed[0], 0, sizeof(struct kevent)); > } else { > err = aio_error((struct aiocb*)changed[0].udata); No need to call aio_error(2) after kevent(2) returns. You can go straight to aio_return. aio_error shouldn't hurt, but it isn't necessary. > ret = aio_return((struct aiocb*)changed[0].udata); > if (ret < 0 ) > reterr = errno; > if (err != 0) { > printf( "%s: slot: %d, Error(%d) at aio_error(): > %s\n", __func__, i, err, strerror (err)); > errcnt++; > if (errcnt > 50) { > exit(3); > } > } > } > } > } > } > > int main() > { > (void) strcpy(filename, FILENAME); > unlink(filename); > fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); > if (fd == -1) { > printf( "%s: Error at open(): %s\n", __func__, strerror(errno)); > exit(1); > } > > int kq = kqueue(); > if (fd == -1) { > printf( "%s: Error at kqueue(): %s\n", __func__, strerror(errno)); > exit(1); > } > > get_events_kevent( fd, kq); > > close(kq); > close(fd); > return 0; > } > > > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"