From nobody Sun Nov 16 05:40:21 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=1598611472; cv=none; d=zohomail.com; s=zohoarc; b=TuKgDXGV4t613J2ZcRPBsRaweEwHj58dsjQJS2b3YjDazfz53Bc5bqVGT1IOIXvAtMjMIMJCZ883+QRrpUUzdTIk5Jl57bMQrSHBjXshJkgFNoTfT7qsd8qePwfhgOhgQfGfafnR7F1OuD+OBYzYoiEGp369BVBcW2ruKeJyhqo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598611472; 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=6VnUOaw47q1sBFWEPDkKi8Rl+L6HJXn0J6GhoPyLYWM=; b=Gak5QKLU7uGR+tM0Oyr4i0WdjLGIheRSL9C4V47sPhIMNosEYnWOKXkQl9NjXulVksHoLmVlDqu6kR2GX0A4DWSHZ0q2mE0BuT/7yEkOD7TKOYqeBsmqwEtw85lonDMmjZL+MQDsG/8ue9nWw1rqGGXw91QLHnElMLXtD9hlN1w= 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 1598611472611251.12486478976257; Fri, 28 Aug 2020 03:44:32 -0700 (PDT) Received: from localhost ([::1]:36646 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBbrz-0008Gm-AM for importer@patchew.org; Fri, 28 Aug 2020 06:44:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34414) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBbpD-0003Pe-Ia for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:39 -0400 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:45770) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBbpA-0005gS-RI for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:39 -0400 Received: by mail-wr1-x443.google.com with SMTP id h15so830654wrt.12 for ; Fri, 28 Aug 2020 03:41:36 -0700 (PDT) Received: from localhost.localdomain ([197.58.77.158]) by smtp.gmail.com with ESMTPSA id e18sm1307453wrx.50.2020.08.28.03.41.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 03:41:34 -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=6VnUOaw47q1sBFWEPDkKi8Rl+L6HJXn0J6GhoPyLYWM=; b=HK/+cklW0orMXNl4Is5lsX4XkmBhx776gGavdwJf8IBs89t4vZJzXPAaCtxc6yKIvc RzQB1Qus6S9Bn6Mzp/bHtBKVuU99Vcl4/E54OUhOshOmMFSRImyfLeEu3yKgjaZNuKZr Q/R91V2p8iDPXFPeLzOrwTJzcHoK+Rwdtb5Izj+iATpzclxbu7oMxWbdaKr5AbmjWolH WQEPE/DkQ7BvxFwG2cz0hL0+6qnx3otbnGYt7t0mxUeUiEXYIxZeDOGK0qs9WxCdaBSo ZNaz31UqFtszJ4YvYS+YHjq3NuI1ipWTa24bHx8MYl6pALyPRYiokQMv3shLjDT4F9bH NkiQ== 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=6VnUOaw47q1sBFWEPDkKi8Rl+L6HJXn0J6GhoPyLYWM=; b=NriiN+nz4yZkfD74HZulD9tIomLmngDWDA8gazf8+8/WJHHvFU5zcT5gGhzY5GBOQQ E+WlbiNH+y2SDEIjCdO7wq/KE26t8X/PZaUepCvMWKRVxyxifYmXvJrfQMn89RuDPkIJ sYzFCFV+adVzh8kg4R26iZWSpisLfzwYbeoiBsUvP3DdYwcYKzywaia/4Tu+yLTK7a+/ 11BbMJiuu9+c8iIjVEX4LUBEhEg8bL/fSEfxWtOJtIVqfld2ausPS8moJRsmx93PLtNC n7liJJuATdjssXl9WRFwM+zup86OT3AXSAmeKCOe59p1sI5+ACWWMErExjyzkKAKXZRY iTdA== X-Gm-Message-State: AOAM533QXLo3R0Uz/AxpcfakWt4+/QsIVEYutCI00pdz4sFd8rW4P2Rl rZ3xit9u9SaskK8XOYJn6gdLKIUiLPu4/A== X-Google-Smtp-Source: ABdhPJzJsAjybpcBhmfxWDs+uG3yuDlTJWs5Nnd98OPlyvZdKNUSixe42FeYCDweCVGzHmXJt3aKPA== X-Received: by 2002:adf:9d44:: with SMTP id o4mr864841wre.99.1598611294899; Fri, 28 Aug 2020 03:41:34 -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 2/9] scripts/performance: Refactor topN_callgrind.py Date: Fri, 28 Aug 2020 12:40:55 +0200 Message-Id: <20200828104102.4490-3-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::443; envelope-from=ahmedkhaledkaraman@gmail.com; helo=mail-wr1-x443.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_callgrind.py | 169 +++++++++++++------------- 1 file changed, 87 insertions(+), 82 deletions(-) diff --git a/scripts/performance/topN_callgrind.py b/scripts/performance/to= pN_callgrind.py index 67c59197af..f8a554f393 100755 --- a/scripts/performance/topN_callgrind.py +++ b/scripts/performance/topN_callgrind.py @@ -1,113 +1,122 @@ #!/usr/bin/env python3 =20 -# Print the top N most executed functions in QEMU using callgrind. -# Syntax: -# topN_callgrind.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_callgrind.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 callgrind. + +Syntax: +topN_callgrind.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_callgrind.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_callgrind.py [-h] [-n] -- ' +PARSER =3D argparse.ArgumentParser( + usage=3D'topN_callgrind.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 valgrind is installed -check_valgrind_presence =3D subprocess.run(["which", "valgrind"], - stdout=3Dsubprocess.DEVNULL) -if check_valgrind_presence.returncode: +CHECK_VALGRIND_PRESENCE =3D subprocess.run(["which", "valgrind"], + stdout=3Dsubprocess.DEVNULL, + check=3DFalse) +if CHECK_VALGRIND_PRESENCE.returncode: sys.exit("Please install valgrind before running the script!") =20 -# Run callgrind -callgrind =3D subprocess.run(( - ["valgrind", "--tool=3Dcallgrind", "--callgrind-out-file=3D/tmp/callgr= ind.data"] - + command), - stdout=3Dsubprocess.DEVNULL, - stderr=3Dsubprocess.PIPE) -if callgrind.returncode: - sys.exit(callgrind.stderr.decode("utf-8")) - -# Save callgrind_annotate output to /tmp/callgrind_annotate.out -with open("/tmp/callgrind_annotate.out", "w") as output: - callgrind_annotate =3D subprocess.run(["callgrind_annotate", - "/tmp/callgrind.data"], - stdout=3Doutput, - stderr=3Dsubprocess.PIPE) - if callgrind_annotate.returncode: - os.unlink('/tmp/callgrind.data') - output.close() - os.unlink('/tmp/callgrind_annotate.out') - sys.exit(callgrind_annotate.stderr.decode("utf-8")) - -# Read the callgrind_annotate output to callgrind_data[] -callgrind_data =3D [] -with open('/tmp/callgrind_annotate.out', 'r') as data: - callgrind_data =3D data.readlines() +# Run callgrind and save all intermediate files in a temporary directory +with tempfile.TemporaryDirectory() as tmpdir: + CALLGRIND_DATA_PATH =3D os.path.join(tmpdir, "callgrind.data") + ANNOTATE_OUT_PATH =3D os.path.join(tmpdir, "callgrind_annotate.out") + + # Run callgrind + CALLGRIND =3D subprocess.run((["valgrind", + "--tool=3Dcallgrind", + "--callgrind-out-file=3D"+CALLGRIND_DATA_= PATH] + + COMMAND), + stdout=3Dsubprocess.DEVNULL, + stderr=3Dsubprocess.PIPE, + check=3DFalse) + if CALLGRIND.returncode: + sys.exit(CALLGRIND.stderr.decode("utf-8")) + + with open(ANNOTATE_OUT_PATH, "w") as output: + CALLGRIND_ANNOTATE =3D subprocess.run(["callgrind_annotate", + CALLGRIND_DATA_PATH], + stdout=3Doutput, + stderr=3Dsubprocess.PIPE, + check=3DFalse) + if CALLGRIND_ANNOTATE.returncode: + sys.exit(CALLGRIND_ANNOTATE.stderr.decode("utf-8")) + + # Read the callgrind_annotate output to CALLGRIND_DATA[] + CALLGRIND_DATA =3D [] + with open(ANNOTATE_OUT_PATH, 'r') as data: + CALLGRIND_DATA =3D data.readlines() =20 # Line number with the total number of instructions -total_instructions_line_number =3D 20 +TOTAL_INSTRUCTIONS_LINE_NO =3D 20 =20 # Get the total number of instructions -total_instructions_line_data =3D callgrind_data[total_instructions_line_nu= mber] -total_number_of_instructions =3D total_instructions_line_data.split(' ')[0] -total_number_of_instructions =3D int( - total_number_of_instructions.replace(',', '')) +TOTAL_INSTRUCTIONS_LINE_DATA =3D CALLGRIND_DATA[TOTAL_INSTRUCTIONS_LINE_NO] +TOTAL_NUMBER_OF_INSTRUCTIONS =3D TOTAL_INSTRUCTIONS_LINE_DATA.split(' ')[0] +TOTAL_NUMBER_OF_INSTRUCTIONS =3D int( + TOTAL_NUMBER_OF_INSTRUCTIONS.replace(',', '')) =20 # Line number with the top function -first_func_line =3D 25 +FIRST_FUNC_LINE =3D 25 =20 # Number of functions recorded by callgrind, last two lines are always emp= ty -number_of_functions =3D len(callgrind_data) - first_func_line - 2 +NUMBER_OF_FUNCTIONS =3D len(CALLGRIND_DATA) - FIRST_FUNC_LINE - 2 =20 # Limit the number of top functions to "top" -number_of_top_functions =3D (top if number_of_functions > - top else number_of_functions) +NUMBER_OF_TOP_FUNCTIONS =3D (TOP if NUMBER_OF_FUNCTIONS > + TOP else NUMBER_OF_FUNCTIONS) =20 # Store the data of the top functions in top_functions[] -top_functions =3D callgrind_data[first_func_line: - first_func_line + number_of_top_functions] +TOP_FUNCTIONS =3D CALLGRIND_DATA[FIRST_FUNC_LINE: + FIRST_FUNC_LINE + NUMBER_OF_TOP_FUNCTIONS] =20 # Print table header print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.format('No.', @@ -121,12 +130,12 @@ print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.for= mat('No.', )) =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() # Calculate function percentage function_instructions =3D float(function_data[0].replace(',', '')) function_percentage =3D (function_instructions / - total_number_of_instructions)*100 + TOTAL_NUMBER_OF_INSTRUCTIONS)*100 # Get function name and source files path function_source_file, function_name =3D function_data[1].split(':') # Print extracted data @@ -134,7 +143,3 @@ for (index, function) in enumerate(top_functions, start= =3D1): round(function_percentage,= 3), function_name, function_source_file)) - -# Remove intermediate files -os.unlink('/tmp/callgrind.data') -os.unlink('/tmp/callgrind_annotate.out') --=20 2.17.1