2012-10-26 11:43:58 +02:00
|
|
|
/*
|
|
|
|
* Block driver for RAW files (win32)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2016-01-18 19:01:42 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2022-12-21 14:35:49 +01:00
|
|
|
#include "block/block-io.h"
|
2012-12-17 18:19:44 +01:00
|
|
|
#include "block/block_int.h"
|
|
|
|
#include "block/aio.h"
|
2016-07-04 18:33:20 +02:00
|
|
|
#include "block/raw-aio.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/event_notifier.h"
|
2013-01-17 15:47:52 +01:00
|
|
|
#include "qemu/iov.h"
|
2022-02-26 19:07:23 +01:00
|
|
|
#include "qemu/memalign.h"
|
2012-10-26 11:43:58 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winioctl.h>
|
|
|
|
|
|
|
|
#define FTYPE_FILE 0
|
|
|
|
#define FTYPE_CD 1
|
|
|
|
#define FTYPE_HARDDISK 2
|
|
|
|
|
|
|
|
struct QEMUWin32AIOState {
|
|
|
|
HANDLE hIOCP;
|
|
|
|
EventNotifier e;
|
|
|
|
int count;
|
2017-02-13 14:52:30 +01:00
|
|
|
AioContext *aio_ctx;
|
2012-10-26 11:43:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct QEMUWin32AIOCB {
|
2014-10-07 13:59:14 +02:00
|
|
|
BlockAIOCB common;
|
2012-10-26 11:43:58 +02:00
|
|
|
struct QEMUWin32AIOState *ctx;
|
|
|
|
int nbytes;
|
|
|
|
OVERLAPPED ov;
|
|
|
|
QEMUIOVector *qiov;
|
|
|
|
void *buf;
|
|
|
|
bool is_read;
|
|
|
|
bool is_linear;
|
|
|
|
} QEMUWin32AIOCB;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Completes an AIO request (calls the callback and frees the ACB).
|
|
|
|
*/
|
|
|
|
static void win32_aio_process_completion(QEMUWin32AIOState *s,
|
|
|
|
QEMUWin32AIOCB *waiocb, DWORD count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
s->count--;
|
|
|
|
|
|
|
|
if (waiocb->ov.Internal != 0) {
|
|
|
|
ret = -EIO;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
if (count < waiocb->nbytes) {
|
|
|
|
/* Short reads mean EOF, pad with zeros. */
|
|
|
|
if (waiocb->is_read) {
|
|
|
|
qemu_iovec_memset(waiocb->qiov, count, 0,
|
|
|
|
waiocb->qiov->size - count);
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!waiocb->is_linear) {
|
|
|
|
if (ret == 0 && waiocb->is_read) {
|
|
|
|
QEMUIOVector *qiov = waiocb->qiov;
|
2013-01-17 15:47:52 +01:00
|
|
|
iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
|
2012-10-26 11:43:58 +02:00
|
|
|
}
|
2013-01-16 21:20:00 +01:00
|
|
|
qemu_vfree(waiocb->buf);
|
2012-10-26 11:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
waiocb->common.cb(waiocb->common.opaque, ret);
|
2014-09-11 07:41:28 +02:00
|
|
|
qemu_aio_unref(waiocb);
|
2012-10-26 11:43:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void win32_aio_completion_cb(EventNotifier *e)
|
|
|
|
{
|
|
|
|
QEMUWin32AIOState *s = container_of(e, QEMUWin32AIOState, e);
|
|
|
|
DWORD count;
|
|
|
|
ULONG_PTR key;
|
|
|
|
OVERLAPPED *ov;
|
|
|
|
|
|
|
|
event_notifier_test_and_clear(&s->e);
|
|
|
|
while (GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov, 0)) {
|
|
|
|
QEMUWin32AIOCB *waiocb = container_of(ov, QEMUWin32AIOCB, ov);
|
|
|
|
|
|
|
|
win32_aio_process_completion(s, waiocb, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 16:34:37 +01:00
|
|
|
static const AIOCBInfo win32_aiocb_info = {
|
2012-10-26 11:43:58 +02:00
|
|
|
.aiocb_size = sizeof(QEMUWin32AIOCB),
|
|
|
|
};
|
|
|
|
|
2014-10-07 13:59:14 +02:00
|
|
|
BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
|
2012-10-26 11:43:58 +02:00
|
|
|
QEMUWin32AIOState *aio, HANDLE hfile,
|
2018-04-24 21:25:02 +02:00
|
|
|
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
|
2014-10-07 13:59:15 +02:00
|
|
|
BlockCompletionFunc *cb, void *opaque, int type)
|
2012-10-26 11:43:58 +02:00
|
|
|
{
|
|
|
|
struct QEMUWin32AIOCB *waiocb;
|
|
|
|
DWORD rc;
|
|
|
|
|
2012-10-31 16:34:37 +01:00
|
|
|
waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
|
2018-04-24 21:25:02 +02:00
|
|
|
waiocb->nbytes = bytes;
|
2012-10-26 11:43:58 +02:00
|
|
|
waiocb->qiov = qiov;
|
|
|
|
waiocb->is_read = (type == QEMU_AIO_READ);
|
|
|
|
|
|
|
|
if (qiov->niov > 1) {
|
2014-05-21 18:05:47 +02:00
|
|
|
waiocb->buf = qemu_try_blockalign(bs, qiov->size);
|
|
|
|
if (waiocb->buf == NULL) {
|
|
|
|
goto out;
|
|
|
|
}
|
2012-10-26 11:43:58 +02:00
|
|
|
if (type & QEMU_AIO_WRITE) {
|
2013-01-17 15:47:52 +01:00
|
|
|
iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->buf, qiov->size);
|
2012-10-26 11:43:58 +02:00
|
|
|
}
|
|
|
|
waiocb->is_linear = false;
|
|
|
|
} else {
|
|
|
|
waiocb->buf = qiov->iov[0].iov_base;
|
|
|
|
waiocb->is_linear = true;
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:09:34 +01:00
|
|
|
memset(&waiocb->ov, 0, sizeof(waiocb->ov));
|
|
|
|
waiocb->ov.Offset = (DWORD)offset;
|
|
|
|
waiocb->ov.OffsetHigh = (DWORD)(offset >> 32);
|
|
|
|
waiocb->ov.hEvent = event_notifier_get_handle(&aio->e);
|
|
|
|
|
2012-10-26 11:43:58 +02:00
|
|
|
aio->count++;
|
|
|
|
|
|
|
|
if (type & QEMU_AIO_READ) {
|
|
|
|
rc = ReadFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
|
|
|
|
} else {
|
|
|
|
rc = WriteFile(hfile, waiocb->buf, waiocb->nbytes, NULL, &waiocb->ov);
|
|
|
|
}
|
|
|
|
if(rc == 0 && GetLastError() != ERROR_IO_PENDING) {
|
|
|
|
goto out_dec_count;
|
|
|
|
}
|
|
|
|
return &waiocb->common;
|
|
|
|
|
|
|
|
out_dec_count:
|
|
|
|
aio->count--;
|
2014-05-21 18:05:47 +02:00
|
|
|
out:
|
2014-09-11 07:41:28 +02:00
|
|
|
qemu_aio_unref(waiocb);
|
2012-10-26 11:43:58 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
|
|
|
|
{
|
|
|
|
if (CreateIoCompletionPort(hfile, aio->hIOCP, (ULONG_PTR) 0, 0) == NULL) {
|
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:34:50 +02:00
|
|
|
void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
|
|
|
|
AioContext *old_context)
|
|
|
|
{
|
aio-posix: split poll check from ready handler
Adaptive polling measures the execution time of the polling check plus
handlers called when a polled event becomes ready. Handlers can take a
significant amount of time, making it look like polling was running for
a long time when in fact the event handler was running for a long time.
For example, on Linux the io_submit(2) syscall invoked when a virtio-blk
device's virtqueue becomes ready can take 10s of microseconds. This
can exceed the default polling interval (32 microseconds) and cause
adaptive polling to stop polling.
By excluding the handler's execution time from the polling check we make
the adaptive polling calculation more accurate. As a result, the event
loop now stays in polling mode where previously it would have fallen
back to file descriptor monitoring.
The following data was collected with virtio-blk num-queues=2
event_idx=off using an IOThread. Before:
168k IOPS, IOThread syscalls:
9837.115 ( 0.020 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 16, iocbpp: 0x7fcb9f937db0) = 16
9837.158 ( 0.002 ms): IO iothread1/620155 write(fd: 103, buf: 0x556a2ef71b88, count: 8) = 8
9837.161 ( 0.001 ms): IO iothread1/620155 write(fd: 104, buf: 0x556a2ef71b88, count: 8) = 8
9837.163 ( 0.001 ms): IO iothread1/620155 ppoll(ufds: 0x7fcb90002800, nfds: 4, tsp: 0x7fcb9f1342d0, sigsetsize: 8) = 3
9837.164 ( 0.001 ms): IO iothread1/620155 read(fd: 107, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.174 ( 0.001 ms): IO iothread1/620155 read(fd: 105, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.176 ( 0.001 ms): IO iothread1/620155 read(fd: 106, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.209 ( 0.035 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 32, iocbpp: 0x7fca7d0cebe0) = 32
174k IOPS (+3.6%), IOThread syscalls:
9809.566 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0cdd62be0) = 32
9809.625 ( 0.001 ms): IO iothread1/623061 write(fd: 103, buf: 0x5647cfba5f58, count: 8) = 8
9809.627 ( 0.002 ms): IO iothread1/623061 write(fd: 104, buf: 0x5647cfba5f58, count: 8) = 8
9809.663 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0d0388b50) = 32
Notice that ppoll(2) and eventfd read(2) syscalls are eliminated because
the IOThread stays in polling mode instead of falling back to file
descriptor monitoring.
As usual, polling is not implemented on Windows so this patch ignores
the new io_poll_read() callback in aio-win32.c.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Message-id: 20211207132336.36627-2-stefanha@redhat.com
[Fixed up aio_set_event_notifier() calls in
tests/unit/test-fdmon-epoll.c added after this series was queued.
--Stefan]
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2021-12-07 14:23:31 +01:00
|
|
|
aio_set_event_notifier(old_context, &aio->e, false, NULL, NULL, NULL);
|
2017-02-13 14:52:30 +01:00
|
|
|
aio->aio_ctx = NULL;
|
2014-05-08 16:34:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
|
|
|
|
AioContext *new_context)
|
|
|
|
{
|
2017-02-13 14:52:30 +01:00
|
|
|
aio->aio_ctx = new_context;
|
2015-10-23 05:08:05 +02:00
|
|
|
aio_set_event_notifier(new_context, &aio->e, false,
|
aio-posix: split poll check from ready handler
Adaptive polling measures the execution time of the polling check plus
handlers called when a polled event becomes ready. Handlers can take a
significant amount of time, making it look like polling was running for
a long time when in fact the event handler was running for a long time.
For example, on Linux the io_submit(2) syscall invoked when a virtio-blk
device's virtqueue becomes ready can take 10s of microseconds. This
can exceed the default polling interval (32 microseconds) and cause
adaptive polling to stop polling.
By excluding the handler's execution time from the polling check we make
the adaptive polling calculation more accurate. As a result, the event
loop now stays in polling mode where previously it would have fallen
back to file descriptor monitoring.
The following data was collected with virtio-blk num-queues=2
event_idx=off using an IOThread. Before:
168k IOPS, IOThread syscalls:
9837.115 ( 0.020 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 16, iocbpp: 0x7fcb9f937db0) = 16
9837.158 ( 0.002 ms): IO iothread1/620155 write(fd: 103, buf: 0x556a2ef71b88, count: 8) = 8
9837.161 ( 0.001 ms): IO iothread1/620155 write(fd: 104, buf: 0x556a2ef71b88, count: 8) = 8
9837.163 ( 0.001 ms): IO iothread1/620155 ppoll(ufds: 0x7fcb90002800, nfds: 4, tsp: 0x7fcb9f1342d0, sigsetsize: 8) = 3
9837.164 ( 0.001 ms): IO iothread1/620155 read(fd: 107, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.174 ( 0.001 ms): IO iothread1/620155 read(fd: 105, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.176 ( 0.001 ms): IO iothread1/620155 read(fd: 106, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.209 ( 0.035 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 32, iocbpp: 0x7fca7d0cebe0) = 32
174k IOPS (+3.6%), IOThread syscalls:
9809.566 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0cdd62be0) = 32
9809.625 ( 0.001 ms): IO iothread1/623061 write(fd: 103, buf: 0x5647cfba5f58, count: 8) = 8
9809.627 ( 0.002 ms): IO iothread1/623061 write(fd: 104, buf: 0x5647cfba5f58, count: 8) = 8
9809.663 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0d0388b50) = 32
Notice that ppoll(2) and eventfd read(2) syscalls are eliminated because
the IOThread stays in polling mode instead of falling back to file
descriptor monitoring.
As usual, polling is not implemented on Windows so this patch ignores
the new io_poll_read() callback in aio-win32.c.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Message-id: 20211207132336.36627-2-stefanha@redhat.com
[Fixed up aio_set_event_notifier() calls in
tests/unit/test-fdmon-epoll.c added after this series was queued.
--Stefan]
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2021-12-07 14:23:31 +01:00
|
|
|
win32_aio_completion_cb, NULL, NULL);
|
2014-05-08 16:34:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 11:43:58 +02:00
|
|
|
QEMUWin32AIOState *win32_aio_init(void)
|
|
|
|
{
|
|
|
|
QEMUWin32AIOState *s;
|
|
|
|
|
|
|
|
s = g_malloc0(sizeof(*s));
|
|
|
|
if (event_notifier_init(&s->e, false) < 0) {
|
|
|
|
goto out_free_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
|
|
|
if (s->hIOCP == NULL) {
|
|
|
|
goto out_close_efd;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
out_close_efd:
|
|
|
|
event_notifier_cleanup(&s->e);
|
|
|
|
out_free_state:
|
|
|
|
g_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-08 16:34:49 +02:00
|
|
|
|
|
|
|
void win32_aio_cleanup(QEMUWin32AIOState *aio)
|
|
|
|
{
|
2017-02-13 14:52:30 +01:00
|
|
|
assert(!aio->aio_ctx);
|
2014-05-08 16:34:49 +02:00
|
|
|
CloseHandle(aio->hIOCP);
|
|
|
|
event_notifier_cleanup(&aio->e);
|
|
|
|
g_free(aio);
|
|
|
|
}
|