From nobody Sun Nov 16 05:40:20 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=1598611396; cv=none; d=zohomail.com; s=zohoarc; b=ZvXo6gcAIc75+lr3MnZJvfMkJKdrOK5VcCO388U78J7T+6BYm5fQCd29x8TxQfMMzrrd0Pi5Ro3RhT8gL5L2pgh6l1khmv8BI+mTQBH7cCGD/pGypTLdN7IeDx3/QWxD1Ile9orhxmzJSGPrDXyieXvRiKTclcGavdGOBJ6M6PI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598611396; 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=BLJGQzaC58i7Jzawy2mcPTfB5Z7zUVTcLSsOZpQQw9o=; b=Yq3Q9oUSpQHHW72doEzXMaM9KI8GhIXkxDHn79VDSIy2RX8fss30LjXhXXoiMj5/lSrpgMW6B1ti1HW7xDQRcRn2lfjMiPlFOManoOUJJOsf51p0L1OYjzipdfP8LFtxZmk+M83Ce3w5iPr0nnh+PE0Xtu7sboYWWXaSRtNA1No= 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 1598611396522729.9845480282758; Fri, 28 Aug 2020 03:43:16 -0700 (PDT) Received: from localhost ([::1]:57154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBbql-00058u-0M for importer@patchew.org; Fri, 28 Aug 2020 06:43:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34440) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBbpH-0003To-FF for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:43 -0400 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]:39586) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kBbpE-0005gz-JQ for qemu-devel@nongnu.org; Fri, 28 Aug 2020 06:41:43 -0400 Received: by mail-wm1-x329.google.com with SMTP id b79so461882wmb.4 for ; Fri, 28 Aug 2020 03:41:40 -0700 (PDT) Received: from localhost.localdomain ([197.58.77.158]) by smtp.gmail.com with ESMTPSA id e18sm1307453wrx.50.2020.08.28.03.41.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Aug 2020 03:41:38 -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=BLJGQzaC58i7Jzawy2mcPTfB5Z7zUVTcLSsOZpQQw9o=; b=Ee1yLB37j//pyLubHmHwGE8x2jNKvKKRnrZaLj1/xd5pSgwuowx6nfn0DOwOpSopLR lGrY3IzEbRA9n29sUMWiAFi5Nj2kl0YaJ/mhdAW/uodYb0B7NNvBctZw+DZXFQ3yidzC EP9i1sM/0hQ2O7xlHkMT6RC679fmLATZv4VoQMCxTWt+zhdBTs6I14b7TXYDDG/KkRCZ 86si0/dF9yibzmS08RLtZXmlOAvo88BSy/Ly0dVn0/ALObIPfXIAO08mTXce39zOOXZf kv9JgEYhlYyp+y5XTcjCCx0XcGHyghgbGVBw4WAQTtaQYY+5HAiIs0lfR5QrPs6MPB7N CXvw== 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=BLJGQzaC58i7Jzawy2mcPTfB5Z7zUVTcLSsOZpQQw9o=; b=A7mJaKGoZVAedVWUOv2sMnSRbe+WRYr5r+gHJs3hCyDC0+PItsbn+MH7zYaac3qPgU oXzUp0EByUSSp383wDWES+vsiNquyZMwj0qQBErrwFtHis8nKRZYZOg6VplliMw+rey8 1v8zDcljO6rTiM10nM2HomWd2ry3rSz6hVcMbj5B9LoUQDaYIT9yHbzPJknThkaoSjuN CezMwJ3yhn2icmsk54U0xDTLCx2zXMnIff2WNrfSTxSEklbr8D8it5TrXi2O9GdckmS1 muKWR3W4ToEFsnOhLcpQyVO+E8Yk1MooiftFUKQVwo5U+YgZBt4UOSE7eIKtR55iZ7cV yu1Q== X-Gm-Message-State: AOAM5333ZetjEBn2uL10Mll/K7q/nQzpX04uhpuT9XH4KXcjM0STbCF0 gsvUkaXDkT+NMIoPkYA2qeJoP0JO5oQqHg== X-Google-Smtp-Source: ABdhPJxzVzFgK/pQN+NIm9cfQdPnRVmfEJwuo9hH0KmgVb+A2Odn35Uv2iFuBuHTUbF0FlSOjwCBsQ== X-Received: by 2002:a1c:e919:: with SMTP id q25mr953381wmc.123.1598611298903; Fri, 28 Aug 2020 03:41:38 -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 4/9] scripts/performance: Add list_fn_callees.py script Date: Fri, 28 Aug 2020 12:40:57 +0200 Message-Id: <20200828104102.4490-5-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::329; envelope-from=ahmedkhaledkaraman@gmail.com; helo=mail-wm1-x329.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 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 | 245 +++++++++++++++++++++++++ 1 file changed, 245 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..6aa8f6b6ca --- /dev/null +++ b/scripts/performance/list_fn_callees.py @@ -0,0 +1,245 @@ +#!/usr/bin/env python3 + +""" +Print the callees of a given list of QEMU functions. + +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 + +from typing import List, Union + + +def find_function_lines(function_name: str, + callgrind_data: List[str]) -> List[int]: + """ + 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[str]): callgrind_annotate output + + Returns: + (List[int]): List of function line numbers + """ + lines =3D [] + for (i, callgrind_datum) in enumerate(callgrind_data): + split_line =3D callgrind_datum.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: List[int], + callgrind_data: List[str]) -> List[List[Union[str, int]]]: + """ + Get all callees data for a function given its list of line numbers in + callgrind_annotate output. + + Parameters: + function_lines (List[int]): Line numbers of the function to get its ca= llees + callgrind_data (List[str]): callgrind_annotate output + + Returns: + (List[List[Union[str, int]]]):[[number_of_instructions(int), + callee_name(str), + number_of_calls(int), + source_file(str)], + ...] + """ + callees: List[List[Union[str, int]]] =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 then start execution. + + 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 + """ + + # 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, check=3DFalse) + 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, + check=3DFalse) + 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, + 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() + + # 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