From patchwork Wed Nov 16 12:22:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 13045109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C6A9CC433FE for ; Wed, 16 Nov 2022 12:30:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ovHRZ-0000iR-2n; Wed, 16 Nov 2022 07:23:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ovHRN-0000dR-4F for qemu-devel@nongnu.org; Wed, 16 Nov 2022 07:22:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ovHRJ-0000uM-Uk for qemu-devel@nongnu.org; Wed, 16 Nov 2022 07:22:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668601368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CqI1u5ra27yhYXgdDyITKa7+J+uNHbpH9ovZo1Jgq+o=; b=NM6jLc/wvBJ1ROJFfubWowlzjYkhwN1rnPmypEuv6JG9DoE7zVKnM59RF07R3gUOh+owpa EVOkvPhETqt8DlUKk/KARpUwHI4BqY7R3u1g3fV6QedEIpU70HAmieEmxzjacYhlWD2uP+ W7sRHq96mSr5hLprYzFa4gk48vzW3Qs= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-DkFlK0EAONSN1dCFirIPKQ-1; Wed, 16 Nov 2022 07:22:45 -0500 X-MC-Unique: DkFlK0EAONSN1dCFirIPKQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0C4533C1022F; Wed, 16 Nov 2022 12:22:45 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id C4A2F9E70; Wed, 16 Nov 2022 12:22:44 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Cc: Kevin Wolf , Hanna Reitz , John Snow , Vladimir Sementsov-Ogievskiy , Eric Blake , Fam Zheng , qemu-devel@nongnu.org, Emanuele Giuseppe Esposito Subject: [PATCH v4 04/11] block-coroutine-wrapper.py: introduce generated_co_wrapper_simple Date: Wed, 16 Nov 2022 07:22:34 -0500 Message-Id: <20221116122241.2856527-5-eesposit@redhat.com> In-Reply-To: <20221116122241.2856527-1-eesposit@redhat.com> References: <20221116122241.2856527-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Received-SPF: pass client-ip=170.10.129.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This new annotation creates just a function wrapper that creates a new coroutine. It assumes the caller is not a coroutine. This is much better as g_c_w, because it is clear if the caller is a coroutine or not, and provides the advantage of automating the code creation. In the future all g_c_w functions will be substituted on g_c_w_simple. Signed-off-by: Emanuele Giuseppe Esposito --- include/block/block-common.h | 1 + scripts/block-coroutine-wrapper.py | 87 ++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/include/block/block-common.h b/include/block/block-common.h index 297704c1e9..8ae750c7cf 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -43,6 +43,7 @@ * Read more in docs/devel/block-coroutine-wrapper.rst */ #define generated_co_wrapper +#define generated_co_wrapper_simple /* block.c */ typedef struct BlockDriver BlockDriver; diff --git a/scripts/block-coroutine-wrapper.py b/scripts/block-coroutine-wrapper.py index 08be813407..f88ef53964 100644 --- a/scripts/block-coroutine-wrapper.py +++ b/scripts/block-coroutine-wrapper.py @@ -62,10 +62,15 @@ def __init__(self, param_decl: str) -> None: class FuncDecl: - def __init__(self, return_type: str, name: str, args: str) -> None: + def __init__(self, return_type: str, name: str, args: str, + variant: str) -> None: self.return_type = return_type.strip() self.name = name.strip() self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] + self.create_only_co = False + + if variant == '_simple': + self.create_only_co = True def gen_list(self, format: str) -> str: return ', '.join(format.format_map(arg.__dict__) for arg in self.args) @@ -75,7 +80,8 @@ def gen_block(self, format: str) -> str: # Match wrappers declared with a generated_co_wrapper mark -func_decl_re = re.compile(r'^int\s*generated_co_wrapper\s*' +func_decl_re = re.compile(r'^int\s*generated_co_wrapper' + r'(?P(_[a-z][a-z0-9_]*)?)\s*' r'(?P[a-z][a-z0-9_]*)' r'\((?P[^)]*)\);$', re.MULTILINE) @@ -84,7 +90,8 @@ def func_decl_iter(text: str) -> Iterator: for m in func_decl_re.finditer(text): yield FuncDecl(return_type='int', name=m.group('wrapper_name'), - args=m.group('args')) + args=m.group('args'), + variant=m.group('variant')) def snake_to_camel(func_name: str) -> str: @@ -97,6 +104,51 @@ def snake_to_camel(func_name: str) -> str: return ''.join(words) +# Checks if we are already in coroutine +def create_g_c_w(func: FuncDecl) -> str: + name = func.co_name + struct_name = func.struct_name + return f"""\ +int {func.name}({ func.gen_list('{decl}') }) +{{ + if (qemu_in_coroutine()) {{ + return {name}({ func.gen_list('{name}') }); + }} else {{ + {struct_name} s = {{ + .poll_state.bs = {func.bs}, + .poll_state.in_progress = true, + +{ func.gen_block(' .{name} = {name},') } + }}; + + s.poll_state.co = qemu_coroutine_create({name}_entry, &s); + + return bdrv_poll_co(&s.poll_state); + }} +}}""" + + +# Assumes we are not in coroutine, and creates one +def create_coroutine_only(func: FuncDecl) -> str: + name = func.co_name + struct_name = func.struct_name + return f"""\ +int {func.name}({ func.gen_list('{decl}') }) +{{ + assert(!qemu_in_coroutine()); + {struct_name} s = {{ + .poll_state.bs = {func.bs}, + .poll_state.in_progress = true, + +{ func.gen_block(' .{name} = {name},') } + }}; + + s.poll_state.co = qemu_coroutine_create({name}_entry, &s); + + return bdrv_poll_co(&s.poll_state); +}}""" + + def gen_wrapper(func: FuncDecl) -> str: assert not '_co_' in func.name assert func.return_type == 'int' @@ -105,7 +157,8 @@ def gen_wrapper(func: FuncDecl) -> str: subsystem, subname = func.name.split('_', 1) - name = f'{subsystem}_co_{subname}' + func.co_name = f'{subsystem}_co_{subname}' + name = func.co_name t = func.args[0].type if t == 'BlockDriverState *': @@ -114,7 +167,13 @@ def gen_wrapper(func: FuncDecl) -> str: bs = 'child->bs' else: bs = 'blk_bs(blk)' - struct_name = snake_to_camel(name) + func.bs = bs + func.struct_name = snake_to_camel(func.name) + struct_name = func.struct_name + + creation_function = create_g_c_w + if func.create_only_co: + creation_function = create_coroutine_only return f"""\ /* @@ -136,23 +195,7 @@ def gen_wrapper(func: FuncDecl) -> str: aio_wait_kick(); }} -int {func.name}({ func.gen_list('{decl}') }) -{{ - if (qemu_in_coroutine()) {{ - return {name}({ func.gen_list('{name}') }); - }} else {{ - {struct_name} s = {{ - .poll_state.bs = {bs}, - .poll_state.in_progress = true, - -{ func.gen_block(' .{name} = {name},') } - }}; - - s.poll_state.co = qemu_coroutine_create({name}_entry, &s); - - return bdrv_poll_co(&s.poll_state); - }} -}}""" +{creation_function(func)}""" def gen_wrappers(input_code: str) -> str: