Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp638517lqg; Fri, 1 Mar 2024 17:02:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUxPJjv2MPVqttosFw16+wW/R82bjC2VoEdfxsmJOqEthWRm/sH5NJ7UfQY8Hpoy+wk2PaFhUy+TMLTMdVqd8naM8nTz4LTSfuw62/4sg== X-Google-Smtp-Source: AGHT+IF+cLXdUSH4yNe5RD7Tn/0k3xh9iyoQGhruj1Efop2Lbga6pL1N8eCcYzHwP3NNx76g/Nw1 X-Received: by 2002:a17:906:fa14:b0:a44:a5a:2a23 with SMTP id lo20-20020a170906fa1400b00a440a5a2a23mr2435434ejb.5.1709341324624; Fri, 01 Mar 2024 17:02:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709341324; cv=pass; d=google.com; s=arc-20160816; b=Ap8a1R1/vm4Wuk6EiLUqHrRcAEFenkMeZUtPP5zGnnAa4pgKSIa3oqxL01D+VvzWnO kn5NQYISf1Wldxhawd9FOXETsU3ZZfJ0GLsrXtd43y734ojJVcxNxOlXbP0sjjNGpHpO uOC7ucUqxFD39D2Mn9u5QhUjgydCpEbf95ZF13tEou/ilkNFWATgUAN9ktw/Lhh2tjbB jzmOV+RD0pqWXyiOR8lCPX3NyZwHwDzWKJen6RbVVm/3PpHa7pXYTWzbLDFm0kwzdaTr i8UYAxFduO6HiK8/izRwiEH88h8qy/zfNbPI4y2C/0PgdZtNtKWQyxygzBilnXsZMsIk gUng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=CEuSAf1lkMchEU55uC9f020RZIQvzTohuVPV0uKWKVI=; fh=RSdAe3QNzwn92fTF1+6a92H4RXopTT2x2OApq0bgktI=; b=RgrxKc26GpdLhcDCVyrXo2pibBrMh5Q59B5+lgMOstjl4I6I1yqXFbWgPcdN0xsQuw tR1hd5XNgBmp4HmSFqq13KHD65f+HxSPIX92AscoNaQcyzoH4mzuIn6i70uigosj+ADD cma1hiIKr566mFsJB+j4IMxu4a39gpsgbIclAzf9aY8dfTbw6t3K73jg88vTKIaqLdMl A98ad5I1LFE/BETPaVZs8ZKLle6xyIjvBVNgtAsTi7puspJlJ3fci53hRq5sLrNHbvPH ALAsri36MF8lRBJzg81rvPgnOUcDKhpCTQPia4t1EMLFeDETS+4l/tZFyOzPA46EGq16 nwoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n32Z0gpv; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-89282-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89282-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id kz1-20020a17090777c100b00a4432548165si2006104ejc.552.2024.03.01.17.02.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 17:02:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-89282-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=n32Z0gpv; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-89282-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-89282-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 35CF21F23AE5 for ; Sat, 2 Mar 2024 01:02:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8D19E1756E; Sat, 2 Mar 2024 01:00:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n32Z0gpv" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 257EC1642B for ; Sat, 2 Mar 2024 01:00:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709341219; cv=none; b=MjOcb3NpPqjizzC2Iy18jYHcdd0GDl5jq8/BkXHz6gp5F0dFyl/IXLlv7r+z0y6CzLlWR678GgeHhbgxir0+4h1hYT72T9wBRbeKNh/4K1H8Ssk2XId+nJXVgIDOLA/hSrNBOjkcVkHeispOXLZcgXb3eXS9awQxIHfBbXHId/g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709341219; c=relaxed/simple; bh=KBRLOPIttIVmUTXmYYpZ5y5zAjJINB3dv9EpY8rLPGk=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=YASz+6JoF3GJArkfzzgTqfpDlGWMDlc9jtqQIFfcI/22KR2Nl3erBXG4Ku/x3kG8x+USJiPR4Ngqb28cBv7uExRUY2CwqBhbNgKG/t55FTXsindbvXtlX3N3JPZfjTy3GLbJsk+Mww/zf509Ing0lecV/K2UR5q31NYWCvOPc6A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n32Z0gpv; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6097409275bso35137997b3.0 for ; Fri, 01 Mar 2024 17:00:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709341217; x=1709946017; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=CEuSAf1lkMchEU55uC9f020RZIQvzTohuVPV0uKWKVI=; b=n32Z0gpvPPT2RMv9UttL3rcM6xcZ96lOJy5YZuWKSQHy7QhaQHqkjw3ne6BbBrYStd YtJ8sC42cq8Pe7TCQnnTFbSOrM4o0z8/LRE/nU1Cdg6jLH8QeUpyAqOXfP9+UDVlHrGC E+wjLAnJqE/sbWvlFa9u+tKguGkeVwWhQYqnQqSKDGWcv35Z/B5HcaWyRo6Jyzu1sJUK P2Adce1O3+MnJqH2dBvDF/WgzC69bnG9plbqwcMz5r0AcTMIuPxK+SuU+qdAdq24hlKV ELsBjUvlJbBD7IRh7YSs2GKXM3Kp8+TBZ4/zJ9kCdDSwvaD/GoOWzNVgRl6sWnSrRDJ5 tMjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709341217; x=1709946017; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CEuSAf1lkMchEU55uC9f020RZIQvzTohuVPV0uKWKVI=; b=pOWklZWyQRX8BX5t4D7TQsCCDDV82MS0MJmP71GQGkrVMIOEdygyr+UqWnSZROf2u+ Vcdj0NHiA5q0NncyiFycPaRENin7mLiBvawWm/hhKroBgH0iZAmiEttHdtVxXIanogIs 2PvmWAsMWL4HSGzrJ31lyhl8X2TWVBvkAnLjB62htxuEyXRFHg+CYRprcoshkdKwIqFv CrHNF82hpD/dZs2EMaSv33oiuipvEXsGUtjZcV04IMyboDafCkrwXuOe8r8Uz5EOJ/RH x/MyEictPyIOl/NDdJHRR7r4uW1ywB8LJcxCwqhin0JhYN+Hq+B1Wo/eNiu3AsIJJYDO CSOg== X-Forwarded-Encrypted: i=1; AJvYcCUxgqbtZ9F5R9tFxXlqqRxIVY0IjXezj3980M4B3C+cvkcpfALZbgAwMfzNCcoodLFw0h3zk41y3ixHS/pighXvv6MmeqykOn8ES6fG X-Gm-Message-State: AOJu0YxeLd0x/j66hopsqaSIUk+7BTRgIRd1gTVfSbO5b8eEXXVcblld 9m4jz9fudjMqUTkElKEyv/hiUcQvm7OsAP6WckPkafBDUQrwIF8l2cCTIdGNzm5aIqQsuCiTaKv R1bze0w== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:aba7:66c5:3365:7114]) (user=irogers job=sendgmr) by 2002:a05:690c:fc3:b0:608:ced0:eb2e with SMTP id dg3-20020a05690c0fc300b00608ced0eb2emr638850ywb.0.1709341217224; Fri, 01 Mar 2024 17:00:17 -0800 (PST) Date: Fri, 1 Mar 2024 16:59:46 -0800 In-Reply-To: <20240302005950.2847058-1-irogers@google.com> Message-Id: <20240302005950.2847058-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240302005950.2847058-1-irogers@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Subject: [PATCH v2 08/12] perf jevents: Move json encoding to its own functions From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , John Garry , Kan Liang , Jing Zhang , Thomas Richter , James Clark , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Andi Kleen , Kajol Jain , Sandipan Das , Ravi Bangoria , Perry Taylor , Samantha Alt , Caleb Biggers , Weilin Wang , Edward Baker , Stephane Eranian Content-Type: text/plain; charset="UTF-8" Have dedicate encode functions rather than having them embedded in MetricGroup. This is to provide some uniformity in the Metric ToXXX routines. Signed-off-by: Ian Rogers --- tools/perf/pmu-events/metric.py | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/tools/perf/pmu-events/metric.py b/tools/perf/pmu-events/metric.py index e81fed2e29b5..b39189182608 100644 --- a/tools/perf/pmu-events/metric.py +++ b/tools/perf/pmu-events/metric.py @@ -484,15 +484,6 @@ class Metric: def ToMetricGroupDescriptions(self, root: bool = True) -> Dict[str, str]: return {} -class _MetricJsonEncoder(json.JSONEncoder): - """Special handling for Metric objects.""" - - def default(self, o): - if isinstance(o, Metric): - return o.ToPerfJson() - return json.JSONEncoder.default(self, o) - - class MetricGroup: """A group of metrics. @@ -523,8 +514,11 @@ class MetricGroup: return result - def ToPerfJson(self) -> str: - return json.dumps(sorted(self.Flatten()), indent=2, cls=_MetricJsonEncoder) + def ToPerfJson(self) -> List[Dict[str, str]]: + result = [] + for x in sorted(self.Flatten()): + result.append(x.ToPerfJson()) + return result def ToMetricGroupDescriptions(self, root: bool = True) -> Dict[str, str]: result = {self.name: self.description} if self.description else {} @@ -533,7 +527,23 @@ class MetricGroup: return result def __str__(self) -> str: - return self.ToPerfJson() + return str(self.ToPerfJson()) + + +def JsonEncodeMetric(x: MetricGroup): + class MetricJsonEncoder(json.JSONEncoder): + """Special handling for Metric objects.""" + + def default(self, o): + if isinstance(o, Metric) or isinstance(o, MetricGroup): + return o.ToPerfJson() + return json.JSONEncoder.default(self, o) + + return json.dumps(x, indent=2, cls=MetricJsonEncoder) + + +def JsonEncodeMetricGroupDescriptions(x: MetricGroup): + return json.dumps(x.ToMetricGroupDescriptions(), indent=2) class _RewriteIfExpToSelect(ast.NodeTransformer): -- 2.44.0.278.ge034bb2e1d-goog