From nobody Fri May 17 01:43:54 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1594896085; cv=none; d=zohomail.com; s=zohoarc; b=RAiA3N2Mb/Vq09XzxT9T+ogILwJ8hVCPEAD80PBI6yedW5Y9oIMzj+9cc6uxvRy9+LwrjQ4XvSqoL7z+SbHcqiyzQlHHz/5H4JbnJpua2qFUt1/O5+l4QLONBASE4e13I9XaA4H3J+a3w5dsqW9LmLIY58y50Ul9aRhIzUs2FeA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594896085; 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=BLHxOCRJ+LZvJUFw52do8TbtBNrpQqvhUB8Ij75kU6M=; b=ilRqivdueWUFIeDHiWNRhgO/vVmlFzTBXZDsF1XpH4zQnlAQpHEUCEAPsYh+E3JRofDmlHzilBG+2PjZko28qdO2i9wp4TAkdBM/emG/a0gtbeIH+YON8+qPKlqLEPeYouU2uRoYYJppkMSP+fgpIrAl8x7dKzwoB4XK1gkS+dY= 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 1594896085745829.2197560817361; Thu, 16 Jul 2020 03:41:25 -0700 (PDT) Received: from localhost ([::1]:60058 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jw1KO-0007dV-Cu for importer@patchew.org; Thu, 16 Jul 2020 06:41:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33632) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jw1JI-0006eo-H9 for qemu-devel@nongnu.org; Thu, 16 Jul 2020 06:40:16 -0400 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]:44512) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jw1JF-0003My-Tg for qemu-devel@nongnu.org; Thu, 16 Jul 2020 06:40:16 -0400 Received: by mail-ed1-x541.google.com with SMTP id by13so4380711edb.11 for ; Thu, 16 Jul 2020 03:40:13 -0700 (PDT) Received: from localhost.localdomain ([197.58.201.14]) by smtp.gmail.com with ESMTPSA id w8sm4707892ejb.10.2020.07.16.03.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2020 03:40:11 -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=BLHxOCRJ+LZvJUFw52do8TbtBNrpQqvhUB8Ij75kU6M=; b=l+Yx74g87rpxCtZV10gqmG8A820LV1KGyYgMswvO8PAwUEjCd3aebxWmNQpdEh4oTS PUbeBtH8gcJrXUWxWpoWGXVf6zqONu8Up6agUzujJzf4uvJqVVslw3zO7m5rZyGvlTvN 1yKQ0NWKVOy9PRWWGVJqTZpyy2uwdjFgWU7axMOh4ec9bCjtTORBiIMrIwN1WJKkpKVM nRM7qD0tnsvhLO6ngrXPXuMoqhP0BMNVNitScozEn0p5G4gdSmq+OWQd23FjRJMz3GQ0 koEaYNAakYflizmzEoUvSzZNQe2NUsAAgboKSWbxfaRXMD49Kvl3PJpsUbL7R8vbLxEN oNUQ== 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=BLHxOCRJ+LZvJUFw52do8TbtBNrpQqvhUB8Ij75kU6M=; b=DSpw1llO8qlRLKi0azvSs5bRg3A7je9a0Y4A7pfsa1MtbnWMHu3aE2y+rSLNvbViRR zUf7RivchQLkpAsz56jx+CcR9YHOFuaNbR6fpwNfUk13uGeebjWOZaHQEsDLZtcFY1n1 o/ZQKHnDfeA7jjKFOrf/D6UKScOuxiJT9cigEegfSTXboRkn5YK4QQLmMakQj4Z8P0tX W61zwVLeAJMsW1smsP/xFR7TRSDm7XuWVJsjC7wQwwmxLHYL587EEZtO7NCiU5yEZHyy XxjRT0Gl6eEexbNcO3nxFRG+nHLbqQwlgIZ7JNWglvZw1C6fn4Rk1P8H58XxkdhG9E48 Gp5w== X-Gm-Message-State: AOAM533pTGEUoEodnzmbjHX9Vv5HNolOTq6UeZFcqAQ1QUojCHjJjrIV Yqx/QLNG2P1MoBHCe7Z80E/t7spw X-Google-Smtp-Source: ABdhPJzK7DdqOa0ev61scA8mKGjBfil7gt8x5GmvU/Zb4kqtPCzSEW1m8DUg2aLN+gBwpAi1an+NvA== X-Received: by 2002:a05:6402:363:: with SMTP id s3mr3756560edw.238.1594896011630; Thu, 16 Jul 2020 03:40:11 -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, rth@twiddle.net, ehabkost@redhat.com, crosa@redhat.com Subject: [PATCH v2 1/2] scripts/performance: Add list_fn_callees.py script Date: Thu, 16 Jul 2020 12:39:20 +0200 Message-Id: <20200716103921.6605-2-ahmedkhaledkaraman@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200716103921.6605-1-ahmedkhaledkaraman@gmail.com> References: <20200716103921.6605-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::541; envelope-from=ahmedkhaledkaraman@gmail.com; helo=mail-ed1-x541.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, URIBL_BLOCKED=0.001 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" Python script that prints the callees of a given list of QEMU functions. Syntax: list_fn_callees.py [-h] -f FUNCTION [FUNCTION ...] -- \ [] \ [] [-h] - Print the script arguments help message. -f FUNCTION [FUNCTION ...] - List of function names Example of usage: list_fn_callees.py -f helper_float_sub_d helper_float_mul_d -- \ qemu-mips coulomb_double-mips -n10 Example output: Total number of instructions: 108,952,851 Callees of helper_float_sub_d: No. Instructions Percentage Calls Ins/Call Function Name Source File --- ------------ ---------- ------ -------- ------------- --------------- 1 153,160 0.141% 1,305 117 float64_sub /fpu/softf= loat.c Callees of helper_float_mul_d: No. Instructions Percentage Calls Ins/Call Function Name Source File --- ------------ ---------- ------ -------- ------------- --------------- 1 131,137 0.120% 1,014 129 float64_mul /fpu/softf= loat.c Signed-off-by: Ahmed Karaman --- scripts/performance/list_fn_callees.py | 228 +++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100755 scripts/performance/list_fn_callees.py diff --git a/scripts/performance/list_fn_callees.py b/scripts/performance/l= ist_fn_callees.py new file mode 100755 index 0000000000..f0ec5c8e81 --- /dev/null +++ b/scripts/performance/list_fn_callees.py @@ -0,0 +1,228 @@ +#!/usr/bin/env python3 + +# Print the callees of a given list of QEMU functions. +# +# Syntax: +# list_fn_callees.py [-h] -f FUNCTION [FUNCTION ...] -- \ +# [] \ +# [] +# +# [-h] - Print the script arguments help message. +# -f FUNCTION [FUNCTION ...] - List of function names +# +# Example of usage: +# list_fn_callees.py -f helper_float_sub_d helper_float_mul_d -- \ +# qemu-mips coulomb_double-mips +# +# 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 . + +import argparse +import os +import subprocess +import sys +import tempfile + + +def find_function_lines(function_name, callgrind_data): + """ + Search for the line with the function name in the + callgrind_annotate output when ran using --tre=3Dcalling. + All the function callees should be listed after that line. + + Parameters: + function_name (string): The desired function name to print its callees + callgrind_data (list): callgrind_annotate output + + Returns: + (list): List of function line numbers + """ + lines =3D [] + for i in range(len(callgrind_data)): + split_line =3D callgrind_data[i].split() + if len(split_line) > 2 and \ + split_line[1] =3D=3D "*" and \ + split_line[2].split(":")[-1] =3D=3D function_name: + # Function might be in the callgrind_annotate output more than + # once, so don't break after finding an instance + if callgrind_data[i + 1] !=3D "\n": + # Only append the line number if the found instance has + # callees + lines.append(i) + return lines + + +def get_function_calles(function_lines, callgrind_data): + """ + Get all callees data for a function given its list of line numbers in + callgrind_annotate output. + + Parameters: + function_lines (list): Line numbers of the function to get its callees + callgrind_data (list): callgrind_annotate output + + Returns: + (list):[[number_of_instructions(int), callee_name(str), + number_of_calls(int), source_file(str)]] + """ + callees =3D [] + for function_line in function_lines: + next_callee =3D function_line + 1 + while (callgrind_data[next_callee] !=3D "\n"): + split_line =3D callgrind_data[next_callee].split() + number_of_instructions =3D int(split_line[0].replace(",", "")) + source_file =3D split_line[2].split(":")[0] + callee_name =3D split_line[2].split(":")[1] + number_of_calls =3D int(split_line[3][1:-2]) + callees.append([number_of_instructions, callee_name, + number_of_calls, source_file]) + next_callee +=3D 1 + return sorted(callees, reverse=3DTrue) + + +def main(): + # Parse the command line arguments + parser =3D argparse.ArgumentParser( + usage=3D"list_fn_callees.py [-h] -f FUNCTION [FUNCTION ...] -- " + " [] " + " []") + + parser.add_argument("-f", dest=3D"function", type=3Dstr, + nargs=3D"+", required=3DTrue, + help=3D"list of function names to print their call= ees") + + parser.add_argument("command", type=3Dstr, nargs=3D"+", help=3Dargpars= e.SUPPRESS) + + args =3D parser.parse_args() + + # Extract the needed variables from the args + command =3D args.command + function_names =3D args.function + + # Insure that valgrind is installed + check_valgrind =3D subprocess.run( + ["which", "valgrind"], stdout=3Dsubprocess.DEVNULL) + if check_valgrind.returncode: + sys.exit("Please install valgrind before running the script.") + + # Save all intermediate files in a temporary directory + with tempfile.TemporaryDirectory() as tmpdirname: + # callgrind output file path + data_path =3D os.path.join(tmpdirname, "callgrind.data") + # callgrind_annotate output file path + annotate_out_path =3D os.path.join(tmpdirname, "callgrind_annotate= .out") + + # Run callgrind + callgrind =3D subprocess.run((["valgrind", + "--tool=3Dcallgrind", + "--callgrind-out-file=3D" + data_path] + + command), + stdout=3Dsubprocess.DEVNULL, + stderr=3Dsubprocess.PIPE) + if callgrind.returncode: + sys.exit(callgrind.stderr.decode("utf-8")) + + # Save callgrind_annotate output + with open(annotate_out_path, "w") as output: + callgrind_annotate =3D subprocess.run( + ["callgrind_annotate", data_path, + "--threshold=3D100", "--tree=3Dcalling"], + stdout=3Doutput, + stderr=3Dsubprocess.PIPE) + 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() + + # Line number with the total number of instructions + total_instructions_line_number =3D 20 + # Get the total number of instructions + total_instructions_line_data =3D \ + callgrind_data[total_instructions_line_number] + total_instructions =3D total_instructions_line_data.split()[0] + + print("Total number of instructions: {}\n".format(total_instructio= ns)) + + # Remove commas and convert to int + total_instructions =3D int(total_instructions.replace(",", "")) + + for function_name in function_names: + # Line numbers with the desired function + function_lines =3D find_function_lines(function_name, callgrin= d_data) + + if len(function_lines) =3D=3D 0: + print("Couldn't locate function: {}.\n".format( + function_name)) + continue + + # Get function callees + function_callees =3D get_function_calles( + function_lines, callgrind_data) + + print("Callees of {}:\n".format(function_name)) + + # Print table header + print("{:>4} {:>15} {:>10} {:>15} {:>10} {:<25} {}". + format( + "No.", + "Instructions", + "Percentage", + "Calls", + "Ins/Call", + "Function Name", + "Source File") + ) + + print("{:>4} {:>15} {:>10} {:>15} {:>10} {:<25} {}". + format( + "-" * 4, + "-" * 15, + "-" * 10, + "-" * 15, + "-" * 10, + "-" * 25, + "-" * 30) + ) + + for (index, callee) in enumerate(function_callees, start=3D1): + instructions =3D callee[0] + percentage =3D (callee[0] / total_instructions) * 100 + calls =3D callee[2] + instruction_per_call =3D int(callee[0] / callee[2]) + function_name =3D callee[1] + source_file =3D callee[3] + # Print extracted data + print("{:>4} {:>15} {:>9.3f}% {:>15} {:>10} {:<25} {= }". + format( + index, + format(instructions, ","), + round(percentage, 3), + format(calls, ","), + format(instruction_per_call, ","), + function_name, + source_file) + ) + + print("\n") + + +if __name__ =3D=3D "__main__": + main() --=20 2.17.1 From nobody Fri May 17 01:43:54 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1594896126; cv=none; d=zohomail.com; s=zohoarc; b=Au86kXzL97MjxdsmCB1564Eq6dioDayjW7Wr8dG9GXIzNyQ8U12zsPOTbFYKW0PXOaF/dGEdwXz14fFy6ST8X446xR49lmnlCJtGYFSrj+fYayq5LHpHoH6E4ZjPwwFRYtEhntUiraIEZYQPpba3sEfJTAWEo/83b00UeLoyKNU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594896126; 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=CkBQjPpokRRSOD3d3LoQgCJvGglsJwGJes84iBBLdZI=; b=QhZ6WB4JTDccr2fbDGnQVDC2q4PuiUtc89ijGhTur4lBpt+Pvxd1aRCZCNxjG09xVtrqV3o0hC14G9xj4i9vqsBb/LTItzzBsfLUgjZmtrp4jcOTx57dYdidkF6RyZYSC/Nyv27ViAoGXnH60hcVK7ZMjWrWrhWJy75wXhXde+w= 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 1594896126569969.4669013416064; Thu, 16 Jul 2020 03:42:06 -0700 (PDT) Received: from localhost ([::1]:33660 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jw1L3-0008NQ-4F for importer@patchew.org; Thu, 16 Jul 2020 06:42:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33866) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jw1Jt-0007LC-64 for qemu-devel@nongnu.org; Thu, 16 Jul 2020 06:40:53 -0400 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]:34023) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jw1Jp-0003iu-B7 for qemu-devel@nongnu.org; Thu, 16 Jul 2020 06:40:52 -0400 Received: by mail-ed1-x543.google.com with SMTP id a8so4423495edy.1 for ; Thu, 16 Jul 2020 03:40:48 -0700 (PDT) Received: from localhost.localdomain ([197.58.201.14]) by smtp.gmail.com with ESMTPSA id w8sm4707892ejb.10.2020.07.16.03.40.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jul 2020 03:40:47 -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=CkBQjPpokRRSOD3d3LoQgCJvGglsJwGJes84iBBLdZI=; b=iVIxF69wwyaDb3mTXmEE9mxKCjNe2ATDQQxGGXFWeZXDdZMzGpC6LsH9PneEGcGbv4 6jqQcqXXuY7PhY9nE5z4TbnXzvZxjucgdfEsE4001IVivKrzhigBDKjwoMUrRG0KIvSH a0cO46KvvinfL8395nGmp0PNg4IT/km6SV3F0SoYhP2yFJqMxxVPFiTdxbFGs7PjZQpE 84OcyKvzldXIvKpwHzunCXFS+WObM6XTl975xE9nfDb0YM+rKWe5lqls6U9tvJQjUgDS VkC+d74iKQ++BwDQijUE6MrWJRoV0BRmryJyfMbyAj3ukkUrr1G1gx88gRfVUtnlSrhT sYEw== 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=CkBQjPpokRRSOD3d3LoQgCJvGglsJwGJes84iBBLdZI=; b=lfeyooA7LVWfK5b1qpRSKiv0axVV5mSmHCvb+LiCcOXwqQaVQrs+UGR1iEQikV/Q4d OJ7/r7NqswzgtdK7jwcRMjNl6SdXCKpmy062kSa1bQ0ikJri6szWOk3X9omtBh9NUqSM C5tt09RBQOQCOrnP4ULUcauDvtluCtbTNTaesn5iNfNJhFx3aYwv5qrYFOKdVn9PzFAk 0cEdxCaqe2OvSJqXoBbsleCdp8R1zqqv2Ag3/OYQM62W1HySuQMoYoBRcK56rg6ii5Px jgQefuk6ewM713i9RMef2/sMK5K2DqlFCunu5T+oxSwonclCMlzBwvKplVcyYOj89ZVE I9Ng== X-Gm-Message-State: AOAM531utR/QCtrm9PnRzO+OrO94TLJXs9T71Y+B/g2xyu1CvxuTrbCA rLn98CNY/OxZUJkhy56hLuPH8qM7 X-Google-Smtp-Source: ABdhPJw4GKx0xDZ1St+uhszMmzdarSVT4/s4CF7Zf52qSkOK87I5KVcoJwXRwaAMsuR70uKnGc4iwg== X-Received: by 2002:a50:e801:: with SMTP id e1mr3736751edn.251.1594896047535; Thu, 16 Jul 2020 03:40:47 -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, rth@twiddle.net, ehabkost@redhat.com, crosa@redhat.com Subject: [PATCH v2 2/2] scripts/performance: Add list_helpers.py script Date: Thu, 16 Jul 2020 12:39:21 +0200 Message-Id: <20200716103921.6605-3-ahmedkhaledkaraman@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200716103921.6605-1-ahmedkhaledkaraman@gmail.com> References: <20200716103921.6605-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::543; envelope-from=ahmedkhaledkaraman@gmail.com; helo=mail-ed1-x543.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, URIBL_BLOCKED=0.001 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" Python script that prints executed helpers of a QEMU invocation. Syntax: list_helpers.py [-h] -- \ [] \ [] [-h] - Print the script arguments help message. Example of usage: list_helpers.py -- qemu-mips coulomb_double-mips -n10 Example output: Total number of instructions: 108,933,695 Executed QEMU Helpers: No. Ins Percent Calls Ins/Call Helper Name Source File --- ------- ------- ------ -------- -------------------- --------------- 1 183,021 0.168% 1,305 140 helper_float_sub_d /target/= mips/fpu_helper.c 2 177,111 0.163% 770 230 helper_float_madd_d /target/= mips/fpu_helper.c 3 171,537 0.157% 1,014 169 helper_float_mul_d /target/= mips/fpu_helper.c 4 157,298 0.144% 2,443 64 helper_lookup_tb_ptr /accel/t= cg/tcg-runtime.c 5 138,123 0.127% 897 153 helper_float_add_d /target/= mips/fpu_helper.c 6 47,083 0.043% 207 227 helper_float_msub_d /target/= mips/fpu_helper.c 7 24,062 0.022% 487 49 helper_cmp_d_lt /target/= mips/fpu_helper.c 8 22,910 0.021% 150 152 helper_float_div_d /target/= mips/fpu_helper.c 9 15,497 0.014% 321 48 helper_cmp_d_eq /target/= mips/fpu_helper.c 10 9,100 0.008% 52 175 helper_float_trunc_w_d /target/= mips/fpu_helper.c 11 7,059 0.006% 10 705 helper_float_sqrt_d /target/= mips/fpu_helper.c 12 3,000 0.003% 40 75 helper_cmp_d_ule /target/= mips/fpu_helper.c 13 2,720 0.002% 20 136 helper_float_cvtd_w /target/= mips/fpu_helper.c 14 2,477 0.002% 27 91 helper_swl /target/= mips/op_helper.c 15 2,000 0.002% 40 50 helper_cmp_d_le /target/= mips/fpu_helper.c 16 1,800 0.002% 40 45 helper_cmp_d_un /target/= mips/fpu_helper.c 17 1,164 0.001% 12 97 helper_raise_exception_ /target/= mips/op_helper.c 18 720 0.001% 10 72 helper_cmp_d_ult /target/= mips/fpu_helper.c 19 560 0.001% 140 4 helper_cfc1 /target/= mips/fpu_helper.c Signed-off-by: Ahmed Karaman --- scripts/performance/list_helpers.py | 207 ++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100755 scripts/performance/list_helpers.py diff --git a/scripts/performance/list_helpers.py b/scripts/performance/list= _helpers.py new file mode 100755 index 0000000000..a97c7ed4fe --- /dev/null +++ b/scripts/performance/list_helpers.py @@ -0,0 +1,207 @@ +#!/usr/bin/env python3 + +# Print the executed helpers of a QEMU invocation. +# +# Syntax: +# list_helpers.py [-h] -- \ +# [] \ +# [] +# +# [-h] - Print the script arguments help message. +# +# Example of usage: +# list_helpers.py -- qemu-mips coulomb_double-mips +# +# 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 . + +import argparse +import os +import subprocess +import sys +import tempfile + + +def find_JIT_line(callgrind_data): + """ + Search for the line with the JIT call in the callgrind_annotate + output when ran using --tre=3Dcalling. + All the helpers should be listed after that line. + + Parameters: + callgrind_data (list): callgrind_annotate output + + Returns: + (int): Line number of JIT call + """ + line =3D -1 + for i in range(len(callgrind_data)): + split_line =3D callgrind_data[i].split() + if len(split_line) > 2 and \ + split_line[1] =3D=3D "*" and \ + split_line[-1] =3D=3D "[???]": + line =3D i + break + return line + + +def get_helpers(JIT_line, callgrind_data): + """ + Get all helpers data given the line number of the JIT call. + + Parameters: + JIT_line (int): Line number of the JIT call + callgrind_data (list): callgrind_annotate output + + Returns: + (list):[[number_of_instructions(int), helper_name(str), + number_of_calls(int), source_file(str)]] + """ + helpers =3D [] + next_helper =3D JIT_line + 1 + while (callgrind_data[next_helper] !=3D "\n"): + split_line =3D callgrind_data[next_helper].split() + number_of_instructions =3D int(split_line[0].replace(",", "")) + source_file =3D split_line[2].split(":")[0] + callee_name =3D split_line[2].split(":")[1] + number_of_calls =3D int(split_line[3][1:-2]) + helpers.append([number_of_instructions, callee_name, + number_of_calls, source_file]) + next_helper +=3D 1 + return sorted(helpers, reverse=3DTrue) + + +def main(): + # Parse the command line arguments + parser =3D argparse.ArgumentParser( + usage=3D"list_helpers.py [-h] -- " + " [] " + " []") + + parser.add_argument("command", type=3Dstr, nargs=3D"+", help=3Dargpars= e.SUPPRESS) + + args =3D parser.parse_args() + + # Extract the needed variables from the args + command =3D args.command + + # Insure that valgrind is installed + check_valgrind =3D subprocess.run( + ["which", "valgrind"], stdout=3Dsubprocess.DEVNULL) + if check_valgrind.returncode: + sys.exit("Please install valgrind before running the script.") + + # Save all intermediate files in a temporary directory + with tempfile.TemporaryDirectory() as tmpdirname: + # callgrind output file path + data_path =3D os.path.join(tmpdirname, "callgrind.data") + # callgrind_annotate output file path + annotate_out_path =3D os.path.join(tmpdirname, "callgrind_annotate= .out") + + # Run callgrind + callgrind =3D subprocess.run((["valgrind", + "--tool=3Dcallgrind", + "--callgrind-out-file=3D" + data_path] + + command), + stdout=3Dsubprocess.DEVNULL, + stderr=3Dsubprocess.PIPE) + if callgrind.returncode: + sys.exit(callgrind.stderr.decode("utf-8")) + + # Save callgrind_annotate output + with open(annotate_out_path, "w") as output: + callgrind_annotate =3D subprocess.run( + ["callgrind_annotate", data_path, + "--threshold=3D100", "--tree=3Dcalling"], + stdout=3Doutput, + stderr=3Dsubprocess.PIPE) + 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() + + # Line number with the total number of instructions + total_instructions_line_number =3D 20 + # Get the total number of instructions + total_instructions_line_data =3D \ + callgrind_data[total_instructions_line_number] + total_instructions =3D total_instructions_line_data.split()[0] + + print("Total number of instructions: {}\n".format(total_instructio= ns)) + + # Remove commas and convert to int + total_instructions =3D int(total_instructions.replace(",", "")) + + # Line number with the JIT call + JIT_line =3D find_JIT_line(callgrind_data) + if JIT_line =3D=3D -1: + sys.exit("Couldn't locate the JIT call ... Exiting") + + # Get helpers + helpers =3D get_helpers(JIT_line, callgrind_data) + + print("Executed QEMU Helpers:\n") + + # Print table header + print("{:>4} {:>15} {:>10} {:>15} {:>10} {:<25} {}". + format( + "No.", + "Instructions", + "Percentage", + "Calls", + "Ins/Call", + "Helper Name", + "Source File") + ) + + print("{:>4} {:>15} {:>10} {:>15} {:>10} {:<25} {}". + format( + "-" * 4, + "-" * 15, + "-" * 10, + "-" * 15, + "-" * 10, + "-" * 25, + "-" * 30) + ) + + for (index, callee) in enumerate(helpers, start=3D1): + instructions =3D callee[0] + percentage =3D (callee[0] / total_instructions) * 100 + calls =3D callee[2] + instruction_per_call =3D int(callee[0] / callee[2]) + helper_name =3D callee[1] + source_file =3D callee[3] + # Print extracted data + print("{:>4} {:>15} {:>9.3f}% {:>15} {:>10} {:<25} {}". + format( + index, + format(instructions, ","), + round(percentage, 3), + format(calls, ","), + format(instruction_per_call, ","), + helper_name, + source_file) + ) + + +if __name__ =3D=3D "__main__": + main() --=20 2.17.1