From nobody Thu Nov 28 12:46:47 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 ARC-Seal: i=1; a=rsa-sha256; t=1692780950; cv=none; d=zohomail.com; s=zohoarc; b=EMR+bcnY6sm7If5EAKWvc83p9EFQnBK6JrrfwRb4h39IiFkSOSReLvU0vcdYGgC9w0aqV/dHKJBK9dvmfZbpM4rc+K7s0ySNqnAkP4ylg5iklzoagBobbyZkYecQKopUacS6aZT8+bNVGegZCTwXvoLt1jJ38m+imxx5S+DWgZ4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692780950; 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=IU4eVSgFHWDP+jifjfGdkBe+orZ14DeYHyLKEqxQd3o=; b=Vh56VdH1YPnWd7qOhw0tA1L3+V4e8g8renB5whA4wzaCt314W04eIGm2gx18zvc1YSh7RwKmb6gBerxpL7xAjQybclNxRQNyDHYWnXVMvYX3r7zxoCZUY94cXvLU2xJNfHLb55cOf9OEAwdo25fHMFH707vrcobvG8nEFdNnK1o= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692780950174722.0024694942886; Wed, 23 Aug 2023 01:55:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qYjds-0004U8-Kl; Wed, 23 Aug 2023 04:55:08 -0400 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 1qYjdr-0004O4-Au for qemu-devel@nongnu.org; Wed, 23 Aug 2023 04:55:07 -0400 Received: from pv50p00im-tydg10021701.me.com ([17.58.6.54]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qYjdp-000051-AM for qemu-devel@nongnu.org; Wed, 23 Aug 2023 04:55:07 -0400 Received: from localhost.localdomain (pv50p00im-dlb-asmtp-mailmevip.me.com [17.56.9.10]) by pv50p00im-tydg10021701.me.com (Postfix) with ESMTPSA id 044763A0B91; Wed, 23 Aug 2023 08:55:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ynddal.dk; s=sig1; t=1692780903; bh=IU4eVSgFHWDP+jifjfGdkBe+orZ14DeYHyLKEqxQd3o=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=eBFZPj/+QRjOlp0Jdbscy36waqUe5sjFsWU+MmSJ4SzJ9Cv7qPiAhB8x5Oa3I5IcE 2BqeRv5upE57h2tgiF7O1pQF6HUhxaj7ZPE9il1K7pDJoo4zfgOPlUobDQrbnIzww1 ueKxG36/7VPnxmClfCoRVpG7sz7wauQBYoC626fJFF/p1Fu515l6HNmCsa0BJtMC6J OKCjBx1Bop6UlesuLH8BC8uXxowVENPIjkV6atLkrxhJJegTmPoUNrMBiNeX/Tb5gr zgjef10nGQCHzItYSj0Qz194tTorN+WHL3AeE1Nt2Gou7CdPmmF4TRYil4mJBtfj2M B29UZYE3omtdw== From: Mads Ynddal To: qemu-devel@nongnu.org Cc: John Snow , Mads Ynddal , Stefan Hajnoczi , Cleber Rosa , Mads Ynddal Subject: [PATCH v4 11/14] simpletrace: move event processing to Analyzer class Date: Wed, 23 Aug 2023 10:54:26 +0200 Message-Id: <20230823085429.20519-12-mads@ynddal.dk> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230823085429.20519-1-mads@ynddal.dk> References: <20230823085429.20519-1-mads@ynddal.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-ORIG-GUID: guMPptZvj-eM39Vgg2ucavEE4OC-PvId X-Proofpoint-GUID: guMPptZvj-eM39Vgg2ucavEE4OC-PvId X-Proofpoint-Virus-Version: =?UTF-8?Q?vendor=3Dfsecure_engine=3D1.1.170-22c6f66c430a71ce266a39bfe25bc?= =?UTF-8?Q?2903e8d5c8f:6.0.517,18.0.883,17.11.64.514.0000000_definitions?= =?UTF-8?Q?=3D2022-06-21=5F08:2022-06-21=5F01,2022-06-21=5F08,2022-02-23?= =?UTF-8?Q?=5F01_signatures=3D0?= X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 mlxlogscore=999 clxscore=1030 bulkscore=0 adultscore=0 phishscore=0 suspectscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2308230080 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=17.58.6.54; envelope-from=mads@ynddal.dk; helo=pv50p00im-tydg10021701.me.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @ynddal.dk) X-ZM-MESSAGEID: 1692780951610100001 Content-Type: text/plain; charset="utf-8" From: Mads Ynddal Moved event processing to the Analyzer class to separate specific analyzer logic (like caching and function signatures) from the _process function. This allows for new types of Analyzer-based subclasses without changing the core code. Note, that the fn_cache is important for performance in cases where the analyzer is branching away from the catch-all a lot. The cache has no measurable performance penalty. Signed-off-by: Mads Ynddal Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Stefan Hajnoczi --- scripts/simpletrace.py | 60 +++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/scripts/simpletrace.py b/scripts/simpletrace.py index 6969fdd59a..4136d00600 100755 --- a/scripts/simpletrace.py +++ b/scripts/simpletrace.py @@ -169,6 +169,35 @@ def catchall(self, event, rec): """Called if no specific method for processing a trace event has b= een found.""" pass =20 + def _build_fn(self, event): + fn =3D getattr(self, event.name, None) + if fn is None: + # Return early to avoid costly call to inspect.getfullargspec + return self.catchall + + event_argcount =3D len(event.args) + fn_argcount =3D len(inspect.getfullargspec(fn)[0]) - 1 + if fn_argcount =3D=3D event_argcount + 1: + # Include timestamp as first argument + return lambda _, rec: fn(*(rec[1:2] + rec[3:3 + event_argcount= ])) + elif fn_argcount =3D=3D event_argcount + 2: + # Include timestamp and pid + return lambda _, rec: fn(*rec[1:3 + event_argcount]) + else: + # Just arguments, no timestamp or pid + return lambda _, rec: fn(*rec[3:3 + event_argcount]) + + def _process_event(self, rec_args, *, event, event_id, timestamp_ns, p= id, **kwargs): + if not hasattr(self, '_fn_cache'): + # NOTE: Cannot depend on downstream subclasses to have + # super().__init__() because of legacy. + self._fn_cache =3D {} + + rec =3D (event_id, timestamp_ns, pid, *rec_args) + if event_id not in self._fn_cache: + self._fn_cache[event_id] =3D self._build_fn(event) + self._fn_cache[event_id](event, rec) + def end(self): """Called at the end of the trace.""" pass @@ -222,32 +251,15 @@ def _process(events, log_fobj, analyzer, read_header= =3DTrue): if read_header: read_trace_header(log_fobj) =20 - def build_fn(analyzer, event): - if isinstance(event, str): - return analyzer.catchall - - fn =3D getattr(analyzer, event.name, None) - if fn is None: - return analyzer.catchall - - event_argcount =3D len(event.args) - fn_argcount =3D len(inspect.getfullargspec(fn)[0]) - 1 - if fn_argcount =3D=3D event_argcount + 1: - # Include timestamp as first argument - return lambda _, rec: fn(*(rec[1:2] + rec[3:3 + event_argcount= ])) - elif fn_argcount =3D=3D event_argcount + 2: - # Include timestamp and pid - return lambda _, rec: fn(*rec[1:3 + event_argcount]) - else: - # Just arguments, no timestamp or pid - return lambda _, rec: fn(*rec[3:3 + event_argcount]) - with analyzer: - fn_cache =3D {} for event, event_id, timestamp_ns, record_pid, *rec_args in read_t= race_records(events, log_fobj, read_header): - if event_id not in fn_cache: - fn_cache[event_id] =3D build_fn(analyzer, event) - fn_cache[event_id](event, (event_id, timestamp_ns, record_pid,= *rec_args)) + analyzer._process_event( + rec_args, + event=3Devent, + event_id=3Devent_id, + timestamp_ns=3Dtimestamp_ns, + pid=3Drecord_pid, + ) =20 def run(analyzer): """Execute an analyzer on a trace file given on the command-line. --=20 2.38.1