From nobody Fri Apr 26 03:27:05 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1549635159258549.6471882868236; Fri, 8 Feb 2019 06:12:39 -0800 (PST) Received: from localhost ([127.0.0.1]:57191 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gs6tL-0000Oy-JY for importer@patchew.org; Fri, 08 Feb 2019 09:12:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:34735) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gs6sU-0008VH-Bp for qemu-devel@nongnu.org; Fri, 08 Feb 2019 09:11:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gs6sO-0007Rl-OV for qemu-devel@nongnu.org; Fri, 08 Feb 2019 09:11:34 -0500 Received: from relay.sw.ru ([185.231.240.75]:40152) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gs6sK-0007PQ-PH; Fri, 08 Feb 2019 09:11:28 -0500 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gs6sF-0003mD-4O; Fri, 08 Feb 2019 17:11:23 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Fri, 8 Feb 2019 17:11:22 +0300 Message-Id: <20190208141122.53046-1-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [RFC PATCH] coroutines: generate wrapper code X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, ehabkost@redhat.com, mreitz@redhat.com, stefanha@redhat.com, crosa@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Hi all! We have a very frequent pattern of wrapping a coroutine_fn function to be called from non-coroutine context: - create structure to pack parameters - create function to call original function taking parameters from struct - create wrapper, which in case of non-coroutine context will create a coroutine, enter it and start poll-loop. Here is a draft of template code + example how it can be used to drop a lot of similar code. Hope someone like it except me) Hmm, it's possible to write it in pure python without jinja2, but I think it's not a true way. It's also possible to generate functions which will just create a corresponding coroutines, for not generic cases. Such generated generators should look like Coroutine *bdrv_co_check__create_co( BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix, int *_ret, bool *_in_progress); - same parameters as in original bdrv_co_check(), plus out-pointers to set return value and mark finished. Signed-off-by: Vladimir Sementsov-Ogievskiy --- Makefile | 5 ++ Makefile.objs | 2 +- include/block/block.h | 3 ++ block.c | 77 ++--------------------------- coroutine-wrapper | 2 + scripts/coroutine-wrapper.py | 96 ++++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+), 75 deletions(-) create mode 100644 coroutine-wrapper create mode 100755 scripts/coroutine-wrapper.py diff --git a/Makefile b/Makefile index 1278a3eb52..8d19b06cf1 100644 --- a/Makefile +++ b/Makefile @@ -122,6 +122,8 @@ endif =20 GENERATED_FILES +=3D module_block.h =20 +GENERATED_FILES +=3D block-gen.c + TRACE_HEADERS =3D trace-root.h $(trace-events-subdirs:%=3D%/trace.h) TRACE_SOURCES =3D trace-root.c $(trace-events-subdirs:%=3D%/trace.c) TRACE_DTRACE =3D @@ -138,6 +140,9 @@ GENERATED_FILES +=3D $(TRACE_SOURCES) GENERATED_FILES +=3D $(BUILD_DIR)/trace-events-all GENERATED_FILES +=3D .git-submodule-status =20 +block-gen.c: coroutine-wrapper $(SRC_PATH)/scripts/coroutine-wrapper.py + $(call quiet-command, python $(SRC_PATH)/scripts/coroutine-wrapper.py < $= < > $@,"GEN","$(TARGET_DIR)$@") + trace-group-name =3D $(shell dirname $1 | sed -e 's/[^a-zA-Z0-9]/_/g') =20 tracetool-y =3D $(SRC_PATH)/scripts/tracetool.py diff --git a/Makefile.objs b/Makefile.objs index 67a054b08a..16159d3e0f 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -23,7 +23,7 @@ slirp-obj-$(CONFIG_SLIRP) =3D slirp/ # block-obj-y is code used by both qemu system emulation and qemu-img =20 block-obj-y +=3D nbd/ -block-obj-y +=3D block.o blockjob.o job.o +block-obj-y +=3D block.o blockjob.o job.o block-gen.o block-obj-y +=3D block/ scsi/ block-obj-y +=3D qemu-io-cmds.o block-obj-$(CONFIG_REPLICATION) +=3D replication.o diff --git a/include/block/block.h b/include/block/block.h index 57233cf2c0..61b2ca6fe5 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -371,6 +371,8 @@ typedef enum { } BdrvCheckMode; =20 int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode f= ix); +int coroutine_fn bdrv_co_check(BlockDriverState *bs, + BdrvCheckResult *res, BdrvCheckMode fix); =20 /* The units of offset and total_work_size may be chosen arbitrarily by the * block driver; total_work_size may change during the course of the amend= ment @@ -399,6 +401,7 @@ int bdrv_co_ioctl(BlockDriverState *bs, int req, void *= buf); =20 /* Invalidate any cached metadata used by image formats */ void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp); +void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **e= rrp); void bdrv_invalidate_cache_all(Error **errp); int bdrv_inactivate_all(void); =20 diff --git a/block.c b/block.c index b67d9b7b65..37ab050f0e 100644 --- a/block.c +++ b/block.c @@ -3706,8 +3706,8 @@ static void bdrv_delete(BlockDriverState *bs) * free of errors) or -errno when an internal error occurred. The results = of the * check are stored in res. */ -static int coroutine_fn bdrv_co_check(BlockDriverState *bs, - BdrvCheckResult *res, BdrvCheckMode = fix) +int coroutine_fn bdrv_co_check(BlockDriverState *bs, + BdrvCheckResult *res, BdrvCheckMode fix) { if (bs->drv =3D=3D NULL) { return -ENOMEDIUM; @@ -3720,43 +3720,6 @@ static int coroutine_fn bdrv_co_check(BlockDriverSta= te *bs, return bs->drv->bdrv_co_check(bs, res, fix); } =20 -typedef struct CheckCo { - BlockDriverState *bs; - BdrvCheckResult *res; - BdrvCheckMode fix; - int ret; -} CheckCo; - -static void bdrv_check_co_entry(void *opaque) -{ - CheckCo *cco =3D opaque; - cco->ret =3D bdrv_co_check(cco->bs, cco->res, cco->fix); - aio_wait_kick(); -} - -int bdrv_check(BlockDriverState *bs, - BdrvCheckResult *res, BdrvCheckMode fix) -{ - Coroutine *co; - CheckCo cco =3D { - .bs =3D bs, - .res =3D res, - .ret =3D -EINPROGRESS, - .fix =3D fix, - }; - - if (qemu_in_coroutine()) { - /* Fast-path if already in coroutine context */ - bdrv_check_co_entry(&cco); - } else { - co =3D qemu_coroutine_create(bdrv_check_co_entry, &cco); - bdrv_coroutine_enter(bs, co); - BDRV_POLL_WHILE(bs, cco.ret =3D=3D -EINPROGRESS); - } - - return cco.ret; -} - /* * Return values: * 0 - success @@ -4623,8 +4586,7 @@ void bdrv_init_with_whitelist(void) bdrv_init(); } =20 -static void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, - Error **errp) +void coroutine_fn bdrv_co_invalidate_cache(BlockDriverState *bs, Error **e= rrp) { BdrvChild *child, *parent; uint64_t perm, shared_perm; @@ -4705,39 +4667,6 @@ static void coroutine_fn bdrv_co_invalidate_cache(Bl= ockDriverState *bs, } } =20 -typedef struct InvalidateCacheCo { - BlockDriverState *bs; - Error **errp; - bool done; -} InvalidateCacheCo; - -static void coroutine_fn bdrv_invalidate_cache_co_entry(void *opaque) -{ - InvalidateCacheCo *ico =3D opaque; - bdrv_co_invalidate_cache(ico->bs, ico->errp); - ico->done =3D true; - aio_wait_kick(); -} - -void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) -{ - Coroutine *co; - InvalidateCacheCo ico =3D { - .bs =3D bs, - .done =3D false, - .errp =3D errp - }; - - if (qemu_in_coroutine()) { - /* Fast-path if already in coroutine context */ - bdrv_invalidate_cache_co_entry(&ico); - } else { - co =3D qemu_coroutine_create(bdrv_invalidate_cache_co_entry, &ico); - bdrv_coroutine_enter(bs, co); - BDRV_POLL_WHILE(bs, !ico.done); - } -} - void bdrv_invalidate_cache_all(Error **errp) { BlockDriverState *bs; diff --git a/coroutine-wrapper b/coroutine-wrapper new file mode 100644 index 0000000000..9d8bc35afc --- /dev/null +++ b/coroutine-wrapper @@ -0,0 +1,2 @@ +bdrv_check: int bdrv_co_check(BlockDriverState *bs, BdrvCheckResult *res, = BdrvCheckMode fix) +bdrv_invalidate_cache: void bdrv_co_invalidate_cache(BlockDriverState *bs,= Error **errp) diff --git a/scripts/coroutine-wrapper.py b/scripts/coroutine-wrapper.py new file mode 100755 index 0000000000..16cd2d5e71 --- /dev/null +++ b/scripts/coroutine-wrapper.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +import re +from jinja2 import Environment + +env =3D Environment(trim_blocks=3DTrue, lstrip_blocks=3DTrue) + +header =3D """/* + * File is generated by scripts/coroutine-wrapper.py + */ + +#include "qemu/osdep.h" +#include "block/block_int.h" +""" + +template =3D """ +{% set has_ret =3D ret_type !=3D 'void' %} +{% set arg_names =3D args|map(attribute=3D'name')|list %} + +/* + * Wrappers for {{name}} + */ + +typedef struct {{name}}__ArgumentsPack { +{% for arg in args %} + {{arg['full']}}; +{% endfor %} + +{% if has_ret %} + {{ret_type}} _ret; +{% endif %} + bool _in_progress; +} {{name}}__ArgumentsPack; + +static void {{name}}__entry(void *opaque) +{ + {{name}}__ArgumentsPack *pack =3D opaque; + {%+ if has_ret %}pack->_ret =3D {% endif %}{{name}}(pack->{{ arg_names= |join(', pack->') }}); + pack->_in_progress =3D false; + aio_wait_kick(); +} + +{{ret_type}} {{bdrv_poll_name}}({{args|join(', ', attribute=3D'full')}}) +{ + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + {%+ if has_ret %}return {% endif %}{{name}}({{ arg_names|join(', '= ) }}); + } else { + {{name}}__ArgumentsPack pack =3D { + {% for arg in args %} + .{{arg['name']}} =3D {{arg['name']}}, + {% endfor %} + + ._in_progress =3D true, + }; + Coroutine *co =3D qemu_coroutine_create({{name}}__entry, &pack); + bdrv_coroutine_enter(bs, co); + BDRV_POLL_WHILE(bs, pack._in_progress); + {% if has_ret %} + + return pack._ret; + {% endif %} + } +} +""" + +func_decl_re =3D re.compile(r'^(([^:]+):\s*)?([^(]+) ([a-z][a-z0-9_]*)\((.= *)\)$') +param_re =3D re.compile(r'(.*[ *])([a-z][a-z0-9_]*)') + +def make_wrapper(funcrion_declaration): + params =3D {} + + try: + m =3D func_decl_re.match(funcrion_declaration) + params['ret_type'] =3D m.group(3) + params['name'] =3D m.group(4) + params['bdrv_poll_name'] =3D m.group(2) or params['name'] + '__bdr= v_poll' + raw_args =3D m.group(5).split(', ') + args =3D [] + for raw_arg in raw_args: + arg =3D param_re.match(raw_arg).group(0, 1, 2) + args.append({'full': arg[0], 'type': arg[1], 'name': arg[2]}) + + params['args'] =3D args + except ValueError: + sys.exit('Failed to parse function declaration') + + return env.from_string(template).render(**params) + + +if __name__ =3D=3D '__main__': + import sys + + print(header) + for line in sys.stdin: + print(make_wrapper(line)) --=20 2.18.0