qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Ahmed Karaman <ahmedkhaledkaraman@gmail.com>
Cc: ldoktor@redhat.com, ehabkost@redhat.com, qemu-devel@nongnu.org,
	aleksandar.qemu.devel@gmail.com, crosa@redhat.com,
	rth@twiddle.net
Subject: Re: [PATCH 3/3] scripts/performance: Add perf_top_25.py script
Date: Wed, 17 Jun 2020 13:21:06 +0100	[thread overview]
Message-ID: <871rmdyjbh.fsf@linaro.org> (raw)
In-Reply-To: <20200616231204.8850-4-ahmedkhaledkaraman@gmail.com>


Ahmed Karaman <ahmedkhaledkaraman@gmail.com> writes:

> Python script that prints the top 25 most executed functions in QEMU
> using perf.
>
> Signed-off-by: Ahmed Karaman <ahmedkhaledkaraman@gmail.com>
> ---
>  scripts/performance/perf_top_25.py | 82 ++++++++++++++++++++++++++++++
>  1 file changed, 82 insertions(+)
>  create mode 100644 scripts/performance/perf_top_25.py
>
> diff --git a/scripts/performance/perf_top_25.py b/scripts/performance/perf_top_25.py
> new file mode 100644
> index 0000000000..eaa8cce3c3
> --- /dev/null
> +++ b/scripts/performance/perf_top_25.py
> @@ -0,0 +1,82 @@
> +#!/usr/bin/env python3
> +
> +#  Print the top 25 most executed functions in QEMU using perf.
> +#  Example Usage:
> +#  perf_top_25.py <qemu-build>/x86_64-linux-user/qemu-x86_64 executable
> +#
> +#  This file is a part of the project "TCG Continuous Benchmarking".
> +#
> +#  Copyright (C) 2020  Ahmed Karaman <ahmedkhaledkaraman@gmail.com>
> +#  Copyright (C) 2020  Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
> +#
> +#  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 <https://www.gnu.org/licenses/>.
> +
> +import os
> +import sys
> +
> +# Ensure sufficient arguments
> +if len(sys.argv) < 3:
> +    print('Insufficient Script Arguments!')
> +    sys.exit(1)
> +
> +# Get the qemu path and the executable + its arguments
> +(qemu_path, executable) = (sys.argv[1], ' '.join(sys.argv[2:]))

Same comments as other script.

> +
> +# Run perf repcord and report
> +os.system('sudo perf record {} {} 2> /dev/null \
> +            && sudo perf report --stdio > tmp.perf.data'
> +          .format(qemu_path, executable))

Why sudo?

Also redirecting just stderr? why?

I think you could separate the steps (as well as use the subprocess
api).

> +
> +# Line number with the top function
> +first_func_line = 11
> +
> +# Perf report output
> +perf_data = []
> +
> +# Open perf report output and store it in perf_data
> +with open('tmp.perf.data', 'r') as data:
> +    perf_data = data.readlines()
> +
> +# Number of functions recorded by perf
> +number_of_functions = len(perf_data) - first_func_line
> +
> +# Limit the number of top functions to 25
> +number_of_top_functions = (25 if number_of_functions >
> +                           25 else number_of_functions)
> +
> +# Store the data of the top functions in top_functions[]
> +top_functions = perf_data[first_func_line:first_func_line
> +                          + number_of_top_functions]
> +
> +# Print information headers
> +print('{:>4}  {:>10}  {:<25}  {}\n{}  {}  {}  {}'.format('No.',
> +                                                         'Percentage',
> +                                                         'Name',
> +                                                         'Caller',
> +                                                         '-' * 4,
> +                                                         '-' * 10,
> +                                                         '-' * 25,
> +                                                         '-' * 25,
> +                                                         ))
> +
> +# Print top 25 functions
> +for (index, function) in enumerate(top_functions, start=1):
> +    function_data = function.split()
> +    print('{:>4}  {:>10}  {:<25}  {}'.format(index,
> +                                             function_data[0],
> +                                             function_data[-1],
> +                                             ' '.join(function_data[2:-2])))
> +
> +# Remove intermediate files
> +os.system('sudo rm perf.data tmp.perf.data')

Again os.unlink()

-- 
Alex Bennée


  parent reply	other threads:[~2020-06-17 12:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-16 23:12 [PATCH 0/3] Add Scripts for Finding Top 25 Executed Functions Ahmed Karaman
2020-06-16 23:12 ` [PATCH 1/3] MAINTAINERS: Add 'Miscellaneous' section Ahmed Karaman
2020-06-17  5:35   ` Aleksandar Markovic
2020-06-17  5:51   ` Aleksandar Markovic
2020-06-17 12:01   ` Alex Bennée
2020-06-16 23:12 ` [PATCH 2/3] scripts/performance: Add callgrind_top_25.py script Ahmed Karaman
2020-06-17  5:42   ` Aleksandar Markovic
2020-06-17 12:16   ` Alex Bennée
2020-06-17 16:08     ` Ahmed Karaman
2020-06-16 23:12 ` [PATCH 3/3] scripts/performance: Add perf_top_25.py script Ahmed Karaman
2020-06-17  5:56   ` Aleksandar Markovic
2020-06-17 12:21   ` Alex Bennée [this message]
2020-06-17 16:15     ` Ahmed Karaman
2020-06-17 17:35       ` Alex Bennée
2020-06-17 18:21         ` Ahmed Karaman
2020-06-18 15:07           ` Aleksandar Markovic
2020-06-16 23:35 ` [PATCH 0/3] Add Scripts for Finding Top 25 Executed Functions no-reply
2020-06-17 13:53 ` Eric Blake
2020-06-17 15:34   ` Alex Bennée
2020-06-17 16:16     ` Aleksandar Markovic
2020-06-17 17:42       ` Alex Bennée
2020-06-17 16:16   ` Ahmed Karaman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871rmdyjbh.fsf@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=ahmedkhaledkaraman@gmail.com \
    --cc=aleksandar.qemu.devel@gmail.com \
    --cc=crosa@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=ldoktor@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).