Skip to contentSkip to conversation

perf: Complete transition to standalone Python scripts

PATCH Superseded Ian Rogers 5 participants 410 messages Sep 20 – Sep 26 Page 3 of 7

Subsystems (2)
lore
Share & tools
lore
author reviewer
#172 lore raw

PATCH v2 36/49 perf python: Port netdev-times to perf module

In reply to Ian Rogers

Port netdev-times.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/:
- Refactor the script into a NetDevTimesAnalyzer class with full type annotations to encapsulate state.
- Collect events via perf.session and sort them in timestamp order before analysis so multi-CPU TX and RX packet timelines are reconstructed deterministically.
- Replace custom argument parsing with argparse (-i/--input, --tx, --rx, --dev, --debug), extract tracepoint fields directly from sample attributes, and remove Python 2 compatibility artifacts.

Add a shell test (test_netdev_times_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/netdev-times.py             | 486 ++++++++++++++++++
 .../tests/shell/test_netdev_times_python.sh   |  61 +++
 2 files changed, 547 insertions(+)
 create mode 100755 tools/perf/python/netdev-times.py
 create mode 100755 tools/perf/tests/shell/test_netdev_times_python.sh
tools/perf/python/netdev-times.py new file +486 -0
new file mode 100755
index 000000000000..85f6d41e1179
@@ -0,0 +1,486 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Display a process of packets and processed time.
+It helps us to investigate networking or network device.
+
+Ported from tools/perf/scripts/python/netdev-times.py
+"""
+
+from __future__ import annotations
+import argparse
+from collections import defaultdict
+import sys
+from typing import Optional
+import perf
+
+# Format for displaying rx packet processing
+PF_IRQ_ENTRY = "  irq_entry(+%.3fmsec irq=%d:%s)"
+PF_SOFT_ENTRY = "  softirq_entry(+%.3fmsec)"
+PF_NAPI_POLL = "  napi_poll_exit(+%.3fmsec %s)"
+PF_JOINT = "         |"
+PF_WJOINT = "         |            |"
+PF_NET_RECV = "         |---netif_receive_skb(+%.3fmsec skb=%x len=%d)"
+PF_NET_RX = "         |---netif_rx(+%.3fmsec skb=%x)"
+PF_CPY_DGRAM = "         |      skb_copy_datagram_iovec(+%.3fmsec %d:%s)"
+PF_KFREE_SKB = "         |      kfree_skb(+%.3fmsec location=%x)"
+PF_CONS_SKB = "         |      consume_skb(+%.3fmsec)"
+
+
+class NetDevTimesAnalyzer:
+    """Analyzes network device events and prints charts."""
+
+    def __init__(self, cfg: argparse.Namespace):
+        self.args = cfg
+        self.session: Optional[perf.session] = None
+        self.show_tx = cfg.tx or (not cfg.tx and not cfg.rx)
+        self.show_rx = cfg.rx or (not cfg.tx and not cfg.rx)
+        self.dev = cfg.dev
+        self.debug = cfg.debug
+        self.buffer_budget = 65536
+        self.irq_dic: dict[int, list[dict]] = defaultdict(list)
+        self.net_rx_dic: dict[int, dict] = {}
+        self.receive_hunk_list: list[dict] = []
+        self.rx_skb_list: list[dict] = []
+        self.tx_queue_list: list[dict] = []
+        self.tx_xmit_list: list[dict] = []
+        self.tx_free_list: list[dict] = []
+
+        self.buffer_budget = 65536
+        self.of_count_rx_skb_list = 0
+        self.of_count_tx_queue_list = 0
+        self.of_count_tx_xmit_list = 0
+
+    def diff_msec(self, src: int, dst: int) -> float:
+        """Calculate a time interval(msec) from src(nsec) to dst(nsec)."""
+        return (dst - src) / 1000000.0
+
+    def print_transmit(self, hunk: dict) -> None:
+        """Display a process of transmitting a packet."""
+        if self.dev and hunk['dev'].find(self.dev) < 0:
+            return
+        queue_t_sec = hunk['queue_t'] // 1000000000
+        queue_t_usec = hunk['queue_t'] % 1000000000 // 1000
+        print(f"{hunk['dev']:7s} {hunk['len']:5d} "
+              f"{queue_t_sec:6d}.{queue_t_usec:06d}sec "
+              f"{self.diff_msec(hunk['queue_t'], hunk['xmit_t']):12.3f}msec      "
+              f"{self.diff_msec(hunk['xmit_t'], hunk['free_t']):12.3f}msec")
+
+    def print_receive(self, hunk: dict) -> None:
+        """Display a process of received packets and interrupts."""
+        show_hunk = False
+        irq_list = hunk['irq_list']
+        if not irq_list:
+            return
+        cpu = irq_list[0]['cpu']
+        base_t = irq_list[0]['irq_ent_t']
+
+        if self.dev:
+            for irq in irq_list:
+                if irq['name'].find(self.dev) >= 0:
+                    show_hunk = True
+                    break
+        else:
+            show_hunk = True
+
+        if not show_hunk:
+            return
+
+        base_t_sec = base_t // 1000000000
+        base_t_usec = base_t % 1000000000 // 1000
+        print(f"{base_t_sec}.{base_t_usec:06d}sec cpu={cpu}")
+        for irq in irq_list:
+            print(PF_IRQ_ENTRY %
+                  (self.diff_msec(base_t, irq['irq_ent_t']),
+                   irq['irq'], irq['name']))
+            print(PF_JOINT)
+            irq_event_list = irq['event_list']
+            for irq_event in irq_event_list:
+                if irq_event['event'] == 'netif_rx':
+                    print(PF_NET_RX %
+                          (self.diff_msec(base_t, irq_event['time']),
+                           irq_event['skbaddr']))
+                    print(PF_JOINT)
+
+        print(PF_SOFT_ENTRY % self.diff_msec(base_t, hunk['sirq_ent_t']))
+        print(PF_JOINT)
+        event_list = hunk['event_list']
+        for i, event in enumerate(event_list):
+            if event['event_name'] == 'napi_poll':
+                print(PF_NAPI_POLL %
+                      (self.diff_msec(base_t, event['event_t']),
+                       event['dev']))
+                if i == len(event_list) - 1:
+                    print("")
+                else:
+                    print(PF_JOINT)
+            else:
+                print(PF_NET_RECV %
+                      (self.diff_msec(base_t, event['event_t']),
+                       event['skbaddr'],
+                       event['len']))
+                if 'handle' in event:
+                    print(PF_WJOINT)
+                    if event['handle'] == "kfree_skb":
+                        print(PF_KFREE_SKB %
+                              (self.diff_msec(base_t, event['comm_t']),
+                               event['location']))
+                    elif event['handle'] == "consume_skb":
+                        print(PF_CONS_SKB %
+                              self.diff_msec(base_t, event['comm_t']))
+                    elif event['handle'] == "skb_copy_datagram_iovec":
+                        print(PF_CPY_DGRAM %
+                              (self.diff_msec(base_t, event['comm_t']),
+                               event['pid'], event['comm']))
+                print(PF_JOINT)
+
+    def handle_irq_handler_entry(self, event: dict) -> None:
+        """Handle irq:irq_handler_entry event."""
+        time = event['time']
+        cpu = event['cpu']
+        irq = event['irq']
+        irq_name = event['irq_name']
+        irq_record = {'irq': irq, 'name': irq_name, 'cpu': cpu,
+                      'irq_ent_t': time, 'event_list': []}
+        self.irq_dic[cpu].append(irq_record)
+
+    def handle_irq_handler_exit(self, event: dict) -> None:
+        """Handle irq:irq_handler_exit event."""
+        time = event['time']
+        cpu = event['cpu']
+        irq = event['irq']
+        if cpu not in self.irq_dic or not self.irq_dic[cpu]:
+            return
+        if irq != self.irq_dic[cpu][-1]['irq']:
+            return
+        irq_record = self.irq_dic[cpu].pop()
+        irq_record['irq_ext_t'] = time
+        # if an irq doesn't include NET_RX softirq, drop.
+        if irq_record['event_list']:
+            self.irq_dic[cpu].append(irq_record)
+
+    def handle_irq_softirq_raise(self, event: dict) -> None:
+        """Handle irq:softirq_raise event."""
+        time = event['time']
+        cpu = event['cpu']
+        if cpu not in self.irq_dic or not self.irq_dic[cpu]:
+            return
+        irq_record = self.irq_dic[cpu].pop()
+        irq_record['event_list'].append({'time': time, 'event': 'sirq_raise'})
+        self.irq_dic[cpu].append(irq_record)
+
+    def handle_irq_softirq_entry(self, event: dict) -> None:
+        """Handle irq:softirq_entry event."""
+        time = event['time']
+        cpu = event['cpu']
+        self.net_rx_dic[cpu] = {'sirq_ent_t': time, 'event_list': []}
+
+    def handle_irq_softirq_exit(self, event: dict) -> None:
+        """Handle irq:softirq_exit event."""
+        time = event['time']
+        cpu = event['cpu']
+        irq_list = None
+        event_list = None
+        sirq_ent_t = None
+
+        if cpu in self.irq_dic:
+            irq_list = self.irq_dic[cpu]
+            del self.irq_dic[cpu]
+        if cpu in self.net_rx_dic:
+            sirq_ent_t = self.net_rx_dic[cpu]['sirq_ent_t']
+            event_list = self.net_rx_dic[cpu]['event_list']
+            del self.net_rx_dic[cpu]
+        if irq_list is None or event_list is None or sirq_ent_t is None:
+            return
+        rec_data = {'sirq_ent_t': sirq_ent_t, 'sirq_ext_t': time,
+                    'irq_list': irq_list, 'event_list': event_list}
+        self.receive_hunk_list.append(rec_data)
+
+    def handle_napi_poll(self, event: dict) -> None:
+        """Handle napi:napi_poll event."""
+        time = event['time']
+        cpu = event['cpu']
+        dev_name = event['dev_name']
+        work = event['work']
+        budget = event['budget']
+        if cpu in self.net_rx_dic:
+            event_list = self.net_rx_dic[cpu]['event_list']
+            rec_data = {'event_name': 'napi_poll',
+                        'dev': dev_name, 'event_t': time,
+                        'work': work, 'budget': budget}
+            event_list.append(rec_data)
+
+    def handle_netif_rx(self, event: dict) -> None:
+        """Handle net:netif_rx event."""
+        time = event['time']
+        cpu = event['cpu']
+        skbaddr = event['skbaddr']
+        skblen = event['skblen']
+        dev_name = event['dev_name']
+        if cpu not in self.irq_dic or not self.irq_dic[cpu]:
+            return
+        irq_record = self.irq_dic[cpu].pop()
+        irq_record['event_list'].append({'time': time, 'event': 'netif_rx',
+                                         'skbaddr': skbaddr, 'skblen': skblen,
+                                         'dev_name': dev_name})
+        self.irq_dic[cpu].append(irq_record)
+
+    def handle_netif_receive_skb(self, event: dict) -> None:
+        """Handle net:netif_receive_skb event."""
+        time = event['time']
+        cpu = event['cpu']
+        skbaddr = event['skbaddr']
+        skblen = event['skblen']
+        if cpu in self.net_rx_dic:
+            rec_data = {'event_name': 'netif_receive_skb',
+                        'event_t': time, 'skbaddr': skbaddr, 'len': skblen}
+            event_list = self.net_rx_dic[cpu]['event_list']
+            event_list.append(rec_data)
+            self.rx_skb_list.insert(0, rec_data)
+            if len(self.rx_skb_list) > self.buffer_budget:
+                self.rx_skb_list.pop()
+                self.of_count_rx_skb_list += 1
+
+    def handle_net_dev_queue(self, event: dict) -> None:
+        """Handle net:net_dev_queue event."""
+        time = event['time']
+        skbaddr = event['skbaddr']
+        skblen = event['skblen']
+        dev_name = event['dev_name']
+        skb = {'dev': dev_name, 'skbaddr': skbaddr, 'len': skblen, 'queue_t': time}
+        self.tx_queue_list.insert(0, skb)
+        if len(self.tx_queue_list) > self.buffer_budget:
+            self.tx_queue_list.pop()
+            self.of_count_tx_queue_list += 1
+
+    def handle_net_dev_xmit(self, event: dict) -> None:
+        """Handle net:net_dev_xmit event."""
+        time = event['time']
+        skbaddr = event['skbaddr']
+        rc = event['rc']
+        if rc == 0:  # NETDEV_TX_OK
+            for i, skb in enumerate(self.tx_queue_list):
+                if skb['skbaddr'] == skbaddr:
+                    skb['xmit_t'] = time
+                    self.tx_xmit_list.insert(0, skb)
+                    del self.tx_queue_list[i]
+                    if len(self.tx_xmit_list) > self.buffer_budget:
+                        self.tx_xmit_list.pop()
+                        self.of_count_tx_xmit_list += 1
+                    return
+
+    def handle_kfree_skb(self, event: dict) -> None:
+        """Handle skb:kfree_skb event."""
+        time = event['time']
+        skbaddr = event['skbaddr']
+        comm = event['comm']
+        pid = event['pid']
+        location = event['location']
+        for i, skb in enumerate(self.tx_queue_list):
+            if skb['skbaddr'] == skbaddr:
+                del self.tx_queue_list[i]
+                return
+        for i, skb in enumerate(self.tx_xmit_list):
+            if skb['skbaddr'] == skbaddr:
+                skb['free_t'] = time
+                self.tx_free_list.append(skb)
+                del self.tx_xmit_list[i]
+                return
+        for i, rec_data in enumerate(self.rx_skb_list):
+            if rec_data['skbaddr'] == skbaddr:
+                rec_data.update({'handle': "kfree_skb",
+                                 'comm': comm, 'pid': pid, 'comm_t': time, 'location': location})
+                del self.rx_skb_list[i]
+                return
+
+    def handle_consume_skb(self, event: dict) -> None:
+        """Handle skb:consume_skb event."""
+        time = event['time']
+        skbaddr = event['skbaddr']
+        for i, skb in enumerate(self.tx_xmit_list):
+            if skb['skbaddr'] == skbaddr:
+                skb['free_t'] = time
+                self.tx_free_list.append(skb)
+                del self.tx_xmit_list[i]
+                return
+        for i, rec_data in enumerate(self.rx_skb_list):
+            if rec_data['skbaddr'] == skbaddr:
+                rec_data.update({'handle': "consume_skb", 'comm_t': time})
+                del self.rx_skb_list[i]
+                return
+
+    def handle_skb_copy_datagram_iovec(self, event: dict) -> None:
+        """Handle skb:skb_copy_datagram_iovec event."""
+        time = event['time']
+        skbaddr = event['skbaddr']
+        comm = event['comm']
+        pid = event['pid']
+        for i, rec_data in enumerate(self.rx_skb_list):
+            if skbaddr == rec_data['skbaddr']:
+                rec_data.update({'handle': "skb_copy_datagram_iovec",
+                                 'comm': comm, 'pid': pid, 'comm_t': time})
+                del self.rx_skb_list[i]
+                return
+
+
+
+    def print_summary(self) -> None:
+        """Print charts."""
+
+        # display receive hunks
+        if self.show_rx:
+            for hunk in self.receive_hunk_list:
+                self.print_receive(hunk)
+
+        # display transmit hunks
+        if self.show_tx:
+            print("   dev    len      Qdisc        "
+                  "       netdevice             free")
+            for hunk in self.tx_free_list:
+                self.print_transmit(hunk)
+
+        if self.debug:
+            print("debug buffer status")
+            print("----------------------------")
+            print(f"xmit Qdisc:remain:{len(self.tx_queue_list)} "
+                  f"overflow:{self.of_count_tx_queue_list}")
+            print(f"xmit netdevice:remain:{len(self.tx_xmit_list)} "
+                  f"overflow:{self.of_count_tx_xmit_list}")
+            print(f"receive:remain:{len(self.rx_skb_list)} "
+                  f"overflow:{self.of_count_rx_skb_list}")
+
+    def handle_single_event(self, event: dict) -> None:
+        """Handle a single processed event."""
+        name = event['name']
+        if name == 'irq:softirq_exit':
+            self.handle_irq_softirq_exit(event)
+        elif name == 'irq:softirq_entry':
+            self.handle_irq_softirq_entry(event)
+        elif name == 'irq:softirq_raise':
+            self.handle_irq_softirq_raise(event)
+        elif name == 'irq:irq_handler_entry':
+            self.handle_irq_handler_entry(event)
+        elif name == 'irq:irq_handler_exit':
+            self.handle_irq_handler_exit(event)
+        elif name == 'napi:napi_poll':
+            self.handle_napi_poll(event)
+        elif name == 'net:netif_receive_skb':
+            self.handle_netif_receive_skb(event)
+        elif name == 'net:netif_rx':
+            self.handle_netif_rx(event)
+        elif name == 'skb:skb_copy_datagram_iovec':
+            self.handle_skb_copy_datagram_iovec(event)
+        elif name == 'net:net_dev_queue':
+            self.handle_net_dev_queue(event)
+        elif name == 'net:net_dev_xmit':
+            self.handle_net_dev_xmit(event)
+        elif name == 'skb:kfree_skb':
+            self.handle_kfree_skb(event)
+        elif name == 'skb:consume_skb':
+            self.handle_consume_skb(event)
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process events directly on-the-fly."""
+        name = str(sample.evsel)
+        ev_name = name[6:-1] if name.startswith("evsel(") else name
+        pid = sample.sample_pid
+        if hasattr(self, 'session') and self.session:
+            try:
+                thread = self.session.find_thread(pid)
+                comm = (thread.comm() if thread else None) or "[unknown]"
+            except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+                comm = "[unknown]"
+        else:
+            comm = "Unknown"
+        event_data = {
+            'name': ev_name,
+            'time': sample.sample_time,
+            'cpu': sample.sample_cpu,
+            'pid': pid,
+            'comm': comm,
+        }
+
+        # Extract specific fields based on event type
+        if ev_name.startswith("irq:softirq_"):
+            event_data['vec'] = getattr(sample, "vec", 0)
+            # Filter for NET_RX
+            if event_data['vec'] != 3:  # NET_RX_SOFTIRQ is usually 3
+                return
+        elif ev_name == "irq:irq_handler_entry":
+            event_data['irq'] = getattr(sample, "irq", -1)
+            event_data['irq_name'] = getattr(sample, "name", "[unknown]")
+        elif ev_name == "irq:irq_handler_exit":
+            event_data['irq'] = getattr(sample, "irq", -1)
+            event_data['ret'] = getattr(sample, "ret", 0)
+        elif ev_name == "napi:napi_poll":
+            event_data['napi'] = getattr(sample, "napi", 0)
+            event_data['dev_name'] = getattr(sample, "dev_name", "[unknown]")
+            event_data['work'] = getattr(sample, "work", 0)
+            event_data['budget'] = getattr(sample, "budget", 0)
+        elif ev_name in ("net:netif_receive_skb", "net:netif_rx",
+                         "net:net_dev_queue"):
+            event_data['skbaddr'] = getattr(sample, "skbaddr", 0)
+            event_data['skblen'] = getattr(sample, "len", 0)
+            event_data['dev_name'] = getattr(sample, "name", "[unknown]")
+        elif ev_name == "net:net_dev_xmit":
+            event_data['skbaddr'] = getattr(sample, "skbaddr", 0)
+            event_data['skblen'] = getattr(sample, "len", 0)
+            event_data['rc'] = getattr(sample, "rc", 0)
+            event_data['dev_name'] = getattr(sample, "name", "[unknown]")
+        elif ev_name == "skb:kfree_skb":
+            event_data['skbaddr'] = getattr(sample, "skbaddr", 0)
+            event_data['location'] = getattr(sample, "location", 0)
+            event_data['protocol'] = getattr(sample, "protocol", 0)
+            event_data['reason'] = getattr(sample, "reason", 0)
+        elif ev_name == "skb:consume_skb":
+            event_data['skbaddr'] = getattr(sample, "skbaddr", 0)
+            event_data['location'] = getattr(sample, "location", 0)
+        elif ev_name == "skb:skb_copy_datagram_iovec":
+            event_data['skbaddr'] = getattr(sample, "skbaddr", 0)
+            event_data['skblen'] = getattr(sample, "len", 0)
+
+        self.handle_single_event(event_data)
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(description="Display a process of packets and processed time.")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("--tx", action="store_true", help="show only tx chart")
+    ap.add_argument("--rx", action="store_true", help="show only rx chart")
+    ap.add_argument("--dev", default=None, help="show only specified device")
+    ap.add_argument("--debug", action="store_true",
+                    help="work with debug mode. It shows buffer status.")
+    ap.add_argument("positionals", nargs="*",
+                    help="optional positional arguments (tx, rx, dev=<name>, debug)")
+    args, unknown_args = ap.parse_known_args()
+
+    parsed_args = argparse.Namespace(
+        tx=args.tx, rx=args.rx, dev=args.dev, debug=args.debug, input=args.input
+    )
+
+    for arg in list(args.positionals) + unknown_args:
+        if not arg or not isinstance(arg, str):
+            continue
+        if arg in ('tx', '--tx'):
+            parsed_args.tx = True
+        elif arg in ('rx', '--rx'):
+            parsed_args.rx = True
+        elif arg.startswith('dev='):
+            parsed_args.dev = arg[4:]
+        elif arg.startswith('--dev='):
+            parsed_args.dev = arg[6:]
+        elif arg in ('debug', '--debug'):
+            parsed_args.debug = True
+
+    analyzer = NetDevTimesAnalyzer(parsed_args)
+
+    try:
+        session = perf.session(perf.data(parsed_args.input), sample=analyzer.process_event)
+        analyzer.session = session
+        session.process_events()
+        analyzer.print_summary()
+    except KeyboardInterrupt:
+        analyzer.print_summary()
+    except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+        print(f"Error processing events: {e}")
+        sys.exit(1)
tools/perf/tests/shell/test_netdev_times_python.sh new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#174 lore raw

PATCH v2 37/49 perf python: Port check-perf-trace to perf module

In reply to Ian Rogers

Port check-perf-trace.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Access tracepoint fields directly as attributes on perf.sample_event instead of per-event dictionaries and legacy Perf-Trace-Util helpers.
- Decode symbolic flag and enum masks for irq:softirq_entry and kmem:kmalloc directly in Python and add -i/--input CLI support via argparse.
- Add full type annotations and clean up Python 2 idioms.

Add a shell test (test_check_perf_trace_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/check-perf-trace.py         | 213 ++++++++++++++++++
 .../shell/test_check_perf_trace_python.sh     |  79 +++++++
 2 files changed, 292 insertions(+)
 create mode 100755 tools/perf/python/check-perf-trace.py
 create mode 100755 tools/perf/tests/shell/test_check_perf_trace_python.sh
tools/perf/python/check-perf-trace.py new file +213 -0
new file mode 100755
index 000000000000..19a91c7934b7
@@ -0,0 +1,213 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Basic test of Python scripting support for perf.
+Ported from tools/perf/scripts/python/check-perf-trace.py
+"""
+from __future__ import annotations
+
+import argparse
+import collections
+import perf
+
+unhandled: collections.defaultdict[str, int] = collections.defaultdict(int)
+session = None
+
+softirq_vecs = {
+    0: "HI_SOFTIRQ",
+    1: "TIMER_SOFTIRQ",
+    2: "NET_TX_SOFTIRQ",
+    3: "NET_RX_SOFTIRQ",
+    4: "BLOCK_SOFTIRQ",
+    5: "IRQ_POLL_SOFTIRQ",
+    6: "TASKLET_SOFTIRQ",
+    7: "SCHED_SOFTIRQ",
+    8: "HRTIMER_SOFTIRQ",
+    9: "RCU_SOFTIRQ",
+}
+
+_GFP_DMA = 1 << 0
+_GFP_HIGHMEM = 1 << 1
+_GFP_DMA32 = 1 << 2
+_GFP_MOVABLE = 1 << 3
+_GFP_RECLAIMABLE = 1 << 4
+_GFP_HIGH = 1 << 5
+_GFP_IO = 1 << 6
+_GFP_FS = 1 << 7
+_GFP_ZERO = 1 << 8
+_GFP_DIRECT_RECLAIM = 1 << 10
+_GFP_KSWAPD_RECLAIM = 1 << 11
+_GFP_WRITE = 1 << 12
+_GFP_NOWARN = 1 << 13
+_GFP_RETRY_MAYFAIL = 1 << 14
+_GFP_NOFAIL = 1 << 15
+_GFP_NORETRY = 1 << 16
+_GFP_MEMALLOC = 1 << 17
+_GFP_COMP = 1 << 18
+_GFP_NOMEMALLOC = 1 << 19
+_GFP_HARDWALL = 1 << 20
+_GFP_THISNODE = 1 << 21
+_GFP_ACCOUNT = 1 << 22
+_GFP_ZEROTAGS = 1 << 23
+
+_GFP_RECLAIM = _GFP_DIRECT_RECLAIM | _GFP_KSWAPD_RECLAIM
+_GFP_KERNEL = _GFP_RECLAIM | _GFP_IO | _GFP_FS
+_GFP_USER = _GFP_KERNEL | _GFP_HARDWALL
+_GFP_HIGHUSER = _GFP_USER | _GFP_HIGHMEM
+_GFP_HIGHUSER_MOVABLE = _GFP_HIGHUSER | _GFP_MOVABLE
+_GFP_TRANSHUGE_LIGHT = (
+    _GFP_HIGHUSER_MOVABLE | _GFP_COMP | _GFP_NOMEMALLOC | _GFP_NOWARN
+) & ~_GFP_RECLAIM
+_GFP_TRANSHUGE = _GFP_TRANSHUGE_LIGHT | _GFP_DIRECT_RECLAIM
+
+GFP_FLAG_NAMES = [
+    (_GFP_TRANSHUGE, "GFP_TRANSHUGE"),
+    (_GFP_TRANSHUGE_LIGHT, "GFP_TRANSHUGE_LIGHT"),
+    (_GFP_HIGHUSER_MOVABLE, "GFP_HIGHUSER_MOVABLE"),
+    (_GFP_HIGHUSER, "GFP_HIGHUSER"),
+    (_GFP_USER, "GFP_USER"),
+    (_GFP_KERNEL | _GFP_ACCOUNT, "GFP_KERNEL_ACCOUNT"),
+    (_GFP_KERNEL, "GFP_KERNEL"),
+    (_GFP_RECLAIM | _GFP_IO, "GFP_NOFS"),
+    (_GFP_HIGH | _GFP_KSWAPD_RECLAIM, "GFP_ATOMIC"),
+    (_GFP_RECLAIM, "GFP_NOIO"),
+    (_GFP_KSWAPD_RECLAIM | _GFP_NOWARN, "GFP_NOWAIT"),
+    (_GFP_DMA, "GFP_DMA"),
+    (_GFP_DMA32, "GFP_DMA32"),
+    (_GFP_RECLAIM, "__GFP_RECLAIM"),
+    (_GFP_DMA, "__GFP_DMA"),
+    (_GFP_HIGHMEM, "__GFP_HIGHMEM"),
+    (_GFP_DMA32, "__GFP_DMA32"),
+    (_GFP_MOVABLE, "__GFP_MOVABLE"),
+    (_GFP_RECLAIMABLE, "__GFP_RECLAIMABLE"),
+    (_GFP_HIGH, "__GFP_HIGH"),
+    (_GFP_IO, "__GFP_IO"),
+    (_GFP_FS, "__GFP_FS"),
+    (_GFP_ZERO, "__GFP_ZERO"),
+    (_GFP_DIRECT_RECLAIM, "__GFP_DIRECT_RECLAIM"),
+    (_GFP_KSWAPD_RECLAIM, "__GFP_KSWAPD_RECLAIM"),
+    (_GFP_WRITE, "__GFP_WRITE"),
+    (_GFP_NOWARN, "__GFP_NOWARN"),
+    (_GFP_RETRY_MAYFAIL, "__GFP_RETRY_MAYFAIL"),
+    (_GFP_NOFAIL, "__GFP_NOFAIL"),
+    (_GFP_NORETRY, "__GFP_NORETRY"),
+    (_GFP_MEMALLOC, "__GFP_MEMALLOC"),
+    (_GFP_COMP, "__GFP_COMP"),
+    (_GFP_NOMEMALLOC, "__GFP_NOMEMALLOC"),
+    (_GFP_HARDWALL, "__GFP_HARDWALL"),
+    (_GFP_THISNODE, "__GFP_THISNODE"),
+    (_GFP_ACCOUNT, "__GFP_ACCOUNT"),
+    (_GFP_ZEROTAGS, "__GFP_ZEROTAGS"),
+]
+
+
+def trace_begin() -> None:
+    """Called at the start of trace processing."""
+    print("trace_begin")
+
+def trace_end() -> None:
+    """Called at the end of trace processing."""
+    print_unhandled()
+    print("trace_end")
+
+def symbol_str(event_name: str, field_name: str, value: int) -> str:
+    """Resolves symbol values to strings."""
+    # Note: The standalone Python API currently lacks dynamic libtraceevent
+    # formatting (equivalent to _perf_trace_context.symbol_str())
+    if event_name == "irq__softirq_entry" and field_name == "vec":
+        return softirq_vecs.get(value, str(value))
+    return str(value)
+
+def flag_str(event_name: str, field_name: str, value: int) -> str:
+    """Resolves flag values to strings."""
+    # Note: The standalone Python API currently lacks dynamic libtraceevent
+    # formatting (equivalent to _perf_trace_context.flag_str())
+    if event_name == "kmem__kmalloc" and field_name == "gfp_flags":
+        if value == 0:
+            return "none"
+        names = []
+        rem = value
+        for mask, name in GFP_FLAG_NAMES:
+            if (rem & mask) == mask:
+                names.append(name)
+                rem &= ~mask
+        if rem:
+            names.append(f"0x{rem:x}")
+        return "|".join(names)
+    return str(value)
+
+def print_header(event_name: str, sample: perf.sample_event) -> None:
+    """Prints common header for events."""
+    secs = sample.sample_time // 1000000000
+    nsecs = sample.sample_time % 1000000000
+    comm = "[unknown]"
+    try:
+        if session:
+            thread = session.find_thread(sample.sample_tid)
+            if thread:
+                comm = thread.comm() or "[unknown]"
+    except (TypeError, AttributeError):
+        pass
+    print(f"{event_name:<20} {sample.sample_cpu:5} {secs:05}.{nsecs:09} "
+          f"{sample.sample_tid:8} {comm:<20} ", end=' ')
+
+def print_uncommon(sample: perf.sample_event) -> None:
+    """Prints uncommon fields for tracepoints."""
+    # Fallback to 0 if field not found (e.g. on older kernels or if not tracepoint)
+    pc = getattr(sample, 'common_preempt_count', 0)
+    flags = getattr(sample, 'common_flags', 0)
+    lock_depth = getattr(sample, 'common_lock_depth', 0)
+
+    print(f"common_preempt_count={pc}, common_flags={flags}, "
+          f"common_lock_depth={lock_depth}, ", end='')
+
+def irq__softirq_entry(sample: perf.sample_event) -> None:
+    """Handles irq:softirq_entry events."""
+    print_header("irq__softirq_entry", sample)
+    print_uncommon(sample)
+    print(f"vec={symbol_str('irq__softirq_entry', 'vec', getattr(sample, 'vec', 0))}")
+
+def kmem__kmalloc(sample: perf.sample_event) -> None:
+    """Handles kmem:kmalloc events."""
+    print_header("kmem__kmalloc", sample)
+    print_uncommon(sample)
+
+    print(f"call_site={getattr(sample, 'call_site', 0):#x}, "
+          f"ptr={getattr(sample, 'ptr', 0):#x}, "
+          f"bytes_req={getattr(sample, 'bytes_req', 0):d}, "
+          f"bytes_alloc={getattr(sample, 'bytes_alloc', 0):d}, "
+          f"gfp_flags={flag_str('kmem__kmalloc', 'gfp_flags', getattr(sample, 'gfp_flags', 0))}")
+
+def trace_unhandled(event_name: str) -> None:
+    """Tracks unhandled events."""
+    unhandled[event_name] += 1
+
+def print_unhandled() -> None:
+    """Prints summary of unhandled events."""
+    if not unhandled:
+        return
+    print("\nunhandled events:\n")
+    print(f"{'event':<40} {'count':>10}")
+    print("---------------------------------------- -----------")
+    for event_name, count in unhandled.items():
+        print(f"{event_name:<40} {count:10}")
+
+def process_event(sample: perf.sample_event) -> None:
+    """Callback for processing events."""
+    event_name = str(sample.evsel)
+    if event_name.startswith("evsel(irq:softirq_entry)"):
+        irq__softirq_entry(sample)
+    elif "evsel(kmem:kmalloc)" in event_name:
+        kmem__kmalloc(sample)
+    else:
+        trace_unhandled(event_name)
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser()
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    args = ap.parse_args()
+
+    trace_begin()
+    session = perf.session(perf.data(args.input), sample=process_event)
+    session.process_events()
+    trace_end()
tools/perf/tests/shell/test_check_perf_trace_python.sh new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#176 lore raw

PATCH v2 38/49 perf python: Port arm-cs-trace-disasm to perf module

In reply to Ian Rogers

Port arm-cs-trace-disasm.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Encapsulate trace disassembly state in a TraceDisasm class
- Automatically search standard kernel debug paths (find_vmlinux()) when -k/--vmlinux is not specified, and query perf.config_get("annotate.objdump") for the default objdump binary.
- Bound DISASM_CACHE memory consumption by evicting the cache at 1024 entries and skipping caching of oversized (> 512 lines) objdump outputs.
- Use sample.srccode() from the perf extension module to annotate disassembly output with source filenames, line numbers, and source lines.

Update the ARM CoreSight disassembly shell test (test_arm_coresight_disasm.sh) to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++
 .../coresight/test_arm_coresight_disasm.sh    |  24 +-
 tools/perf/tests/shell/lib/setup_python.sh    |   4 +
 3 files changed, 378 insertions(+), 6 deletions(-)
 create mode 100755 tools/perf/python/arm-cs-trace-disasm.py
tools/perf/python/arm-cs-trace-disasm.py new file +356 -0
new file mode 100755
index 000000000000..1a9d9a01d1e4
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember using perf python module
+"""
+from __future__ import annotations
+
+import os
+from os import path
+import re
+from subprocess import CalledProcessError, check_output
+import argparse
+import platform
+import sys
+from typing import Dict, List, Optional
+
+import perf
+
+# Initialize global dicts and regular expression
+DISASM_CACHE: Dict[str, List[str]] = {}
+CPU_DATA: Dict[str, int] = {}
+DISASM_RE = re.compile(r"^\s*([0-9a-fA-F]+):")
+DISASM_FUNC_RE = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
+CACHE_SIZE = 1024
+class _State:
+    sample_idx: int = -1
+    source_file_name: Optional[str] = None
+    line_number: Optional[int] = None
+    dso: Optional[str] = None
+
+_STATE = _State()
+
+KVER = platform.release()
+VMLINUX_PATHS = [
+    f"/usr/lib/debug/boot/vmlinux-{KVER}.debug",
+    f"/usr/lib/debug/lib/modules/{KVER}/vmlinux",
+    f"/lib/modules/{KVER}/build/vmlinux",
+    f"/usr/lib/debug/boot/vmlinux-{KVER}",
+    f"/boot/vmlinux-{KVER}",
+    "/boot/vmlinux",
+    "vmlinux"
+]
+
+def default_objdump() -> str:
+    """Return the default objdump path from perf config or 'objdump'."""
+    try:
+        config = perf.config_get("annotate.objdump")
+        return str(config) if config else "objdump"
+    except (AttributeError, TypeError):
+        return "objdump"
+
+def find_vmlinux() -> Optional[str]:
+    """Find the vmlinux file in standard paths."""
+    if hasattr(find_vmlinux, "path"):
+        return getattr(find_vmlinux, "path")
+
+    for v in VMLINUX_PATHS:
+        if os.access(v, os.R_OK):
+            setattr(find_vmlinux, "path", v)
+            return v
+    setattr(find_vmlinux, "path", None)
+    return None
+
+def get_dso_file_path(dso_name: str, dso_build_id: str, vmlinux: Optional[str]) -> str:
+    """Return the path to the DSO file."""
+    buildid_dir = os.environ.get('PERF_BUILDID_DIR')
+    if not buildid_dir:
+        buildid_dir = os.path.join(os.environ.get('HOME', ''), '.debug')
+
+    if dso_name in ("[kernel.kallsyms]", "vmlinux"):
+        if vmlinux:
+            return vmlinux
+        if dso_build_id:
+            for kname in (dso_name, "vmlinux", "[kernel.kallsyms]"):
+                candidate = os.path.join(buildid_dir, kname, dso_build_id, "elf")
+                if os.access(candidate, os.R_OK):
+                    return candidate
+        return find_vmlinux() or dso_name
+
+    if dso_name == "[vdso]":
+        append = "/vdso"
+    else:
+        append = "/elf"
+
+    dso_path = buildid_dir + "/" + dso_name + "/" + dso_build_id + append
+    # Replace duplicate slash chars to single slash char
+    dso_path = dso_path.replace('//', '/', 1)
+    return dso_path
+
+def read_disam(dso_fname: str, dso_start: int, start_addr: int,
+               stop_addr: int, objdump: str) -> List[str]:
+    """Read disassembly from a DSO file using objdump."""
+    addr_range = f"{start_addr}:{stop_addr}:{dso_start}:{dso_fname}"
+
+    # Don't let the cache get too big, clear it when it hits max size
+    if len(DISASM_CACHE) > CACHE_SIZE:
+        DISASM_CACHE.clear()
+
+    if addr_range in DISASM_CACHE:
+        disasm_output = DISASM_CACHE[addr_range]
+    else:
+        start_addr = start_addr - dso_start
+        stop_addr = stop_addr - dso_start
+        disasm = [objdump, "-d", "-z",
+                  f"--start-address={start_addr:#x}",
+                  f"--stop-address={stop_addr:#x}"]
+        disasm += [dso_fname]
+        try:
+            disasm_output = check_output(disasm).decode('utf-8', errors='replace').split('\n')
+        except (CalledProcessError, OSError):
+            return []
+        if len(disasm_output) <= 512:
+            DISASM_CACHE[addr_range] = disasm_output
+
+    return disasm_output
+
+def print_disam(dso_fname: str, dso_start: int, start_addr: int,
+                stop_addr: int, objdump: str) -> None:
+    """Print disassembly for a given address range."""
+    for line in read_disam(dso_fname, dso_start, start_addr, stop_addr, objdump):
+        m = DISASM_FUNC_RE.search(line)
+        if m is None:
+            m = DISASM_RE.search(line)
+            if m is None:
+                continue
+        print(f"\t{line}")
+
+def print_sample(sample: perf.sample_event) -> None:
+    """Print sample details."""
+    print(f"Sample = {{ cpu: {sample.sample_cpu:04d} addr: {sample.sample_addr:016x} "
+          f"phys_addr: {sample.sample_phys_addr:016x} ip: {sample.sample_ip:016x} "
+          f"pid: {sample.sample_pid} tid: {sample.sample_tid} period: {sample.sample_period} "
+          f"time: {sample.sample_time} index: {_STATE.sample_idx}}}")
+
+def common_start_str(comm: str, sample: perf.sample_event) -> str:
+    """Return common start string for sample output."""
+    sec = int(sample.sample_time / 1000000000)
+    ns = sample.sample_time % 1000000000
+    cpu = sample.sample_cpu
+    pid = sample.sample_pid
+    tid = sample.sample_tid
+    return f"{comm:>16s} {pid:5d}/{tid:<5d} [{cpu:04d}] {sec:9d}.{ns:09d}  "
+
+def print_srccode(comm: str, sample: perf.sample_event, symbol: str, dso: str) -> None:
+    """Print source code and symbols for a sample."""
+    ip = sample.sample_ip
+    if symbol == "[unknown]":
+        start_str = common_start_str(comm, sample) + f"{ip:x}".rjust(16).ljust(40)
+    else:
+        symoff = 0
+        symoff = getattr(sample, 'sym_offset', 0) or 0
+        offs = f"+{symoff:#x}" if symoff != 0 else ""
+        start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
+
+    source_file_name, line_number, source_line = sample.srccode() or (None, 0, None)
+    if source_file_name:
+        if _STATE.line_number == line_number and _STATE.source_file_name == source_file_name:
+            src_str = ""
+        else:
+            if len(source_file_name) > 40:
+                src_file = f"...{source_file_name[-37:]} "
+            else:
+                src_file = source_file_name.ljust(41)
+
+            if source_line is None:
+                src_str = f"{src_file}{line_number:>4d} <source not found>"
+            else:
+                src_str = f"{src_file}{line_number:>4d} {source_line}"
+        _STATE.dso = None
+    elif dso == _STATE.dso:
+        src_str = ""
+    else:
+        src_str = dso
+        _STATE.dso = dso
+
+    _STATE.line_number = line_number
+    _STATE.source_file_name = source_file_name
+
+    print(start_str, src_str)
+
+class TraceDisasm:
+    """Class to handle trace disassembly."""
+    def __init__(self, cli_options: argparse.Namespace):
+        self.options = cli_options
+        self.sample_idx = -1
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process a single perf event."""
+        self.sample_idx += 1
+        _STATE.sample_idx = self.sample_idx
+
+        if self.options.start_time is not None and sample.sample_time < self.options.start_time:
+            return
+        if self.options.stop_time is not None and sample.sample_time > self.options.stop_time:
+            sys.exit(0)
+        if self.options.start_sample is not None and self.sample_idx < self.options.start_sample:
+            return
+        if self.options.stop_sample is not None and self.sample_idx > self.options.stop_sample:
+            sys.exit(0)
+
+        ev_name = str(sample.evsel)
+        if self.options.verbose:
+            print(f"Event type: {ev_name}")
+            print_sample(sample)
+
+        dso = sample.dso or '[unknown]'
+        symbol = sample.symbol or '[unknown]'
+        dso_bid = (sample.dso_bid.decode('utf-8')
+                   if isinstance(sample.dso_bid, bytes)
+                   else str(sample.dso_bid or '[unknown]'))
+        dso_start = sample.map_start
+        dso_end = sample.map_end
+        map_pgoff = sample.map_pgoff or 0
+
+        comm = "[unknown]"
+        try:
+            if self.session:
+                thread_info = self.session.find_thread(sample.sample_tid)
+                if thread_info:
+                    comm = thread_info.comm() or "[unknown]"
+        except (TypeError, AttributeError):
+            pass
+
+        if dso == '[unknown]':
+            return
+
+        if dso_start is None or dso_end is None:
+            print(f"Failed to find valid dso map for dso {dso}")
+            return
+
+        if "instructions" in ev_name:
+            print_srccode(comm, sample, symbol, dso)
+            return
+
+        if "branches" not in ev_name:
+            return
+
+        self._process_branch(sample, comm, symbol, dso, dso_bid, dso_start, dso_end, map_pgoff)
+
+    def _process_branch(self, sample: perf.sample_event, comm: str, symbol: str, dso: str,
+                        dso_bid: str, dso_start: int, dso_end: int, map_pgoff: int) -> None:
+        """Helper to process branch events."""
+        cpu = sample.sample_cpu
+        ip = sample.sample_ip
+        addr = sample.sample_addr
+
+        if CPU_DATA.get(str(cpu) + 'addr') is None:
+            CPU_DATA[str(cpu) + 'addr'] = addr
+            return
+
+        start_addr = CPU_DATA[str(cpu) + 'addr']
+        stop_addr  = ip + 4
+
+        # Record for previous sample packet
+        CPU_DATA[str(cpu) + 'addr'] = addr
+
+        # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet
+        if start_addr == 0:
+            if stop_addr == 4 and self.options.verbose:
+                print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted")
+            return
+
+        if start_addr < dso_start or start_addr > dso_end:
+            print(f"Start address {start_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if stop_addr < dso_start or stop_addr > dso_end:
+            print(f"Stop address {stop_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if self.options.objdump is not None:
+            if dso == "[kernel.kallsyms]":
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            elif dso_start == 0x400000:
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            else:
+                dso_vm_start = dso_start
+                map_pgoff_local = map_pgoff
+
+            dso_fname = get_dso_file_path(dso, dso_bid, self.options.vmlinux)
+            if path.exists(dso_fname):
+                print_disam(dso_fname, dso_vm_start, start_addr + map_pgoff_local,
+                            stop_addr + map_pgoff_local, self.options.objdump)
+            else:
+                print(f"Failed to find dso {dso} for address range [ "
+                      f"{start_addr + map_pgoff_local:#x} .. {stop_addr + map_pgoff_local:#x} ]")
+
+        print_srccode(comm, sample, symbol, dso)
+
+    def run(self) -> None:
+        """Run the trace disassembly session."""
+        input_file = self.options.input or "perf.data"
+        if input_file != "-" and not os.path.exists(input_file):
+            print(f"Error: {input_file} not found.", file=sys.stderr)
+            sys.exit(1)
+
+        print('ARM CoreSight Trace Data Assembler Dump')
+        try:
+            self.session = perf.session(
+                perf.data(input_file),
+                sample=self.process_event,
+                itrace=self.options.itrace
+            )
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+            print(f"Error opening session: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        try:
+            self.session.process_events()
+        finally:
+            self.session = None
+        print('End')
+
+if __name__ == "__main__":
+    def int_arg(v: str) -> int:
+        """Helper for integer command line arguments."""
+        val = int(v)
+        if val < 0:
+            raise argparse.ArgumentTypeError("Argument must be a positive integer")
+        return val
+
+    arg_parser = argparse.ArgumentParser(description="ARM CoreSight Trace Dump With Disassembler")
+    arg_parser.add_argument("-i", "--input", help="input perf.data file")
+    arg_parser.add_argument("-k", "--vmlinux",
+                            help="Set path to vmlinux file. Omit to autodetect")
+    arg_parser.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
+                            help="Show disassembly. Can also be used to change the objdump path")
+    arg_parser.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
+    arg_parser.add_argument("--start-time", type=int_arg,
+                            help="Monotonic clock time of sample to start from.")
+    arg_parser.add_argument("--stop-time", type=int_arg,
+                            help="Monotonic clock time of sample to stop at.")
+    arg_parser.add_argument("--itrace", default="b",
+                            help="Instruction tracing options.")
+    arg_parser.add_argument("--start-sample", type=int_arg,
+                            help="Index of sample to start from.")
+    arg_parser.add_argument("--stop-sample", type=int_arg,
+                            help="Index of sample to stop at.")
+
+    parsed_options = arg_parser.parse_args()
+    if (parsed_options.start_time is not None and parsed_options.stop_time is not None and
+            parsed_options.start_time >= parsed_options.stop_time):
+        print("--start-time must less than --stop-time")
+        sys.exit(2)
+    if (parsed_options.start_sample is not None and parsed_options.stop_sample is not None and
+            parsed_options.start_sample >= parsed_options.stop_sample):
+        print("--start-sample must less than --stop-sample")
+        sys.exit(2)
+
+    td = TraceDisasm(parsed_options)
+    td.run()
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +18 -6
index f3ebad596378..51bcbd78f460 100755
@@ -1,6 +1,6 @@
 #!/bin/bash
-# Check Arm CoreSight disassembly script completes without errors (exclusive)
 # SPDX-License-Identifier: GPL-2.0
+# Check Arm CoreSight disassembly script completes without errors (exclusive)
 
 # The disassembly script reconstructs ranges of instructions and gives these to objdump to
 # decode. objdump doesn't like ranges that go backwards, but these are a good indication
@@ -22,9 +22,21 @@ glb_err=1
 
 perfdata_dir=$(mktemp -d /tmp/__perf_test.perf.data.XXXXX)
 perfdata=${perfdata_dir}/perf.data
+perfdata2=${perfdata_dir}/perf2.data
 file=$(mktemp /tmp/temporary_file.XXXXX)
 # Relative path works whether it's installed or running from repo
-script_path=$(dirname "$0")/../../../scripts/python/arm-cs-trace-disasm.py
+if [ -n "$PERF_EXEC_PATH" ] && [ -e "$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py" ]; then
+	script_path="$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py"
+else
+	script_path=$(dirname "$0")/../../../python/arm-cs-trace-disasm.py
+fi
+
+# shellcheck source=lib/setup_python.sh
+. "$(dirname "$0")"/../lib/setup_python.sh
+$PYTHON -c "import perf" 2>/dev/null || {
+	echo "Skipping test, perf python module not found"
+	exit 2
+}
 
 cleanup_files()
 {
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
 if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
 	echo "Testing kernel disassembly"
 	perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-	perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-		-d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+	$PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+		-k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
 	grep -q -E ${branch_search} ${file}
 	echo "Found kernel branches"
 else
@@ -56,8 +68,8 @@ fi
 ## Test user ##
 echo "Testing userspace disassembly"
 perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-	-d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}
 grep -q -E ${branch_search} ${file}
 echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +4 -0
index 2173215a0517..220d9663f81a 100644
@@ -18,6 +18,10 @@ fi
 # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
 if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
   PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"
 elif [ -d "$(dirname "$0")/../../python" ]; then
   PYTHONPATH_DIR="$(dirname "$0")/../../python"
 elif [ -d "$(dirname "$0")/../python" ]; then
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#187 lore raw

PATCH v2 39/49 perf python: Port powerpc-hcalls to perf module

In reply to Ian Rogers

Port powerpc-hcalls.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/:
- Refactor the script into an HCallAnalyzer class with full type annotations to encapsulate per-CPU hypervisor call entry/exit state.
- Use perf.session for event processing to track hypervisor call entry and exit timestamps and aggregate min/max/average duration statistics against HCALL_TABLE.
- Add argparse CLI support (-i/--input) and remove Python 2 compatibility code.

Add a shell test (test_powerpc_hcalls_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/powerpc-hcalls.py           | 330 ++++++++++++++++++
 .../tests/shell/test_powerpc_hcalls_python.sh |  95 +++++
 2 files changed, 425 insertions(+)
 create mode 100755 tools/perf/python/powerpc-hcalls.py
 create mode 100755 tools/perf/tests/shell/test_powerpc_hcalls_python.sh
tools/perf/python/powerpc-hcalls.py new file +330 -0
new file mode 100755
index 000000000000..4c354556c0fb
@@ -0,0 +1,330 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0+
+"""
+Hypervisor call statistics
+
+Copyright (C) 2018 Ravi Bangoria, IBM Corporation
+Ported from tools/perf/scripts/python/powerpc-hcalls.py
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import sys
+import perf
+
+# Hypervisor call table
+HCALL_TABLE = {
+	4: 'H_REMOVE',
+	8: 'H_ENTER',
+	12: 'H_READ',
+	16: 'H_CLEAR_MOD',
+	20: 'H_CLEAR_REF',
+	24: 'H_PROTECT',
+	28: 'H_GET_TCE',
+	32: 'H_PUT_TCE',
+	36: 'H_SET_SPRG0',
+	40: 'H_SET_DABR',
+	44: 'H_PAGE_INIT',
+	48: 'H_SET_ASR',
+	52: 'H_ASR_ON',
+	56: 'H_ASR_OFF',
+	60: 'H_LOGICAL_CI_LOAD',
+	64: 'H_LOGICAL_CI_STORE',
+	68: 'H_LOGICAL_CACHE_LOAD',
+	72: 'H_LOGICAL_CACHE_STORE',
+	76: 'H_LOGICAL_ICBI',
+	80: 'H_LOGICAL_DCBF',
+	84: 'H_GET_TERM_CHAR',
+	88: 'H_PUT_TERM_CHAR',
+	92: 'H_REAL_TO_LOGICAL',
+	96: 'H_HYPERVISOR_DATA',
+	100: 'H_EOI',
+	104: 'H_CPPR',
+	108: 'H_IPI',
+	112: 'H_IPOLL',
+	116: 'H_XIRR',
+	120: 'H_MIGRATE_DMA',
+	124: 'H_PERFMON',
+	220: 'H_REGISTER_VPA',
+	224: 'H_CEDE',
+	228: 'H_CONFER',
+	232: 'H_PROD',
+	236: 'H_GET_PPP',
+	240: 'H_SET_PPP',
+	244: 'H_PURR',
+	248: 'H_PIC',
+	252: 'H_REG_CRQ',
+	256: 'H_FREE_CRQ',
+	260: 'H_VIO_SIGNAL',
+	264: 'H_SEND_CRQ',
+	272: 'H_COPY_RDMA',
+	276: 'H_REGISTER_LOGICAL_LAN',
+	280: 'H_FREE_LOGICAL_LAN',
+	284: 'H_ADD_LOGICAL_LAN_BUFFER',
+	288: 'H_SEND_LOGICAL_LAN',
+	292: 'H_BULK_REMOVE',
+	304: 'H_MULTICAST_CTRL',
+	308: 'H_SET_XDABR',
+	312: 'H_STUFF_TCE',
+	316: 'H_PUT_TCE_INDIRECT',
+	332: 'H_CHANGE_LOGICAL_LAN_MAC',
+	336: 'H_VTERM_PARTNER_INFO',
+	340: 'H_REGISTER_VTERM',
+	344: 'H_FREE_VTERM',
+	348: 'H_RESET_EVENTS',
+	352: 'H_ALLOC_RESOURCE',
+	356: 'H_FREE_RESOURCE',
+	360: 'H_MODIFY_QP',
+	364: 'H_QUERY_QP',
+	368: 'H_REREGISTER_PMR',
+	372: 'H_REGISTER_SMR',
+	376: 'H_QUERY_MR',
+	380: 'H_QUERY_MW',
+	384: 'H_QUERY_HCA',
+	388: 'H_QUERY_PORT',
+	392: 'H_MODIFY_PORT',
+	396: 'H_DEFINE_AQP1',
+	400: 'H_GET_TRACE_BUFFER',
+	404: 'H_DEFINE_AQP0',
+	408: 'H_RESIZE_MR',
+	412: 'H_ATTACH_MCQP',
+	416: 'H_DETACH_MCQP',
+	420: 'H_CREATE_RPT',
+	424: 'H_REMOVE_RPT',
+	428: 'H_REGISTER_RPAGES',
+	432: 'H_DISABLE_AND_GET',
+	436: 'H_ERROR_DATA',
+	440: 'H_GET_HCA_INFO',
+	444: 'H_GET_PERF_COUNT',
+	448: 'H_MANAGE_TRACE',
+	456: 'H_GET_CPU_CHARACTERISTICS',
+	468: 'H_FREE_LOGICAL_LAN_BUFFER',
+	472: 'H_POLL_PENDING',
+	484: 'H_QUERY_INT_STATE',
+	580: 'H_ILLAN_ATTRIBUTES',
+	584: 'H_ADD_LOGICAL_LAN_BUFFERS',
+	592: 'H_MODIFY_HEA_QP',
+	596: 'H_QUERY_HEA_QP',
+	600: 'H_QUERY_HEA',
+	604: 'H_QUERY_HEA_PORT',
+	608: 'H_MODIFY_HEA_PORT',
+	612: 'H_REG_BCMC',
+	616: 'H_DEREG_BCMC',
+	620: 'H_REGISTER_HEA_RPAGES',
+	624: 'H_DISABLE_AND_GET_HEA',
+	628: 'H_GET_HEA_INFO',
+	632: 'H_ALLOC_HEA_RESOURCE',
+	644: 'H_ADD_CONN',
+	648: 'H_DEL_CONN',
+	664: 'H_JOIN',
+	672: 'H_VASI_SIGNAL',
+	676: 'H_VASI_STATE',
+	680: 'H_VIOCTL',
+	688: 'H_ENABLE_CRQ',
+	696: 'H_GET_EM_PARMS',
+	720: 'H_SET_MPP',
+	724: 'H_GET_MPP',
+	732: 'H_REG_SUB_CRQ',
+	736: 'H_FREE_SUB_CRQ',
+	740: 'H_SEND_SUB_CRQ',
+	744: 'H_SEND_SUB_CRQ_INDIRECT',
+	748: 'H_HOME_NODE_ASSOCIATIVITY',
+	756: 'H_BEST_ENERGY',
+	764: 'H_XIRR_X',
+	768: 'H_RANDOM',
+	772: 'H_COP',
+	788: 'H_GET_MPP_X',
+	796: 'H_SET_MODE',
+	808: 'H_BLOCK_REMOVE',
+	856: 'H_CLEAR_HPT',
+	864: 'H_REQUEST_VMC',
+	876: 'H_RESIZE_HPT_PREPARE',
+	880: 'H_RESIZE_HPT_COMMIT',
+	892: 'H_REGISTER_PROC_TBL',
+	896: 'H_SIGNAL_SYS_RESET',
+	904: 'H_ALLOCATE_VAS_WINDOW',
+	908: 'H_MODIFY_VAS_WINDOW',
+	912: 'H_DEALLOCATE_VAS_WINDOW',
+	916: 'H_QUERY_VAS_WINDOW',
+	920: 'H_QUERY_VAS_CAPABILITIES',
+	924: 'H_QUERY_NX_CAPABILITIES',
+	928: 'H_GET_NX_FAULT',
+	936: 'H_INT_GET_SOURCE_INFO',
+	940: 'H_INT_SET_SOURCE_CONFIG',
+	944: 'H_INT_GET_SOURCE_CONFIG',
+	948: 'H_INT_GET_QUEUE_INFO',
+	952: 'H_INT_SET_QUEUE_CONFIG',
+	956: 'H_INT_GET_QUEUE_CONFIG',
+	960: 'H_INT_SET_OS_REPORTING_LINE',
+	964: 'H_INT_GET_OS_REPORTING_LINE',
+	968: 'H_INT_ESB',
+	972: 'H_INT_SYNC',
+	976: 'H_INT_RESET',
+	996: 'H_SCM_READ_METADATA',
+	1000: 'H_SCM_WRITE_METADATA',
+	1004: 'H_SCM_BIND_MEM',
+	1008: 'H_SCM_UNBIND_MEM',
+	1012: 'H_SCM_QUERY_BLOCK_MEM_BINDING',
+	1016: 'H_SCM_QUERY_LOGICAL_MEM_BINDING',
+	1020: 'H_SCM_UNBIND_ALL',
+	1024: 'H_SCM_HEALTH',
+	1048: 'H_SCM_PERFORMANCE_STATS',
+	1052: 'H_PKS_GET_CONFIG',
+	1056: 'H_PKS_SET_PASSWORD',
+	1060: 'H_PKS_GEN_PASSWORD',
+	1068: 'H_PKS_WRITE_OBJECT',
+	1072: 'H_PKS_GEN_KEY',
+	1076: 'H_PKS_READ_OBJECT',
+	1080: 'H_PKS_REMOVE_OBJECT',
+	1084: 'H_PKS_CONFIRM_OBJECT_FLUSHED',
+	1096: 'H_RPT_INVALIDATE',
+	1100: 'H_SCM_FLUSH',
+	1104: 'H_GET_ENERGY_SCALE_INFO',
+	1108: 'H_PKS_SIGNED_UPDATE',
+	1112: 'H_HTM',
+	1116: 'H_WATCHDOG',
+	# Platform specific hcalls used by KVM on PowerVM
+	1120: 'H_GUEST_GET_CAPABILITIES',
+	1124: 'H_GUEST_SET_CAPABILITIES',
+	1136: 'H_GUEST_CREATE',
+	1140: 'H_GUEST_CREATE_VCPU',
+	1144: 'H_GUEST_GET_STATE',
+	1148: 'H_GUEST_SET_STATE',
+	1152: 'H_GUEST_RUN_VCPU',
+	1156: 'H_GUEST_COPY_MEMORY',
+	1160: 'H_GUEST_DELETE',
+	# Key wrapping hcalls
+	1168: 'H_PKS_WRAP_OBJECT',
+	1172: 'H_PKS_UNWRAP_OBJECT',
+	# Platform-specific hcalls used by the Ultravisor
+	61184: 'H_SVM_PAGE_IN',
+	61188: 'H_SVM_PAGE_OUT',
+	61192: 'H_SVM_INIT_START',
+	61196: 'H_SVM_INIT_DONE',
+	61204: 'H_SVM_INIT_ABORT',
+	# Platform specific hcalls used by KVM
+	61440: 'H_RTAS',
+	# Platform specific hcalls used by QEMU/SLOF
+	61441: 'H_LOGICAL_MEMOP',
+	61442: 'H_CAS',
+	61443: 'H_UPDATE_DT',
+	# Platform specific hcalls provided by PHYP
+	61560: 'H_GET_24X7_CATALOG_PAGE',
+	61564: 'H_GET_24X7_DATA',
+	61568: 'H_GET_PERF_COUNTER_INFO',
+	# Platform-specific hcalls used for nested HV KVM
+	63488: 'H_SET_PARTITION_TABLE',
+	63492: 'H_ENTER_NESTED',
+	63496: 'H_TLB_INVALIDATE',
+	63500: 'H_COPY_TOFROM_GUEST',
+}
+
+
+class HCallAnalyzer:
+    """Analyzes hypervisor calls and aggregates statistics."""
+
+    def __init__(self, cmd_args) -> None:
+        self.args = cmd_args
+        # output: {opcode: {'min': min, 'max': max, 'time': time, 'cnt': cnt}}
+        self.output: dict[int, dict[str, 'int | float']] = \
+            defaultdict(lambda: {'time': 0, 'cnt': 0, 'min': float('inf'), 'max': 0})
+        # d_enter: {cpu: {opcode: nsec}}
+        self.d_enter: dict[int, dict[int, int]] = {}
+        self.print_ptrn = '%-28s%10s%10s%10s%10s'
+
+    def hcall_table_lookup(self, opcode: int) -> str:
+        """Lookup hcall name by opcode."""
+        return HCALL_TABLE.get(opcode, str(opcode))
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process a single sample event."""
+        name = str(sample.evsel)
+        if name not in ("evsel(powerpc:hcall_entry)", "evsel(powerpc:hcall_exit)"):
+            return
+
+        try:
+            cpu = sample.sample_cpu
+            opcode = sample.opcode
+        except AttributeError:
+            print("ERROR: tracepoint fields missed (is libtraceevent enabled?)",
+                  file=sys.stderr)
+            sys.exit(1)
+
+        # sample.sample_time represents the nsecs
+        time = sample.sample_time
+
+        if opcode < 0 or cpu < 0:
+            return
+
+        if name == "evsel(powerpc:hcall_entry)":
+            if cpu not in self.d_enter:
+                self.d_enter[cpu] = {}
+            self.d_enter[cpu][opcode] = time
+        elif name == "evsel(powerpc:hcall_exit)":
+            if cpu in self.d_enter and opcode in self.d_enter[cpu]:
+                time_entry = self.d_enter[cpu][opcode]
+                diff = time - time_entry
+                del self.d_enter[cpu][opcode]
+
+                stats = self.output[opcode]
+                stats['time'] += diff
+                stats['cnt'] += 1
+                if diff < stats['min']:
+                    stats['min'] = diff
+                if diff > stats['max']:
+                    stats['max'] = diff
+
+    def print_summary(self) -> None:
+        """Print aggregated statistics."""
+        print(self.print_ptrn % ('hcall', 'count', 'min(ns)', 'max(ns)', 'avg(ns)'))
+        print('-' * 68)
+
+        def sort_output(opcode):
+            stats = self.output[opcode]
+            sort_by = getattr(self.args, 'sort', None)
+            if sort_by == 'min':
+                return stats['min']
+            if sort_by == 'max':
+                return stats['max']
+            if sort_by == 'avg':
+                return stats['time'] // stats['cnt']
+            return stats['cnt']
+
+        for opcode in sorted(self.output.keys(), key=sort_output, reverse=True):
+            h_name = self.hcall_table_lookup(opcode)
+            stats = self.output[opcode]
+            time = stats['time']
+            cnt = stats['cnt']
+            min_t = stats['min']
+            max_t = stats['max']
+
+            # Avoid float representation for large integers if possible,
+            # or use formatted strings. Legacy used time//cnt.
+            avg_t = time // cnt if cnt > 0 else 0
+
+            # If min was not updated, it remains inf, but cnt should be > 0 if in output
+            if min_t == float('inf'):
+                min_t = 0
+
+            print(self.print_ptrn % (h_name, cnt, int(min_t), int(max_t), avg_t))
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(description="Hypervisor call statistics")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("-s", "--sort", default="count",
+                    choices=["count", "min", "max", "avg"], help="Sort key")
+    args = ap.parse_args()
+
+    analyzer = HCallAnalyzer(args)
+
+    try:
+        session = perf.session(perf.data(args.input), sample=analyzer.process_event)
+        session.process_events()
+        analyzer.print_summary()
+    except KeyboardInterrupt:
+        analyzer.print_summary()
+    except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+        print(f"Error processing events: {e}")
+        sys.exit(1)
tools/perf/tests/shell/test_powerpc_hcalls_python.sh new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#189 lore raw

PATCH v2 40/49 perf python: Port intel-pt-events and libxed to perf module

In reply to Ian Rogers

Port intel-pt-events.py and libxed.py from tools/perf/scripts/python/ to standalone modules in tools/perf/python/:
- Refactor intel-pt-events.py into an IntelPTAnalyzer class with full type annotations to encapsulate trace state and stashed output.
- Configure instruction trace decoding directly via perf.session's itrace option and context_switch callback.
- Dynamically select 32-bit vs 64-bit disassembly mode in libxed.py using session.is_64_bit and annotate instructions with source lines via sample.srccode().
- Rename methods in libxed.py to snake_case (instruction, set_mode, disassemble_one) and remove Python 2 compatibility code.

Add a shell test (test_intel_pt_events_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/intel-pt-events.py          | 606 ++++++++++++++++++
 tools/perf/python/libxed.py                   | 123 ++++
 .../shell/test_intel_pt_events_python.sh      |  69 ++
 3 files changed, 798 insertions(+)
 create mode 100755 tools/perf/python/intel-pt-events.py
 create mode 100755 tools/perf/python/libxed.py
 create mode 100755 tools/perf/tests/shell/test_intel_pt_events_python.sh
tools/perf/python/intel-pt-events.py new file +606 -0
new file mode 100755
index 000000000000..f5c7d81b86b9
@@ -0,0 +1,606 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Print Intel PT Events including Power Events and PTWRITE.
+Ported from tools/perf/scripts/python/intel-pt-events.py
+"""
+from __future__ import annotations
+
+import argparse
+from typing import Dict, List, Tuple
+import contextlib
+from ctypes import addressof, create_string_buffer
+import io
+import os
+import struct
+import sys
+from typing import Any, Optional
+import typing
+import perf
+
+try:
+    from libxed import LibXED as _LibXED
+    LibXED: Optional[type[_LibXED]] = _LibXED
+except ImportError:
+    LibXED = None
+
+
+def sample_flags_to_name(flags: int) -> str:
+    """Implement perf's sample_flags_to_name."""
+    if not isinstance(flags, int) or flags == 0:
+        return "".ljust(21)
+    sample_flags = [
+        ((1 << 0) | (1 << 1), "call"),
+        ((1 << 0) | (1 << 2), "return"),
+        ((1 << 0) | (1 << 3), "jcc"),
+        ((1 << 0), "jmp"),
+        ((1 << 0) | (1 << 1) | (1 << 6), "int"),
+        ((1 << 0) | (1 << 2) | (1 << 6), "iret"),
+        ((1 << 0) | (1 << 1) | (1 << 4), "syscall"),
+        ((1 << 0) | (1 << 2) | (1 << 4), "sysret"),
+        ((1 << 0) | (1 << 5), "async"),
+        ((1 << 0) | (1 << 1) | (1 << 5) | (1 << 6), "hw int"),
+        ((1 << 0) | (1 << 7), "tx abrt"),
+        ((1 << 0) | (1 << 8), "tr strt"),
+        ((1 << 0) | (1 << 9), "tr end"),
+        ((1 << 0) | (1 << 1) | (1 << 11), "vmentry"),
+        ((1 << 0) | (1 << 1) | (1 << 12), "vmexit"),
+    ]
+    additional_mask = (1 << 10) | (1 << 13) | (1 << 14)
+    branch_event_mask = (1 << 15) | (1 << 16)
+    xf = flags & additional_mask
+    rem_flags = flags & ~additional_mask
+
+    if rem_flags & (1 << 8):
+        prefix = "tr strt "
+    elif rem_flags & (1 << 9):
+        prefix = "tr end  "
+    else:
+        prefix = ""
+
+    rem_flags &= ~((1 << 8) | (1 << 9))
+    types = rem_flags & ~branch_event_mask
+    type_name = ""
+    for f_mask, f_name in sample_flags:
+        if f_mask == types:
+            type_name = f_name
+            break
+
+    s = prefix + type_name
+    ev_parts = []
+    if rem_flags & (1 << 15):
+        ev_parts.append("miss")
+    if rem_flags & (1 << 16):
+        ev_parts.append("not_taken")
+    if ev_parts:
+        s += "/" + ",".join(ev_parts) + "/"
+
+    if xf:
+        xs = "("
+        if xf & (1 << 10):
+            xs += "x"
+        if xf & (1 << 13):
+            xs += "D"
+        if xf & (1 << 14):
+            xs += "t"
+        xs += ")"
+        if len(s) + len(xs) < 21:
+            s = s + xs.rjust(21 - len(s))
+        else:
+            s = s + " " + xs
+    return s.ljust(21)
+
+class IntelPTAnalyzer:
+    """Analyzes Intel PT events and prints details."""
+
+    def __init__(self, cfg: argparse.Namespace):
+        self.args = cfg
+        self.session: Optional[perf.session] = None
+        self.insn = False
+        self.src = False
+        self.source_file_name: Optional[str] = None
+        self.line_number: int = 0
+        self.dso: Optional[str] = None
+        self.stash_dict: Dict[int, List[str]] = {}
+        self.output: Any = None
+        self.output_pos: int = 0
+        self.cpu: int = -1
+        self.time: int = 0
+        self.switch_str: Dict[int, str] = {}
+
+        if cfg.insn_trace:
+            print("Intel PT Instruction Trace")
+            self.insn = True
+        elif cfg.src_trace:
+            print("Intel PT Source Trace")
+            self.insn = True
+            self.src = True
+        else:
+            print("Intel PT Branch Trace, Power Events, Event Trace and PTWRITE")
+
+        self.disassembler: Any = None
+        if self.insn and LibXED is not None:
+            try:
+                self.disassembler = LibXED()
+            except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError,
+                    ImportError) as e:
+                print(f"Failed to initialize LibXED: {e}")
+                self.disassembler = None
+
+    def print_ptwrite(self, raw_buf: bytes) -> None:
+        """Print PTWRITE data."""
+        if not raw_buf:
+            return
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+            flags = data[0]
+            payload = data[1]
+        except struct.error:
+            return
+        exact_ip = flags & 1
+        try:
+            s = payload.to_bytes(8, "little").decode("ascii").rstrip("\x00")
+            if not s.isprintable():
+                s = ""
+        except (UnicodeDecodeError, ValueError):
+            s = ""
+        print(f"IP: {exact_ip} payload: {payload:#x} {s}", end=' ')
+
+    def print_cbr(self, raw_buf: bytes) -> None:
+        """Print CBR data."""
+        if len(raw_buf) < 12:
+            return
+        try:
+            data = struct.unpack_from("<BBBBII", raw_buf)
+        except struct.error:
+            return
+        cbr = data[0]
+        f = (data[4] + 500) // 1000
+        if data[2] == 0:
+            return
+        p = ((cbr * 1000 // data[2]) + 5) // 10
+        print(f"{cbr:3d}  freq: {f:4d} MHz  ({p:3d}%)", end=' ')
+
+    def print_mwait(self, raw_buf: bytes) -> None:
+        """Print MWAIT data."""
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+        except struct.error:
+            return
+        payload = data[1]
+        hints = payload & 0xff
+        extensions = (payload >> 32) & 0x3
+        print(f"hints: {hints:#x} extensions: {extensions:#x}", end=' ')
+
+    def print_pwre(self, raw_buf: bytes) -> None:
+        """Print PWRE data."""
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+        except struct.error:
+            return
+        payload = data[1]
+        hw = (payload >> 7) & 1
+        cstate = (payload >> 12) & 0xf
+        subcstate = (payload >> 8) & 0xf
+        print(f"hw: {hw} cstate: {cstate} sub-cstate: {subcstate}", end=' ')
+
+    def print_exstop(self, raw_buf: bytes) -> None:
+        """Print EXSTOP data."""
+        try:
+            data = struct.unpack_from("<I", raw_buf)
+        except struct.error:
+            return
+        flags = data[0]
+        exact_ip = flags & 1
+        print(f"IP: {exact_ip}", end=' ')
+
+    def print_pwrx(self, raw_buf: bytes) -> None:
+        """Print PWRX data."""
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+        except struct.error:
+            return
+        payload = data[1]
+        deepest_cstate = payload & 0xf
+        last_cstate = (payload >> 4) & 0xf
+        wake_reason = (payload >> 8) & 0xf
+        print(f"deepest cstate: {deepest_cstate} last cstate: {last_cstate} "
+              f"wake reason: {wake_reason:#x}", end=' ')
+
+    def print_psb(self, raw_buf: bytes) -> None:
+        """Print PSB data."""
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+        except struct.error:
+            return
+        offset = data[1]
+        print(f"offset: {offset:#x}", end=' ')
+
+    def print_evt(self, raw_buf: bytes) -> None:
+        """Print EVT data."""
+        glb_cfe = ["", "INTR", "IRET", "SMI", "RSM", "SIPI", "INIT", "VMENTRY", "VMEXIT",
+                   "VMEXIT_INTR", "SHUTDOWN", "", "UINT", "UIRET"] + [""] * 18
+        glb_evd = ["", "PFA", "VMXQ", "VMXR"] + [""] * 60
+
+        try:
+            data = struct.unpack_from("<BBH", raw_buf)
+        except struct.error:
+            return
+        typ = data[0] & 0x1f
+        ip_flag = (data[0] & 0x80) >> 7
+        vector = data[1]
+        evd_cnt = data[2]
+        s = glb_cfe[typ]
+        if s:
+            print(f" cfe: {s} IP: {ip_flag} vector: {vector}", end=' ')
+        else:
+            print(f" cfe: {typ} IP: {ip_flag} vector: {vector}", end=' ')
+        pos = 4
+        for _ in range(evd_cnt):
+            if len(raw_buf) < pos + 16:
+                break
+            try:
+                data = struct.unpack_from("<QQ", raw_buf, pos)
+            except struct.error:
+                return
+            et = data[0] & 0x3f
+            s = glb_evd[et]
+            if s:
+                print(f"{s}: {data[1]:#x}", end=' ')
+            else:
+                print(f"EVD_{et}: {data[1]:#x}", end=' ')
+            pos += 16
+
+    def print_iflag(self, raw_buf: bytes) -> None:
+        """Print IFLAG data."""
+        try:
+            data = struct.unpack_from("<IQ", raw_buf)
+        except struct.error:
+            return
+        iflag = data[0] & 1
+        old_iflag = iflag ^ 1
+        via_branch = data[0] & 2
+        s = "via" if via_branch else "non"
+        print(f"IFLAG: {old_iflag}->{iflag} {s} branch", end=' ')
+
+    def common_start_str(self, comm: str, sample: perf.sample_event) -> str:
+        """Return common start string for display."""
+        ts = sample.sample_time
+        cpu = sample.sample_cpu
+        pid = sample.sample_pid
+        tid = sample.sample_tid
+        machine_pid = getattr(sample, "machine_pid", 0)
+        if machine_pid:
+            vcpu = getattr(sample, "vcpu", -1)
+            return (f"VM:{machine_pid:5d} VCPU:{vcpu:03d} {comm:>16s} {pid:5d}/{tid:<5d} "
+                    f"[{cpu:03d}] {ts // 1000000000:9d}.{ts % 1000000000:09d}  ")
+        return (f"{comm:>16s} {pid:5d}/{tid:<5d} [{cpu:03d}] "
+                f"{ts // 1000000000:9d}.{ts % 1000000000:09d}  ")
+
+    def print_common_start(self, comm: str, sample: perf.sample_event, name: str) -> None:
+        """Print common start info."""
+        flags_disp = sample_flags_to_name(getattr(sample, "flags", 0))
+        print(self.common_start_str(comm, sample) + f"{name:>8s}  {flags_disp:>21s}", end=' ')
+
+    def print_instructions_start(self, comm: str, sample: perf.sample_event) -> None:
+        """Print instructions start info."""
+        raw_flags = getattr(sample, "flags", 0)
+        if isinstance(raw_flags, int) and (raw_flags & (1 << 10)):
+            print(self.common_start_str(comm, sample) + "x", end=' ')
+        else:
+            print(self.common_start_str(comm, sample), end='  ')
+
+    def disassem(self, insn: bytes, ip: int) -> Tuple[int, str]:
+        """Disassemble instruction using LibXED."""
+        inst = self.disassembler.instruction()
+        is_64_bit = getattr(self.session, "is_64_bit", True) if self.session else True
+        self.disassembler.set_mode(inst, 0 if is_64_bit else 1)
+        buf = create_string_buffer(insn, 64)
+        return self.disassembler.disassemble_one(inst, addressof(buf), len(insn), ip)
+
+    def print_common_ip(self, sample: perf.sample_event, symbol: str, dso: str) -> None:
+        """Print IP and symbol info."""
+        ip = sample.sample_ip
+        offs = f"+{sample.sym_offset:#x}" if getattr(sample, "sym_offset", None) is not None else ""
+        cyc_cnt = getattr(sample, "sample_cyc_count", 0)
+        if cyc_cnt:
+            insn_cnt = getattr(sample, "sample_insn_count", 0)
+            ipc_str = f"  IPC: {insn_cnt / cyc_cnt:#.2f} ({insn_cnt}/{cyc_cnt})"
+        else:
+            ipc_str = ""
+
+        if self.insn and self.disassembler is not None:
+            try:
+                insn = sample.insn()
+            except AttributeError:
+                insn = None
+            if insn:
+                cnt, text = self.disassem(insn, ip)
+                byte_str = (f"{ip:x}").rjust(16)
+                for k in range(cnt):
+                    byte_str += f" {insn[k]:02x}"
+                print(f"{byte_str:<40s}  {text:<30s}", end=' ')
+            else:
+                print(f"{ip:16x}", end=' ')
+            print(f"{symbol}{offs} ({dso})", end=' ')
+        else:
+            print(f"{ip:16x} {symbol}{offs} ({dso})", end=' ')
+
+        addr_correlates_sym = getattr(sample, "addr_symbol", None) is not None
+        if addr_correlates_sym:
+            addr = getattr(sample, "sample_addr", getattr(sample, "addr", 0))
+            addr_dso = (sample.addr_dso or '[unknown]')
+            addr_symbol = (sample.addr_symbol or '[unknown]')
+            addr_offs = (f"+{sample.addr_sym_offset:#x}"
+                         if getattr(sample, "addr_sym_offset", None) is not None else "")
+            print(f"=> {addr:x} {addr_symbol}{addr_offs} ({addr_dso}){ipc_str}")
+        else:
+            print(ipc_str)
+
+    def print_srccode(self, comm: str, sample: perf.sample_event,
+                      symbol: str, dso: str, with_insn: bool) -> None:
+        """Print source code info."""
+        ip = sample.sample_ip
+        if symbol == "[unknown]":
+            start_str = self.common_start_str(comm, sample) + (f"{ip:x}").rjust(16).ljust(40)
+        else:
+            offs = (f"+{sample.sym_offset:#x}"
+                    if getattr(sample, "sym_offset", None) is not None else "")
+            start_str = self.common_start_str(comm, sample) + (symbol + offs).ljust(40)
+
+        if with_insn and self.insn and self.disassembler is not None:
+            try:
+                insn = sample.insn()
+            except AttributeError:
+                insn = None
+            if insn:
+                _, text = self.disassem(insn, ip)
+                start_str += text.ljust(30)
+
+        source_file_name: typing.Any = None
+        line_number: typing.Any = 0
+        source_line: typing.Any = None
+        try:
+            res_srcc = sample.srccode()
+            if res_srcc:
+                source_file_name, line_number, source_line = res_srcc
+        except (AttributeError, ValueError, TypeError):
+            pass
+
+        if source_file_name:
+            if self.line_number == line_number and self.source_file_name == source_file_name:
+                src_str = ""
+            else:
+                if len(source_file_name) > 40:
+                    src_file = ("..." + source_file_name[-37:]) + " "
+                else:
+                    src_file = source_file_name.ljust(41)
+                if source_line is None:
+                    src_str = src_file + str(line_number).rjust(4) + " <source not found>"
+                else:
+                    src_str = src_file + str(line_number).rjust(4) + " " + source_line
+            self.dso = None
+        elif dso == self.dso:
+            src_str = ""
+        else:
+            src_str = dso
+            self.dso = dso
+
+        self.line_number = line_number
+        self.source_file_name = source_file_name
+        print(start_str, src_str)
+
+    def do_process_event(self, sample: perf.sample_event) -> None:
+        """Process event and print info."""
+        cpu = getattr(sample, "sample_cpu", getattr(sample, "cpu", 0))
+        if cpu in self.switch_str:
+            print(self.switch_str[cpu])
+            del self.switch_str[cpu]
+
+        comm = "Unknown"
+        if hasattr(self, 'session') and self.session:
+            try:
+                thread = self.session.find_thread(sample.sample_pid, sample.sample_tid)
+                if thread:
+                    comm = thread.comm() or "Unknown"
+            except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+                pass
+        # Python < 3.9 compatibility
+        name = str(sample.evsel)
+        if name.startswith("evsel("):
+            name = name[6:-1]
+        dso = (sample.dso or '[unknown]')
+        symbol = (sample.symbol or '[unknown]')
+
+
+        raw_buf = getattr(sample, 'raw_buf', b'') or b''
+
+        if name.startswith("instructions"):
+            if self.src:
+                self.print_srccode(comm, sample, symbol, dso, True)
+            else:
+                self.print_instructions_start(comm, sample)
+                self.print_common_ip(sample, symbol, dso)
+        elif name.startswith("branches"):
+            if self.src:
+                self.print_srccode(comm, sample, symbol, dso, False)
+            else:
+                self.print_common_start(comm, sample, name)
+                self.print_common_ip(sample, symbol, dso)
+        elif name == "ptwrite":
+            self.print_common_start(comm, sample, name)
+            self.print_ptwrite(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "cbr":
+            self.print_common_start(comm, sample, name)
+            self.print_cbr(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "mwait":
+            self.print_common_start(comm, sample, name)
+            self.print_mwait(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "pwre":
+            self.print_common_start(comm, sample, name)
+            self.print_pwre(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "exstop":
+            self.print_common_start(comm, sample, name)
+            self.print_exstop(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "pwrx":
+            self.print_common_start(comm, sample, name)
+            self.print_pwrx(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "psb":
+            self.print_common_start(comm, sample, name)
+            self.print_psb(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "evt":
+            self.print_common_start(comm, sample, name)
+            self.print_evt(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        elif name == "iflag":
+            self.print_common_start(comm, sample, name)
+            self.print_iflag(raw_buf)
+            self.print_common_ip(sample, symbol, dso)
+        else:
+            self.print_common_start(comm, sample, name)
+            self.print_common_ip(sample, symbol, dso)
+
+    def interleave_events(self, sample: perf.sample_event) -> None:
+        """Interleave output to avoid garbled lines from different CPUs."""
+        self.cpu = sample.sample_cpu
+        ts = sample.sample_time
+
+        if self.time != ts:
+            self.time = ts
+            self.flush_stashed_output()
+
+        self.output_pos = 0
+        with contextlib.redirect_stdout(io.StringIO()) as self.output:
+            self.do_process_event(sample)
+
+        self.stash_output()
+
+    def stash_output(self) -> None:
+        """Stash output for later flushing."""
+        output_str = self.output.getvalue()[self.output_pos:]
+        n = len(output_str)
+        if n:
+            self.output_pos += n
+            if self.cpu not in self.stash_dict:
+                self.stash_dict[self.cpu] = []
+            self.stash_dict[self.cpu].append(output_str)
+            if len(self.stash_dict[self.cpu]) > 1000:
+                self.flush_stashed_output()
+
+    def flush_stashed_output(self) -> None:
+        """Flush stashed output."""
+        while self.stash_dict:
+            cpus = list(self.stash_dict.keys())
+            for cpu in cpus:
+                items = self.stash_dict[cpu]
+                countdown = self.args.interleave
+                while len(items) and countdown:
+                    sys.stdout.write(items[0])
+                    del items[0]
+                    countdown -= 1
+                if not items:
+                    del self.stash_dict[cpu]
+
+    def process_context_switch(self, event: perf.switch_event) -> None:
+        """Process context switch."""
+        cpu = getattr(event, "sample_cpu", getattr(event, "cpu", 0))
+        pid = getattr(event, "sample_pid", getattr(event, "pid", 0))
+        tid = getattr(event, "sample_tid", getattr(event, "tid", 0))
+        ts = getattr(event, "sample_time", getattr(event, "time", 0))
+        np_pid = getattr(event, "next_prev_pid", None)
+        np_tid = getattr(event, "next_prev_tid", None)
+        machine_pid = getattr(event, "machine_pid", -1)
+        vcpu = getattr(event, "vcpu", -1)
+        misc = getattr(event, "misc", 0)
+        out = bool(misc & (1 << 13))
+        out_preempt = bool(misc & (1 << 14))
+
+        if self.args.interleave:
+            self.flush_stashed_output()
+
+        if out:
+            out_str = "Switch out "
+        else:
+            out_str = "Switch In  "
+
+        preempt_str = "preempt" if out_preempt else ""
+
+        if machine_pid == -1:
+            machine_str = ""
+        elif vcpu == -1:
+            machine_str = f"machine PID {machine_pid}"
+        else:
+            machine_str = f"machine PID {machine_pid} VCPU {vcpu}"
+
+        np_str = f"{np_pid:5d}/{np_tid:<5d} " if np_pid is not None and np_tid is not None else ""
+        c_str = (f"{out_str:>16s} {pid:5d}/{tid:<5d} [{cpu:03d}] "
+                 f"{ts // 1000000000:9d}.{ts % 1000000000:09d} "
+                 f"{np_str}{machine_str} {preempt_str}")
+
+        if self.args.all_switch_events:
+            print(c_str)
+        else:
+            self.switch_str[cpu] = c_str
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Wrapper to handle interleaving and exceptions."""
+        try:
+            if self.args.interleave:
+                self.interleave_events(sample)
+            else:
+                self.do_process_event(sample)
+        except BrokenPipeError:
+            # Stop python printing broken pipe errors and traceback
+            sys.stdout = open(os.devnull, 'w', encoding='utf-8')
+            sys.exit(1)
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser()
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("--insn-trace", action='store_true')
+    ap.add_argument("--src-trace", action='store_true')
+    ap.add_argument("--all-switch-events", action='store_true')
+    ap.add_argument("--interleave", type=int, nargs='?', const=4, default=0)
+    ap.add_argument("--itrace", default=None, help="itrace options")
+    args = ap.parse_args()
+
+    if args.itrace is None:
+        if args.insn_trace or args.src_trace:
+            args.itrace = "i0nsepwxI"
+        else:
+            args.itrace = "bepwxI"
+    analyzer = IntelPTAnalyzer(args)
+
+    try:
+        # Note: Python API currently lacks auxtrace_error callbacks affecting
+        # chronological interleaving
+        session = perf.session(
+            perf.data(args.input),
+            sample=analyzer.process_event,
+            context_switch=analyzer.process_context_switch,
+            itrace=args.itrace
+        )
+        analyzer.session = session
+        session.process_events()
+        if args.interleave:
+            analyzer.flush_stashed_output()
+        print("End")
+    except KeyboardInterrupt:
+        if args.interleave:
+            analyzer.flush_stashed_output()
+        print("End")
+    except BrokenPipeError:
+        sys.exit(0)
+    except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+        import traceback
+        traceback.print_exc()
+        sys.exit(1)
+
+
tools/perf/python/libxed.py new file +123 -0

123 lines not on this page · raw message

tools/perf/tests/shell/test_intel_pt_events_python.sh new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#191 lore raw

PATCH v2 41/49 perf test: Migrate Intel PT virtual LBR test to Python API

In reply to Ian Rogers

Migrate the Intel PT virtual LBR test from generating an inline legacy perf script callback to using a standalone Python script (perf_brstack_max.py) with the brstack iterator API in the perf Python module.

Assisted-by Antigravity:gemini-3.1-pro
---
 .../perf/tests/shell/lib/perf_brstack_max.py  | 37 +++++++++++++++++
 tools/perf/tests/shell/test_intel_pt.sh       | 41 ++++++++-----------
 2 files changed, 54 insertions(+), 24 deletions(-)
 create mode 100644 tools/perf/tests/shell/lib/perf_brstack_max.py
tools/perf/tests/shell/lib/perf_brstack_max.py new file +37 -0
new file mode 100644
index 000000000000..e2f5362de5e6
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+# SPDX-License-Identifier: GPL-2.0
+# Determine the maximum size of branch stacks in a perf.data file.
+
+import argparse
+import sys
+
+import perf
+
+def main():
+    ap = argparse.ArgumentParser()
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    args = ap.parse_args()
+
+    bmax = 0
+
+    def process_event(sample):
+        nonlocal bmax
+        try:
+            brstack = sample.brstack
+            if brstack:
+                n = len(list(brstack))
+                if n > bmax:
+                    bmax = n
+        except AttributeError:
+            pass
+
+    try:
+        session = perf.session(perf.data(args.input), sample=process_event)
+        session.process_events()
+        print("max brstack", bmax)
+    except Exception as e:
+        print(f"Error processing events: {e}", file=sys.stderr)
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_intel_pt.sh +17 -24

64 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#193 lore raw

PATCH v2 42/49 perf python: Port export-to-sqlite to perf module

In reply to Ian Rogers

Port export-to-sqlite.py to a standalone script in tools/perf/python/ using the perf module and Python's standard library sqlite3 module.

Improvements compared to the legacy script:
- Remove the dependency on PySide/QtSql by using Python's built-in sqlite3 module in DatabaseExporter, allowing SQLite export on headless and minimal systems without Qt installed.
- Support Intel PT and hardware instruction trace export via perf.session(itrace=...) and perf.call_return callbacks, reconstructing relational call_paths and calls tables and decoding synthesized PT payloads (ptwrite, cbr, mwait, pwre, exstop, pwrx).
- Export context_switches via perf.session's context_switch callback and map thread and comm IDs to their relational database keys.
- Manage temporary staging files inside an isolated tempfile.mkdtemp() directory with guaranteed cleanup in a finally block.

Update Documentation/db-export.txt and add a shell test (test_export_to_sqlite_python.sh) to verify the standalone exporter.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Documentation/db-export.txt        |   2 +-
 tools/perf/python/export-to-sqlite.py         | 937 ++++++++++++++++++
 .../shell/test_export_to_sqlite_python.sh     | 108 ++
 3 files changed, 1046 insertions(+), 1 deletion(-)
 create mode 100755 tools/perf/python/export-to-sqlite.py
 create mode 100755 tools/perf/tests/shell/test_export_to_sqlite_python.sh
tools/perf/Documentation/db-export.txt +1 -1
index 52ffccb02d55..b43f12b96973 100644
@@ -7,7 +7,7 @@ perf tool's python scripting engine:
 
 supports scripts:
 
-	tools/perf/scripts/python/export-to-sqlite.py
+	tools/perf/python/export-to-sqlite.py
 	tools/perf/scripts/python/export-to-postgresql.py
 
 which export data to a SQLite3 or PostgreSQL database.
tools/perf/python/export-to-sqlite.py new file +937 -0

937 lines not on this page · raw message

tools/perf/tests/shell/test_export_to_sqlite_python.sh new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#195 lore raw

PATCH v2 43/49 perf python: Port export-to-postgresql to perf module

In reply to Ian Rogers

Port export-to-postgresql.py to a standalone script in tools/perf/python/ using the perf module and libpq via ctypes.

Improvements compared to the legacy script:
- Remove the dependency on PySide/QtSql for database creation and DDL execution by driving libpq directly via ctypes (PQconnectdb, PQexec, PQputCopyData, PQputCopyEnd) and streaming binary PostgreSQL COPY files in PostgresExporter, enabling export on headless servers without Qt installed.
- Harden database connection and SQL identifier handling by rejecting URI ('://') and connection-parameter ('=') injection strings in connect(), escaping single quotes and backslashes in connection strings, and quoting SQL identifiers (quote_ident).
- Support Intel PT and instruction trace export via perf.session(itrace=...) and perf.call_return callbacks, reconstructing relational call_paths and calls tables (including id=0 placeholder rows and callfk/returnfk foreign keys) and unpacking synthesized PT payloads (ptwrite, cbr, mwait, pwre, exstop, pwrx).
- Use a dedicated context_switch ID counter so context-switch exports do not advance sample database IDs out of sync with call_return references.

Update Documentation/db-export.txt and add a shell test (test_export_to_postgresql_python.sh) to verify the standalone exporter.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Documentation/db-export.txt        |    2 +-
 tools/perf/python/export-to-postgresql.py     | 1320 +++++++++++++++++
 .../shell/test_export_to_postgresql_python.sh |  119 ++
 3 files changed, 1440 insertions(+), 1 deletion(-)
 create mode 100755 tools/perf/python/export-to-postgresql.py
 create mode 100755 tools/perf/tests/shell/test_export_to_postgresql_python.sh
tools/perf/Documentation/db-export.txt +1 -1
index b43f12b96973..20024e1f9164 100644
@@ -8,7 +8,7 @@ perf tool's python scripting engine:
 supports scripts:
 
 	tools/perf/python/export-to-sqlite.py
-	tools/perf/scripts/python/export-to-postgresql.py
+	tools/perf/python/export-to-postgresql.py
 
 which export data to a SQLite3 or PostgreSQL database.
 
tools/perf/python/export-to-postgresql.py new file +1320 -0

1,320 lines not on this page · raw message

tools/perf/tests/shell/test_export_to_postgresql_python.sh new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#197 lore raw

PATCH v2 44/49 perf python: Move and clean up exported-sql-viewer.py

In reply to Ian Rogers

Move exported-sql-viewer.py from tools/perf/scripts/python/ to tools/perf/python/ as it is a standalone PySide/PyQt GUI application for viewing SQLite and PostgreSQL databases exported by perf script.

Improvements and cleanups compared to the legacy script:
- Add support for PySide6 (falling back to PySide2 and PySide) loaded dynamically via importlib, and provide stub base classes (_QtBase, _QtWidget) so static analysis (mypy and pylint) and --help-only execution succeed when Qt bindings are not installed.
- Fix a Python 3 NameError crash in HBoxLayout where expanded_mark and not_expanded_mark called Python 2's removed unicode() builtin instead of chr(0x25BC) and chr(0x25B6).
- Replace instance-level method monkey-patching (self.mousePressEvent = self.MousePressEvent) on SwitchGraphView with proper class methods gated by self.rb_enabled so Qt virtual method dispatch works cleanly across PySide2 and PySide6.
- Remove Python 2 constructs (cPickle, xrange), wildcard imports, bare 'except:' clauses, and variable names shadowing Python builtins.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/exported-sql-viewer.py      | 5104 +++++++++++++++++
 .../scripts/python/exported-sql-viewer.py     | 5030 ----------------
 2 files changed, 5104 insertions(+), 5030 deletions(-)
 create mode 100755 tools/perf/python/exported-sql-viewer.py
 delete mode 100755 tools/perf/scripts/python/exported-sql-viewer.py
tools/perf/python/exported-sql-viewer.py new file +5104 -0
new file mode 100755
index 000000000000..bdd03c7fd5b6
@@ -0,0 +1,5104 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: GPL-2.0
+"""exported-sql-viewer.py: view data from sql database."""
+# Copyright (c) 2014-2018, Intel Corporation.
+
+# To use this script you will need to have exported data using either the
+# export-to-sqlite.py or the export-to-postgresql.py script.  Refer to those
+# scripts for details.
+#
+# Following on from the example in the export scripts, a
+# call-graph can be displayed for the pt_example database like this:
+#
+#	python exported-sql-viewer.py pt_example
+#
+# Note that for PostgreSQL, this script supports connecting to remote databases
+# by setting hostname, port, username, password, and dbname e.g.
+#
+#	python exported-sql-viewer.py \
+#		"hostname=myhost username=myuser password=mypassword dbname=pt_example"
+#
+# The result is a GUI window with a tree representing a context-sensitive
+# call-graph.  Expanding a couple of levels of the tree and adjusting column
+# widths to suit will display something like:
+#
+#                                         Call Graph: pt_example
+# Call Path                          Object      Count   Time(ns)  Time(%)  Branch Count   Branch Count(%)
+# v- ls
+#     v- 2638:2638
+#         v- _start                  ld-2.19.so    1     10074071   100.0         211135            100.0
+#           |- unknown               unknown       1        13198     0.1              1              0.0
+#           >- _dl_start             ld-2.19.so    1      1400980    13.9          19637              9.3
+#           >- _d_linit_internal     ld-2.19.so    1       448152     4.4          11094              5.3
+#           v-__libc_start_main@plt  ls            1      8211741    81.5         180397             85.4
+#              >- _dl_fixup          ld-2.19.so    1         7607     0.1            108              0.1
+#              >- __cxa_atexit       libc-2.19.so  1        11737     0.1             10              0.0
+#              >- __libc_csu_init    ls            1        10354     0.1             10              0.0
+#              |- _setjmp            libc-2.19.so  1            0     0.0              4              0.0
+#              v- main               ls            1      8182043    99.6         180254             99.9
+#
+# Points to note:
+#	The top level is a command name (comm)
+#	The next level is a thread (pid:tid)
+#	Subsequent levels are functions
+#	'Count' is the number of calls
+#	'Time' is the elapsed time until the function returns
+#	Percentages are relative to the level above
+#	'Branch Count' is the total number of branches for that function and all
+#       functions that it calls
+
+# There is also a "All branches" report, which displays branches and
+# possibly disassembly.  However, presently, the only supported disassembler is
+# Intel XED, and additionally the object code must be present in perf build ID
+# cache. To use Intel XED, libxed.so must be present. To build and install
+# libxed.so:
+#            git clone https://github.com/intelxed/mbuild.git mbuild
+#            git clone https://github.com/intelxed/xed
+#            cd xed
+#            ./mfile.py --share
+#            sudo ./mfile.py --prefix=/usr/local install
+#            sudo ldconfig
+#
+# Example report:
+#
+# Time           CPU  Command  PID    TID    Branch Type            In Tx  Branch
+# 8107675239590  2    ls       22011  22011  return from interrupt  No     ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea260 _start (ld-2.19.so)
+#                                                                              7fab593ea260 48 89 e7                                        mov %rsp, %rdi
+# 8107675239899  2    ls       22011  22011  hardware interrupt     No         7fab593ea260 _start (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
+# 8107675241900  2    ls       22011  22011  return from interrupt  No     ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea260 _start (ld-2.19.so)
+#                                                                              7fab593ea260 48 89 e7                                        mov %rsp, %rdi
+#                                                                              7fab593ea263 e8 c8 06 00 00                                  callq  0x7fab593ea930
+# 8107675241900  2    ls       22011  22011  call                   No         7fab593ea263 _start+0x3 (ld-2.19.so) -> 7fab593ea930 _dl_start (ld-2.19.so)
+#                                                                              7fab593ea930 55                                              pushq  %rbp
+#                                                                              7fab593ea931 48 89 e5                                        mov %rsp, %rbp
+#                                                                              7fab593ea934 41 57                                           pushq  %r15
+#                                                                              7fab593ea936 41 56                                           pushq  %r14
+#                                                                              7fab593ea938 41 55                                           pushq  %r13
+#                                                                              7fab593ea93a 41 54                                           pushq  %r12
+#                                                                              7fab593ea93c 53                                              pushq  %rbx
+#                                                                              7fab593ea93d 48 89 fb                                        mov %rdi, %rbx
+#                                                                              7fab593ea940 48 83 ec 68                                     sub $0x68, %rsp
+#                                                                              7fab593ea944 0f 31                                           rdtsc
+#                                                                              7fab593ea946 48 c1 e2 20                                     shl $0x20, %rdx
+#                                                                              7fab593ea94a 89 c0                                           mov %eax, %eax
+#                                                                              7fab593ea94c 48 09 c2                                        or %rax, %rdx
+#                                                                              7fab593ea94f 48 8b 05 1a 15 22 00                            movq  0x22151a(%rip), %rax
+# 8107675242232  2    ls       22011  22011  hardware interrupt     No         7fab593ea94f _dl_start+0x1f (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
+# 8107675242900  2    ls       22011  22011  return from interrupt  No     ffffffff86a00a67 native_irq_return_iret ([kernel]) -> 7fab593ea94f _dl_start+0x1f (ld-2.19.so)
+#                                                                              7fab593ea94f 48 8b 05 1a 15 22 00                            movq  0x22151a(%rip), %rax
+#                                                                              7fab593ea956 48 89 15 3b 13 22 00                            movq  %rdx, 0x22133b(%rip)
+# 8107675243232  2    ls       22011  22011  hardware interrupt     No         7fab593ea956 _dl_start+0x26 (ld-2.19.so) -> ffffffff86a012e0 page_fault ([kernel])
+
+from __future__ import print_function
+
+import sys
+# Only change warnings if the python -W option was not used
+if not sys.warnoptions:
+    import warnings
+    # PySide2 causes deprecation warnings, ignore them.
+    warnings.filterwarnings("ignore", category=DeprecationWarning)
+import argparse
+import weakref
+import threading
+import pickle
+glb_nsz = 16
+import re
+import os
+import random
+import copy
+import math
+from libxed import LibXED
+
+import importlib
+from typing import Any
+
+class _QtBase:
+    def __init__(self, *args: Any, **kwargs: Any) -> None:
+        pass
+    def __call__(self, *args: Any, **kwargs: Any) -> Any:
+        return self
+    def __getattr__(self, name: str) -> Any:
+        return _QtBase()
+    def mousePressEvent(self, event: Any) -> None:
+        pass
+    def mouseMoveEvent(self, event: Any) -> None:
+        pass
+    def mouseReleaseEvent(self, event: Any) -> None:
+        pass
+    def resizeEvent(self, event: Any) -> None:
+        pass
+    def changeEvent(self, event: Any) -> None:
+        pass
+
+class _QtWidget(_QtBase):
+    pass
+
+class _QtMdiSubWindow(_QtWidget):
+    pass
+
+QAbstractItemModel: Any = _QtBase
+QAbstractItemView: Any = _QtBase
+QAbstractTableModel: Any = _QtBase
+QAction: Any = _QtBase
+QApplication: Any = _QtBase
+QCheckBox: Any = _QtBase
+QColor: Any = _QtBase
+QComboBox: Any = _QtBase
+QDialog: Any = _QtWidget
+QEvent: Any = _QtBase
+QFont: Any = _QtBase
+QFontMetrics: Any = _QtBase
+QGraphicsItem: Any = _QtBase
+QGraphicsLineItem: Any = _QtBase
+QGraphicsScene: Any = _QtBase
+QGraphicsSimpleTextItem: Any = _QtBase
+QGraphicsView: Any = _QtBase
+QGridLayout: Any = _QtBase
+QHBoxLayout: Any = _QtBase
+QKeySequence: Any = _QtBase
+QLabel: Any = _QtBase
+QLineEdit: Any = _QtBase
+QMainWindow: Any = _QtWidget
+QMdiArea: Any = _QtBase
+QMdiSubWindow: Any = _QtMdiSubWindow
+QMenu: Any = _QtBase
+QMessageBox: Any = _QtBase
+QModelIndex: Any = _QtBase
+QObject: Any = _QtBase
+QPalette: Any = _QtBase
+QPoint: Any = _QtBase
+QPointF: Any = _QtBase
+QProgressBar: Any = _QtBase
+QPushButton: Any = _QtBase
+QRect: Any = _QtBase
+QRectF: Any = _QtBase
+QRubberBand: Any = _QtBase
+QSize: Any = _QtBase
+QSizePolicy: Any = _QtBase
+QSortFilterProxyModel: Any = _QtBase
+QSpinBox: Any = _QtBase
+QSplitter: Any = _QtWidget
+QSqlDatabase: Any = _QtBase
+QSqlQuery: Any = _QtBase
+QStyle: Any = _QtBase
+Qt: Any = _QtBase
+QTableView: Any = _QtWidget
+QTextBrowser: Any = _QtWidget
+QThread: Any = _QtBase
+QToolButton: Any = _QtBase
+QTreeView: Any = _QtWidget
+QVBoxLayout: Any = _QtBase
+qVersion: Any = _QtBase
+QWidget: Any = _QtWidget
+Signal: Any = _QtBase
+
+pyside_version_1 = False
+for _pkg in ("PySide6", "PySide2"):
+    try:
+        for _mod_name in ("QtCore", "QtGui", "QtSql", "QtWidgets"):
+            _mod = importlib.import_module(f"{_pkg}.{_mod_name}")
+            for _sym in dir(_mod):
+                if not _sym.startswith("_"):
+                    globals()[_sym] = getattr(_mod, _sym)
+        break
+    except ImportError:
+        pass
+
+
+from decimal import Decimal, ROUND_HALF_UP
+from ctypes import create_string_buffer, addressof, sizeof, \
+           c_void_p, c_bool, c_char, c_longlong
+from multiprocessing import Process, Array, Value, Event
+
+
+
+def printerr(*args, **keyword_args):
+    print(*args, file=sys.stderr, **keyword_args)
+
+# Data formatting helpers
+
+def tohex(ip):
+    if ip < 0:
+        ip += 1 << 64
+    return "%x" % ip
+
+def offstr(offset):
+    if offset:
+        return "+0x%x" % offset
+    return ""
+
+def dsoname(name):
+    if name == "[kernel.kallsyms]":
+        return "[kernel]"
+    return name
+
+def findnth(s, sub, n, offs=0):
+    pos = s.find(sub)
+    if pos < 0:
+        return pos
+    if n <= 1:
+        return offs + pos
+    return findnth(s[pos + 1:], sub, n - 1, offs + pos + 1)
+
+# Percent to one decimal place
+
+def PercentToOneDP(n, d):
+    if not d:
+        return "0.0"
+    x = (n * Decimal(100)) / d
+    return str(x.quantize(Decimal(".1"), rounding=ROUND_HALF_UP))
+
+# Helper for queries that must not fail
+
+def QueryExec(query, stmt):
+    ret = query.exec_(stmt)
+    if not ret:
+        raise RuntimeError("Query failed: " + query.lastError().text())
+
+# Background thread
+
+class Thread(QThread):
+
+    done = Signal(object)
+
+    def __init__(self, task, param=None, parent=None):
+        super(Thread, self).__init__(parent)
+        self.task = task
+        self.param = param
+
+    def run(self):
+        while True:
+            if self.param is None:
+                done, result = self.task()
+            else:
+                done, result = self.task(self.param)
+            self.done.emit(result)
+            if done:
+                break
+
+# Tree data model
+
+class TreeModel(QAbstractItemModel):
+
+    def __init__(self, glb, params, parent=None):
+        super(TreeModel, self).__init__(parent)
+        self.glb = glb
+        self.params = params
+        self.root = self.GetRoot()
+        self.last_row_read = 0
+
+    def Item(self, parent):
+        if parent.isValid():
+            return parent.internalPointer()
+        else:
+            return self.root
+
+    def rowCount(self, parent):
+        result = self.Item(parent).childCount()
+        if result < 0:
+            result = 0
+            self.dataChanged.emit(parent, parent)
+        return result
+
+    def hasChildren(self, parent):
+        return self.Item(parent).hasChildren()
+
+    def headerData(self, section, orientation, role):
+        if role == Qt.TextAlignmentRole:
+            return self.columnAlignment(section)
+        if role != Qt.DisplayRole:
+            return None
+        if orientation != Qt.Horizontal:
+            return None
+        return self.columnHeader(section)
+
+    def parent(self, child):
+        child_item = child.internalPointer()
+        if child_item is self.root:
+            return QModelIndex()
+        parent_item = child_item.getParentItem()
+        return self.createIndex(parent_item.getRow(), 0, parent_item)
+
+    def index(self, row, column, parent):
+        child_item = self.Item(parent).getChildItem(row)
+        return self.createIndex(row, column, child_item)
+
+    def DisplayData(self, item, index):
+        return item.getData(index.column())
+
+    def FetchIfNeeded(self, row):
+        if row > self.last_row_read:
+            self.last_row_read = row
+            if row + 10 >= self.root.child_count:
+                self.fetcher.Fetch(glb_chunk_sz)
+
+    def columnAlignment(self, _column):
+        return Qt.AlignLeft
+
+    def columnFont(self, _column):
+        return None
+
+    def data(self, index, role):
+        if role == Qt.TextAlignmentRole:
+            return self.columnAlignment(index.column())
+        if role == Qt.FontRole:
+            return self.columnFont(index.column())
+        if role != Qt.DisplayRole:
+            return None
+        item = index.internalPointer()
+        return self.DisplayData(item, index)
+
+# Table data model
+
+class TableModel(QAbstractTableModel):
+
+    def __init__(self, parent=None):
+        super(TableModel, self).__init__(parent)
+        self.child_count = 0
+        self.child_items = []
+        self.last_row_read = 0
+
+    def Item(self, parent):
+        if parent.isValid():
+            return parent.internalPointer()
+        else:
+            return self
+
+    def rowCount(self, _parent):
+        return self.child_count
+
+    def headerData(self, section, orientation, role):
+        if role == Qt.TextAlignmentRole:
+            return self.columnAlignment(section)
+        if role != Qt.DisplayRole:
+            return None
+        if orientation != Qt.Horizontal:
+            return None
+        return self.columnHeader(section)
+
+    def index(self, row, column, _parent):
+        return self.createIndex(row, column, self.child_items[row])
+
+    def DisplayData(self, item, index):
+        return item.getData(index.column())
+
+    def FetchIfNeeded(self, row):
+        if row > self.last_row_read:
+            self.last_row_read = row
+            if row + 10 >= self.child_count:
+                self.fetcher.Fetch(glb_chunk_sz)
+
+    def columnAlignment(self, _column):
+        return Qt.AlignLeft
+
+    def columnFont(self, _column):
+        return None
+
+    def data(self, index, role):
+        if role == Qt.TextAlignmentRole:
+            return self.columnAlignment(index.column())
+        if role == Qt.FontRole:
+            return self.columnFont(index.column())
+        if role != Qt.DisplayRole:
+            return None
+        item = index.internalPointer()
+        return self.DisplayData(item, index)
+
+# Model cache
+
+model_cache: Any = weakref.WeakValueDictionary()
+model_cache_lock = threading.Lock()
+
+def LookupCreateModel(model_name, create_fn):
+    model_cache_lock.acquire()
+    try:
+        model = model_cache[model_name]
+    except KeyError:
+        model = None
+    if model is None:
+        model = create_fn()
+        model_cache[model_name] = model
+    model_cache_lock.release()
+    return model
+
+def LookupModel(model_name):
+    model_cache_lock.acquire()
+    try:
+        model = model_cache[model_name]
+    except KeyError:
+        model = None
+    model_cache_lock.release()
+    return model
+
+# Find bar
+
+class FindBar():
+
+    def __init__(self, parent, finder, is_reg_expr=False):
+        self.finder = finder
+        self.context = []
+        self.last_value = None
+        self.last_pattern = None
+
+        label = QLabel("Find:")
+        label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
+
+        self.textbox = QComboBox()
+        self.textbox.setEditable(True)
+        self.textbox.currentIndexChanged.connect(self.ValueChanged)
+
+        self.progress = QProgressBar()
+        self.progress.setRange(0, 0)
+        self.progress.hide()
+
+        if is_reg_expr:
+            self.pattern = QCheckBox("Regular Expression")
+        else:
+            self.pattern = QCheckBox("Pattern")
+        self.pattern.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
+
+        self.next_button = QToolButton()
+        self.next_button.setIcon(parent.style().standardIcon(QStyle.SP_ArrowDown))
+        self.next_button.released.connect(lambda: self.NextPrev(1))
+
+        self.prev_button = QToolButton()
+        self.prev_button.setIcon(parent.style().standardIcon(QStyle.SP_ArrowUp))
+        self.prev_button.released.connect(lambda: self.NextPrev(-1))
+
+        self.close_button = QToolButton()
+        self.close_button.setIcon(parent.style().standardIcon(QStyle.SP_DockWidgetCloseButton))
+        self.close_button.released.connect(self.Deactivate)
+
+        self.hbox = QHBoxLayout()
+        self.hbox.setContentsMargins(0, 0, 0, 0)
+
+        self.hbox.addWidget(label)
+        self.hbox.addWidget(self.textbox)
+        self.hbox.addWidget(self.progress)
+        self.hbox.addWidget(self.pattern)
+        self.hbox.addWidget(self.next_button)
+        self.hbox.addWidget(self.prev_button)
+        self.hbox.addWidget(self.close_button)
+
+        self.bar = QWidget()
+        self.bar.setLayout(self.hbox)
+        self.bar.hide()
+
+    def Widget(self):
+        return self.bar
+
+    def Activate(self):
+        self.bar.show()
+        self.textbox.lineEdit().selectAll()
+        self.textbox.setFocus()
+
+    def Deactivate(self):
+        self.bar.hide()
+
+    def Busy(self):
+        self.textbox.setEnabled(False)
+        self.pattern.hide()
+        self.next_button.hide()
+        self.prev_button.hide()
+        self.progress.show()
+
+    def Idle(self):
+        self.textbox.setEnabled(True)
+        self.progress.hide()
+        self.pattern.show()
+        self.next_button.show()
+        self.prev_button.show()
+
+    def Find(self, direction):
+        value = self.textbox.currentText()
+        pattern = self.pattern.isChecked()
+        self.last_value = value
+        self.last_pattern = pattern
+        self.finder.Find(value, direction, pattern, self.context)
+
+    def ValueChanged(self):
+        _value = self.textbox.currentText()
+        pattern = self.pattern.isChecked()
+        index = self.textbox.currentIndex()
+        data = self.textbox.itemData(index)
+        # Store the pattern in the combo box to keep it with the text value
+        if data == None:
+            self.textbox.setItemData(index, pattern)
+        else:
+            self.pattern.setChecked(data)
+        self.Find(0)
+
+    def NextPrev(self, direction):
+        value = self.textbox.currentText()
+        pattern = self.pattern.isChecked()
+        if value != self.last_value:
+            index = self.textbox.findText(value)
+            # Allow for a button press before the value has been added to the combo box
+            if index < 0:
+                index = self.textbox.count()
+                self.textbox.addItem(value, pattern)
+                self.textbox.setCurrentIndex(index)
+                return
+            else:
+                self.textbox.setItemData(index, pattern)
+        elif pattern != self.last_pattern:
+            # Keep the pattern recorded in the combo box up to date
+            index = self.textbox.currentIndex()
+            self.textbox.setItemData(index, pattern)
+        self.Find(direction)
+
+    def NotFound(self):
+        QMessageBox.information(self.bar, "Find", "'" + self.textbox.currentText() + "' not found")
+
+# Context-sensitive call graph data model item base
+
+class CallGraphLevelItemBase(object):
+    def Select(self) -> None:
+        pass
+    def data(self, _column: int, _role: int) -> Any:
+        return None
+
+    def __init__(self, glb, params, row, parent_item):
+        self.glb = glb
+        self.params = params
+        self.row = row
+        self.parent_item = parent_item
+        self.query_done = False
+        self.child_count = 0
+        self.child_items = []
+        if parent_item:
+            self.level = parent_item.level + 1
+        else:
+            self.level = 0
+
+    def getChildItem(self, row):
+        return self.child_items[row]
+
+    def getParentItem(self):
+        return self.parent_item
+
+    def getRow(self):
+        return self.row
+
+    def childCount(self):
+        if not self.query_done:
+            self.Select()
+            if not self.child_count:
+                return -1
+        return self.child_count
+
+    def hasChildren(self):
+        if not self.query_done:
+            return True
+        return self.child_count > 0
+
+    def getData(self, column):
+        return self.data[column]
+
+# Context-sensitive call graph data model level 2+ item base
+
+class CallGraphLevelTwoPlusItemBase(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, call_path_id, time, insn_cnt, cyc_cnt, branch_count, parent_item):
+        super(CallGraphLevelTwoPlusItemBase, self).__init__(glb, params, row, parent_item)
+        self.comm_id = comm_id
+        self.thread_id = thread_id
+        self.call_path_id = call_path_id
+        self.insn_cnt = insn_cnt
+        self.cyc_cnt = cyc_cnt
+        self.branch_count = branch_count
+        self.time = time
+
+    def Select(self):
+        self.query_done = True
+        query = QSqlQuery(self.glb.db)
+        if self.params.have_ipc:
+            ipc_str = ", SUM(insn_count), SUM(cyc_count)"
+        else:
+            ipc_str = ""
+        QueryExec(query, "SELECT call_path_id, name, short_name, COUNT(calls.id), SUM(return_time - call_time)" + ipc_str + ", SUM(branch_count)"
+                    " FROM calls"
+                    " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
+                    " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
+                    " INNER JOIN dsos ON symbols.dso_id = dsos.id"
+                    " WHERE parent_call_path_id = " + str(self.call_path_id) +
+                    " AND comm_id = " + str(self.comm_id) +
+                    " AND thread_id = " + str(self.thread_id) +
+                    " GROUP BY call_path_id, name, short_name"
+                    " ORDER BY call_path_id")
+        while query.next():
+            if self.params.have_ipc:
+                insn_cnt = int(query.value(5))
+                cyc_cnt = int(query.value(6))
+                branch_count = int(query.value(7))
+            else:
+                insn_cnt = 0
+                cyc_cnt = 0
+                branch_count = int(query.value(5))
+            child_item = CallGraphLevelThreeItem(self.glb, self.params, self.child_count, self.comm_id, self.thread_id, query.value(0), query.value(1), query.value(2), query.value(3), int(query.value(4)), insn_cnt, cyc_cnt, branch_count, self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Context-sensitive call graph data model level three item
+
+class CallGraphLevelThreeItem(CallGraphLevelTwoPlusItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, call_path_id, name, dso, count, time, insn_cnt, cyc_cnt, branch_count, parent_item):
+        super(CallGraphLevelThreeItem, self).__init__(glb, params, row, comm_id, thread_id, call_path_id, time, insn_cnt, cyc_cnt, branch_count, parent_item)
+        dso = dsoname(dso)
+        if self.params.have_ipc:
+            insn_pcnt = PercentToOneDP(insn_cnt, parent_item.insn_cnt)
+            cyc_pcnt = PercentToOneDP(cyc_cnt, parent_item.cyc_cnt)
+            br_pcnt = PercentToOneDP(branch_count, parent_item.branch_count)
+            ipc = CalcIPC(cyc_cnt, insn_cnt)
+            self.data = [ name, dso, str(count), str(time), PercentToOneDP(time, parent_item.time), str(insn_cnt), insn_pcnt, str(cyc_cnt), cyc_pcnt, ipc, str(branch_count), br_pcnt ]
+        else:
+            self.data = [ name, dso, str(count), str(time), PercentToOneDP(time, parent_item.time), str(branch_count), PercentToOneDP(branch_count, parent_item.branch_count) ]
+        self.dbid = call_path_id
+
+# Context-sensitive call graph data model level two item
+
+class CallGraphLevelTwoItem(CallGraphLevelTwoPlusItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, pid, tid, parent_item):
+        super(CallGraphLevelTwoItem, self).__init__(glb, params, row, comm_id, thread_id, 1, 0, 0, 0, 0, parent_item)
+        if self.params.have_ipc:
+            self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", "", "", "", "", "", ""]
+        else:
+            self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", ""]
+        self.dbid = thread_id
+
+    def Select(self):
+        super(CallGraphLevelTwoItem, self).Select()
+        for child_item in self.child_items:
+            self.time += child_item.time
+            self.insn_cnt += child_item.insn_cnt
+            self.cyc_cnt += child_item.cyc_cnt
+            self.branch_count += child_item.branch_count
+        for child_item in self.child_items:
+            child_item.data[4] = PercentToOneDP(child_item.time, self.time)
+            if self.params.have_ipc:
+                child_item.data[6] = PercentToOneDP(child_item.insn_cnt, self.insn_cnt)
+                child_item.data[8] = PercentToOneDP(child_item.cyc_cnt, self.cyc_cnt)
+                child_item.data[11] = PercentToOneDP(child_item.branch_count, self.branch_count)
+            else:
+                child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count)
+
+# Context-sensitive call graph data model level one item
+
+class CallGraphLevelOneItem(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params, row, comm_id, comm, parent_item):
+        super(CallGraphLevelOneItem, self).__init__(glb, params, row, parent_item)
+        if self.params.have_ipc:
+            self.data = [comm, "", "", "", "", "", "", "", "", "", "", ""]
+        else:
+            self.data = [comm, "", "", "", "", "", ""]
+        self.dbid = comm_id
+
+    def Select(self):
+        self.query_done = True
+        query = QSqlQuery(self.glb.db)
+        QueryExec(query, "SELECT thread_id, pid, tid"
+                    " FROM comm_threads"
+                    " INNER JOIN threads ON thread_id = threads.id"
+                    " WHERE comm_id = " + str(self.dbid))
+        while query.next():
+            child_item = CallGraphLevelTwoItem(self.glb, self.params, self.child_count, self.dbid, query.value(0), query.value(1), query.value(2), self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Context-sensitive call graph data model root item
+
+class CallGraphRootItem(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params):
+        super(CallGraphRootItem, self).__init__(glb, params, 0, None)
+        self.dbid = 0
+        self.query_done = True
+        if_has_calls = ""
+        if IsSelectable(glb.db, "comms", columns = "has_calls"):
+            if_has_calls = " WHERE has_calls = " + glb.dbref.TRUE
+        query = QSqlQuery(glb.db)
+        QueryExec(query, "SELECT id, comm FROM comms" + if_has_calls)
+        while query.next():
+            if not query.value(0):
+                continue
+            child_item = CallGraphLevelOneItem(glb, params, self.child_count, query.value(0), query.value(1), self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Call graph model parameters
+
+class CallGraphModelParams():
+
+    def __init__(self, glb, _parent=None):
+        self.have_ipc = IsSelectable(glb.db, "calls", columns = "insn_count, cyc_count")
+
+# Context-sensitive call graph data model base
+
+class CallGraphModelBase(TreeModel):
+
+    def __init__(self, glb, parent=None):
+        super(CallGraphModelBase, self).__init__(glb, CallGraphModelParams(glb), parent)
+
+    def FindSelect(self, value, pattern, query):
+        if pattern:
+            # postgresql and sqlite pattern patching differences:
+            #   postgresql LIKE is case sensitive but sqlite LIKE is not
+            #   postgresql LIKE allows % and _ to be escaped with \ but sqlite LIKE does not
+            #   postgresql supports ILIKE which is case insensitive
+            #   sqlite supports GLOB (text only) which uses * and ? and is case sensitive
+            if not self.glb.dbref.is_sqlite3:
+                # Escape % and _
+                s = value.replace("%", "\\%")
+                s = s.replace("_", "\\_")
+                # Translate * and ? into SQL LIKE pattern characters % and _
+                if sys.version_info[0] == 3:
+                    trans = str.maketrans("*?", "%_")
+                else:
+                    trans = bytes.maketrans("*?", "%_")
+                match = " LIKE '" + str(s).translate(trans) + "'"
+            else:
+                match = " GLOB '" + str(value) + "'"
+        else:
+            match = " = '" + str(value) + "'"
+        self.DoFindSelect(query, match)
+
+    def Found(self, query, found):
+        if found:
+            return self.FindPath(query)
+        return []
+
+    def FindValue(self, value, pattern, query, last_value, last_pattern):
+        if last_value == value and pattern == last_pattern:
+            found = query.first()
+        else:
+            self.FindSelect(value, pattern, query)
+            found = query.next()
+        return self.Found(query, found)
+
+    def FindNext(self, query):
+        found = query.next()
+        if not found:
+            found = query.first()
+        return self.Found(query, found)
+
+    def FindPrev(self, query):
+        found = query.previous()
+        if not found:
+            found = query.last()
+        return self.Found(query, found)
+
+    def FindThread(self, c):
+        if c.direction == 0 or c.value != c.last_value or c.pattern != c.last_pattern:
+            ids = self.FindValue(c.value, c.pattern, c.query, c.last_value, c.last_pattern)
+        elif c.direction > 0:
+            ids = self.FindNext(c.query)
+        else:
+            ids = self.FindPrev(c.query)
+        return (True, ids)
+
+    def Find(self, value, direction, pattern, context, callback):
+        class Context():
+            def __init__(self, *x):
+                self.value, self.direction, self.pattern, self.query, self.last_value, self.last_pattern = x
+            def Update(self, *x):
+                self.value, self.direction, self.pattern, self.last_value, self.last_pattern = x + (self.value, self.pattern)
+        if len(context):
+            context[0].Update(value, direction, pattern)
+        else:
+            context.append(Context(value, direction, pattern, QSqlQuery(self.glb.db), None, None))
+        # Use a thread so the UI is not blocked during the SELECT
+        thread = Thread(self.FindThread, context[0])
+        thread.done.connect(lambda ids, t=thread, c=callback: self.FindDone(t, c, ids), Qt.QueuedConnection)
+        thread.start()
+
+    def FindDone(self, _thread, callback, ids):
+        callback(ids)
+
+# Context-sensitive call graph data model
+
+class CallGraphModel(CallGraphModelBase):
+
+    def GetRoot(self):
+        return CallGraphRootItem(self.glb, self.params)
+
+    def columnCount(self, _parent=None):
+        if self.params.have_ipc:
+            return 12
+        else:
+            return 7
+
+    def columnHeader(self, column):
+        if self.params.have_ipc:
+            headers = ["Call Path", "Object", "Count ", "Time (ns) ", "Time (%) ", "Insn Cnt", "Insn Cnt (%)", "Cyc Cnt", "Cyc Cnt (%)", "IPC", "Branch Count ", "Branch Count (%) "]
+        else:
+            headers = ["Call Path", "Object", "Count ", "Time (ns) ", "Time (%) ", "Branch Count ", "Branch Count (%) "]
+        return headers[column]
+
+    def columnAlignment(self, column):
+        if self.params.have_ipc:
+            alignment = [ Qt.AlignLeft, Qt.AlignLeft, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight ]
+        else:
+            alignment = [ Qt.AlignLeft, Qt.AlignLeft, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight ]
+        return alignment[column]
+
+    def DoFindSelect(self, query, match):
+        QueryExec(query, "SELECT call_path_id, comm_id, thread_id"
+                        " FROM calls"
+                        " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
+                        " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
+                        " WHERE calls.id <> 0"
+                        " AND symbols.name" + match +
+                        " GROUP BY comm_id, thread_id, call_path_id"
+                        " ORDER BY comm_id, thread_id, call_path_id")
+
+    def FindPath(self, query):
+        # Turn the query result into a list of ids that the tree view can walk
+        # to open the tree at the right place.
+        ids = []
+        parent_id = query.value(0)
+        while parent_id:
+            ids.insert(0, parent_id)
+            q2 = QSqlQuery(self.glb.db)
+            QueryExec(q2, "SELECT parent_id"
+                    " FROM call_paths"
+                    " WHERE id = " + str(parent_id))
+            if not q2.next():
+                break
+            parent_id = q2.value(0)
+        # The call path root is not used
+        if ids[0] == 1:
+            del ids[0]
+        ids.insert(0, query.value(2))
+        ids.insert(0, query.value(1))
+        return ids
+
+# Call tree data model level 2+ item base
+
+class CallTreeLevelTwoPlusItemBase(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, calls_id, call_time, time, insn_cnt, cyc_cnt, branch_count, parent_item):
+        super(CallTreeLevelTwoPlusItemBase, self).__init__(glb, params, row, parent_item)
+        self.comm_id = comm_id
+        self.thread_id = thread_id
+        self.calls_id = calls_id
+        self.call_time = call_time
+        self.time = time
+        self.insn_cnt = insn_cnt
+        self.cyc_cnt = cyc_cnt
+        self.branch_count = branch_count
+
+    def Select(self):
+        self.query_done = True
+        if self.calls_id == 0:
+            comm_thread = " AND comm_id = " + str(self.comm_id) + " AND thread_id = " + str(self.thread_id)
+        else:
+            comm_thread = ""
+        if self.params.have_ipc:
+            ipc_str = ", insn_count, cyc_count"
+        else:
+            ipc_str = ""
+        query = QSqlQuery(self.glb.db)
+        QueryExec(query, "SELECT calls.id, name, short_name, call_time, return_time - call_time" + ipc_str + ", branch_count"
+                    " FROM calls"
+                    " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
+                    " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
+                    " INNER JOIN dsos ON symbols.dso_id = dsos.id"
+                    " WHERE calls.parent_id = " + str(self.calls_id) + comm_thread +
+                    " ORDER BY call_time, calls.id")
+        while query.next():
+            if self.params.have_ipc:
+                insn_cnt = int(query.value(5))
+                cyc_cnt = int(query.value(6))
+                branch_count = int(query.value(7))
+            else:
+                insn_cnt = 0
+                cyc_cnt = 0
+                branch_count = int(query.value(5))
+            child_item = CallTreeLevelThreeItem(self.glb, self.params, self.child_count, self.comm_id, self.thread_id, query.value(0), query.value(1), query.value(2), query.value(3), int(query.value(4)), insn_cnt, cyc_cnt, branch_count, self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Call tree data model level three item
+
+class CallTreeLevelThreeItem(CallTreeLevelTwoPlusItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, calls_id, name, dso, call_time, time, insn_cnt, cyc_cnt, branch_count, parent_item):
+        super(CallTreeLevelThreeItem, self).__init__(glb, params, row, comm_id, thread_id, calls_id, call_time, time, insn_cnt, cyc_cnt, branch_count, parent_item)
+        dso = dsoname(dso)
+        if self.params.have_ipc:
+            insn_pcnt = PercentToOneDP(insn_cnt, parent_item.insn_cnt)
+            cyc_pcnt = PercentToOneDP(cyc_cnt, parent_item.cyc_cnt)
+            br_pcnt = PercentToOneDP(branch_count, parent_item.branch_count)
+            ipc = CalcIPC(cyc_cnt, insn_cnt)
+            self.data = [ name, dso, str(call_time), str(time), PercentToOneDP(time, parent_item.time), str(insn_cnt), insn_pcnt, str(cyc_cnt), cyc_pcnt, ipc, str(branch_count), br_pcnt ]
+        else:
+            self.data = [ name, dso, str(call_time), str(time), PercentToOneDP(time, parent_item.time), str(branch_count), PercentToOneDP(branch_count, parent_item.branch_count) ]
+        self.dbid = calls_id
+
+# Call tree data model level two item
+
+class CallTreeLevelTwoItem(CallTreeLevelTwoPlusItemBase):
+
+    def __init__(self, glb, params, row, comm_id, thread_id, pid, tid, parent_item):
+        super(CallTreeLevelTwoItem, self).__init__(glb, params, row, comm_id, thread_id, 0, 0, 0, 0, 0, 0, parent_item)
+        if self.params.have_ipc:
+            self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", "", "", "", "", "", ""]
+        else:
+            self.data = [str(pid) + ":" + str(tid), "", "", "", "", "", ""]
+        self.dbid = thread_id
+
+    def Select(self):
+        super(CallTreeLevelTwoItem, self).Select()
+        for child_item in self.child_items:
+            self.time += child_item.time
+            self.insn_cnt += child_item.insn_cnt
+            self.cyc_cnt += child_item.cyc_cnt
+            self.branch_count += child_item.branch_count
+        for child_item in self.child_items:
+            child_item.data[4] = PercentToOneDP(child_item.time, self.time)
+            if self.params.have_ipc:
+                child_item.data[6] = PercentToOneDP(child_item.insn_cnt, self.insn_cnt)
+                child_item.data[8] = PercentToOneDP(child_item.cyc_cnt, self.cyc_cnt)
+                child_item.data[11] = PercentToOneDP(child_item.branch_count, self.branch_count)
+            else:
+                child_item.data[6] = PercentToOneDP(child_item.branch_count, self.branch_count)
+
+# Call tree data model level one item
+
+class CallTreeLevelOneItem(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params, row, comm_id, comm, parent_item):
+        super(CallTreeLevelOneItem, self).__init__(glb, params, row, parent_item)
+        if self.params.have_ipc:
+            self.data = [comm, "", "", "", "", "", "", "", "", "", "", ""]
+        else:
+            self.data = [comm, "", "", "", "", "", ""]
+        self.dbid = comm_id
+
+    def Select(self):
+        self.query_done = True
+        query = QSqlQuery(self.glb.db)
+        QueryExec(query, "SELECT thread_id, pid, tid"
+                    " FROM comm_threads"
+                    " INNER JOIN threads ON thread_id = threads.id"
+                    " WHERE comm_id = " + str(self.dbid))
+        while query.next():
+            child_item = CallTreeLevelTwoItem(self.glb, self.params, self.child_count, self.dbid, query.value(0), query.value(1), query.value(2), self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Call tree data model root item
+
+class CallTreeRootItem(CallGraphLevelItemBase):
+
+    def __init__(self, glb, params):
+        super(CallTreeRootItem, self).__init__(glb, params, 0, None)
+        self.dbid = 0
+        self.query_done = True
+        if_has_calls = ""
+        if IsSelectable(glb.db, "comms", columns = "has_calls"):
+            if_has_calls = " WHERE has_calls = " + glb.dbref.TRUE
+        query = QSqlQuery(glb.db)
+        QueryExec(query, "SELECT id, comm FROM comms" + if_has_calls)
+        while query.next():
+            if not query.value(0):
+                continue
+            child_item = CallTreeLevelOneItem(glb, params, self.child_count, query.value(0), query.value(1), self)
+            self.child_items.append(child_item)
+            self.child_count += 1
+
+# Call Tree data model
+
+class CallTreeModel(CallGraphModelBase):
+
+    def GetRoot(self):
+        return CallTreeRootItem(self.glb, self.params)
+
+    def columnCount(self, _parent=None):
+        if self.params.have_ipc:
+            return 12
+        else:
+            return 7
+
+    def columnHeader(self, column):
+        if self.params.have_ipc:
+            headers = ["Call Path", "Object", "Call Time", "Time (ns) ", "Time (%) ", "Insn Cnt", "Insn Cnt (%)", "Cyc Cnt", "Cyc Cnt (%)", "IPC", "Branch Count ", "Branch Count (%) "]
+        else:
+            headers = ["Call Path", "Object", "Call Time", "Time (ns) ", "Time (%) ", "Branch Count ", "Branch Count (%) "]
+        return headers[column]
+
+    def columnAlignment(self, column):
+        if self.params.have_ipc:
+            alignment = [ Qt.AlignLeft, Qt.AlignLeft, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight ]
+        else:
+            alignment = [ Qt.AlignLeft, Qt.AlignLeft, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight, Qt.AlignRight ]
+        return alignment[column]
+
+    def DoFindSelect(self, query, match):
+        QueryExec(query, "SELECT calls.id, comm_id, thread_id"
+                        " FROM calls"
+                        " INNER JOIN call_paths ON calls.call_path_id = call_paths.id"
+                        " INNER JOIN symbols ON call_paths.symbol_id = symbols.id"
+                        " WHERE calls.id <> 0"
+                        " AND symbols.name" + match +
+                        " ORDER BY comm_id, thread_id, call_time, calls.id")
+
+    def FindPath(self, query):
+        # Turn the query result into a list of ids that the tree view can walk
+        # to open the tree at the right place.
+        ids = []
+        parent_id = query.value(0)
+        while parent_id:
+            ids.insert(0, parent_id)
+            q2 = QSqlQuery(self.glb.db)
+            QueryExec(q2, "SELECT parent_id"
+                    " FROM calls"
+                    " WHERE id = " + str(parent_id))
+            if not q2.next():
+                break
+            parent_id = q2.value(0)
+        ids.insert(0, query.value(2))
+        ids.insert(0, query.value(1))
+        return ids
+
+# Vertical layout
+
+class HBoxLayout(QHBoxLayout):
+
+    def __init__(self, *children):
+        super(HBoxLayout, self).__init__()
+
+        self.layout().setContentsMargins(0, 0, 0, 0)
+        for child in children:
+            if child.isWidgetType():
+                self.layout().addWidget(child)
+            else:
+                self.layout().addLayout(child)
+
+# Horizontal layout
+
+class VBoxLayout(QVBoxLayout):
+
+    def __init__(self, *children):
+        super(VBoxLayout, self).__init__()
+
+        self.layout().setContentsMargins(0, 0, 0, 0)
+        for child in children:
+            if child.isWidgetType():
+                self.layout().addWidget(child)
+            else:
+                self.layout().addLayout(child)
+
+# Vertical layout widget
+
+class VBox():
+
+    def __init__(self, *children):
+        self.vbox = QWidget()
+        self.vbox.setLayout(VBoxLayout(*children))
+
+    def Widget(self):
+        return self.vbox
+
+# Tree window base
+
+class TreeWindowBase(QMdiSubWindow):
+
+    def __init__(self, parent=None):
+        super(TreeWindowBase, self).__init__(parent)
+
+        self.model = None
+        self.find_bar = None
+
+        self.view = QTreeView()
+        self.view.setSelectionMode(QAbstractItemView.ContiguousSelection)
+        self.view.CopyCellsToClipboard = CopyTreeCellsToClipboard
+
+        self.context_menu = TreeContextMenu(self.view)
+
+    def DisplayFound(self, ids):
+        if not len(ids):
+            return False
+        parent = QModelIndex()
+        for dbid in ids:
+            found = False
+            n = self.model.rowCount(parent)
+            for row in range(n):
+                child = self.model.index(row, 0, parent)
+                if child.internalPointer().dbid == dbid:
+                    found = True
+                    self.view.setExpanded(parent, True)
+                    self.view.setCurrentIndex(child)
+                    parent = child
+                    break
+            if not found:
+                break
+        return found
+
+    def Find(self, value, direction, pattern, context):
+        self.view.setFocus()
+        self.find_bar.Busy()
+        self.model.Find(value, direction, pattern, context, self.FindDone)
+
+    def FindDone(self, ids):
+        found = True
+        if not self.DisplayFound(ids):
+            found = False
+        self.find_bar.Idle()
+        if not found:
+            self.find_bar.NotFound()
+
+
+# Context-sensitive call graph window
+
+class CallGraphWindow(TreeWindowBase):
+
+    def __init__(self, glb, parent=None):
+        super(CallGraphWindow, self).__init__(parent)
+
+        self.model = LookupCreateModel("Context-Sensitive Call Graph", lambda x=glb: CallGraphModel(x))
+
+        self.view.setModel(self.model)
+
+        for c, w in ((0, 250), (1, 100), (2, 60), (3, 70), (4, 70), (5, 100)):
+            self.view.setColumnWidth(c, w)
+
+        self.find_bar = FindBar(self, self)
+
+        self.vbox = VBox(self.view, self.find_bar.Widget())
+
+        self.setWidget(self.vbox.Widget())
+
+        AddSubWindow(glb.mainwindow.mdi_area, self, "Context-Sensitive Call Graph")
+
+# Call tree window
+
+class CallTreeWindow(TreeWindowBase):
+
+    def __init__(self, glb, parent=None, thread_at_time=None):
+        super(CallTreeWindow, self).__init__(parent)
+
+        self.model = LookupCreateModel("Call Tree", lambda x=glb: CallTreeModel(x))
+
+        self.view.setModel(self.model)
+
+        for c, w in ((0, 230), (1, 100), (2, 100), (3, 70), (4, 70), (5, 100)):
+            self.view.setColumnWidth(c, w)
+
+        self.find_bar = FindBar(self, self)
+
+        self.vbox = VBox(self.view, self.find_bar.Widget())
+
+        self.setWidget(self.vbox.Widget())
+
+        AddSubWindow(glb.mainwindow.mdi_area, self, "Call Tree")
+
+        if thread_at_time:
+            self.DisplayThreadAtTime(*thread_at_time)
+
+    def DisplayThreadAtTime(self, comm_id, thread_id, time):
+        parent = QModelIndex()
+        for dbid in (comm_id, thread_id):
+            found = False
+            n = self.model.rowCount(parent)
+            for row in range(n):
+                child = self.model.index(row, 0, parent)
+                if child.internalPointer().dbid == dbid:
+                    found = True
+                    self.view.setExpanded(parent, True)
+                    self.view.setCurrentIndex(child)
+                    parent = child
+                    break
+            if not found:
+                return
+        found = False
+        while True:
+            n = self.model.rowCount(parent)
+            if not n:
+                return
+            last_child = None
+            for row in range(n):
+                self.view.setExpanded(parent, True)
+                child = self.model.index(row, 0, parent)
+                child_call_time = child.internalPointer().call_time
+                if child_call_time < time:
+                    last_child = child
+                elif child_call_time == time:
+                    self.view.setCurrentIndex(child)
+                    return
+                elif child_call_time > time:
+                    break
+            if not last_child:
+                if not found:
+                    child = self.model.index(0, 0, parent)
+                    self.view.setExpanded(parent, True)
+                    self.view.setCurrentIndex(child)
+                return
+            found = True
+            self.view.setExpanded(parent, True)
+            self.view.setCurrentIndex(last_child)
+            parent = last_child
+
+# ExecComm() gets the comm_id of the command string that was set when the process exec'd i.e. the program name
+
+def ExecComm(db, thread_id, time):
+    query = QSqlQuery(db)
+    QueryExec(query, "SELECT comm_threads.comm_id, comms.c_time, comms.exec_flag"
+                " FROM comm_threads"
+                " INNER JOIN comms ON comms.id = comm_threads.comm_id"
+                " WHERE comm_threads.thread_id = " + str(thread_id) +
+                " ORDER BY comms.c_time, comms.id")
+    first = None
+    last = None
+    while query.next():
+        if first is None:
+            first = query.value(0)
+        if query.value(2) and Decimal(query.value(1)) <= Decimal(time):
+            last = query.value(0)
+    if not(last is None):
+        return last
+    return first
+
+# Container for (x, y) data
+
+class XY():
+    def __init__(self, x=0, y=0):
+        self.x = x
+        self.y = y
+
+    def __str__(self):
+        return "XY({}, {})".format(str(self.x), str(self.y))
+
+# Container for sub-range data
+
+class Subrange():
+    def __init__(self, lo=0, hi=0):
+        self.lo = lo
+        self.hi = hi
+
+    def __str__(self):
+        return "Subrange({}, {})".format(str(self.lo), str(self.hi))
+
+# Graph data region base class
+
+class GraphDataRegion(object):
+
+    def __init__(self, key, title = "", ordinal = ""):
+        self.key = key
+        self.title = title
+        self.ordinal = ordinal
+
+# Function to sort GraphDataRegion
+
+def GraphDataRegionOrdinal(data_region):
+    return data_region.ordinal
+
+# Attributes for a graph region
+
+class GraphRegionAttribute():
+
+    def __init__(self, colour):
+        self.colour = colour
+
+# Switch graph data region represents a task
+
+class SwitchGraphDataRegion(GraphDataRegion):
+
+    def __init__(self, key, exec_comm_id, pid, tid, comm, thread_id, comm_id):
+        super(SwitchGraphDataRegion, self).__init__(key)
+
+        self.title = str(pid) + " / " + str(tid) + " " + comm
+        # Order graph legend within exec comm by pid / tid / time
+        self.ordinal = str(pid).rjust(16) + str(exec_comm_id).rjust(8) + str(tid).rjust(16)
+        self.exec_comm_id = exec_comm_id
+        self.pid = pid
+        self.tid = tid
+        self.comm = comm
+        self.thread_id = thread_id
+        self.comm_id = comm_id
+
+# Graph data point
+
+class GraphDataPoint():
+
+    def __init__(self, data, index, x, y, altx=None, alty=None, hregion=None, vregion=None):
+        self.data = data
+        self.index = index
+        self.x = x
+        self.y = y
+        self.altx = altx
+        self.alty = alty
+        self.hregion = hregion
+        self.vregion = vregion
+
+# Graph data (single graph) base class
+
+class GraphData(object):
+
+    def __init__(self, collection, xbase=Decimal(0), ybase=Decimal(0)):
+        self.collection = collection
+        self.points = []
+        self.xbase = xbase
+        self.ybase = ybase
+        self.title = ""
+
+    def AddPoint(self, x, y, altx=None, alty=None, hregion=None, vregion=None):
+        index = len(self.points)
+
+        x = float(Decimal(x) - self.xbase)
+        y = float(Decimal(y) - self.ybase)
+
+        self.points.append(GraphDataPoint(self, index, x, y, altx, alty, hregion, vregion))
+
+    def XToData(self, x):
+        return Decimal(x) + self.xbase
+
+    def YToData(self, y):
+        return Decimal(y) + self.ybase
+
+# Switch graph data (for one CPU)
+
+class SwitchGraphData(GraphData):
+
+    def __init__(self, db, collection, cpu, xbase):
+        super(SwitchGraphData, self).__init__(collection, xbase)
+
+        self.cpu = cpu
+        self.title = "CPU " + str(cpu)
+        self.SelectSwitches(db)
+
+    def SelectComms(self, db, thread_id, last_comm_id, start_time, end_time):
+        query = QSqlQuery(db)
+        QueryExec(query, "SELECT id, c_time"
+                    " FROM comms"
+                    " WHERE c_thread_id = " + str(thread_id) +
+                    "   AND exec_flag = " + self.collection.glb.dbref.TRUE +
+                    "   AND c_time >= " + str(start_time) +
+                    "   AND c_time <= " + str(end_time) +
+                    " ORDER BY c_time, id")
+        while query.next():
+            comm_id = query.value(0)
+            if comm_id == last_comm_id:
+                continue
+            time = query.value(1)
+            hregion = self.HRegion(db, thread_id, comm_id, time)
+            self.AddPoint(time, 1000, None, None, hregion)
+
+    def SelectSwitches(self, db):
+        last_time = None
+        last_comm_id = None
+        last_thread_id = None
+        query = QSqlQuery(db)
+        QueryExec(query, "SELECT time, thread_out_id, thread_in_id, comm_out_id, comm_in_id, flags"
+                    " FROM context_switches"
+                    " WHERE machine_id = " + str(self.collection.machine_id) +
+                    "   AND cpu = " + str(self.cpu) +
+                    " ORDER BY time, id")
+        while query.next():
+            flags = int(query.value(5))
+            if flags & 1:
+                # Schedule-out: detect and add exec's
+                if last_thread_id == query.value(1) and last_comm_id is not None and last_comm_id != query.value(3):
+                    self.SelectComms(db, last_thread_id, last_comm_id, last_time, query.value(0))
+                continue
+            # Schedule-in: add data point
+            if len(self.points) == 0:
+                start_time = self.collection.glb.StartTime(self.collection.machine_id)
+                hregion = self.HRegion(db, query.value(1), query.value(3), start_time)
+                self.AddPoint(start_time, 1000, None, None, hregion)
+            time = query.value(0)
+            comm_id = query.value(4)
+            thread_id = query.value(2)
+            hregion = self.HRegion(db, thread_id, comm_id, time)
+            self.AddPoint(time, 1000, None, None, hregion)
+            last_time = time
+            last_comm_id = comm_id
+            last_thread_id = thread_id
+
+    def NewHRegion(self, db, key, thread_id, comm_id, time):
+        exec_comm_id = ExecComm(db, thread_id, time)
+        query = QSqlQuery(db)
+        QueryExec(query, "SELECT pid, tid FROM threads WHERE id = " + str(thread_id))
+        if query.next():
+            pid = query.value(0)
+            tid = query.value(1)
+        else:
+            pid = -1
+            tid = -1
+        query = QSqlQuery(db)
+        QueryExec(query, "SELECT comm FROM comms WHERE id = " + str(comm_id))
+        if query.next():
+            comm = query.value(0)
+        else:
+            comm = ""
+        return SwitchGraphDataRegion(key, exec_comm_id, pid, tid, comm, thread_id, comm_id)
+
+    def HRegion(self, db, thread_id, comm_id, time):
+        key = str(thread_id) + ":" + str(comm_id)
+        hregion = self.collection.LookupHRegion(key)
+        if hregion is None:
+            hregion = self.NewHRegion(db, key, thread_id, comm_id, time)
+            self.collection.AddHRegion(key, hregion)
+        return hregion
+
+# Graph data collection (multiple related graphs) base class
+
+class GraphDataCollection(object):
+
+    def __init__(self, glb):
+        self.glb = glb
+        self.data = []
+        self.hregions = {}
+        self.xrangelo = None
+        self.xrangehi = None
+        self.yrangelo = None
+        self.yrangehi = None
+        self.dp = XY(0, 0)
+
+    def AddGraphData(self, data):
+        self.data.append(data)
+
+    def LookupHRegion(self, key):
+        if key in self.hregions:
+            return self.hregions[key]
+        return None
+
+    def AddHRegion(self, key, hregion):
+        self.hregions[key] = hregion
+
+# Switch graph data collection (SwitchGraphData for each CPU)
+
+class SwitchGraphDataCollection(GraphDataCollection):
+
+    def __init__(self, glb, db, machine_id):
+        super(SwitchGraphDataCollection, self).__init__(glb)
+
+        self.machine_id = machine_id
+        self.cpus = self.SelectCPUs(db)
+
+        self.xrangelo = glb.StartTime(machine_id)
+        self.xrangehi = glb.FinishTime(machine_id)
+
+        self.yrangelo = Decimal(0)
+        self.yrangehi = Decimal(1000)
+
+        for cpu in self.cpus:
+            self.AddGraphData(SwitchGraphData(db, self, cpu, self.xrangelo))
+
+    def SelectCPUs(self, db):
+        cpus = []
+        query = QSqlQuery(db)
+        QueryExec(query, "SELECT DISTINCT cpu"
+                    " FROM context_switches"
+                    " WHERE machine_id = " + str(self.machine_id))
+        while query.next():
+            cpus.append(int(query.value(0)))
+        return sorted(cpus)
+
+# Switch graph data graphics item displays the graphed data
+
+class SwitchGraphDataGraphicsItem(QGraphicsItem):
+
+    def __init__(self, data, graph_width, graph_height, attrs, event_handler, parent=None):
+        super(SwitchGraphDataGraphicsItem, self).__init__(parent)
+
+        self.data = data
+        self.graph_width = graph_width
+        self.graph_height = graph_height
+        self.attrs = attrs
+        self.event_handler = event_handler
+        self.setAcceptHoverEvents(True)
+
+    def boundingRect(self):
+        return QRectF(0, 0, self.graph_width, self.graph_height)
+
+    def PaintPoint(self, painter, last, x):
+        if not(last is None or last.hregion.pid == 0 or x < self.attrs.subrange.x.lo):
+            if last.x < self.attrs.subrange.x.lo:
+                x0 = self.attrs.subrange.x.lo
+            else:
+                x0 = last.x
+            if x > self.attrs.subrange.x.hi:
+                x1 = self.attrs.subrange.x.hi
+            else:
+                x1 = x - 1
+            x0 = self.attrs.XToPixel(x0)
+            x1 = self.attrs.XToPixel(x1)
+
+            y0 = self.attrs.YToPixel(last.y)
+
+            colour = self.attrs.region_attributes[last.hregion.key].colour
+
+            width = x1 - x0 + 1
+            if width < 2:
+                painter.setPen(colour)
+                painter.drawLine(x0, self.graph_height - y0, x0, self.graph_height)
+            else:
+                painter.fillRect(x0, self.graph_height - y0, width, self.graph_height - 1, colour)
+
+    def paint(self, painter, _option, _widget):
+        last = None
+        for point in self.data.points:
+            self.PaintPoint(painter, last, point.x)
+            if point.x > self.attrs.subrange.x.hi:
+                break
+            last = point
+        self.PaintPoint(painter, last, self.attrs.subrange.x.hi + 1)
+
+    def BinarySearchPoint(self, target):
+        lower_pos = 0
+        higher_pos = len(self.data.points)
+        while True:
+            pos = int((lower_pos + higher_pos) / 2)
+            val = self.data.points[pos].x
+            if target >= val:
+                lower_pos = pos
+            else:
+                higher_pos = pos
+            if higher_pos <= lower_pos + 1:
+                return lower_pos
+
+    def XPixelToData(self, x):
+        x = self.attrs.PixelToX(x)
+        if x < self.data.points[0].x:
+            x = 0
+            pos = 0
+            low = True
+        else:
+            pos = self.BinarySearchPoint(x)
+            low = False
+        return (low, pos, self.data.XToData(x))
+
+    def EventToData(self, event):
+        no_data = (None,) * 4
+        if len(self.data.points) < 1:
+            return no_data
+        x = event.pos().x()
+        if x < 0:
+            return no_data
+        _low0, pos0, time_from = self.XPixelToData(x)
+        low1, pos1, time_to = self.XPixelToData(x + 1)
+        hregions = set()
+        hregion_times = []
+        if not low1:
+            for i in range(pos0, pos1 + 1):
+                hregion = self.data.points[i].hregion
+                hregions.add(hregion)
+                if i == pos0:
+                    time = time_from
+                else:
+                    time = self.data.XToData(self.data.points[i].x)
+                hregion_times.append((hregion, time))
+        return (time_from, time_to, hregions, hregion_times)
+
+    def hoverMoveEvent(self, event):
+        time_from, time_to, hregions, _hregion_times = self.EventToData(event)
+        if time_from is not None:
+            self.event_handler.PointEvent(self.data.cpu, time_from, time_to, hregions)
+
+    def hoverLeaveEvent(self, _event):
+        self.event_handler.NoPointEvent()
+
+    def mousePressEvent(self, event):
+        if event.button() != Qt.RightButton:
+            super(SwitchGraphDataGraphicsItem, self).mousePressEvent(event)
+            return
+        _time_from, _time_to, _hregions, hregion_times = self.EventToData(event)
+        if hregion_times:
+            self.event_handler.RightClickEvent(self.data.cpu, hregion_times, event.screenPos())
+
+# X-axis graphics item
+
+class XAxisGraphicsItem(QGraphicsItem):
+
+    def __init__(self, width, parent=None):
+        super(XAxisGraphicsItem, self).__init__(parent)
+
+        self.width = width
+        self.max_mark_sz = 4
+        self.height = self.max_mark_sz + 1
+
+    def boundingRect(self):
+        return QRectF(0, 0, self.width, self.height)
+
+    def Step(self):
+        attrs = self.parentItem().attrs
+        subrange = attrs.subrange.x
+        t = subrange.hi - subrange.lo
+        s = (3.0 * t) / self.width
+        n = 1.0
+        while s > n:
+            n = n * 10.0
+        return n
+
+    def PaintMarks(self, painter, at_y, lo, hi, step, i):
+        attrs = self.parentItem().attrs
+        x = lo
+        while x <= hi:
+            xp = attrs.XToPixel(x)
+            if i % 10:
+                if i % 5:
+                    sz = 1
+                else:
+                    sz = 2
+            else:
+                sz = self.max_mark_sz
+                i = 0
+            painter.drawLine(xp, at_y, xp, at_y + sz)
+            x += step
+            i += 1
+
+    def paint(self, painter, _option, _widget):
+        # Using QPainter::drawLine(int x1, int y1, int x2, int y2) so x2 = width -1
+        painter.drawLine(0, 0, self.width - 1, 0)
+        n = self.Step()
+        attrs = self.parentItem().attrs
+        subrange = attrs.subrange.x
+        if subrange.lo:
+            x_offset = n - (subrange.lo % n)
+        else:
+            x_offset = 0.0
+        x = subrange.lo + x_offset
+        i = (x / n) % 10
+        self.PaintMarks(painter, 0, x, subrange.hi, n, i)
+
+    def ScaleDimensions(self):
+        n = self.Step()
+        attrs = self.parentItem().attrs
+        lo = attrs.subrange.x.lo
+        hi = (n * 10.0) + lo
+        width = attrs.XToPixel(hi)
+        if width > 500:
+            width = 0
+        return (n, lo, hi, width)
+
+    def PaintScale(self, painter, at_x, at_y):
+        n, lo, hi, width = self.ScaleDimensions()
+        if not width:
+            return
+        painter.drawLine(at_x, at_y, at_x + width, at_y)
+        self.PaintMarks(painter, at_y, lo, hi, n, 0)
+
+    def ScaleWidth(self):
+        _n, _lo, _hi, width = self.ScaleDimensions()
+        return width
+
+    def ScaleHeight(self):
+        return self.height
+
+    def ScaleUnit(self):
+        return self.Step() * 10
+
+# Scale graphics item base class
+
+class ScaleGraphicsItem(QGraphicsItem):
+
+    def __init__(self, axis, parent=None):
+        super(ScaleGraphicsItem, self).__init__(parent)
+        self.axis = axis
+
+    def boundingRect(self):
+        scale_width = self.axis.ScaleWidth()
+        if not scale_width:
+            return QRectF()
+        return QRectF(0, 0, self.axis.ScaleWidth() + 100, self.axis.ScaleHeight())
+
+    def paint(self, painter, _option, _widget):
+        scale_width = self.axis.ScaleWidth()
+        if not scale_width:
+            return
+        self.axis.PaintScale(painter, 0, 5)
+        x = scale_width + 4
+        painter.drawText(QPointF(x, 10), self.Text())
+
+    def Unit(self):
+        return self.axis.ScaleUnit()
+
+    def Text(self):
+        return ""
+
+# Switch graph scale graphics item
+
+class SwitchScaleGraphicsItem(ScaleGraphicsItem):
+
+    def Text(self):
+        unit = self.Unit()
+        if unit >= 1000000000:
+            unit = int(unit / 1000000000)
+            us = "s"
+        elif unit >= 1000000:
+            unit = int(unit / 1000000)
+            us = "ms"
+        elif unit >= 1000:
+            unit = int(unit / 1000)
+            us = "us"
+        else:
+            unit = int(unit)
+            us = "ns"
+        return " = " + str(unit) + " " + us
+
+# Switch graph graphics item contains graph title, scale, x/y-axis, and the graphed data
+
+class SwitchGraphGraphicsItem(QGraphicsItem):
+
+    def __init__(self, collection, data, attrs, event_handler, first, parent=None):
+        super(SwitchGraphGraphicsItem, self).__init__(parent)
+        self.collection = collection
+        self.data = data
+        self.attrs = attrs
+        self.event_handler = event_handler
+
+        margin = 20
+        title_width = 50
+
+        self.title_graphics = QGraphicsSimpleTextItem(data.title, self)
+
+        self.title_graphics.setPos(margin, margin)
+        graph_width = attrs.XToPixel(attrs.subrange.x.hi) + 1
+        graph_height = attrs.YToPixel(attrs.subrange.y.hi) + 1
+
+        self.graph_origin_x = margin + title_width + margin
+        self.graph_origin_y = graph_height + margin
+
+        _x_axis_size = 1
+        y_axis_size = 1
+        self.yline = QGraphicsLineItem(0, 0, 0, graph_height, self)
+
+        self.x_axis = XAxisGraphicsItem(graph_width, self)
+        self.x_axis.setPos(self.graph_origin_x, self.graph_origin_y + 1)
+
+        if first:
+            self.scale_item = SwitchScaleGraphicsItem(self.x_axis, self)
+            self.scale_item.setPos(self.graph_origin_x, self.graph_origin_y + 10)
+
+        self.yline.setPos(self.graph_origin_x - y_axis_size, self.graph_origin_y - graph_height)
+
+        self.axis_point = QGraphicsLineItem(0, 0, 0, 0, self)
+        self.axis_point.setPos(self.graph_origin_x - 1, self.graph_origin_y +1)
+
+        self.width = self.graph_origin_x + graph_width + margin
+        self.height = self.graph_origin_y + margin
+
+        self.graph = SwitchGraphDataGraphicsItem(data, graph_width, graph_height, attrs, event_handler, self)
+        self.graph.setPos(self.graph_origin_x, self.graph_origin_y - graph_height)
+
+        if parent and 'EnableRubberBand' in dir(parent):
+            parent.EnableRubberBand(self.graph_origin_x, self.graph_origin_x + graph_width - 1, self)
+
+    def boundingRect(self):
+        return QRectF(0, 0, self.width, self.height)
+
+    def paint(self, painter, option, widget):
+        pass
+
+    def RBXToPixel(self, x):
+        return self.attrs.PixelToX(x - self.graph_origin_x)
+
+    def RBXRangeToPixel(self, x0, x1):
+        return (self.RBXToPixel(x0), self.RBXToPixel(x1 + 1))
+
+    def RBPixelToTime(self, x):
+        if x < self.data.points[0].x:
+            return self.data.XToData(0)
+        return self.data.XToData(x)
+
+    def RBEventTimes(self, x0, x1):
+        x0, x1 = self.RBXRangeToPixel(x0, x1)
+        time_from = self.RBPixelToTime(x0)
+        time_to = self.RBPixelToTime(x1)
+        return (time_from, time_to)
+
+    def RBEvent(self, x0, x1):
+        time_from, time_to = self.RBEventTimes(x0, x1)
+        self.event_handler.RangeEvent(time_from, time_to)
+
+    def RBMoveEvent(self, x0, x1):
+        if x1 < x0:
+            x0, x1 = x1, x0
+        self.RBEvent(x0, x1)
+
+    def RBReleaseEvent(self, x0, x1, selection_state):
+        if x1 < x0:
+            x0, x1 = x1, x0
+        x0, x1 = self.RBXRangeToPixel(x0, x1)
+        self.event_handler.SelectEvent(x0, x1, selection_state)
+
+# Graphics item to draw a vertical bracket (used to highlight "forward" sub-range)
+
+class VerticalBracketGraphicsItem(QGraphicsItem):
+
+    def __init__(self, parent=None):
+        super(VerticalBracketGraphicsItem, self).__init__(parent)
+
+        self.width = 0
+        self.height = 0
+        self.hide()
+
+    def SetSize(self, width, height):
+        self.width = width + 1
+        self.height = height + 1
+
+    def boundingRect(self):
+        return QRectF(0, 0, self.width, self.height)
+
+    def paint(self, painter, _option, _widget):
+        colour = QColor(255, 255, 0, 32)
+        painter.fillRect(0, 0, self.width, self.height, colour)
+        x1 = self.width - 1
+        y1 = self.height - 1
+        painter.drawLine(0, 0, x1, 0)
+        painter.drawLine(0, 0, 0, 3)
+        painter.drawLine(x1, 0, x1, 3)
+        painter.drawLine(0, y1, x1, y1)
+        painter.drawLine(0, y1, 0, y1 - 3)
+        painter.drawLine(x1, y1, x1, y1 - 3)
+
+# Graphics item to contain graphs arranged vertically
+
+class VertcalGraphSetGraphicsItem(QGraphicsItem):
+
+    def __init__(self, collection, attrs, event_handler, child_class, parent=None):
+        super(VertcalGraphSetGraphicsItem, self).__init__(parent)
+
+        self.collection = collection
+
+        self.top = 10
+
+        self.width = 0
+        self.height = self.top
+
+        self.rubber_band = None
+        self.rb_enabled = False
+        self.rb_in_view = False
+        self.rb_xlo = 0
+        self.rb_xhi = 0
+        self.rb_event_handler = None
+        self.rb_origin = None
+
+        first = True
+        for data in collection.data:
+            child = child_class(collection, data, attrs, event_handler, first, self)
+            child.setPos(0, self.height + 1)
+            rect = child.boundingRect()
+            if rect.right() > self.width:
+                self.width = rect.right()
+            self.height = self.height + rect.bottom() + 1
+            first = False
+
+        self.bracket = VerticalBracketGraphicsItem(self)
+
+    def EnableRubberBand(self, xlo, xhi, rb_event_handler):
+        if self.rb_enabled:
+            return
+        self.rb_enabled = True
+        self.rb_in_view = False
+        self.setAcceptedMouseButtons(Qt.LeftButton)
+        self.rb_xlo = xlo
+        self.rb_xhi = xhi
+        self.rb_event_handler = rb_event_handler
+
+    def mousePressEvent(self, event):
+        if self.rb_enabled:
+            self.MousePressEvent(event)
+
+    def mouseMoveEvent(self, event):
+        if self.rb_enabled:
+            self.MouseMoveEvent(event)
+
+    def mouseReleaseEvent(self, event):
+        if self.rb_enabled:
+            self.MouseReleaseEvent(event)
+
+    def boundingRect(self):
+        return QRectF(0, 0, self.width, self.height)
+
+    def paint(self, painter, option, widget):
+        pass
+
+    def RubberBandParent(self):
+        scene = self.scene()
+        view = scene.views()[0]
+        viewport = view.viewport()
+        return viewport
+
+    def RubberBandSetGeometry(self, rect):
+        scene_rectf = self.mapRectToScene(QRectF(rect))
+        scene = self.scene()
+        view = scene.views()[0]
+        poly = view.mapFromScene(scene_rectf)
+        self.rubber_band.setGeometry(poly.boundingRect())
+
+    def SetSelection(self, selection_state):
+        if self.rubber_band:
+            if selection_state:
+                self.RubberBandSetGeometry(selection_state)
+                self.rubber_band.show()
+            else:
+                self.rubber_band.hide()
+
+    def SetBracket(self, rect):
+        if rect:
+            x, y, width, height = rect.x(), rect.y(), rect.width(), rect.height()
+            self.bracket.setPos(x, y)
+            self.bracket.SetSize(width, height)
+            self.bracket.show()
+        else:
+            self.bracket.hide()
+
+    def RubberBandX(self, event):
+        x = event.pos().toPoint().x()
+        if x < self.rb_xlo:
+            x = self.rb_xlo
+        elif x > self.rb_xhi:
+            x = self.rb_xhi
+        else:
+            self.rb_in_view = True
+        return x
+
+    def RubberBandRect(self, x):
+        if self.rb_origin.x() <= x:
+            width = x - self.rb_origin.x()
+            rect = QRect(self.rb_origin, QSize(width, self.height))
+        else:
+            width = self.rb_origin.x() - x
+            top_left = QPoint(self.rb_origin.x() - width, self.rb_origin.y())
+            rect = QRect(top_left, QSize(width, self.height))
+        return rect
+
+    def MousePressEvent(self, event):
+        self.rb_in_view = False
+        x = self.RubberBandX(event)
+        self.rb_origin = QPoint(x, self.top)
+        if self.rubber_band is None:
+            self.rubber_band = QRubberBand(QRubberBand.Rectangle, self.RubberBandParent())
+        self.RubberBandSetGeometry(QRect(self.rb_origin, QSize(0, self.height)))
+        if self.rb_in_view:
+            self.rubber_band.show()
+            self.rb_event_handler.RBMoveEvent(x, x)
+        else:
+            self.rubber_band.hide()
+
+    def MouseMoveEvent(self, event):
+        x = self.RubberBandX(event)
+        rect = self.RubberBandRect(x)
+        self.RubberBandSetGeometry(rect)
+        if self.rb_in_view:
+            self.rubber_band.show()
+            self.rb_event_handler.RBMoveEvent(self.rb_origin.x(), x)
+
+    def MouseReleaseEvent(self, event):
+        x = self.RubberBandX(event)
+        if self.rb_in_view:
+            selection_state = self.RubberBandRect(x)
+        else:
+            selection_state = None
+        self.rb_event_handler.RBReleaseEvent(self.rb_origin.x(), x, selection_state)
+
+# Switch graph legend data model
+
+class SwitchGraphLegendModel(QAbstractTableModel):
+
+    def __init__(self, collection, region_attributes, parent=None):
+        super(SwitchGraphLegendModel, self).__init__(parent)
+
+        self.region_attributes = region_attributes
+
+        self.child_items = sorted(collection.hregions.values(), key=GraphDataRegionOrdinal)
+        self.child_count = len(self.child_items)
+
+        self.highlight_set = set()
+
+        self.column_headers = ("pid", "tid", "comm")
+
+    def rowCount(self, _parent):
+        return self.child_count
+
+    def headerData(self, section, orientation, role):
+        if role != Qt.DisplayRole:
+            return None
+        if orientation != Qt.Horizontal:
+            return None
+        return self.columnHeader(section)
+
+    def index(self, row, column, _parent):
+        return self.createIndex(row, column, self.child_items[row])
+
+    def columnCount(self, _parent=None):
+        return len(self.column_headers)
+
+    def columnHeader(self, column):
+        return self.column_headers[column]
+
+    def data(self, index, role):
+        if role == Qt.BackgroundRole:
+            child = self.child_items[index.row()]
+            if child in self.highlight_set:
+                return self.region_attributes[child.key].colour
+            return None
+        if role == Qt.ForegroundRole:
+            child = self.child_items[index.row()]
+            if child in self.highlight_set:
+                return QColor(255, 255, 255)
+            return self.region_attributes[child.key].colour
+        if role != Qt.DisplayRole:
+            return None
+        hregion = self.child_items[index.row()]
+        col = index.column()
+        if col == 0:
+            return hregion.pid
+        if col == 1:
+            return hregion.tid
+        if col == 2:
+            return hregion.comm
+        return None
+
+    def SetHighlight(self, row, _set_highlight):
+        child = self.child_items[row]
+        top_left = self.createIndex(row, 0, child)
+        bottom_right = self.createIndex(row, len(self.column_headers) - 1, child)
+        self.dataChanged.emit(top_left, bottom_right)
+
+    def Highlight(self, highlight_set):
+        for row in range(self.child_count):
+            child = self.child_items[row]
+            if child in self.highlight_set:
+                if child not in highlight_set:
+                    self.SetHighlight(row, False)
+            elif child in highlight_set:
+                self.SetHighlight(row, True)
+        self.highlight_set = highlight_set
+
+# Switch graph legend is a table
+
+class SwitchGraphLegend(QWidget):
+
+    def __init__(self, collection, region_attributes, parent=None):
+        super(SwitchGraphLegend, self).__init__(parent)
+
+        self.data_model = SwitchGraphLegendModel(collection, region_attributes)
+
+        self.model = QSortFilterProxyModel()
+        self.model.setSourceModel(self.data_model)
+
+        self.view = QTableView()
+        self.view.setModel(self.model)
+        self.view.setEditTriggers(QAbstractItemView.NoEditTriggers)
+        self.view.verticalHeader().setVisible(False)
+        self.view.sortByColumn(-1, Qt.AscendingOrder)
+        self.view.setSortingEnabled(True)
+        self.view.resizeColumnsToContents()
+        self.view.resizeRowsToContents()
+
+        self.vbox = VBoxLayout(self.view)
+        self.setLayout(self.vbox)
+
+        sz1 = self.view.columnWidth(0) + self.view.columnWidth(1) + self.view.columnWidth(2) + 2
+        sz1 = sz1 + self.view.verticalScrollBar().sizeHint().width()
+        self.saved_size = sz1
+
+    def resizeEvent(self, event):
+        self.saved_size = self.size().width()
+        super(SwitchGraphLegend, self).resizeEvent(event)
+
+    def Highlight(self, highlight_set):
+        self.data_model.Highlight(highlight_set)
+        self.update()
+
+    def changeEvent(self, event):
+        if event.type() == QEvent.FontChange:
+            self.view.resizeRowsToContents()
+            self.view.resizeColumnsToContents()
+            # Need to resize rows again after column resize
+            self.view.resizeRowsToContents()
+        super(SwitchGraphLegend, self).changeEvent(event)
+
+# Random colour generation
+
+def RGBColourTooLight(r, g, _b):
+    if g > 230:
+        return True
+    if g <= 160:
+        return False
+    if r <= 180 and g <= 180:
+        return False
+    if r < 60:
+        return False
+    return True
+
+def GenerateColours(x):
+    cs = [0]
+    for i in range(1, x):
+        cs.append(int((255.0 / i) + 0.5))
+    colours = []
+    for r in cs:
+        for g in cs:
+            for b in cs:
+                # Exclude black and colours that look too light against a white background
+                if (r, g, b) == (0, 0, 0) or RGBColourTooLight(r, g, b):
+                    continue
+                colours.append(QColor(r, g, b))
+    return colours
+
+def GenerateNColours(n):
+    for x in range(2, n + 2):
+        colours = GenerateColours(x)
+        if len(colours) >= n:
+            return colours
+    return []
+
+def GenerateNRandomColours(n, seed):
+    colours = GenerateNColours(n)
+    random.seed(seed)
+    random.shuffle(colours)
+    return colours
+
+# Graph attributes, in particular the scale and subrange that change when zooming
+
+class GraphAttributes():
+
+    def __init__(self, scale, subrange, region_attributes, dp):
+        self.scale = scale
+        self.subrange = subrange
+        self.region_attributes = region_attributes
+        # Rounding avoids errors due to finite floating point precision
+        self.dp = dp	# data decimal places
+        self.Update()
+
+    def XToPixel(self, x):
+        return int(round((x - self.subrange.x.lo) * self.scale.x, self.pdp.x))
+
+    def YToPixel(self, y):
+        return int(round((y - self.subrange.y.lo) * self.scale.y, self.pdp.y))
+
+    def PixelToXRounded(self, px):
+        return round((round(px, 0) / self.scale.x), self.dp.x) + self.subrange.x.lo
+
+    def PixelToYRounded(self, py):
+        return round((round(py, 0) / self.scale.y), self.dp.y) + self.subrange.y.lo
+
+    def PixelToX(self, px):
+        x = self.PixelToXRounded(px)
+        if self.pdp.x == 0:
+            rt = self.XToPixel(x)
+            if rt > px:
+                return x - 1
+        return x
+
+    def PixelToY(self, py):
+        y = self.PixelToYRounded(py)
+        if self.pdp.y == 0:
+            rt = self.YToPixel(y)
+            if rt > py:
+                return y - 1
+        return y
+
+    def ToPDP(self, dp, scale):
+        # Calculate pixel decimal places:
+        #    (10 ** dp) is the minimum delta in the data
+        #    scale it to get the minimum delta in pixels
+        #    log10 gives the number of decimals places negatively
+        #    subtrace 1 to divide by 10
+        #    round to the lower negative number
+        #    change the sign to get the number of decimals positively
+        x = math.log10((10 ** dp) * scale)
+        if x < 0:
+            x -= 1
+            x = -int(math.floor(x) - 0.1)
+        else:
+            x = 0
+        return x
+
+    def Update(self):
+        x = self.ToPDP(self.dp.x, self.scale.x)
+        y = self.ToPDP(self.dp.y, self.scale.y)
+        self.pdp = XY(x, y) # pixel decimal places
+
+# Switch graph splitter which divides the CPU graphs from the legend
+
+class SwitchGraphSplitter(QSplitter):
+
+    def __init__(self, parent=None):
+        super(SwitchGraphSplitter, self).__init__(parent)
+
+        self.first_time = False
+
+    def resizeEvent(self, event):
+        if self.first_time:
+            self.first_time = False
+            sz1 = self.widget(1).view.columnWidth(0) + self.widget(1).view.columnWidth(1) + self.widget(1).view.columnWidth(2) + 2
+            sz1 = sz1 + self.widget(1).view.verticalScrollBar().sizeHint().width()
+            sz0 = self.size().width() - self.handleWidth() - sz1
+            self.setSizes([sz0, sz1])
+        elif not(self.widget(1).saved_size is None):
+            sz1 = self.widget(1).saved_size
+            sz0 = self.size().width() - self.handleWidth() - sz1
+            self.setSizes([sz0, sz1])
+        super(SwitchGraphSplitter, self).resizeEvent(event)
+
+# Graph widget base class
+
+class GraphWidget(QWidget):
+
+    graph_title_changed = Signal(object)
+
+    def __init__(self, parent=None):
+        super(GraphWidget, self).__init__(parent)
+
+    def GraphTitleChanged(self, title):
+        self.graph_title_changed.emit(title)
+
+    def Title(self):
+        return ""
+
+# Display time in s, ms, us or ns
+
+def ToTimeStr(val):
+    val = Decimal(val)
+    if val >= 1000000000:
+        return "{} s".format((val / 1000000000).quantize(Decimal("0.000000001")))
+    if val >= 1000000:
+        return "{} ms".format((val / 1000000).quantize(Decimal("0.000001")))
+    if val >= 1000:
+        return "{} us".format((val / 1000).quantize(Decimal("0.001")))
+    return "{} ns".format(val.quantize(Decimal("1")))
+
+# Switch (i.e. context switch i.e. Time Chart by CPU) graph widget which contains the CPU graphs and the legend and control buttons
+
+class SwitchGraphWidget(GraphWidget):
+
+    def __init__(self, glb, collection, parent=None):
+        super(SwitchGraphWidget, self).__init__(parent)
+
+        self.glb = glb
+        self.collection = collection
+
+        self.back_state = []
+        self.forward_state = []
+        self.selection_state = (None, None)
+        self.fwd_rect = None
+        self.start_time = self.glb.StartTime(collection.machine_id)
+
+        i = 0
+        hregions = collection.hregions.values()
+        colours = GenerateNRandomColours(len(hregions), 1013)
+        region_attributes = {}
+        for hregion in hregions:
+            if hregion.pid == 0 and hregion.tid == 0:
+                region_attributes[hregion.key] = GraphRegionAttribute(QColor(0, 0, 0))
+            else:
+                region_attributes[hregion.key] = GraphRegionAttribute(colours[i])
+                i = i + 1
+
+        # Default to entire range
+        xsubrange = Subrange(0.0, float(collection.xrangehi - collection.xrangelo) + 1.0)
+        ysubrange = Subrange(0.0, float(collection.yrangehi - collection.yrangelo) + 1.0)
+        subrange = XY(xsubrange, ysubrange)
+
+        scale = self.GetScaleForRange(subrange)
+
+        self.attrs = GraphAttributes(scale, subrange, region_attributes, collection.dp)
+
+        self.item = VertcalGraphSetGraphicsItem(collection, self.attrs, self, SwitchGraphGraphicsItem)
+
+        self.scene = QGraphicsScene()
+        self.scene.addItem(self.item)
+
+        self.view = QGraphicsView(self.scene)
+        self.view.centerOn(0, 0)
+        self.view.setAlignment(Qt.AlignLeft | Qt.AlignTop)
+
+        self.legend = SwitchGraphLegend(collection, region_attributes)
+
+        self.splitter = SwitchGraphSplitter()
+        self.splitter.addWidget(self.view)
+        self.splitter.addWidget(self.legend)
+
+        self.point_label = QLabel("")
+        self.point_label.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
+
+        self.back_button = QToolButton()
+        self.back_button.setIcon(self.style().standardIcon(QStyle.SP_ArrowLeft))
+        self.back_button.setDisabled(True)
+        self.back_button.released.connect(self.Back)
+
+        self.forward_button = QToolButton()
+        self.forward_button.setIcon(self.style().standardIcon(QStyle.SP_ArrowRight))
+        self.forward_button.setDisabled(True)
+        self.forward_button.released.connect(self.Forward)
+
+        self.zoom_button = QToolButton()
+        self.zoom_button.setText("Zoom")
+        self.zoom_button.setDisabled(True)
+        self.zoom_button.released.connect(self.Zoom)
+
+        self.hbox = HBoxLayout(self.back_button, self.forward_button, self.zoom_button, self.point_label)
+
+        self.vbox = VBoxLayout(self.splitter, self.hbox)
+
+        self.setLayout(self.vbox)
+
+    def GetScaleForRangeX(self, xsubrange):
+        # Default graph 1000 pixels wide
+        dflt = 1000.0
+        r = xsubrange.hi - xsubrange.lo
+        return dflt / r
+
+    def GetScaleForRangeY(self, ysubrange):
+        # Default graph 50 pixels high
+        dflt = 50.0
+        r = ysubrange.hi - ysubrange.lo
+        return dflt / r
+
+    def GetScaleForRange(self, subrange):
+        # Default graph 1000 pixels wide, 50 pixels high
+        xscale = self.GetScaleForRangeX(subrange.x)
+        yscale = self.GetScaleForRangeY(subrange.y)
+        return XY(xscale, yscale)
+
+    def PointEvent(self, cpu, time_from, _time_to, hregions):
+        text = "CPU: " + str(cpu)
+        time_from = time_from.quantize(Decimal(1))
+        rel_time_from = time_from - self.glb.StartTime(self.collection.machine_id)
+        text = text + " Time: " + str(time_from) + " (+" + ToTimeStr(rel_time_from) + ")"
+        self.point_label.setText(text)
+        self.legend.Highlight(hregions)
+
+    def RightClickEvent(self, _cpu, hregion_times, pos):
+        if not IsSelectable(self.glb.db, "calls", "WHERE parent_id >= 0"):
+            return
+        menu = QMenu(self.view)
+        for hregion, time in hregion_times:
+            thread_at_time = (hregion.exec_comm_id, hregion.thread_id, time)
+            menu_text = "Show Call Tree for {} {}:{} at {}".format(hregion.comm, hregion.pid, hregion.tid, time)
+            menu.addAction(CreateAction(menu_text, "Show Call Tree", lambda a=None, args=thread_at_time: self.RightClickSelect(args), self.view))
+        menu.exec_(pos)
+
+    def RightClickSelect(self, args):
+        CallTreeWindow(self.glb, self.glb.mainwindow, thread_at_time=args)
+
+    def NoPointEvent(self):
+        self.point_label.setText("")
+        self.legend.Highlight({})
+
+    def RangeEvent(self, time_from, time_to):
+        time_from = time_from.quantize(Decimal(1))
+        time_to = time_to.quantize(Decimal(1))
+        if time_to <= time_from:
+            self.point_label.setText("")
+            return
+        rel_time_from = time_from - self.start_time
+        rel_time_to = time_to - self.start_time
+        text = " Time: " + str(time_from) + " (+" + ToTimeStr(rel_time_from) + ") to: " + str(time_to) + " (+" + ToTimeStr(rel_time_to) + ")"
+        text = text + " duration: " + ToTimeStr(time_to - time_from)
+        self.point_label.setText(text)
+
+    def BackState(self):
+        return (self.attrs.subrange, self.attrs.scale, self.selection_state, self.fwd_rect)
+
+    def PushBackState(self):
+        state = copy.deepcopy(self.BackState())
+        self.back_state.append(state)
+        self.back_button.setEnabled(True)
+
+    def PopBackState(self):
+        self.attrs.subrange, self.attrs.scale, self.selection_state, self.fwd_rect = self.back_state.pop()
+        self.attrs.Update()
+        if not self.back_state:
+            self.back_button.setDisabled(True)
+
+    def PushForwardState(self):
+        state = copy.deepcopy(self.BackState())
+        self.forward_state.append(state)
+        self.forward_button.setEnabled(True)
+
+    def PopForwardState(self):
+        self.attrs.subrange, self.attrs.scale, self.selection_state, self.fwd_rect = self.forward_state.pop()
+        self.attrs.Update()
+        if not self.forward_state:
+            self.forward_button.setDisabled(True)
+
+    def Title(self):
+        time_from = self.collection.xrangelo + Decimal(self.attrs.subrange.x.lo)
+        time_to = self.collection.xrangelo + Decimal(self.attrs.subrange.x.hi)
+        rel_time_from = time_from - self.start_time
+        rel_time_to = time_to - self.start_time
+        title = "+" + ToTimeStr(rel_time_from) + " to +" + ToTimeStr(rel_time_to)
+        title = title + " (" + ToTimeStr(time_to - time_from) + ")"
+        return title
+
+    def Update(self):
+        selected_subrange, selection_state = self.selection_state
+        self.item.SetSelection(selection_state)
+        self.item.SetBracket(self.fwd_rect)
+        self.zoom_button.setDisabled(selected_subrange is None)
+        self.GraphTitleChanged(self.Title())
+        self.item.update(self.item.boundingRect())
+
+    def Back(self):
+        if not self.back_state:
+            return
+        self.PushForwardState()
+        self.PopBackState()
+        self.Update()
+
+    def Forward(self):
+        if not self.forward_state:
+            return
+        self.PushBackState()
+        self.PopForwardState()
+        self.Update()
+
+    def SelectEvent(self, x0, x1, selection_state):
+        if selection_state is None:
+            selected_subrange = None
+        else:
+            if x1 - x0 < 1.0:
+                x1 += 1.0
+            selected_subrange = Subrange(x0, x1)
+        self.selection_state = (selected_subrange, selection_state)
+        self.zoom_button.setDisabled(selected_subrange is None)
+
+    def Zoom(self):
+        selected_subrange, selection_state = self.selection_state
+        if selected_subrange is None:
+            return
+        self.fwd_rect = selection_state
+        self.item.SetSelection(None)
+        self.PushBackState()
+        self.attrs.subrange.x = selected_subrange
+        self.forward_state = []
+        self.forward_button.setDisabled(True)
+        self.selection_state = (None, None)
+        self.fwd_rect = None
+        self.attrs.scale.x = self.GetScaleForRangeX(self.attrs.subrange.x)
+        self.attrs.Update()
+        self.Update()
+
+# Slow initialization - perform non-GUI initialization in a separate thread and put up a modal message box while waiting
+
+class SlowInitClass():
+
+    def __init__(self, glb, title, init_fn):
+        self.init_fn = init_fn
+        self.done = False
+        self.result = None
+
+        self.msg_box = QMessageBox(glb.mainwindow)
+        self.msg_box.setText("Initializing " + title + ". Please wait.")
+        self.msg_box.setWindowTitle("Initializing " + title)
+        self.msg_box.setWindowIcon(glb.mainwindow.style().standardIcon(QStyle.SP_MessageBoxInformation))
+
+        self.init_thread = Thread(self.ThreadFn, glb)
+        self.init_thread.done.connect(self.Done, Qt.QueuedConnection)
+
+        self.init_thread.start()
+
+    def Done(self):
+        self.msg_box.done(0)
+
+    def ThreadFn(self, glb):
+        conn_name = "SlowInitClass" + str(os.getpid())
+        db, _dbname = glb.dbref.Open(conn_name)
+        self.result = self.init_fn(db)
+        self.done = True
+        return (True, 0)
+
+    def Result(self):
+        while not self.done:
+            self.msg_box.exec_()
+        self.init_thread.wait()
+        return self.result
+
+def SlowInit(glb, title, init_fn):
+    init = SlowInitClass(glb, title, init_fn)
+    return init.Result()
+
+# Time chart by CPU window
+
+class TimeChartByCPUWindow(QMdiSubWindow):
+
+    def __init__(self, glb, parent=None):
+        super(TimeChartByCPUWindow, self).__init__(parent)
+
+        self.glb = glb
+        self.machine_id = glb.HostMachineId()
+        self.collection_name = "SwitchGraphDataCollection " + str(self.machine_id)
+
+        collection = LookupModel(self.collection_name)
+        if collection is None:
+            collection = SlowInit(glb, "Time Chart", self.Init)
+
+        self.widget = SwitchGraphWidget(glb, collection, self)
+        self.view = self.widget
+
+        self.base_title = "Time Chart by CPU"
+        self.setWindowTitle(self.base_title + self.widget.Title())
+        self.widget.graph_title_changed.connect(self.GraphTitleChanged)
+
+        self.setWidget(self.widget)
+
+        AddSubWindow(glb.mainwindow.mdi_area, self, self.windowTitle())
+
+    def Init(self, db):
+        return LookupCreateModel(self.collection_name, lambda : SwitchGraphDataCollection(self.glb, db, self.machine_id))
+
+    def GraphTitleChanged(self, title):
+        self.setWindowTitle(self.base_title + " : " + title)
+
+# Child data item  finder
+
+class ChildDataItemFinder():
+
+    def __init__(self, root):
+        self.root = root
+        self.value, self.direction, self.pattern, self.last_value, self.last_pattern = (None,) * 5
+        self.rows = []
+        self.pos = 0
+
+    def FindSelect(self):
+        self.rows = []
+        if self.pattern:
+            pattern = re.compile(self.value)
+            for child in self.root.child_items:
+                for column_data in child.data:
+                    if re.search(pattern, str(column_data)) is not None:
+                        self.rows.append(child.row)
+                        break
+        else:
+            for child in self.root.child_items:
+                for column_data in child.data:
+                    if self.value in str(column_data):
+                        self.rows.append(child.row)
+                        break
+
+    def FindValue(self):
+        self.pos = 0
+        if self.last_value != self.value or self.pattern != self.last_pattern:
+            self.FindSelect()
+        if not len(self.rows):
+            return -1
+        return self.rows[self.pos]
+
+    def FindThread(self):
+        if self.direction == 0 or self.value != self.last_value or self.pattern != self.last_pattern:
+            row = self.FindValue()
+        elif len(self.rows):
+            if self.direction > 0:
+                self.pos += 1
+                if self.pos >= len(self.rows):
+                    self.pos = 0
+            else:
+                self.pos -= 1
+                if self.pos < 0:
+                    self.pos = len(self.rows) - 1
+            row = self.rows[self.pos]
+        else:
+            row = -1
+        return (True, row)
+
+    def Find(self, value, direction, pattern, _context, callback):
+        self.value, self.direction, self.pattern, self.last_value, self.last_pattern = (value, direction,pattern, self.value, self.pattern)
+        # Use a thread so the UI is not blocked
+        thread = Thread(self.FindThread)
+        thread.done.connect(lambda row, t=thread, c=callback: self.FindDone(t, c, row), Qt.QueuedConnection)
+        thread.start()
+
+    def FindDone(self, _thread, callback, row):
+        callback(row)
+
+# Number of database records to fetch in one go
+
+glb_chunk_sz = 10000
+
+# Background process for SQL data fetcher
+
+class SQLFetcherProcess():
+
+    def __init__(self, dbref, sql, buffer, head, tail, fetch_count, fetching_done, process_target, wait_event, fetched_event, prep):
+        # Need a unique connection name
+        conn_name = "SQLFetcher" + str(os.getpid())
+        self.db, _dbname = dbref.Open(conn_name)
+        self.sql = sql
+        self.buffer = buffer
+        self.head = head
+        self.tail = tail
+        self.fetch_count = fetch_count
+        self.fetching_done = fetching_done
+        self.process_target = process_target
+        self.wait_event = wait_event
+        self.fetched_event = fetched_event
+        self.prep = prep
+        self.query = QSqlQuery(self.db)
+        self.query_limit = 0 if "$$last_id$$" in sql else 2
+        self.last_id = -1
+        self.fetched = 0
+        self.more = True
+        self.local_head = self.head.value
+        self.local_tail = self.tail.value
+
+    def Select(self):
+        if self.query_limit:
+            if self.query_limit == 1:
+                return
+            self.query_limit -= 1
+        stmt = self.sql.replace("$$last_id$$", str(self.last_id))
+        QueryExec(self.query, stmt)
+
+    def Next(self):
+        if not self.query.next():
+            self.Select()
+            if not self.query.next():
+                return None
+        self.last_id = self.query.value(0)
+        return self.prep(self.query)
+
+    def WaitForTarget(self):
+        while True:
+            self.wait_event.clear()
+            target = self.process_target.value
+            if target > self.fetched or target < 0:
+                break
+            self.wait_event.wait()
+        return target
+
+    def HasSpace(self, sz):
+        if self.local_tail <= self.local_head:
+            space = len(self.buffer) - self.local_head
+            if space > sz:
+                return True
+            if space >= glb_nsz:
+                # Use 0 (or space < glb_nsz) to mean there is no more at the top of the buffer
+                nd = pickle.dumps(0, pickle.HIGHEST_PROTOCOL)
+                self.buffer[self.local_head : self.local_head + len(nd)] = nd
+            self.local_head = 0
+        if self.local_tail - self.local_head > sz:
+            return True
+        return False
+
+    def WaitForSpace(self, sz):
+        if self.HasSpace(sz):
+            return
+        while True:
+            self.wait_event.clear()
+            self.local_tail = self.tail.value
+            if self.HasSpace(sz):
+                return
+            self.wait_event.wait()
+
+    def AddToBuffer(self, obj):
+        d = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
+        n = len(d)
+        nd = pickle.dumps(n, pickle.HIGHEST_PROTOCOL)
+        sz = n + glb_nsz
+        self.WaitForSpace(sz)
+        pos = self.local_head
+        self.buffer[pos : pos + len(nd)] = nd
+        self.buffer[pos + glb_nsz : pos + sz] = d
+        self.local_head += sz
+
+    def FetchBatch(self, batch_size):
+        fetched = 0
+        while batch_size > fetched:
+            obj = self.Next()
+            if obj is None:
+                self.more = False
+                break
+            self.AddToBuffer(obj)
+            fetched += 1
+        if fetched:
+            self.fetched += fetched
+            with self.fetch_count.get_lock():
+                self.fetch_count.value += fetched
+            self.head.value = self.local_head
+            self.fetched_event.set()
+
+    def Run(self):
+        while self.more:
+            target = self.WaitForTarget()
+            if target < 0:
+                break
+            batch_size = min(glb_chunk_sz, target - self.fetched)
+            self.FetchBatch(batch_size)
+        self.fetching_done.value = True
+        self.fetched_event.set()
+
+def SQLFetcherFn(*x):
+    process = SQLFetcherProcess(*x)
+    process.Run()
+
+# SQL data fetcher
+
+class SQLFetcher(QObject):
+
+    done = Signal(object)
+
+    def __init__(self, glb, sql, prep, process_data, parent=None):
+        super(SQLFetcher, self).__init__(parent)
+        self.process_data = process_data
+        self.more = True
+        self.target = 0
+        self.last_target = 0
+        self.fetched = 0
+        self.buffer_size = 16 * 1024 * 1024
+        self.buffer = Array(c_char, self.buffer_size, lock=False)
+        self.head = Value(c_longlong)
+        self.tail = Value(c_longlong)
+        self.local_tail = 0
+        self.fetch_count = Value(c_longlong)
+        self.fetching_done = Value(c_bool)
+        self.last_count = 0
+        self.process_target = Value(c_longlong)
+        self.wait_event = Event()
+        self.fetched_event = Event()
+        glb.AddInstanceToShutdownOnExit(self)
+        self.process = Process(target=SQLFetcherFn, args=(glb.dbref, sql, self.buffer, self.head, self.tail, self.fetch_count, self.fetching_done, self.process_target, self.wait_event, self.fetched_event, prep))
+        self.process.start()
+        self.thread = Thread(self.Thread)
+        self.thread.done.connect(self.ProcessData, Qt.QueuedConnection)
+        self.thread.start()
+
+    def Shutdown(self):
+        # Tell the thread and process to exit
+        self.process_target.value = -1
+        self.wait_event.set()
+        self.more = False
+        self.fetching_done.value = True
+        self.fetched_event.set()
+
+    def Thread(self):
+        if not self.more:
+            return True, 0
+        while True:
+            self.fetched_event.clear()
+            fetch_count = self.fetch_count.value
+            if fetch_count != self.last_count:
+                break
+            if self.fetching_done.value:
+                self.more = False
+                return True, 0
+            self.fetched_event.wait()
+        count = fetch_count - self.last_count
+        self.last_count = fetch_count
+        self.fetched += count
+        return False, count
+
+    def Fetch(self, nr):
+        if not self.more:
+            # -1 inidcates there are no more
+            return -1
+        result = self.fetched
+        extra = result + nr - self.target
+        if extra > 0:
+            self.target += extra
+            # process_target < 0 indicates shutting down
+            if self.process_target.value >= 0:
+                self.process_target.value = self.target
+            self.wait_event.set()
+        return result
+
+    def RemoveFromBuffer(self):
+        pos = self.local_tail
+        if len(self.buffer) - pos < glb_nsz:
+            pos = 0
+        n = pickle.loads(self.buffer[pos : pos + glb_nsz])
+        if n == 0:
+            pos = 0
+            n = pickle.loads(self.buffer[0 : glb_nsz])
+        pos += glb_nsz
+        obj = pickle.loads(self.buffer[pos : pos + n])
+        self.local_tail = pos + n
+        return obj
+
+    def ProcessData(self, count):
+        for _i in range(count):
+            obj = self.RemoveFromBuffer()
+            self.process_data(obj)
+        self.tail.value = self.local_tail
+        self.wait_event.set()
+        self.done.emit(count)
+
+# Fetch more records bar
+
+class FetchMoreRecordsBar():
+
+    def __init__(self, model, parent):
+        self.model = model
+
+        self.label = QLabel("Number of records (x " + "{:,}".format(glb_chunk_sz) + ") to fetch:")
+        self.label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
+
+        self.fetch_count = QSpinBox()
+        self.fetch_count.setRange(1, 1000000)
+        self.fetch_count.setValue(10)
+        self.fetch_count.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
+
+        self.fetch = QPushButton("Go!")
+        self.fetch.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
+        self.fetch.released.connect(self.FetchMoreRecords)
+
+        self.progress = QProgressBar()
+        self.progress.setRange(0, 100)
+        self.progress.hide()
+
+        self.done_label = QLabel("All records fetched")
+        self.done_label.hide()
+
+        self.spacer = QLabel("")
+
+        self.close_button = QToolButton()
+        self.close_button.setIcon(parent.style().standardIcon(QStyle.SP_DockWidgetCloseButton))
+        self.close_button.released.connect(self.Deactivate)
+
+        self.hbox = QHBoxLayout()
+        self.hbox.setContentsMargins(0, 0, 0, 0)
+
+        self.hbox.addWidget(self.label)
+        self.hbox.addWidget(self.fetch_count)
+        self.hbox.addWidget(self.fetch)
+        self.hbox.addWidget(self.spacer)
+        self.hbox.addWidget(self.progress)
+        self.hbox.addWidget(self.done_label)
+        self.hbox.addWidget(self.close_button)
+
+        self.bar = QWidget()
+        self.bar.setLayout(self.hbox)
+        self.bar.show()
+
+        self.in_progress = False
+        self.start = 0
+        self.model.progress.connect(self.Progress)
+
+        self.done = False
+
+        if not model.HasMoreRecords():
+            self.Done()
+
+    def Widget(self):
+        return self.bar
+
+    def Activate(self):
+        self.bar.show()
+        self.fetch.setFocus()
+
+    def Deactivate(self):
+        self.bar.hide()
+
+    def Enable(self, enable):
+        self.fetch.setEnabled(enable)
+        self.fetch_count.setEnabled(enable)
+
+    def Busy(self):
+        self.Enable(False)
+        self.fetch.hide()
+        self.spacer.hide()
+        self.progress.show()
+
+    def Idle(self):
+        self.in_progress = False
+        self.Enable(True)
+        self.progress.hide()
+        self.fetch.show()
+        self.spacer.show()
+
+    def Target(self):
+        return self.fetch_count.value() * glb_chunk_sz
+
+    def Done(self):
+        self.done = True
+        self.Idle()
+        self.label.hide()
+        self.fetch_count.hide()
+        self.fetch.hide()
+        self.spacer.hide()
+        self.done_label.show()
+
+    def Progress(self, count):
+        if self.in_progress:
+            if count:
+                percent = ((count - self.start) * 100) / self.Target()
+                if percent >= 100:
+                    self.Idle()
+                else:
+                    self.progress.setValue(percent)
+        if not count:
+            # Count value of zero means no more records
+            self.Done()
+
+    def FetchMoreRecords(self):
+        if self.done:
+            return
+        self.progress.setValue(0)
+        self.Busy()
+        self.in_progress = True
+        self.start = self.model.FetchMoreRecords(self.Target())
+
+# Brance data model level two item
+
+class BranchLevelTwoItem():
+
+    def __init__(self, row, col, text, parent_item):
+        self.row = row
+        self.parent_item = parent_item
+        self.data = [""] * (col + 1)
+        self.data[col] = text
+        self.level = 2
+
+    def getParentItem(self):
+        return self.parent_item
+
+    def getRow(self):
+        return self.row
+
+    def childCount(self):
+        return 0
+
+    def hasChildren(self):
+        return False
+
+    def getData(self, column):
+        return self.data[column]
+
+# Brance data model level one item
+
+class BranchLevelOneItem():
+
+    def __init__(self, glb, row, data, parent_item):
+        self.glb = glb
+        self.row = row
+        self.parent_item = parent_item
+        self.child_count = 0
+        self.child_items = []
+        self.data = data[1:]
+        self.dbid = data[0]
+        self.level = 1
+        self.query_done = False
+        self.br_col = len(self.data) - 1
+
+    def getChildItem(self, row):
+        return self.child_items[row]
+
+    def getParentItem(self):
+        return self.parent_item
+
+    def getRow(self):
+        return self.row
+
+    def Select(self):
+        self.query_done = True
+
+        if not self.glb.have_disassembler:
+            return
+
+        query = QSqlQuery(self.glb.db)
+
+        QueryExec(query, "SELECT cpu, to_dso_id, to_symbol_id, to_sym_offset, short_name, long_name, build_id, sym_start, to_ip"
+                  " FROM samples"
+                  " INNER JOIN dsos ON samples.to_dso_id = dsos.id"
+                  " INNER JOIN symbols ON samples.to_symbol_id = symbols.id"
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

tools/perf/scripts/python/exported-sql-viewer.py deleted +0 -5030

5,030 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog

Part of the diff is not shown here. View the raw message.

author reviewer
#198 lore raw

PATCH v2 45/49 perf python: Move and clean up parallel-perf.py

In reply to Ian Rogers

Move parallel-perf.py from tools/perf/scripts/python/ to tools/perf/python/ as it is a standalone Python utility that invokes 'perf script' in parallel across time slices and CPUs. Update tools/perf/tests/shell/script.sh accordingly.

Also fix bugs and clean up the script to pass mypy and pylint without suppression comments:
- Fix Work.command() to return the shlex.quote()-escaped command string (previously sh_cmd was computed with shlex.quote() and discarded in favor of unquoted self.cmd).
- Close self.popen.stdout in the parent process after spawning the consumer subprocess in Work.start() when --pipe-to is used, ensuring the producer receives SIGPIPE if the consumer exits early and avoiding leaking the pipe file descriptor.
- Convert method and function names to snake_case, specify utf-8 file encodings, and add type annotations and docstrings.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/parallel-perf.py         | 1250 ++++++++++++++++++++
 tools/perf/scripts/python/parallel-perf.py |  989 ----------------
 tools/perf/tests/shell/script.sh           |    4 +-
 3 files changed, 1252 insertions(+), 991 deletions(-)
 create mode 100755 tools/perf/python/parallel-perf.py
 delete mode 100755 tools/perf/scripts/python/parallel-perf.py
tools/perf/python/parallel-perf.py new file +1250 -0
new file mode 100755
index 000000000000..c545933e1cdd
@@ -0,0 +1,1250 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""Parallel perf script."""
+#
+# run a perf script command multiple times in parallel, using perf script
+# options --cpu and --time so that each job processes a different chunk
+# of the data.
+#
+# Copyright (c) 2024, Intel Corporation.
+
+import subprocess
+import argparse
+import pathlib
+import shlex
+import time
+from typing import Any
+import copy
+import sys
+import os
+import re
+
+glb_prog_name = "parallel-perf.py"
+glb_min_interval = 10.0
+glb_min_samples = 64
+
+
+class Verbosity():
+
+    def __init__(self, quiet: bool = False, verbose: bool = False,
+                 debug: bool = False) -> None:
+        """__init__."""
+
+        self.normal = True
+        self.verbose = verbose
+        self.debug = debug
+        self.self_test = True
+        if self.debug:
+            self.verbose = True
+        if self.verbose:
+            quiet = False
+        if quiet:
+            self.normal = False
+
+# Manage work (start/wait/kill), as represented by a subprocess.Popen command
+
+
+class Work():
+
+    def __init__(self, cmd: list[str], pipe_to: str,
+                 output_dir: str = ".") -> None:
+        """__init__."""
+
+        self.popen: Any = None
+        self.consumer: Any = None
+        self.cmd = cmd
+        self.pipe_to = pipe_to
+        self.output_dir = output_dir
+        self.cmdout_name = f"{output_dir}/cmd.txt"
+        self.stdout_name = f"{output_dir}/out.txt"
+        self.stderr_name = f"{output_dir}/err.txt"
+
+    def command(self):
+        """command."""
+
+        return " ".join(shlex.quote(x) for x in self.cmd)
+
+    def stdout(self):
+        """stdout."""
+
+        return open(self.stdout_name, "w", encoding="utf-8")
+
+    def stderr(self):
+        """stderr."""
+
+        return open(self.stderr_name, "w", encoding="utf-8")
+
+    def create_output_dir(self):
+        """create_output_dir."""
+
+        pathlib.Path(self.output_dir).mkdir(parents=True, exist_ok=True)
+
+    def start(self):
+        """start."""
+
+        if self.popen:
+            return
+        self.create_output_dir()
+        with open(self.cmdout_name, "w", encoding="utf-8") as f:
+            f.write(self.command())
+            f.write("\n")
+        stdout = self.stdout()
+        stderr = self.stderr()
+        if self.pipe_to:
+            self.popen = subprocess.Popen(
+                self.cmd, stdout=subprocess.PIPE, stderr=stderr)
+            args = shlex.split(self.pipe_to)
+            self.consumer = subprocess.Popen(
+                args, stdin=self.popen.stdout, stdout=stdout, stderr=stderr)
+            # The consumer now owns the read end of the pipe. Close this
+            # process's copy so that the producer receives SIGPIPE if the
+            # consumer exits early, and so that the descriptor isn't leaked
+            # for the lifetime of this Work.
+            self.popen.stdout.close()
+            self.popen.stdout = None
+        else:
+            self.popen = subprocess.Popen(
+                self.cmd, stdout=stdout, stderr=stderr)
+
+    def remove_empty_err_file(self):
+        """remove_empty_err_file."""
+
+        if os.path.exists(self.stderr_name):
+            if os.path.getsize(self.stderr_name) == 0:
+                os.unlink(self.stderr_name)
+
+    def errors(self):
+        """errors."""
+
+        if os.path.exists(self.stderr_name):
+            if os.path.getsize(self.stderr_name) != 0:
+                return [f"Non-empty error file {self.stderr_name}"]
+        return []
+
+    def tidy_up(self):
+        """tidy_up."""
+
+        self.remove_empty_err_file()
+
+    def raw_poll_wait(self, p, wait):
+        """raw_poll_wait."""
+
+        if wait:
+            return p.wait()
+        return p.poll()
+
+    def poll(self, wait=False):
+        """poll."""
+
+        if not self.popen:
+            return None
+        result = self.raw_poll_wait(self.popen, wait)
+        if self.consumer:
+            res = result
+            result = self.raw_poll_wait(self.consumer, wait)
+            if result is not None and res is None:
+                self.popen.kill()
+                result = None
+            elif result == 0 and res is not None and res != 0:
+                result = res
+        if result is not None:
+            self.tidy_up()
+        return result
+
+    def wait(self):
+        """wait."""
+
+        return self.poll(wait=True)
+
+    def kill(self):
+        """kill."""
+
+        if not self.popen:
+            return
+        self.popen.kill()
+        if self.consumer:
+            self.consumer.kill()
+
+
+def kill_work(worklist, _verbosity):
+    """kill_work."""
+
+    for w in worklist:
+        w.kill()
+    for w in worklist:
+        w.wait()
+
+
+def number_of_cp_us():
+    """number_of_cp_us."""
+
+    return os.sysconf("SC_NPROCESSORS_ONLN")
+
+
+def nano_secs_to_secs_str(x):
+    """nano_secs_to_secs_str."""
+
+    if x is None:
+        return ""
+    x = str(x)
+    if len(x) < 10:
+        x = "0" * (10 - len(x)) + x
+    return x[:len(x) - 9] + "." + x[-9:]
+
+
+def insert_option_after(cmd, option, after):
+    """insert_option_after."""
+
+    try:
+        pos = cmd.index(after)
+        cmd.insert(pos + 1, option)
+    except (OSError, ValueError, RuntimeError):
+        cmd.append(option)
+
+
+def create_work_list(cmd, pipe_to, output_dir, cpus, time_ranges_by_cpu):
+    """create_work_list."""
+
+    max_len = len(str(cpus[-1]))
+    cpu_dir_fmt = f"cpu-%.{max_len}u"
+    worklist = []
+    pos = 0
+    for cpu in cpus:
+        if cpu >= 0:
+            cpu_dir = os.path.join(output_dir, cpu_dir_fmt % cpu)
+            cpu_option = f"--cpu={cpu}"
+        else:
+            cpu_dir = output_dir
+            cpu_option = None
+
+        tr_dir_fmt = "time-range"
+
+        if len(time_ranges_by_cpu) > 1:
+            time_ranges = time_ranges_by_cpu[pos]
+            tr_dir_fmt += f"-{pos}"
+            pos += 1
+        else:
+            time_ranges = time_ranges_by_cpu[0]
+
+        max_len = len(str(len(time_ranges)))
+        tr_dir_fmt += f"-%.{max_len}u"
+
+        i = 0
+        for r in time_ranges:
+            if r == [None, None]:
+                time_option = None
+                work_output_dir = cpu_dir
+            else:
+                time_option = "--time=" + \
+                    nano_secs_to_secs_str(r[0]) + "," + \
+                    nano_secs_to_secs_str(r[1])
+                work_output_dir = os.path.join(cpu_dir, tr_dir_fmt % i)
+                i += 1
+            work_cmd = list(cmd)
+            if time_option is not None:
+                insert_option_after(work_cmd, time_option, "script")
+            if cpu_option is not None:
+                insert_option_after(work_cmd, cpu_option, "script")
+            w = Work(work_cmd, pipe_to, work_output_dir)
+            worklist.append(w)
+    return worklist
+
+
+def do_run_work(worklist: list[Work], nr_jobs: int,
+                _verbosity: Verbosity) -> bool:
+    """do_run_work."""
+
+    nr_to_do = len(worklist)
+    not_started = list(worklist)
+    running: list[Work] = []
+    done: list[Work] = []
+    chg = False
+    while True:
+        nr_done = len(done)
+        if chg and _verbosity.normal:
+            nr_run = len(running)
+            print(
+                f"\rThere are {nr_to_do} jobs: {nr_done} completed, {nr_run} running",
+                flush=True, end=" ")
+            if _verbosity.verbose:
+                print()
+            chg = False
+        if nr_done == nr_to_do:
+            break
+        while len(running) < nr_jobs and len(not_started):
+            w = not_started.pop(0)
+            running.append(w)
+            if _verbosity.verbose:
+                print("Starting:", w.command())
+            w.start()
+            chg = True
+        if len(running):
+            time.sleep(0.1)
+        finished: list[Work] = []
+        not_finished: list[Work] = []
+        while len(running):
+            w = running.pop(0)
+            r = w.poll()
+            if r is None:
+                not_finished.append(w)
+                continue
+            if r == 0:
+                if _verbosity.verbose:
+                    print("Finished:", w.command())
+                finished.append(w)
+                chg = True
+                continue
+            if _verbosity.normal and not _verbosity.verbose:
+                print()
+            print("Job failed!\n    return code:", r,
+                  "\n    command:    ", w.command())
+            if w.pipe_to:
+                print("    piped to:   ", w.pipe_to)
+            print("Killing outstanding jobs")
+            kill_work(not_finished, _verbosity)
+            kill_work(running, _verbosity)
+            return False
+        running = not_finished
+        done += finished
+    errorlist: list[str] = []
+    for w in worklist:
+        errorlist += w.errors()
+    if len(errorlist):
+        print("errors:")
+        for e in errorlist:
+            print(e)
+    elif _verbosity.normal:
+        print("\r", " "*50, "\rAll jobs finished successfully", flush=True)
+    return True
+
+
+def run_work(worklist: list[Work], nr_jobs: int = number_of_cp_us(),
+             _verbosity: Verbosity = Verbosity()) -> bool:
+    """run_work."""
+    try:
+        return do_run_work(worklist, nr_jobs, _verbosity)
+    except (OSError, ValueError, RuntimeError, KeyboardInterrupt):
+        for w in worklist:
+            w.kill()
+        raise
+
+
+def read_header(perf, file_name):
+    """read_header."""
+
+    cmd = [perf, "script", "--header-only", "--input", file_name]
+    with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
+        out = proc.stdout.read() if proc.stdout else b""
+    return out.decode("utf-8")
+
+
+def parse_header(hdr):
+    """parse_header."""
+
+    result = {}
+    lines = hdr.split("\n")
+    for line in lines:
+        if ":" in line and line[0] == "#":
+            pos = line.index(":")
+            name = line[1:pos-1].strip()
+            value = line[pos+1:].strip()
+            if name in result:
+                orig_name = name
+                nr = 2
+                while True:
+                    name = f"{orig_name} {nr}"
+                    if name not in result:
+                        break
+                    nr += 1
+            result[name] = value
+    return result
+
+
+def header_field(hdr_dict, hdr_fld):
+    """header_field."""
+
+    if hdr_fld not in hdr_dict:
+        raise RuntimeError(f"'{hdr_fld}' missing from header information")
+    return hdr_dict[hdr_fld]
+
+# Represent the position of an option within a command string
+# and provide the option value and/or remove the option
+
+
+class OptPos():
+
+    def init(self, opt_element=-1, value_element=-1, opt_pos=-1, value_pos=-1, error=None):
+        """init."""
+
+        self.opt_element = opt_element		# list element that contains option
+        self.value_element = value_element  # list element that contains option value
+        self.opt_pos = opt_pos			# string position of option
+        self.value_pos = value_pos		# string position of value
+        self.error = error			# error message string
+
+    def __init__(self, args, short_name, long_name, default=None):
+        """__init__."""
+
+        self.args = list(args)
+        self.default = default
+        n = 2 + len(long_name)
+        m = len(short_name)
+        pos = -1
+        for opt in args:
+            pos += 1
+            if m and opt[:2] == f"-{short_name}":
+                if len(opt) == 2:
+                    if pos + 1 < len(args):
+                        self.init(pos, pos + 1, 0, 0)
+                    else:
+                        self.init(error=f"-{short_name} option missing value")
+                else:
+                    self.init(pos, pos, 0, 2)
+                return
+            if opt[:n] == f"--{long_name}":
+                if len(opt) == n:
+                    if pos + 1 < len(args):
+                        self.init(pos, pos + 1, 0, 0)
+                    else:
+                        self.init(error=f"--{long_name} option missing value")
+                elif opt[n] == "=":
+                    self.init(pos, pos, 0, n + 1)
+                else:
+                    self.init(error=f"--{long_name} option expected '='")
+                return
+            if m and opt[:1] == "-" and opt[:2] != "--" and short_name in opt:
+                ipos = opt.index(short_name)
+                if "-" in opt[1:]:
+                    hpos = opt[1:].index("-")
+                    if hpos < ipos:
+                        continue
+                if ipos + 1 == len(opt):
+                    if pos + 1 < len(args):
+                        self.init(pos, pos + 1, ipos, 0)
+                    else:
+                        self.init(error=f"-{short_name} option missing value")
+                else:
+                    self.init(pos, pos, ipos, ipos + 1)
+                return
+        self.init()
+
+    def value(self):
+        """value."""
+
+        if self.opt_element >= 0:
+            if self.opt_element != self.value_element:
+                return self.args[self.value_element]
+            else:
+                return self.args[self.value_element][self.value_pos:]
+        return self.default
+
+    def remove(self, args):
+        """remove."""
+
+        if self.opt_element == -1:
+            return
+        if self.opt_element != self.value_element:
+            del args[self.value_element]
+        if self.opt_pos:
+            args[self.opt_element] = args[self.opt_element][:self.opt_pos]
+        else:
+            del args[self.opt_element]
+
+
+def determine_input_file_name(cmd):
+    """determine_input_file_name."""
+
+    p = OptPos(cmd, "i", "input", "perf.data")
+    if p.error:
+        raise RuntimeError(f"perf command {p.error}")
+    file_name = p.value()
+    if not os.path.exists(file_name):
+        raise RuntimeError(f"perf command input file '{file_name}' not found")
+    return file_name
+
+
+def read_option(args, short_name, long_name, err_prefix, remove=False):
+    """read_option."""
+
+    p = OptPos(args, short_name, long_name)
+    if p.error:
+        raise RuntimeError(f"{err_prefix}{p.error}")
+    value = p.value()
+    if remove:
+        p.remove(args)
+    return value
+
+
+def extract_option(args, short_name, long_name, err_prefix):
+    """extract_option."""
+
+    return read_option(args, short_name, long_name, err_prefix, True)
+
+
+def read_perf_option(args, short_name, long_name):
+    """read_perf_option."""
+
+    return read_option(args, short_name, long_name, "perf command ")
+
+
+def extract_perf_option(args, short_name, long_name):
+    """extract_perf_option."""
+
+    return extract_option(args, short_name, long_name, "perf command ")
+
+
+def perf_double_quick_commands(cmd, file_name):
+    """perf_double_quick_commands."""
+
+    cpu_str = read_perf_option(cmd, "C", "cpu")
+    time_str = read_perf_option(cmd, "", "time")
+    # Use double-quick sampling to determine trace data density
+    times_cmd = ["perf", "script", "--ns",
+                 "--input", file_name, "--itrace=qqi"]
+    if cpu_str is not None and cpu_str != "":
+        times_cmd.append(f"--cpu={cpu_str}")
+    if time_str is not None and time_str != "":
+        times_cmd.append(f"--time={time_str}")
+    cnts_cmd = list(times_cmd)
+    cnts_cmd.append("-Fcpu")
+    times_cmd.append("-Fcpu,time")
+    return cnts_cmd, times_cmd
+
+
+class CPUTimeRange():
+    def __init__(self, cpu):
+        """__init__."""
+
+        self.cpu = cpu
+        self.sample_cnt = 0
+        self.time_ranges = None
+        self.interval = 0
+        self.interval_remaining = 0
+        self.remaining = 0
+        self.tr_pos = 0
+
+
+def calc_time_ranges_by_cpu(line, cpu, cpu_time_ranges, max_time):
+    """calc_time_ranges_by_cpu."""
+
+    cpu_time_range = cpu_time_ranges[cpu]
+    cpu_time_range.remaining -= 1
+    cpu_time_range.interval_remaining -= 1
+    if cpu_time_range.remaining == 0:
+        cpu_time_range.time_ranges[cpu_time_range.tr_pos][1] = max_time
+        return
+    if cpu_time_range.interval_remaining == 0:
+        ts = time_val(line[1][:-1], 0)
+        time_ranges = cpu_time_range.time_ranges
+        time_ranges[cpu_time_range.tr_pos][1] = ts - 1
+        time_ranges.append([ts, max_time])
+        cpu_time_range.tr_pos += 1
+        cpu_time_range.interval_remaining = cpu_time_range.interval
+
+
+def count_samples_by_cpu(_line: list[str], cpu: int,
+                         cpu_time_ranges: list[Any]) -> None:
+    """count_samples_by_cpu."""
+
+    try:
+        cpu_time_ranges[cpu].sample_cnt += 1
+    except (OSError, ValueError, RuntimeError, IndexError):
+        print("exception")
+        print("cpu", cpu)
+        print("len(cpu_time_ranges)", len(cpu_time_ranges))
+        raise
+
+
+def process_command_output_lines(cmd, per_cpu, fn, *x):
+    """process_command_output_lines."""
+
+    # Assume CPU number is at beginning of line and enclosed by []
+    pat = re.compile(r"\s*\[[0-9]+\]")
+    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+    while True:
+        line = p.stdout.readline()
+        if line:
+            line = line.decode("utf-8")
+            if pat.match(line):
+                line = line.split()
+                if per_cpu:
+                    # Assumes CPU number is enclosed by []
+                    cpu = int(line[0][1:-1])
+                else:
+                    cpu = 0
+                fn(line, cpu, *x)
+        else:
+            break
+    p.wait()
+
+
+def intersect_time_ranges(new_time_ranges, time_ranges):
+    """intersect_time_ranges."""
+
+    pos = 0
+    new_pos = 0
+    # Can assume len(time_ranges) != 0 and len(new_time_ranges) != 0
+    # Note also, there *must* be at least one intersection.
+    while pos < len(time_ranges) and new_pos < len(new_time_ranges):
+        # new end < old start => no intersection, remove new
+        if new_time_ranges[new_pos][1] < time_ranges[pos][0]:
+            del new_time_ranges[new_pos]
+            continue
+        # new start > old end => no intersection, check next
+        if new_time_ranges[new_pos][0] > time_ranges[pos][1]:
+            pos += 1
+            if pos < len(time_ranges):
+                continue
+            # no next, so remove remaining
+            while new_pos < len(new_time_ranges):
+                del new_time_ranges[new_pos]
+            return
+        # Found an intersection
+        # new start < old start => adjust new start = old start
+        if new_time_ranges[new_pos][0] < time_ranges[pos][0]:
+            new_time_ranges[new_pos][0] = time_ranges[pos][0]
+        # new end > old end => keep the overlap, insert the remainder
+        if new_time_ranges[new_pos][1] > time_ranges[pos][1]:
+            r = [time_ranges[pos][1] + 1, new_time_ranges[new_pos][1]]
+            new_time_ranges[new_pos][1] = time_ranges[pos][1]
+            new_pos += 1
+            new_time_ranges.insert(new_pos, r)
+            continue
+        # new [start, end] is within old [start, end]
+        new_pos += 1
+
+
+def split_time_ranges_by_trace_data_density(
+    time_ranges, cpus, nr, cmd, file_name, per_cpu, min_size, min_interval, _verbosity
+):
+    """split_time_ranges_by_trace_data_density."""
+
+    if _verbosity.normal:
+        print("\rAnalyzing...", flush=True, end=" ")
+        if _verbosity.verbose:
+            print()
+    cnts_cmd, times_cmd = perf_double_quick_commands(cmd, file_name)
+
+    nr_cpus = cpus[-1] + 1 if per_cpu else 1
+    if per_cpu:
+        nr_cpus = cpus[-1] + 1
+        cpu_time_ranges = [CPUTimeRange(cpu) for cpu in range(nr_cpus)]
+    else:
+        nr_cpus = 1
+        cpu_time_ranges = [CPUTimeRange(-1)]
+
+    if _verbosity.debug:
+        print("nr_cpus", nr_cpus)
+        print("cnts_cmd", cnts_cmd)
+        print("times_cmd", times_cmd)
+
+    # Count the number of "double quick" samples per CPU
+    process_command_output_lines(
+        cnts_cmd, per_cpu, count_samples_by_cpu, cpu_time_ranges)
+
+    tot = 0
+    mx = 0
+    for cpu_time_range in cpu_time_ranges:
+        cnt = cpu_time_range.sample_cnt
+        tot += cnt
+        if cnt > mx:
+            mx = cnt
+        if _verbosity.debug:
+            print("cpu:", cpu_time_range.cpu, "sample_cnt", cnt)
+
+    if min_size < 1:
+        min_size = 1
+
+    if mx < min_size:
+        # Too little data to be worth splitting
+        if _verbosity.debug:
+            print("Too little data to split by time")
+        if nr == 0:
+            nr = 1
+        return [split_time_ranges_into_n(time_ranges, nr, min_interval)]
+
+    if nr:
+        divisor = nr
+        min_size = 1
+    else:
+        divisor = number_of_cp_us()
+
+    interval = int(round(tot / divisor, 0))
+    if interval < min_size:
+        interval = min_size
+
+    if _verbosity.debug:
+        print("divisor", divisor)
+        print("min_size", min_size)
+        print("interval", interval)
+
+    min_time = time_ranges[0][0]
+    max_time = time_ranges[-1][1]
+
+    for cpu_time_range in cpu_time_ranges:
+        cnt = cpu_time_range.sample_cnt
+        if cnt == 0:
+            cpu_time_range.time_ranges = copy.deepcopy(time_ranges)
+            continue
+        # Adjust target interval for CPU to give approximately equal interval sizes
+        # Determine number of intervals, rounding to nearest integer
+        n = int(round(cnt / interval, 0))
+        if n < 1:
+            n = 1
+        # Determine interval size, rounding up
+        d, m = divmod(cnt, n)
+        if m:
+            d += 1
+        cpu_time_range.interval = d
+        cpu_time_range.interval_remaining = d
+        cpu_time_range.remaining = cnt
+        # init. time ranges for each CPU with the start time
+        cpu_time_range.time_ranges = [[min_time, max_time]]
+
+    # Set time ranges so that the same number of "double quick" samples
+    # will fall into each time range.
+    process_command_output_lines(
+        times_cmd, per_cpu, calc_time_ranges_by_cpu, cpu_time_ranges, max_time)
+
+    for cpu_time_range in cpu_time_ranges:
+        if cpu_time_range.sample_cnt:
+            intersect_time_ranges(cpu_time_range.time_ranges, time_ranges)
+
+    return [cpu_time_ranges[cpu].time_ranges for cpu in cpus]
+
+
+def split_single_time_range_into_n(time_range, n):
+    """split_single_time_range_into_n."""
+
+    if n <= 1:
+        return [time_range]
+    start = time_range[0]
+    end = time_range[1]
+    duration = int((end - start + 1) / n)
+    if duration < 1:
+        return [time_range]
+    time_ranges = []
+    for _i in range(n):
+        time_ranges.append([start, start + duration - 1])
+        start += duration
+    time_ranges[-1][1] = end
+    return time_ranges
+
+
+def time_range_duration(r):
+    """time_range_duration."""
+
+    return r[1] - r[0] + 1
+
+
+def total_duration(time_ranges):
+    """total_duration."""
+
+    duration = 0
+    for r in time_ranges:
+        duration += time_range_duration(r)
+    return duration
+
+
+def split_time_ranges_by_interval(time_ranges, interval):
+    """split_time_ranges_by_interval."""
+
+    new_ranges = []
+    for r in time_ranges:
+        duration = time_range_duration(r)
+        n = duration / interval
+        n = int(round(n, 0))
+        new_ranges += split_single_time_range_into_n(r, n)
+    return new_ranges
+
+
+def split_time_ranges_into_n(time_ranges, n, min_interval):
+    """split_time_ranges_into_n."""
+
+    if n <= len(time_ranges):
+        return time_ranges
+    duration = total_duration(time_ranges)
+    interval = duration / n
+    if interval < min_interval:
+        interval = min_interval
+    return split_time_ranges_by_interval(time_ranges, interval)
+
+
+def recombine_time_ranges(tr):
+    """recombine_time_ranges."""
+
+    new_tr = copy.deepcopy(tr)
+    i = 1
+    while i < len(new_tr):
+        # if prev end + 1 == cur start, combine them
+        if new_tr[i - 1][1] + 1 == new_tr[i][0]:
+            new_tr[i][0] = new_tr[i - 1][0]
+            del new_tr[i - 1]
+        else:
+            i += 1
+    return new_tr
+
+
+def open_time_range_ends(time_ranges, min_time, max_time):
+    """open_time_range_ends."""
+
+    if time_ranges[0][0] <= min_time:
+        time_ranges[0][0] = None
+    if time_ranges[-1][1] >= max_time:
+        time_ranges[-1][1] = None
+
+
+def bad_time_str(time_str):
+    """bad_time_str."""
+
+    raise RuntimeError(
+        f"perf command bad time option: '{time_str}'\n"
+        "Check also 'time of first sample' and 'time of last sample' "
+        "in perf script --header-only"
+    )
+
+
+def validate_time_ranges(time_ranges, time_str):
+    """validate_time_ranges."""
+
+    n = len(time_ranges)
+    for i in range(n):
+        start = time_ranges[i][0]
+        end = time_ranges[i][1]
+        if i != 0 and start <= time_ranges[i - 1][1]:
+            bad_time_str(time_str)
+        if start > end:
+            bad_time_str(time_str)
+
+
+def time_val(s, dflt):
+    """time_val."""
+
+    s = s.strip()
+    if s == "":
+        return dflt
+    a = s.split(".")
+    if len(a) > 2:
+        raise RuntimeError(f"Bad time value'{s}'")
+    x = int(a[0])
+    if x < 0:
+        raise RuntimeError("Negative time not allowed")
+    x *= 1000000000
+    if len(a) > 1:
+        x += int((a[1] + "000000000")[:9])
+    return x
+
+
+def bad_cpu_str(cpu_str):
+    """bad_cpu_str."""
+
+    raise RuntimeError(
+        f"perf command bad cpu option: '{cpu_str}'\n"
+        "Check also 'nrcpus avail' in perf script --header-only"
+    )
+
+
+def parse_time_str(time_str, min_time, max_time):
+    """parse_time_str."""
+
+    if time_str is None or time_str == "":
+        return [[min_time, max_time]]
+    time_ranges = []
+    for r in time_str.split():
+        a = r.split(",")
+        if len(a) != 2:
+            bad_time_str(time_str)
+        try:
+            start = time_val(a[0], min_time)
+            end = time_val(a[1], max_time)
+        except (OSError, ValueError, RuntimeError):
+            bad_time_str(time_str)
+        time_ranges.append([start, end])
+    validate_time_ranges(time_ranges, time_str)
+    return time_ranges
+
+
+def parse_cpu_str(cpu_str, nr_cpus):
+    """parse_cpu_str."""
+
+    if cpu_str is None or cpu_str == "":
+        return [-1]
+    cpus = []
+    for r in cpu_str.split(","):
+        a = r.split("-")
+        if len(a) < 1 or len(a) > 2:
+            bad_cpu_str(cpu_str)
+        try:
+            start = int(a[0].strip())
+            if len(a) > 1:
+                end = int(a[1].strip())
+            else:
+                end = start
+        except (OSError, ValueError, RuntimeError):
+            bad_cpu_str(cpu_str)
+        if start < 0 or end < 0 or end < start or end >= nr_cpus:
+            bad_cpu_str(cpu_str)
+        cpus.extend(range(start, end + 1))
+    cpus = list(set(cpus))  # remove duplicates
+    cpus.sort()
+    return cpus
+
+
+class ParallelPerf():
+
+    def __init__(self, a):
+        """init."""
+        self.nr = 0
+        self.jobs = 0
+        self.file_name = None
+        self.hdr = None
+        self.hdr_dict = None
+        self.cmd_line = None
+        self.min_time = None
+        self.max_time = None
+        self.time_str = None
+        self.time_ranges = None
+        self.cpu_str = None
+        self.cpus = None
+        self.split_time_ranges_for_each_cpu = None
+        self.worklist = None
+        self.per_cpu = None
+        self.interval = None
+        self.min_size = None
+        self.min_interval = None
+        self.pipe_to = None
+        self.output_dir = None
+        self.cmd = getattr(a, 'cmd', None)
+        self.no_per_cpu = getattr(a, 'no_per_cpu', False)
+        self.dry_run = getattr(a, 'dry_run', False)
+        self._verbosity = getattr(a, "_verbosity", Verbosity())
+
+        for arg_name in vars(a):
+            setattr(self, arg_name, getattr(a, arg_name))
+        self.orig_nr = self.nr
+        self.orig_cmd = list(self.cmd)
+        self.perf = self.cmd[0]
+        if os.path.exists(self.output_dir):
+            raise RuntimeError(f"Output '{self.output_dir}' already exists")
+        if self.jobs < 0 or self.nr < 0 or self.interval < 0:
+            raise RuntimeError(
+                "Bad options (negative values): try -h option for help")
+        if self.nr != 0 and self.interval != 0:
+            raise RuntimeError(
+                "Cannot specify number of time subdivisions and time interval")
+        if self.jobs == 0:
+            self.jobs = number_of_cp_us()
+        if self.nr == 0 and self.interval == 0:
+            if self.per_cpu:
+                self.nr = 1
+            else:
+                self.nr = self.jobs
+
+    def init(self):
+        """init."""
+
+        if self._verbosity.debug:
+            print("cmd", self.cmd)
+        self.file_name = determine_input_file_name(self.cmd)
+        self.hdr = read_header(self.perf, self.file_name)
+        self.hdr_dict = parse_header(self.hdr)
+        self.cmd_line = header_field(self.hdr_dict, "cmdline")
+
+    def extract_time_info(self):
+        """extract_time_info."""
+
+        self.min_time = time_val(header_field(
+            self.hdr_dict, "time of first sample"), 0)
+        self.max_time = time_val(header_field(
+            self.hdr_dict, "time of last sample"), 0)
+        self.time_str = extract_perf_option(self.cmd, "", "time")
+        self.time_ranges = parse_time_str(
+            self.time_str, self.min_time, self.max_time)
+        if self._verbosity.debug:
+            print("time_ranges", self.time_ranges)
+
+    def extract_cpu_info(self):
+        """extract_cpu_info."""
+
+        if self.per_cpu:
+            nr_cpus = int(header_field(self.hdr_dict, "nrcpus avail"))
+            self.cpu_str = extract_perf_option(self.cmd, "C", "cpu")
+            if self.cpu_str is None or self.cpu_str == "":
+                self.cpus = [x for x in range(nr_cpus)]
+            else:
+                self.cpus = parse_cpu_str(self.cpu_str, nr_cpus)
+        else:
+            self.cpu_str = None
+            self.cpus = [-1]
+        if self._verbosity.debug:
+            print("cpus", self.cpus)
+
+    def is_intel_pt(self):
+        """is_intel_pt."""
+
+        return self.cmd_line.find("intel_pt") >= 0
+
+    def split_time_ranges(self):
+        """split_time_ranges."""
+
+        if self.is_intel_pt() and self.interval == 0:
+            self.split_time_ranges_for_each_cpu = (
+                split_time_ranges_by_trace_data_density(
+                    self.time_ranges, self.cpus, self.orig_nr,
+                    self.orig_cmd, self.file_name, self.per_cpu,
+                    self.min_size, self.min_interval, self._verbosity
+                )
+            )
+        elif self.nr:
+            self.split_time_ranges_for_each_cpu = [split_time_ranges_into_n(
+                self.time_ranges, self.nr, self.min_interval)]
+        else:
+            self.split_time_ranges_for_each_cpu = [
+                split_time_ranges_by_interval(self.time_ranges, self.interval)]
+
+    def check_time_ranges(self):
+        """check_time_ranges."""
+
+        for tr in self.split_time_ranges_for_each_cpu:
+            # Re-combined time ranges should be the same
+            new_tr = recombine_time_ranges(tr)
+            if new_tr != self.time_ranges:
+                if self._verbosity.debug:
+                    print("tr", tr)
+                    print("new_tr", new_tr)
+                raise RuntimeError("Self test failed!")
+
+    def open_time_range_ends(self):
+        """open_time_range_ends."""
+
+        for time_ranges in self.split_time_ranges_for_each_cpu:
+            open_time_range_ends(time_ranges, self.min_time, self.max_time)
+
+    def create_work_list(self):
+        """create_work_list."""
+
+        self.worklist = create_work_list(
+            self.cmd, self.pipe_to, self.output_dir, self.cpus, self.split_time_ranges_for_each_cpu)
+
+    def perf_data_recorded_per_cpu(self):
+        """perf_data_recorded_per_cpu."""
+
+        if "--per-thread" in self.cmd_line.split():
+            return False
+        return True
+
+    def default_to_per_cpu(self):
+        """default_to_per_cpu."""
+
+        # --no-per-cpu option takes precedence
+        if self.no_per_cpu:
+            return False
+        if not self.perf_data_recorded_per_cpu():
+            return False
+        # Default to per-cpu for Intel PT data that was recorded per-cpu,
+        # because decoding can be done for each CPU separately.
+        if self.is_intel_pt():
+            return True
+        return False
+
+    def config(self):
+        """config."""
+
+        self.init()
+        self.extract_time_info()
+        if not self.per_cpu:
+            self.per_cpu = self.default_to_per_cpu()
+        if self._verbosity.debug:
+            print("per_cpu", self.per_cpu)
+        self.extract_cpu_info()
+        self.split_time_ranges()
+        if self._verbosity.self_test:
+            self.check_time_ranges()
+        # Prefer open-ended time range to starting / ending with min_time / max_time resp.
+        self.open_time_range_ends()
+        self.create_work_list()
+
+    def run(self):
+        """run."""
+
+        if self.dry_run:
+            print(len(self.worklist), "jobs:")
+            for w in self.worklist:
+                print(w.command())
+            return True
+        result = run_work(self.worklist, self.jobs, _verbosity=self._verbosity)
+        if self._verbosity.verbose:
+            print(glb_prog_name, "done")
+        return result
+
+
+def run_parallel_perf(a):
+    """run_parallel_perf."""
+
+    pp = ParallelPerf(a)
+    pp.config()
+    return pp.run()
+
+
+def main(args):
+    """main."""
+
+    ap = argparse.ArgumentParser(
+            prog=glb_prog_name, formatter_class=argparse.RawDescriptionHelpFormatter,
+            description="""
+run a perf script command multiple times in parallel, using perf script options
+--cpu and --time so that each job processes a different chunk of the data.
+""",
+            epilog="""
+Follow the options by '--' and then the perf script command e.g.
+
+	$ perf record -a -- sleep 10
+	$ parallel-perf.py --nr=4 -- perf script --ns
+	All jobs finished successfully
+	$ tree parallel-perf-output/
+	parallel-perf-output/
+	├── time-range-0
+	│   ├── cmd.txt
+	│   └── out.txt
+	├── time-range-1
+	│   ├── cmd.txt
+	│   └── out.txt
+	├── time-range-2
+	│   ├── cmd.txt
+	│   └── out.txt
+	└── time-range-3
+	    ├── cmd.txt
+	    └── out.txt
+	$ find parallel-perf-output -name cmd.txt | sort | xargs grep -H .
+	parallel-perf-output/time-range-0/cmd.txt:perf script --time=,9466.504461499 --ns
+	parallel-perf-output/time-range-1/cmd.txt:perf script --time=9466.504461500,9469.005396999 --ns
+	parallel-perf-output/time-range-2/cmd.txt:perf script --time=9469.005397000,9471.506332499 --ns
+	parallel-perf-output/time-range-3/cmd.txt:perf script --time=9471.506332500, --ns
+
+Any perf script command can be used, including the use of perf script options
+--dlfilter and --script, so that the benefit of running parallel jobs
+naturally extends to them also.
+
+If option --pipe-to is used, standard output is first piped through that
+command. Beware, if the command fails (e.g. grep with no matches), it will be
+considered a fatal error.
+
+Final standard output is redirected to files named out.txt in separate
+subdirectories under the output directory. Similarly, standard error is
+written to files named err.txt. In addition, files named cmd.txt contain the
+corresponding perf script command. After processing, err.txt files are removed
+if they are empty.
+
+If any job exits with a non-zero exit code, then all jobs are killed and no
+more are started. A message is printed if any job results in a non-empty
+err.txt file.
+
+There is a separate output subdirectory for each time range. If the --per-cpu
+option is used, these are further grouped under cpu-n subdirectories, e.g.
+
+	$ parallel-perf.py --per-cpu --nr=2 -- perf script --ns --cpu=0,1
+	All jobs finished successfully
+	$ tree parallel-perf-output
+	parallel-perf-output/
+	├── cpu-0
+	│   ├── time-range-0
+	│   │   ├── cmd.txt
+	│   │   └── out.txt
+	│   └── time-range-1
+	│       ├── cmd.txt
+	│       └── out.txt
+	└── cpu-1
+	    ├── time-range-0
+	    │   ├── cmd.txt
+	    │   └── out.txt
+	    └── time-range-1
+	        ├── cmd.txt
+	        └── out.txt
+	$ find parallel-perf-output -name cmd.txt | sort | xargs grep -H .
+	parallel-perf-output/cpu-0/time-range-0/cmd.txt:perf script --cpu=0 --time=,9469.005396999 --ns
+	parallel-perf-output/cpu-0/time-range-1/cmd.txt:perf script --cpu=0 --time=9469.005397000, --ns
+	parallel-perf-output/cpu-1/time-range-0/cmd.txt:perf script --cpu=1 --time=,9469.005396999 --ns
+	parallel-perf-output/cpu-1/time-range-1/cmd.txt:perf script --cpu=1 --time=9469.005397000, --ns
+
+Subdivisions of time range, and cpus if the --per-cpu option is used, are
+expressed by the --time and --cpu perf script options respectively. If the
+supplied perf script command has a --time option, then that time range is
+subdivided, otherwise the time range given by 'time of first sample' to
+'time of last sample' is used (refer perf script --header-only). Similarly, the
+supplied perf script command may provide a --cpu option, and only those CPUs
+will be processed.
+
+To prevent time intervals becoming too small, the --min-interval option can
+be used.
+
+Note there is special handling for processing Intel PT traces. If an interval is
+not specified and the perf record command contained the intel_pt event, then the
+time range will be subdivided in order to produce subdivisions that contain
+approximately the same amount of trace data. That is accomplished by counting
+double-quick (--itrace=qqi) samples, and choosing time ranges that encompass
+approximately the same number of samples. In that case, time ranges may not be
+the same for each CPU processed. For Intel PT, --per-cpu is the default, but
+that can be overridden by --no-per-cpu. Note, for Intel PT, double-quick
+decoding produces 1 sample for each PSB synchronization packet, which in turn
+come after a certain number of bytes output, determined by psb_period (refer
+perf Intel PT documentation). The minimum number of double-quick samples that
+will define a time range can be set by the --min_size option, which defaults to
+64.
+""")
+    ap.add_argument("-o", "--output-dir", default="parallel-perf-output",
+                    help="output directory (default 'parallel-perf-output')")
+    ap.add_argument("-j", "--jobs", type=int, default=0,
+                    help="maximum number of jobs to run in parallel at one time "
+                         "(default is the number of CPUs)")
+    ap.add_argument("-n", "--nr", type=int, default=0,
+                    help="number of time subdivisions (default is the number of jobs)")
+    ap.add_argument("-i", "--interval", type=float, default=0,
+                    help="subdivide the time range using this time interval "
+                         "(in seconds e.g. 0.1 for a tenth of a second)")
+    ap.add_argument("-c", "--per-cpu", action="store_true",
+                    help="process data for each CPU in parallel")
+    ap.add_argument("-m", "--min-interval", type=float, default=glb_min_interval,
+                    help=f"minimum interval (default {glb_min_interval} seconds)")
+    ap.add_argument("-p", "--pipe-to",
+                    help="command to pipe output to (optional)")
+    ap.add_argument("-N", "--no-per-cpu", action="store_true",
+                    help="do not process data for each CPU in parallel")
+    ap.add_argument("-b", "--min_size", type=int, default=glb_min_samples,
+                    help="minimum data size (for Intel PT in PSBs)")
+    ap.add_argument("-D", "--dry-run", action="store_true",
+                    help="do not run any jobs, just show the perf script commands")
+    ap.add_argument("-q", "--quiet", action="store_true",
+                    help="do not print any messages except errors")
+    ap.add_argument("-v", "--verbose", action="store_true",
+                    help="print more messages")
+    ap.add_argument("-d", "--debug", action="store_true",
+                    help="print debugging messages")
+    cmd_line = list(args)
+    try:
+        split_pos = cmd_line.index("--")
+        cmd = cmd_line[split_pos + 1:]
+        args = cmd_line[:split_pos]
+    except (OSError, ValueError, RuntimeError):
+        cmd = None
+        args = cmd_line
+    a = ap.parse_args(args=args[1:])
+    a.cmd = cmd
+    setattr(a, "_verbosity", Verbosity(a.quiet, a.verbose, a.debug))
+    try:
+        if not a.cmd:
+            if a.cmd is None and len(args) <= 1:
+                ap.print_help()
+                return True
+            raise RuntimeError(
+                "command line must contain '--' before perf command")
+        return run_parallel_perf(a)
+    except (OSError, ValueError, RuntimeError, KeyboardInterrupt) as e:
+        print("Fatal error: ", str(e))
+        if a.debug:
+            raise
+        return False
+
+
+if __name__ == "__main__":
+    if not main(sys.argv):
+        sys.exit(1)
tools/perf/scripts/python/parallel-perf.py deleted +0 -989

989 lines not on this page · raw message

tools/perf/tests/shell/script.sh +2 -2
index 7007f1cdf761..254fc3ae94e7 100755
@@ -1,6 +1,6 @@
 #!/bin/bash
-# perf script tests
 # SPDX-License-Identifier: GPL-2.0
+# perf script tests
 
 set -e
 
@@ -76,7 +76,7 @@ test_parallel_perf()
 		err=2
 		return
 	fi
-	pp=$(dirname "$0")/../../scripts/python/parallel-perf.py
+	pp=$(dirname "$0")/../../python/parallel-perf.py
 	if [ ! -f "${pp}" ] ; then
 		echo "SKIP: parallel-perf.py script not found "
 		err=2
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#200 lore raw

PATCH v2 46/49 perf: Remove libpython support and legacy Python scripts

In reply to Ian Rogers

Remove embedded Python interpreter support (libpython) from perf, as all Python scripts have been migrated to standalone scripts using the perf Python extension module.

Changes include:
- Remove libpython detection and build flags from Makefile.config.
- Remove legacy Python script installation rules from Makefile.perf.
- Delete tools/perf/util/scripting-engines/trace-event-python.c and tools/perf/scripts/python/Perf-Trace-Util/Context.c.
- Remove Python scripting engine registration from trace-event-scripting.c.
- Remove libpython from the supported features list in builtin-check.c and Documentation/perf-check.txt.
- Delete the legacy Python scripts and bin wrappers in tools/perf/scripts/python/ and update shell tests.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Documentation/perf-check.txt       |    1 -
 tools/perf/Makefile.config                    |   26 +-
 tools/perf/Makefile.perf                      |    7 +-
 tools/perf/builtin-check.c                    |    2 +-
 tools/perf/scripts/Build                      |   27 -
 .../perf/scripts/python/Perf-Trace-Util/Build |    4 -
 .../scripts/python/Perf-Trace-Util/Context.c  |  225 --
 .../Perf-Trace-Util/lib/Perf/Trace/Core.py    |  116 -
 .../lib/Perf/Trace/EventClass.py              |   97 -
 .../lib/Perf/Trace/SchedGui.py                |  184 --
 .../Perf-Trace-Util/lib/Perf/Trace/Util.py    |   92 -
 .../scripts/python/arm-cs-trace-disasm.py     |  356 ---
 .../python/bin/compaction-times-record        |    2 -
 .../python/bin/compaction-times-report        |    4 -
 .../python/bin/event_analyzing_sample-record  |    8 -
 .../python/bin/event_analyzing_sample-report  |    3 -
 .../python/bin/export-to-postgresql-record    |    8 -
 .../python/bin/export-to-postgresql-report    |   29 -
 .../python/bin/export-to-sqlite-record        |    8 -
 .../python/bin/export-to-sqlite-report        |   29 -
 .../python/bin/failed-syscalls-by-pid-record  |    3 -
 .../python/bin/failed-syscalls-by-pid-report  |   10 -
 .../perf/scripts/python/bin/flamegraph-record |    2 -
 .../perf/scripts/python/bin/flamegraph-report |    3 -
 .../python/bin/futex-contention-record        |    2 -
 .../python/bin/futex-contention-report        |    4 -
 tools/perf/scripts/python/bin/gecko-record    |    2 -
 tools/perf/scripts/python/bin/gecko-report    |    7 -
 .../scripts/python/bin/intel-pt-events-record |   13 -
 .../scripts/python/bin/intel-pt-events-report |    3 -
 .../scripts/python/bin/mem-phys-addr-record   |   19 -
 .../scripts/python/bin/mem-phys-addr-report   |    3 -
 .../scripts/python/bin/net_dropmonitor-record |    2 -
 .../scripts/python/bin/net_dropmonitor-report |    4 -
 .../scripts/python/bin/netdev-times-record    |    8 -
 .../scripts/python/bin/netdev-times-report    |    5 -
 .../scripts/python/bin/powerpc-hcalls-record  |    2 -
 .../scripts/python/bin/powerpc-hcalls-report  |    2 -
 .../scripts/python/bin/sched-migration-record |    2 -
 .../scripts/python/bin/sched-migration-report |    3 -
 tools/perf/scripts/python/bin/sctop-record    |    3 -
 tools/perf/scripts/python/bin/sctop-report    |   24 -
 .../scripts/python/bin/stackcollapse-record   |    8 -
 .../scripts/python/bin/stackcollapse-report   |    3 -
 .../python/bin/syscall-counts-by-pid-record   |    3 -
 .../python/bin/syscall-counts-by-pid-report   |   10 -
 .../scripts/python/bin/syscall-counts-record  |    3 -
 .../scripts/python/bin/syscall-counts-report  |   10 -
 .../scripts/python/bin/task-analyzer-record   |    2 -
 .../scripts/python/bin/task-analyzer-report   |    3 -
 tools/perf/scripts/python/check-perf-trace.py |   84 -
 tools/perf/scripts/python/compaction-times.py |  311 ---
 .../scripts/python/event_analyzing_sample.py  |  192 --
 .../scripts/python/export-to-postgresql.py    | 1114 --------
 tools/perf/scripts/python/export-to-sqlite.py |  799 ------
 .../scripts/python/failed-syscalls-by-pid.py  |   79 -
 tools/perf/scripts/python/flamegraph.py       |  267 --
 tools/perf/scripts/python/futex-contention.py |   57 -
 tools/perf/scripts/python/gecko.py            |  395 ---
 tools/perf/scripts/python/intel-pt-events.py  |  494 ----
 tools/perf/scripts/python/libxed.py           |  107 -
 tools/perf/scripts/python/mem-phys-addr.py    |  127 -
 tools/perf/scripts/python/net_dropmonitor.py  |   78 -
 tools/perf/scripts/python/netdev-times.py     |  473 ----
 tools/perf/scripts/python/powerpc-hcalls.py   |  335 ---
 tools/perf/scripts/python/sched-migration.py  |  462 ----
 tools/perf/scripts/python/sctop.py            |   89 -
 tools/perf/scripts/python/stackcollapse.py    |  127 -
 tools/perf/scripts/python/stat-cpi.py         |   79 -
 .../scripts/python/syscall-counts-by-pid.py   |   75 -
 tools/perf/scripts/python/syscall-counts.py   |   65 -
 tools/perf/scripts/python/task-analyzer.py    |  934 -------
 tools/perf/tests/shell/script.sh              |   39 -
 tools/perf/tests/shell/script_python.sh       |  113 -
 tools/perf/util/scripting-engines/Build       |    4 -
 .../scripting-engines/trace-event-python.c    | 2333 -----------------
 tools/perf/util/trace-event-scripting.c       |    9 -
 77 files changed, 5 insertions(+), 10632 deletions(-)
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/Build
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/Context.c
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py
 delete mode 100755 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/EventClass.py
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py
 delete mode 100755 tools/perf/scripts/python/arm-cs-trace-disasm.py
 delete mode 100644 tools/perf/scripts/python/bin/compaction-times-record
 delete mode 100644 tools/perf/scripts/python/bin/compaction-times-report
 delete mode 100644 tools/perf/scripts/python/bin/event_analyzing_sample-record
 delete mode 100644 tools/perf/scripts/python/bin/event_analyzing_sample-report
 delete mode 100644 tools/perf/scripts/python/bin/export-to-postgresql-record
 delete mode 100644 tools/perf/scripts/python/bin/export-to-postgresql-report
 delete mode 100644 tools/perf/scripts/python/bin/export-to-sqlite-record
 delete mode 100644 tools/perf/scripts/python/bin/export-to-sqlite-report
 delete mode 100644 tools/perf/scripts/python/bin/failed-syscalls-by-pid-record
 delete mode 100644 tools/perf/scripts/python/bin/failed-syscalls-by-pid-report
 delete mode 100755 tools/perf/scripts/python/bin/flamegraph-record
 delete mode 100755 tools/perf/scripts/python/bin/flamegraph-report
 delete mode 100644 tools/perf/scripts/python/bin/futex-contention-record
 delete mode 100644 tools/perf/scripts/python/bin/futex-contention-report
 delete mode 100644 tools/perf/scripts/python/bin/gecko-record
 delete mode 100755 tools/perf/scripts/python/bin/gecko-report
 delete mode 100644 tools/perf/scripts/python/bin/intel-pt-events-record
 delete mode 100644 tools/perf/scripts/python/bin/intel-pt-events-report
 delete mode 100644 tools/perf/scripts/python/bin/mem-phys-addr-record
 delete mode 100644 tools/perf/scripts/python/bin/mem-phys-addr-report
 delete mode 100755 tools/perf/scripts/python/bin/net_dropmonitor-record
 delete mode 100755 tools/perf/scripts/python/bin/net_dropmonitor-report
 delete mode 100644 tools/perf/scripts/python/bin/netdev-times-record
 delete mode 100644 tools/perf/scripts/python/bin/netdev-times-report
 delete mode 100644 tools/perf/scripts/python/bin/powerpc-hcalls-record
 delete mode 100644 tools/perf/scripts/python/bin/powerpc-hcalls-report
 delete mode 100644 tools/perf/scripts/python/bin/sched-migration-record
 delete mode 100644 tools/perf/scripts/python/bin/sched-migration-report
 delete mode 100644 tools/perf/scripts/python/bin/sctop-record
 delete mode 100644 tools/perf/scripts/python/bin/sctop-report
 delete mode 100755 tools/perf/scripts/python/bin/stackcollapse-record
 delete mode 100755 tools/perf/scripts/python/bin/stackcollapse-report
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-by-pid-record
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-by-pid-report
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-record
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-report
 delete mode 100755 tools/perf/scripts/python/bin/task-analyzer-record
 delete mode 100755 tools/perf/scripts/python/bin/task-analyzer-report
 delete mode 100644 tools/perf/scripts/python/check-perf-trace.py
 delete mode 100644 tools/perf/scripts/python/compaction-times.py
 delete mode 100644 tools/perf/scripts/python/event_analyzing_sample.py
 delete mode 100644 tools/perf/scripts/python/export-to-postgresql.py
 delete mode 100644 tools/perf/scripts/python/export-to-sqlite.py
 delete mode 100644 tools/perf/scripts/python/failed-syscalls-by-pid.py
 delete mode 100755 tools/perf/scripts/python/flamegraph.py
 delete mode 100644 tools/perf/scripts/python/futex-contention.py
 delete mode 100644 tools/perf/scripts/python/gecko.py
 delete mode 100644 tools/perf/scripts/python/intel-pt-events.py
 delete mode 100644 tools/perf/scripts/python/libxed.py
 delete mode 100644 tools/perf/scripts/python/mem-phys-addr.py
 delete mode 100755 tools/perf/scripts/python/net_dropmonitor.py
 delete mode 100644 tools/perf/scripts/python/netdev-times.py
 delete mode 100644 tools/perf/scripts/python/powerpc-hcalls.py
 delete mode 100644 tools/perf/scripts/python/sched-migration.py
 delete mode 100644 tools/perf/scripts/python/sctop.py
 delete mode 100755 tools/perf/scripts/python/stackcollapse.py
 delete mode 100644 tools/perf/scripts/python/stat-cpi.py
 delete mode 100644 tools/perf/scripts/python/syscall-counts-by-pid.py
 delete mode 100644 tools/perf/scripts/python/syscall-counts.py
 delete mode 100755 tools/perf/scripts/python/task-analyzer.py
 delete mode 100755 tools/perf/tests/shell/script_python.sh
 delete mode 100644 tools/perf/util/scripting-engines/trace-event-python.c
tools/perf/Documentation/perf-check.txt +0 -1
index 09e1d35677f5..3d169e5bb372 100644
@@ -60,7 +60,6 @@ feature::
                 libopencsd              /  HAVE_CSTRACE_SUPPORT
                 libperl                 /  HAVE_LIBPERL_SUPPORT
                 libpfm4                 /  HAVE_LIBPFM
-                libpython               /  HAVE_LIBPYTHON_SUPPORT
                 libslang                /  HAVE_SLANG_SUPPORT
                 libtraceevent           /  HAVE_LIBTRACEEVENT
                 libunwind               /  HAVE_LIBUNWIND_SUPPORT
tools/perf/Makefile.config +2 -24
index 4ee7393a39f9..6e03d8f808b5 100644
@@ -793,26 +793,7 @@ ifdef GTK4
   endif
 endif
 
-ifdef LIBPERL
-  PERL_EMBED_LDOPTS = $(shell perl -MExtUtils::Embed -e ldopts 2>/dev/null)
-  PERL_EMBED_LDFLAGS = $(call strip-libs,$(PERL_EMBED_LDOPTS))
-  PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS))
-  PERL_EMBED_CCOPTS = $(shell perl -MExtUtils::Embed -e ccopts 2>/dev/null)
-  PERL_EMBED_CCOPTS := $(filter-out -specs=%,$(PERL_EMBED_CCOPTS))
-  PERL_EMBED_CCOPTS := $(filter-out -flto% -ffat-lto-objects, $(PERL_EMBED_CCOPTS))
-  PERL_EMBED_LDOPTS := $(filter-out -specs=%,$(PERL_EMBED_LDOPTS))
-  FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)
-
-  $(call feature_check,libperl)
-  ifneq ($(feature-libperl), 1)
-    $(error Missing perl devel files. Please install perl-ExtUtils-Embed/libperl-dev)
-  else
-    LDFLAGS += $(PERL_EMBED_LDFLAGS)
-    EXTLIBS += $(PERL_EMBED_LIBADD)
-    CFLAGS += -DHAVE_LIBPERL_SUPPORT
-    $(call detected,CONFIG_LIBPERL)
-  endif
-endif
+
 
 ifeq ($(feature-timerfd), 1)
   CFLAGS += -DHAVE_TIMERFD_SUPPORT
@@ -844,8 +825,7 @@ else
       ifneq ($(feature-libpython), 1)
         $(call disable-python,No 'Python.h' was found: disables Python support - please install python-devel/python-dev)
       else
-         LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
-         EXTLIBS += $(PYTHON_EMBED_LIBADD)
+         CFLAGS += -DHAVE_LIBPYTHON_SUPPORT
          PYTHON_SETUPTOOLS_INSTALLED := $(shell $(PYTHON) -c 'import setuptools;' 2> /dev/null && echo "yes" || echo "no")
          ifeq ($(PYTHON_SETUPTOOLS_INSTALLED), yes)
            PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
@@ -856,8 +836,6 @@ else
 	 else
            $(warning Missing python setuptools, the python binding won't be built, please install python3-setuptools or equivalent)
          endif
-         CFLAGS += -DHAVE_LIBPYTHON_SUPPORT
-         $(call detected,CONFIG_LIBPYTHON)
          ifeq ($(filter -fPIC,$(CFLAGS)),)
            # Building a shared library requires position independent code.
            CFLAGS += -fPIC
tools/perf/Makefile.perf +2 -5
index abd377c16435..45e5a860d537 100644
@@ -900,11 +900,8 @@ ifdef LIBPERL
 endif
 ifndef NO_LIBPYTHON
 	$(call QUIET_INSTALL, python-scripts) \
-		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'; \
-		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin'; \
-		$(INSTALL) scripts/python/Perf-Trace-Util/lib/Perf/Trace/* -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'; \
-		$(INSTALL) scripts/python/*.py -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'; \
-		$(INSTALL) scripts/python/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin'
+		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'; \
+		$(INSTALL) python/*.py -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'
 	$(call QUIET_INSTALL, python-scripts-standalone) \
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python'; \
 		$(INSTALL) python/*.py -m 755 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python'
tools/perf/builtin-check.c +1 -1
index 60437650c50f..35272aaeb613 100644
@@ -52,8 +52,8 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("libnuma", HAVE_LIBNUMA_SUPPORT),
 	FEATURE_STATUS("libopencsd", HAVE_CSTRACE_SUPPORT),
 	FEATURE_STATUS_TIP("libperl", HAVE_LIBPERL_SUPPORT, "Deprecated, use LIBPERL=1 and install perl-ExtUtils-Embed/libperl-dev to build with it"),
+	FEATURE_STATUS("python-module", HAVE_LIBPYTHON_SUPPORT),
 	FEATURE_STATUS("libpfm4", HAVE_LIBPFM),
-	FEATURE_STATUS("libpython", HAVE_LIBPYTHON_SUPPORT),
 	FEATURE_STATUS("libslang", HAVE_SLANG_SUPPORT),
 	FEATURE_STATUS("libtraceevent", HAVE_LIBTRACEEVENT),
 	FEATURE_STATUS_TIP("libunwind", HAVE_LIBUNWIND_SUPPORT, "Deprecated, use LIBUNWIND=1 and install libunwind-dev[el] to build with it"),
tools/perf/scripts/Build +0 -27
index 91229a1fe3ff..fbeab8fff88b 100644
@@ -1,30 +1,3 @@
 ifeq ($(CONFIG_LIBTRACEEVENT),y)
   perf-util-$(CONFIG_LIBPERL)   += perl/Perf-Trace-Util/
 endif
-perf-util-$(CONFIG_LIBPYTHON) += python/Perf-Trace-Util/
-
-ifdef MYPY
-  PY_TESTS := $(shell find python -type f -name '*.py')
-  MYPY_TEST_LOGS := $(PY_TESTS:python/%=python/%.mypy_log)
-else
-  MYPY_TEST_LOGS :=
-endif
-
-$(OUTPUT)%.mypy_log: %
-	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
-
-perf-y += $(MYPY_TEST_LOGS)
-
-ifdef PYLINT
-  PY_TESTS := $(shell find python -type f -name '*.py')
-  PYLINT_TEST_LOGS := $(PY_TESTS:python/%=python/%.pylint_log)
-else
-  PYLINT_TEST_LOGS :=
-endif
-
-$(OUTPUT)%.pylint_log: %
-	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)pylint "$<" > $@ || (cat $@ && rm $@ && false)
-
-perf-y += $(PYLINT_TEST_LOGS)
tools/perf/scripts/python/Perf-Trace-Util/Build deleted +0 -4
deleted file mode 100644
index be3710c61320..000000000000
@@ -1,4 +0,0 @@
-perf-util-y += Context.o
-
-# -Wno-declaration-after-statement: The python headers have mixed code with declarations (decls after asserts, for instance)
-CFLAGS_Context.o += $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs -Wno-declaration-after-statement
tools/perf/scripts/python/Perf-Trace-Util/Context.c deleted +0 -225

225 lines not on this page · raw message

tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py deleted +0 -116

116 lines not on this page · raw message

tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/EventClass.py deleted +0 -97

97 lines not on this page · raw message

tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py deleted +0 -184

184 lines not on this page · raw message

tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py deleted +0 -92

92 lines not on this page · raw message

tools/perf/scripts/python/arm-cs-trace-disasm.py deleted +0 -356

356 lines not on this page · raw message

tools/perf/scripts/python/bin/compaction-times-record deleted +0 -2
deleted file mode 100644
index 6edcd40e14e8..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e compaction:mm_compaction_begin -e compaction:mm_compaction_end -e compaction:mm_compaction_migratepages -e compaction:mm_compaction_isolate_migratepages -e compaction:mm_compaction_isolate_freepages $@
tools/perf/scripts/python/bin/compaction-times-report deleted +0 -4
deleted file mode 100644
index 3dc13897cfde..000000000000
@@ -1,4 +0,0 @@
-#!/bin/bash
-#description: display time taken by mm compaction
-#args: [-h] [-u] [-p|-pv] [-t | [-m] [-fs] [-ms]] [pid|pid-range|comm-regex]
-perf script -s "$PERF_EXEC_PATH"/scripts/python/compaction-times.py $@
tools/perf/scripts/python/bin/event_analyzing_sample-record deleted +0 -8
deleted file mode 100644
index 5ce652dabd02..000000000000
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-#
-# event_analyzing_sample.py can cover all type of perf samples including
-# the tracepoints, so no special record requirements, just record what
-# you want to analyze.
-#
-perf record $@
tools/perf/scripts/python/bin/event_analyzing_sample-report deleted +0 -3
deleted file mode 100644
index 0941fc94e158..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: analyze all perf samples
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/event_analyzing_sample.py
tools/perf/scripts/python/bin/export-to-postgresql-record deleted +0 -8
deleted file mode 100644
index 221d66e05713..000000000000
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-#
-# export perf data to a postgresql database. Can cover
-# perf ip samples (excluding the tracepoints). No special
-# record requirements, just record what you want to export.
-#
-perf record $@
tools/perf/scripts/python/bin/export-to-postgresql-report deleted +0 -29
deleted file mode 100644
index cd335b6e2a01..000000000000
@@ -1,29 +0,0 @@
-#!/bin/bash
-# description: export perf data to a postgresql database
-# args: [database name] [columns] [calls]
-n_args=0
-for i in "$@"
-do
-    if expr match "$i" "-" > /dev/null ; then
-	break
-    fi
-    n_args=$(( $n_args + 1 ))
-done
-if [ "$n_args" -gt 3 ] ; then
-    echo "usage: export-to-postgresql-report [database name] [columns] [calls]"
-    exit
-fi
-if [ "$n_args" -gt 2 ] ; then
-    dbname=$1
-    columns=$2
-    calls=$3
-    shift 3
-elif [ "$n_args" -gt 1 ] ; then
-    dbname=$1
-    columns=$2
-    shift 2
-elif [ "$n_args" -gt 0 ] ; then
-    dbname=$1
-    shift
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/export-to-postgresql.py $dbname $columns $calls
tools/perf/scripts/python/bin/export-to-sqlite-record deleted +0 -8
deleted file mode 100644
index 070204fd6d00..000000000000
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-#
-# export perf data to a sqlite3 database. Can cover
-# perf ip samples (excluding the tracepoints). No special
-# record requirements, just record what you want to export.
-#
-perf record $@
tools/perf/scripts/python/bin/export-to-sqlite-report deleted +0 -29
deleted file mode 100644
index 5ff6033e70ba..000000000000
@@ -1,29 +0,0 @@
-#!/bin/bash
-# description: export perf data to a sqlite3 database
-# args: [database name] [columns] [calls]
-n_args=0
-for i in "$@"
-do
-    if expr match "$i" "-" > /dev/null ; then
-	break
-    fi
-    n_args=$(( $n_args + 1 ))
-done
-if [ "$n_args" -gt 3 ] ; then
-    echo "usage: export-to-sqlite-report [database name] [columns] [calls]"
-    exit
-fi
-if [ "$n_args" -gt 2 ] ; then
-    dbname=$1
-    columns=$2
-    calls=$3
-    shift 3
-elif [ "$n_args" -gt 1 ] ; then
-    dbname=$1
-    columns=$2
-    shift 2
-elif [ "$n_args" -gt 0 ] ; then
-    dbname=$1
-    shift
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/export-to-sqlite.py $dbname $columns $calls
tools/perf/scripts/python/bin/failed-syscalls-by-pid-record deleted +0 -3
deleted file mode 100644
index 74685f318379..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-(perf record -e raw_syscalls:sys_exit $@ || \
- perf record -e syscalls:sys_exit $@) 2> /dev/null
tools/perf/scripts/python/bin/failed-syscalls-by-pid-report deleted +0 -10
deleted file mode 100644
index fda5096d0cbf..000000000000
@@ -1,10 +0,0 @@
-#!/bin/bash
-# description: system-wide failed syscalls, by pid
-# args: [comm]
-if [ $# -gt 0 ] ; then
-    if ! expr match "$1" "-" > /dev/null ; then
-	comm=$1
-	shift
-    fi
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/failed-syscalls-by-pid.py $comm
tools/perf/scripts/python/bin/flamegraph-record deleted +0 -2
deleted file mode 100755
index 7df5a19c0163..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -g "$@"
tools/perf/scripts/python/bin/flamegraph-report deleted +0 -3
deleted file mode 100755
index 453a6918afbe..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: create flame graphs
-perf script -s "$PERF_EXEC_PATH"/scripts/python/flamegraph.py "$@"
tools/perf/scripts/python/bin/futex-contention-record deleted +0 -2
deleted file mode 100644
index b1495c9a9b20..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e syscalls:sys_enter_futex -e syscalls:sys_exit_futex $@
tools/perf/scripts/python/bin/futex-contention-report deleted +0 -4
deleted file mode 100644
index 6c44271091ab..000000000000
@@ -1,4 +0,0 @@
-#!/bin/bash
-# description: futext contention measurement
-
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/futex-contention.py
tools/perf/scripts/python/bin/gecko-record deleted +0 -2
deleted file mode 100644
index f0d1aa55f171..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -F 99 -g "$@"
tools/perf/scripts/python/bin/gecko-report deleted +0 -7
deleted file mode 100755
index 1867ec8d9757..000000000000
@@ -1,7 +0,0 @@
-#!/bin/bash
-# description: create firefox gecko profile json format from perf.data
-if [ "$*" = "-i -" ]; then
-perf script -s "$PERF_EXEC_PATH"/scripts/python/gecko.py
-else
-perf script -s "$PERF_EXEC_PATH"/scripts/python/gecko.py -- "$@"
-fi
tools/perf/scripts/python/bin/intel-pt-events-record deleted +0 -13
deleted file mode 100644
index 6b9877cfe23e..000000000000
@@ -1,13 +0,0 @@
-#!/bin/bash
-
-#
-# print Intel PT Events including Power Events and PTWRITE. The intel_pt PMU
-# event needs to be specified with appropriate config terms.
-#
-if ! echo "$@" | grep -q intel_pt ; then
-	echo "Options must include the Intel PT event e.g. -e intel_pt/pwr_evt,ptw/"
-	echo "and for power events it probably needs to be system wide i.e. -a option"
-	echo "For example: -a -e intel_pt/pwr_evt,branch=0/ sleep 1"
-	exit 1
-fi
-perf record $@
tools/perf/scripts/python/bin/intel-pt-events-report deleted +0 -3
deleted file mode 100644
index beeac3fde9db..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: print Intel PT Events including Power Events and PTWRITE
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/intel-pt-events.py
tools/perf/scripts/python/bin/mem-phys-addr-record deleted +0 -19
deleted file mode 100644
index 5a875122a904..000000000000
@@ -1,19 +0,0 @@
-#!/bin/bash
-
-#
-# Profiling physical memory by all retired load instructions/uops event
-# MEM_INST_RETIRED.ALL_LOADS or MEM_UOPS_RETIRED.ALL_LOADS
-#
-
-load=`perf list | grep mem_inst_retired.all_loads`
-if [ -z "$load" ]; then
-	load=`perf list | grep mem_uops_retired.all_loads`
-fi
-if [ -z "$load" ]; then
-	echo "There is no event to count all retired load instructions/uops."
-	exit 1
-fi
-
-arg=$(echo $load | tr -d ' ')
-arg="$arg:P"
-perf record --phys-data -e $arg $@
tools/perf/scripts/python/bin/mem-phys-addr-report deleted +0 -3
deleted file mode 100644
index 3f2b847e2eab..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: resolve physical address samples
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/mem-phys-addr.py
tools/perf/scripts/python/bin/net_dropmonitor-record deleted +0 -2
deleted file mode 100755
index 423fb81dadae..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e skb:kfree_skb $@
tools/perf/scripts/python/bin/net_dropmonitor-report deleted +0 -4
deleted file mode 100755
index 8d698f5a06aa..000000000000
@@ -1,4 +0,0 @@
-#!/bin/bash
-# description: display a table of dropped frames
-
-perf script -s "$PERF_EXEC_PATH"/scripts/python/net_dropmonitor.py $@
tools/perf/scripts/python/bin/netdev-times-record deleted +0 -8
deleted file mode 100644
index 558754b840a9..000000000000
@@ -1,8 +0,0 @@
-#!/bin/bash
-perf record -e net:net_dev_xmit -e net:net_dev_queue		\
-		-e net:netif_receive_skb -e net:netif_rx		\
-		-e skb:consume_skb -e skb:kfree_skb			\
-		-e skb:skb_copy_datagram_iovec -e napi:napi_poll	\
-		-e irq:irq_handler_entry -e irq:irq_handler_exit	\
-		-e irq:softirq_entry -e irq:softirq_exit		\
-		-e irq:softirq_raise $@
tools/perf/scripts/python/bin/netdev-times-report deleted +0 -5
deleted file mode 100644
index 8f759291da86..000000000000
@@ -1,5 +0,0 @@
-#!/bin/bash
-# description: display a process of packet and processing time
-# args: [tx] [rx] [dev=] [debug]
-
-perf script -s "$PERF_EXEC_PATH"/scripts/python/netdev-times.py $@
tools/perf/scripts/python/bin/powerpc-hcalls-record deleted +0 -2
deleted file mode 100644
index b7402aa9147d..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e "{powerpc:hcall_entry,powerpc:hcall_exit}" $@
tools/perf/scripts/python/bin/powerpc-hcalls-report deleted +0 -2
deleted file mode 100644
index dd32ad7465f6..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/powerpc-hcalls.py
tools/perf/scripts/python/bin/sched-migration-record deleted +0 -2
deleted file mode 100644
index 7493fddbe995..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -m 16384 -e sched:sched_wakeup -e sched:sched_wakeup_new -e sched:sched_switch -e sched:sched_migrate_task $@
tools/perf/scripts/python/bin/sched-migration-report deleted +0 -3
deleted file mode 100644
index 68b037a1849b..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: sched migration overview
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/sched-migration.py
tools/perf/scripts/python/bin/sctop-record deleted +0 -3
deleted file mode 100644
index d6940841e54f..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-(perf record -e raw_syscalls:sys_enter $@ || \
- perf record -e syscalls:sys_enter $@) 2> /dev/null
tools/perf/scripts/python/bin/sctop-report deleted +0 -24
deleted file mode 100644
index c32db294124d..000000000000
@@ -1,24 +0,0 @@
-#!/bin/bash
-# description: syscall top
-# args: [comm] [interval]
-n_args=0
-for i in "$@"
-do
-    if expr match "$i" "-" > /dev/null ; then
-	break
-    fi
-    n_args=$(( $n_args + 1 ))
-done
-if [ "$n_args" -gt 2 ] ; then
-    echo "usage: sctop-report [comm] [interval]"
-    exit
-fi
-if [ "$n_args" -gt 1 ] ; then
-    comm=$1
-    interval=$2
-    shift 2
-elif [ "$n_args" -gt 0 ] ; then
-    interval=$1
-    shift
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/sctop.py $comm $interval
tools/perf/scripts/python/bin/stackcollapse-record deleted +0 -8
deleted file mode 100755
index 9d8f9f0f3a17..000000000000
@@ -1,8 +0,0 @@
-#!/bin/sh
-
-#
-# stackcollapse.py can cover all type of perf samples including
-# the tracepoints, so no special record requirements, just record what
-# you want to analyze.
-#
-perf record "$@"
tools/perf/scripts/python/bin/stackcollapse-report deleted +0 -3
deleted file mode 100755
index 21a356bd27f6..000000000000
@@ -1,3 +0,0 @@
-#!/bin/sh
-# description: produce callgraphs in short form for scripting use
-perf script -s "$PERF_EXEC_PATH"/scripts/python/stackcollapse.py "$@"
tools/perf/scripts/python/bin/syscall-counts-by-pid-record deleted +0 -3
deleted file mode 100644
index d6940841e54f..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-(perf record -e raw_syscalls:sys_enter $@ || \
- perf record -e syscalls:sys_enter $@) 2> /dev/null
tools/perf/scripts/python/bin/syscall-counts-by-pid-report deleted +0 -10
deleted file mode 100644
index 16eb8d65c543..000000000000
@@ -1,10 +0,0 @@
-#!/bin/bash
-# description: system-wide syscall counts, by pid
-# args: [comm]
-if [ $# -gt 0 ] ; then
-    if ! expr match "$1" "-" > /dev/null ; then
-	comm=$1
-	shift
-    fi
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/syscall-counts-by-pid.py $comm
tools/perf/scripts/python/bin/syscall-counts-record deleted +0 -3
deleted file mode 100644
index d6940841e54f..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-(perf record -e raw_syscalls:sys_enter $@ || \
- perf record -e syscalls:sys_enter $@) 2> /dev/null
tools/perf/scripts/python/bin/syscall-counts-report deleted +0 -10
deleted file mode 100644
index 0f0e9d453bb4..000000000000
@@ -1,10 +0,0 @@
-#!/bin/bash
-# description: system-wide syscall counts
-# args: [comm]
-if [ $# -gt 0 ] ; then
-    if ! expr match "$1" "-" > /dev/null ; then
-	comm=$1
-	shift
-    fi
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/python/syscall-counts.py $comm
tools/perf/scripts/python/bin/task-analyzer-record deleted +0 -2
deleted file mode 100755
index 0f6b51bb2767..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e sched:sched_switch -e sched:sched_migrate_task "$@"
tools/perf/scripts/python/bin/task-analyzer-report deleted +0 -3
deleted file mode 100755
index 4b16a8cc40a0..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: analyze timings of tasks
-perf script -s "$PERF_EXEC_PATH"/scripts/python/task-analyzer.py -- "$@"
tools/perf/scripts/python/check-perf-trace.py deleted +0 -84

84 lines not on this page · raw message

tools/perf/scripts/python/compaction-times.py deleted +0 -311

311 lines not on this page · raw message

tools/perf/scripts/python/event_analyzing_sample.py deleted +0 -192

192 lines not on this page · raw message

tools/perf/scripts/python/export-to-postgresql.py deleted +0 -1114

977 lines not on this page · raw message

tools/perf/scripts/python/export-to-sqlite.py deleted +0 -799

799 lines not on this page · raw message

tools/perf/scripts/python/failed-syscalls-by-pid.py deleted +0 -79

79 lines not on this page · raw message

tools/perf/scripts/python/flamegraph.py deleted +0 -267

267 lines not on this page · raw message

tools/perf/scripts/python/futex-contention.py deleted +0 -57

57 lines not on this page · raw message

tools/perf/scripts/python/gecko.py deleted +0 -395

395 lines not on this page · raw message

tools/perf/scripts/python/intel-pt-events.py deleted +0 -494

494 lines not on this page · raw message

tools/perf/scripts/python/libxed.py deleted +0 -107

107 lines not on this page · raw message

tools/perf/scripts/python/mem-phys-addr.py deleted +0 -127

127 lines not on this page · raw message

tools/perf/scripts/python/net_dropmonitor.py deleted +0 -78

78 lines not on this page · raw message

tools/perf/scripts/python/netdev-times.py deleted +0 -473

473 lines not on this page · raw message

tools/perf/scripts/python/powerpc-hcalls.py deleted +0 -335

335 lines not on this page · raw message

tools/perf/scripts/python/sched-migration.py deleted +0 -462

462 lines not on this page · raw message

tools/perf/scripts/python/sctop.py deleted +0 -89

89 lines not on this page · raw message

tools/perf/scripts/python/stackcollapse.py deleted +0 -127

127 lines not on this page · raw message

tools/perf/scripts/python/stat-cpi.py deleted +0 -79

79 lines not on this page · raw message

tools/perf/scripts/python/syscall-counts-by-pid.py deleted +0 -75

75 lines not on this page · raw message

tools/perf/scripts/python/syscall-counts.py deleted +0 -65

65 lines not on this page · raw message

tools/perf/scripts/python/task-analyzer.py deleted +0 -934

934 lines not on this page · raw message

tools/perf/tests/shell/script.sh +0 -39
index 254fc3ae94e7..902343f7a83c 100755

Lines of this file are not shown. View the raw message.

tools/perf/tests/shell/script_python.sh deleted +0 -113

113 lines not on this page · raw message

tools/perf/util/scripting-engines/Build +0 -4
index 24f087b0cd11..3f1dc10526f8 100644

Lines of this file are not shown. View the raw message.

tools/perf/util/scripting-engines/trace-event-python.c deleted +0 -2333

2,333 lines not on this page · raw message

tools/perf/util/trace-event-scripting.c +0 -9
index dc584ac316a3..c78b317978dd 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog

Part of the diff is not shown here. View the raw message.

author reviewer
#201 lore raw

PATCH v2 47/49 perf Makefile: Update Python script installation path

In reply to Ian Rogers

Replace the libpython feature test with a python-module feature test checking for Python C extension build capability, and update feature test references accordingly.

Remove references to the legacy scripts/python directory and install standalone Python scripts directly under the python directory in libexec. Update the TUI script browser (ui/browsers/scripts.c) to discover standalone scripts from the updated installation path.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/build/Makefile.feature             |   4 +-
 tools/build/feature/Makefile             |   4 +-
 tools/build/feature/test-all.c           |   6 +-
 tools/build/feature/test-libpython.c     |  10 --
 tools/build/feature/test-python-module.c |  13 ++
 tools/perf/Documentation/perf-check.txt  |   1 +
 tools/perf/Makefile.config               |  15 +-
 tools/perf/Makefile.perf                 |   8 +-
 tools/perf/builtin-check.c               |   2 +-
 tools/perf/scripts/install-build-deps.sh |   4 +-
 tools/perf/tests/make                    |   8 +-
 tools/perf/ui/browsers/scripts.c         | 182 +++++++++++++++--------
 12 files changed, 155 insertions(+), 102 deletions(-)
 delete mode 100644 tools/build/feature/test-libpython.c
 create mode 100644 tools/build/feature/test-python-module.c
tools/build/Makefile.feature +2 -2
index 331f5cdfc34b..3ca7a4f5c7fd 100644
@@ -79,7 +79,7 @@ FEATURE_TESTS_BASIC :=                  \
         libelf-zstd                     \
         libnuma                         \
         numa_num_possible_cpus          \
-        libpython                       \
+        python-module                   \
         libslang                        \
         libtraceevent                   \
         libcpupower                     \
@@ -145,7 +145,7 @@ FEATURE_DISPLAY ?=              \
          libelf                 \
          libnuma                \
          numa_num_possible_cpus \
-         libpython              \
+         python-module          \
          libcapstone            \
          llvm-perf              \
          zlib                   \
tools/build/feature/Makefile +2 -2
index cdf89f132074..269af8e8f5cf 100644
@@ -32,7 +32,7 @@ FILES=                                          \
          test-libnuma.bin                       \
          test-numa_num_possible_cpus.bin        \
          test-libperl.bin                       \
-         test-libpython.bin                     \
+         test-python-module.bin                 \
          test-libslang.bin                      \
          test-libtraceevent.bin                 \
          test-libcpupower.bin                   \
@@ -261,7 +261,7 @@ endif
 $(OUTPUT)test-libperl.bin:
 	$(BUILD) $(FLAGS_PERL_EMBED)
 
-$(OUTPUT)test-libpython.bin:
+$(OUTPUT)test-python-module.bin:
 	$(BUILD) $(FLAGS_PYTHON_EMBED)
 
 $(OUTPUT)test-libbfd.bin:
tools/build/feature/test-all.c +3 -3
index 544563d62950..0ee16eccd9e0 100644
@@ -10,8 +10,8 @@
  * Quirk: Python headers cannot be in arbitrary places, so keep this testcase at
  * the top:
  */
-#define main main_test_libpython
-# include "test-libpython.c"
+#define main main_test_python_module
+# include "test-python-module.c"
 #undef main
 
 #define main main_test_hello
@@ -148,7 +148,7 @@
 
 int main(int argc, char *argv[])
 {
-	main_test_libpython();
+	main_test_python_module();
 	main_test_hello();
 	main_test_libelf();
 	main_test_gettid();
tools/build/feature/test-libpython.c deleted +0 -10
deleted file mode 100644
index 371c9113e49d..000000000000
@@ -1,10 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-#include <Python.h>
-
-int main(void)
-{
-	Py_Initialize();
-
-	return 0;
-}
-#undef _GNU_SOURCE
tools/build/feature/test-python-module.c new file +13 -0
new file mode 100644
index 000000000000..50e9e5062feb
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <Python.h>
+
+int main(void)
+{
+	static struct PyModuleDef moduledef = {
+		PyModuleDef_HEAD_INIT,
+		.m_name = "test",
+	};
+	PyObject *module = PyModule_Create(&moduledef);
+
+	return module ? 0 : -1;
+}
tools/perf/Documentation/perf-check.txt +1 -0
index 3d169e5bb372..a80913794eb6 100644
@@ -65,6 +65,7 @@ feature::
                 libunwind               /  HAVE_LIBUNWIND_SUPPORT
                 lzma                    /  HAVE_LZMA_SUPPORT
                 numa_num_possible_cpus  /  HAVE_LIBNUMA_SUPPORT
+                python-module           /  HAVE_PYTHON_MODULE_SUPPORT
                 zlib                    /  HAVE_ZLIB_SUPPORT
                 zstd                    /  HAVE_ZSTD_SUPPORT
 
tools/perf/Makefile.config +7 -8
index 6e03d8f808b5..7e05b7943b3b 100644
@@ -305,7 +305,7 @@ PYTHON_CONFIG_SQ := $(call shell-sq,$(PYTHON_CONFIG))
 
 # Python 3.8 changed the output of `python-config --ldflags` to not include the
 # '-lpythonX.Y' flag unless '--embed' is also passed. The feature check for
-# libpython fails if that flag is not included in LDFLAGS
+# python-module fails if that flag is not included in LDFLAGS
 ifeq ($(shell $(PYTHON_CONFIG_SQ) --ldflags --embed 2>&1 1>/dev/null; echo $$?), 0)
   PYTHON_CONFIG_LDFLAGS := --ldflags --embed
 else
@@ -328,8 +328,8 @@ ifdef PYTHON_CONFIG
   endif
 endif
 
-FEATURE_CHECK_CFLAGS-libpython := $(PYTHON_EMBED_CCOPTS)
-FEATURE_CHECK_LDFLAGS-libpython := $(PYTHON_EMBED_LDOPTS)
+FEATURE_CHECK_CFLAGS-python-module := $(PYTHON_EMBED_CCOPTS)
+FEATURE_CHECK_LDFLAGS-python-module := $(PYTHON_EMBED_LDOPTS)
 
 FEATURE_CHECK_LDFLAGS-libaio = -lrt
 
@@ -803,13 +803,12 @@ endif
 
 disable-python = $(eval $(disable-python_code))
 define disable-python_code
-  CFLAGS += -DNO_LIBPYTHON
   $(warning $1)
-  NO_LIBPYTHON := 1
+  NO_PYTHON_MODULE := 1
 endef
 
 PYTHON_EXTENSION_SUFFIX := '.so'
-ifdef NO_LIBPYTHON
+ifdef NO_PYTHON_MODULE
   $(call disable-python,Python support disabled by user)
 else
 
@@ -822,10 +821,10 @@ else
       $(call disable-python,No 'python-config' tool was found: disables Python support - please install python-devel/python-dev)
     else
 
-      ifneq ($(feature-libpython), 1)
+      ifneq ($(feature-python-module), 1)
         $(call disable-python,No 'Python.h' was found: disables Python support - please install python-devel/python-dev)
       else
-         CFLAGS += -DHAVE_LIBPYTHON_SUPPORT
+         CFLAGS += -DHAVE_PYTHON_MODULE_SUPPORT
          PYTHON_SETUPTOOLS_INSTALLED := $(shell $(PYTHON) -c 'import setuptools;' 2> /dev/null && echo "yes" || echo "no")
          ifeq ($(PYTHON_SETUPTOOLS_INSTALLED), yes)
            PYTHON_EXTENSION_SUFFIX := $(shell $(PYTHON) -c 'from importlib import machinery; print(machinery.EXTENSION_SUFFIXES[0])')
tools/perf/Makefile.perf +3 -5
index 45e5a860d537..6bcea56f1f90 100644
@@ -19,7 +19,7 @@ include ../scripts/utilities.mak
 #
 # Define LIBPERL to enable perl script extension.
 #
-# Define NO_LIBPYTHON to disable python script extension.
+# Define NO_PYTHON_MODULE to disable python script extension.
 #
 # Define PYTHON to point to the python binary if the default
 # `python' is not correct; for example: PYTHON=python2
@@ -898,11 +898,9 @@ ifdef LIBPERL
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'; \
 		$(INSTALL) scripts/perl/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'
 endif
-ifndef NO_LIBPYTHON
+
+ifndef NO_PYTHON_MODULE
 	$(call QUIET_INSTALL, python-scripts) \
-		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'; \
-		$(INSTALL) python/*.py -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'
-	$(call QUIET_INSTALL, python-scripts-standalone) \
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python'; \
 		$(INSTALL) python/*.py -m 755 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python'
 endif
tools/perf/builtin-check.c +1 -1
index 35272aaeb613..06711362d37f 100644
@@ -52,7 +52,7 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("libnuma", HAVE_LIBNUMA_SUPPORT),
 	FEATURE_STATUS("libopencsd", HAVE_CSTRACE_SUPPORT),
 	FEATURE_STATUS_TIP("libperl", HAVE_LIBPERL_SUPPORT, "Deprecated, use LIBPERL=1 and install perl-ExtUtils-Embed/libperl-dev to build with it"),
-	FEATURE_STATUS("python-module", HAVE_LIBPYTHON_SUPPORT),
+	FEATURE_STATUS("python-module", HAVE_PYTHON_MODULE_SUPPORT),
 	FEATURE_STATUS("libpfm4", HAVE_LIBPFM),
 	FEATURE_STATUS("libslang", HAVE_SLANG_SUPPORT),
 	FEATURE_STATUS("libtraceevent", HAVE_LIBTRACEEVENT),
tools/perf/scripts/install-build-deps.sh +2 -2
index a601a5260c17..3e53c7bf0f85 100755
@@ -155,7 +155,7 @@ fedora_pkg_for() {
 	libcapstone)
 		echo "capstone-devel"
 		;;
-	libpython)
+	python-module)
 		echo "python3-devel"
 		;;
 	libtraceevent)
@@ -314,7 +314,7 @@ debian_pkg_for() {
 	libcapstone)
 		echo "libcapstone-dev"
 		;;
-	libpython)
+	python-module)
 		echo "python3-dev"
 		;;
 	libtraceevent)
tools/perf/tests/make +4 -4
index f879f8109072..b5eaf326573c 100644
@@ -77,8 +77,8 @@ make_jevents_all    := JEVENTS_ARCH=all
 make_no_bpf_skel    := BUILD_BPF_SKEL=0
 make_gen_vmlinux_h  := GEN_VMLINUX_H=1
 make_libperl        := LIBPERL=1
-make_no_libpython   := NO_LIBPYTHON=1
-make_no_scripts     := NO_LIBPYTHON=1
+make_no_python_module := NO_PYTHON_MODULE=1
+make_no_scripts     := NO_PYTHON_MODULE=1
 make_no_slang       := NO_SLANG=1
 make_no_demangle    := NO_DEMANGLE=1
 make_no_libelf      := NO_LIBELF=1
@@ -118,7 +118,7 @@ make_install_prefix_slash := install prefix=/tmp/krava/
 make_static         := LDFLAGS=-static NO_PERF_READ_VDSO32=1 NO_PERF_READ_VDSOX32=1 NO_JVMTI=1 NO_LIBTRACEEVENT=1 NO_LIBELF=1
 
 # all the NO_* variable combined
-make_minimal        := NO_LIBPYTHON=1
+make_minimal        := NO_PYTHON_MODULE=1
 make_minimal        += NO_DEMANGLE=1 NO_LIBELF=1 NO_BACKTRACE=1
 make_minimal        += NO_LIBNUMA=1 NO_LIBBIONIC=1 NO_LIBDW=1
 make_minimal        += NO_LIBBPF=1
@@ -150,7 +150,7 @@ run += make_jevents_all
 run += make_no_bpf_skel
 run += make_gen_vmlinux_h
 run += make_libperl
-run += make_no_libpython
+run += make_no_python_module
 run += make_no_scripts
 run += make_no_slang
 run += make_no_demangle
tools/perf/ui/browsers/scripts.c +117 -65

221 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
author reviewer
#203 lore raw

PATCH v2 48/49 perf script: Support standalone scripts and remove embedded scripting

In reply to Ian Rogers

Refactor 'perf script' to launch standalone scripts directly via fork() and execvp() and remove the legacy embedded Perl and Python scripting engines:
- Remove the embedded Perl scripting engine (util/scripting-engines/trace-event-perl.c), Perl scripts, bin wrappers, and Trace-Util library (scripts/perl/Perf-Trace-Util/), and script_perl.sh test.
- Remove libperl feature checks from Makefile.config, Makefile.perf, builtin-check.c, and Documentation/perf-check.txt.
- Remove -g / --gen-script option and scripting_ops dispatch table from builtin-script.c and trace-event-scripting.c.
- Hide the legacy -s / --script option and update script discovery in find_script() and list_available_scripts() to prioritize the system 'python' directory over bare filenames in the current directory.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/build/Makefile.feature                  |   1 -
 tools/build/feature/Makefile                  |  20 -
 tools/build/feature/test-libperl.c            |  10 -
 tools/perf/Build                              |   1 -
 tools/perf/Documentation/perf-check.txt       |   1 -
 tools/perf/Makefile.config                    |   1 -
 tools/perf/Makefile.perf                      |  14 -
 tools/perf/builtin-check.c                    |   1 -
 tools/perf/builtin-script.c                   | 824 ++++++++----------
 tools/perf/scripts/Build                      |   3 -
 tools/perf/scripts/perl/Perf-Trace-Util/Build |   9 -
 .../scripts/perl/Perf-Trace-Util/Context.c    | 122 ---
 .../scripts/perl/Perf-Trace-Util/Context.xs   |  42 -
 .../scripts/perl/Perf-Trace-Util/Makefile.PL  |  18 -
 .../perf/scripts/perl/Perf-Trace-Util/README  |  59 --
 .../Perf-Trace-Util/lib/Perf/Trace/Context.pm |  55 --
 .../Perf-Trace-Util/lib/Perf/Trace/Core.pm    | 192 ----
 .../Perf-Trace-Util/lib/Perf/Trace/Util.pm    |  94 --
 .../perf/scripts/perl/Perf-Trace-Util/typemap |   1 -
 .../scripts/perl/bin/check-perf-trace-record  |   2 -
 .../scripts/perl/bin/failed-syscalls-record   |   3 -
 .../scripts/perl/bin/failed-syscalls-report   |  10 -
 tools/perf/scripts/perl/bin/rw-by-file-record |   3 -
 tools/perf/scripts/perl/bin/rw-by-file-report |  10 -
 tools/perf/scripts/perl/bin/rw-by-pid-record  |   2 -
 tools/perf/scripts/perl/bin/rw-by-pid-report  |   3 -
 tools/perf/scripts/perl/bin/rwtop-record      |   2 -
 tools/perf/scripts/perl/bin/rwtop-report      |  20 -
 .../scripts/perl/bin/wakeup-latency-record    |   6 -
 .../scripts/perl/bin/wakeup-latency-report    |   3 -
 tools/perf/scripts/perl/check-perf-trace.pl   | 106 ---
 tools/perf/scripts/perl/failed-syscalls.pl    |  47 -
 tools/perf/scripts/perl/rw-by-file.pl         | 106 ---
 tools/perf/scripts/perl/rw-by-pid.pl          | 184 ----
 tools/perf/scripts/perl/rwtop.pl              | 203 -----
 tools/perf/scripts/perl/wakeup-latency.pl     | 107 ---
 tools/perf/tests/make                         |   2 -
 tools/perf/tests/shell/script_perl.sh         | 102 ---
 tools/perf/ui/browsers/scripts.c              | 161 +---
 tools/perf/util/Build                         |   2 -
 tools/perf/util/scripting-engines/Build       |   5 -
 .../util/scripting-engines/trace-event-perl.c | 770 ----------------
 tools/perf/util/trace-event-parse.c           |  65 --
 tools/perf/util/trace-event-scripting.c       | 398 ---------
 tools/perf/util/trace-event.h                 |  72 +-
 45 files changed, 355 insertions(+), 3507 deletions(-)
 delete mode 100644 tools/build/feature/test-libperl.c
 delete mode 100644 tools/perf/scripts/Build
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Build
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Context.c
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Context.xs
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/README
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/typemap
 delete mode 100644 tools/perf/scripts/perl/bin/check-perf-trace-record
 delete mode 100644 tools/perf/scripts/perl/bin/failed-syscalls-record
 delete mode 100644 tools/perf/scripts/perl/bin/failed-syscalls-report
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-file-record
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-file-report
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-pid-record
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-pid-report
 delete mode 100644 tools/perf/scripts/perl/bin/rwtop-record
 delete mode 100644 tools/perf/scripts/perl/bin/rwtop-report
 delete mode 100644 tools/perf/scripts/perl/bin/wakeup-latency-record
 delete mode 100644 tools/perf/scripts/perl/bin/wakeup-latency-report
 delete mode 100644 tools/perf/scripts/perl/check-perf-trace.pl
 delete mode 100644 tools/perf/scripts/perl/failed-syscalls.pl
 delete mode 100644 tools/perf/scripts/perl/rw-by-file.pl
 delete mode 100644 tools/perf/scripts/perl/rw-by-pid.pl
 delete mode 100644 tools/perf/scripts/perl/rwtop.pl
 delete mode 100644 tools/perf/scripts/perl/wakeup-latency.pl
 delete mode 100755 tools/perf/tests/shell/script_perl.sh
 delete mode 100644 tools/perf/util/scripting-engines/Build
 delete mode 100644 tools/perf/util/scripting-engines/trace-event-perl.c
 delete mode 100644 tools/perf/util/trace-event-scripting.c
tools/build/Makefile.feature +0 -1
index 3ca7a4f5c7fd..dbef5b6c4e43 100644
@@ -121,7 +121,6 @@ FEATURE_TESTS_EXTRA :=                  \
          libbfd-liberty                 \
          libbfd-liberty-z               \
          libopencsd                     \
-         libperl                        \
          llvm                           \
          libbpf                         \
          libpfm4                        \
tools/build/feature/Makefile +0 -20
index 269af8e8f5cf..62f9cbf3dee9 100644
@@ -31,7 +31,6 @@ FILES=                                          \
          test-libdebuginfod.bin                 \
          test-libnuma.bin                       \
          test-numa_num_possible_cpus.bin        \
-         test-libperl.bin                       \
          test-python-module.bin                 \
          test-libslang.bin                      \
          test-libtraceevent.bin                 \
@@ -242,25 +241,6 @@ $(OUTPUT)test-libtracefs.bin:
 $(OUTPUT)test-gtk4.bin:
 	$(BUILD) $(shell $(PKG_CONFIG) --libs --cflags gtk4 2>/dev/null)
 
-grep-libs  = $(filter -l%,$(1))
-strip-libs = $(filter-out -l%,$(1))
-
-PERL_EMBED_LDOPTS = $(shell perl -MExtUtils::Embed -e ldopts 2>/dev/null)
-PERL_EMBED_LDFLAGS = $(call strip-libs,$(PERL_EMBED_LDOPTS))
-PERL_EMBED_LIBADD = $(call grep-libs,$(PERL_EMBED_LDOPTS))
-PERL_EMBED_CCOPTS = $(shell perl -MExtUtils::Embed -e ccopts 2>/dev/null)
-FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)
-
-ifeq ($(CC_NO_CLANG), 0)
-  PERL_EMBED_LDOPTS := $(filter-out -specs=%,$(PERL_EMBED_LDOPTS))
-  PERL_EMBED_CCOPTS := $(filter-out -flto=auto -ffat-lto-objects, $(PERL_EMBED_CCOPTS))
-  PERL_EMBED_CCOPTS := $(filter-out -specs=%,$(PERL_EMBED_CCOPTS))
-  FLAGS_PERL_EMBED += -Wno-compound-token-split-by-macro
-endif
-
-$(OUTPUT)test-libperl.bin:
-	$(BUILD) $(FLAGS_PERL_EMBED)
-
 $(OUTPUT)test-python-module.bin:
 	$(BUILD) $(FLAGS_PYTHON_EMBED)
 
tools/build/feature/test-libperl.c deleted +0 -10
deleted file mode 100644
index 0415f437eb31..000000000000
@@ -1,10 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-#include <EXTERN.h>
-#include <perl.h>
-
-int main(void)
-{
-	perl_alloc();
-
-	return 0;
-}
tools/perf/Build +0 -1
index 5bd0e58d7cb0..8a0b5592a973 100644
@@ -61,7 +61,6 @@ perf-util-y += arch/
 perf-y += arch/
 perf-test-y += arch/
 perf-ui-y += ui/
-perf-util-y += scripts/
 
 gtk-y += ui/gtk/
 
tools/perf/Documentation/perf-check.txt +0 -1
index a80913794eb6..1406a41c5d09 100644
@@ -58,7 +58,6 @@ feature::
                 libLLVM                 /  HAVE_LIBLLVM_SUPPORT
                 libnuma                 /  HAVE_LIBNUMA_SUPPORT
                 libopencsd              /  HAVE_CSTRACE_SUPPORT
-                libperl                 /  HAVE_LIBPERL_SUPPORT
                 libpfm4                 /  HAVE_LIBPFM
                 libslang                /  HAVE_SLANG_SUPPORT
                 libtraceevent           /  HAVE_LIBTRACEEVENT
tools/perf/Makefile.config +0 -1
index 7e05b7943b3b..5c404289780b 100644
@@ -1275,7 +1275,6 @@ $(call detected_var,tipdir_SQ)
 $(call detected_var,srcdir_SQ)
 $(call detected_var,LIBDIR)
 $(call detected_var,GTK_CFLAGS)
-$(call detected_var,PERL_EMBED_CCOPTS)
 $(call detected_var,PYTHON_EMBED_CCOPTS)
 ifneq ($(BISON_FILE_PREFIX_MAP),)
 $(call detected_var,BISON_FILE_PREFIX_MAP)
tools/perf/Makefile.perf +0 -14
index 6bcea56f1f90..57e1a1a04e8a 100644
@@ -17,7 +17,6 @@ include ../scripts/utilities.mak
 #
 # Define CROSS_COMPILE as prefix name of compiler if you want cross-builds.
 #
-# Define LIBPERL to enable perl script extension.
 #
 # Define NO_PYTHON_MODULE to disable python script extension.
 #
@@ -481,11 +480,6 @@ OTHER_PROGRAMS = $(OUTPUT)perf
 ifndef SHELL_PATH
   SHELL_PATH = /bin/sh
 endif
-ifndef PERL_PATH
-  PERL_PATH = /usr/bin/perl
-endif
-
-export PERL_PATH
 
 LIBPERF_BENCH_IN := $(OUTPUT)perf-bench-in.o
 LIBPERF_BENCH := $(OUTPUT)libperf-bench.a
@@ -890,14 +884,6 @@ endif
 		$(INSTALL) $(OUTPUT)perf-archive -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
 	$(call QUIET_INSTALL, perf-iostat) \
 		$(INSTALL) $(OUTPUT)perf-iostat -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
-ifdef LIBPERL
-	$(call QUIET_INSTALL, perl-scripts) \
-		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace'; \
-		$(INSTALL) scripts/perl/Perf-Trace-Util/lib/Perf/Trace/* -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace'; \
-		$(INSTALL) scripts/perl/*.pl -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl'; \
-		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'; \
-		$(INSTALL) scripts/perl/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'
-endif
 
 ifndef NO_PYTHON_MODULE
 	$(call QUIET_INSTALL, python-scripts) \
tools/perf/builtin-check.c +0 -1
index 06711362d37f..1e43e097e7a7 100644
@@ -51,7 +51,6 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("libLLVM", HAVE_LIBLLVM_SUPPORT),
 	FEATURE_STATUS("libnuma", HAVE_LIBNUMA_SUPPORT),
 	FEATURE_STATUS("libopencsd", HAVE_CSTRACE_SUPPORT),
-	FEATURE_STATUS_TIP("libperl", HAVE_LIBPERL_SUPPORT, "Deprecated, use LIBPERL=1 and install perl-ExtUtils-Embed/libperl-dev to build with it"),
 	FEATURE_STATUS("python-module", HAVE_PYTHON_MODULE_SUPPORT),
 	FEATURE_STATUS("libpfm4", HAVE_LIBPFM),
 	FEATURE_STATUS("libslang", HAVE_SLANG_SUPPORT),
tools/perf/builtin-script.c +347 -477

1,113 lines not on this page · raw message

tools/perf/scripts/Build deleted +0 -3
deleted file mode 100644
index fbeab8fff88b..000000000000
@@ -1,3 +0,0 @@
-ifeq ($(CONFIG_LIBTRACEEVENT),y)
-  perf-util-$(CONFIG_LIBPERL)   += perl/Perf-Trace-Util/
-endif
tools/perf/scripts/perl/Perf-Trace-Util/Build deleted +0 -9
deleted file mode 100644
index 01a1a0ed51ae..000000000000
@@ -1,9 +0,0 @@
-perf-util-y += Context.o
-
-CFLAGS_Context.o += $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-bad-function-cast -Wno-declaration-after-statement -Wno-switch-enum
-CFLAGS_Context.o += -Wno-unused-parameter -Wno-nested-externs -Wno-undef
-CFLAGS_Context.o += -Wno-switch-default -Wno-shadow -Wno-thread-safety-analysis
-
-ifeq ($(CC_NO_CLANG), 1)
-  CFLAGS_Context.o += -Wno-unused-command-line-argument
-endif
tools/perf/scripts/perl/Perf-Trace-Util/Context.c deleted +0 -122

122 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/Context.xs deleted +0 -42

42 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL deleted +0 -18
deleted file mode 100644
index e8994332d7dc..000000000000
@@ -1,18 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0
-use 5.010000;
-use ExtUtils::MakeMaker;
-# See lib/ExtUtils/MakeMaker.pm for details of how to influence
-# the contents of the Makefile that is written.
-WriteMakefile(
-    NAME              => 'Perf::Trace::Context',
-    VERSION_FROM      => 'lib/Perf/Trace/Context.pm', # finds $VERSION
-    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
-    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
-      (ABSTRACT_FROM  => 'lib/Perf/Trace/Context.pm', # retrieve abstract from module
-       AUTHOR         => 'Tom Zanussi <[email protected]>') : ()),
-    LIBS              => [''], # e.g., '-lm'
-    DEFINE            => '-I ../..', # e.g., '-DHAVE_SOMETHING'
-    INC               => '-I.', # e.g., '-I. -I/usr/include/other'
-	# Un-comment this if you add C files to link with later:
-    OBJECT            => 'Context.o', # link all the C files too
-);
tools/perf/scripts/perl/Perf-Trace-Util/README deleted +0 -59

59 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm deleted +0 -55

55 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm deleted +0 -192

192 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm deleted +0 -94

94 lines not on this page · raw message

tools/perf/scripts/perl/Perf-Trace-Util/typemap deleted +0 -1
deleted file mode 100644
index 840836804aa7..000000000000
@@ -1 +0,0 @@
-struct scripting_context * T_PTR
tools/perf/scripts/perl/bin/check-perf-trace-record deleted +0 -2
deleted file mode 100644
index 423ad6aed056..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -a -e kmem:kmalloc -e irq:softirq_entry -e kmem:kfree
tools/perf/scripts/perl/bin/failed-syscalls-record deleted +0 -3
deleted file mode 100644
index 74685f318379..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-(perf record -e raw_syscalls:sys_exit $@ || \
- perf record -e syscalls:sys_exit $@) 2> /dev/null
tools/perf/scripts/perl/bin/failed-syscalls-report deleted +0 -10
deleted file mode 100644
index 9f83cc1ad8ba..000000000000
@@ -1,10 +0,0 @@
-#!/bin/bash
-# description: system-wide failed syscalls
-# args: [comm]
-if [ $# -gt 0 ] ; then
-    if ! expr match "$1" "-" > /dev/null ; then
-	comm=$1
-	shift
-    fi
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/perl/failed-syscalls.pl $comm
tools/perf/scripts/perl/bin/rw-by-file-record deleted +0 -3
deleted file mode 100644
index 33efc8673aae..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-perf record -e syscalls:sys_enter_read -e syscalls:sys_enter_write $@
-
tools/perf/scripts/perl/bin/rw-by-file-report deleted +0 -10
deleted file mode 100644
index 77200b3f3100..000000000000
@@ -1,10 +0,0 @@
-#!/bin/bash
-# description: r/w activity for a program, by file
-# args: <comm>
-if [ $# -lt 1 ] ; then
-    echo "usage: rw-by-file <comm>"
-    exit
-fi
-comm=$1
-shift
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/perl/rw-by-file.pl $comm
tools/perf/scripts/perl/bin/rw-by-pid-record deleted +0 -2
deleted file mode 100644
index 7cb9db230448..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e syscalls:sys_enter_read -e syscalls:sys_exit_read -e syscalls:sys_enter_write -e syscalls:sys_exit_write $@
tools/perf/scripts/perl/bin/rw-by-pid-report deleted +0 -3
deleted file mode 100644
index a27b9f311f95..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: system-wide r/w activity
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/perl/rw-by-pid.pl
tools/perf/scripts/perl/bin/rwtop-record deleted +0 -2
deleted file mode 100644
index 7cb9db230448..000000000000
@@ -1,2 +0,0 @@
-#!/bin/bash
-perf record -e syscalls:sys_enter_read -e syscalls:sys_exit_read -e syscalls:sys_enter_write -e syscalls:sys_exit_write $@
tools/perf/scripts/perl/bin/rwtop-report deleted +0 -20
deleted file mode 100644
index 83e11ec2e190..000000000000
@@ -1,20 +0,0 @@
-#!/bin/bash
-# description: system-wide r/w top
-# args: [interval]
-n_args=0
-for i in "$@"
-do
-    if expr match "$i" "-" > /dev/null ; then
-	break
-    fi
-    n_args=$(( $n_args + 1 ))
-done
-if [ "$n_args" -gt 1 ] ; then
-    echo "usage: rwtop-report [interval]"
-    exit
-fi
-if [ "$n_args" -gt 0 ] ; then
-    interval=$1
-    shift
-fi
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/perl/rwtop.pl $interval
tools/perf/scripts/perl/bin/wakeup-latency-record deleted +0 -6
deleted file mode 100644
index 464251a1bd7e..000000000000
@@ -1,6 +0,0 @@
-#!/bin/bash
-perf record -e sched:sched_switch -e sched:sched_wakeup $@
-
-
-
-
tools/perf/scripts/perl/bin/wakeup-latency-report deleted +0 -3
deleted file mode 100644
index 889e8130cca5..000000000000
@@ -1,3 +0,0 @@
-#!/bin/bash
-# description: system-wide min/max/avg wakeup latency
-perf script $@ -s "$PERF_EXEC_PATH"/scripts/perl/wakeup-latency.pl
tools/perf/scripts/perl/check-perf-trace.pl deleted +0 -106

106 lines not on this page · raw message

tools/perf/scripts/perl/failed-syscalls.pl deleted +0 -47

47 lines not on this page · raw message

tools/perf/scripts/perl/rw-by-file.pl deleted +0 -106

106 lines not on this page · raw message

tools/perf/scripts/perl/rw-by-pid.pl deleted +0 -184

184 lines not on this page · raw message

tools/perf/scripts/perl/rwtop.pl deleted +0 -203

203 lines not on this page · raw message

tools/perf/scripts/perl/wakeup-latency.pl deleted +0 -107

107 lines not on this page · raw message

tools/perf/tests/make +0 -2
index b5eaf326573c..8fa2c4d58b8b 100644
@@ -76,7 +76,6 @@ make_no_jevents     := NO_JEVENTS=1
 make_jevents_all    := JEVENTS_ARCH=all
 make_no_bpf_skel    := BUILD_BPF_SKEL=0
 make_gen_vmlinux_h  := GEN_VMLINUX_H=1
-make_libperl        := LIBPERL=1
 make_no_python_module := NO_PYTHON_MODULE=1
 make_no_scripts     := NO_PYTHON_MODULE=1
 make_no_slang       := NO_SLANG=1
@@ -149,7 +148,6 @@ run += make_no_jevents
 run += make_jevents_all
 run += make_no_bpf_skel
 run += make_gen_vmlinux_h
-run += make_libperl
 run += make_no_python_module
 run += make_no_scripts
 run += make_no_slang
tools/perf/tests/shell/script_perl.sh deleted +0 -102

102 lines not on this page · raw message

tools/perf/ui/browsers/scripts.c +7 -154

184 lines not on this page · raw message

tools/perf/util/Build +0 -2
index 3ea888f4210a..f3edc3bbaede 100644
@@ -95,7 +95,6 @@ perf-util-y += tool_pmu.o
 perf-util-y += tp_pmu.o
 perf-util-y += svghelper.o
 perf-util-y += trace-event-info.o
-perf-util-y += trace-event-scripting.o
 perf-util-$(CONFIG_LIBTRACEEVENT) += trace-event.o
 perf-util-$(CONFIG_LIBTRACEEVENT) += trace-event-parse.o
 perf-util-$(CONFIG_LIBTRACEEVENT) += trace-event-read.o
@@ -236,7 +235,6 @@ endif
 
 perf-util-y += data-convert-json.o
 
-perf-util-y += scripting-engines/
 
 perf-util-$(CONFIG_ZLIB) += zlib.o
 perf-util-$(CONFIG_LZMA) += lzma.o
tools/perf/util/scripting-engines/Build deleted +0 -5
deleted file mode 100644
index 3f1dc10526f8..000000000000
@@ -1,5 +0,0 @@
-ifeq ($(CONFIG_LIBTRACEEVENT),y)
-  perf-util-$(CONFIG_LIBPERL)   += trace-event-perl.o
-endif
-
-CFLAGS_trace-event-perl.o += $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow -Wno-nested-externs -Wno-undef -Wno-switch-default -Wno-bad-function-cast -Wno-declaration-after-statement -Wno-switch-enum -Wno-thread-safety-analysis
tools/perf/util/scripting-engines/trace-event-perl.c deleted +0 -770

47 lines not on this page · raw message

tools/perf/util/trace-event-parse.c +0 -65

65 lines not on this page · raw message

tools/perf/util/trace-event-scripting.c deleted +0 -398

398 lines not on this page · raw message

tools/perf/util/trace-event.h +1 -71

72 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog

Part of the diff is not shown here. View the raw message.

author reviewer
#205 lore raw

PATCH v2 49/49 perf Documentation: Update for standalone Python scripts

In reply to Ian Rogers

Update perf-script documentation to reflect standalone Python script execution and the removal of embedded Python and Perl scripting:
- Remove documentation for the removed -g and -s options and legacy record/report script wrapper modes in perf-script.txt.
- Remove references to perf-script-perl and delete obsolete perf-script-perl.txt.
- Rewrite perf-script-python.txt to document writing and running standalone Python scripts using the perf module.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Documentation/db-export.txt        |  16 +-
 tools/perf/Documentation/perf-script-perl.txt | 216 ------
 .../perf/Documentation/perf-script-python.txt | 725 +++---------------
 tools/perf/Documentation/perf-script.txt      |  80 +-
 tools/perf/Documentation/tips.txt             |   1 -
 5 files changed, 113 insertions(+), 925 deletions(-)
 delete mode 100644 tools/perf/Documentation/perf-script-perl.txt
tools/perf/Documentation/db-export.txt +5 -11
index 20024e1f9164..a736be3b6e84 100644
@@ -1,11 +1,11 @@
 Database Export
 ===============
 
-perf tool's python scripting engine:
+perf tool's python module:
 
-	tools/perf/util/scripting-engines/trace-event-python.c
+	tools/perf/util/python.c
 
-supports scripts:
+supports standalone scripts:
 
 	tools/perf/python/export-to-sqlite.py
 	tools/perf/python/export-to-postgresql.py
@@ -31,11 +31,5 @@ backward compatibility by testing for the presence of new tables and columns
 before using them. e.g. function IsSelectable() in exported-sql-viewer.py
 
 4. The export scripts themselves maintain forward compatibility (i.e. an existing
-script will continue to work with new versions of perf) by accepting a variable
-number of arguments (e.g. def call_return_table(*x)) i.e. perf can pass more
-arguments which old scripts will ignore.
-
-5. The scripting engine tests for the existence of script handler functions
-before calling them.  The scripting engine can also test for the support of new
-or optional features by checking for the existence and value of script global
-variables.
+script will continue to work with new versions of perf) by querying event and
+sample attributes dynamically from the perf Python module.
tools/perf/Documentation/perf-script-perl.txt deleted +0 -216

216 lines not on this page · raw message

tools/perf/Documentation/perf-script-python.txt +102 -623

778 lines not on this page · raw message

tools/perf/Documentation/perf-script.txt +6 -74

117 lines not on this page · raw message

tools/perf/Documentation/tips.txt +0 -1
index eb6d56d4d8ba..28953179f59d 100644
@@ -4,7 +4,6 @@ Compare performance results with: perf diff [<old file> <new file>]
 Boolean options have negative forms, e.g.: perf report --no-children
 To not accumulate CPU time of children symbols add --no-children
 Customize output of perf script with: perf script -F event,ip,sym
-Generate a script for your data: perf script -g <lang>
 Save output of perf stat using: perf stat record <target workload>
 Create an archive with symtabs to analyse on other machine: perf archive
 Search options using a keyword: perf report -h <keyword>
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#107 lore raw

Re: [PATCH v2 05/49] perf python: Clean up pylint warnings in tracepoint.py

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

Clean up pylint warnings in tracepoint.py so that it passes pylint without warning suppression comments:
- Catch ImportError instead of bare 'except:' (W0702: bare-except) when importing setproctitle.
- Remove unnecessary trailing semicolon (W0301: unnecessary-semicolon) after evlist.enable().

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#109 lore raw

Re: [PATCH v2 06/49] perf python: Clean up pylint warnings in twatch.py

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Clean up pylint warnings in twatch.py so that it passes pylint without warning suppression comments:
- Convert tab indentation to 4 spaces (W0311: bad-indentation).
- Remove unnecessary trailing semicolon (W0301: unnecessary-semicolon) after evsel.open().
- Convert floating multi-line string comments to '#' comments (W0105:
pointless-string-statement).

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#123 lore raw

Re: [PATCH v2 12/49] perf test: Clean up mypy and pylint issues in shell test libraries

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Clean up mypy type errors and pylint errors/warnings in tools/perf/tests/shell/lib/ (attr.py, perf_json_output_lint.py, and perf_metric_validation.py):
- Fix E0606 (possibly-used-before-assignment) in perf_metric_validation.py by importing sys at module level.
- Add type annotations for stack, second_results, collectlist, get_bounds(),
and main() in perf_metric_validation.py.
- Avoid assigning -1 to list[int] expected_items in perf_json_output_lint.py,
rename shadowing variables, and remove redundant lambdas.
- Remove unnecessary semicolons, use lazy logging arguments, specify exception types and file encodings, and initialize module-level logger in attr.py.

Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#99 lore raw

Re: [PATCH v2 01/49] perf python: Update syscall format string to optional positional

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Update the PyArg_ParseTupleAndKeywords format string for pyrf__syscall_name() and pyrf__syscall_id() from "i|$i" to "i|i" (and "s|$i" to "s|i"), allowing elf_machine to be passed as an optional positional argument as well as a keyword argument.

Update syscall_id() and syscall_name() in perf.pyi to match, and set the return type of syscall_name() to Optional[str] as unknown syscall numbers return None.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#121 lore raw

Re: [PATCH v2 11/49] perf pmu-events: Clean up mypy and pylint issues

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Clean up mypy type errors and pylint warnings across the pmu-events Python scripts (amd_metrics.py, intel_metrics.py, jevents.py, make_legacy_cache.py,
and metric.py) in preparation for making MYPY and PYLINT build tests opt-out.

In addition to type annotations and unused import/variable cleanups, fix three bugs in intel_metrics.py exposed by static analysis:
- Fix references to undefined 'args.model' (instead of '_args.model') inside
try/except blocks in IntelL2() and UncoreMemBw(), which previously threw NameError and silently disabled Skylake/CascadeLake L2 silent eviction adjustments and uncore memory bandwidth JSON loading.
- Define DC_WB_U and DC_WB_D before their first use in IntelL2(), fixing an UnboundLocalError that silently dropped the l2_out_rate, wbn, and isd metrics.
- Fix Event-to-string comparison and addition in IntelFp() by comparing and modifying 'f_assist.name' instead of the Event object 'f_assist'.

Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#105 lore raw

Re: [PATCH v2 04/49] perf python: Clean up pylint warnings in treport.py

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Clean up pylint warnings in treport.py so that it passes pylint without warning suppression comments:
- Replace broad 'except Exception:' clauses (W0718:
broad-exception-caught) with specific exception tuples.
- Pass both pid and sample_tid to session.find_thread() so guest and process threads are resolved accurately.
- Rename parameter 'args' to 'pos_args' in FlameGraph.__init__() to avoid shadowing the outer scope variable 'args' (W0621:
redefined-outer-name).

Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#119 lore raw

Re: [PATCH v2 10/49] perf python: Allow KeyboardInterrupt to propagate in LiveSession

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

LiveSession.run() synchronously blocks while polling evlist for events. When running interactive tools or scripts from the terminal, KeyboardInterrupt exceptions raised via Ctrl-C must propagate so the caller can break out of the polling loop.

Quoted 7 lines

Previously, LiveSession caught KeyboardInterrupt with an empty pass statement, swallowing the exception and preventing calling scripts from detecting the interrupt cleanly. Remove the empty handler so calling scripts receive KeyboardInterrupt and can perform clean teardown.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#101 lore raw

Re: [PATCH v2 02/49] perf python: Update callchain stubs and session thread lookup

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Update pyrf_session__find_thread() in util/python.c to accept an optional tid parameter (defaulting to pid) and return None instead of raising TypeError when a thread is not found in the session machines.

Update perf.pyi to match:
- Set session.find_thread() signature to (pid: int, tid: int = -1) -> Optional[thread].
- Update callchain_node.symbol and callchain_node.dso to str (the C getters return "[unknown]" fallback strings rather than None) and add __iter__() to callchain.

Quoted 5 lines

Also update treport.py to handle Optional[thread] returned by session.find_thread().

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#131 lore raw

Re: [PATCH v2 15/49] perf Makefile: Install standalone Python scripts during transition

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Install the newly ported standalone Python scripts from tools/perf/python/ alongside the legacy scripts during the transition phase so that newly ported scripts are available in the installation directory before legacy support is removed.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#103 lore raw

Re: [PATCH v2 03/49] perf python: Clean up pylint warnings in ilist.py

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Replace bare 'except:' clauses (pylint W0702: bare-except) in ilist.py with specific exception tuples (OSError, ValueError, RuntimeError, TypeError, KeyError) and use a safe '.get("name", "")' sort key in IListApp.compose() so that ilist.py passes pylint without warning suppression comments and handles unnamed PMU events gracefully.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#129 lore raw

Re: [PATCH v2 14/49] perf build: Make pylint build test opt-out (NO_PYLINT=1)

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines

Commit 8a54784e708b ("perf build: Add pylint build tests") added a compile-time pylint check for Python files in tools/perf, but left it opt-in via PYLINT=1.

Now that pylint issues across tools/perf/python, tools/perf/pmu-events, and tools/perf/tests/shell/lib have been cleaned up, enable the pylint check by default when pylint is installed on the system, with an opt-out via NO_PYLINT=1 (matching other perf build feature flags).

Configure PYTHONPATH in Makefile.perf so pylint resolves perf.pyi type stubs and pmu-events modules, enable error and warning categories (--disable=all --enable=E,W --disable=W0123,W0311,W0511,W0603,W0622,W0718) appropriate for perf's standalone CLI and test scripts, and require pylint >= 2.16.0 (skipping with a warning if an older version is installed).

Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#125 lore raw

Re: [PATCH v2 13/49] perf build: Make mypy build test opt-out (NO_MYPY=1)

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Commit 168910d0f937 ("perf build: Add mypy build tests") added mypy as an opt-in compile-time check (MYPY=1) because of existing mypy errors across perf Python scripts.

Quoted 8 lines

Now that all Python scripts in tools/perf have been cleaned up and pass mypy without errors, make the mypy build test opt-out (default on when mypy is installed, disabled with NO_MYPY=1), matching NO_SHELLCHECK=1. Set MYPYPATH to tools/perf/python so local imports resolve during type checking, and clean *.mypy_log files in 'make clean'.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#135 lore raw

Re: [PATCH v2 17/49] perf python: Port mem-phys-addr to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Port mem-phys-addr.py to a standalone script in tools/perf/python/ using the perf.session API to read perf.data files and profile physical memory access types against /proc/iomem.

Improvements compared to the legacy script:
- Parse the full indentation hierarchy of /proc/iomem into a parent-child tree of frozen IomemEntry dataclasses (instead of only top-level indent-0 ranges), resolving physical addresses to the most specific sub-range (such as Kernel code/data/bss inside System RAM) and rolling child counts up into parent totals.
- Support profiling multiple memory events in a single perf.data session (keyed by evsel name) instead of assuming a single global event.
- Add argparse CLI options (-i/--input and --iomem to allow supplying an offline /proc/iomem snapshot from a target system).

Quoted 5 lines

Add a shell test (test_mem_phys_addr_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#115 lore raw

Re: [PATCH v2 09/49] perf python: Add Intel PT call_return and itrace capability

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 23 lines

Add support for Intel PT call_return events and itrace options to the perf Python extension so standalone Python scripts can decode and inspect low-level instruction traces.

Specifically:
- Add a call_return callback parameter and itrace option string to perf.session, treating explicit None (Py_None) callbacks as omitted.
- Expose the perf.call_return object with db_id, parent_id, insn_count, cyc_count, comm, pid, tid, call_time, return_time, branch_count, call_ref, return_ref, flags, and call_path (resolved as a perf.callchain ordered from outermost caller to callee).
- Export perf.CALL_RETURN_NO_CALL, perf.CALL_RETURN_NO_RETURN, and perf.CALL_RETURN_NON_CALL flag constants.
- Expose session.e_machine and session.is_64_bit properties, preferring recorded thread ELF machines (perf_session__e_machine) over perf_env so pipe-mode traces do not prematurely cache host bitness.
- Support optional vmlinux, kallsyms, and symfs symbol configuration parameters (and PERF_SYMBOL_VMLINUX, PERF_SYMBOL_KALLSYMS, PERF_SYMBOL_SYMFS environment fallbacks) in perf.session, resetting symbol_conf pointers and freeing vm_tm_corr_args, cpu_bitmap, and ptime_range in
pyrf_session__delete().

Quoted 1 line

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#139 lore raw

Re: [PATCH v2 19/49] perf python: Port flamegraph to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Port flamegraph.py to a standalone script in tools/perf/python/ that uses the perf module directly, avoiding intermediate dictionary allocations for event fields.

Improvements compared to the legacy script:
- Add Subresource Integrity (integrity="sha256-..." and crossorigin="anonymous") attributes to external CDN stylesheet and script tags in MINIMAL_HTML.
- Upgrade CDN HTML template hash verification from weak MD5 (hashlib.md5) to cryptographic SHA-256 (hashlib.sha256).
- Escape '<', '>', and '&' ('\u003c', '\u003e', '\u0026') in embedded JSON payloads (stacks_json and options_json) to prevent HTML script injection / XSS when rendering untrusted symbol or command names.
- Skip invoking 'perf report --header-only' when the input is stdin ('-'), a FIFO pipe, or a character device (S_ISFIFO / S_ISCHR) so non-seekable streams do not hang or fail.

Quoted 5 lines

Add a shell test (test_flamegraph_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#137 lore raw

Re: [PATCH v2 18/49] perf python: Port stackcollapse to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

Port stackcollapse.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/ refactored into a StackCollapseAnalyzer class.

Improvements compared to the legacy script:
- Traverse sample.callchain directly from perf.session without allocating per-event dictionaries, and fall back to sample.symbol when a sample has no callchain.
- Replace deprecated optparse with argparse, adding -i/--input alongside --include-tid, --include-pid, --no-comm, --tidy-java, and --kernel.
- Handle BrokenPipeError cleanly when output is piped into downstream tools (such as head or flamegraph.pl).

Quoted 5 lines

Add a shell test (test_stackcollapse_python.sh) using a CPU workload (perf test -w noploop) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
#147 lore raw

Re: [PATCH v2 23/49] perf python: Port syscall-counts-by-pid to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Port tools/perf/scripts/python/syscall-counts-by-pid.py to a standalone script in tools/perf/python/ using the perf module. Avoiding the embedded interpreter and per-event dictionary overhead improves execution speed by ~3.8x:

```

$ perf record -e raw_syscalls:sys_enter -a sleep 1
...
$ time perf script tools/perf/scripts/python/syscall-counts-by-pid.py perf
...
real    0m3.852s
user    0m3.512s
sys     0m0.336s
$ time python3 tools/perf/python/syscall-counts-by-pid.py perf
...
[ ... ]
Add a shell test (test_syscall_counts_by_pid_python.sh) to verify the
standalone script.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#151 lore raw

Re: [PATCH v2 25/49] perf python: Port failed-syscalls from Perl to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Replace the legacy Perl script failed-syscalls.pl with a standalone Python script in tools/perf/python/failed-syscalls.py using the perf Python module.

Improvements compared to the legacy Perl script:
- Remove the dependency on libperl and Perf::Trace::Util.
- Support both syscalls:sys_exit_* and raw_syscalls:sys_exit events (the legacy script only handled raw_syscalls::sys_exit).
- Use session.is_64_bit to distinguish 32-bit vs 64-bit unsigned error return ranges (0xfffff000..0xffffffff vs >= 0xfffffffffffff000) so valid 64-bit syscalls returning ~4GB values are not misclassified as 32-bit negative errors.
- Add argparse CLI support (-i/--input and optional comm filter) and full type annotations.

Quoted 5 lines

Add a shell test
(test_failed_syscalls_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
#111 lore raw

Re: [PATCH v2 07/49] perf python: Improve perf script -l descriptions

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Add module docstrings to counting.py, tracepoint.py, and twatch.py so that 'perf script -l' displays concise descriptions for all available standalone scripts:

```
$ perf script -l
List of available scripts:
...
  counting
    Example for counting perf events.
...
  tracepoint
    Example showing how to enable a tracepoint and access its fields.
  twatch
    Example to show how to enable a software event and track
    context switches.
...
```
Quoted 1 line

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#141 lore raw

Re: [PATCH v2 20/49] perf python: Port gecko to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Port gecko.py to a standalone script in tools/perf/python/ that uses the perf module directly to convert perf.data profiles into Firefox Gecko profile format.

Improvements compared to the legacy script:
- Encapsulate profiler state in GeckoCLI and CategoryData classes with full type annotations, removing global variables.
- Harden the local HTTP server in _write_and_launch(): write the temporary profile to an isolated tempfile.TemporaryDirectory() with a randomized UUID filename instead of the current working directory, bind HTTPServer exclusively to 127.0.0.1 on an OS-assigned ephemeral port (0), restrict CORS Access-Control-Allow-Origin to 'https://profiler.firefox.com' instead of '*', and restrict HTTP GET requests exclusively to the randomized profile filename (returning HTTP 403 for directory listings and any other path).
- Add -i/--input and -e/--event CLI options via argparse.

Quoted 4 lines

Add a shell test (test_gecko_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
#113 lore raw

Re: [PATCH v2 08/49] perf python: Expose addr location, transaction, and context_switch

In reply to Ian Rogers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf python: default guest samples resolved against host symbols --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf python: default guest samples resolved against host symbols --

commit 7820cd6d68d2c391ed1e11785faec0799b2925fe
Author: Ian Rogers <[email protected]>

perf python: Expose addr location, transaction, and context_switch

This commit exposes various destination address location components, branch metrics, and other fields directly to the Python extension. It also adds support for fetching sym_offset and populates sample machine_pid and vcpu from the evlist when perf_guest is enabled.

Quoted 4 lines
tools/perf/util/python.c +0 -0
index fbfa71b1c4b6c..49f37198f4b34 100644

[ ... ]

Quoted 9 lines
tools/perf/util/python.c +1 -0
@@ -679,6 +683,7 @@ static int pyrf_sample_event__resolve_al(struct pyrf_event *pevent)
 	struct evsel *evsel = pevent->sample.evsel;
 	struct evlist *evlist = evsel ? evsel->evlist : NULL;
 	struct perf_session *session = evlist ? evlist__session(evlist) : NULL;
+	struct machine *machine;
 
 	if (pevent->al_resolved)
 		return 0;

tools/perf/util/python.c static int pyrf_sample_event__resolve_al(struct pyrf_event *pevent) · line 696

Quoted 6 lines
tools/perf/util/python.c +3 -0
@@ -686,8 +691,14 @@ static int pyrf_sample_event__resolve_al(struct pyrf_event *pevent)
 	if (!session)
 		return -1;
 
+	machine = pevent->sample.machine_pid ?
+		machines__find(&session->machines, pevent->sample.machine_pid) :
+		&session->machines.host;

[Severity: Medium]
Will this ternary operation incorrectly redirect default guest samples to the host machine?

When pevent->sample.machine_pid evaluates to 0, which represents DEFAULT_GUEST_KERNEL_ID, it seems this bypasses necessary cpumode and guest fallbacks. This could cause KVM guest trace events that lack an explicit machine_pid to unconditionally fall back to host symbols instead of the guest machine, yielding incorrect symbol mappings for guest events in Python scripts.

Quoted 9 lines
tools/perf/util/python.c +4 -1
+	if (!machine)
+		machine = &session->machines.host;
+
 	addr_location__init(&pevent->al);
-	if (machine__resolve(&session->machines.host, &pevent->al, &pevent->sample) < 0) {
+	if (machine__resolve(machine, &pevent->al, &pevent->sample) < 0) {
 		addr_location__exit(&pevent->al);
 		return -1;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#143 lore raw

Re: [PATCH v2 21/49] perf python: Port event_analyzing_sample to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

Port event_analyzing_sample.py to a standalone script in tools/perf/python/ using the perf module and standard library sqlite3 module.

Improvements compared to the legacy script:
- Encapsulate database state in a _DB container instead of mutating module-level globals, and ensure temporary SQLite database files are cleaned up on exit.
- Add argparse CLI options (-i/--input and -d/--db) while preserving PerfEvent, PebsEvent, and PebsNHM binary raw_buf unpacking and symbol/DSO histogram reporting.
- Remove Python 2 compatibility code and add type annotations.

Quoted 5 lines

Add a shell test (test_event_analyzing_sample_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
#145 lore raw

Re: [PATCH v2 22/49] perf python: Port syscall-counts to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Port tools/perf/scripts/python/syscall-counts.py to a standalone script in tools/perf/python/ using the perf module. Avoiding the embedded interpreter and per-event dictionary allocation overhead improves execution speed by ~4x:

```

$ perf record -e raw_syscalls:sys_enter -a sleep 1
...
$ time perf script tools/perf/scripts/python/syscall-counts.py perf
...
real    0m3.887s
user    0m3.578s
sys     0m0.308s
$ time python3 tools/perf/python/syscall-counts.py perf
...
[ ... ]
temporarily during the transition to maintain bisectability and are
removed once all scripts are migrated.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
#149 lore raw

Re: [PATCH v2 24/49] perf python: Port failed-syscalls-by-pid to perf module

In reply to Ian Rogers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf python: strerror() bypasses generic errnos for _ARCH_ERRNO_TABLES targets - [Medium] perf…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf python: strerror() bypasses generic errnos for _ARCH_ERRNO_TABLES targets
- [Medium] perf tests: test script silently masks comm/pid filter failures --

commit c03d620ae52c4572d4e7598d33712bda57f18571
Author: Ian Rogers <[email protected]>

perf python: Port failed-syscalls-by-pid to perf module

Port failed-syscalls-by-pid.py to a standalone script in tools/perf/python/ using the perf module.

Quoted 5 lines
tools/perf/python/failed-syscalls-by-pid.py new file +0 -0
new file mode 100755
index 0000000000000..30332062f89a4

[ ... ]

tools/perf/python/failed-syscalls-by-pid.py line 44

Quoted 8 lines
tools/perf/python/failed-syscalls-by-pid.py new file +8 -0
+def strerror(nr: int, e_machine: int = 0) -> str:
+    """Return error string for a given errno, accounting for target e_machine."""
+    err_num = abs(nr)
+    if e_machine in _ARCH_ERRNO_TABLES:
+        if err_num in _ARCH_ERRNO_TABLES[e_machine]:
+            return _ARCH_ERRNO_TABLES[e_machine][err_num]
+    elif e_machine != 0 and err_num in _GENERIC_ERRNO_OVERRIDES:
+        return _GENERIC_ERRNO_OVERRIDES[err_num]

[Severity: Medium]
In strerror(), does this code bypass the generic errno overrides for architectures listed in _ARCH_ERRNO_TABLES?

Because the elif is attached to the outer if, if the architecture is present in the table but the specific errno is not mapped in that arch's dictionary, it will skip the elif entirely. This will cause it to fall back to the host's errno.errorcode instead of trying _GENERIC_ERRNO_OVERRIDES first.

Should this be an independent if statement instead?

Quoted 4 lines
tools/perf/python/failed-syscalls-by-pid.py new file +4 -0
+    try:
+        return errno.errorcode[err_num]
+    except KeyError:
+        return f"Unknown {nr} errno"

[ ... ]

Quoted 5 lines
tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh new file +0 -0
new file mode 100755
index 0000000000000..d5fab922b8a9c

[ ... ]

tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh line 75

Quoted 8 lines
tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh new file +8 -0
+	# Run the script and check output
+	if ! "$PYTHON" "$script_path" -i "${temp_data}" > "${temp_out}"; then
+		echo "failed-syscalls-by-pid test failed."
+		err=1
+	elif ! "$PYTHON" "$script_path" -i "${temp_data}" "ls" >/dev/null; then
+		echo "failed-syscalls-by-pid comm filter test failed."
+		err=1
+	elif ! "$PYTHON" "$script_path" -i "${temp_data}" "$$" >/dev/null; then

[Severity: Medium]
When running test_file_mode(), will redirecting the output to /dev/null and only checking the exit status mask failures in the comm and PID filtering logic?

Because failed-syscalls-by-pid.py will exit with a 0 status even when no events match the filter (it just prints an empty table header), checking the exit code won't detect if the filtering actually matched any events.

Should the output be captured and inspected instead?

Quoted 13 lines
tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh new file +13 -0
+		echo "failed-syscalls-by-pid PID filter test failed."
+		err=1
+	else
+		if ! grep -n -q "err = ENOENT" "${temp_out}"; then
+			echo "Failed to find expected failed syscalls"
+			cat "${temp_out}"
+			err=1
+		else
+			echo "failed-syscalls-by-pid test passed."
+		fi
+	fi
+	rm -f "${temp_out}"
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#161 lore raw

Re: [PATCH v2 30/49] perf python: Port futex-contention to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

Port tools/perf/scripts/python/futex-contention.py to a standalone script in tools/perf/python/ using the perf module. Avoiding the embedded interpreter overhead improves execution speed by ~3.2x:

```

$ perf record -e syscalls:sys_*_futex -a sleep 1
...
$ time perf script tools/perf/scripts/python/futex-contention.py
...
real    0m1.007s
user    0m0.935s
sys     0m0.072s
$ time python3 tools/perf/python/futex-contention.py
...
real    0m0.314s
[ ... ]
Add a shell test (test_futex_contention_python.sh) to verify the
standalone script.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
#165 lore raw

Re: [PATCH v2 32/49] perf python: Port sched-migration and SchedGui to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Port sched-migration.py and SchedGui.py from tools/perf/scripts/python/ to standalone modules in tools/perf/python/:
- Refactor sched-migration.py into a SchedMigrationAnalyzer class using perf.session for event processing and add argparse CLI support (-i/--input, -v/--verbose, --gui, --no-gui).
- Port SchedGui.py to tools/perf/python/ as a local module dependency.
- Load wxPython dynamically via importlib only when GUI mode (--gui) is requested so text-mode analysis, testing on headless systems, and static analysis (mypy/pylint) succeed without wx installed.
- Remove Python 2 compatibility code.

Quoted 5 lines

Add a shell test (test_sched_migration_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32
#157 lore raw

Re: [PATCH v2 28/49] perf python: Port rw-by-pid from Perl to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Replace the legacy Perl script rw-by-pid.pl with a standalone Python script in tools/perf/python/rw-by-pid.py using the perf Python module.

Improvements compared to the legacy Perl script:
- Remove the dependency on libperl and Perf::Trace::Util.
- Convert unsigned 32-bit and 64-bit error return values (0xfffff000..0xffffffff and >= 0x8000000000000000) to negative errnos so failed read/write syscalls are recorded in the error table rather than inflating bytes_read / bytes_written.
- Add argparse CLI support (-i/--input) and full type annotations.

Quoted 5 lines

Add a shell test
(test_rw_by_pid_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=28
#171 lore raw

Re: [PATCH v2 35/49] perf python: Port net_dropmonitor to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Port net_dropmonitor.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/:
- Refactor the script into a DropMonitor class with full type annotations to encapsulate state.
- Use perf.session for skb:kfree_skb event processing and add argparse CLI support (-i/--input and -k/--kallsyms).
- Resolve kernel drop addresses via perf.session symbols/callchains and binary search over /proc/kallsyms, ignoring zeroed kptr_restrict addresses with graceful fallback when kallsyms is unavailable.
- Remove Python 2 compatibility code.

Quoted 5 lines

Add a shell test (test_net_dropmonitor_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=35
#155 lore raw

Re: [PATCH v2 27/49] perf python: Port rw-by-file from Perl to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Replace the legacy Perl script rw-by-file.pl with a standalone Python script in tools/perf/python/rw-by-file.py using the perf Python module.

Improvements compared to the legacy Perl script:
- Remove the dependency on libperl and Perf::Trace::Util.
- Encapsulate per-file-descriptor read/write byte and call count aggregation in an RwByFile class using perf.session and resolve thread command names via session.find_thread(pid, sample_tid).
- Add argparse CLI support (-i/--input and target program filter) and full type annotations.

Quoted 5 lines

Add a shell test
(test_rw_by_file_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
#167 lore raw

Re: [PATCH v2 33/49] perf python: Port wakeup-latency from Perl to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Replace the legacy Perl script wakeup-latency.pl with a standalone Python script in tools/perf/python/wakeup-latency.py using the perf Python module.

Improvements compared to the legacy Perl script:
- Remove the dependency on libperl and Perf::Trace::Util.
- Guard print_totals() when total_wakeups == 0 (printing 'N/A' instead of dividing by zero when a trace contains no matched wakeup/switch pairs).
- Add argparse CLI options (-i/--input) and full type annotations.

Quoted 5 lines

Add a shell test
(test_wakeup_latency_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=33
#159 lore raw

Re: [PATCH v2 29/49] perf python: Port rwtop from Perl to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Replace the legacy Perl script rwtop.pl with a standalone Python script in tools/perf/python/rwtop.py using the perf Python module.

Improvements compared to the legacy Perl script:
- Remove the dependency on libperl and Perf::Trace::Util.
- Support both offline perf.data files (via perf.session) and live recording (via LiveSession with --live), driving periodic interval summaries from event timestamps rather than wall-clock SIGALRM timers so both live and offline runs produce deterministic output.
- Use session.is_64_bit (falling back to host bitness in live mode) when checking for negative syscall return values so 64-bit byte counts in 0xfffff000..0xffffffff are not misclassified as 32-bit errors.
- Sanitize non-printable characters in /proc/<pid>/comm to prevent terminal control sequence injection.

Quoted 5 lines

Add a shell test
(test_rwtop_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=29
#133 lore raw

Re: [PATCH v2 16/49] perf python: Port stat-cpi to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines

Port stat-cpi.py from the legacy embedded scripting framework to a standalone Python script in tools/perf/python/ to calculate Cycles Per Instruction (CPI) per interval per CPU or thread.

Improvements compared to the legacy script:
- Support both perf.data file mode (via perf.session stat callbacks) and live counter collection mode (using perf.parse_events, evlist.open, and evsel.read across intervals), with automatic fallback to user-space (:u) and self-process monitoring when perf_event_paranoid restricts system-wide events (EACCES).
- Compute per-interval counter deltas (val, ena, run) keyed by raw event name so cumulative PERF_RECORD_STAT snapshots and hybrid PMU events (e.g. cpu_core/cycles/, cpu_atom/cycles/) are accumulated accurately, and scale counts by time_enabled / time_running when multiplexed.
- Replace hard-coded CPU ([0, 1]) and thread ([0]) arrays with dynamic CPU and thread discovery so arbitrary system topologies work automatically.
- Add CLI option handling (-i, -I, -p) via argparse and type annotations passing mypy and pylint.

Quoted 5 lines

Add a shell test (test_stat_cpi_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#153 lore raw

Re: [PATCH v2 26/49] perf python: Port sctop to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Port sctop.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/ using an SCTopAnalyzer class structure.

Improvements compared to the legacy script:
- Support both offline perf.data analysis (via perf.session, advancing display intervals deterministically using event timestamps) and live monitoring (via LiveSession with automatic tracepoint fallback from raw_syscalls:sys_enter to syscalls:sys_enter_*).
- Resolve architecture-aware syscall names via perf.syscall_name(id, session.e_machine) without requiring python-audit.
- Replace unsafe signal.SIGALRM dictionary mutation and os.popen("clear") subshell spawning with a synchronized threading.Lock / threading.Event timer and direct ANSI terminal escape sequences ('\x1b[2J\x1b[H').

Quoted 4 lines

Add a shell test (test_sctop_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
#188 lore raw

Re: [PATCH v2 39/49] perf python: Port powerpc-hcalls to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Port powerpc-hcalls.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/:
- Refactor the script into an HCallAnalyzer class with full type annotations to encapsulate per-CPU hypervisor call entry/exit state.
- Use perf.session for event processing to track hypervisor call entry and exit timestamps and aggregate min/max/average duration statistics against HCALL_TABLE.
- Add argparse CLI support (-i/--input) and remove Python 2 compatibility code.

Quoted 5 lines

Add a shell test (test_powerpc_hcalls_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=39
#169 lore raw

Re: [PATCH v2 34/49] perf python: Port compaction-times to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

Port compaction-times.py to a standalone script in tools/perf/python/ using the perf module directly to analyze mm_compaction tracepoints.

Improvements compared to the legacy script:
- Replace Python 2 constructs (such as sys.maxint and raw integer bitmasks) with Python 3 enum.IntEnum (Popt) and enum.IntFlag (Topt) types.
- Access tracepoint fields directly on perf.sample_event and add -i/--input CLI option support via argparse.
- Add full type annotations passing mypy and pylint without suppression comments.

Quoted 5 lines

Add a shell test (test_compaction_times_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=34
#163 lore raw

Re: [PATCH v2 31/49] perf python: Port task-analyzer to perf module

In reply to Ian Rogers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf python: CSV injection mitigation bypass via quoted strings in task-analyzer - [Low] perf…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf python: CSV injection mitigation bypass via quoted strings in task-analyzer
- [Low] perf python: misaligned task-analyzer summary table columns due to width truncation --

commit 062cfbddd4574d86bc24087ab0abc8cd8fc7623b
Author: Ian Rogers <[email protected]>

perf python: Port task-analyzer to perf module

This commit ports the task-analyzer.py script to a standalone Python script in tools/perf/python/ using a class-based architecture. It adds support for offline file analysis and live trace capture, automatically disables ANSI terminal colors for CSV output, and sanitizes task comm strings.

Quoted 5 lines
tools/perf/python/task-analyzer.py new file +0 -0
new file mode 100755
index 0000000000000..a39bdfe32fc15

[ ... ]

tools/perf/python/task-analyzer.py line 223

Quoted 6 lines
tools/perf/python/task-analyzer.py new file +6 -0
+        fmt += "{}{{:>{}}}".format(
+            separator, self.db["runtime_info"]["median"] * fix_csv_align
+        )
+        fmt += "{}{{:>{}}}".format(
+            separator, max(0, self.db["runtime_info"]["min"] - decimal_precision) * fix_csv_align
+        )

[Severity: Low]
Is this column header width calculation correct? The column header width is set to the truncated integer length, but Python's format function will expand it to fit the value and its decimal places. Can this cause the summary table columns to become misaligned when printed?

[ ... ]

tools/perf/python/task-analyzer.py line 331

Quoted 5 lines
tools/perf/python/task-analyzer.py new file +5 -0
+        len_acc = self.db["runtime_info"]["acc"] * fix_csv_align
+        len_mean = self.db["runtime_info"]["mean"] * fix_csv_align
+        len_median = self.db["runtime_info"]["median"] * fix_csv_align
+        len_min = max(0, self.db["runtime_info"]["min"] - decimal_precision) * fix_csv_align
+        len_max = max(0, self.db["runtime_info"]["max"] - decimal_precision) * fix_csv_align

[Severity: Low]
Does this field width calculation break alignment? The row formatting field width is set to the truncated length, but it does not appear to account for the decimal points if the raw value string length is shorter than the required formatted length.

[ ... ]

tools/perf/python/task-analyzer.py line 495

Quoted 11 lines
tools/perf/python/task-analyzer.py new file +11 -0
+    def _filter_non_printable(self, unfiltered: Union[str, bytearray]) -> str:
+        if isinstance(unfiltered, (bytearray, bytes)):
+            unfiltered = unfiltered.decode('utf-8', 'ignore')
+        filtered = ""
+        for char in unfiltered:
+            if char in string.printable and char not in "\r\n\t\x0b\x0c;":
+                filtered += char
+        stripped = filtered.lstrip()
+        if stripped and stripped[0] in "=+-@":
+            filtered = "_" + stripped[1:]
+        return filtered

[Severity: Medium]
Is this CSV formula injection mitigation sufficient? The check only looks at the first character of the string. Can an attacker bypass this sanitization by wrapping the malicious payload in quotes, such as '"=cmd"'?

This would result in ;"=cmd"; in the CSV output, which a spreadsheet program might parse as a quoted string, unquote to =cmd, and evaluate as a formula.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
#192 lore raw

Re: [PATCH v2 41/49] perf test: Migrate Intel PT virtual LBR test to Python API

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Migrate the Intel PT virtual LBR test from generating an inline legacy perf script callback to using a standalone Python script (perf_brstack_max.py) with the brstack iterator API in the perf Python module.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=41
#190 lore raw

Re: [PATCH v2 40/49] perf python: Port intel-pt-events and libxed to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 12 lines

Port intel-pt-events.py and libxed.py from tools/perf/scripts/python/ to standalone modules in tools/perf/python/:
- Refactor intel-pt-events.py into an IntelPTAnalyzer class with full type annotations to encapsulate trace state and stashed output.
- Configure instruction trace decoding directly via perf.session's itrace option and context_switch callback.
- Dynamically select 32-bit vs 64-bit disassembly mode in libxed.py using session.is_64_bit and annotate instructions with source lines via sample.srccode().
- Rename methods in libxed.py to snake_case (instruction, set_mode, disassemble_one) and remove Python 2 compatibility code.

Quoted 5 lines

Add a shell test (test_intel_pt_events_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=40
#175 lore raw

Re: [PATCH v2 37/49] perf python: Port check-perf-trace to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Port check-perf-trace.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Access tracepoint fields directly as attributes on perf.sample_event instead of per-event dictionaries and legacy Perf-Trace-Util helpers.
- Decode symbolic flag and enum masks for irq:softirq_entry and kmem:kmalloc directly in Python and add -i/--input CLI support via argparse.
- Add full type annotations and clean up Python 2 idioms.

Quoted 5 lines

Add a shell test (test_check_perf_trace_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=37
#199 lore raw

Re: [PATCH v2 45/49] perf python: Move and clean up parallel-perf.py

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines

Move parallel-perf.py from tools/perf/scripts/python/ to tools/perf/python/ as it is a standalone Python utility that invokes 'perf script' in parallel across time slices and CPUs. Update tools/perf/tests/shell/script.sh accordingly.

Also fix bugs and clean up the script to pass mypy and pylint without suppression comments:
- Fix Work.command() to return the shlex.quote()-escaped command string (previously sh_cmd was computed with shlex.quote() and discarded in favor of unquoted self.cmd).
- Close self.popen.stdout in the parent process after spawning the consumer subprocess in Work.start() when --pipe-to is used, ensuring the producer receives SIGPIPE if the consumer exits early and avoiding leaking the pipe file descriptor.
- Convert method and function names to snake_case, specify utf-8 file encodings, and add type annotations and docstrings.

Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=45
#206 lore raw

Re: [PATCH v2 49/49] perf Documentation: Update for standalone Python scripts

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Update perf-script documentation to reflect standalone Python script execution and the removal of embedded Python and Perl scripting:
- Remove documentation for the removed -g and -s options and legacy record/report script wrapper modes in perf-script.txt.
- Remove references to perf-script-perl and delete obsolete perf-script-perl.txt.
- Rewrite perf-script-python.txt to document writing and running standalone Python scripts using the perf module.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=49
#194 lore raw

Re: [PATCH v2 42/49] perf python: Port export-to-sqlite to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines

Port export-to-sqlite.py to a standalone script in tools/perf/python/ using the perf module and Python's standard library sqlite3 module.

Improvements compared to the legacy script:
- Remove the dependency on PySide/QtSql by using Python's built-in sqlite3 module in DatabaseExporter, allowing SQLite export on headless and minimal systems without Qt installed.
- Support Intel PT and hardware instruction trace export via perf.session(itrace=...) and perf.call_return callbacks, reconstructing relational call_paths and calls tables and decoding synthesized PT payloads (ptwrite, cbr, mwait, pwre, exstop, pwrx).
- Export context_switches via perf.session's context_switch callback and map thread and comm IDs to their relational database keys.
- Manage temporary staging files inside an isolated tempfile.mkdtemp() directory with guaranteed cleanup in a finally block.

Quoted 5 lines

Update Documentation/db-export.txt and add a shell test (test_export_to_sqlite_python.sh) to verify the standalone exporter.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=42
#173 lore raw

Re: [PATCH v2 36/49] perf python: Port netdev-times to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Port netdev-times.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/:
- Refactor the script into a NetDevTimesAnalyzer class with full type annotations to encapsulate state.
- Collect events via perf.session and sort them in timestamp order before analysis so multi-CPU TX and RX packet timelines are reconstructed deterministically.
- Replace custom argument parsing with argparse (-i/--input, --tx, --rx, --dev, --debug), extract tracepoint fields directly from sample attributes, and remove Python 2 compatibility artifacts.

Quoted 5 lines

Add a shell test (test_netdev_times_python.sh) to verify the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=36
#177 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

Port arm-cs-trace-disasm.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Encapsulate trace disassembly state in a TraceDisasm class
- Automatically search standard kernel debug paths (find_vmlinux()) when -k/--vmlinux is not specified, and query perf.config_get("annotate.objdump") for the default objdump binary.
- Bound DISASM_CACHE memory consumption by evicting the cache at 1024 entries and skipping caching of oversized (> 512 lines) objdump outputs.
- Use sample.srccode() from the perf extension module to annotate disassembly output with source filenames, line numbers, and source lines.

Quoted 5 lines

Update the ARM CoreSight disassembly shell test (test_arm_coresight_disasm.sh) to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=38
#196 lore raw

Re: [PATCH v2 43/49] perf python: Port export-to-postgresql to perf module

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines

Port export-to-postgresql.py to a standalone script in tools/perf/python/ using the perf module and libpq via ctypes.

Improvements compared to the legacy script:
- Remove the dependency on PySide/QtSql for database creation and DDL execution by driving libpq directly via ctypes (PQconnectdb, PQexec, PQputCopyData, PQputCopyEnd) and streaming binary PostgreSQL COPY files in PostgresExporter, enabling export on headless servers without Qt installed.
- Harden database connection and SQL identifier handling by rejecting URI ('://') and connection-parameter ('=') injection strings in connect(), escaping single quotes and backslashes in connection strings, and quoting SQL identifiers (quote_ident).
- Support Intel PT and instruction trace export via perf.session(itrace=...) and perf.call_return callbacks, reconstructing relational call_paths and calls tables (including id=0 placeholder rows and callfk/returnfk foreign keys) and unpacking synthesized PT payloads (ptwrite, cbr, mwait, pwre, exstop, pwrx).
- Use a dedicated context_switch ID counter so context-switch exports do not advance sample database IDs out of sync with call_return references.

Quoted 5 lines

Update Documentation/db-export.txt and add a shell test (test_export_to_postgresql_python.sh) to verify the standalone exporter.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=43
#202 lore raw

Re: [PATCH v2 47/49] perf Makefile: Update Python script installation path

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Replace the libpython feature test with a python-module feature test checking for Python C extension build capability, and update feature test references accordingly.

Remove references to the legacy scripts/python directory and install standalone Python scripts directly under the python directory in libexec. Update the TUI script browser (ui/browsers/scripts.c) to discover standalone scripts from the updated installation path.

Assisted-by Antigravity:gemini-3.1-pro

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=47
#204 lore raw

Re: [PATCH v2 48/49] perf script: Support standalone scripts and remove embedded scripting

In reply to Ian Rogers
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf script: parent process fails to ignore SIGINT during cmd_script() waitpid - [Low] perf…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf script: parent process fails to ignore SIGINT during cmd_script() waitpid
- [Low] perf script: legacy out-of-tree Python scripts silently exit with success --

commit c8ca0ff8a669e06dee2142297270bd19fbc8f680
Author: Ian Rogers <[email protected]>

perf script: Support standalone scripts and remove embedded scripting

Refactor 'perf script' to launch standalone scripts directly via fork() and execvp() and remove the legacy embedded Perl and Python scripting engines.

Quoted 5 lines
tools/perf/builtin-script.c +0 -0
index 0174489d1c0fd..c9e1b5ac5c8f2 100644
@@ -4345,149 +4325,107 @@ int cmd_script(int argc, const char **argv)

[ ... ]

Quoted 6 lines
tools/perf/builtin-script.c +6 -0
+			if (symbol_conf.vmlinux_name)
+				setenv("PERF_SYMBOL_VMLINUX", symbol_conf.vmlinux_name, 1);
+			if (symbol_conf.kallsyms_name)
+				setenv("PERF_SYMBOL_KALLSYMS", symbol_conf.kallsyms_name, 1);
+			if (symbol_conf.symfs && symbol_conf.symfs[0])
+				setenv("PERF_SYMBOL_SYMFS", symbol_conf.symfs, 1);

tools/perf/builtin-script.c int cmd_script(int argc, const char **argv)

Quoted 10 lines
tools/perf/builtin-script.c +2 -7
 
-			execvp("/bin/sh", (char **)__argv);
-			free(__argv);
-			exit(-1);
-		}
-
-		dup2(live_pipe[0], 0);
-		close(live_pipe[1]);
+			if (is_python)
+				execvp("python3", (char **)__argv);

[Severity: Low]
Does this code silently ignore legacy out-of-tree Python scripts?

When a user executes perf script -s legacy_script.py, this directly invokes execvp("python3", ...). Since legacy scripts often only contain callback definitions (e.g., def process_event...) without an active execution block, the Python interpreter will just parse them and exit successfully with a return code of 0, doing nothing.

Will this UX regression cause confusion, making it difficult for users to realize their scripts are no longer supported?

Quoted 13 lines
tools/perf/builtin-script.c +6 -6
+			else
+				execvp(script_name, (char **)__argv);
+			{
+				int err_code = errno;
 
-		__argv = calloc(argc + 4, sizeof(const char *));
-		if (!__argv) {
-			pr_err("malloc failed\n");
-			err = -ENOMEM;
-			goto out;
-		}
+				pr_err("failed to execute script '%s': %s\n",
+				       script_name, strerror(err_code));

tools/perf/builtin-script.c int cmd_script(int argc, const char **argv) · line 4419

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+				_exit(err_code);
+			}
+		} else { /* parent */
+			int status = 0;
+
+			while (waitpid(pid, &status, 0) != pid) {
+				if (errno == EINTR)
+					continue;
+				pr_err("failed to wait for script '%s': %s\n",
+				script_name, strerror(errno));

[Severity: Medium]
Can this wait loop result in interleaved terminal output and a messy state when terminating a script?

If a user runs a standalone script using perf script -s script.py and then presses Ctrl-C to interrupt it, the parent process is inside this waitpid() loop without having configured its own signal handlers to ignore SIGINT or SIGQUIT.

Because perf script does not use the standard POSIX system() semantics (ignoring SIGINT/SIGQUIT in the parent while waiting), the parent process will die immediately via SIG_DFL. It fails to wait for the child script to finish cleaning up or printing its KeyboardInterrupt traceback, causing the shell prompt to return immediately while the child continues writing to the terminal.

Quoted 4 lines
tools/perf/builtin-script.c +1 -1
+				err = -errno;
 				goto out;
 			}
-		}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=48
reviewer
#116 lore raw

Re: [PATCH v2 09/49] perf python: Add Intel PT call_return and itrace capability

In reply to Ian Rogers
Quoted 29 lines · Ian Rogers

Add support for Intel PT call_return events and itrace options to the perf Python extension so standalone Python scripts can decode and inspect low-level instruction traces.

Specifically:
- Add a call_return callback parameter and itrace option string to perf.session, treating explicit None (Py_None) callbacks as omitted.
- Expose the perf.call_return object with db_id, parent_id, insn_count, cyc_count, comm, pid, tid, call_time, return_time, branch_count, call_ref, return_ref, flags, and call_path (resolved as a perf.callchain ordered from outermost caller to callee).
- Export perf.CALL_RETURN_NO_CALL, perf.CALL_RETURN_NO_RETURN, and perf.CALL_RETURN_NON_CALL flag constants.
- Expose session.e_machine and session.is_64_bit properties, preferring recorded thread ELF machines (perf_session__e_machine) over perf_env so pipe-mode traces do not prematurely cache host bitness.
- Support optional vmlinux, kallsyms, and symfs symbol configuration parameters (and PERF_SYMBOL_VMLINUX, PERF_SYMBOL_KALLSYMS, PERF_SYMBOL_SYMFS environment fallbacks) in perf.session, resetting symbol_conf pointers and freeing vm_tm_corr_args, cpu_bitmap, and ptime_range in
pyrf_session__delete().

---
  tools/perf/python/perf.pyi |  35 +++
  tools/perf/util/python.c   | 624 +++++++++++++++++++++++++++++++++++--
  2 files changed, 632 insertions(+), 27 deletions(-)

[...]

tools/perf/util/python.c static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject · line 4591

Quoted 9 lines
tools/perf/util/python.c +4 -1
@@ -4161,23 +4585,40 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
  {
  	struct pyrf_data *pdata;
  	PyObject *sample = NULL, *stat = NULL, *context_switch = NULL;
-	static char * const kwlist[] = { "data", "sample", "stat", "context_switch", NULL };
+	PyObject *call_return = NULL;
+	char *itrace_str = NULL;
+	char *vmlinux_str = NULL, *kallsyms_str = NULL, *symfs_str = NULL;
+	static char * const kwlist[] = { "data", "sample", "stat", "context_switch",

Hi Ian,

This needs to be "static char *kwlist[]" to support older Python versions:

Changed in version 3.13: The keywords parameter has now type char *const* in C and const char *const* in C++, instead of char**

Quoted 4 lines
tools/perf/util/python.c +2 -0
+			      "call_return", "itrace", "vmlinux", "kallsyms",
+			      "symfs", NULL };
  	struct pyrf_session *psession;
  	struct perf_session *session;
reviewer
#126 lore raw

Re: [PATCH v2 13/49] perf build: Make mypy build test opt-out (NO_MYPY=1)

In reply to Ian Rogers
Quoted 4 lines · Ian Rogers

Commit 168910d0f937 ("perf build: Add mypy build tests") added mypy as an opt-in compile-time check (MYPY=1) because of existing mypy errors across perf Python scripts.

Quoted 8 lines

Now that all Python scripts in tools/perf have been cleaned up and pass mypy without errors, make the mypy build test opt-out (default on when mypy is installed, disabled with NO_MYPY=1), matching NO_SHELLCHECK=1. Set MYPYPATH to tools/perf/python so local imports resolve during type checking, and clean *.mypy_log files in 'make clean'.

Assisted-by Antigravity:gemini-3.1-pro

Is there some minimum version or other setup requirement? I get this:

pmu-events/amd_metrics.py:181: error: Unsupported operand types for + ("Event" and "object") [operator] pmu-events/amd_metrics.py:182: error: Unsupported operand types for + ("Event" and "object") [operator] pmu-events/amd_metrics.py:230: error: Argument 1 to "d_ratio" has incompatible type "object"; expected "int | float | Expression" [arg-type] pmu-events/amd_metrics.py:231: error: Unsupported left operand type for + ("object") [operator] pmu-events/amd_metrics.py:236: error: Argument 1 to "d_ratio" has incompatible type "object"; expected "int | float | Expression" [arg-type] pmu-events/amd_metrics.py:236: error: Unsupported left operand type for + ("object") [operator]
Found 6 errors in 1 file (checked 1 source file)
make[3]: *** [pmu-events/Build:192: /home/james/workspace/linux/build/local/pmu-events/amd_metrics.py.mypy_log] Error 1 make[2]: *** [Makefile.perf:562: /home/james/workspace/linux/build/local/pmu-events/pmu-events-in.o] Error 2
make[2]: *** Waiting for unfinished jobs....
python/ilist.py:13: error: Cannot find implementation or library stub for module named "textual" [import-not-found] python/ilist.py:14: error: Cannot find implementation or library stub for module named "textual.app" [import-not-found] python/ilist.py:15: error: Cannot find implementation or library stub for module named "textual.binding" [import-not-found] python/ilist.py:16: error: Cannot find implementation or library stub for module named "textual.containers" [import-not-found] python/ilist.py:17: error: Cannot find implementation or library stub for module named "textual.css.query" [import-not-found] python/ilist.py:17: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
python/ilist.py:18: error: Cannot find implementation or library stub for module named "textual.command" [import-not-found] python/ilist.py:19: error: Cannot find implementation or library stub for module named "textual.screen" [import-not-found] python/ilist.py:20: error: Cannot find implementation or library stub for module named "textual.widgets" [import-not-found] python/ilist.py:21: error: Cannot find implementation or library stub for module named "textual.widgets.tree" [import-not-found] Found 9 errors in 1 file (checked 1 source file)
make[3]: *** [Build:91: /home/james/workspace/linux/build/local/python/ilist.py.mypy_log] Error 1
make[3]: *** Waiting for unfinished jobs....
python/treport.py:12: error: Cannot find implementation or library stub for module named "textual" [import-not-found] python/treport.py:13: error: Cannot find implementation or library stub for module named "textual.app" [import-not-found] python/treport.py:14: error: Cannot find implementation or library stub for module named "textual.binding" [import-not-found] python/treport.py:15: error: Cannot find implementation or library stub for module named "textual.color" [import-not-found] python/treport.py:16: error: Cannot find implementation or library stub for module named "textual.scroll_view" [import-not-found] python/treport.py:17: error: Cannot find implementation or library stub for module named "textual.strip" [import-not-found] python/treport.py:18: error: Cannot find implementation or library stub for module named "textual.widgets" [import-not-found] python/treport.py:19: error: Cannot find implementation or library stub for module named "textual.widgets.tree" [import-not-found] python/treport.py:19: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 8 errors in 1 file (checked 1 source file)

reviewer Reviewed-by on v2 38/49
#178 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

In reply to Ian Rogers
Quoted 451 lines · Ian Rogers

Port arm-cs-trace-disasm.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Encapsulate trace disassembly state in a TraceDisasm class
- Automatically search standard kernel debug paths (find_vmlinux()) when -k/--vmlinux is not specified, and query perf.config_get("annotate.objdump") for the default objdump binary.
- Bound DISASM_CACHE memory consumption by evicting the cache at 1024 entries and skipping caching of oversized (> 512 lines) objdump outputs.
- Use sample.srccode() from the perf extension module to annotate disassembly output with source filenames, line numbers, and source lines.

Update the ARM CoreSight disassembly shell test (test_arm_coresight_disasm.sh) to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
  tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++
  .../coresight/test_arm_coresight_disasm.sh    |  24 +-
  tools/perf/tests/shell/lib/setup_python.sh    |   4 +
  3 files changed, 378 insertions(+), 6 deletions(-)

create mode 100755 tools/perf/python/arm-cs-trace-disasm.py

tools/perf/python/arm-cs-trace-disasm.py new file +356 -0
new file mode 100755
index 000000000000..1a9d9a01d1e4
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember using perf python module
+"""
+from __future__ import annotations
+
+import os
+from os import path
+import re
+from subprocess import CalledProcessError, check_output
+import argparse
+import platform
+import sys
+from typing import Dict, List, Optional
+
+import perf
+
+# Initialize global dicts and regular expression
+DISASM_CACHE: Dict[str, List[str]] = {}
+CPU_DATA: Dict[str, int] = {}
+DISASM_RE = re.compile(r"^\s*([0-9a-fA-F]+):")
+DISASM_FUNC_RE = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
+CACHE_SIZE = 1024
+class _State:
+    sample_idx: int = -1
+    source_file_name: Optional[str] = None
+    line_number: Optional[int] = None
+    dso: Optional[str] = None
+
+_STATE = _State()
+
+KVER = platform.release()
+VMLINUX_PATHS = [
+    f"/usr/lib/debug/boot/vmlinux-{KVER}.debug",
+    f"/usr/lib/debug/lib/modules/{KVER}/vmlinux",
+    f"/lib/modules/{KVER}/build/vmlinux",
+    f"/usr/lib/debug/boot/vmlinux-{KVER}",
+    f"/boot/vmlinux-{KVER}",
+    "/boot/vmlinux",
+    "vmlinux"
+]
+
+def default_objdump() -> str:
+    """Return the default objdump path from perf config or 'objdump'."""
+    try:
+        config = perf.config_get("annotate.objdump")
+        return str(config) if config else "objdump"
+    except (AttributeError, TypeError):
+        return "objdump"
+
+def find_vmlinux() -> Optional[str]:
+    """Find the vmlinux file in standard paths."""
+    if hasattr(find_vmlinux, "path"):
+        return getattr(find_vmlinux, "path")
+
+    for v in VMLINUX_PATHS:
+        if os.access(v, os.R_OK):
+            setattr(find_vmlinux, "path", v)
+            return v
+    setattr(find_vmlinux, "path", None)
+    return None
+
+def get_dso_file_path(dso_name: str, dso_build_id: str, vmlinux: Optional[str]) -> str:
+    """Return the path to the DSO file."""
+    buildid_dir = os.environ.get('PERF_BUILDID_DIR')
+    if not buildid_dir:
+        buildid_dir = os.path.join(os.environ.get('HOME', ''), '.debug')
+
+    if dso_name in ("[kernel.kallsyms]", "vmlinux"):
+        if vmlinux:
+            return vmlinux
+        if dso_build_id:
+            for kname in (dso_name, "vmlinux", "[kernel.kallsyms]"):
+                candidate = os.path.join(buildid_dir, kname, dso_build_id, "elf")
+                if os.access(candidate, os.R_OK):
+                    return candidate
+        return find_vmlinux() or dso_name
+
+    if dso_name == "[vdso]":
+        append = "/vdso"
+    else:
+        append = "/elf"
+
+    dso_path = buildid_dir + "/" + dso_name + "/" + dso_build_id + append
+    # Replace duplicate slash chars to single slash char
+    dso_path = dso_path.replace('//', '/', 1)
+    return dso_path
+
+def read_disam(dso_fname: str, dso_start: int, start_addr: int,
+               stop_addr: int, objdump: str) -> List[str]:
+    """Read disassembly from a DSO file using objdump."""
+    addr_range = f"{start_addr}:{stop_addr}:{dso_start}:{dso_fname}"
+
+    # Don't let the cache get too big, clear it when it hits max size
+    if len(DISASM_CACHE) > CACHE_SIZE:
+        DISASM_CACHE.clear()
+
+    if addr_range in DISASM_CACHE:
+        disasm_output = DISASM_CACHE[addr_range]
+    else:
+        start_addr = start_addr - dso_start
+        stop_addr = stop_addr - dso_start
+        disasm = [objdump, "-d", "-z",
+                  f"--start-address={start_addr:#x}",
+                  f"--stop-address={stop_addr:#x}"]
+        disasm += [dso_fname]
+        try:
+            disasm_output = check_output(disasm).decode('utf-8', errors='replace').split('\n')
+        except (CalledProcessError, OSError):
+            return []
+        if len(disasm_output) <= 512:
+            DISASM_CACHE[addr_range] = disasm_output
+
+    return disasm_output
+
+def print_disam(dso_fname: str, dso_start: int, start_addr: int,
+                stop_addr: int, objdump: str) -> None:
+    """Print disassembly for a given address range."""
+    for line in read_disam(dso_fname, dso_start, start_addr, stop_addr, objdump):
+        m = DISASM_FUNC_RE.search(line)
+        if m is None:
+            m = DISASM_RE.search(line)
+            if m is None:
+                continue
+        print(f"\t{line}")
+
+def print_sample(sample: perf.sample_event) -> None:
+    """Print sample details."""
+    print(f"Sample = {{ cpu: {sample.sample_cpu:04d} addr: {sample.sample_addr:016x} "
+          f"phys_addr: {sample.sample_phys_addr:016x} ip: {sample.sample_ip:016x} "
+          f"pid: {sample.sample_pid} tid: {sample.sample_tid} period: {sample.sample_period} "
+          f"time: {sample.sample_time} index: {_STATE.sample_idx}}}")
+
+def common_start_str(comm: str, sample: perf.sample_event) -> str:
+    """Return common start string for sample output."""
+    sec = int(sample.sample_time / 1000000000)
+    ns = sample.sample_time % 1000000000
+    cpu = sample.sample_cpu
+    pid = sample.sample_pid
+    tid = sample.sample_tid
+    return f"{comm:>16s} {pid:5d}/{tid:<5d} [{cpu:04d}] {sec:9d}.{ns:09d}  "
+
+def print_srccode(comm: str, sample: perf.sample_event, symbol: str, dso: str) -> None:
+    """Print source code and symbols for a sample."""
+    ip = sample.sample_ip
+    if symbol == "[unknown]":
+        start_str = common_start_str(comm, sample) + f"{ip:x}".rjust(16).ljust(40)
+    else:
+        symoff = 0
+        symoff = getattr(sample, 'sym_offset', 0) or 0
+        offs = f"+{symoff:#x}" if symoff != 0 else ""
+        start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
+
+    source_file_name, line_number, source_line = sample.srccode() or (None, 0, None)
+    if source_file_name:
+        if _STATE.line_number == line_number and _STATE.source_file_name == source_file_name:
+            src_str = ""
+        else:
+            if len(source_file_name) > 40:
+                src_file = f"...{source_file_name[-37:]} "
+            else:
+                src_file = source_file_name.ljust(41)
+
+            if source_line is None:
+                src_str = f"{src_file}{line_number:>4d} <source not found>"
+            else:
+                src_str = f"{src_file}{line_number:>4d} {source_line}"
+        _STATE.dso = None
+    elif dso == _STATE.dso:
+        src_str = ""
+    else:
+        src_str = dso
+        _STATE.dso = dso
+
+    _STATE.line_number = line_number
+    _STATE.source_file_name = source_file_name
+
+    print(start_str, src_str)
+
+class TraceDisasm:
+    """Class to handle trace disassembly."""
+    def __init__(self, cli_options: argparse.Namespace):
+        self.options = cli_options
+        self.sample_idx = -1
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process a single perf event."""
+        self.sample_idx += 1
+        _STATE.sample_idx = self.sample_idx
+
+        if self.options.start_time is not None and sample.sample_time < self.options.start_time:
+            return
+        if self.options.stop_time is not None and sample.sample_time > self.options.stop_time:
+            sys.exit(0)
+        if self.options.start_sample is not None and self.sample_idx < self.options.start_sample:
+            return
+        if self.options.stop_sample is not None and self.sample_idx > self.options.stop_sample:
+            sys.exit(0)
+
+        ev_name = str(sample.evsel)
+        if self.options.verbose:
+            print(f"Event type: {ev_name}")
+            print_sample(sample)
+
+        dso = sample.dso or '[unknown]'
+        symbol = sample.symbol or '[unknown]'
+        dso_bid = (sample.dso_bid.decode('utf-8')
+                   if isinstance(sample.dso_bid, bytes)
+                   else str(sample.dso_bid or '[unknown]'))
+        dso_start = sample.map_start
+        dso_end = sample.map_end
+        map_pgoff = sample.map_pgoff or 0
+
+        comm = "[unknown]"
+        try:
+            if self.session:
+                thread_info = self.session.find_thread(sample.sample_tid)
+                if thread_info:
+                    comm = thread_info.comm() or "[unknown]"
+        except (TypeError, AttributeError):
+            pass
+
+        if dso == '[unknown]':
+            return
+
+        if dso_start is None or dso_end is None:
+            print(f"Failed to find valid dso map for dso {dso}")
+            return
+
+        if "instructions" in ev_name:
+            print_srccode(comm, sample, symbol, dso)
+            return
+
+        if "branches" not in ev_name:
+            return
+
+        self._process_branch(sample, comm, symbol, dso, dso_bid, dso_start, dso_end, map_pgoff)
+
+    def _process_branch(self, sample: perf.sample_event, comm: str, symbol: str, dso: str,
+                        dso_bid: str, dso_start: int, dso_end: int, map_pgoff: int) -> None:
+        """Helper to process branch events."""
+        cpu = sample.sample_cpu
+        ip = sample.sample_ip
+        addr = sample.sample_addr
+
+        if CPU_DATA.get(str(cpu) + 'addr') is None:
+            CPU_DATA[str(cpu) + 'addr'] = addr
+            return
+
+        start_addr = CPU_DATA[str(cpu) + 'addr']
+        stop_addr  = ip + 4
+
+        # Record for previous sample packet
+        CPU_DATA[str(cpu) + 'addr'] = addr
+
+        # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet
+        if start_addr == 0:
+            if stop_addr == 4 and self.options.verbose:
+                print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted")
+            return
+
+        if start_addr < dso_start or start_addr > dso_end:
+            print(f"Start address {start_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if stop_addr < dso_start or stop_addr > dso_end:
+            print(f"Stop address {stop_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if self.options.objdump is not None:
+            if dso == "[kernel.kallsyms]":
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            elif dso_start == 0x400000:
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            else:
+                dso_vm_start = dso_start
+                map_pgoff_local = map_pgoff
+
+            dso_fname = get_dso_file_path(dso, dso_bid, self.options.vmlinux)
+            if path.exists(dso_fname):
+                print_disam(dso_fname, dso_vm_start, start_addr + map_pgoff_local,
+                            stop_addr + map_pgoff_local, self.options.objdump)
+            else:
+                print(f"Failed to find dso {dso} for address range [ "
+                      f"{start_addr + map_pgoff_local:#x} .. {stop_addr + map_pgoff_local:#x} ]")
+
+        print_srccode(comm, sample, symbol, dso)
+
+    def run(self) -> None:
+        """Run the trace disassembly session."""
+        input_file = self.options.input or "perf.data"
+        if input_file != "-" and not os.path.exists(input_file):
+            print(f"Error: {input_file} not found.", file=sys.stderr)
+            sys.exit(1)
+
+        print('ARM CoreSight Trace Data Assembler Dump')
+        try:
+            self.session = perf.session(
+                perf.data(input_file),
+                sample=self.process_event,
+                itrace=self.options.itrace
+            )
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+            print(f"Error opening session: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        try:
+            self.session.process_events()
+        finally:
+            self.session = None
+        print('End')
+
+if __name__ == "__main__":
+    def int_arg(v: str) -> int:
+        """Helper for integer command line arguments."""
+        val = int(v)
+        if val < 0:
+            raise argparse.ArgumentTypeError("Argument must be a positive integer")
+        return val
+
+    arg_parser = argparse.ArgumentParser(description="ARM CoreSight Trace Dump With Disassembler")
+    arg_parser.add_argument("-i", "--input", help="input perf.data file")
+    arg_parser.add_argument("-k", "--vmlinux",
+                            help="Set path to vmlinux file. Omit to autodetect")
+    arg_parser.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
+                            help="Show disassembly. Can also be used to change the objdump path")
+    arg_parser.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
+    arg_parser.add_argument("--start-time", type=int_arg,
+                            help="Monotonic clock time of sample to start from.")
+    arg_parser.add_argument("--stop-time", type=int_arg,
+                            help="Monotonic clock time of sample to stop at.")
+    arg_parser.add_argument("--itrace", default="b",
+                            help="Instruction tracing options.")
+    arg_parser.add_argument("--start-sample", type=int_arg,
+                            help="Index of sample to start from.")
+    arg_parser.add_argument("--stop-sample", type=int_arg,
+                            help="Index of sample to stop at.")
+
+    parsed_options = arg_parser.parse_args()
+    if (parsed_options.start_time is not None and parsed_options.stop_time is not None and
+            parsed_options.start_time >= parsed_options.stop_time):
+        print("--start-time must less than --stop-time")
+        sys.exit(2)
+    if (parsed_options.start_sample is not None and parsed_options.stop_sample is not None and
+            parsed_options.start_sample >= parsed_options.stop_sample):
+        print("--start-sample must less than --stop-sample")
+        sys.exit(2)
+
+    td = TraceDisasm(parsed_options)
+    td.run()
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +18 -6
index f3ebad596378..51bcbd78f460 100755
@@ -1,6 +1,6 @@
  #!/bin/bash
-# Check Arm CoreSight disassembly script completes without errors (exclusive)
  # SPDX-License-Identifier: GPL-2.0
+# Check Arm CoreSight disassembly script completes without errors (exclusive)
 
  # The disassembly script reconstructs ranges of instructions and gives these to objdump to
  # decode. objdump doesn't like ranges that go backwards, but these are a good indication
@@ -22,9 +22,21 @@ glb_err=1
 
  perfdata_dir=$(mktemp -d /tmp/__perf_test.perf.data.XXXXX)
  perfdata=${perfdata_dir}/perf.data
+perfdata2=${perfdata_dir}/perf2.data
  file=$(mktemp /tmp/temporary_file.XXXXX)
  # Relative path works whether it's installed or running from repo
-script_path=$(dirname "$0")/../../../scripts/python/arm-cs-trace-disasm.py
+if [ -n "$PERF_EXEC_PATH" ] && [ -e "$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py" ]; then
+	script_path="$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py"
+else
+	script_path=$(dirname "$0")/../../../python/arm-cs-trace-disasm.py
+fi
+
+# shellcheck source=lib/setup_python.sh
+. "$(dirname "$0")"/../lib/setup_python.sh
+$PYTHON -c "import perf" 2>/dev/null || {
+	echo "Skipping test, perf python module not found"
+	exit 2
+}
 
  cleanup_files()
  {
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
  if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
  	echo "Testing kernel disassembly"
  	perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-	perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-		-d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+	$PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+		-k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
  	grep -q -E ${branch_search} ${file}
  	echo "Found kernel branches"
  else
@@ -56,8 +68,8 @@ fi
  ## Test user ##
  echo "Testing userspace disassembly"
  perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-	-d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}
  grep -q -E ${branch_search} ${file}
  echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +0 -0
index 2173215a0517..220d9663f81a 100644

tools/perf/tests/shell/lib/setup_python.sh fi · line 27

Quoted 10 lines
tools/perf/tests/shell/lib/setup_python.sh +4 -0
@@ -18,6 +18,10 @@ fi
  # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
  if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
    PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"
  elif [ -d "$(dirname "$0")/../../python" ]; then
    PYTHONPATH_DIR="$(dirname "$0")/../../python"
  elif [ -d "$(dirname "$0")/../python" ]; then
author reviewer
#127 lore raw

Re: [PATCH v2 13/49] perf build: Make mypy build test opt-out (NO_MYPY=1)

In reply to James Clark
Quoted 72 lines · James Clark
Quoted 4 lines · Ian Rogers

Commit 168910d0f937 ("perf build: Add mypy build tests") added mypy as an opt-in compile-time check (MYPY=1) because of existing mypy errors across perf Python scripts.

Quoted 8 lines

Now that all Python scripts in tools/perf have been cleaned up and pass mypy without errors, make the mypy build test opt-out (default on when mypy is installed, disabled with NO_MYPY=1), matching NO_SHELLCHECK=1. Set MYPYPATH to tools/perf/python so local imports resolve during type checking, and clean *.mypy_log files in 'make clean'.

Assisted-by Antigravity:gemini-3.1-pro

Is there some minimum version or other setup requirement? I get this:

pmu-events/amd_metrics.py:181: error: Unsupported operand types for + ("Event" and "object") [operator] pmu-events/amd_metrics.py:182: error: Unsupported operand types for + ("Event" and "object") [operator] pmu-events/amd_metrics.py:230: error: Argument 1 to "d_ratio" has incompatible type "object"; expected "int | float | Expression" [arg-type] pmu-events/amd_metrics.py:231: error: Unsupported left operand type for
+ ("object") [operator]
pmu-events/amd_metrics.py:236: error: Argument 1 to "d_ratio" has incompatible type "object"; expected "int | float | Expression" [arg-type] pmu-events/amd_metrics.py:236: error: Unsupported left operand type for
+ ("object") [operator]
Found 6 errors in 1 file (checked 1 source file)
make[3]: *** [pmu-events/Build:192:
/home/james/workspace/linux/build/local/pmu-events/amd_metrics.py.mypy_log] Error 1
make[2]: *** [Makefile.perf:562:
/home/james/workspace/linux/build/local/pmu-events/pmu-events-in.o] Error 2 make[2]: *** Waiting for unfinished jobs.... python/ilist.py:13: error: Cannot find implementation or library stub for module named "textual" [import-not-found] python/ilist.py:14: error: Cannot find implementation or library stub for module named "textual.app" [import-not-found] python/ilist.py:15: error: Cannot find implementation or library stub for module named "textual.binding" [import-not-found] python/ilist.py:16: error: Cannot find implementation or library stub for module named "textual.containers" [import-not-found] python/ilist.py:17: error: Cannot find implementation or library stub for module named "textual.css.query" [import-not-found] python/ilist.py:17: note: See
https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
python/ilist.py:18: error: Cannot find implementation or library stub for module named "textual.command" [import-not-found] python/ilist.py:19: error: Cannot find implementation or library stub for module named "textual.screen" [import-not-found] python/ilist.py:20: error: Cannot find implementation or library stub for module named "textual.widgets" [import-not-found] python/ilist.py:21: error: Cannot find implementation or library stub for module named "textual.widgets.tree" [import-not-found] Found 9 errors in 1 file (checked 1 source file)
make[3]: *** [Build:91:
/home/james/workspace/linux/build/local/python/ilist.py.mypy_log] Error 1 make[3]: *** Waiting for unfinished jobs.... python/treport.py:12: error: Cannot find implementation or library stub for module named "textual" [import-not-found] python/treport.py:13: error: Cannot find implementation or library stub for module named "textual.app" [import-not-found] python/treport.py:14: error: Cannot find implementation or library stub for module named "textual.binding" [import-not-found] python/treport.py:15: error: Cannot find implementation or library stub for module named "textual.color" [import-not-found] python/treport.py:16: error: Cannot find implementation or library stub for module named "textual.scroll_view" [import-not-found] python/treport.py:17: error: Cannot find implementation or library stub

Quoted 8 lines

for module named "textual.strip" [import-not-found] python/treport.py:18: error: Cannot find implementation or library stub for module named "textual.widgets" [import-not-found] python/treport.py:19: error: Cannot find implementation or library stub for module named "textual.widgets.tree" [import-not-found] python/treport.py:19: note: See
https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 8 errors in 1 file (checked 1 source file)

Thanks James! So there are 2 issues, and I'll fix the code in v3. The first issue is that in older mypys the join the plus operator isn't computed correctly and this can be fixed with a type annotation. The textual issue is resolvable by running mypy with --ignore-missing-imports.

Thanks,
Ian

author reviewer
#117 lore raw

Re: [PATCH v2 09/49] perf python: Add Intel PT call_return and itrace capability

In reply to James Clark
Quoted 46 lines · James Clark
Quoted 29 lines · Ian Rogers

Add support for Intel PT call_return events and itrace options to the perf Python extension so standalone Python scripts can decode and inspect low-level instruction traces.

Specifically:
- Add a call_return callback parameter and itrace option string to perf.session, treating explicit None (Py_None) callbacks as omitted.
- Expose the perf.call_return object with db_id, parent_id, insn_count, cyc_count, comm, pid, tid, call_time, return_time, branch_count, call_ref, return_ref, flags, and call_path (resolved as a perf.callchain ordered from outermost caller to callee).
- Export perf.CALL_RETURN_NO_CALL, perf.CALL_RETURN_NO_RETURN, and perf.CALL_RETURN_NON_CALL flag constants.
- Expose session.e_machine and session.is_64_bit properties, preferring recorded thread ELF machines (perf_session__e_machine) over perf_env so pipe-mode traces do not prematurely cache host bitness.
- Support optional vmlinux, kallsyms, and symfs symbol configuration parameters (and PERF_SYMBOL_VMLINUX, PERF_SYMBOL_KALLSYMS, PERF_SYMBOL_SYMFS environment fallbacks) in perf.session, resetting symbol_conf pointers and freeing vm_tm_corr_args, cpu_bitmap, and ptime_range in
pyrf_session__delete().

---
  tools/perf/python/perf.pyi |  35 +++
  tools/perf/util/python.c   | 624 +++++++++++++++++++++++++++++++++++--
  2 files changed, 632 insertions(+), 27 deletions(-)

[...]

tools/perf/util/python.c static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject · line 4591

Quoted 9 lines
tools/perf/util/python.c +4 -1
@@ -4161,23 +4585,40 @@ static PyObject *pyrf_session__new(PyTypeObject *type, PyObject *args, PyObject
  {
      struct pyrf_data *pdata;
      PyObject *sample = NULL, *stat = NULL, *context_switch = NULL;
-     static char * const kwlist[] = { "data", "sample", "stat", "context_switch", NULL };
+     PyObject *call_return = NULL;
+     char *itrace_str = NULL;
+     char *vmlinux_str = NULL, *kallsyms_str = NULL, *symfs_str = NULL;
+     static char * const kwlist[] = { "data", "sample", "stat", "context_switch",

Hi Ian,

Quoted 4 lines

This needs to be "static char *kwlist[]" to support older Python versions:

Changed in version 3.13: The keywords parameter has now type char *const* in C and const char *const* in C++, instead of char**

Thanks! Fixing in v3.

Ian

Quoted 4 lines
Quoted 4 lines
tools/perf/util/python.c +2 -0
+                           "call_return", "itrace", "vmlinux", "kallsyms",
+                           "symfs", NULL };
      struct pyrf_session *psession;
      struct perf_session *session;
author reviewer
#179 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

In reply to James Clark
Quoted 459 lines · James Clark
Quoted 455 lines · Ian Rogers

Port arm-cs-trace-disasm.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Encapsulate trace disassembly state in a TraceDisasm class
- Automatically search standard kernel debug paths (find_vmlinux()) when -k/--vmlinux is not specified, and query perf.config_get("annotate.objdump") for the default objdump binary.
- Bound DISASM_CACHE memory consumption by evicting the cache at 1024 entries and skipping caching of oversized (> 512 lines) objdump outputs.
- Use sample.srccode() from the perf extension module to annotate disassembly output with source filenames, line numbers, and source lines.

Update the ARM CoreSight disassembly shell test (test_arm_coresight_disasm.sh) to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
  tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++
  .../coresight/test_arm_coresight_disasm.sh    |  24 +-
  tools/perf/tests/shell/lib/setup_python.sh    |   4 +
  3 files changed, 378 insertions(+), 6 deletions(-)

create mode 100755 tools/perf/python/arm-cs-trace-disasm.py

tools/perf/python/arm-cs-trace-disasm.py new file +356 -0
new file mode 100755
index 000000000000..1a9d9a01d1e4
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember using perf python module
+"""
+from __future__ import annotations
+
+import os
+from os import path
+import re
+from subprocess import CalledProcessError, check_output
+import argparse
+import platform
+import sys
+from typing import Dict, List, Optional
+
+import perf
+
+# Initialize global dicts and regular expression
+DISASM_CACHE: Dict[str, List[str]] = {}
+CPU_DATA: Dict[str, int] = {}
+DISASM_RE = re.compile(r"^\s*([0-9a-fA-F]+):")
+DISASM_FUNC_RE = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
+CACHE_SIZE = 1024
+class _State:
+    sample_idx: int = -1
+    source_file_name: Optional[str] = None
+    line_number: Optional[int] = None
+    dso: Optional[str] = None
+
+_STATE = _State()
+
+KVER = platform.release()
+VMLINUX_PATHS = [
+    f"/usr/lib/debug/boot/vmlinux-{KVER}.debug",
+    f"/usr/lib/debug/lib/modules/{KVER}/vmlinux",
+    f"/lib/modules/{KVER}/build/vmlinux",
+    f"/usr/lib/debug/boot/vmlinux-{KVER}",
+    f"/boot/vmlinux-{KVER}",
+    "/boot/vmlinux",
+    "vmlinux"
+]
+
+def default_objdump() -> str:
+    """Return the default objdump path from perf config or 'objdump'."""
+    try:
+        config = perf.config_get("annotate.objdump")
+        return str(config) if config else "objdump"
+    except (AttributeError, TypeError):
+        return "objdump"
+
+def find_vmlinux() -> Optional[str]:
+    """Find the vmlinux file in standard paths."""
+    if hasattr(find_vmlinux, "path"):
+        return getattr(find_vmlinux, "path")
+
+    for v in VMLINUX_PATHS:
+        if os.access(v, os.R_OK):
+            setattr(find_vmlinux, "path", v)
+            return v
+    setattr(find_vmlinux, "path", None)
+    return None
+
+def get_dso_file_path(dso_name: str, dso_build_id: str, vmlinux: Optional[str]) -> str:
+    """Return the path to the DSO file."""
+    buildid_dir = os.environ.get('PERF_BUILDID_DIR')
+    if not buildid_dir:
+        buildid_dir = os.path.join(os.environ.get('HOME', ''), '.debug')
+
+    if dso_name in ("[kernel.kallsyms]", "vmlinux"):
+        if vmlinux:
+            return vmlinux
+        if dso_build_id:
+            for kname in (dso_name, "vmlinux", "[kernel.kallsyms]"):
+                candidate = os.path.join(buildid_dir, kname, dso_build_id, "elf")
+                if os.access(candidate, os.R_OK):
+                    return candidate
+        return find_vmlinux() or dso_name
+
+    if dso_name == "[vdso]":
+        append = "/vdso"
+    else:
+        append = "/elf"
+
+    dso_path = buildid_dir + "/" + dso_name + "/" + dso_build_id + append
+    # Replace duplicate slash chars to single slash char
+    dso_path = dso_path.replace('//', '/', 1)
+    return dso_path
+
+def read_disam(dso_fname: str, dso_start: int, start_addr: int,
+               stop_addr: int, objdump: str) -> List[str]:
+    """Read disassembly from a DSO file using objdump."""
+    addr_range = f"{start_addr}:{stop_addr}:{dso_start}:{dso_fname}"
+
+    # Don't let the cache get too big, clear it when it hits max size
+    if len(DISASM_CACHE) > CACHE_SIZE:
+        DISASM_CACHE.clear()
+
+    if addr_range in DISASM_CACHE:
+        disasm_output = DISASM_CACHE[addr_range]
+    else:
+        start_addr = start_addr - dso_start
+        stop_addr = stop_addr - dso_start
+        disasm = [objdump, "-d", "-z",
+                  f"--start-address={start_addr:#x}",
+                  f"--stop-address={stop_addr:#x}"]
+        disasm += [dso_fname]
+        try:
+            disasm_output = check_output(disasm).decode('utf-8', errors='replace').split('\n')
+        except (CalledProcessError, OSError):
+            return []
+        if len(disasm_output) <= 512:
+            DISASM_CACHE[addr_range] = disasm_output
+
+    return disasm_output
+
+def print_disam(dso_fname: str, dso_start: int, start_addr: int,
+                stop_addr: int, objdump: str) -> None:
+    """Print disassembly for a given address range."""
+    for line in read_disam(dso_fname, dso_start, start_addr, stop_addr, objdump):
+        m = DISASM_FUNC_RE.search(line)
+        if m is None:
+            m = DISASM_RE.search(line)
+            if m is None:
+                continue
+        print(f"\t{line}")
+
+def print_sample(sample: perf.sample_event) -> None:
+    """Print sample details."""
+    print(f"Sample = {{ cpu: {sample.sample_cpu:04d} addr: {sample.sample_addr:016x} "
+          f"phys_addr: {sample.sample_phys_addr:016x} ip: {sample.sample_ip:016x} "
+          f"pid: {sample.sample_pid} tid: {sample.sample_tid} period: {sample.sample_period} "
+          f"time: {sample.sample_time} index: {_STATE.sample_idx}}}")
+
+def common_start_str(comm: str, sample: perf.sample_event) -> str:
+    """Return common start string for sample output."""
+    sec = int(sample.sample_time / 1000000000)
+    ns = sample.sample_time % 1000000000
+    cpu = sample.sample_cpu
+    pid = sample.sample_pid
+    tid = sample.sample_tid
+    return f"{comm:>16s} {pid:5d}/{tid:<5d} [{cpu:04d}] {sec:9d}.{ns:09d}  "
+
+def print_srccode(comm: str, sample: perf.sample_event, symbol: str, dso: str) -> None:
+    """Print source code and symbols for a sample."""
+    ip = sample.sample_ip
+    if symbol == "[unknown]":
+        start_str = common_start_str(comm, sample) + f"{ip:x}".rjust(16).ljust(40)
+    else:
+        symoff = 0
+        symoff = getattr(sample, 'sym_offset', 0) or 0
+        offs = f"+{symoff:#x}" if symoff != 0 else ""
+        start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
+
+    source_file_name, line_number, source_line = sample.srccode() or (None, 0, None)
+    if source_file_name:
+        if _STATE.line_number == line_number and _STATE.source_file_name == source_file_name:
+            src_str = ""
+        else:
+            if len(source_file_name) > 40:
+                src_file = f"...{source_file_name[-37:]} "
+            else:
+                src_file = source_file_name.ljust(41)
+
+            if source_line is None:
+                src_str = f"{src_file}{line_number:>4d} <source not found>"
+            else:
+                src_str = f"{src_file}{line_number:>4d} {source_line}"
+        _STATE.dso = None
+    elif dso == _STATE.dso:
+        src_str = ""
+    else:
+        src_str = dso
+        _STATE.dso = dso
+
+    _STATE.line_number = line_number
+    _STATE.source_file_name = source_file_name
+
+    print(start_str, src_str)
+
+class TraceDisasm:
+    """Class to handle trace disassembly."""
+    def __init__(self, cli_options: argparse.Namespace):
+        self.options = cli_options
+        self.sample_idx = -1
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process a single perf event."""
+        self.sample_idx += 1
+        _STATE.sample_idx = self.sample_idx
+
+        if self.options.start_time is not None and sample.sample_time < self.options.start_time:
+            return
+        if self.options.stop_time is not None and sample.sample_time > self.options.stop_time:
+            sys.exit(0)
+        if self.options.start_sample is not None and self.sample_idx < self.options.start_sample:
+            return
+        if self.options.stop_sample is not None and self.sample_idx > self.options.stop_sample:
+            sys.exit(0)
+
+        ev_name = str(sample.evsel)
+        if self.options.verbose:
+            print(f"Event type: {ev_name}")
+            print_sample(sample)
+
+        dso = sample.dso or '[unknown]'
+        symbol = sample.symbol or '[unknown]'
+        dso_bid = (sample.dso_bid.decode('utf-8')
+                   if isinstance(sample.dso_bid, bytes)
+                   else str(sample.dso_bid or '[unknown]'))
+        dso_start = sample.map_start
+        dso_end = sample.map_end
+        map_pgoff = sample.map_pgoff or 0
+
+        comm = "[unknown]"
+        try:
+            if self.session:
+                thread_info = self.session.find_thread(sample.sample_tid)
+                if thread_info:
+                    comm = thread_info.comm() or "[unknown]"
+        except (TypeError, AttributeError):
+            pass
+
+        if dso == '[unknown]':
+            return
+
+        if dso_start is None or dso_end is None:
+            print(f"Failed to find valid dso map for dso {dso}")
+            return
+
+        if "instructions" in ev_name:
+            print_srccode(comm, sample, symbol, dso)
+            return
+
+        if "branches" not in ev_name:
+            return
+
+        self._process_branch(sample, comm, symbol, dso, dso_bid, dso_start, dso_end, map_pgoff)
+
+    def _process_branch(self, sample: perf.sample_event, comm: str, symbol: str, dso: str,
+                        dso_bid: str, dso_start: int, dso_end: int, map_pgoff: int) -> None:
+        """Helper to process branch events."""
+        cpu = sample.sample_cpu
+        ip = sample.sample_ip
+        addr = sample.sample_addr
+
+        if CPU_DATA.get(str(cpu) + 'addr') is None:
+            CPU_DATA[str(cpu) + 'addr'] = addr
+            return
+
+        start_addr = CPU_DATA[str(cpu) + 'addr']
+        stop_addr  = ip + 4
+
+        # Record for previous sample packet
+        CPU_DATA[str(cpu) + 'addr'] = addr
+
+        # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet
+        if start_addr == 0:
+            if stop_addr == 4 and self.options.verbose:
+                print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted")
+            return
+
+        if start_addr < dso_start or start_addr > dso_end:
+            print(f"Start address {start_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if stop_addr < dso_start or stop_addr > dso_end:
+            print(f"Stop address {stop_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if self.options.objdump is not None:
+            if dso == "[kernel.kallsyms]":
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            elif dso_start == 0x400000:
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            else:
+                dso_vm_start = dso_start
+                map_pgoff_local = map_pgoff
+
+            dso_fname = get_dso_file_path(dso, dso_bid, self.options.vmlinux)
+            if path.exists(dso_fname):
+                print_disam(dso_fname, dso_vm_start, start_addr + map_pgoff_local,
+                            stop_addr + map_pgoff_local, self.options.objdump)
+            else:
+                print(f"Failed to find dso {dso} for address range [ "
+                      f"{start_addr + map_pgoff_local:#x} .. {stop_addr + map_pgoff_local:#x} ]")
+
+        print_srccode(comm, sample, symbol, dso)
+
+    def run(self) -> None:
+        """Run the trace disassembly session."""
+        input_file = self.options.input or "perf.data"
+        if input_file != "-" and not os.path.exists(input_file):
+            print(f"Error: {input_file} not found.", file=sys.stderr)
+            sys.exit(1)
+
+        print('ARM CoreSight Trace Data Assembler Dump')
+        try:
+            self.session = perf.session(
+                perf.data(input_file),
+                sample=self.process_event,
+                itrace=self.options.itrace
+            )
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+            print(f"Error opening session: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        try:
+            self.session.process_events()
+        finally:
+            self.session = None
+        print('End')
+
+if __name__ == "__main__":
+    def int_arg(v: str) -> int:
+        """Helper for integer command line arguments."""
+        val = int(v)
+        if val < 0:
+            raise argparse.ArgumentTypeError("Argument must be a positive integer")
+        return val
+
+    arg_parser = argparse.ArgumentParser(description="ARM CoreSight Trace Dump With Disassembler")
+    arg_parser.add_argument("-i", "--input", help="input perf.data file")
+    arg_parser.add_argument("-k", "--vmlinux",
+                            help="Set path to vmlinux file. Omit to autodetect")
+    arg_parser.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
+                            help="Show disassembly. Can also be used to change the objdump path")
+    arg_parser.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
+    arg_parser.add_argument("--start-time", type=int_arg,
+                            help="Monotonic clock time of sample to start from.")
+    arg_parser.add_argument("--stop-time", type=int_arg,
+                            help="Monotonic clock time of sample to stop at.")
+    arg_parser.add_argument("--itrace", default="b",
+                            help="Instruction tracing options.")
+    arg_parser.add_argument("--start-sample", type=int_arg,
+                            help="Index of sample to start from.")
+    arg_parser.add_argument("--stop-sample", type=int_arg,
+                            help="Index of sample to stop at.")
+
+    parsed_options = arg_parser.parse_args()
+    if (parsed_options.start_time is not None and parsed_options.stop_time is not None and
+            parsed_options.start_time >= parsed_options.stop_time):
+        print("--start-time must less than --stop-time")
+        sys.exit(2)
+    if (parsed_options.start_sample is not None and parsed_options.stop_sample is not None and
+            parsed_options.start_sample >= parsed_options.stop_sample):
+        print("--start-sample must less than --stop-sample")
+        sys.exit(2)
+
+    td = TraceDisasm(parsed_options)
+    td.run()
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +18 -6
index f3ebad596378..51bcbd78f460 100755
@@ -1,6 +1,6 @@
  #!/bin/bash
-# Check Arm CoreSight disassembly script completes without errors (exclusive)
  # SPDX-License-Identifier: GPL-2.0
+# Check Arm CoreSight disassembly script completes without errors (exclusive)
 
  # The disassembly script reconstructs ranges of instructions and gives these to objdump to
  # decode. objdump doesn't like ranges that go backwards, but these are a good indication
@@ -22,9 +22,21 @@ glb_err=1
 
  perfdata_dir=$(mktemp -d /tmp/__perf_test.perf.data.XXXXX)
  perfdata=${perfdata_dir}/perf.data
+perfdata2=${perfdata_dir}/perf2.data
  file=$(mktemp /tmp/temporary_file.XXXXX)
  # Relative path works whether it's installed or running from repo
-script_path=$(dirname "$0")/../../../scripts/python/arm-cs-trace-disasm.py
+if [ -n "$PERF_EXEC_PATH" ] && [ -e "$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py" ]; then
+     script_path="$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py"
+else
+     script_path=$(dirname "$0")/../../../python/arm-cs-trace-disasm.py
+fi
+
+# shellcheck source=lib/setup_python.sh
+. "$(dirname "$0")"/../lib/setup_python.sh
+$PYTHON -c "import perf" 2>/dev/null || {
+     echo "Skipping test, perf python module not found"
+     exit 2
+}
 
  cleanup_files()
  {
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
  if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
      echo "Testing kernel disassembly"
      perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-     perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-             -d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+     $PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+             -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
      grep -q -E ${branch_search} ${file}
      echo "Found kernel branches"
  else
@@ -56,8 +68,8 @@ fi
  ## Test user ##
  echo "Testing userspace disassembly"
  perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-     -d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}
  grep -q -E ${branch_search} ${file}
  echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +1 -0
index 2173215a0517..220d9663f81a 100644
@@ -18,6 +18,10 @@ fi
  # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
  if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
    PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
Quoted 8 lines

tools/perf/tests/shell/lib/setup_python.sh fi · line 27

Quoted 6 lines
tools/perf/tests/shell/lib/setup_python.sh +3 -0
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"
  elif [ -d "$(dirname "$0")/../../python" ]; then
    PYTHONPATH_DIR="$(dirname "$0")/../../python"
  elif [ -d "$(dirname "$0")/../python" ]; then

Thanks! v3 has some Sashiko related nits cleaned up in this code, but I'll keep the reviewed-by tag. I hope that's okay.

Thanks,
Ian

reviewer
#180 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

Quoted 469 lines · Ian Rogers
Quoted 459 lines · James Clark
Quoted 455 lines · Ian Rogers

Port arm-cs-trace-disasm.py to a standalone script in tools/perf/python/ using the perf module directly.

Improvements compared to the legacy script:
- Encapsulate trace disassembly state in a TraceDisasm class
- Automatically search standard kernel debug paths (find_vmlinux()) when -k/--vmlinux is not specified, and query perf.config_get("annotate.objdump") for the default objdump binary.
- Bound DISASM_CACHE memory consumption by evicting the cache at 1024 entries and skipping caching of oversized (> 512 lines) objdump outputs.
- Use sample.srccode() from the perf extension module to annotate disassembly output with source filenames, line numbers, and source lines.

Update the ARM CoreSight disassembly shell test (test_arm_coresight_disasm.sh) to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
   tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++
   .../coresight/test_arm_coresight_disasm.sh    |  24 +-
   tools/perf/tests/shell/lib/setup_python.sh    |   4 +
   3 files changed, 378 insertions(+), 6 deletions(-)

create mode 100755 tools/perf/python/arm-cs-trace-disasm.py

tools/perf/python/arm-cs-trace-disasm.py new file +356 -0
new file mode 100755
index 000000000000..1a9d9a01d1e4
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+arm-cs-trace-disasm.py: ARM CoreSight Trace Dump With Disassember using perf python module
+"""
+from __future__ import annotations
+
+import os
+from os import path
+import re
+from subprocess import CalledProcessError, check_output
+import argparse
+import platform
+import sys
+from typing import Dict, List, Optional
+
+import perf
+
+# Initialize global dicts and regular expression
+DISASM_CACHE: Dict[str, List[str]] = {}
+CPU_DATA: Dict[str, int] = {}
+DISASM_RE = re.compile(r"^\s*([0-9a-fA-F]+):")
+DISASM_FUNC_RE = re.compile(r"^\s*([0-9a-fA-F]+)\s.*:")
+CACHE_SIZE = 1024
+class _State:
+    sample_idx: int = -1
+    source_file_name: Optional[str] = None
+    line_number: Optional[int] = None
+    dso: Optional[str] = None
+
+_STATE = _State()
+
+KVER = platform.release()
+VMLINUX_PATHS = [
+    f"/usr/lib/debug/boot/vmlinux-{KVER}.debug",
+    f"/usr/lib/debug/lib/modules/{KVER}/vmlinux",
+    f"/lib/modules/{KVER}/build/vmlinux",
+    f"/usr/lib/debug/boot/vmlinux-{KVER}",
+    f"/boot/vmlinux-{KVER}",
+    "/boot/vmlinux",
+    "vmlinux"
+]
+
+def default_objdump() -> str:
+    """Return the default objdump path from perf config or 'objdump'."""
+    try:
+        config = perf.config_get("annotate.objdump")
+        return str(config) if config else "objdump"
+    except (AttributeError, TypeError):
+        return "objdump"
+
+def find_vmlinux() -> Optional[str]:
+    """Find the vmlinux file in standard paths."""
+    if hasattr(find_vmlinux, "path"):
+        return getattr(find_vmlinux, "path")
+
+    for v in VMLINUX_PATHS:
+        if os.access(v, os.R_OK):
+            setattr(find_vmlinux, "path", v)
+            return v
+    setattr(find_vmlinux, "path", None)
+    return None
+
+def get_dso_file_path(dso_name: str, dso_build_id: str, vmlinux: Optional[str]) -> str:
+    """Return the path to the DSO file."""
+    buildid_dir = os.environ.get('PERF_BUILDID_DIR')
+    if not buildid_dir:
+        buildid_dir = os.path.join(os.environ.get('HOME', ''), '.debug')
+
+    if dso_name in ("[kernel.kallsyms]", "vmlinux"):
+        if vmlinux:
+            return vmlinux
+        if dso_build_id:
+            for kname in (dso_name, "vmlinux", "[kernel.kallsyms]"):
+                candidate = os.path.join(buildid_dir, kname, dso_build_id, "elf")
+                if os.access(candidate, os.R_OK):
+                    return candidate
+        return find_vmlinux() or dso_name
+
+    if dso_name == "[vdso]":
+        append = "/vdso"
+    else:
+        append = "/elf"
+
+    dso_path = buildid_dir + "/" + dso_name + "/" + dso_build_id + append
+    # Replace duplicate slash chars to single slash char
+    dso_path = dso_path.replace('//', '/', 1)
+    return dso_path
+
+def read_disam(dso_fname: str, dso_start: int, start_addr: int,
+               stop_addr: int, objdump: str) -> List[str]:
+    """Read disassembly from a DSO file using objdump."""
+    addr_range = f"{start_addr}:{stop_addr}:{dso_start}:{dso_fname}"
+
+    # Don't let the cache get too big, clear it when it hits max size
+    if len(DISASM_CACHE) > CACHE_SIZE:
+        DISASM_CACHE.clear()
+
+    if addr_range in DISASM_CACHE:
+        disasm_output = DISASM_CACHE[addr_range]
+    else:
+        start_addr = start_addr - dso_start
+        stop_addr = stop_addr - dso_start
+        disasm = [objdump, "-d", "-z",
+                  f"--start-address={start_addr:#x}",
+                  f"--stop-address={stop_addr:#x}"]
+        disasm += [dso_fname]
+        try:
+            disasm_output = check_output(disasm).decode('utf-8', errors='replace').split('\n')
+        except (CalledProcessError, OSError):
+            return []
+        if len(disasm_output) <= 512:
+            DISASM_CACHE[addr_range] = disasm_output
+
+    return disasm_output
+
+def print_disam(dso_fname: str, dso_start: int, start_addr: int,
+                stop_addr: int, objdump: str) -> None:
+    """Print disassembly for a given address range."""
+    for line in read_disam(dso_fname, dso_start, start_addr, stop_addr, objdump):
+        m = DISASM_FUNC_RE.search(line)
+        if m is None:
+            m = DISASM_RE.search(line)
+            if m is None:
+                continue
+        print(f"\t{line}")
+
+def print_sample(sample: perf.sample_event) -> None:
+    """Print sample details."""
+    print(f"Sample = {{ cpu: {sample.sample_cpu:04d} addr: {sample.sample_addr:016x} "
+          f"phys_addr: {sample.sample_phys_addr:016x} ip: {sample.sample_ip:016x} "
+          f"pid: {sample.sample_pid} tid: {sample.sample_tid} period: {sample.sample_period} "
+          f"time: {sample.sample_time} index: {_STATE.sample_idx}}}")
+
+def common_start_str(comm: str, sample: perf.sample_event) -> str:
+    """Return common start string for sample output."""
+    sec = int(sample.sample_time / 1000000000)
+    ns = sample.sample_time % 1000000000
+    cpu = sample.sample_cpu
+    pid = sample.sample_pid
+    tid = sample.sample_tid
+    return f"{comm:>16s} {pid:5d}/{tid:<5d} [{cpu:04d}] {sec:9d}.{ns:09d}  "
+
+def print_srccode(comm: str, sample: perf.sample_event, symbol: str, dso: str) -> None:
+    """Print source code and symbols for a sample."""
+    ip = sample.sample_ip
+    if symbol == "[unknown]":
+        start_str = common_start_str(comm, sample) + f"{ip:x}".rjust(16).ljust(40)
+    else:
+        symoff = 0
+        symoff = getattr(sample, 'sym_offset', 0) or 0
+        offs = f"+{symoff:#x}" if symoff != 0 else ""
+        start_str = common_start_str(comm, sample) + (symbol + offs).ljust(40)
+
+    source_file_name, line_number, source_line = sample.srccode() or (None, 0, None)
+    if source_file_name:
+        if _STATE.line_number == line_number and _STATE.source_file_name == source_file_name:
+            src_str = ""
+        else:
+            if len(source_file_name) > 40:
+                src_file = f"...{source_file_name[-37:]} "
+            else:
+                src_file = source_file_name.ljust(41)
+
+            if source_line is None:
+                src_str = f"{src_file}{line_number:>4d} <source not found>"
+            else:
+                src_str = f"{src_file}{line_number:>4d} {source_line}"
+        _STATE.dso = None
+    elif dso == _STATE.dso:
+        src_str = ""
+    else:
+        src_str = dso
+        _STATE.dso = dso
+
+    _STATE.line_number = line_number
+    _STATE.source_file_name = source_file_name
+
+    print(start_str, src_str)
+
+class TraceDisasm:
+    """Class to handle trace disassembly."""
+    def __init__(self, cli_options: argparse.Namespace):
+        self.options = cli_options
+        self.sample_idx = -1
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process a single perf event."""
+        self.sample_idx += 1
+        _STATE.sample_idx = self.sample_idx
+
+        if self.options.start_time is not None and sample.sample_time < self.options.start_time:
+            return
+        if self.options.stop_time is not None and sample.sample_time > self.options.stop_time:
+            sys.exit(0)
+        if self.options.start_sample is not None and self.sample_idx < self.options.start_sample:
+            return
+        if self.options.stop_sample is not None and self.sample_idx > self.options.stop_sample:
+            sys.exit(0)
+
+        ev_name = str(sample.evsel)
+        if self.options.verbose:
+            print(f"Event type: {ev_name}")
+            print_sample(sample)
+
+        dso = sample.dso or '[unknown]'
+        symbol = sample.symbol or '[unknown]'
+        dso_bid = (sample.dso_bid.decode('utf-8')
+                   if isinstance(sample.dso_bid, bytes)
+                   else str(sample.dso_bid or '[unknown]'))
+        dso_start = sample.map_start
+        dso_end = sample.map_end
+        map_pgoff = sample.map_pgoff or 0
+
+        comm = "[unknown]"
+        try:
+            if self.session:
+                thread_info = self.session.find_thread(sample.sample_tid)
+                if thread_info:
+                    comm = thread_info.comm() or "[unknown]"
+        except (TypeError, AttributeError):
+            pass
+
+        if dso == '[unknown]':
+            return
+
+        if dso_start is None or dso_end is None:
+            print(f"Failed to find valid dso map for dso {dso}")
+            return
+
+        if "instructions" in ev_name:
+            print_srccode(comm, sample, symbol, dso)
+            return
+
+        if "branches" not in ev_name:
+            return
+
+        self._process_branch(sample, comm, symbol, dso, dso_bid, dso_start, dso_end, map_pgoff)
+
+    def _process_branch(self, sample: perf.sample_event, comm: str, symbol: str, dso: str,
+                        dso_bid: str, dso_start: int, dso_end: int, map_pgoff: int) -> None:
+        """Helper to process branch events."""
+        cpu = sample.sample_cpu
+        ip = sample.sample_ip
+        addr = sample.sample_addr
+
+        if CPU_DATA.get(str(cpu) + 'addr') is None:
+            CPU_DATA[str(cpu) + 'addr'] = addr
+            return
+
+        start_addr = CPU_DATA[str(cpu) + 'addr']
+        stop_addr  = ip + 4
+
+        # Record for previous sample packet
+        CPU_DATA[str(cpu) + 'addr'] = addr
+
+        # Filter out zero start_address. Optionally identify CS_ETM_TRACE_ON packet
+        if start_addr == 0:
+            if stop_addr == 4 and self.options.verbose:
+                print(f"CPU{cpu}: CS_ETM_TRACE_ON packet is inserted")
+            return
+
+        if start_addr < dso_start or start_addr > dso_end:
+            print(f"Start address {start_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if stop_addr < dso_start or stop_addr > dso_end:
+            print(f"Stop address {stop_addr:#x} is out of range [ {dso_start:#x} .. "
+                  f"{dso_end:#x} ] for dso {dso}")
+            return
+
+        if self.options.objdump is not None:
+            if dso == "[kernel.kallsyms]":
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            elif dso_start == 0x400000:
+                dso_vm_start = 0
+                map_pgoff_local = 0
+            else:
+                dso_vm_start = dso_start
+                map_pgoff_local = map_pgoff
+
+            dso_fname = get_dso_file_path(dso, dso_bid, self.options.vmlinux)
+            if path.exists(dso_fname):
+                print_disam(dso_fname, dso_vm_start, start_addr + map_pgoff_local,
+                            stop_addr + map_pgoff_local, self.options.objdump)
+            else:
+                print(f"Failed to find dso {dso} for address range [ "
+                      f"{start_addr + map_pgoff_local:#x} .. {stop_addr + map_pgoff_local:#x} ]")
+
+        print_srccode(comm, sample, symbol, dso)
+
+    def run(self) -> None:
+        """Run the trace disassembly session."""
+        input_file = self.options.input or "perf.data"
+        if input_file != "-" and not os.path.exists(input_file):
+            print(f"Error: {input_file} not found.", file=sys.stderr)
+            sys.exit(1)
+
+        print('ARM CoreSight Trace Data Assembler Dump')
+        try:
+            self.session = perf.session(
+                perf.data(input_file),
+                sample=self.process_event,
+                itrace=self.options.itrace
+            )
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as e:
+            print(f"Error opening session: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        try:
+            self.session.process_events()
+        finally:
+            self.session = None
+        print('End')
+
+if __name__ == "__main__":
+    def int_arg(v: str) -> int:
+        """Helper for integer command line arguments."""
+        val = int(v)
+        if val < 0:
+            raise argparse.ArgumentTypeError("Argument must be a positive integer")
+        return val
+
+    arg_parser = argparse.ArgumentParser(description="ARM CoreSight Trace Dump With Disassembler")
+    arg_parser.add_argument("-i", "--input", help="input perf.data file")
+    arg_parser.add_argument("-k", "--vmlinux",
+                            help="Set path to vmlinux file. Omit to autodetect")
+    arg_parser.add_argument("-d", "--objdump", nargs="?", const=default_objdump(),
+                            help="Show disassembly. Can also be used to change the objdump path")
+    arg_parser.add_argument("-v", "--verbose", action="store_true", help="Enable debugging log")
+    arg_parser.add_argument("--start-time", type=int_arg,
+                            help="Monotonic clock time of sample to start from.")
+    arg_parser.add_argument("--stop-time", type=int_arg,
+                            help="Monotonic clock time of sample to stop at.")
+    arg_parser.add_argument("--itrace", default="b",
+                            help="Instruction tracing options.")
+    arg_parser.add_argument("--start-sample", type=int_arg,
+                            help="Index of sample to start from.")
+    arg_parser.add_argument("--stop-sample", type=int_arg,
+                            help="Index of sample to stop at.")
+
+    parsed_options = arg_parser.parse_args()
+    if (parsed_options.start_time is not None and parsed_options.stop_time is not None and
+            parsed_options.start_time >= parsed_options.stop_time):
+        print("--start-time must less than --stop-time")
+        sys.exit(2)
+    if (parsed_options.start_sample is not None and parsed_options.stop_sample is not None and
+            parsed_options.start_sample >= parsed_options.stop_sample):
+        print("--start-sample must less than --stop-sample")
+        sys.exit(2)
+
+    td = TraceDisasm(parsed_options)
+    td.run()
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +18 -6
index f3ebad596378..51bcbd78f460 100755
@@ -1,6 +1,6 @@
   #!/bin/bash
-# Check Arm CoreSight disassembly script completes without errors (exclusive)
   # SPDX-License-Identifier: GPL-2.0
+# Check Arm CoreSight disassembly script completes without errors (exclusive)
 
   # The disassembly script reconstructs ranges of instructions and gives these to objdump to
   # decode. objdump doesn't like ranges that go backwards, but these are a good indication
@@ -22,9 +22,21 @@ glb_err=1
 
   perfdata_dir=$(mktemp -d /tmp/__perf_test.perf.data.XXXXX)
   perfdata=${perfdata_dir}/perf.data
+perfdata2=${perfdata_dir}/perf2.data
   file=$(mktemp /tmp/temporary_file.XXXXX)
   # Relative path works whether it's installed or running from repo
-script_path=$(dirname "$0")/../../../scripts/python/arm-cs-trace-disasm.py
+if [ -n "$PERF_EXEC_PATH" ] && [ -e "$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py" ]; then
+     script_path="$PERF_EXEC_PATH/python/arm-cs-trace-disasm.py"
+else
+     script_path=$(dirname "$0")/../../../python/arm-cs-trace-disasm.py
+fi
+
+# shellcheck source=lib/setup_python.sh
+. "$(dirname "$0")"/../lib/setup_python.sh
+$PYTHON -c "import perf" 2>/dev/null || {
+     echo "Skipping test, perf python module not found"
+     exit 2
+}
 
   cleanup_files()
   {
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
   if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
       echo "Testing kernel disassembly"
       perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-     perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-             -d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+     $PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+             -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
       grep -q -E ${branch_search} ${file}
       echo "Found kernel branches"
   else
@@ -56,8 +68,8 @@ fi
   ## Test user ##
   echo "Testing userspace disassembly"
   perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-     -d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}
   grep -q -E ${branch_search} ${file}
   echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +1 -0
index 2173215a0517..220d9663f81a 100644
@@ -18,6 +18,10 @@ fi
   # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
   if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
     PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
Quoted 8 lines

tools/perf/tests/shell/lib/setup_python.sh fi · line 27

Quoted 6 lines
tools/perf/tests/shell/lib/setup_python.sh +3 -0
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"
   elif [ -d "$(dirname "$0")/../../python" ]; then
     PYTHONPATH_DIR="$(dirname "$0")/../../python"
   elif [ -d "$(dirname "$0")/../python" ]; then
Quoted 5 lines

Thanks! v3 has some Sashiko related nits cleaned up in this code, but I'll keep the reviewed-by tag. I hope that's okay.

Thanks,
Ian

Yep sure.

reviewer
#181 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

In reply to Ian Rogers

On Sun, Sep 20, 2026 at 10:06:56PM -0700, Ian Rogers wrote:

[...]

Quoted 2 lines
---
 tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++

A new script is added under tools/perf/python/, but I don't see the legacy script under tools/perf/scripts/python/ being removed.

Should we maintain two copies of the script for the same purpose?

Quoted 15 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +2 -2
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
 if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
 	echo "Testing kernel disassembly"
 	perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-	perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-		-d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+	$PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+		-k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
 	grep -q -E ${branch_search} ${file}
 	echo "Found kernel branches"
 else

tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh fi · line 72

Quoted 7 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +2 -2
@@ -56,8 +68,8 @@ fi
 ## Test user ##
 echo "Testing userspace disassembly"
 perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-	-d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}

The perf script command is replaced with perf inject and a Python command. I learned from the above that we can pass the itrace option to the Python script.

This is fine with me. I'm just curious why we don't use a single Python command here.

Quoted 8 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +0 -0
 grep -q -E ${branch_search} ${file}
 echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +0 -0
index 2173215a0517..220d9663f81a 100644

tools/perf/tests/shell/lib/setup_python.sh fi · line 24

Quoted 7 lines
tools/perf/tests/shell/lib/setup_python.sh +4 -0
@@ -18,6 +18,10 @@ fi
 # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
 if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
   PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"

Not sure if this is specific to arm-cs-trace-disasm.py. Would this be a common change?

Thanks,
Leo

reviewer
#182 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

Quoted 11 lines · Leo Yan

On Sun, Sep 20, 2026 at 10:06:56PM -0700, Ian Rogers wrote:

[...]

Quoted 2 lines
---
 tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++

A new script is added under tools/perf/python/, but I don't see the legacy script under tools/perf/scripts/python/ being removed.

Should we maintain two copies of the script for the same purpose?

Please ignore this question. I saw the patch 46 removes all legacy scripts.

author reviewer
#183 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

Quoted 9 lines · Leo Yan
Quoted 7 lines · Leo Yan

On Sun, Sep 20, 2026 at 10:06:56PM -0700, Ian Rogers wrote:

[...]

Quoted 2 lines
---
 tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++
Quoted 6 lines
Quoted 4 lines

A new script is added under tools/perf/python/, but I don't see the legacy script under tools/perf/scripts/python/ being removed.

Should we maintain two copies of the script for the same purpose?

Please ignore this question. I saw the patch 46 removes all legacy scripts.

Thanks, Leo. Yes, this is intentional so that if needed, we can land the ported scripts without removing the previous scripts. We could always add and remove them together; I was trying to think of ways to reduce friction for some or all of these changes to land.

Ian

author reviewer
#184 lore raw

Re: [PATCH v2 38/49] perf python: Port arm-cs-trace-disasm to perf module

In reply to Leo Yan
Quoted 40 lines · Leo Yan

On Sun, Sep 20, 2026 at 10:06:56PM -0700, Ian Rogers wrote:

[...]

Quoted 2 lines
---
 tools/perf/python/arm-cs-trace-disasm.py      | 356 ++++++++++++++++++

A new script is added under tools/perf/python/, but I don't see the legacy script under tools/perf/scripts/python/ being removed.

Should we maintain two copies of the script for the same purpose?

Quoted 15 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +2 -2
@@ -44,8 +56,8 @@ branch_search='[[:space:]](bl|b(\.(eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al)
 if [ "$(id -u)" == 0 ] && [ -e /proc/kcore ]; then
      echo "Testing kernel disassembly"
      perf record -o ${perfdata} -e cs_etm//k --kcore -Se -m,64K -- touch $file > /dev/null 2>&1
-     perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-             -d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+     $PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+             -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
      grep -q -E ${branch_search} ${file}
      echo "Found kernel branches"
 else

tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh fi · line 72

Quoted 7 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +2 -2
@@ -56,8 +68,8 @@ fi
 ## Test user ##
 echo "Testing userspace disassembly"
 perf record -o ${perfdata} -e cs_etm//u -Se -m,64K -- touch $file > /dev/null 2>&1
-perf script -i ${perfdata} --itrace=b -s python:${script_path} -- \
-     -d --stop-sample=2 2> /dev/null > ${file}
+perf inject --itrace=b -i ${perfdata} -o ${perfdata2}
+$PYTHON ${script_path} -i ${perfdata2} -d --stop-sample=2 2> /dev/null > ${file}

The perf script command is replaced with perf inject and a Python command. I learned from the above that we can pass the itrace option to the Python script.

Quoted 2 lines

This is fine with me. I'm just curious why we don't use a single Python command here.

A single Python command that would handle both the itrace and file processing? We could. Having the script command also perform an inject-like rewrite of the data seemed less obvious to me than just having perf inject do the work and then running the script. It does mean a change for people using the script, and two commands could be less convenient than one. We could expand the Python module "session" code to include the ability to rewrite itrace data specifically for this script. I'm hoping that we can have more composable perf_tools in the code, similar to the ASLR code. It would be nice to add itrace support to the Python code with composing tools in mind. I was just putting off worrying about this for now.

One effect of not using the inject command with itrace was that it had accidentally broken for intel-pt over time as we lacked testing coverage.

Quoted 18 lines
Quoted 8 lines
tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +0 -0
 grep -q -E ${branch_search} ${file}
 echo "Found userspace branches"
 
tools/perf/tests/shell/lib/setup_python.sh +0 -0
index 2173215a0517..220d9663f81a 100644

tools/perf/tests/shell/lib/setup_python.sh fi · line 24

Quoted 7 lines
tools/perf/tests/shell/lib/setup_python.sh +4 -0
@@ -18,6 +18,10 @@ fi
 # Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
 if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
   PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
+elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
+elif [ -d "$(dirname "$0")/../../../python" ]; then
+  PYTHONPATH_DIR="$(dirname "$0")/../../../python"

Not sure if this is specific to arm-cs-trace-disasm.py. Would this be a common change?

Agreed. Will fix in v3.

Thanks,
Ian

Quoted 2 lines

Thanks,
Leo

author reviewer
#207 lore raw

v3 cover perf: Complete transition to standalone Python scripts

In reply to Ian Rogers

The perf script command has historically supported running Python and Perl scripts by embedding libpython and libperl. That approach has several drawbacks:
- high overhead from constructing Python dictionaries for every event (whether used by the script or not),
- complex build dependencies on specific Python/Perl libraries,
- threading and interpreter lifecycle complications from embedding the interpreter inside perf,
- no unified model for standalone scripts such as ilist.py, treport.py, tracepoint.py, and twatch.py.

Parts of this series were previously posted as part of the larger "perf: Reorganize scripting support" series:
https://lore.kernel.org/linux-perf-users/[email protected]/
with earlier RFC and mailing list discussion at:
https://lore.kernel.org/linux-perf-users/[email protected]/
https://lore.kernel.org/lkml/CAP-5=fWDqE8SYfOLZkg_0=4Ayx6E7O+h7uUp4NDeCFkiN4b7-w@mail.gmail.com/

From that earlier series, the prerequisite patches (header dependency cleanups, evlist/evsel reference counting, initial perf.data and perf.session C extension wrappers, callchain/brstack/stat/syscall/config accessors, perf.pyi type stubs, LiveSession, and removal of embedded libperl support) have now been merged. We restarted the version number at v1 for the remaining patches, along with additional C extension capabilities, build-time static analysis (mypy and pylint), and review fixes.

Running scripts as standalone Python applications using the perf Python C extension module provides a dramatic speedup. For example, with mem-phys-addr.py:

Before (using embedded libpython in perf script):

```
$ perf mem record -a sleep 1
$ time perf script tools/perf/scripts/python/mem-phys-addr.py
Event: cpu_core/mem-loads-aux/
Memory type                                    count  percentage
 ---------------------------------------  ----------  ----------
0-fff : Reserved                                3217       100.0

real    0m3.754s
user    0m0.023s
sys     0m0.018s
```

After (using standalone Python script with perf module):

```
$ PYTHONPATH=/tmp/perf/python time python3 tools/perf/python/mem-phys-addr.py
Event: evsel(cpu_core/mem-loads-aux/)
Memory type                                    count  percentage
 ---------------------------------------  ----------  ----------
0-fff : Reserved                                3217       100.0

real    0m0.106s
user    0m0.021s
sys     0m0.020s
```

The 49 patches in this series are organized into four groups:

1. Perf Python C Extension & Existing Script Updates (Patches 01-10):
- Refine syscall_name()/syscall_id() argument parsing, expose perf.arch_strerrno() from the auto-generated trace beauty errno tables, and update callchain stubs and session.find_thread() lookup in tools/perf/util/python.c and tools/perf/python/perf.pyi.
- Clean up pylint warnings and improve perf script -l descriptions in existing standalone scripts (ilist.py, treport.py, tracepoint.py, twatch.py).
- Expose destination address location attributes (addr_dso, addr_symbol, addr_sym_offset), branch/transaction fields, sym_offset, context_switch callbacks, and Intel PT call_return / itrace synthesis directly in the perf Python extension (many parts previously posted in
https://lore.kernel.org/linux-perf-users/[email protected]/).
- Allow KeyboardInterrupt to propagate cleanly in LiveSession.

2. Build-Time Python Static Analysis (Patches 11-14):
- Clean up mypy and pylint warnings in tools/perf/pmu-events/ and tools/perf/tests/shell/lib/.
- Make MYPY and PYLINT build checks enabled by default when mypy and pylint (>= 2.16.0) are installed, with NO_MYPY=1 and NO_PYLINT=1 opt-out controls (matching NO_SHELLCHECK=1), so all Python scripts in tools/perf are continuously type-checked and linted at build time.

3. Porting Legacy Python & Perl Scripts to tools/perf/python/ (Patches 15-45):
- Previously sent as part of
https://lore.kernel.org/linux-perf-users/[email protected]/
and updated with static type annotations, mypy/pylint cleanups, and accompanying shell tests under tools/perf/tests/shell/. Sashiko-driven review and improvement was undertaken while porting so that the ported scripts are in the best shape possible:
* Temporarily install tools/perf/python/ scripts during the transition (Patch 15).
* General & profiling scripts: stat-cpi.py, mem-phys-addr.py, stackcollapse.py, flamegraph.py, gecko.py, event_analyzing_sample.py (Patches 16-21).
* Syscall & I/O scripts (ported from legacy Python and Perl):
syscall-counts.py, syscall-counts-by-pid.py, failed-syscalls-by-pid.py, failed-syscalls.py, sctop.py, rw-by-file.py, rw-by-pid.py, rwtop.py (Patches 22-29).
* Scheduling, locking, MM & networking scripts: futex-contention.py, task-analyzer.py, sched-migration.py (with SchedGui.py), wakeup-latency.py, compaction-times.py, net_dropmonitor.py, netdev-times.py, check-perf-trace.py (Patches 30-37).
* Hardware trace & architecture-specific scripts: arm-cs-trace-disasm.py, powerpc-hcalls.py, intel-pt-events.py (with libxed.py), and migrating the Intel PT virtual LBR test to the Python API (Patches 38-41).
* Database export & viewer utilities: export-to-sqlite.py, export-to-postgresql.py, exported-sql-viewer.py, and parallel-perf.py (Patches 42-45).

4. Removing Embedded libpython & Updating Build, CLI, and Docs (Patches 46-49):
- Previously sent as part of
https://lore.kernel.org/linux-perf-users/[email protected]/ :
* Remove embedded libpython support (scripting-engines/trace-event-python.c) and delete the legacy tools/perf/scripts/python/ directory (Patch 46).
* Replace the libpython feature check with test-python-module.c and update Python script installation paths (Patch 47).
* Update perf script (builtin-script.c) to execute standalone scripts directly and remove embedded scripting engine support (Patch 48).
* Update perf man pages (perf-script.txt, perf-script-python.txt, perf-check.txt) for standalone Python scripts (Patch 49).

Changes in v3:
- Expose perf_env__arch_strerrno() as perf.arch_strerrno() in tools/perf/util/python.c (and tighten arch_errno_names.sh macro matching to E[A-Z0-9]+ with EM_SPARC32PLUS support) so failed-syscalls-by-pid.py uses the auto-generated C beauty errno tables instead of maintaining Python errno dictionaries (Patches 01, 24).
- Change static char * const kwlist[] to static char *kwlist[] in pyrf_session__new() in tools/perf/util/python.c to support Python < 3.13 where PyArg_ParseTupleAndKeywords() expects char ** rather than char *const *, and register psession->tool.event_update so pipe-mode PERF_RECORD_EVENT_UPDATE metadata is processed (Patches 08, 09).
- Explicitly annotate d_hcoal: Union[Event, int] and
d_mcoal: Union[Event, int] in AmdDtlb() in tools/perf/pmu-events/amd_metrics.py so older mypy versions do not infer join(Event, int) = object (Patch 11).
- Pass --ignore-missing-imports to MYPY and --ignored-modules=textual,setproctitle to PYLINT in tools/perf/Makefile.perf so default static analysis builds succeed on systems without optional third-party Python packages (python3-textual, python3-setproctitle) installed (Patches 13, 14).
- Chain $(INSTALL) commands with && in Makefile.perf for the python-scripts installation rules and invoke "perf script " without -s from script_browse() in ui/browsers/scripts.c (Patches 15, 47).
- Add Subresource Integrity (integrity="sha256-...") attributes to the CDN <link> and <script> tags in MINIMAL_HTML in flamegraph.py (Patch 19).
- Remove obsolete AttributeError fallback in gecko.py and ensure SQLite connections in event_analyzing_sample.py are closed on error in trace_begin() and trace_end() (Patches 20, 21).
- Continue updating timeslices in sched_switch() in sched-migration.py after printing a rejection warning so runqueue state resynchronizes after dropped ring-buffer events (Patch 32).
- Check if not irq_list in handle_irq_softirq_exit() in netdev-times.py so empty irq_list entries do not append vacuous hunks (Patch 36).
- Move setup_python.sh path resolution updates to Patch 12, encapsulate cpu_data on TraceDisasm and honor PERF_SYMBOL_VMLINUX in arm-cs-trace-disasm.py, and add Reviewed-by: James Clark <[email protected]> (Patches 12, 38).
- Fix unpacked cbr payload variable names (cbr_val, max_nonturbo, freq) in export-to-sqlite.py and export-to-postgresql.py, and catch BaseException before terminating worker processes and re-raising in parallel-perf.py (Patches 42, 43, 45).
- Improve shell test robustness in test_stat_cpi_python.sh, test_task_analyzer.sh, test_compaction_times_python.sh, test_net_dropmonitor_python.sh, and test_arm_coresight_disasm.sh, and update all Python script shell tests to invoke perf script <script> in Patch 48 (Patches 16, 31, 34, 35, 38, 48).

Changes in v2:
- Mark session.find_thread() parameters as positional-only (/) and update sample_event.srccode() return type annotation to Optional[tuple[Optional[str], int, Optional[str]]] in perf.pyi (Patches 02, 08).
- Break self -> self.session -> bound method callback reference cycles in flamegraph.py, failed-syscalls.py, wakeup-latency.py, and arm-cs-trace-disasm.py via try ... finally: self.session = None (Patches 19, 25, 33, 38).
- Separate raw_syscalls:sys_enter (id) from syscalls:sys_enter* (__syscall_nr / nr) and support x32 ABI syscall numbers (0x40000000) in syscall-counts.py, syscall-counts-by-pid.py, and sctop.py (Patches 22, 23, 26).
- Prevent non-generic architectures in _ARCH_ERRNO_TABLES from falling through into _GENERIC_ERRNO_OVERRIDES in failed-syscalls-by-pid.py (Patch 24).
- Treat 0-byte read/write return values as non-errors in rw-by-pid.py (Patch 28).
- Avoid repeated /proc/kallsyms parsing and cache fallback symbol resolutions in net_dropmonitor.py (Patch 35).
- Check is None instead of truthiness for event_list in netdev-times.py so valid softirqs with empty event lists are not dropped (Patch 36).
- Handle objdump subprocess errors gracefully in arm-cs-trace-disasm.py and update setup_python.sh / test_arm_coresight_disasm.sh so nested shell tests locate perf.so when run directly (Patches 12, 38).
- Populate branch_types, samples_view.branch_type_name, calls parent indexes (pcpid_idx, pid_idx), and comms.has_calls in export-to-sqlite.py for exported-sql-viewer.py compatibility (Patch 42).
- Escape single quotes with \' for libpq connection strings in export-to-postgresql.py (Patch 43).
- Replace Markdown ### subsection headers with Asciidoc ~~~~ underlines in perf-script-python.txt (Patch 49).

This patch series has also been covered by Phoronix:
https://www.phoronix.com/news/Linux-Perf-Python-Module
and a talk about this work will be given by Alice Rogers and Ian Rogers at the 2026 Linux Plumbers Conference:
https://lpc.events/event/20/contributions/2510/

Ian Rogers (49):
perf python: Update syscall helpers and expose arch_strerrno perf python: Update callchain stubs and session thread lookup perf python: Clean up pylint warnings in ilist.py
perf python: Clean up pylint warnings in treport.py
perf python: Clean up pylint warnings in tracepoint.py perf python: Clean up pylint warnings in twatch.py
perf python: Improve perf script -l descriptions
perf python: Expose addr location, transaction, and context_switch perf python: Add Intel PT call_return and itrace capability perf python: Allow KeyboardInterrupt to propagate in LiveSession perf pmu-events: Clean up mypy and pylint issues
perf test: Clean up mypy and pylint issues in shell test libraries perf build: Make mypy build test opt-out (NO_MYPY=1)
perf build: Make pylint build test opt-out (NO_PYLINT=1) perf Makefile: Install standalone Python scripts during transition perf python: Port stat-cpi to perf module
perf python: Port mem-phys-addr to perf module
perf python: Port stackcollapse to perf module
perf python: Port flamegraph to perf module
perf python: Port gecko to perf module
perf python: Port event_analyzing_sample to perf module perf python: Port syscall-counts to perf module
perf python: Port syscall-counts-by-pid to perf module perf python: Port failed-syscalls-by-pid to perf module perf python: Port failed-syscalls from Perl to perf module perf python: Port sctop to perf module
perf python: Port rw-by-file from Perl to perf module
perf python: Port rw-by-pid from Perl to perf module
perf python: Port rwtop from Perl to perf module
perf python: Port futex-contention to perf module
perf python: Port task-analyzer to perf module
perf python: Port sched-migration and SchedGui to perf module perf python: Port wakeup-latency from Perl to perf module perf python: Port compaction-times to perf module
perf python: Port net_dropmonitor to perf module
perf python: Port netdev-times to perf module
perf python: Port check-perf-trace to perf module
perf python: Port arm-cs-trace-disasm to perf module
perf python: Port powerpc-hcalls to perf module
perf python: Port intel-pt-events and libxed to perf module perf test: Migrate Intel PT virtual LBR test to Python API perf python: Port export-to-sqlite to perf module
perf python: Port export-to-postgresql to perf module
perf python: Move and clean up exported-sql-viewer.py
perf python: Move and clean up parallel-perf.py
perf: Remove libpython support and legacy Python scripts perf Makefile: Update Python script installation path
perf script: Support standalone scripts and remove embedded scripting perf Documentation: Update for standalone Python scripts

 tools/build/Makefile.feature                  |    5 +-
 tools/build/feature/Makefile                  |   24 +-
 tools/build/feature/test-all.c                |    6 +-
 tools/build/feature/test-libperl.c            |   10 -
 tools/build/feature/test-libpython.c          |   10 -
 tools/build/feature/test-python-module.c      |   13 +
 tools/perf/Build                              |    5 +-
 tools/perf/Documentation/db-export.txt        |   20 +-
 tools/perf/Documentation/perf-check.txt       |    3 +-
 tools/perf/Documentation/perf-script-perl.txt |  216 -
 .../perf/Documentation/perf-script-python.txt |  797 +--
 tools/perf/Documentation/perf-script.txt      |   82 +-
 tools/perf/Documentation/tips.txt             |    1 -
 tools/perf/Makefile.config                    |   40 +-
 tools/perf/Makefile.perf                      |   94 +-
 tools/perf/builtin-check.c                    |    3 +-
 tools/perf/builtin-script.c                   |  986 ++--
 tools/perf/pmu-events/Build                   |    4 +-
 tools/perf/pmu-events/amd_metrics.py          |   16 +-
 tools/perf/pmu-events/intel_metrics.py        |  259 +-
 tools/perf/pmu-events/jevents.py              |   77 +-
 tools/perf/pmu-events/make_legacy_cache.py    |   34 +-
 tools/perf/pmu-events/metric.py               |   58 +-
 tools/perf/python/SchedGui.py                 |  246 +
 tools/perf/python/arm-cs-trace-disasm.py      |  389 ++
 tools/perf/python/check-perf-trace.py         |  223 +
 tools/perf/python/compaction-times.py         |  362 ++
 tools/perf/python/counting.py                 |    1 +
 tools/perf/python/event_analyzing_sample.py   |  332 ++
 tools/perf/python/export-to-postgresql.py     | 1328 +++++
 tools/perf/python/export-to-sqlite.py         |  946 +++
 tools/perf/python/exported-sql-viewer.py      | 5103 +++++++++++++++++
 tools/perf/python/failed-syscalls-by-pid.py   |  161 +
 tools/perf/python/failed-syscalls.py          |   94 +
 tools/perf/python/flamegraph.py               |  283 +
 tools/perf/python/futex-contention.py         |  107 +
 tools/perf/python/gecko.py                    |  411 ++
 tools/perf/python/ilist.py                    |    8 +-
 tools/perf/python/intel-pt-events.py          |  632 ++
 tools/perf/python/libxed.py                   |  123 +
 tools/perf/python/mem-phys-addr.py            |  136 +
 tools/perf/python/net_dropmonitor.py          |  173 +
 tools/perf/python/netdev-times.py             |  494 ++
 tools/perf/python/parallel-perf.py            | 1250 ++++
 tools/perf/python/perf.pyi                    |  122 +-
 tools/perf/python/perf_live.py                |   10 +-
 .../{scripts => }/python/powerpc-hcalls.py    |  213 +-
 tools/perf/python/rw-by-file.py               |  113 +
 tools/perf/python/rw-by-pid.py                |  205 +
 tools/perf/python/rwtop.py                    |  256 +
 tools/perf/python/sched-migration.py          |  496 ++
 tools/perf/python/sctop.py                    |  246 +
 tools/perf/python/stackcollapse.py            |  145 +
 tools/perf/python/stat-cpi.py                 |  210 +
 tools/perf/python/syscall-counts-by-pid.py    |  108 +
 tools/perf/python/syscall-counts.py           |   91 +
 tools/perf/python/task-analyzer.py            |  908 +++
 tools/perf/python/tracepoint.py               |    5 +-
 tools/perf/python/treport.py                  |   17 +-
 tools/perf/python/twatch.py                   |   81 +-
 tools/perf/python/wakeup-latency.py           |  103 +
 tools/perf/scripts/Build                      |   30 -
 tools/perf/scripts/install-build-deps.sh      |    4 +-
 tools/perf/scripts/perl/Perf-Trace-Util/Build |    9 -
 .../scripts/perl/Perf-Trace-Util/Context.c    |  122 -
 .../scripts/perl/Perf-Trace-Util/Context.xs   |   42 -
 .../scripts/perl/Perf-Trace-Util/Makefile.PL  |   18 -
 .../perf/scripts/perl/Perf-Trace-Util/README  |   59 -
 .../Perf-Trace-Util/lib/Perf/Trace/Context.pm |   55 -
 .../Perf-Trace-Util/lib/Perf/Trace/Core.pm    |  192 -
 .../Perf-Trace-Util/lib/Perf/Trace/Util.pm    |   94 -
 .../perf/scripts/perl/Perf-Trace-Util/typemap |    1 -
 .../scripts/perl/bin/check-perf-trace-record  |    2 -
 .../scripts/perl/bin/failed-syscalls-record   |    3 -
 .../scripts/perl/bin/failed-syscalls-report   |   10 -
 tools/perf/scripts/perl/bin/rw-by-file-record |    3 -
 tools/perf/scripts/perl/bin/rw-by-file-report |   10 -
 tools/perf/scripts/perl/bin/rw-by-pid-record  |    2 -
 tools/perf/scripts/perl/bin/rw-by-pid-report  |    3 -
 tools/perf/scripts/perl/bin/rwtop-record      |    2 -
 tools/perf/scripts/perl/bin/rwtop-report      |   20 -
 .../scripts/perl/bin/wakeup-latency-record    |    6 -
 .../scripts/perl/bin/wakeup-latency-report    |    3 -
 tools/perf/scripts/perl/check-perf-trace.pl   |  106 -
 tools/perf/scripts/perl/failed-syscalls.pl    |   47 -
 tools/perf/scripts/perl/rw-by-file.pl         |  106 -
 tools/perf/scripts/perl/rw-by-pid.pl          |  184 -
 tools/perf/scripts/perl/rwtop.pl              |  203 -
 tools/perf/scripts/perl/wakeup-latency.pl     |  107 -
 .../perf/scripts/python/Perf-Trace-Util/Build |    4 -
 .../scripts/python/Perf-Trace-Util/Context.c  |  225 -
 .../Perf-Trace-Util/lib/Perf/Trace/Core.py    |  116 -
 .../lib/Perf/Trace/EventClass.py              |   97 -
 .../lib/Perf/Trace/SchedGui.py                |  184 -
 .../Perf-Trace-Util/lib/Perf/Trace/Util.py    |   92 -
 .../scripts/python/arm-cs-trace-disasm.py     |  356 --
 .../python/bin/compaction-times-record        |    2 -
 .../python/bin/compaction-times-report        |    4 -
 .../python/bin/event_analyzing_sample-record  |    8 -
 .../python/bin/event_analyzing_sample-report  |    3 -
 .../python/bin/export-to-postgresql-record    |    8 -
 .../python/bin/export-to-postgresql-report    |   29 -
 .../python/bin/export-to-sqlite-record        |    8 -
 .../python/bin/export-to-sqlite-report        |   29 -
 .../python/bin/failed-syscalls-by-pid-record  |    3 -
 .../python/bin/failed-syscalls-by-pid-report  |   10 -
 .../perf/scripts/python/bin/flamegraph-record |    2 -
 .../perf/scripts/python/bin/flamegraph-report |    3 -
 .../python/bin/futex-contention-record        |    2 -
 .../python/bin/futex-contention-report        |    4 -
 tools/perf/scripts/python/bin/gecko-record    |    2 -
 tools/perf/scripts/python/bin/gecko-report    |    7 -
 .../scripts/python/bin/intel-pt-events-record |   13 -
 .../scripts/python/bin/intel-pt-events-report |    3 -
 .../scripts/python/bin/mem-phys-addr-record   |   19 -
 .../scripts/python/bin/mem-phys-addr-report   |    3 -
 .../scripts/python/bin/net_dropmonitor-record |    2 -
 .../scripts/python/bin/net_dropmonitor-report |    4 -
 .../scripts/python/bin/netdev-times-record    |    8 -
 .../scripts/python/bin/netdev-times-report    |    5 -
 .../scripts/python/bin/powerpc-hcalls-record  |    2 -
 .../scripts/python/bin/powerpc-hcalls-report  |    2 -
 .../scripts/python/bin/sched-migration-record |    2 -
 .../scripts/python/bin/sched-migration-report |    3 -
 tools/perf/scripts/python/bin/sctop-record    |    3 -
 tools/perf/scripts/python/bin/sctop-report    |   24 -
 .../scripts/python/bin/stackcollapse-record   |    8 -
 .../scripts/python/bin/stackcollapse-report   |    3 -
 .../python/bin/syscall-counts-by-pid-record   |    3 -
 .../python/bin/syscall-counts-by-pid-report   |   10 -
 .../scripts/python/bin/syscall-counts-record  |    3 -
 .../scripts/python/bin/syscall-counts-report  |   10 -
 .../scripts/python/bin/task-analyzer-record   |    2 -
 .../scripts/python/bin/task-analyzer-report   |    3 -
 tools/perf/scripts/python/check-perf-trace.py |   84 -
 tools/perf/scripts/python/compaction-times.py |  311 -
 .../scripts/python/event_analyzing_sample.py  |  192 -
 .../scripts/python/export-to-postgresql.py    | 1114 ----
 tools/perf/scripts/python/export-to-sqlite.py |  799 ---
 .../scripts/python/exported-sql-viewer.py     | 5030 ----------------
 .../scripts/python/failed-syscalls-by-pid.py  |   79 -
 tools/perf/scripts/python/flamegraph.py       |  267 -
 tools/perf/scripts/python/futex-contention.py |   57 -
 tools/perf/scripts/python/gecko.py            |  395 --
 tools/perf/scripts/python/intel-pt-events.py  |  494 --
 tools/perf/scripts/python/libxed.py           |  107 -
 tools/perf/scripts/python/mem-phys-addr.py    |  127 -
 tools/perf/scripts/python/net_dropmonitor.py  |   78 -
 tools/perf/scripts/python/netdev-times.py     |  473 --
 tools/perf/scripts/python/parallel-perf.py    |  989 ----
 tools/perf/scripts/python/sched-migration.py  |  462 --
 tools/perf/scripts/python/sctop.py            |   89 -
 tools/perf/scripts/python/stackcollapse.py    |  127 -
 tools/perf/scripts/python/stat-cpi.py         |   79 -
 .../scripts/python/syscall-counts-by-pid.py   |   75 -
 tools/perf/scripts/python/syscall-counts.py   |   65 -
 tools/perf/scripts/python/task-analyzer.py    |  934 ---
 tools/perf/tests/Build                        |    4 +-
 tools/perf/tests/make                         |   10 +-
 .../coresight/test_arm_coresight_disasm.sh    |   15 +-
 tools/perf/tests/shell/lib/attr.py            |   86 +-
 .../perf/tests/shell/lib/perf_brstack_max.py  |   40 +
 .../tests/shell/lib/perf_json_output_lint.py  |   36 +-
 .../tests/shell/lib/perf_metric_validation.py |   53 +-
 tools/perf/tests/shell/lib/setup_python.sh    |   29 +-
 tools/perf/tests/shell/script.sh              |   46 +-
 tools/perf/tests/shell/script_perl.sh         |  102 -
 tools/perf/tests/shell/script_python.sh       |  113 -
 .../shell/test_check_perf_trace_python.sh     |   85 +
 .../shell/test_compaction_times_python.sh     |  127 +
 .../test_event_analyzing_sample_python.sh     |   63 +
 .../shell/test_export_to_postgresql_python.sh |  124 +
 .../shell/test_export_to_sqlite_python.sh     |  109 +
 .../test_failed_syscalls_by_pid_python.sh     |   96 +
 .../shell/test_failed_syscalls_python.sh      |   80 +
 .../tests/shell/test_flamegraph_python.sh     |  110 +
 .../shell/test_futex_contention_python.sh     |  115 +
 tools/perf/tests/shell/test_gecko_python.sh   |   95 +
 tools/perf/tests/shell/test_intel_pt.sh       |   43 +-
 .../shell/test_intel_pt_events_python.sh      |   69 +
 .../tests/shell/test_mem_phys_addr_python.sh  |  103 +
 .../shell/test_net_dropmonitor_python.sh      |  102 +
 .../tests/shell/test_netdev_times_python.sh   |  104 +
 .../tests/shell/test_powerpc_hcalls_python.sh |   95 +
 .../tests/shell/test_rw_by_file_python.sh     |   68 +
 .../perf/tests/shell/test_rw_by_pid_python.sh |   75 +
 tools/perf/tests/shell/test_rwtop_python.sh   |   74 +
 .../shell/test_sched_migration_python.sh      |   72 +
 tools/perf/tests/shell/test_sctop_python.sh   |   78 +
 .../tests/shell/test_stackcollapse_python.sh  |   78 +
 .../perf/tests/shell/test_stat_cpi_python.sh  |  115 +
 .../test_syscall_counts_by_pid_python.sh      |   93 +
 .../tests/shell/test_syscall_counts_python.sh |   81 +
 tools/perf/tests/shell/test_task_analyzer.sh  |   90 +-
 .../tests/shell/test_wakeup_latency_python.sh |   82 +
 tools/perf/trace/beauty/arch_errno_names.sh   |    4 +-
 tools/perf/ui/browsers/scripts.c              |  219 +-
 tools/perf/util/Build                         |    7 +-
 tools/perf/util/python.c                      | 1040 +++-
 tools/perf/util/scripting-engines/Build       |    9 -
 .../util/scripting-engines/trace-event-perl.c |  770 ---
 .../scripting-engines/trace-event-python.c    | 2333 --------
 tools/perf/util/trace-event-parse.c           |   65 -
 tools/perf/util/trace-event-scripting.c       |  407 --
 tools/perf/util/trace-event.h                 |   72 +-
 205 files changed, 20492 insertions(+), 21491 deletions(-)
 delete mode 100644 tools/build/feature/test-libperl.c
 delete mode 100644 tools/build/feature/test-libpython.c
 create mode 100644 tools/build/feature/test-python-module.c
 delete mode 100644 tools/perf/Documentation/perf-script-perl.txt
 create mode 100755 tools/perf/python/SchedGui.py
 create mode 100755 tools/perf/python/arm-cs-trace-disasm.py
 create mode 100755 tools/perf/python/check-perf-trace.py
 create mode 100755 tools/perf/python/compaction-times.py
 create mode 100755 tools/perf/python/event_analyzing_sample.py
 create mode 100755 tools/perf/python/export-to-postgresql.py
 create mode 100755 tools/perf/python/export-to-sqlite.py
 create mode 100755 tools/perf/python/exported-sql-viewer.py
 create mode 100755 tools/perf/python/failed-syscalls-by-pid.py
 create mode 100755 tools/perf/python/failed-syscalls.py
 create mode 100755 tools/perf/python/flamegraph.py
 create mode 100755 tools/perf/python/futex-contention.py
 create mode 100755 tools/perf/python/gecko.py
 create mode 100755 tools/perf/python/intel-pt-events.py
 create mode 100755 tools/perf/python/libxed.py
 create mode 100755 tools/perf/python/mem-phys-addr.py
 create mode 100755 tools/perf/python/net_dropmonitor.py
 create mode 100755 tools/perf/python/netdev-times.py
 create mode 100755 tools/perf/python/parallel-perf.py
 rename tools/perf/{scripts => }/python/powerpc-hcalls.py (54%)
 mode change 100644 => 100755
 create mode 100755 tools/perf/python/rw-by-file.py
 create mode 100755 tools/perf/python/rw-by-pid.py
 create mode 100755 tools/perf/python/rwtop.py
 create mode 100755 tools/perf/python/sched-migration.py
 create mode 100755 tools/perf/python/sctop.py
 create mode 100755 tools/perf/python/stackcollapse.py
 create mode 100755 tools/perf/python/stat-cpi.py
 create mode 100755 tools/perf/python/syscall-counts-by-pid.py
 create mode 100755 tools/perf/python/syscall-counts.py
 create mode 100755 tools/perf/python/task-analyzer.py
 create mode 100755 tools/perf/python/wakeup-latency.py
 delete mode 100644 tools/perf/scripts/Build
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Build
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Context.c
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Context.xs
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/Makefile.PL
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/README
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Context.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Core.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/lib/Perf/Trace/Util.pm
 delete mode 100644 tools/perf/scripts/perl/Perf-Trace-Util/typemap
 delete mode 100644 tools/perf/scripts/perl/bin/check-perf-trace-record
 delete mode 100644 tools/perf/scripts/perl/bin/failed-syscalls-record
 delete mode 100644 tools/perf/scripts/perl/bin/failed-syscalls-report
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-file-record
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-file-report
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-pid-record
 delete mode 100644 tools/perf/scripts/perl/bin/rw-by-pid-report
 delete mode 100644 tools/perf/scripts/perl/bin/rwtop-record
 delete mode 100644 tools/perf/scripts/perl/bin/rwtop-report
 delete mode 100644 tools/perf/scripts/perl/bin/wakeup-latency-record
 delete mode 100644 tools/perf/scripts/perl/bin/wakeup-latency-report
 delete mode 100644 tools/perf/scripts/perl/check-perf-trace.pl
 delete mode 100644 tools/perf/scripts/perl/failed-syscalls.pl
 delete mode 100644 tools/perf/scripts/perl/rw-by-file.pl
 delete mode 100644 tools/perf/scripts/perl/rw-by-pid.pl
 delete mode 100644 tools/perf/scripts/perl/rwtop.pl
 delete mode 100644 tools/perf/scripts/perl/wakeup-latency.pl
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/Build
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/Context.c
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py
 delete mode 100755 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/EventClass.py
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py
 delete mode 100644 tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py
 delete mode 100755 tools/perf/scripts/python/arm-cs-trace-disasm.py
 delete mode 100644 tools/perf/scripts/python/bin/compaction-times-record
 delete mode 100644 tools/perf/scripts/python/bin/compaction-times-report
 delete mode 100644 tools/perf/scripts/python/bin/event_analyzing_sample-record
 delete mode 100644 tools/perf/scripts/python/bin/event_analyzing_sample-report
 delete mode 100644 tools/perf/scripts/python/bin/export-to-postgresql-record
 delete mode 100644 tools/perf/scripts/python/bin/export-to-postgresql-report
 delete mode 100644 tools/perf/scripts/python/bin/export-to-sqlite-record
 delete mode 100644 tools/perf/scripts/python/bin/export-to-sqlite-report
 delete mode 100644 tools/perf/scripts/python/bin/failed-syscalls-by-pid-record
 delete mode 100644 tools/perf/scripts/python/bin/failed-syscalls-by-pid-report
 delete mode 100755 tools/perf/scripts/python/bin/flamegraph-record
 delete mode 100755 tools/perf/scripts/python/bin/flamegraph-report
 delete mode 100644 tools/perf/scripts/python/bin/futex-contention-record
 delete mode 100644 tools/perf/scripts/python/bin/futex-contention-report
 delete mode 100644 tools/perf/scripts/python/bin/gecko-record
 delete mode 100755 tools/perf/scripts/python/bin/gecko-report
 delete mode 100644 tools/perf/scripts/python/bin/intel-pt-events-record
 delete mode 100644 tools/perf/scripts/python/bin/intel-pt-events-report
 delete mode 100644 tools/perf/scripts/python/bin/mem-phys-addr-record
 delete mode 100644 tools/perf/scripts/python/bin/mem-phys-addr-report
 delete mode 100755 tools/perf/scripts/python/bin/net_dropmonitor-record
 delete mode 100755 tools/perf/scripts/python/bin/net_dropmonitor-report
 delete mode 100644 tools/perf/scripts/python/bin/netdev-times-record
 delete mode 100644 tools/perf/scripts/python/bin/netdev-times-report
 delete mode 100644 tools/perf/scripts/python/bin/powerpc-hcalls-record
 delete mode 100644 tools/perf/scripts/python/bin/powerpc-hcalls-report
 delete mode 100644 tools/perf/scripts/python/bin/sched-migration-record
 delete mode 100644 tools/perf/scripts/python/bin/sched-migration-report
 delete mode 100644 tools/perf/scripts/python/bin/sctop-record
 delete mode 100644 tools/perf/scripts/python/bin/sctop-report
 delete mode 100755 tools/perf/scripts/python/bin/stackcollapse-record
 delete mode 100755 tools/perf/scripts/python/bin/stackcollapse-report
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-by-pid-record
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-by-pid-report
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-record
 delete mode 100644 tools/perf/scripts/python/bin/syscall-counts-report
 delete mode 100755 tools/perf/scripts/python/bin/task-analyzer-record
 delete mode 100755 tools/perf/scripts/python/bin/task-analyzer-report
 delete mode 100644 tools/perf/scripts/python/check-perf-trace.py
 delete mode 100644 tools/perf/scripts/python/compaction-times.py
 delete mode 100644 tools/perf/scripts/python/event_analyzing_sample.py
 delete mode 100644 tools/perf/scripts/python/export-to-postgresql.py
 delete mode 100644 tools/perf/scripts/python/export-to-sqlite.py
 delete mode 100755 tools/perf/scripts/python/exported-sql-viewer.py
 delete mode 100644 tools/perf/scripts/python/failed-syscalls-by-pid.py
 delete mode 100755 tools/perf/scripts/python/flamegraph.py
 delete mode 100644 tools/perf/scripts/python/futex-contention.py
 delete mode 100644 tools/perf/scripts/python/gecko.py
 delete mode 100644 tools/perf/scripts/python/intel-pt-events.py
 delete mode 100644 tools/perf/scripts/python/libxed.py
 delete mode 100644 tools/perf/scripts/python/mem-phys-addr.py
 delete mode 100755 tools/perf/scripts/python/net_dropmonitor.py
 delete mode 100644 tools/perf/scripts/python/netdev-times.py
 delete mode 100755 tools/perf/scripts/python/parallel-perf.py
 delete mode 100644 tools/perf/scripts/python/sched-migration.py
 delete mode 100644 tools/perf/scripts/python/sctop.py
 delete mode 100755 tools/perf/scripts/python/stackcollapse.py
 delete mode 100644 tools/perf/scripts/python/stat-cpi.py
 delete mode 100644 tools/perf/scripts/python/syscall-counts-by-pid.py
 delete mode 100644 tools/perf/scripts/python/syscall-counts.py
 delete mode 100755 tools/perf/scripts/python/task-analyzer.py
 create mode 100644 tools/perf/tests/shell/lib/perf_brstack_max.py
 delete mode 100755 tools/perf/tests/shell/script_perl.sh
 delete mode 100755 tools/perf/tests/shell/script_python.sh
 create mode 100755 tools/perf/tests/shell/test_check_perf_trace_python.sh
 create mode 100755 tools/perf/tests/shell/test_compaction_times_python.sh
 create mode 100755 tools/perf/tests/shell/test_event_analyzing_sample_python.sh
 create mode 100755 tools/perf/tests/shell/test_export_to_postgresql_python.sh
 create mode 100755 tools/perf/tests/shell/test_export_to_sqlite_python.sh
 create mode 100755 tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh
 create mode 100755 tools/perf/tests/shell/test_failed_syscalls_python.sh
 create mode 100755 tools/perf/tests/shell/test_flamegraph_python.sh
 create mode 100755 tools/perf/tests/shell/test_futex_contention_python.sh
 create mode 100755 tools/perf/tests/shell/test_gecko_python.sh
 create mode 100755 tools/perf/tests/shell/test_intel_pt_events_python.sh
 create mode 100755 tools/perf/tests/shell/test_mem_phys_addr_python.sh
 create mode 100755 tools/perf/tests/shell/test_net_dropmonitor_python.sh
 create mode 100755 tools/perf/tests/shell/test_netdev_times_python.sh
 create mode 100755 tools/perf/tests/shell/test_powerpc_hcalls_python.sh
 create mode 100755 tools/perf/tests/shell/test_rw_by_file_python.sh
 create mode 100755 tools/perf/tests/shell/test_rw_by_pid_python.sh
 create mode 100755 tools/perf/tests/shell/test_rwtop_python.sh
 create mode 100755 tools/perf/tests/shell/test_sched_migration_python.sh
 create mode 100755 tools/perf/tests/shell/test_sctop_python.sh
 create mode 100755 tools/perf/tests/shell/test_stackcollapse_python.sh
 create mode 100755 tools/perf/tests/shell/test_stat_cpi_python.sh
 create mode 100755 tools/perf/tests/shell/test_syscall_counts_by_pid_python.sh
 create mode 100755 tools/perf/tests/shell/test_syscall_counts_python.sh
 create mode 100755 tools/perf/tests/shell/test_wakeup_latency_python.sh
 delete mode 100644 tools/perf/util/scripting-engines/Build
 delete mode 100644 tools/perf/util/scripting-engines/trace-event-perl.c
 delete mode 100644 tools/perf/util/scripting-engines/trace-event-python.c
 delete mode 100644 tools/perf/util/trace-event-scripting.c
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
author reviewer
#208 lore raw

PATCH v3 1/49 perf python: Update syscall helpers and expose arch_strerrno

Update the PyArg_ParseTupleAndKeywords format string for pyrf__syscall_name() and pyrf__syscall_id() from "i|$i" to "i|i" (and "s|$i" to "s|i"), allowing elf_machine to be passed as an optional positional argument as well as a keyword argument. Update syscall_id() and syscall_name() in perf.pyi to match, and set the return type of syscall_name() to Optional[str] as unknown syscall numbers return None.

Also expose perf_env__arch_strerrno() as perf.arch_strerrno(err, elf_machine=None) so standalone Python scripts can resolve architecture-specific errno names directly from the auto-generated trace beauty tables instead of maintaining duplicate errno dictionaries in Python. Tighten the macro pattern in arch_errno_names.sh to E[A-Z0-9]+ so compiler -D flags containing underscores (such as LLVM's -DEXPERIMENTAL_KEY_INSTRUCTIONS=1) do not shadow EPERM (1), and add EM_SPARC32PLUS alongside EM_SPARC and EM_SPARCV9.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/perf.pyi                  | 16 +++++-
 tools/perf/trace/beauty/arch_errno_names.sh |  4 +-
 tools/perf/util/python.c                    | 60 ++++++++++++++++++++-
 3 files changed, 74 insertions(+), 6 deletions(-)
tools/perf/python/perf.pyi +14 -2
index 58b0b3ed819d..c390e17708b1 100644
@@ -20,7 +20,7 @@ def metrics() -> List[Dict[str, Union[str, List[str]]]]:
     """
     ...
 
-def syscall_name(id: int, *, elf_machine: Optional[int] = None) -> str:
+def syscall_name(id: int, elf_machine: Optional[int] = None) -> Optional[str]:
     """Convert a syscall number to its name.
 
     Args:
@@ -32,7 +32,7 @@ def syscall_name(id: int, *, elf_machine: Optional[int] = None) -> str:
     """
     ...
 
-def syscall_id(name: str, *, elf_machine: Optional[int] = None) -> int:
+def syscall_id(name: str, elf_machine: Optional[int] = None) -> int:
     """Convert a syscall name to its number.
 
     Args:
@@ -44,6 +44,18 @@ def syscall_id(name: str, *, elf_machine: Optional[int] = None) -> int:
     """
     ...
 
+def arch_strerrno(err: int, elf_machine: Optional[int] = None) -> Optional[str]:
+    """Convert an errno number to its symbolic name.
+
+    Args:
+        err: The errno number (positive or negative).
+        elf_machine: Optional ELF machine type.
+
+    Returns:
+        The symbolic name of the errno, or None if unknown.
+    """
+    ...
+
 def parse_events(
     event_string: str,
     cpus: Optional[cpu_map] = None,
tools/perf/trace/beauty/arch_errno_names.sh +2 -2
index 8751bfa4a2b2..e75cd5bc4328 100755
@@ -46,7 +46,7 @@ process_arch()
 	asm_errno=$(asm_errno_file "$arch")
 
 	$gcc $CFLAGS $include_path -E -dM -x c $asm_errno \
-		|grep -hE '^#define[[:blank:]]+(E[^[:blank:]]+)[[:blank:]]+([[:digit:]]+).*' \
+		|grep -hE '^#define[[:blank:]]+(E[A-Z0-9]+)[[:blank:]]+([[:digit:]]+).*' \
 		|awk '{ print $2","$3; }' \
 		|sort -t, -k2 -nu \
 		|IFS=, create_errno_lookup_func "$arch"
@@ -69,7 +69,7 @@ arch_to_e_machine()
 	riscv)      printf '\tcase EM_RISCV:\n' ;;
 	s390)       printf '\tcase EM_S390:\n' ;;
 	sh)         printf '\tcase EM_SH:\n' ;;
-	sparc)      printf '\tcase EM_SPARC:\n\tcase EM_SPARCV9:\n' ;;
+	sparc)      printf '\tcase EM_SPARC:\n\tcase EM_SPARC32PLUS:\n\tcase EM_SPARCV9:\n' ;;
 	x86)        printf '\tcase EM_386:\n\tcase EM_X86_64:\n' ;;
 	xtensa)     printf '\tcase EM_XTENSA:\n' ;;
 	esac
tools/perf/util/python.c +58 -2

105 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
author reviewer
#210 lore raw

PATCH v3 2/49 perf python: Update callchain stubs and session thread lookup

In reply to Ian Rogers

Update pyrf_session__find_thread() in util/python.c to accept an optional tid parameter (defaulting to pid) and return None instead of raising TypeError when a thread is not found in the session machines.

Update perf.pyi to match:
- Set session.find_thread() signature to (pid: int, tid: int = -1) -> Optional[thread].
- Update callchain_node.symbol and callchain_node.dso to str (the C getters return "[unknown]" fallback strings rather than None) and add __iter__() to callchain.

Also update treport.py to handle Optional[thread] returned by session.find_thread().

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/perf.pyi   |  7 ++++---
 tools/perf/python/treport.py |  2 +-
 tools/perf/util/python.c     | 19 +++++++++++--------
 3 files changed, 16 insertions(+), 12 deletions(-)
tools/perf/python/perf.pyi +4 -3
index c390e17708b1..92aad84ca542 100644
@@ -332,13 +332,14 @@ class branch_stack:
 class callchain_node:
     """Represents a frame in the callchain."""
     ip: int
-    symbol: Optional[str]
-    dso: Optional[str]
+    symbol: str
+    dso: str
 
 class callchain:
     """Sequence of callchain frames."""
     def __len__(self) -> int: ...
     def __getitem__(self, index: int) -> callchain_node: ...
+    def __iter__(self) -> Iterator[callchain_node]: ...
 
 class stat_event(_sample_members):
     """Represents a stat event from perf."""
@@ -453,7 +454,7 @@ class session:
     def process_events(self) -> None:
         """Process all events in the session."""
         ...
-    def find_thread(self, pid: int) -> thread:
+    def find_thread(self, pid: int, tid: int = -1, /) -> Optional[thread]:
         """Returns the thread associated with a pid."""
         ...
 
tools/perf/python/treport.py +1 -1
index 43542599a884..786b852f471c 100755
@@ -92,7 +92,7 @@ class ProfileNode:
         try:
             assert session
             thread = session.find_thread(sample.sample_tid)
-            comm = thread.comm()
+            comm = (thread.comm() if thread else None) or f"unknown ({pid})"
         except Exception:
             comm = f"unknown ({pid})"
 
tools/perf/util/python.c +11 -8
index 6ff0b4ed0bef..9e5ecc2d9abf 100644
@@ -3914,26 +3914,29 @@ static PyObject *pyrf_session__find_thread(struct pyrf_session *psession, PyObje
 	struct machine *machine;
 	struct thread *thread = NULL;
 	PyObject *result;
-	int pid;
+	int pid, tid = -1;
 
 	CHECK_INITIALIZED(psession->session, "session");
 
-	if (!PyArg_ParseTuple(args, "i", &pid))
+	if (!PyArg_ParseTuple(args, "i|i", &pid, &tid))
 		return NULL;
 
+	if (tid == -1)
+		tid = pid;
+
+	/* Look up the thread in the host machine first, then fall back to guest machines. */
 	machine = &psession->session->machines.host;
-	thread = machine__find_thread(machine, pid, pid);
+	thread = machine__find_thread(machine, pid, tid);
 
 	if (!thread) {
 		machine = perf_session__find_machine(psession->session, pid);
 		if (machine)
-			thread = machine__find_thread(machine, pid, pid);
+			thread = machine__find_thread(machine, pid, tid);
 	}
 
-	if (!thread) {
-		PyErr_Format(PyExc_TypeError, "Failed to find thread %d", pid);
-		return NULL;
-	}
+	/* Return None rather than raising TypeError when a PID/TID is not known. */
+	if (!thread)
+		Py_RETURN_NONE;
 	result = pyrf_thread__from_thread(thread);
 	thread__put(thread);
 	return result;
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog