From nobody Fri Apr 19 19:27:06 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1669204305; cv=none; d=zohomail.com; s=zohoarc; b=hF1e8WKvP2MgbyGo+luut8vHRTHC/AhuvhEETVjPQ1UUB44+OqLvrXa65xtk8yjAWb6hCgvTVMvbnAusso4Ql+KcWJzu+viNad6S3P48mIVEwRl8zgpz61nM9fO1R61TdRojUiwZ8+GsFEvICltC96EVKO6mQBuSqWyQk7hzlVs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669204305; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=PcTwWBMjg4w4P6lxlZz9ZldmXaYa6AY+1XzFhM0vsu8=; b=TLYIRNADFp9bM7EMGADagnXielk9D/oc2gHXVp0eTvPzRos6aMe5DHpaT+Yl2p5uj/+wF8K5J5IwalgCxBpYIxzFetwbgncxOUxsH4QUoqMWfAkr9xyIWXRVFTf2FjgcGxU3YmmU6Ive7R04XP4CW57Q1HYXtqvpbSwx5H+igEE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1669204305028750.7875167450072; Wed, 23 Nov 2022 03:51:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oxo9P-0002kg-3u; Wed, 23 Nov 2022 06:42:47 -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 1oxo9I-0002eO-Q3 for qemu-devel@nongnu.org; Wed, 23 Nov 2022 06:42:40 -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 1oxo9G-000620-BV for qemu-devel@nongnu.org; Wed, 23 Nov 2022 06:42:40 -0500 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-207-tdH_CFeqNk2peblAUipVzw-1; Wed, 23 Nov 2022 06:42:34 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 05F2529DD9A8; Wed, 23 Nov 2022 11:42:34 +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 A2E4CC1908E; Wed, 23 Nov 2022 11:42:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669203757; 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=PcTwWBMjg4w4P6lxlZz9ZldmXaYa6AY+1XzFhM0vsu8=; b=DIqYwVuPVTSsW0uphlC1BekZKeT+GtSZb/rIIKuF1gsr3LtLva2meTdwM+/3D5RsuXn7fo OCDeVr7nmR+u18mGREPV+G+wzKGtUP8wmDm0+epQ8+6/D2k7JkpBq2w2gGV/+druj/+m9j /VB4y6LakSclC5yIYBI2/MzaQmTM7BI= X-MC-Unique: tdH_CFeqNk2peblAUipVzw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Cc: Kevin Wolf , Hanna Reitz , John Snow , Vladimir Sementsov-Ogievskiy , Eric Blake , Fam Zheng , Stefan Hajnoczi , "Denis V. Lunev" , Stefan Weil , Jeff Cody , Cleber Rosa , qemu-devel@nongnu.org, Emanuele Giuseppe Esposito Subject: [PATCH v5 10/15] block-coroutine-wrapper.py: introduce generated_co_wrapper_simple Date: Wed, 23 Nov 2022 06:42:22 -0500 Message-Id: <20221123114227.85757-11-eesposit@redhat.com> In-Reply-To: <20221123114227.85757-1-eesposit@redhat.com> References: <20221123114227.85757-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 Received-SPF: pass (zohomail.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; 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=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669204305753100001 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Kevin Wolf --- include/block/block-common.h | 1 + scripts/block-coroutine-wrapper.py | 105 ++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 31 deletions(-) diff --git a/include/block/block-common.h b/include/block/block-common.h index 24de1d63fd..46b57501d8 100644 --- a/include/block/block-common.h +++ b/include/block/block-common.h @@ -54,6 +54,7 @@ * Read more in docs/devel/block-coroutine-wrapper.rst */ #define generated_co_wrapper +#define generated_co_wrapper_simple =20 /* block.c */ typedef struct BlockDriver BlockDriver; diff --git a/scripts/block-coroutine-wrapper.py b/scripts/block-coroutine-w= rapper.py index 08be813407..2fa3d01898 100644 --- a/scripts/block-coroutine-wrapper.py +++ b/scripts/block-coroutine-wrapper.py @@ -62,10 +62,28 @@ def __init__(self, param_decl: str) -> None: =20 =20 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 =3D return_type.strip() self.name =3D name.strip() + self.struct_name =3D snake_to_camel(self.name) self.args =3D [ParamDecl(arg.strip()) for arg in args.split(',')] + self.create_only_co =3D False + + if variant =3D=3D '_simple': + self.create_only_co =3D True + + subsystem, subname =3D self.name.split('_', 1) + self.co_name =3D f'{subsystem}_co_{subname}' + + t =3D self.args[0].type + if t =3D=3D 'BlockDriverState *': + bs =3D 'bs' + elif t =3D=3D 'BdrvChild *': + bs =3D 'child->bs' + else: + bs =3D 'blk_bs(blk)' + self.bs =3D bs =20 def gen_list(self, format: str) -> str: return ', '.join(format.format_map(arg.__dict__) for arg in self.a= rgs) @@ -75,7 +93,8 @@ def gen_block(self, format: str) -> str: =20 =20 # Match wrappers declared with a generated_co_wrapper mark -func_decl_re =3D re.compile(r'^int\s*generated_co_wrapper\s*' +func_decl_re =3D 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) =20 @@ -84,7 +103,8 @@ def func_decl_iter(text: str) -> Iterator: for m in func_decl_re.finditer(text): yield FuncDecl(return_type=3D'int', name=3Dm.group('wrapper_name'), - args=3Dm.group('args')) + args=3Dm.group('args'), + variant=3Dm.group('variant')) =20 =20 def snake_to_camel(func_name: str) -> str: @@ -97,24 +117,63 @@ def snake_to_camel(func_name: str) -> str: return ''.join(words) =20 =20 +# Checks if we are already in coroutine +def create_g_c_w(func: FuncDecl) -> str: + name =3D func.co_name + struct_name =3D 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 =3D {{ + .poll_state.bs =3D {func.bs}, + .poll_state.in_progress =3D true, + +{ func.gen_block(' .{name} =3D {name},') } + }}; + + s.poll_state.co =3D 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 =3D func.co_name + struct_name =3D func.struct_name + return f"""\ +int {func.name}({ func.gen_list('{decl}') }) +{{ + {struct_name} s =3D {{ + .poll_state.bs =3D {func.bs}, + .poll_state.in_progress =3D true, + +{ func.gen_block(' .{name} =3D {name},') } + }}; + assert(!qemu_in_coroutine()); + + s.poll_state.co =3D 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 =3D=3D 'int' assert func.args[0].type in ['BlockDriverState *', 'BdrvChild *', 'BlockBackend *'] =20 - subsystem, subname =3D func.name.split('_', 1) - - name =3D f'{subsystem}_co_{subname}' + name =3D func.co_name + struct_name =3D func.struct_name =20 - t =3D func.args[0].type - if t =3D=3D 'BlockDriverState *': - bs =3D 'bs' - elif t =3D=3D 'BdrvChild *': - bs =3D 'child->bs' - else: - bs =3D 'blk_bs(blk)' - struct_name =3D snake_to_camel(name) + creation_function =3D create_g_c_w + if func.create_only_co: + creation_function =3D create_coroutine_only =20 return f"""\ /* @@ -136,23 +195,7 @@ def gen_wrapper(func: FuncDecl) -> str: aio_wait_kick(); }} =20 -int {func.name}({ func.gen_list('{decl}') }) -{{ - if (qemu_in_coroutine()) {{ - return {name}({ func.gen_list('{name}') }); - }} else {{ - {struct_name} s =3D {{ - .poll_state.bs =3D {bs}, - .poll_state.in_progress =3D true, - -{ func.gen_block(' .{name} =3D {name},') } - }}; - - s.poll_state.co =3D qemu_coroutine_create({name}_entry, &s); - - return bdrv_poll_co(&s.poll_state); - }} -}}""" +{creation_function(func)}""" =20 =20 def gen_wrappers(input_code: str) -> str: --=20 2.31.1