All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] perf scripts: Add configurable sorting option to powerpc-hcalls
@ 2026-06-17 14:40 Shivani Nittor
  2026-06-17 14:46 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shivani Nittor @ 2026-06-17 14:40 UTC (permalink / raw)
  To: acme, jolsa, adrian.hunter, mpetlan, tmricht, maddy, irogers,
	namhyung, ravi.bangoria
  Cc: linux-perf-users, linuxppc-dev, atrajeev, hbathini, Tejas.Manhas1,
	Tanushree.Shah, shivani

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.

Signed-off-by: Shivani Nittor <shivani@linux.ibm.com>
---
Changelog:
v1 -> v2
- Add bounds check for --sort argument handling
- Prevent IndexError when sort key is omitted

---
tools/perf/scripts/python/powerpc-hcalls.py | 38 ++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/tools/perf/scripts/python/powerpc-hcalls.py b/tools/perf/scripts/python/powerpc-hcalls.py
index 8b78dc790adb..e5b0c1bee65f 100644
--- a/tools/perf/scripts/python/powerpc-hcalls.py
+++ b/tools/perf/scripts/python/powerpc-hcalls.py
@@ -25,6 +25,7 @@ from Util import *
 #	} ...
 # }
 output = {}
+sort_key = 'count'
 
 # d_enter: {
 #	cpu: {
@@ -158,10 +159,45 @@ 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':
+            if i + 1 >= len(sys.argv):
+                print("Error: -s/--sort requires a sort key argument")
+                sys.exit(1)
+            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']
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] perf scripts: Add configurable sorting option to powerpc-hcalls
  2026-06-17 14:40 [PATCH v2] perf scripts: Add configurable sorting option to powerpc-hcalls Shivani Nittor
@ 2026-06-17 14:46 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-06-17 14:46 UTC (permalink / raw)
  To: Shivani Nittor; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The `--sort` argument value is not validated, leading to silently ignored user intent and misleading output for invalid keys.
--

commit 1aa1f92efbcdcc2ec41737225787d40ac02abf40
Author: Shivani Nittor <shivani@linux.ibm.com>

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. This patch
adds a --sort option to runtime-configure the sort order (count, min, max, avg).

> diff --git a/tools/perf/scripts/python/powerpc-hcalls.py b/tools/perf/scripts/python/powerpc-hcalls.py
> index 8b78dc790adb1..e5b0c1bee65fb 100644
> --- a/tools/perf/scripts/python/powerpc-hcalls.py
> +++ b/tools/perf/scripts/python/powerpc-hcalls.py

[ ... ]

> @@ -158,10 +159,45 @@ 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':
> +            if i + 1 >= len(sys.argv):
> +                print("Error: -s/--sort requires a sort key argument")
> +                sys.exit(1)
> +            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)

[Severity: Medium]
Will this result in silently falling back to sorting by count if an invalid
sort key is provided?

If a user runs the script with an invalid sort option like --sort=latency,
trace_begin() will assign it to sort_key and print "SORT KEY = latency".

Then in sort_output(), it will fall through the conditionals and return
stats['cnt'], leaving the user with output sorted by count despite the
printed sort key.

Could we add validation for the sort_key argument to error out if it isn't
one of the expected values (count, min, max, avg)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260617144011.17359-1-shivani@linux.ibm.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-06-17 14:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-17 14:40 [PATCH v2] perf scripts: Add configurable sorting option to powerpc-hcalls Shivani Nittor
2026-06-17 14:46 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.