perf scripts: Add configurable sorting option to powerpc-hcalls
PATCH 1/1 perf scripts: Add configurable sorting option to powerpc-hcalls
The powerpc-hcalls.py script currently prints hypercall statistics in a fixed sort order based on the number of hcall occurrences, making it difficult to analyze hcalls from different execution characteristics.
Add support for runtime-configurable sorting so users can order hypercall statistics by count, minimum latency, maximum latency, or average latency using a --sort option.
Parse arguments through sys.argv to support perf script argument passing semantics.
Example:
perf record -a -e powerpc* sleep 10
perf script -s ./powerpc-hcalls.py -i ./perf.data -- --sort=max
SORT KEY = max
hcall count min(ns) max(ns) avg(ns)
H_SEND_LOGICAL_LAN 47 7380 40148 8739 H_VIO_SIGNAL 706 880 17454 1911 H_RANDOM 1 15176 15176 15176 H_PUT_TCE_INDIRECT 4 3032 10444 4956 H_ADD_LOGICAL_LAN_BUFFER 363 1250 8716 1534 H_SEND_CRQ 8 2086 6846 3044 H_PUT_TCE 9 1284 4932 2646 H_STUFF_TCE 13 1620 3962 2358
This makes it easier to identify frequently occurring or high-latency hypercalls depending on the analysis being performed.
--- tools/perf/scripts/python/powerpc-hcalls.py | 35 ++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
tools/perf/scripts/python/powerpc-hcalls.py +34 -1
@@ -25,6 +25,7 @@ from Util import * # } ... # } output = {} +sort_key = 'count' # d_enter: { # cpu: {
@@ -158,10 +159,42 @@ def hcall_table_lookup(opcode): print_ptrn = '%-28s%10s%10s%10s%10s' +def sort_output(opcode): + stats = output[opcode] + + if sort_key == 'min': + return stats['min'] + if sort_key == 'max': + return stats['max'] + if sort_key == 'avg': + return stats['time'] // stats['cnt'] + + return stats['cnt'] + +def trace_begin(): + global sort_key + + i = 1 + while i < len(sys.argv): + arg = sys.argv[i] + + if arg == '-s' or arg == '--sort': + sort_key = sys.argv[i + 1] + i += 2 + continue + + if arg.startswith('--sort='): + sort_key = arg.split('=', 1)[1] + + i += 1 + + print("SORT KEY =", sort_key) + def trace_end(): print(print_ptrn % ('hcall', 'count', 'min(ns)', 'max(ns)', 'avg(ns)')) print('-' * 68) - for opcode in output: + for opcode in sorted(output, key = sort_output, + reverse=True): h_name = hcall_table_lookup(opcode) time = output[opcode]['time'] cnt = output[opcode]['cnt']
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The argument parsing loop in `trace_begin()` will crash with an `IndexError` if `-s` or `--sort`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The argument parsing loop in trace_begin() will crash with an IndexError if -s or --sort is the last argument provided. --
commit e3cf27646cdda0e6126f0f6cde0a6fdbb2db8e8c
Author: Shivani Nittor <[email protected]>
perf scripts: Add configurable sorting option to powerpc-hcalls
This commit adds a runtime-configurable sorting option to the powerpc-hcalls.py script. It parses sys.argv to allow users to order hypercall statistics by count or various latency metrics.
Quoted 4 lines
tools/perf/scripts/python/powerpc-hcalls.py +0
-0
[ ... ]
Quoted 4 lines
tools/perf/scripts/python/powerpc-hcalls.py +0
-0@@ -158,10 +159,42 @@ def hcall_table_lookup(opcode): print_ptrn = '%-28s%10s%10s%10s%10s'
[ ... ]
tools/perf/scripts/python/powerpc-hcalls.py def hcall_table_lookup(opcode): · line 182
Quoted 9 lines
tools/perf/scripts/python/powerpc-hcalls.py +9
-0+def trace_begin(): + global sort_key + + i = 1 + while i < len(sys.argv): + arg = sys.argv[i] + + if arg == '-s' or arg == '--sort': + sort_key = sys.argv[i + 1]
[Severity: Medium]
Does this code risk an IndexError if the script is invoked with -s or --sort as the very last argument?
If a user fails to provide the actual sort key value at the end of the argument list, accessing sys.argv[i + 1] will be out of bounds and crash the script.
Quoted 9 lines
tools/perf/scripts/python/powerpc-hcalls.py +9
-0+ i += 2 + continue + + if arg.startswith('--sort='): + sort_key = arg.split('=', 1)[1] + + i += 1 + + print("SORT KEY =", sort_key)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1