From nobody Sun Nov 16 05:40:23 2025 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1598611381; cv=none; d=zohomail.com; s=zohoarc; b=EvHu86h/gC9Y1qM8smIQxfjttleC86YQRehMkyzlRocgiZmtHxnKtPNg/K1j9NKaMFx2TowTLSccCKPrzBNBl6n6uftOo+/6CnpJdRm0VHYvjhC8vZRBtd3HCU9dm27jm3wmvROS3FGT8rkbqPnu00jBu5EjFZ3a3/OoVTMmuvQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598611381; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=GGrLy3IbvYhI9UUc6EFtGpu2pNno9f5HSPArLmtNmFM=; b=JdvIueEQ9aOusVGg5ZNeX7G0WRtJJUKF/fTYjhO6nTLHe2GrQI5t+Ip8123BKT4Om3I0HGYg6n/4pZ5X5heCZWf1H3kQScN2ZffdooAerHaRVQmkLF4TX/FInj+jcip+qFT3XyDwJPCBk63gdjoorZAnvfIsMyg6XScX+Hm5lVk= 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) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1598611381474949.0592556375858; Fri, 28 Aug 2020 03:43:01 -0700 (PDT) Received: from localhost ([::1]:56658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBbqV-0004wk-TE for importer@patchew.org; Fri, 28 Aug 2020 06:42:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34384) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBbpB-0003MZ-Ex for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:37 -0400 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:42827) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBbp8-0005fA-Ql for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:37 -0400 Received: by mail-wr1-x442.google.com with SMTP id c18so833999wrm.9 for ; Fri, 28 Aug 2020 03:41:34 -0700 (PDT) Received: from localhost.localdomain ([197.58.77.158]) by smtp.gmail.com with ESMTPSA id e18sm1307453wrx.50.2020.08.28.03.41.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 03:41:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GGrLy3IbvYhI9UUc6EFtGpu2pNno9f5HSPArLmtNmFM=; b=ULG82S0L3kCib5VT4GaouqmZivM+bay/mzr4qks3za+D7dtBqR++RqUjn5NKpkuVLE /3WjJcnTTdwSFiaGo8qwVLUJahcQ7YdGddB3xqtaldRO0TWnk8RyT7Cau2kciF5uc7V7 Nk/GzfRznwhn6IjcEL5I8C3h1O59R/HBQrVnu7SUJ3+UrmTDzW1TF7XsuaY1bR65VcNx sZ3AFsVXCOcMZP1jtQhLb8qc/1cQPTORJ/AxlxH/uybNPNQO95i10kyWpJ8XPe87hLJA AQLF6MfAcC6rwAgym1SY74AMz2zNBol60n2pAN+xDpkD9mAz2NmUuitE7eVfTahJz+cX 4BPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GGrLy3IbvYhI9UUc6EFtGpu2pNno9f5HSPArLmtNmFM=; b=edhvkGgaW9FNDljG3BIyWtLg3vd0nXEB+/VdohZOpFWeWNs3yQU+HeWPI6aFOXHqip w9iA0fpMObgbfEg/q9ms4kU1Td1b/+f5x4Ujjxb4tbeSLEfHVuBXTCrmeu+dzRBVRdOd KTaTcdGN+h854DLMezFJJJE+lqYJyY7gep7kYzCj5BzIqTS9cqSIIVPBBcFB1hC+3U4d 7RX1jMrvlxe7E5Qh1aZIcQ2ntgdnxUDhsbZhfIezgHAVJt9rbhqI/58mbqcy9rwhmJLr FXKvpSlFTuI4+ereHs7Wv3rBgAizzUm+s/azfzQAc6n7LkmlymlVZCZkfCZ4KHaN585s GQig== X-Gm-Message-State: AOAM532cer+BEXV1Y4xxukMb6m+oXF80a4vYTa7iq27DCeVULpPw8SUI B7Snuz/ED3TY/vhWtxrk80YWy3tKpaP0UA== X-Google-Smtp-Source: ABdhPJy+UxZw0jQnURExU+I0JXJh37Lpqu5CD/VUPQVnG0uk7ICLiSSbKv/D/w+rB/FxGK0ERvMoDA== X-Received: by 2002:adf:a1d6:: with SMTP id v22mr954931wrv.185.1598611292937; Fri, 28 Aug 2020 03:41:32 -0700 (PDT) From: Ahmed Karaman To: qemu-devel@nongnu.org, aleksandar.qemu.devel@gmail.com, philmd@redhat.com, alex.bennee@linaro.org, eblake@redhat.com, ldoktor@redhat.com, jsnow@redhat.com, rth@twiddle.net, ehabkost@redhat.com, crosa@redhat.com Subject: [PATCH 1/9] scripts/performance: Refactor topN_perf.py Date: Fri, 28 Aug 2020 12:40:54 +0200 Message-Id: <20200828104102.4490-2-ahmedkhaledkaraman@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200828104102.4490-1-ahmedkhaledkaraman@gmail.com> References: <20200828104102.4490-1-ahmedkhaledkaraman@gmail.com> 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=2a00:1450:4864:20::442; envelope-from=ahmedkhaledkaraman@gmail.com; helo=mail-wr1-x442.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_PDS_OTHER_BAD_TLD=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ahmed Karaman Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" - Apply pylint and flake8 formatting rules to the script. - Use 'tempfile' instead of '/tmp' for creating temporary files. Signed-off-by: Ahmed Karaman Reviewed-by: Aleksandar Markovic --- scripts/performance/topN_perf.py | 174 +++++++++++++++---------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/scripts/performance/topN_perf.py b/scripts/performance/topN_pe= rf.py index 07be195fc8..56b100da87 100755 --- a/scripts/performance/topN_perf.py +++ b/scripts/performance/topN_perf.py @@ -1,72 +1,77 @@ #!/usr/bin/env python3 =20 -# Print the top N most executed functions in QEMU using perf. -# Syntax: -# topN_perf.py [-h] [-n] -- \ -# [] \ -# [] -# -# [-h] - Print the script arguments help message. -# [-n] - Specify the number of top functions to print. -# - If this flag is not specified, the tool defaults to 25. -# -# Example of usage: -# topN_perf.py -n 20 -- qemu-arm coulomb_double-arm -# -# This file is a part of the project "TCG Continuous Benchmarking". -# -# Copyright (C) 2020 Ahmed Karaman -# Copyright (C) 2020 Aleksandar Markovic -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . +""" +Print the top N most executed functions in QEMU using perf. + +Syntax: +topN_perf.py [-h] [-n ] -- \ + [] \ + [] + +[-h] - Print the script arguments help message. +[-n] - Specify the number of top functions to print. + - If this flag is not specified, the tool defaults to 25. + +Example of usage: +topN_perf.py -n 20 -- qemu-arm coulomb_double-arm + +This file is a part of the project "TCG Continuous Benchmarking". + +Copyright (C) 2020 Ahmed Karaman +Copyright (C) 2020 Aleksandar Markovic + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" =20 import argparse import os import subprocess import sys +import tempfile =20 =20 # Parse the command line arguments -parser =3D argparse.ArgumentParser( - usage=3D'topN_perf.py [-h] [-n] = -- ' +PARSER =3D argparse.ArgumentParser( + usage=3D'topN_perf.py [-h] [-n ] --= ' ' [] ' ' []') =20 -parser.add_argument('-n', dest=3D'top', type=3Dint, default=3D25, +PARSER.add_argument('-n', dest=3D'top', type=3Dint, default=3D25, help=3D'Specify the number of top functions to print.') =20 -parser.add_argument('command', type=3Dstr, nargs=3D'+', help=3Dargparse.SU= PPRESS) +PARSER.add_argument('command', type=3Dstr, nargs=3D'+', help=3Dargparse.SU= PPRESS) =20 -args =3D parser.parse_args() +ARGS =3D PARSER.parse_args() =20 # Extract the needed variables from the args -command =3D args.command -top =3D args.top +COMMAND =3D ARGS.command +TOP =3D ARGS.top =20 # Insure that perf is installed -check_perf_presence =3D subprocess.run(["which", "perf"], - stdout=3Dsubprocess.DEVNULL) -if check_perf_presence.returncode: +CHECK_PERF_PRESENCE =3D subprocess.run(["which", "perf"], + stdout=3Dsubprocess.DEVNULL, + check=3DFalse) +if CHECK_PERF_PRESENCE.returncode: sys.exit("Please install perf before running the script!") =20 # Insure user has previllage to run perf -check_perf_executability =3D subprocess.run(["perf", "stat", "ls", "/"], +CHECK_PERF_EXECUTABILITY =3D subprocess.run(["perf", "stat", "ls", "/"], stdout=3Dsubprocess.DEVNULL, - stderr=3Dsubprocess.DEVNULL) -if check_perf_executability.returncode: - sys.exit( -""" + stderr=3Dsubprocess.DEVNULL, + check=3DFalse) +if CHECK_PERF_EXECUTABILITY.returncode: + sys.exit(""" Error: You may not have permission to collect stats. =20 @@ -85,43 +90,42 @@ To make this setting permanent, edit /etc/sysctl.conf t= oo, e.g.: kernel.perf_event_paranoid =3D -1 =20 * Alternatively, you can run this script under sudo privileges. -""" -) - -# Run perf record -perf_record =3D subprocess.run((["perf", "record", "--output=3D/tmp/perf.d= ata"] + - command), - stdout=3Dsubprocess.DEVNULL, - stderr=3Dsubprocess.PIPE) -if perf_record.returncode: - os.unlink('/tmp/perf.data') - sys.exit(perf_record.stderr.decode("utf-8")) - -# Save perf report output to /tmp/perf_report.out -with open("/tmp/perf_report.out", "w") as output: - perf_report =3D subprocess.run( - ["perf", "report", "--input=3D/tmp/perf.data", "--stdio"], - stdout=3Doutput, - stderr=3Dsubprocess.PIPE) - if perf_report.returncode: - os.unlink('/tmp/perf.data') - output.close() - os.unlink('/tmp/perf_report.out') - sys.exit(perf_report.stderr.decode("utf-8")) - -# Read the reported data to functions[] -functions =3D [] -with open("/tmp/perf_report.out", "r") as data: - # Only read lines that are not comments (comments start with #) - # Only read lines that are not empty - functions =3D [line for line in data.readlines() if line and line[0] - !=3D '#' and line[0] !=3D "\n"] - -# Limit the number of top functions to "top" -number_of_top_functions =3D top if len(functions) > top else len(functions) - -# Store the data of the top functions in top_functions[] -top_functions =3D functions[:number_of_top_functions] +""") + +# Run perf and save all intermediate files in a temporary directory +with tempfile.TemporaryDirectory() as tmpdir: + RECORD_PATH =3D os.path.join(tmpdir, "record.data") + REPORT_PATH =3D os.path.join(tmpdir, "report.txt") + + PERF_RECORD =3D subprocess.run((["perf", "record", "--output=3D"+RECOR= D_PATH] + + COMMAND), + stdout=3Dsubprocess.DEVNULL, + stderr=3Dsubprocess.PIPE, + check=3DFalse) + if PERF_RECORD.returncode: + sys.exit(PERF_RECORD.stderr.decode("utf-8")) + + with open(REPORT_PATH, "w") as output: + PERF_REPORT =3D subprocess.run( + ["perf", "report", "--input=3D"+RECORD_PATH, "--stdio"], + stdout=3Doutput, + stderr=3Dsubprocess.PIPE, + check=3DFalse) + if PERF_REPORT.returncode: + sys.exit(PERF_REPORT.stderr.decode("utf-8")) + + # Save the reported data to FUNCTIONS[] + with open(REPORT_PATH, "r") as data: + # Only read lines that are not comments (comments start with #) + # Only read lines that are not empty + FUNCTIONS =3D [line for line in data.readlines() if line and + line[0] !=3D '#' and line[0] !=3D "\n"] + +# Limit the number of top functions to "TOP" +NO_TOP_FUNCTIONS =3D TOP if len(FUNCTIONS) > TOP else len(FUNCTIONS) + +# Store the data of the top functions in TOP_FUNCTIONS[] +TOP_FUNCTIONS =3D FUNCTIONS[:NO_TOP_FUNCTIONS] =20 # Print table header print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.format('No.', @@ -134,7 +138,7 @@ print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.forma= t('No.', '-' * 25)) =20 # Print top N functions -for (index, function) in enumerate(top_functions, start=3D1): +for (index, function) in enumerate(TOP_FUNCTIONS, start=3D1): function_data =3D function.split() function_percentage =3D function_data[0] function_name =3D function_data[-1] @@ -143,7 +147,3 @@ for (index, function) in enumerate(top_functions, start= =3D1): function_percentage, function_name, function_invoker)) - -# Remove intermediate files -os.unlink('/tmp/perf.data') -os.unlink('/tmp/perf_report.out') --=20 2.17.1