2
0

topN_perf.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. # Print the top N most executed functions in QEMU using perf.
  3. # Syntax:
  4. # topN_perf.py [-h] [-n] <number of displayed top functions> -- \
  5. # <qemu executable> [<qemu executable options>] \
  6. # <target executable> [<target executable options>]
  7. #
  8. # [-h] - Print the script arguments help message.
  9. # [-n] - Specify the number of top functions to print.
  10. # - If this flag is not specified, the tool defaults to 25.
  11. #
  12. # Example of usage:
  13. # topN_perf.py -n 20 -- qemu-arm coulomb_double-arm
  14. #
  15. # This file is a part of the project "TCG Continuous Benchmarking".
  16. #
  17. # Copyright (C) 2020 Ahmed Karaman <ahmedkhaledkaraman@gmail.com>
  18. # Copyright (C) 2020 Aleksandar Markovic <aleksandar.qemu.devel@gmail.com>
  19. #
  20. # This program is free software: you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation, either version 2 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  32. import argparse
  33. import os
  34. import subprocess
  35. import sys
  36. # Parse the command line arguments
  37. parser = argparse.ArgumentParser(
  38. usage='topN_perf.py [-h] [-n] <number of displayed top functions > -- '
  39. '<qemu executable> [<qemu executable options>] '
  40. '<target executable> [<target executable options>]')
  41. parser.add_argument('-n', dest='top', type=int, default=25,
  42. help='Specify the number of top functions to print.')
  43. parser.add_argument('command', type=str, nargs='+', help=argparse.SUPPRESS)
  44. args = parser.parse_args()
  45. # Extract the needed variables from the args
  46. command = args.command
  47. top = args.top
  48. # Insure that perf is installed
  49. check_perf_presence = subprocess.run(["which", "perf"],
  50. stdout=subprocess.DEVNULL)
  51. if check_perf_presence.returncode:
  52. sys.exit("Please install perf before running the script!")
  53. # Insure user has previllage to run perf
  54. check_perf_executability = subprocess.run(["perf", "stat", "ls", "/"],
  55. stdout=subprocess.DEVNULL,
  56. stderr=subprocess.DEVNULL)
  57. if check_perf_executability.returncode:
  58. sys.exit(
  59. """
  60. Error:
  61. You may not have permission to collect stats.
  62. Consider tweaking /proc/sys/kernel/perf_event_paranoid,
  63. which controls use of the performance events system by
  64. unprivileged users (without CAP_SYS_ADMIN).
  65. -1: Allow use of (almost) all events by all users
  66. Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
  67. 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
  68. Disallow raw tracepoint access by users without CAP_SYS_ADMIN
  69. 1: Disallow CPU event access by users without CAP_SYS_ADMIN
  70. 2: Disallow kernel profiling by users without CAP_SYS_ADMIN
  71. To make this setting permanent, edit /etc/sysctl.conf too, e.g.:
  72. kernel.perf_event_paranoid = -1
  73. * Alternatively, you can run this script under sudo privileges.
  74. """
  75. )
  76. # Run perf record
  77. perf_record = subprocess.run((["perf", "record", "--output=/tmp/perf.data"] +
  78. command),
  79. stdout=subprocess.DEVNULL,
  80. stderr=subprocess.PIPE)
  81. if perf_record.returncode:
  82. os.unlink('/tmp/perf.data')
  83. sys.exit(perf_record.stderr.decode("utf-8"))
  84. # Save perf report output to /tmp/perf_report.out
  85. with open("/tmp/perf_report.out", "w") as output:
  86. perf_report = subprocess.run(
  87. ["perf", "report", "--input=/tmp/perf.data", "--stdio"],
  88. stdout=output,
  89. stderr=subprocess.PIPE)
  90. if perf_report.returncode:
  91. os.unlink('/tmp/perf.data')
  92. output.close()
  93. os.unlink('/tmp/perf_report.out')
  94. sys.exit(perf_report.stderr.decode("utf-8"))
  95. # Read the reported data to functions[]
  96. functions = []
  97. with open("/tmp/perf_report.out", "r") as data:
  98. # Only read lines that are not comments (comments start with #)
  99. # Only read lines that are not empty
  100. functions = [line for line in data.readlines() if line and line[0]
  101. != '#' and line[0] != "\n"]
  102. # Limit the number of top functions to "top"
  103. number_of_top_functions = top if len(functions) > top else len(functions)
  104. # Store the data of the top functions in top_functions[]
  105. top_functions = functions[:number_of_top_functions]
  106. # Print table header
  107. print('{:>4} {:>10} {:<30} {}\n{} {} {} {}'.format('No.',
  108. 'Percentage',
  109. 'Name',
  110. 'Invoked by',
  111. '-' * 4,
  112. '-' * 10,
  113. '-' * 30,
  114. '-' * 25))
  115. # Print top N functions
  116. for (index, function) in enumerate(top_functions, start=1):
  117. function_data = function.split()
  118. function_percentage = function_data[0]
  119. function_name = function_data[-1]
  120. function_invoker = ' '.join(function_data[2:-2])
  121. print('{:>4} {:>10} {:<30} {}'.format(index,
  122. function_percentage,
  123. function_name,
  124. function_invoker))
  125. # Remove intermediate files
  126. os.unlink('/tmp/perf.data')
  127. os.unlink('/tmp/perf_report.out')