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 5 of 7

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

PATCH v3 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..a43b003634fd
@@ -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 BaseException:
+        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.56.0.rc1.310.g51773c2048-goog
author reviewer
#298 lore raw

PATCH v3 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              |   42 -
 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, 4 insertions(+), 10636 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 +1 -25
index 4ee7393a39f9..66291d868bc0 100644
@@ -793,27 +793,6 @@ 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
 else
@@ -844,8 +823,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 +834,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 d0754255c62a..36c251b98919 100644
@@ -905,11 +905,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 -42

42 lines not on this page · 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.56.0.rc1.310.g51773c2048-goog

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

author reviewer
#299 lore raw

PATCH v3 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 (python/*.py), type stubs (python/perf.pyi), and the compiled perf extension module (python/perf*.so, when built) 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         | 184 +++++++++++++++--------
 12 files changed, 156 insertions(+), 103 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 66291d868bc0..e469c5ee5424 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
 
@@ -801,13 +801,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
 
@@ -820,10 +819,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 36c251b98919..0daac2f9e349 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
@@ -903,11 +903,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' && \
 		$(INSTALL) python/perf.pyi -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python' && \
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 +118 -66

229 lines not on this page · raw message

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

PATCH v3 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.
- Update the Python script shell tests (test_arm_coresight_disasm.sh, test_task_analyzer.sh, and test_*_python.sh) to invoke scripts via 'perf script <script>' rather than running the Python interpreter directly on the script file path.

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                   | 986 +++++++++---------
 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 -
 .../coresight/test_arm_coresight_disasm.sh    |  14 +-
 tools/perf/tests/shell/script_perl.sh         | 102 --
 .../shell/test_check_perf_trace_python.sh     |   2 +-
 .../shell/test_compaction_times_python.sh     |   6 +-
 .../test_event_analyzing_sample_python.sh     |   3 +-
 .../shell/test_export_to_postgresql_python.sh |   8 +-
 .../shell/test_export_to_sqlite_python.sh     |   4 +-
 .../test_failed_syscalls_by_pid_python.sh     |   7 +-
 .../shell/test_failed_syscalls_python.sh      |   2 +-
 .../tests/shell/test_flamegraph_python.sh     |   8 +-
 .../shell/test_futex_contention_python.sh     |   2 +-
 tools/perf/tests/shell/test_gecko_python.sh   |   2 +-
 .../shell/test_intel_pt_events_python.sh      |   2 +-
 .../tests/shell/test_mem_phys_addr_python.sh  |   3 +-
 .../shell/test_net_dropmonitor_python.sh      |   2 +-
 .../tests/shell/test_netdev_times_python.sh   |   2 +-
 .../tests/shell/test_powerpc_hcalls_python.sh |   2 +-
 .../tests/shell/test_rw_by_file_python.sh     |   2 +-
 .../perf/tests/shell/test_rw_by_pid_python.sh |   2 +-
 tools/perf/tests/shell/test_rwtop_python.sh   |   2 +-
 .../shell/test_sched_migration_python.sh      |   2 +-
 tools/perf/tests/shell/test_sctop_python.sh   |   4 +-
 .../tests/shell/test_stackcollapse_python.sh  |   6 +-
 .../perf/tests/shell/test_stat_cpi_python.sh  |   4 +-
 .../test_syscall_counts_by_pid_python.sh      |   7 +-
 .../tests/shell/test_syscall_counts_python.sh |   4 +-
 tools/perf/tests/shell/test_task_analyzer.sh  |  58 +-
 .../tests/shell/test_wakeup_latency_python.sh |   4 +-
 tools/perf/ui/browsers/scripts.c              | 161 +--
 tools/perf/util/Build                         |   3 -
 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 +-
 72 files changed, 579 insertions(+), 3610 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 e469c5ee5424..fa1d984772aa 100644
@@ -1273,7 +1273,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 0daac2f9e349..52f8247e01a0 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.
 #
@@ -486,11 +485,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
@@ -895,14 +889,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 +494 -492

1,281 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/coresight/test_arm_coresight_disasm.sh +4 -10
index 2c04249a3496..d298b18154b2 100755
@@ -20,13 +20,6 @@ skip_if_no_cs_etm_event || exit 2
 set -e
 glb_err=1
 
-# Relative path works whether it's installed or running from repo
-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 || {
@@ -56,8 +49,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
-	$PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
-		-k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
+	perf script -i ${perfdata} arm-cs-trace-disasm -- \
+		-d --stop-sample=2 -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
 	grep -q -E ${branch_search} ${file}
 	echo "Found kernel branches"
 else
@@ -68,7 +61,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
-$PYTHON ${script_path} -i ${perfdata} -d --stop-sample=2 2> /dev/null > ${file}
+perf script -i ${perfdata} arm-cs-trace-disasm -- \
+	-d --stop-sample=2 2> /dev/null > ${file}
 grep -q -E ${branch_search} ${file}
 echo "Found userspace branches"
 
tools/perf/tests/shell/script_perl.sh deleted +0 -102

102 lines not on this page · raw message

tools/perf/tests/shell/test_check_perf_trace_python.sh +1 -1
index a346e45eb18c..92cc7b1f0033 100755
@@ -68,7 +68,7 @@ test_file_mode() {
 	fi
 
 	# Run the script
-	if ! "$PYTHON" "$script_path" -i "${temp_data}" > "${temp_out}"; then
+	if ! perf script check-perf-trace -i "${temp_data}" > "${temp_out}"; then
 		echo "File mode test failed."
 		err=1
 	elif ! grep -q "in trace_begin" "${temp_out}" || \
tools/perf/tests/shell/test_compaction_times_python.sh +3 -3
index ad950f3602b5..50397afaafed 100755
@@ -58,7 +58,7 @@ test_file_mode() {
 	fi
 
 	# Run the script with some filters to validate filtering logic
-	if ! "$PYTHON" "$script_path" -i "${temp_data}" > "${temp_out}"; then
+	if ! perf script compaction-times -i "${temp_data}" > "${temp_out}"; then
 		echo "File mode default test failed."
 		err=1
 	elif ! grep -q "total:" "${temp_out}"; then
@@ -66,7 +66,7 @@ test_file_mode() {
 		err=1
 	fi
 
-	if ! "$PYTHON" "$script_path" -i "${temp_data}" "0-0" > "${temp_out}"; then
+	if ! perf script compaction-times -i "${temp_data}" "0-0" > "${temp_out}"; then
 		echo "File mode strict PID filter test failed."
 		err=1
 	elif ! grep -q "total:" "${temp_out}"; then
@@ -74,7 +74,7 @@ test_file_mode() {
 		err=1
 	fi
 
-	if ! "$PYTHON" "$script_path" -i "${temp_data}" "sleep" > "${temp_out}"; then
+	if ! perf script compaction-times -i "${temp_data}" "sleep" > "${temp_out}"; then
 		echo "File mode comm filter test failed."
 		err=1
 	elif ! grep -q "total:" "${temp_out}"; then
tools/perf/tests/shell/test_event_analyzing_sample_python.sh +2 -1
index 4678edfd9e5b..a7a8ded003db 100755
@@ -45,7 +45,8 @@ test_file_mode() {
 	fi
 
 	# Run the script
-	if ! "$PYTHON" "$script_path" -i "${temp_data}" -d "${temp_db}" > "${temp_dir}/perf.out" 2>&1; then
+	if ! perf script event_analyzing_sample -i "${temp_data}" \
+		-d "${temp_db}" > "${temp_dir}/perf.out" 2>&1; then
 		echo "File mode test failed."
 		err=1
 	elif ! grep -q "Statistics about the general events" "${temp_dir}/perf.out" || \
tools/perf/tests/shell/test_export_to_postgresql_python.sh +5 -3
index ce13a1aa7cd7..350813466700 100755
@@ -54,7 +54,8 @@ test_file_mode() {
 	echo "Testing export-to-postgresql.py..."
 
 	# Verify that invalid URI / key-value database names are rejected
-	if "$PYTHON" "$script_path" -i /dev/null -o "dbname=foo host=evil" >/dev/null 2>&1; then
+	if perf script export-to-postgresql -i /dev/null \
+		-o "dbname=foo host=evil" >/dev/null 2>&1; then
 		echo "Connection parameter injection check failed."
 		err=1
 	fi

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

tools/perf/tests/shell/test_export_to_sqlite_python.sh +2 -2
index 59280d372399..d3c5e22a0754 100755

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

tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh +4 -3
index 5bbd1a0adb1e..215372d4e1c5 100755

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

tools/perf/tests/shell/test_failed_syscalls_python.sh +1 -1
index 0cabb127d7e9..861c2ba71c0a 100755

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

tools/perf/tests/shell/test_flamegraph_python.sh +4 -4
index 3733d53b916b..08f4f6598ed3 100755

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

tools/perf/tests/shell/test_futex_contention_python.sh +1 -1
index 26b13d263d20..a8846ed68ac2 100755

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

tools/perf/tests/shell/test_gecko_python.sh +1 -1
index eac9b2f8e3c7..de1d7fcf88b7 100755

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

tools/perf/tests/shell/test_intel_pt_events_python.sh +1 -1
index 0d397792ced8..b5c3173fa2db 100755

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

tools/perf/tests/shell/test_mem_phys_addr_python.sh +2 -1
index d094a0611a24..451692d5e089 100755

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

tools/perf/tests/shell/test_net_dropmonitor_python.sh +1 -1
index d00be255d259..05056a897bc9 100755

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

tools/perf/tests/shell/test_netdev_times_python.sh +1 -1
index bb0f8c30207f..393bf446efb2 100755

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

tools/perf/tests/shell/test_powerpc_hcalls_python.sh +1 -1
index 5316c28f243f..19569c6d3613 100755

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

tools/perf/tests/shell/test_rw_by_file_python.sh +1 -1
index 98218bf8a39e..f121597f418f 100755

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

tools/perf/tests/shell/test_rw_by_pid_python.sh +1 -1
index da8b065d43de..41eaa29097a2 100755

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

tools/perf/tests/shell/test_rwtop_python.sh +1 -1
index 062fca2dad14..05897e384702 100755

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

tools/perf/tests/shell/test_sched_migration_python.sh +1 -1
index f243e86c6700..75465bcd3791 100755

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

tools/perf/tests/shell/test_sctop_python.sh +2 -2
index cd38cdd4794c..007f2584cce6 100755

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

tools/perf/tests/shell/test_stackcollapse_python.sh +3 -3
index a3b6c917e842..ce0f80407eec 100755

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

tools/perf/tests/shell/test_stat_cpi_python.sh +2 -2
index 0960afa4c797..fe7562307634 100755

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

tools/perf/tests/shell/test_syscall_counts_by_pid_python.sh +4 -3
index 9f2ad27751a2..45bbdc554c8f 100755

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

tools/perf/tests/shell/test_syscall_counts_python.sh +2 -2
index 0c4b75499209..310e05d399f9 100755

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

tools/perf/tests/shell/test_task_analyzer.sh +24 -34

58 lines not on this page · raw message

tools/perf/tests/shell/test_wakeup_latency_python.sh +2 -2
index fc39ac117609..cb2571449168 100755

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

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

161 lines not on this page · raw message

tools/perf/util/Build +0 -3
index 3ea888f4210a..1d63751af4f2 100644

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

tools/perf/util/scripting-engines/Build deleted +0 -5
deleted file mode 100644
index 3f1dc10526f8..000000000000

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

tools/perf/util/scripting-engines/trace-event-perl.c deleted +0 -770

770 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.56.0.rc1.310.g51773c2048-goog

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

author reviewer
#304 lore raw

PATCH v3 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 | 797 ++++--------------
 tools/perf/Documentation/perf-script.txt      |  82 +-
 tools/perf/Documentation/tips.txt             |   1 -
 5 files changed, 162 insertions(+), 950 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 +151 -646

827 lines not on this page · raw message

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

118 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.56.0.rc1.310.g51773c2048-goog
#219 lore raw

Re: [PATCH v3 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
#217 lore raw

Re: [PATCH v3 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
#221 lore raw

Re: [PATCH v3 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 22 lines

Add module docstrings to counting.py, tracepoint.py, and twatch.py. This satisfies pylint's missing-module-docstring check and prepares these existing standalone scripts in tools/perf/python/ for the later commit in this series ("perf script: Support standalone scripts and remove embedded scripting"), which updates 'perf script -l' to scan tools/perf/python/ and display each script's module docstring:

```
$ 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 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=7
#227 lore raw

Re: [PATCH v3 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
#215 lore raw

Re: [PATCH v3 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 12 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).
- Clear session = None in a finally block after session.process_events() to break the C perf_session reference cycle before launching the TUI.

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
#211 lore raw

Re: [PATCH v3 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
#209 lore raw

Re: [PATCH v3 01/49] perf python: Update syscall helpers and expose arch_strerrno

In reply to Ian Rogers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 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.

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.

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=1
#231 lore raw

Re: [PATCH v3 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 21 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.

Also update tools/perf/tests/shell/lib/setup_python.sh to:
- Avoid x-prefix string comparisons.
- Export PYTHON and resolve PYTHONPATH and PERF_EXEC_PATH from BASH_SOURCE[0], ../../../python (for nested test directories such as coresight/), and the directory containing the perf binary so out-of-tree builds and standalone perf python scripts work seamlessly.

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
#229 lore raw

Re: [PATCH v3 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
#233 lore raw

Re: [PATCH v3 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 13 lines

Commit 168910d0f937 ("perf build: Add mypy build tests") added a compile-time mypy type check for Python files in tools/perf, but left it opt-in via MYPY=1.

Now that mypy issues across tools/perf/python, tools/perf/pmu-events, and tools/perf/tests/shell/lib have been cleaned up, enable the mypy check by default when mypy is installed on the system, with an opt-out via NO_MYPY=1 (matching NO_SHELLCHECK=1 and other perf build feature flags). Also configure MYPYPATH in Makefile.perf so scripts can resolve perf.pyi type stubs and local modules, pass --ignore-missing-imports so optional third-party packages do not fail default builds, and remove *.mypy_log files during make clean.

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=13
#237 lore raw

Re: [PATCH v3 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 9 lines

Install the newly ported standalone Python scripts (python/*.py), type stubs (python/perf.pyi), and compiled perf Python extension module (python/perf*.so, when built) into $(perfexec_instdir_SQ)/python alongside the legacy scripts during the transition phase so that newly ported scripts can import perf directly from 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
#213 lore raw

Re: [PATCH v3 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
#241 lore raw

Re: [PATCH v3 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
#235 lore raw

Re: [PATCH v3 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 17 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 NO_SHELLCHECK=1, NO_MYPY=1, and 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 --ignored-modules=textual,setproctitle) 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
#245 lore raw

Re: [PATCH v3 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
#223 lore raw

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

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

Expose destination address location components (addr_dso, addr_symbol, addr_sym_offset), branch/transaction metrics (branch_type, in_tx, flags, transaction), machine_pid, vcpu, sym_offset, and context_switch callbacks directly to the Python extension.

Also include supporting fixes and infrastructure updates in util/python.c and perf.pyi:
- Support fetching sym_offset on resolved sample symbols.
- Populate sample machine_pid and vcpu from evlist__id2sid() when perf_guest is enabled, and resolve samples against the matching guest or host machine.
- Expose next_prev_pid and next_prev_tid on switch_event only for PERF_RECORD_SWITCH_CPU_WIDE records (returning None otherwise).
- Wire up tracing_data in pyrf_session__new() and update perf.pyi type stubs (including srccode() and insn() return types).

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=8
#239 lore raw

Re: [PATCH v3 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
#225 lore raw

Re: [PATCH v3 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 24 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, populating frame->map and frame->sym so call_path entries expose resolved symbol and DSO names).
- 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 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=9
#243 lore raw

Re: [PATCH v3 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
#251 lore raw

Re: [PATCH v3 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
#247 lore raw

Re: [PATCH v3 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
#249 lore raw

Re: [PATCH v3 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
#255 lore raw

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

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

Port failed-syscalls-by-pid.py to a standalone script in tools/perf/python/ using the perf module:
- Resolve architecture-aware syscall names using perf.syscall_name(id, session.e_machine) instead of host python-audit tables, supporting cross-architecture perf.data files without external dependencies.
- Translate negative syscall return values into symbolic E* error names using perf.arch_strerrno(ret, session.e_machine), leveraging the auto-generated trace beauty architecture errno tables.
- Encapsulate aggregation in a SyscallAnalyzer class using collections.defaultdict and add argparse filtering by comm, PID, and -i/--input.

Quoted 5 lines

Add a shell test (test_failed_syscalls_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=24
#253 lore raw

Re: [PATCH v3 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
#257 lore raw

Re: [PATCH v3 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
#268 lore raw

Re: [PATCH v3 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
#262 lore raw

Re: [PATCH v3 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
#259 lore raw

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

In reply to Ian Rogers
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf python: sctop silently drops legacy syscalls:sys_enter events --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf python: sctop silently drops legacy syscalls:sys_enter events --

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

perf python: Port sctop to perf module

This commit ports the sctop.py script to a standalone script in tools/perf/python/ using an SCTopAnalyzer class structure. It adds support for both offline analysis and live monitoring, resolves architecture-aware syscall names, and replaces unsafe signal mutation and subshell spawning with threading locks and ANSI escape sequences.

Quoted 5 lines
tools/perf/python/sctop.py new file +0 -0
new file mode 100755
index 0000000000000..fb709206993a1

[ ... ]

Quoted 5 lines
tools/perf/python/sctop.py new file +5 -0
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Collect syscall events."""
+        if not self.offline and sample.sample_pid == self.own_pid:
+            return
+

tools/perf/python/sctop.py line 74

Quoted 10 lines
tools/perf/python/sctop.py new file +10 -0
+        name = str(sample.evsel)
+        # raw_syscalls:sys_enter exposes the syscall number as 'id', whereas
+        # per-syscall syscalls:sys_enter_* tracepoints expose '__syscall_nr' (or 'nr')
+        # and may have an unrelated syscall argument named 'id'.
+        if name.startswith("evsel(raw_syscalls:sys_enter"):
+            syscall_id = getattr(sample, "id", -1)
+        elif name.startswith("evsel(syscalls:sys_enter"):
+            syscall_id = getattr(sample, "__syscall_nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "nr", -1)

[Severity: Medium]
Will this silently drop legacy syscalls:sys_enter events?

When processing an event named evsel(syscalls:sys_enter), it matches this elif block intended for per-syscall tracepoints like syscalls:sys_enter_openat. This path expects the syscall number in __syscall_nr or nr.

However, the legacy syscalls:sys_enter tracepoint (used by older kernels like RHEL6) stores its syscall number precisely in the id field. Since this block avoids fetching id, syscall_id will evaluate to -1, causing the script to silently drop the event and report empty totals.

Could the prefix match be updated to distinguish the legacy tracepoint from the per-syscall variants, so we don't drop events from older perf.data files?

Quoted 2 lines
tools/perf/python/sctop.py new file +2 -0
+        else:
+            syscall_id = -1
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
#272 lore raw

Re: [PATCH v3 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
#264 lore raw

Re: [PATCH v3 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
#266 lore raw

Re: [PATCH v3 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 16 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.
- 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.
- 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
#276 lore raw

Re: [PATCH v3 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
#270 lore raw

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

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

Port task-analyzer.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/ refactored into a class-based architecture.

Improvements compared to the legacy script:
- Support both offline perf.data file analysis (using perf.session) and live trace capture (using evlist.read_on_cpu), accessing sched:sched_switch tracepoint fields directly from sample objects.
- Automatically disable ANSI terminal color escape sequences when --csv or --csv-summary is enabled so CSV column headers ('Comm,', 'Time Out-Out,', etc.) are never polluted by color codes when running interactively on a TTY (e.g. as root).
- Sanitize non-printable characters and leading CSV formula characters (=, +, -, @) in task comm strings.
- Emit a one-time warning to stderr if sched:sched_switch samples are missing tracepoint fields (such as when recorded without libtraceevent support).

Quoted 4 lines

Update test_task_analyzer.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=31
#282 lore raw

Re: [PATCH v3 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
#274 lore raw

Re: [PATCH v3 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 15 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.
- Track wakeup timestamps per-task (keyed by the wakee PID on sched:sched_wakeup and matched against next_pid on sched:sched_switch) rather than per-CPU, avoiding mixed latencies when multiple wakeups occur on a CPU before a context switch.
- 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
#278 lore raw

Re: [PATCH v3 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
#280 lore raw

Re: [PATCH v3 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
#286 lore raw

Re: [PATCH v3 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
#290 lore raw

Re: [PATCH v3 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
#288 lore raw

Re: [PATCH v3 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
#284 lore raw

Re: [PATCH v3 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 6 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
#297 lore raw

Re: [PATCH v3 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
#292 lore raw

Re: [PATCH v3 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
#305 lore raw

Re: [PATCH v3 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
#294 lore raw

Re: [PATCH v3 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 setup_db() and 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
#300 lore raw

Re: [PATCH v3 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 (python/*.py), type stubs (python/perf.pyi), and the compiled perf extension module (python/perf*.so, when built) 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.

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=47
#302 lore raw

Re: [PATCH v3 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 1 potential issue(s) to consider: - [Medium] perf script: TOCTOU race condition in signal handler allows sending signals to recycled PIDs --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf script: TOCTOU race condition in signal handler allows sending signals to recycled PIDs --

commit 1ed2e4983f9ede9efdfc6d59669cdd2e466dc05b
Author: Ian Rogers <[email protected]>
Subject: perf script: Support standalone scripts and remove embedded scripting

This commit refactors perf script to launch standalone scripts directly via fork and execvp, while removing the legacy embedded Perl and Python scripting engines. It updates script discovery to prioritize the system python directory and updates shell tests to use the new invocation style.

Quoted 3 lines
tools/perf/builtin-script.c +0 -0

[ ... ]

Quoted 5 lines
tools/perf/builtin-script.c +5 -0
+static volatile sig_atomic_t script_child_pid;
+
+static void forward_script_sig(int sig, siginfo_t *info, void *ucontext __maybe_unused)
+{
+	/*

tools/perf/builtin-script.c static int parse_callret_trace(const struct option *opt __maybe_unused, · line 4182

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+	 * Terminal-generated signals (Ctrl-C / Ctrl-\) have si_code > 0
+	 * (SI_KERNEL) and are already delivered by the TTY driver to the
+	 * entire foreground process group, including the child script.
+	 * Signals sent directly to the perf process via kill(2) / sigqueue(3)
+	 * have si_code <= 0 (SI_USER, SI_QUEUE, etc.) and must be forwarded
+	 * to the child script so it can catch KeyboardInterrupt / SIGTERM
+	 * and flush its output before exiting.
+	 */
+	if (script_child_pid > 0 && (sig == SIGTERM || !info || info->si_code <= 0))
+		kill(script_child_pid, sig);

[Severity: Medium]
Does this signal handler risk targeting a reused PID?

The script_child_pid variable is not updated atomically with the waitpid() call reaping the process, which creates a race window.

If the parent is preempted right after waitpid() returns but before script_child_pid is cleared, the system could recycle the PID. This handler might then trigger a kill() on an unrelated, newly created process.

Quoted 1 line

+}

[ ... ]

Quoted 8 lines
tools/perf/builtin-script.c +8 -0
+		} else { /* parent */
+			int status = 0;
+
+			script_child_pid = pid;
+			sigprocmask(SIG_SETMASK, &old_mask, NULL);
+			while (waitpid(pid, &status, 0) != pid) {
+				if (errno == EINTR)
+					continue;

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

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+				pr_err("failed to wait for script '%s': %s\n",
+				       script_name, strerror(errno));
+				err = -errno;
+				script_child_pid = 0;
+				sigaction(SIGINT, &old_int, NULL);
+				sigaction(SIGQUIT, &old_quit, NULL);
+				sigaction(SIGTERM, &old_term, NULL);
+				goto out;
+			}
+			script_child_pid = 0;

[Severity: Medium]
Is there a way to safely synchronize the clearing of script_child_pid with the reaping of the child process?

The child process is fully reaped as soon as waitpid() successfully returns here in cmd_script().

If the thread is suspended or preempted exactly at this point, script_child_pid remains set to the old PID while the system is free to reassign that PID.

If a signal like SIGTERM arrives during this window, forward_script_sig() will read the stale script_child_pid and send it to the new process holding that PID.

Quoted 4 lines
tools/perf/builtin-script.c +4 -0
+			sigaction(SIGINT, &old_int, NULL);
+			sigaction(SIGQUIT, &old_quit, NULL);
+			sigaction(SIGTERM, &old_term, NULL);
+			if (WIFEXITED(status))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=48
author reviewer
#260 lore raw

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

In reply to [email protected]
Quoted 46 lines · On Wed, Sep 23, 2026 at 11:35 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf python: sctop silently drops legacy syscalls:sys_enter events --

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

perf python: Port sctop to perf module

This commit ports the sctop.py script to a standalone script in tools/perf/python/ using an SCTopAnalyzer class structure. It adds support for both offline analysis and live monitoring, resolves architecture-aware syscall names, and replaces unsafe signal mutation and subshell spawning with threading locks and ANSI escape sequences.

Quoted 5 lines
tools/perf/python/sctop.py new file +0 -0
new file mode 100755
index 0000000000000..fb709206993a1

[ ... ]

Quoted 5 lines
tools/perf/python/sctop.py new file +5 -0
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Collect syscall events."""
+        if not self.offline and sample.sample_pid == self.own_pid:
+            return
+

tools/perf/python/sctop.py line 74

Quoted 10 lines
tools/perf/python/sctop.py new file +10 -0
+        name = str(sample.evsel)
+        # raw_syscalls:sys_enter exposes the syscall number as 'id', whereas
+        # per-syscall syscalls:sys_enter_* tracepoints expose '__syscall_nr' (or 'nr')
+        # and may have an unrelated syscall argument named 'id'.
+        if name.startswith("evsel(raw_syscalls:sys_enter"):
+            syscall_id = getattr(sample, "id", -1)
+        elif name.startswith("evsel(syscalls:sys_enter"):
+            syscall_id = getattr(sample, "__syscall_nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "nr", -1)

[Severity: Medium]
Will this silently drop legacy syscalls:sys_enter events?

When processing an event named evsel(syscalls:sys_enter), it matches this elif block intended for per-syscall tracepoints like syscalls:sys_enter_openat. This path expects the syscall number in __syscall_nr or nr.

Quoted 8 lines

However, the legacy syscalls:sys_enter tracepoint (used by older kernels like RHEL6) stores its syscall number precisely in the id field. Since this block avoids fetching id, syscall_id will evaluate to -1, causing the script to silently drop the event and report empty totals.

Could the prefix match be updated to distinguish the legacy tracepoint from the per-syscall variants, so we don't drop events from older perf.data files?

So syscalls:sys_enter was renamed to raw_syscalls:sys_enter in Linux 2.6.35 (commit 308fd7dd9151), and sctop in live mode records raw_syscalls:sys_enter. It seems unlikely that supporting pre-Linux 2.6.35 is a real concern.

That said, distinguishing per-syscall syscalls:sys_enter_* (with the trailing underscore) from bare syscalls:sys_enter is straightforward — checking name.startswith("evsel(syscalls:sys_enter_") first and falling back to sample.id for raw_syscalls:sys_enter / syscalls:sys_enter handles pre-2.6.35 perf.data files as well. I can fold that into v4 if needed, or send it as a small follow-up patch.

Thanks,
Ian

Quoted 5 lines

tools/perf/python/sctop.py

Quoted 2 lines
tools/perf/python/sctop.py new file +2 -0
+        else:
+            syscall_id = -1

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26

author reviewer
#303 lore raw

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

In reply to [email protected]
Quoted 39 lines · On Wed, Sep 23, 2026 at 11:49 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf script: TOCTOU race condition in signal handler allows sending signals to recycled PIDs --

commit 1ed2e4983f9ede9efdfc6d59669cdd2e466dc05b
Author: Ian Rogers <[email protected]>
Subject: perf script: Support standalone scripts and remove embedded scripting

This commit refactors perf script to launch standalone scripts directly via fork and execvp, while removing the legacy embedded Perl and Python scripting engines. It updates script discovery to prioritize the system python directory and updates shell tests to use the new invocation style.

Quoted 3 lines
tools/perf/builtin-script.c +0 -0

[ ... ]

Quoted 5 lines
tools/perf/builtin-script.c +5 -0
+static volatile sig_atomic_t script_child_pid;
+
+static void forward_script_sig(int sig, siginfo_t *info, void *ucontext __maybe_unused)
+{
+     /*

tools/perf/builtin-script.c static int parse_callret_trace(const struct option *opt __maybe_unused, · line 4182

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+      * Terminal-generated signals (Ctrl-C / Ctrl-\) have si_code > 0
+      * (SI_KERNEL) and are already delivered by the TTY driver to the
+      * entire foreground process group, including the child script.
+      * Signals sent directly to the perf process via kill(2) / sigqueue(3)
+      * have si_code <= 0 (SI_USER, SI_QUEUE, etc.) and must be forwarded
+      * to the child script so it can catch KeyboardInterrupt / SIGTERM
+      * and flush its output before exiting.
+      */
+     if (script_child_pid > 0 && (sig == SIGTERM || !info || info->si_code <= 0))
+             kill(script_child_pid, sig);

[Severity: Medium]
Does this signal handler risk targeting a reused PID?

Quoted 6 lines

The script_child_pid variable is not updated atomically with the waitpid() call reaping the process, which creates a race window.

If the parent is preempted right after waitpid() returns but before script_child_pid is cleared, the system could recycle the PID. This handler might then trigger a kill() on an unrelated, newly created process.

In practice this window is small (O(a single instruction)) right before perf script exits, and it would require both PID wrap-around/reuse and an external kill() directed at perf's PID during that boundary.

However, we can eliminate the window completely by waiting with waitid(P_PID, pid, &info, WEXITED | WNOWAIT) so the child PID remains unreaped while we clear script_child_pid = 0 and restore sigaction, and then calling waitpid(pid, &status, 0) to reap the zombie. I'm happy to include that if there is a v4 or as a follow-up cleanup.

Thanks,
Ian

Quoted 43 lines
Quoted 1 line

+}

[ ... ]

Quoted 8 lines
tools/perf/builtin-script.c +8 -0
+             } else { /* parent */
+                     int status = 0;
+
+                     script_child_pid = pid;
+                     sigprocmask(SIG_SETMASK, &old_mask, NULL);
+                     while (waitpid(pid, &status, 0) != pid) {
+                             if (errno == EINTR)
+                                     continue;

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

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+                             pr_err("failed to wait for script '%s': %s\n",
+                                    script_name, strerror(errno));
+                             err = -errno;
+                             script_child_pid = 0;
+                             sigaction(SIGINT, &old_int, NULL);
+                             sigaction(SIGQUIT, &old_quit, NULL);
+                             sigaction(SIGTERM, &old_term, NULL);
+                             goto out;
+                     }
+                     script_child_pid = 0;

[Severity: Medium]
Is there a way to safely synchronize the clearing of script_child_pid with the reaping of the child process?

The child process is fully reaped as soon as waitpid() successfully returns here in cmd_script().

If the thread is suspended or preempted exactly at this point, script_child_pid remains set to the old PID while the system is free to reassign that PID.

If a signal like SIGTERM arrives during this window, forward_script_sig() will read the stale script_child_pid and send it to the new process holding that PID.

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

Quoted 4 lines
tools/perf/builtin-script.c +4 -0
+                     sigaction(SIGINT, &old_int, NULL);
+                     sigaction(SIGQUIT, &old_quit, NULL);
+                     sigaction(SIGTERM, &old_term, NULL);
+                     if (WIFEXITED(status))

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=48

reviewer
#185 lore raw

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

In reply to Ian Rogers

On Wed, Sep 23, 2026 at 06:40:57AM -0700, Ian Rogers wrote:

[...]

Quoted 37 lines
Quoted 24 lines
Quoted 12 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.

Quoted 3 lines

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.

Sorry for the confusion. I should have made this clearer in my previous reply.

I was referring to this change:

+ $PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+ -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}

If itrace processing is not currently supported by the Python code, wouldn't adding --itrace=b here break the test?

Thanks,
Leo

author reviewer
#186 lore raw

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

Quoted 54 lines · Leo Yan

On Wed, Sep 23, 2026 at 06:40:57AM -0700, Ian Rogers wrote:

[...]

Quoted 37 lines
Quoted 24 lines
Quoted 12 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.

Quoted 3 lines

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.

Sorry for the confusion. I should have made this clearer in my previous reply.

I was referring to this change:

tools/perf/tests/shell/coresight/test_arm_coresight_disasm.sh +2 -0
+     $PYTHON ${script_path} -i ${perfdata} --itrace=b -d --stop-sample=2 \
+             -k ${perfdata}/kcore_dir/kcore 2> /dev/null > ${file}
Quoted 2 lines

If itrace processing is not currently supported by the Python code, wouldn't adding --itrace=b here break the test?

Thanks Leo, so I misspoke before. The LLM already added the itrace support into the Python session API in patch 9 ("perf python: Add Intel PT call_return and itrace capability"). It adds itrace support directly to perf.session() in the C extension (calling itrace_do_parse_synth_opts() on the session's itrace_synth_opts), and arm-cs-trace-disasm.py defaults --itrace to "b" and passes itrace=self.options.itrace when constructing perf.session().

Because arm-cs-trace-disasm.py already defaults to itrace="b" (matching commit 89db7f0db172 ("perf cs-etm: Filter synthesized branch samples")), passing --itrace=b in test_arm_coresight_disasm.sh is redundant. In v4, I will remove --itrace=b from test_arm_coresight_disasm.sh, add PERF_ITRACE forwarding when perf script --itrace=... <script> is used, and fix two issues found while inspecting the CoreSight disassembly flow (symbol_conf.vmlinux_name initialization order before perf_session__new() when --kcore is present, and recording the initial CS_ETM_TRACE_ON sample's sample_addr before filtering dso == '[unknown]').

Thanks,
Ian

Quoted 2 lines

Thanks,
Leo

author reviewer
#306 lore raw

v4 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 v4:
- Rebase onto latest perf-tools-next, explicitly #include <stdint.h> for UINT16_MAX in tools/perf/util/python.c, and ensure Event.ToPerfJson()
in tools/perf/pmu-events/metric.py and IntelFpu() in intel_metrics.py
escape event modifier = and , characters (Patches 01, 11).
- Set symbol_conf.{vmlinux_name,kallsyms_name,symfs} before calling perf_session__new() in pyrf_session__init() so --kcore directory loads
in perf_session__new() do not overwrite a user-specified vmlinux_name, unconditionally attach session->itrace_synth_opts to perf.session, and
honor PERF_ITRACE in perf.session, arm-cs-trace-disasm.py,
intel-pt-events.py, and perf script --itrace=... <script> (Patches 09,
38, 40, 48, 49).
- Match exact base event names (cycles / cpu-cycles and instructions) in stat-cpi.py instead of substring matching so events such as bus-cycles or branch-instructions do not corrupt CPI totals, and validate file-mode and
pipe-mode JSON outputs independently in test_flamegraph_python.sh (Patches
16, 19, 48).
- Distinguish per-syscall syscalls:sys_enter_* tracepoints (prefix evsel(syscalls:sys_enter_, which expose __syscall_nr or nr) from raw_syscalls:sys_enter and legacy syscalls:sys_enter tracepoints (which
expose id) in syscall-counts.py, syscall-counts-by-pid.py, and
sctop.py, and guard sample_time interval checks in rwtop.py when
sample_time is zero or absent (Patches 22, 23, 26, 29).
- In arm-cs-trace-disasm.py, initialize cpu_data with the initial sample's sample_addr before filtering dso == '[unknown]' (preserving the behavior
of commit 2d98dbb4c9c5 ("perf scripts python arm-cs-trace-disasm.py: Do not ignore disam first sample") on initial CS_ETM_TRACE_ON samples with ip == 0), restore the original
script's usage and packet diagram comments, and remove the redundant
--itrace=b argument in test_arm_coresight_disasm.sh since
arm-cs-trace-disasm.py defaults to itrace="b" (Patch 38, reported by Leo
Yan).
- In builtin-script.c, wait on the standalone script child process with
waitid(..., WEXITED | WNOWAIT) before clearing script_child_pid and
restoring signal handlers, then reap with waitpid(), preventing a PID-reuse
race window in forward_script_sig() (Patch 48).

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 |  798 +--
 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                   | 1016 ++--
 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               |   60 +-
 tools/perf/python/SchedGui.py                 |  246 +
 tools/perf/python/arm-cs-trace-disasm.py      |  442 ++
 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                    |  250 +
 tools/perf/python/stackcollapse.py            |  145 +
 tools/perf/python/stat-cpi.py                 |  219 +
 tools/perf/python/syscall-counts-by-pid.py    |  112 +
 tools/perf/python/syscall-counts.py           |   95 +
 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     |  122 +
 .../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                      | 1043 +++-
 tools/perf/util/scripting-engines/Build       |    9 -
 .../util/scripting-engines/trace-event-perl.c |  770 ---
 .../scripting-engines/trace-event-python.c    | 2335 --------
 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, 20611 insertions(+), 21496 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.315.gc6ed9934b7-goog
author reviewer
#307 lore raw

PATCH v4 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                    | 61 ++++++++++++++++++++-
 3 files changed, 75 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 +59 -2

106 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#309 lore raw

PATCH v4 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 543312c99c20..40b327801b7d 100644
@@ -3915,26 +3915,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.315.gc6ed9934b7-goog
author reviewer
#311 lore raw

PATCH v4 3/49 perf python: Clean up pylint warnings in ilist.py

In reply to Ian Rogers

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
---
 tools/perf/python/ilist.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/python/ilist.py +4 -4
index ebff0a843b7a..86bd65452e6e 100755
@@ -82,7 +82,7 @@ class Metric(TreeValue):
         try:
             val = evlist.compute_metric(self.metric_name, cpu, thread)
             return 0 if math.isnan(val) else val
-        except:
+        except (OSError, ValueError, RuntimeError, TypeError):
             # Be tolerant of failures to compute metrics on particular CPUs/threads.
             return 0
 
@@ -419,7 +419,7 @@ class IListApp(App):
             if self.evlist:
                 self.evlist.open()
                 self.evlist.enable()
-        except:
+        except (OSError, ValueError, RuntimeError):
             self.evlist = None
 
         if not self.evlist:
@@ -450,7 +450,7 @@ class IListApp(App):
                 pmu_name = pmu.name().lower()
                 pmu_node = pmus.add(pmu_name)
                 try:
-                    for event in sorted(pmu.events(), key=lambda x: x["name"]):
+                    for event in sorted(pmu.events(), key=lambda x: x.get("name", "")):
                         if "deprecated" in event:
                             continue
                         if "name" in event:
@@ -460,7 +460,7 @@ class IListApp(App):
                                                   data=PmuEvent(pmu_name, e))
                             else:
                                 pmu_node.add_leaf(e, data=PmuEvent(pmu_name, e))
-                except:
+                except (OSError, ValueError, RuntimeError, KeyError, TypeError):
                     # Reading events may fail with EPERM, ignore.
                     pass
             metrics = tree.root.add("Metrics")
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#313 lore raw

PATCH v4 4/49 perf python: Clean up pylint warnings in treport.py

In reply to Ian Rogers

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).
- Clear session = None in a finally block after session.process_events() to break the C perf_session reference cycle before launching the TUI.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/treport.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
tools/perf/python/treport.py +9 -6
index 786b852f471c..0921b428b1aa 100755
@@ -91,9 +91,9 @@ class ProfileNode:
         pid = sample.sample_pid
         try:
             assert session
-            thread = session.find_thread(sample.sample_tid)
+            thread = session.find_thread(pid, sample.sample_tid)
             comm = (thread.comm() if thread else None) or f"unknown ({pid})"
-        except Exception:
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
             comm = f"unknown ({pid})"
 
         period = sample.sample_period
@@ -412,9 +412,9 @@ class FlameGraph(ScrollView):
     }
     """
 
-    def __init__(self, root: ProfileNode, *args, **kwargs):
+    def __init__(self, root: ProfileNode, *pos_args, **kwargs):
         """Initialize the FlameGraph widget."""
-        super().__init__(*args, **kwargs)
+        super().__init__(*pos_args, **kwargs)
         self.root = root
         self.cursor = root
         self.selected = root
@@ -549,12 +549,15 @@ if __name__ == "__main__":
     profile = ProfileBuilder()
     try:
         session = perf.session(perf.data(input_file), sample=profile.process_event)
-    except Exception as e:
+    except (OSError, ValueError, RuntimeError) as e:
         print(f"Error opening session: {e}", file=sys.stderr)
         sys.exit(1)
 
     # profile.process_event is called for each perf event to build the profile.
-    session.process_events()
+    try:
+        session.process_events()
+    finally:
+        session = None
 
     # Visualize data.
     app = ReportApp(profile.root)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#315 lore raw

PATCH v4 5/49 perf python: Clean up pylint warnings in tracepoint.py

In reply to Ian Rogers

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
---
 tools/perf/python/tracepoint.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/python/tracepoint.py +2 -2
index 15b0c8268996..9d49a25b3b1e 100755
@@ -9,7 +9,7 @@ def change_proctitle():
     try:
         import setproctitle
         setproctitle.setproctitle("tracepoint.py")
-    except:
+    except ImportError:
         print("Install the setproctitle python package to help with top and friends")
 
 def main():
@@ -29,7 +29,7 @@ def main():
 
     evlist.open()
     evlist.mmap()
-    evlist.enable();
+    evlist.enable()
 
     while True:
         evlist.poll(timeout = -1)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#317 lore raw

PATCH v4 6/49 perf python: Clean up pylint warnings in twatch.py

In reply to Ian Rogers

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
---
 tools/perf/python/twatch.py | 80 ++++++++++++++++++-------------------
 1 file changed, 39 insertions(+), 41 deletions(-)
tools/perf/python/twatch.py +39 -41
index 04f3db29b9bc..ad35b1620781 100755
@@ -9,53 +9,51 @@
 import perf
 
 def main(context_switch = 0, thread = -1):
-	cpus = perf.cpu_map()
-	threads = perf.thread_map(thread)
-	evsel = perf.evsel(type	  = perf.TYPE_SOFTWARE,
-			   config = perf.COUNT_SW_DUMMY,
-			   task = 1, comm = 1, mmap = 0, freq = 0,
-			   wakeup_events = 1, watermark = 1,
-			   sample_id_all = 1, context_switch = context_switch,
-			   sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU)
-
-	"""What we want are just the PERF_RECORD_ lifetime events for threads,
-	 using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1
-	 (the default), makes perf reenable irq_vectors:local_timer_entry, when
-	 disabling nohz, not good for some use cases where all we want is to get
-	 threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY,
-	 freq=0) instead."""
-
-	evsel.open(cpus = cpus, threads = threads);
-	evlist = perf.evlist(cpus, threads)
-	evlist.add(evsel)
-	evlist.mmap()
-	while True:
-		evlist.poll(timeout = -1)
-		for cpu in cpus:
-			event = evlist.read_on_cpu(cpu)
-			if not event:
-				continue
-			print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu,
+    cpus = perf.cpu_map()
+    threads = perf.thread_map(thread)
+    evsel = perf.evsel(type	  = perf.TYPE_SOFTWARE,
+               config = perf.COUNT_SW_DUMMY,
+               task = 1, comm = 1, mmap = 0, freq = 0,
+               wakeup_events = 1, watermark = 1,
+               sample_id_all = 1, context_switch = context_switch,
+               sample_type = perf.SAMPLE_PERIOD | perf.SAMPLE_TID | perf.SAMPLE_CPU)
+
+    # What we want are just the PERF_RECORD_ lifetime events for threads,
+    # using the default, PERF_TYPE_HARDWARE + PERF_COUNT_HW_CYCLES & freq=1
+    # (the default), makes perf reenable irq_vectors:local_timer_entry, when
+    # disabling nohz, not good for some use cases where all we want is to get
+    # threads comes and goes... So use (perf.TYPE_SOFTWARE, perf_COUNT_SW_DUMMY,
+    # freq=0) instead.
+
+    evsel.open(cpus = cpus, threads = threads)
+    evlist = perf.evlist(cpus, threads)
+    evlist.add(evsel)
+    evlist.mmap()
+    while True:
+        evlist.poll(timeout = -1)
+        for cpu in cpus:
+            event = evlist.read_on_cpu(cpu)
+            if not event:
+                continue
+            print("cpu: {0}, pid: {1}, tid: {2} {3}".format(event.sample_cpu,
                                                                         event.sample_pid,
                                                                         event.sample_tid,
                                                                         event))
 
 if __name__ == '__main__':
-    """
-	To test the PERF_RECORD_SWITCH record, pick a pid and replace
-	in the following line.
+    #     To test the PERF_RECORD_SWITCH record, pick a pid and replace
+    # in the following line.
 
-	Example output:
+    # Example output:
 
-cpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 }
-cpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 }
-cpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 }
-cpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 }
+    # cpu: 3, pid: 31463, tid: 31593 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31593, switch_out: 1 }
+    # cpu: 1, pid: 31463, tid: 31489 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31489, switch_out: 1 }
+    # cpu: 2, pid: 31463, tid: 31496 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31496, switch_out: 1 }
+    # cpu: 3, pid: 31463, tid: 31491 { type: context_switch, next_prev_pid: 31463, next_prev_tid: 31491, switch_out: 0 }
+    #
+    # It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT
+    # to figure out if this is a context switch in or out of the monitored threads.
 
-	It is possible as well to use event.misc & perf.PERF_RECORD_MISC_SWITCH_OUT
-	to figure out if this is a context switch in or out of the monitored threads.
-
-	If bored, please add command line option parsing support for these options :-)
-    """
-    # main(context_switch = 1, thread = 31463)
+    # If bored, please add command line option parsing support for these options :-)
+    #     # main(context_switch = 1, thread = 31463)
     main()
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#319 lore raw

PATCH v4 7/49 perf python: Improve perf script -l descriptions

In reply to Ian Rogers

Add module docstrings to counting.py, tracepoint.py, and twatch.py. This satisfies pylint's missing-module-docstring check and prepares these existing standalone scripts in tools/perf/python/ for the later commit in this series ("perf script: Support standalone scripts and remove embedded scripting"), which updates 'perf script -l' to scan tools/perf/python/ and display each script's module docstring:

```
$ 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.
...
```
Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/counting.py   | 1 +
 tools/perf/python/tracepoint.py | 1 +
 tools/perf/python/twatch.py     | 1 +
 3 files changed, 3 insertions(+)
tools/perf/python/counting.py +1 -0
index 02121d2bb11d..9adbbeccdacd 100755
@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 # SPDX-License-Identifier: GPL-2.0
+"""Example for counting perf events."""
 # -*- python -*-
 # -*- coding: utf-8 -*-
 
tools/perf/python/tracepoint.py +1 -0
index 9d49a25b3b1e..faa5311b8418 100755
@@ -1,5 +1,6 @@
 #! /usr/bin/env python
 # SPDX-License-Identifier: GPL-2.0
+"""Example showing how to enable a tracepoint and access its fields."""
 # -*- python -*-
 # -*- coding: utf-8 -*-
 
tools/perf/python/twatch.py +1 -0
index ad35b1620781..b0f1bf113162 100755
@@ -1,5 +1,6 @@
 #! /usr/bin/env python
 # SPDX-License-Identifier: GPL-2.0-only
+"""Example to show how to enable a software event and track context switches."""
 # -*- python -*-
 # -*- coding: utf-8 -*-
 #   twatch - Experimental use of the perf python interface
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#321 lore raw

PATCH v4 8/49 perf python: Expose addr location, transaction, and context_switch

In reply to Ian Rogers

Expose destination address location components (addr_dso, addr_symbol, addr_sym_offset), branch/transaction metrics (branch_type, in_tx, flags, transaction), machine_pid, vcpu, sym_offset, and context_switch callbacks directly to the Python extension.

Also include supporting fixes and infrastructure updates in util/python.c and perf.pyi:
- Support fetching sym_offset on resolved sample symbols.
- Populate sample machine_pid and vcpu from evlist__id2sid() when perf_guest is enabled, and resolve samples against the matching guest or host machine.
- Expose next_prev_pid and next_prev_tid on switch_event only for PERF_RECORD_SWITCH_CPU_WIDE records (returning None otherwise).
- Wire up tracing_data in pyrf_session__new() and update perf.pyi type stubs (including srccode() and insn() return types).

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/perf.pyi |  23 ++-
 tools/perf/util/python.c   | 291 ++++++++++++++++++++++++++++++++++---
 2 files changed, 289 insertions(+), 25 deletions(-)
tools/perf/python/perf.pyi +18 -5
index 92aad84ca542..44c693761838 100644
@@ -194,6 +194,8 @@ class _sample_members:
     sample_time: int
     sample_id: int
     sample_stream_id: int
+    machine_pid: int
+    vcpu: int
     sample_period: int
     sample_cpu: int
 
@@ -218,10 +220,18 @@ class sample_event(_sample_members):
     symbol: str
     sym_start: int
     sym_end: int
+    sym_offset: Optional[int]
+    addr_dso: Optional[str]
+    addr_symbol: Optional[str]
+    addr_sym_offset: Optional[int]
+    branch_type: int
+    in_tx: int
+    flags: int
+    transaction: int
     brstack: Optional['branch_stack']
     callchain: Optional['callchain']
-    def srccode(self) -> str: ...
-    def insn(self) -> str: ...
+    def srccode(self) -> Optional[tuple[Optional[str], int, Optional[str]]]: ...
+    def insn(self) -> Optional[bytes]: ...
     def __getattr__(self, name: str) -> Any: ...
 
 class mmap_event(_sample_members):
@@ -298,8 +308,9 @@ class read_event(_sample_members):
 class switch_event(_sample_members):
     """Represents a SWITCH or SWITCH_CPU_WIDE record."""
     type: int
-    next_prev_pid: int
-    next_prev_tid: int
+    misc: int
+    next_prev_pid: Optional[int]
+    next_prev_tid: Optional[int]
     evsel: Optional['evsel']
 
 class branch_entry:
@@ -334,6 +345,7 @@ class callchain_node:
     ip: int
     symbol: str
     dso: str
+    sym_offset: Optional[int]
 
 class callchain:
     """Sequence of callchain frames."""
@@ -441,7 +453,8 @@ class session:
         self,
         data: data,
         sample: Optional[Callable[[sample_event], None]] = None,
-        stat: Optional[Callable[[Any, Optional[str]], None]] = None
+        stat: Optional[Callable[[Any, Optional[str]], None]] = None,
+        context_switch: Optional[Callable[[switch_event], None]] = None,
     ) -> None:
         """Initialize a perf session.
 
tools/perf/util/python.c +271 -20

445 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#323 lore raw

PATCH v4 9/49 perf python: Add Intel PT call_return and itrace capability

In reply to 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, populating frame->map and frame->sym so call_path entries expose resolved symbol and DSO names).
- 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().

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/perf.pyi |  78 ++++-
 tools/perf/util/python.c   | 698 ++++++++++++++++++++++++++++++++++---
 2 files changed, 711 insertions(+), 65 deletions(-)
tools/perf/python/perf.pyi +61 -17
index 44c693761838..087da00728fd 100644
@@ -96,7 +96,7 @@ class data:
 
 class thread:
     """Represents a thread in the system."""
-    def comm(self) -> str:
+    def comm(self) -> Optional[str]:
         """Get the command name of the thread."""
         ...
     pid: int
@@ -165,7 +165,13 @@ class evsel:
     def __str__(self) -> str:
         """Return string representation of the event."""
         ...
-    def open(self) -> None:
+    def open(
+        self,
+        cpus: Optional['cpu_map'] = None,
+        threads: Optional[thread_map] = None,
+        group: bool = False,
+        inherit: bool = False,
+    ) -> None:
         """Open the event selector file descriptor table."""
         ...
     def read(self, cpu: int, thread: int) -> counts_values:
@@ -211,15 +217,15 @@ class sample_event(_sample_members):
     sample_cyc_count: int
     type: int
     raw_buf: bytes
-    dso: str
-    dso_long_name: str
-    dso_bid: Optional[bytes]
-    map_start: int
-    map_end: int
-    map_pgoff: int
-    symbol: str
-    sym_start: int
-    sym_end: int
+    dso: Optional[str]
+    dso_long_name: Optional[str]
+    dso_bid: Optional[str]
+    map_start: Optional[int]
+    map_end: Optional[int]
+    map_pgoff: Optional[int]
+    symbol: Optional[str]
+    sym_start: Optional[int]
+    sym_end: Optional[int]
     sym_offset: Optional[int]
     addr_dso: Optional[str]
     addr_symbol: Optional[str]
@@ -230,7 +236,7 @@ class sample_event(_sample_members):
     transaction: int
     brstack: Optional['branch_stack']
     callchain: Optional['callchain']
-    def srccode(self) -> Optional[tuple[Optional[str], int, Optional[str]]]: ...
+    def srccode(self, addr: int = ..., /) -> Optional[tuple[Optional[str], int, Optional[str]]]: ...
     def insn(self) -> Optional[bytes]: ...
     def __getattr__(self, name: str) -> Any: ...
 
@@ -339,6 +345,7 @@ class branch_stack:
     """Sequence of branch entries in the branch stack."""
     def __len__(self) -> int: ...
     def __getitem__(self, index: int) -> branch_entry: ...
+    def __iter__(self) -> Iterator[branch_entry]: ...
 
 class callchain_node:
     """Represents a frame in the callchain."""
@@ -387,10 +394,10 @@ class evlist:
     def close(self) -> None:
         """Close the events in the list."""
         ...
-    def mmap(self) -> None:
+    def mmap(self, pages: int = 128, overwrite: bool = False) -> None:
         """Memory map the event buffers."""
         ...
-    def poll(self, timeout: int) -> int:
+    def poll(self, timeout: int = -1) -> int:
         """Poll for events.
 
         Args:
@@ -400,7 +407,7 @@ class evlist:
             Number of events ready.
         """
         ...
-    def read_on_cpu(self, cpu: int) -> Optional[Any]:
+    def read_on_cpu(self, cpu: int, sample_id_all: bool = True) -> Optional[Any]:
         """Read a sample event from a specific CPU.
 
         Args:
@@ -437,17 +444,38 @@ class evlist:
     def enable(self) -> None:
         """Enable all events in the list."""
         ...
-    def get_pollfd(self) -> List[int]:
+    def get_pollfd(self) -> List[Any]:
         """Get a list of file descriptors for polling."""
         ...
-    def add(self, evsel: evsel) -> int:
+    def add(self, evsel: evsel, /) -> int:
         """Add an event to the list."""
         ...
+    def __len__(self) -> int: ...
+    def __getitem__(self, index: int) -> evsel: ...
     def __iter__(self) -> Iterator[evsel]:
         """Iterate over the events (evsel) in the list."""
         ...
 
 
+class call_return:
+    """Represents a call/return event from instruction trace decoding."""
+    db_id: int
+    parent_id: int
+    insn_count: int
+    cyc_count: int
+    comm: Optional[str]
+    machine_pid: int
+    pid: Optional[int]
+    tid: Optional[int]
+    call_time: int
+    return_time: int
+    branch_count: int
+    call_ref: int
+    return_ref: int
+    flags: int
+    call_path: Optional[callchain]
+
+
 class session:
     def __init__(
         self,
@@ -455,6 +483,11 @@ class session:
         sample: Optional[Callable[[sample_event], None]] = None,
         stat: Optional[Callable[[Any, Optional[str]], None]] = None,
         context_switch: Optional[Callable[[switch_event], None]] = None,
+        call_return: Optional[Callable[[call_return], None]] = None,
+        itrace: Optional[str] = None,
+        vmlinux: Optional[str] = None,
+        kallsyms: Optional[str] = None,
+        symfs: Optional[str] = None
     ) -> None:
         """Initialize a perf session.
 
@@ -464,6 +497,8 @@ class session:
             stat: Callback for stat events.
         """
         ...
+    e_machine: int
+    is_64_bit: bool
     def process_events(self) -> None:
         """Process all events in the session."""
         ...
@@ -696,3 +731,12 @@ RECORD_STAT_ROUND: int
 
 RECORD_MISC_SWITCH_OUT: int
 """MISC_SWITCH_OUT record."""
+
+CALL_RETURN_NO_CALL: int
+"""'return' but no matching 'call'."""
+
+CALL_RETURN_NO_RETURN: int
+"""'call' but no matching 'return'."""
+
+CALL_RETURN_NON_CALL: int
+"""A branch but not a 'call' to the start of a different symbol."""
tools/perf/util/python.c +650 -48

1,013 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#325 lore raw

PATCH v4 10/49 perf python: Allow KeyboardInterrupt to propagate in LiveSession

In reply to Ian Rogers

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.

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
---
 tools/perf/python/perf_live.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/perf/python/perf_live.py +7 -3
index 616b5f657463..99e73d58a96f 100755
@@ -33,10 +33,16 @@ class LiveSession:
                 # Poll for events with 100ms timeout
                 try:
                     self.evlist.poll(100)
+                # CPython's PyErr_SetFromErrno() invokes PyErr_CheckSignals() on
+                # EINTR, converting SIGINT into KeyboardInterrupt, which inherits
+                # from BaseException rather than InterruptedError/OSError, so
+                # Ctrl-C exits the loop and triggers finally: self.evlist.close()
+                # while non-fatal signals retry poll().
                 except InterruptedError:
                     continue
                 for cpu in self.cpus:
-                    for _ in range(1000): # Limit to 1000 events per CPU per poll to prevent starvation
+                    # Limit to 1000 events per CPU per poll to prevent starvation
+                    for _ in range(1000):
                         try:
                             event = self.evlist.read_on_cpu(cpu)
                         except TypeError as e:
@@ -53,7 +59,5 @@ class LiveSession:
 
                         if event.type == perf.RECORD_SAMPLE:
                             self.sample_callback(event)
-        except KeyboardInterrupt:
-            pass
         finally:
             self.evlist.close()
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#327 lore raw

PATCH v4 11/49 perf pmu-events: Clean up mypy and pylint issues

In reply to Ian Rogers

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'.

Assisted-by Antigravity:gemini-3.1-pro
---
 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            |  60 +++--
 5 files changed, 214 insertions(+), 232 deletions(-)
tools/perf/pmu-events/amd_metrics.py +6 -10
index dccfcacaf148..42569b9153f4 100755
@@ -3,7 +3,7 @@
 import argparse
 import math
 import os
-from typing import Optional
+from typing import Optional, Union
 from common_metrics import Cycles
 from metric import (d_ratio, has_event, max, Event, JsonEncodeMetric,
                     JsonEncodeMetricGroupDescriptions, Literal, LoadEvents,
@@ -70,7 +70,6 @@ def AmdBr():
         ])
 
     def Conditional() -> Optional[MetricGroup]:
-        global _zen_model
         br = Event("ex_ret_brn_cond", "ex_ret_cond")
         br_r = d_ratio(br, interval_sec)
         ins_r = d_ratio(ins, br)
@@ -156,25 +155,24 @@ def AmdCtxSw() -> MetricGroup:
 
 
 def AmdDtlb() -> Optional[MetricGroup]:
-    global _zen_model
     if _zen_model >= 4:
         return None
 
-    d_dat = Event("ls_dc_accesses") if _zen_model <= 3 else None
+    d_dat = Event("ls_dc_accesses")
     d_h4k = Event("ls_l1_d_tlb_miss.tlb_reload_4k_l2_hit")
-    d_hcoal = Event(
+    d_hcoal: Union[Event, int] = Event(
         "ls_l1_d_tlb_miss.tlb_reload_coalesced_page_hit") if _zen_model >= 2 else 0
     d_h2m = Event("ls_l1_d_tlb_miss.tlb_reload_2m_l2_hit")
     d_h1g = Event("ls_l1_d_tlb_miss.tlb_reload_1g_l2_hit")
 
     d_m4k = Event("ls_l1_d_tlb_miss.tlb_reload_4k_l2_miss")
-    d_mcoal = Event(
+    d_mcoal: Union[Event, int] = Event(
         "ls_l1_d_tlb_miss.tlb_reload_coalesced_page_miss") if _zen_model >= 2 else 0
     d_m2m = Event("ls_l1_d_tlb_miss.tlb_reload_2m_l2_miss")
     d_m1g = Event("ls_l1_d_tlb_miss.tlb_reload_1g_l2_miss")
 
-    d_w0 = Event("ls_tablewalker.dc_type0") if _zen_model <= 3 else None
-    d_w1 = Event("ls_tablewalker.dc_type1") if _zen_model <= 3 else None
+    d_w0 = Event("ls_tablewalker.dc_type0")
+    d_w1 = Event("ls_tablewalker.dc_type1")
     walks = d_w0 + d_w1
     walks_r = d_ratio(walks, interval_sec)
     ins_w = d_ratio(ins, walks)
@@ -266,7 +264,6 @@ def AmdDtlb() -> Optional[MetricGroup]:
 
 
 def AmdIotlb() -> Optional[MetricGroup]:
-    global _zen_model
     if _zen_model < 2:
         return None
 
@@ -322,7 +319,6 @@ def AmdIotlb() -> Optional[MetricGroup]:
 
 
 def AmdItlb():
-    global _zen_model
     l2h = Event("bp_l1_tlb_miss_l2_tlb_hit", "bp_l1_tlb_miss_l2_hit")
     l2m = Event("bp_l1_tlb_miss_l2_tlb_miss.all", "l2_itlb_misses",)
     l2r = l2h + l2m
tools/perf/pmu-events/intel_metrics.py +126 -133

558 lines not on this page · raw message

tools/perf/pmu-events/jevents.py +36 -41

231 lines not on this page · raw message

tools/perf/pmu-events/make_legacy_cache.py +18 -16
index 28a1ff804f86..b224544f85f6 100755
@@ -61,7 +61,7 @@ hw_cache_result = [
      "misses"),
 ]
 
-events = []
+events: list[dict[str, str]] = []
 def add_event(name: str,
               cache_id: int, cache_op: int, cache_result: int,
               desc: str,
@@ -87,10 +87,10 @@ def add_event(name: str,
         event["Deprecated"] = "1"
     events.append(event)
 
-for (cache_id, names, ops, cache_desc) in hw_cache_id:
-    for name in names:
-        add_event(name,
-                  cache_id,
+for (cid, names, ops, cache_desc) in hw_cache_id:
+    for cname in names:
+        add_event(cname,
+                  cid,
                   0, # PERF_COUNT_HW_CACHE_OP_READ
                   0, # PERF_COUNT_HW_CACHE_RESULT_ACCESS
                   f"{cache_desc} read accesses.",
@@ -100,27 +100,29 @@ for (cache_id, names, ops, cache_desc) in hw_cache_id:
             if op not in ops:
                 continue
             for op_name in op_names:
-                deprecated = (names[0] != name or op_names[1] != op_name)
-                add_event(f"{name}-{op_name}",
-                          cache_id,
+                is_deprecated = (names[0] != cname or op_names[1] != op_name)
+                add_event(f"{cname}-{op_name}",
+                          cid,
                           op,
                           0, # PERF_COUNT_HW_CACHE_RESULT_ACCESS
                           f"{cache_desc} {op_desc} accesses.",
-                          deprecated)
+                          is_deprecated)
 
                 for (result,  result_names, result_desc) in hw_cache_result:
                     for result_name in result_names:
-                        deprecated = ((names[0] != name or op_names[0] != op_name) or
-                                      (result == 0) or (result_names[0] != result_name))
-                        add_event(f"{name}-{op_name}-{result_name}",
-                                  cache_id, op, result,
+                        is_deprecated = ((names[0] != cname or op_names[0] != op_name) or
+                                         (result == 0) or (result_names[0] != result_name))
+                        add_event(f"{cname}-{op_name}-{result_name}",
+                                  cid,
+                                  op,
+                                  result,
                                   f"{cache_desc} {op_desc} {result_desc}.",
-                                  deprecated)
+                                  is_deprecated)
 
         for (result,  result_names, result_desc) in hw_cache_result:
             for result_name in result_names:
-                add_event(f"{name}-{result_name}",
-                          cache_id,
+                add_event(f"{cname}-{result_name}",
+                          cid,
                           0, # PERF_COUNT_HW_CACHE_OP_READ
                           result,
                           f"{cache_desc} read {result_desc}.",
tools/perf/pmu-events/metric.py +28 -32

135 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#329 lore raw

PATCH v4 12/49 perf test: Clean up mypy and pylint issues in shell test libraries

In reply to Ian Rogers

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.

Also update tools/perf/tests/shell/lib/setup_python.sh to:
- Avoid x-prefix string comparisons.
- Export PYTHON and resolve PYTHONPATH and PERF_EXEC_PATH from BASH_SOURCE[0], ../../../python (for nested test directories such as coresight/), and the directory containing the perf binary so out-of-tree builds and standalone perf python scripts work seamlessly.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/tests/shell/lib/attr.py            | 86 ++++++++++---------
 .../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 ++++++-
 4 files changed, 113 insertions(+), 91 deletions(-)
tools/perf/tests/shell/lib/attr.py +44 -42
index bfccc727d9b2..7f3d5b64b00d 100644
@@ -12,6 +12,8 @@ import re
 import shutil
 import subprocess
 
+log = logging.getLogger('test')
+
 def data_equal(a, b):
     # Allow multiple values in assignment separated by '|'
     a_list = a.split('|')
@@ -89,19 +91,19 @@ class Event(dict):
 
     def add(self, data):
         for key, val in data:
-            log.debug("      %s = %s" % (key, val))
+            log.debug("      %s = %s", key, val)
             self[key] = val
 
     def __init__(self, name, data, base):
-        log.debug("    Event %s" % name);
-        self.name  = name;
+        log.debug("    Event %s", name)
+        self.name  = name
         self.group = ''
         self.add(base)
         self.add(data)
 
     def equal(self, other):
         for t in Event.terms:
-            log.debug("      [%s] %s %s" % (t, self[t], other[t]));
+            log.debug("      [%s] %s %s", t, self[t], other[t])
             if t not in self or t not in other:
                 return False
             if not data_equal(self[t], other[t]):
@@ -118,7 +120,7 @@ class Event(dict):
             if t not in self or t not in other:
                 continue
             if not data_equal(self[t], other[t]):
-                log.warning("expected %s=%s, got %s" % (t, self[t], other[t]))
+                log.warning("expected %s=%s, got %s", t, self[t], other[t])
 
 def parse_version(version):
     if not version:
@@ -149,7 +151,7 @@ class Test(object):
         parser = configparser.ConfigParser()
         parser.read(path)
 
-        log.warning("running '%s'" % path)
+        log.warning("running '%s'", path)
 
         self.path     = path
         self.test_dir = options.test_dir
@@ -159,15 +161,15 @@ class Test(object):
 
         try:
             self.ret  = parser.get('config', 'ret')
-        except:
+        except Exception:
             self.ret  = 0
 
         self.test_ret = parser.getboolean('config', 'test_ret', fallback=False)
 
         try:
             self.arch  = parser.get('config', 'arch')
-            log.warning("test limitation '%s'" % self.arch)
-        except:
+            log.warning("test limitation '%s'", self.arch)
+        except Exception:
             self.arch  = ''
 
         self.auxv = parser.get('config', 'auxv', fallback=None)
@@ -175,7 +177,7 @@ class Test(object):
         self.kernel_until = parse_version(parser.get('config', 'kernel_until', fallback=None))
         self.expect   = {}
         self.result   = {}
-        log.debug("  loading expected events");
+        log.debug("  loading expected events")
         self.load_events(path, self.expect)
 
     def is_event(self, name):
@@ -203,7 +205,7 @@ class Test(object):
             else:
                 try:
                     value = int(items[-1], 0)
-                except:
+                except ValueError:
                     value = items[-1]
             return (items[0], value)
 
@@ -227,7 +229,7 @@ class Test(object):
         # Handle negated list such as !s390x,ppc
         if arch_list[0][0] == '!':
             arch_list[0] = arch_list[0][1:]
-            log.warning("excluded architecture list %s" % arch_list)
+            log.warning("excluded architecture list %s", arch_list)
             for arch_item in arch_list:
                 # log.warning("test for %s arch is %s" % (arch_item, myarch))
                 if arch_item == myarch:
@@ -243,19 +245,19 @@ class Test(object):
     def restore_sample_rate(self, value=10000):
         try:
             # Check value of sample_rate
-            with open("/proc/sys/kernel/perf_event_max_sample_rate", "r") as fIn:
+            with open("/proc/sys/kernel/perf_event_max_sample_rate", "r", encoding="utf-8") as fIn:
                 curr_value = fIn.readline()
             # If too low restore to reasonable value
             if not curr_value or int(curr_value) < int(value):
-                with open("/proc/sys/kernel/perf_event_max_sample_rate", "w") as fOut:
+                with open("/proc/sys/kernel/perf_event_max_sample_rate", "w", encoding="utf-8") as fOut:
                     fOut.write(str(value))
 
         except IOError as e:
-            log.warning("couldn't restore sample_rate value: I/O error %s" % e)
+            log.warning("couldn't restore sample_rate value: I/O error %s", e)
         except ValueError as e:
-            log.warning("couldn't restore sample_rate value: Value error %s" % e)
+            log.warning("couldn't restore sample_rate value: Value error %s", e)
         except TypeError as e:
-            log.warning("couldn't restore sample_rate value: Type error %s" % e)
+            log.warning("couldn't restore sample_rate value: Type error %s", e)
 
     def load_events(self, path, events):
         parser_event = configparser.ConfigParser()
@@ -266,7 +268,7 @@ class Test(object):
         # event' first as a base
         for section in filter(self.is_event, parser_event.sections()):
 
-            parser_items = parser_event.items(section);
+            parser_items = parser_event.items(section)
             base_items   = {}
 
             # Read parent event if there's any
@@ -280,7 +282,7 @@ class Test(object):
             events[section] = e
 
     def run_cmd(self, tempdir):
-        junk1, junk2, junk3, junk4, myarch = (os.uname())
+        _junk1, _junk2, _junk3, _junk4, myarch = (os.uname())
 
         if self.skip_test_arch(myarch):
             raise Notest(self, myarch)
@@ -299,7 +301,7 @@ class Test(object):
               self.perf, self.command, tempdir, self.args)
         ret = os.WEXITSTATUS(os.system(cmd))
 
-        log.info("  '%s' ret '%s', expected '%s'" % (cmd, str(ret), str(self.ret)))
+        log.info("  '%s' ret '%s', expected '%s'", cmd, str(ret), str(self.ret))
 
         if not data_equal(str(ret), str(self.ret)):
             if self.test_ret:
@@ -310,34 +312,34 @@ class Test(object):
     def compare(self, expect, result):
         match = {}
 
-        log.debug("  compare");
+        log.debug("  compare")
 
         # For each expected event find all matching
         # events in result. Fail if there's not any.
         for exp_name, exp_event in expect.items():
             exp_list = []
             res_event = {}
-            log.debug("    matching [%s]" % exp_name)
+            log.debug("    matching [%s]", exp_name)
             for res_name, res_event in result.items():
-                log.debug("      to [%s]" % res_name)
+                log.debug("      to [%s]", res_name)
                 if (exp_event.equal(res_event)):
                     exp_list.append(res_name)
                     log.debug("    ->OK")
                 else:
-                    log.debug("    ->FAIL");
+                    log.debug("    ->FAIL")
 
-            log.debug("    match: [%s] matches %s" % (exp_name, str(exp_list)))
+            log.debug("    match: [%s] matches %s", exp_name, str(exp_list))
 
             # we did not any matching event - fail
             if not exp_list:
                 if exp_event.optional():
-                    log.debug("    %s does not match, but is optional" % exp_name)
+                    log.debug("    %s does not match, but is optional", exp_name)
                 else:
                     if not res_event:
-                        log.debug("    res_event is empty");
+                        log.debug("    res_event is empty")
                     else:
                         exp_event.diff(res_event)
-                    raise Fail(self, 'match failure');
+                    raise Fail(self, 'match failure')
 
             match[exp_name] = exp_list
 
@@ -354,38 +356,38 @@ class Test(object):
                 if res_group not in match[group]:
                     raise Fail(self, 'group failure')
 
-                log.debug("    group: [%s] matches group leader %s" %
-                         (exp_name, str(match[group])))
+                log.debug("    group: [%s] matches group leader %s",
+                          exp_name, str(match[group]))
 
         log.debug("  matched")
 
     def resolve_groups(self, events):
         for name, event in events.items():
-            group_fd = event['group_fd'];
+            group_fd = event['group_fd']
             if group_fd == '-1':
-                continue;
+                continue
 
             for iname, ievent in events.items():
                 if (ievent['fd'] == group_fd):
                     event.group = iname
-                    log.debug('[%s] has group leader [%s]' % (name, iname))
-                    break;
+                    log.debug('[%s] has group leader [%s]', name, iname)
+                    break
 
     def run(self):
-        tempdir = tempfile.mkdtemp();
+        tempdir = tempfile.mkdtemp()
 
         try:
             # run the test script
-            self.run_cmd(tempdir);
+            self.run_cmd(tempdir)
 
             # load events expectation for the test
-            log.debug("  loading result events");
+            log.debug("  loading result events")
             for f in glob.glob(tempdir + '/event*'):
-                self.load_events(f, self.result);
+                self.load_events(f, self.result)
 
             # resolve group_fd to event names
-            self.resolve_groups(self.expect);
-            self.resolve_groups(self.result);
+            self.resolve_groups(self.expect)
+            self.resolve_groups(self.result)
 
             # do the expectation - results matching - both ways
             self.compare(self.expect, self.result)
@@ -401,9 +403,9 @@ def run_tests(options):
         try:
             Test(f, options).run()
         except Unsup as obj:
-            log.warning("unsupp  %s" % obj.getMsg())
+            log.warning("unsupp  %s", obj.getMsg())
         except Notest as obj:
-            log.warning("skipped %s" % obj.getMsg())
+            log.warning("skipped %s", obj.getMsg())
 
 def setup_log(verbose):
     global log
tools/perf/tests/shell/lib/perf_json_output_lint.py +18 -18
index dafbde56cc76..8cde22ac9d97 100644
@@ -46,46 +46,46 @@ def is_counter_value(num):
 def is_metric_value(num):
   return isfloat(num) or num == 'none'
 
-def check_json_output(expected_items):
+def check_json_output(expected_count):
   checks = {
-      'counters': lambda x: isfloat(x),
+      'counters': isfloat,
       'core': lambda x: True,
-      'counter-value': lambda x: is_counter_value(x),
+      'counter-value': is_counter_value,
       'cgroup': lambda x: True,
-      'cpu': lambda x: isint(x),
+      'cpu': isint,
       'cache': lambda x: True,
       'cluster': lambda x: True,
       'die': lambda x: True,
       'event': lambda x: True,
-      'event-runtime': lambda x: isfloat(x),
-      'interval': lambda x: isfloat(x),
+      'event-runtime': isfloat,
+      'interval': isfloat,
       'metric-unit': lambda x: True,
-      'metric-value': lambda x: is_metric_value(x),
+      'metric-value': is_metric_value,
       'metric-threshold': lambda x: x in ['unknown', 'good', 'less good', 'nearly bad', 'bad'],
       'metricgroup': lambda x: True,
       'node': lambda x: True,
-      'pcnt-running': lambda x: isfloat(x),
+      'pcnt-running': isfloat,
       'socket': lambda x: True,
       'thread': lambda x: True,
       'unit': lambda x: True,
   }
-  input = '[\n' + ','.join(Lines) + '\n]'
-  for item in json.loads(input):
-    if expected_items != -1:
+  json_input = '[\n' + ','.join(Lines) + '\n]'
+  for item in json.loads(json_input):
+    if expected_count:
       count = len(item)
-      if count not in expected_items and count >= 1 and count <= 7 and 'metric-value' in item:
+      if count not in expected_count and count >= 1 and count <= 7 and 'metric-value' in item:
         # Events that generate >1 metric may have isolated metric
         # values and possibly other prefixes like interval, core,
         # counters, or event-runtime/pcnt-running from multiplexing.
         pass
-      elif count not in expected_items and count >= 1 and count <= 5 and 'metricgroup' in item:
+      elif count not in expected_count and count >= 1 and count <= 5 and 'metricgroup' in item:
         pass
-      elif count - 1 in expected_items and 'metric-threshold' in item:
+      elif count - 1 in expected_count and 'metric-threshold' in item:
           pass
-      elif count in expected_items and 'insn per cycle' in item:
+      elif count in expected_count and 'insn per cycle' in item:
           pass
-      elif count not in expected_items:
-        raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_items}'
+      elif count not in expected_count:
+        raise RuntimeError(f'wrong number of fields. counted {count} expected {expected_count}'
                            f' in \'{item}\'')
     for key, value in item.items():
       if key not in checks:
@@ -107,7 +107,7 @@ try:
     expected_items = [1, 2]
   else:
     # If no option is specified, don't check the number of items.
-    expected_items = -1
+    expected_items = []
   check_json_output(expected_items)
 except:
   print('Test failed for input:\n' + '\n'.join(Lines))
tools/perf/tests/shell/lib/perf_metric_validation.py +26 -27

166 lines not on this page · raw message

tools/perf/tests/shell/lib/setup_python.sh +25 -4
index 2173215a0517..758834f8dcf8 100644
@@ -1,29 +1,50 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
 
-if [ "x$PYTHON" = "x" ]
+if [ -z "$PYTHON" ]
 then
   python3 --version >/dev/null 2>&1 && PYTHON=python3
 fi
-if [ "x$PYTHON" = "x" ]
+if [ -z "$PYTHON" ]
 then
   python --version >/dev/null 2>&1 && PYTHON=python
 fi
-if [ "x$PYTHON" = "x" ]
+if [ -z "$PYTHON" ]
 then
   echo Skipping test, python not detected please set environment variable PYTHON.
   exit 2
 fi
+export PYTHON
 
-# Set PYTHONPATH to find the in-tree built perf.so first, avoiding system-wide perf.so
+# Set PYTHONPATH to find the built perf.so and standalone scripts 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
   PYTHONPATH_DIR="$(dirname "$0")/../python"
 fi
 
+if [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
+  SRC_PYTHON_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../python" && pwd)"
+  export PYTHONPATH="$SRC_PYTHON_DIR${PYTHONPATH:+:$PYTHONPATH}"
+  if [ -z "$PERF_EXEC_PATH" ] || [ ! -f "$PERF_EXEC_PATH/python/perf_live.py" ]; then
+    PERF_EXEC_PATH="$(dirname "$SRC_PYTHON_DIR")"
+    export PERF_EXEC_PATH
+  fi
+fi
+
 if [ -n "$PYTHONPATH_DIR" ]; then
   export PYTHONPATH="$PYTHONPATH_DIR${PYTHONPATH:+:$PYTHONPATH}"
 fi
+
+PERF_BIN=$(which perf 2>/dev/null || true)
+if [ -n "$PERF_BIN" ] && [ -d "$(dirname "$PERF_BIN")/python" ]; then
+  PERF_BIN_PYTHON="$(dirname "$PERF_BIN")/python"
+  export PYTHONPATH="$PERF_BIN_PYTHON${PYTHONPATH:+:$PYTHONPATH}"
+fi
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#331 lore raw

PATCH v4 13/49 perf build: Make mypy build test opt-out (NO_MYPY=1)

In reply to Ian Rogers

Commit 168910d0f937 ("perf build: Add mypy build tests") added a compile-time mypy type check for Python files in tools/perf, but left it opt-in via MYPY=1.

Now that mypy issues across tools/perf/python, tools/perf/pmu-events, and tools/perf/tests/shell/lib have been cleaned up, enable the mypy check by default when mypy is installed on the system, with an opt-out via NO_MYPY=1 (matching NO_SHELLCHECK=1 and other perf build feature flags). Also configure MYPYPATH in Makefile.perf so scripts can resolve perf.pyi type stubs and local modules, pass --ignore-missing-imports so optional third-party packages do not fail default builds, and remove *.mypy_log files during make clean.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Build            |  2 +-
 tools/perf/Makefile.perf    | 26 ++++++++++++++++++++++----
 tools/perf/pmu-events/Build |  2 +-
 tools/perf/tests/Build      |  2 +-
 tools/perf/util/Build       |  2 +-
 5 files changed, 26 insertions(+), 8 deletions(-)
tools/perf/Build +1 -1
index e18c80a5c1bc..37d603faea6d 100644
@@ -88,7 +88,7 @@ endif
 
 $(OUTPUT)%.mypy_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-y += $(MYPY_TEST_LOGS)
 
tools/perf/Makefile.perf +22 -4
index e91d6138330e..fbb5dfb67089 100644
@@ -126,6 +126,8 @@ include ../scripts/utilities.mak
 # Define GEN_VMLINUX_H to generate vmlinux.h from the BTF.
 #
 # Define NO_SHELLCHECK if you do not want to run shellcheck during build
+#
+# Define NO_MYPY if you do not want to run mypy during build
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
@@ -259,9 +261,24 @@ ifneq ($(SHELLCHECK),)
   endif
 endif
 
-# Runs mypy on perf python files
-ifeq ($(MYPY),1)
-  MYPY := $(shell which mypy 2> /dev/null)
+# Runs mypy on perf python files.
+# Wait until after the fixdep bootstrap sub-make (force_fixdep != 1) so
+# command-line overrides (MYPY=1, MYPY=/custom/path, NO_MYPY=1) reach the
+# main Makefile.perf pass intact, then strip MYPY=% from MAKEOVERRIDES
+# (substituting escaped spaces so multi-word overrides like MYPY="mypy --strict"
+# are removed as a single token) so child Makefile.build sub-makes inherit the
+# MYPYPATH-wrapped export MYPY environment variable.
+ifneq ($(force_fixdep),1)
+  MAKEOVERRIDES := $(subst __SPACE__,\ ,$(filter-out MYPY=%,$(subst \ ,__SPACE__,$(MAKEOVERRIDES))))
+  ifeq ($(NO_MYPY),1)
+    override MYPY :=
+  else ifeq ($(filter-out 1,$(MYPY)),)
+    override MYPY := $(shell which mypy 2> /dev/null)
+  endif
+
+  ifneq ($(MYPY),)
+    override MYPY := MYPYPATH=$(srctree)/tools/perf/python $(MYPY) --ignore-missing-imports
+  endif
 endif
 
 # Runs pylint on perf python files
@@ -945,7 +962,8 @@ clean:: $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBSYMBOL)-clean $(
 	$(call QUIET_CLEAN, core-objs)  $(RM) $(LIBPERF_A) $(OUTPUT)perf-archive \
 		$(OUTPUT)perf-iostat $(LANG_BINDINGS)
 	$(Q)find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '*.a' -delete -o \
-		-name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete
+		-name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete -o \
+		-name '*.mypy_log' -delete
 	$(Q)$(RM) $(OUTPUT).config-detected
 	$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 \
 		perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI).so
tools/perf/pmu-events/Build +1 -1
index 372773b998e4..c3542e5ae46f 100644
@@ -187,7 +187,7 @@ JEVENTS_DEPS := $(OUT_JSON) $(GEN_JSON) $(JEVENTS_PY) $(METRIC_PY) $(EMPTY_PMU_E
 ifdef MYPY
 define MYPY_RULE
 $(2): $(1)
-	$$(Q)$$(call echo-cmd,test)mypy $(1) > $(2) || (cat $(2) && rm $(2) && false)
+	$$(Q)$$(call echo-cmd,test)$$(MYPY) $(1) > $(2) || (cat $(2) && rm $(2) && false)
 endef
 $(foreach src,$(wildcard pmu-events/*.py), \
     $(eval dest := $(patsubst pmu-events/%,$(OUTPUT)pmu-events/%.mypy_log,$(src))) \
tools/perf/tests/Build +1 -1
index 81c311b131b7..cd7b62a6c1b8 100644
@@ -104,7 +104,7 @@ endif
 
 $(OUTPUT)%.mypy_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-test-y += $(MYPY_TEST_LOGS)
 
tools/perf/util/Build +1 -1
index a4618e4e4848..f922794e8108 100644
@@ -426,7 +426,7 @@ endif
 
 $(OUTPUT)%.mypy_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)mypy "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(MYPY) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-util-y += $(MYPY_TEST_LOGS)
 
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#333 lore raw

PATCH v4 14/49 perf build: Make pylint build test opt-out (NO_PYLINT=1)

In reply to Ian Rogers

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 NO_SHELLCHECK=1, NO_MYPY=1, and 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 --ignored-modules=textual,setproctitle) 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).

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Build            |  2 +-
 tools/perf/Makefile.perf    | 40 +++++++++++++++++++++++++++++++++----
 tools/perf/pmu-events/Build |  2 +-
 tools/perf/tests/Build      |  2 +-
 tools/perf/util/Build       |  2 +-
 5 files changed, 40 insertions(+), 8 deletions(-)
tools/perf/Build +1 -1
index 37d603faea6d..5bd0e58d7cb0 100644
@@ -101,6 +101,6 @@ endif
 
 $(OUTPUT)%.pylint_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)pylint "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(PYLINT) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-y += $(PYLINT_TEST_LOGS)
tools/perf/Makefile.perf +36 -4
index fbb5dfb67089..168b286770f3 100644
@@ -128,6 +128,8 @@ include ../scripts/utilities.mak
 # Define NO_SHELLCHECK if you do not want to run shellcheck during build
 #
 # Define NO_MYPY if you do not want to run mypy during build
+#
+# Define NO_PYLINT if you do not want to run pylint during build
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
@@ -281,9 +283,39 @@ ifneq ($(force_fixdep),1)
   endif
 endif
 
-# Runs pylint on perf python files
-ifeq ($(PYLINT),1)
-  PYLINT := $(shell which pylint 2> /dev/null)
+# Runs pylint on perf python files.
+# Require pylint >= 2.16.0 (which introduced W0718 broad-exception-caught and
+# astroid .pyi stub support for perf.pyi).
+# Disabled warnings:
+#   W0123 (eval-used): Use of eval()
+#   W0311 (bad-indentation): Non-4-space indentation
+#   W0511 (fixme): Presence of FIXME/TODO comments
+#   W0603 (global-statement): Use of the global statement
+#   W0622 (redefined-builtin): Redefining a built-in name (e.g. id, type, dir)
+#   W0718 (broad-exception-caught): Catching broad Exception
+# Wait until after the fixdep bootstrap sub-make (force_fixdep != 1) before
+# resolving PYLINT and stripping PYLINT=% from MAKEOVERRIDES (substituting
+# escaped spaces so multi-word overrides are removed as a single token) so child
+# Makefile.build sub-makes inherit the PYTHONPATH- and flag-wrapped export.
+ifneq ($(force_fixdep),1)
+  MAKEOVERRIDES := $(subst __SPACE__,\ ,$(filter-out PYLINT=%,$(subst \ ,__SPACE__,$(MAKEOVERRIDES))))
+  ifeq ($(NO_PYLINT),1)
+    override PYLINT :=
+  else ifeq ($(filter-out 1,$(PYLINT)),)
+    override PYLINT := $(shell which pylint 2> /dev/null)
+  endif
+
+  ifneq ($(PYLINT),)
+    ifneq ($(shell $(PYLINT) --version 2>/dev/null | \
+          awk '/^pylint / { split($$2, v, "."); print (v[1]+0 > 2 || (v[1]+0 == 2 && v[2]+0 >= 16)) }'), 1)
+      $(warning Warning: pylint version is older than 2.16.0, skipping pylint checks.)
+      override PYLINT :=
+    else
+      override PYLINT := PYTHONPATH=$(srctree)/tools/perf/python:$(srctree)/tools/perf/pmu-events \
+        $(PYLINT) --disable=all --enable=E,W --disable=W0123,W0311,W0511,W0603,W0622,W0718 \
+        --ignored-modules=textual,setproctitle
+    endif
+  endif
 endif
 
 export srctree OUTPUT RM CC CXX RUSTC CLANG LD AR CFLAGS CXXFLAGS RUST_FLAGS V BISON FLEX AWK LIBBPF READELF
@@ -963,7 +995,7 @@ clean:: $(LIBAPI)-clean $(LIBBPF)-clean $(LIBSUBCMD)-clean $(LIBSYMBOL)-clean $(
 		$(OUTPUT)perf-iostat $(LANG_BINDINGS)
 	$(Q)find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '*.a' -delete -o \
 		-name '\.*.cmd' -delete -o -name '\.*.d' -delete -o -name '*.shellcheck_log' -delete -o \
-		-name '*.mypy_log' -delete
+		-name '*.mypy_log' -delete -o -name '*.pylint_log' -delete
 	$(Q)$(RM) $(OUTPUT).config-detected
 	$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf perf-read-vdso32 \
 		perf-read-vdsox32 $(OUTPUT)$(LIBJVMTI).so
tools/perf/pmu-events/Build +1 -1
index c3542e5ae46f..1ac067be9d34 100644
@@ -203,7 +203,7 @@ endif
 ifdef PYLINT
 define PYLINT_RULE
 $(2): $(1)
-	$$(Q)$$(call echo-cmd,test)pylint $(1) > $(2) || (cat $(2) && rm $(2) && false)
+	$$(Q)$$(call echo-cmd,test)$$(PYLINT) $(1) > $(2) || (cat $(2) && rm $(2) && false)
 endef
 $(foreach src,$(wildcard pmu-events/*.py), \
     $(eval dest := $(patsubst pmu-events/%,$(OUTPUT)pmu-events/%.pylint_log,$(src))) \
tools/perf/tests/Build +1 -1
index cd7b62a6c1b8..8fecd5390124 100644
@@ -117,6 +117,6 @@ endif
 
 $(OUTPUT)%.pylint_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)pylint "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(PYLINT) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-test-y += $(PYLINT_TEST_LOGS)
tools/perf/util/Build +1 -1
index f922794e8108..afcf98dc5006 100644
@@ -438,7 +438,7 @@ endif
 
 $(OUTPUT)%.pylint_log: %
 	$(call rule_mkdir)
-	$(Q)$(call echo-cmd,test)pylint "$<" > $@ || (cat $@ && rm $@ && false)
+	$(Q)$(call echo-cmd,test)$(PYLINT) "$<" > $@ || (cat $@ && rm $@ && false)
 
 perf-util-y += $(PYLINT_TEST_LOGS)
 
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#335 lore raw

PATCH v4 15/49 perf Makefile: Install standalone Python scripts during transition

In reply to Ian Rogers

Install the newly ported standalone Python scripts (python/*.py), type stubs (python/perf.pyi), and compiled perf Python extension module (python/perf*.so, when built) into $(perfexec_instdir_SQ)/python alongside the legacy scripts during the transition phase so that newly ported scripts can import perf directly from the installation directory before legacy support is removed.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/Makefile.perf | 7 +++++++
 1 file changed, 7 insertions(+)
tools/perf/Makefile.perf +7 -0
index 168b286770f3..89edf2720fc1 100644
@@ -907,6 +907,13 @@ ifndef NO_LIBPYTHON
 		$(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'
+	$(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' && \
+		$(INSTALL) python/perf.pyi -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python' && \
+		if [ -f "$(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX)" ]; then \
+			$(INSTALL) "$(OUTPUT)python/perf$(PYTHON_EXTENSION_SUFFIX)" -m 644 -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/python'; \
+		fi
 endif
 	$(call QUIET_INSTALL, dlfilters) \
 		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/dlfilters'; \
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#337 lore raw

PATCH v4 16/49 perf python: Port stat-cpi to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/stat-cpi.py                 | 219 ++++++++++++++++++
 .../perf/tests/shell/test_stat_cpi_python.sh  | 115 +++++++++
 2 files changed, 334 insertions(+)
 create mode 100755 tools/perf/python/stat-cpi.py
 create mode 100755 tools/perf/tests/shell/test_stat_cpi_python.sh
tools/perf/python/stat-cpi.py new file +219 -0
new file mode 100755
index 000000000000..0b7d76876a6c
@@ -0,0 +1,219 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""Calculate CPI from perf stat data or live."""
+from __future__ import annotations
+
+import argparse
+import os
+import signal
+import sys
+import time
+from typing import Any, Optional
+import perf
+
+class StatCpiAnalyzer:
+    """Accumulates cycles and instructions and calculates CPI."""
+
+    def __init__(self, args: argparse.Namespace) -> None:
+        self.args = args
+        self.data: dict[str, float] = {}
+        self.prev_data: dict[str, tuple[int, int, int]] = {}
+        self.recorded_pairs: set[tuple[int, int]] = set()
+
+    def get_key(self, event: str, cpu: int, thread: int) -> str:
+        """Get key for data dictionary."""
+        return f"{event}-{cpu}-{thread}"
+
+    def store_key(self, cpu: int, thread: int) -> None:
+        """Store CPU and thread IDs."""
+        self.recorded_pairs.add((cpu, thread))
+
+    def store(self, event: str, cpu: int, thread: int,
+              counts: tuple[int, int, int], is_delta: bool = False,
+              raw_name: Optional[str] = None) -> None:
+        """Store counter values, computing difference from previous
+        absolute values if not already deltas."""
+        self.store_key(cpu, thread)
+        key = self.get_key(event, cpu, thread)
+        prev_key = self.get_key(raw_name or event, cpu, thread)
+
+        val, ena, run = counts
+        if is_delta:
+            # counts are already deltas
+            cur_val = val
+            cur_ena = ena
+            cur_run = run
+        else:
+            if prev_key in self.prev_data:
+                prev_val, prev_ena, prev_run = self.prev_data[prev_key]
+                cur_val = val - prev_val
+                cur_ena = ena - prev_ena
+                cur_run = run - prev_run
+            else:
+                cur_val = val
+                cur_ena = ena
+                cur_run = run
+            self.prev_data[prev_key] = counts  # Store absolute value for next time
+
+        # Scale each raw event's delta by its own multiplexing ratio before
+        # summing across PMUs (e.g. cpu_core and cpu_atom on hybrid systems) so
+        # enabled time from an idle PMU does not inflate the multiplier of an
+        # active PMU.
+        scaled_val = cur_val * (cur_ena / float(cur_run)) if cur_run > 0 else float(cur_val)
+        self.data[key] = self.data.get(key, 0.0) + scaled_val
+
+    def get(self, event: str, cpu: int, thread: int) -> float:
+        """Get scaled counter value."""
+        key = self.get_key(event, cpu, thread)
+        return self.data.get(key, 0.0)
+
+    @staticmethod
+    def _classify_event(name: str) -> Optional[str]:
+        """Classify an event name as 'cycles' or 'instructions'."""
+        ev = name[6:-1] if name.startswith("evsel(") and name.endswith(")") else name
+        ev = ev.split(":", 1)[0]
+        if "/" in ev:
+            parts = [p for p in ev.split("/") if p]
+            if len(parts) >= 2:
+                ev = parts[1]
+        if ev in ("cycles", "cpu-cycles"):
+            return "cycles"
+        if ev == "instructions":
+            return "instructions"
+        return None
+
+    def process_stat_event(self, event: Any, name: Optional[str] = None) -> None:
+        """Process PERF_RECORD_STAT and PERF_RECORD_STAT_ROUND events."""
+        if event.type == perf.RECORD_STAT:
+            if name:
+                event_name = self._classify_event(name)
+                if not event_name:
+                    return
+                self.store(event_name, event.cpu, event.thread,
+                           (event.val, event.ena, event.run), raw_name=name)
+        elif event.type == perf.RECORD_STAT_ROUND:
+            timestamp = getattr(event, "time", 0)
+            self.print_interval(timestamp)
+            self.data.clear()
+            self.recorded_pairs.clear()
+
+    def print_interval(self, timestamp: int) -> None:
+        """Print CPI for the current interval."""
+        for cpu, thread in sorted(self.recorded_pairs):
+            cyc = self.get("cycles", cpu, thread)
+            ins = self.get("instructions", cpu, thread)
+            cpi = 0.0
+            if ins != 0:
+                cpi = cyc / float(ins)
+            t_sec = timestamp / 1000000000.0
+            print(f"{t_sec:15f}: cpu {cpu}, thread {thread} -> cpi {cpi:f} ({cyc:.0f}/{ins:.0f})")
+
+    def read_counters(self, evlist: Any) -> None:
+        """Read counters live."""
+        for evsel in evlist:
+            name = str(evsel)
+            event_name = self._classify_event(name)
+            if not event_name:
+                continue
+
+            for cpu in evsel.cpus():
+                for thread in evsel.threads():
+                    try:
+                        counts = evsel.read(cpu, thread)
+                        self.store(event_name, cpu, thread,
+                                   (counts.val, counts.ena, counts.run),
+                                   is_delta=True, raw_name=name)
+                    except OSError:
+                        pass
+
+    def run_file(self) -> None:
+        """Process events from file."""
+        session: Optional[perf.session] = perf.session(
+            perf.data(self.args.input), stat=self.process_stat_event
+        )
+        try:
+            assert session is not None
+            session.process_events()
+        finally:
+            session = None
+
+    def _open_live_evlist(self) -> Any:
+        """Open evlist for live mode, falling back to user-space or process scope on EACCES."""
+        threads = perf.thread_map(self.args.pid) if self.args.pid else None
+        candidates = [
+            ("cycles,instructions", threads),
+            ("cycles:u,instructions:u", threads),
+        ]
+        if threads is None:
+            self_threads = perf.thread_map(os.getpid())
+            candidates.append(("cycles,instructions", self_threads))
+            candidates.append(("cycles:u,instructions:u", self_threads))
+
+        last_err: Optional[OSError] = None
+        for events, tmap in candidates:
+            try:
+                evlist = perf.parse_events(events, None, tmap)
+                for evsel in evlist:
+                    evsel.read_format |= (
+                        perf.FORMAT_TOTAL_TIME_ENABLED | perf.FORMAT_TOTAL_TIME_RUNNING
+                    )
+                evlist.open()
+                evlist.enable()
+                return evlist
+            except PermissionError as e:
+                last_err = e
+            except OSError as e:
+                if e.errno == 13:
+                    last_err = e
+                else:
+                    raise
+        if last_err is not None:
+            raise last_err
+        raise RuntimeError("Failed to open events")
+
+    def run_live(self) -> None:
+        """Read counters live."""
+        try:
+            evlist = self._open_live_evlist()
+        except OSError as e:
+            print(f"Failed to open events: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        def handle_signal(_signum: int, _frame: Any) -> None:
+            raise KeyboardInterrupt
+
+        signal.signal(signal.SIGINT, signal.default_int_handler)
+        signal.signal(signal.SIGTERM, handle_signal)
+
+        print("Live mode started. Press Ctrl+C to stop.")
+        try:
+            while True:
+                time.sleep(self.args.interval)
+                timestamp = time.time_ns()
+                self.read_counters(evlist)
+                self.print_interval(timestamp)
+                self.data.clear()
+                self.recorded_pairs.clear()
+        except KeyboardInterrupt:
+            print("\nStopped.")
+        finally:
+            evlist.close()
+
+def main() -> None:
+    """Main function."""
+    ap = argparse.ArgumentParser(description="Calculate CPI from perf stat data or live")
+    ap.add_argument("-i", "--input", help="Input file name (enables file mode)")
+    ap.add_argument("-I", "--interval", type=float, default=1.0,
+                    help="Interval in seconds for live mode")
+    ap.add_argument("-p", "--pid", type=int,
+                    help="Monitor specific process ID in live mode")
+    args = ap.parse_args()
+
+    analyzer = StatCpiAnalyzer(args)
+    if args.input:
+        analyzer.run_file()
+    else:
+        analyzer.run_live()
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_stat_cpi_python.sh new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#339 lore raw

PATCH v4 17/49 perf python: Port mem-phys-addr to perf module

In reply to Ian Rogers

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).

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/mem-phys-addr.py            | 136 ++++++++++++++++++
 .../tests/shell/test_mem_phys_addr_python.sh  | 102 +++++++++++++
 2 files changed, 238 insertions(+)
 create mode 100755 tools/perf/python/mem-phys-addr.py
 create mode 100755 tools/perf/tests/shell/test_mem_phys_addr_python.sh
tools/perf/python/mem-phys-addr.py new file +136 -0
new file mode 100755
index 000000000000..53ef7e9a14b5
@@ -0,0 +1,136 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""mem-phys-addr.py: Resolve physical address samples"""
+from __future__ import annotations
+import argparse
+import bisect
+import collections
+from dataclasses import dataclass
+import re
+from typing import (Dict, List, Optional)
+
+import perf
+
+@dataclass(frozen=True)
+class IomemEntry:
+    """Read from a line in /proc/iomem"""
+    begin: int
+    end: int
+    indent: int
+    label: str
+
+    def __lt__(self, other) -> bool:
+        if isinstance(other, int):
+            return self.begin < other
+        return self.begin < other.begin
+
+    def __gt__(self, other) -> bool:
+        if isinstance(other, int):
+            return self.begin > other
+        return self.begin > other.begin
+
+# Physical memory layout from /proc/iomem. Key is the indent and then
+# a list of ranges.
+iomem: Dict[int, List[IomemEntry]] = collections.defaultdict(list)
+# Child nodes from the iomem parent.
+children: Dict[IomemEntry, List[IomemEntry]] = collections.defaultdict(list)
+# Maximum indent seen before an entry in the iomem file.
+_STATE: Dict[str, int] = {"max_indent": 0}
+# Per-event counts for each range of memory.
+event_counts: Dict[str, collections.Counter] = collections.defaultdict(collections.Counter)
+
+def parse_iomem(iomem_path: str):
+    """Populate iomem from iomem file"""
+    with open(iomem_path, 'r', encoding='ascii') as f:
+        for line in f:
+            line = line.rstrip('\n')
+            if not line or line.isspace():
+                continue
+            indent = 0
+            while indent < len(line) and line[indent] == ' ':
+                indent += 1
+            _STATE["max_indent"] = max(_STATE["max_indent"], indent)
+            m = re.split('-|:', line, maxsplit=2)
+            if len(m) < 3:
+                continue
+            begin = int(m[0].strip(), 16)
+            end = int(m[1].strip(), 16)
+            label = m[2].strip()
+            entry = IomemEntry(begin, end, indent, label)
+            # Before adding entry, search for a parent node using its begin.
+            if indent > 0:
+                parent = find_memory_type(begin)
+                assert parent, f"Given indent expected a parent for {label}"
+                children[parent].append(entry)
+            iomem[indent].append(entry)
+
+def find_memory_type(phys_addr) -> Optional[IomemEntry]:
+    """Search iomem for the range containing phys_addr with the maximum indent"""
+    for i in range(_STATE["max_indent"], -1, -1):
+        if i not in iomem:
+            continue
+        position = bisect.bisect_right(iomem[i], phys_addr)
+        if position == 0:
+            continue
+        iomem_entry = iomem[i][position-1]
+        if  iomem_entry.begin <= phys_addr <= iomem_entry.end:
+            return iomem_entry
+    return None
+
+def _print_entries(entries, load_mem_type_cnt, total):
+    """Print counts from parents down to their children"""
+    for entry in sorted(entries,
+                        key=lambda e: (-load_mem_type_cnt[e], e.begin)):
+        count = load_mem_type_cnt[entry]
+        if count > 0:
+            mem_type = ' ' * entry.indent + f"{entry.begin:x}-{entry.end:x} : {entry.label}"
+            percent = 100 * count / total
+            print(f"{mem_type:<40}  {count:>10}  {percent:>10.1f}")
+            _print_entries(children[entry], load_mem_type_cnt, total)
+
+def print_memory_type():
+    """Print the resolved memory types and their counts."""
+    if not event_counts:
+        print("No valid physical address samples found in perf data.")
+        return
+
+    for event_name, load_mem_type_cnt in event_counts.items():
+        print(f"Event: {event_name}")
+        print(f"{'Memory type':<40}  {'count':>10}  {'percentage':>10}")
+        print(f"{'-' * 40:<40}  {'-' * 10:>10}  {'-' * 10:>10}")
+        total = sum(load_mem_type_cnt.values())
+        if total == 0:
+            continue
+
+        # Add count from children into the parent.
+        for i in range(_STATE["max_indent"], -1, -1):
+            if i not in iomem:
+                continue
+            for entry in iomem[i]:
+                for child in children[entry]:
+                    if load_mem_type_cnt[child] > 0:
+                        load_mem_type_cnt[entry] += load_mem_type_cnt[child]
+
+        _print_entries(iomem[0], load_mem_type_cnt, total)
+        print()
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(description="Resolve physical address samples")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("--iomem", default="/proc/iomem", help="Path to iomem file")
+    args = ap.parse_args()
+
+    def process_event(sample):
+        """Process a single sample event."""
+        phys_addr = sample.sample_phys_addr or 0
+        if not phys_addr:
+            return
+        entry = find_memory_type(phys_addr)
+        if entry:
+            event_name = str(sample.evsel)
+            if event_name.startswith("evsel(") and event_name.endswith(")"):
+                event_name = event_name[6:-1]
+            event_counts[event_name][entry] += 1
+
+    parse_iomem(args.iomem)
+    perf.session(perf.data(args.input), sample=process_event).process_events()
+    print_memory_type()
tools/perf/tests/shell/test_mem_phys_addr_python.sh new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#341 lore raw

PATCH v4 18/49 perf python: Port stackcollapse to perf module

In reply to Ian Rogers

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).

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
---
 tools/perf/python/stackcollapse.py            | 145 ++++++++++++++++++
 .../tests/shell/test_stackcollapse_python.sh  |  78 ++++++++++
 2 files changed, 223 insertions(+)
 create mode 100755 tools/perf/python/stackcollapse.py
 create mode 100755 tools/perf/tests/shell/test_stackcollapse_python.sh
tools/perf/python/stackcollapse.py new file +145 -0
new file mode 100755
index 000000000000..0e8a65969db3
@@ -0,0 +1,145 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+stackcollapse.py - format perf samples with one line per distinct call stack
+
+This script's output has two space-separated fields.  The first is a semicolon
+separated stack including the program name (from the "comm" field) and the
+function names from the call stack.  The second is a count:
+
+ swapper;start_kernel;rest_init;cpu_idle;default_idle;native_safe_halt 2
+
+The file is sorted according to the first field.
+
+Ported from tools/perf/scripts/python/stackcollapse.py
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import os
+import sys
+import perf
+
+
+class StackCollapseAnalyzer:
+    """Accumulates call stacks and prints them collapsed."""
+
+    def __init__(self, args: argparse.Namespace) -> None:
+        self.args = args
+        self.lines: dict[str, int] = defaultdict(int)
+        self.session: perf.session | None = None
+
+    def tidy_function_name(self, sym: str, dso: str) -> str:
+        """Beautify function names based on options."""
+        if sym is None:
+            sym = "[unknown]"
+
+        sym = sym.replace(";", ":")
+        if self.args.tidy_java:
+            # Beautify Java signatures
+            sym = sym.replace("<", "")
+            sym = sym.replace(">", "")
+            if sym.startswith("L") and "/" in sym:
+                sym = sym[1:]
+            try:
+                sym = sym[:sym.index("(")]
+            except ValueError:
+                pass
+
+        if self.args.annotate_kernel and dso == "[kernel.kallsyms]":
+            return sym + "_[k]"
+        return sym
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Collect call stack for each sample."""
+        stack = []
+        callchain = sample.callchain
+        if callchain is not None:
+            for node in callchain:
+                stack.append(self.tidy_function_name(node.symbol, node.dso))
+        else:
+            # Fallback if no callchain
+            sym = (sample.symbol or '[unknown]')
+            dso = (sample.dso or '[unknown]')
+            stack.append(self.tidy_function_name(sym, dso))
+
+        if self.args.include_comm:
+            comm = "Unknown"
+            if self.session is not None:
+                try:
+                    proc = self.session.find_thread(
+                        sample.sample_pid, sample.sample_tid
+                    )
+                    if proc:
+                        proc_comm = proc.comm()
+                        if proc_comm is not None:
+                            comm = proc_comm
+                except TypeError:
+                    pass
+            comm = str(comm).replace(" ", "_")
+            sep = "-"
+            if self.args.include_pid:
+                comm = f"{comm}{sep}{(sample.sample_pid or 0)}"
+                sep = "/"
+            if self.args.include_tid:
+                comm = f"{comm}{sep}{(sample.sample_tid or 0)}"
+            stack.append(comm)
+
+        stack_string = ";".join(reversed(stack))
+        self.lines[stack_string] += 1
+
+    def print_totals(self) -> None:
+        """Print sorted collapsed stacks."""
+        try:
+            for stack in sorted(self.lines):
+                print(f"{stack} {self.lines[stack]}")
+            sys.stdout.flush()
+        except BrokenPipeError:
+            devnull = os.open(os.devnull, os.O_WRONLY)
+            os.dup2(devnull, sys.stdout.fileno())
+            os.close(devnull)
+
+
+def main():
+    """Main function."""
+    ap = argparse.ArgumentParser(
+        description="Format perf samples with one line per distinct call stack"
+    )
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("--include-tid", action="store_true", help="include thread id in stack")
+    ap.add_argument("--include-pid", action="store_true", help="include process id in stack")
+    ap.add_argument("--no-comm", dest="include_comm", action="store_false", default=True,
+                    help="do not separate stacks according to comm")
+    ap.add_argument("--tidy-java", action="store_true", help="beautify Java signatures")
+    ap.add_argument("--kernel", dest="annotate_kernel", action="store_true",
+                    help="annotate kernel functions with _[k]")
+
+    args = ap.parse_args()
+
+    if args.include_tid and not args.include_comm:
+        print("requesting tid but not comm is invalid", file=sys.stderr)
+        sys.exit(1)
+    if args.include_pid and not args.include_comm:
+        print("requesting pid but not comm is invalid", file=sys.stderr)
+        sys.exit(1)
+
+    analyzer = StackCollapseAnalyzer(args)
+
+    try:
+        session = perf.session(perf.data(args.input), sample=analyzer.process_event)
+        analyzer.session = session
+        session.process_events()
+    except IOError as e:
+        print(f"Error: {e}", file=sys.stderr)
+        sys.exit(1)
+    except KeyboardInterrupt:
+        pass
+    finally:
+        analyzer.session = None
+
+    analyzer.print_totals()
+
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_stackcollapse_python.sh new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#343 lore raw

PATCH v4 19/49 perf python: Port flamegraph to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/flamegraph.py               | 283 ++++++++++++++++++
 .../tests/shell/test_flamegraph_python.sh     | 122 ++++++++
 2 files changed, 405 insertions(+)
 create mode 100755 tools/perf/python/flamegraph.py
 create mode 100755 tools/perf/tests/shell/test_flamegraph_python.sh
tools/perf/python/flamegraph.py new file +283 -0
new file mode 100755
index 000000000000..1b06a11fa86c
@@ -0,0 +1,283 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+flamegraph.py - create flame graphs from perf samples using perf python module
+"""
+from __future__ import annotations
+
+import argparse
+import hashlib
+import json
+import os
+import re
+import subprocess
+import sys
+import urllib.request
+from typing import Dict, Optional, Union
+import perf
+
+MINIMAL_HTML = """<head>
+  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3-flamegraph.css" integrity="sha256-fXXWjLZigZo3QBJIhjEA3Z2zUfS+vzMQhF2Rlvurfio=" crossorigin="anonymous">
+</head>
+<body>
+  <div id="chart"></div>
+  <script type="text/javascript" src="https://d3js.org/d3.v7.js" integrity="sha256-DA1YviEGwU4kL8dbwJoXRngAYwdRWAZl9ulVKgwGTqI=" crossorigin="anonymous"></script>
+  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/d3-flamegraph.min.js" integrity="sha256-GEbx5jbtG9LDL8h9HiTWOv9+Txhim0u9gZNR8J6iI1M=" crossorigin="anonymous"></script>
+  <script type="text/javascript">
+  const stacks = [/** @flamegraph_json **/];
+  // Note, options is unused.
+  const options = [/** @options_json **/];
+
+  var chart = flamegraph();
+  d3.select("#chart")
+        .datum(stacks[0])
+        .call(chart);
+  </script>
+</body>
+"""
+
+class Node:
+    """A node in the flame graph tree."""
+    def __init__(self, name: str, libtype: str):
+        self.name = name
+        self.libtype = libtype
+        self.value: int = 0
+        self.children: dict[str, Node] = {}
+
+    def to_json(self) -> Dict[str, Union[str, int, list[Dict]]]:
+        """Convert the node to a JSON-serializable dictionary."""
+        return {
+            "n": self.name,
+            "l": self.libtype,
+            "v": self.value,
+            "c": [x.to_json() for x in self.children.values()]
+        }
+
+
+class FlameGraphCLI:
+    """Command-line interface for generating flame graphs."""
+    def __init__(self, args):
+        self.args = args
+        self.stack = Node("all", "root")
+        self.session = None
+
+    @staticmethod
+    def get_libtype_from_dso(dso: Optional[str]) -> str:
+        """Determine the library type from the DSO name."""
+        if dso and (dso == "[kernel.kallsyms]" or dso.endswith("/vmlinux") or dso == "[kernel]"):
+            return "kernel"
+        return ""
+
+    @staticmethod
+    def find_or_create_node(node: Node, name: str, libtype: str) -> Node:
+        """Find a child node with the given name or create a new one."""
+        if name in node.children:
+            return node.children[name]
+        child = Node(name, libtype)
+        node.children[name] = child
+        return child
+
+    def process_event(self, sample) -> None:
+        """Process a single perf sample event."""
+        if self.args.event_name and self.args.event_name not in str(sample.evsel):
+            return
+
+        pid = sample.sample_pid
+        dso_type = ""
+        try:
+            thread = self.session.find_thread(sample.sample_pid, sample.sample_tid)
+            comm = (thread.comm() if thread else None) or "[unknown]"
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+            comm = "[unknown]"
+
+        if pid == 0:
+            comm = comm if comm != "[unknown]" else "swapper"
+            dso_type = "kernel"
+        else:
+            comm = f"{comm} ({pid})"
+
+        node = self.find_or_create_node(self.stack, comm, dso_type)
+
+        callchain = sample.callchain
+        if callchain:
+            # We want to traverse from root to leaf.
+            # perf callchain iterator gives leaf to root.
+            # We collect them and reverse.
+            frames = list(callchain)
+            for entry in reversed(frames):
+                name = entry.symbol or "[unknown]"
+                libtype = self.get_libtype_from_dso(entry.dso)
+                node = self.find_or_create_node(node, name, libtype)
+        else:
+            # Fallback if no callchain
+            name = (sample.symbol or '[unknown]')
+            libtype = self.get_libtype_from_dso((sample.dso or '[unknown]'))
+            node = self.find_or_create_node(node, name, libtype)
+
+        node.value += 1
+
+    def get_report_header(self) -> str:
+        """Get the header from the perf report."""
+        try:
+            input_file = self.args.input or "perf.data"
+            if input_file == "-":
+                return ""
+            mode = os.stat(input_file).st_mode
+            import stat
+            if stat.S_ISFIFO(mode) or stat.S_ISCHR(mode):
+                return ""
+            output = subprocess.check_output(["perf", "report", "--header-only", "-i", input_file])
+            result = output.decode("utf-8")
+            if self.args.event_name:
+                result += "\nFocused event: " + self.args.event_name
+            return result
+        except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError,
+                subprocess.CalledProcessError):
+            return ""
+
+    def run(self) -> None:
+        """Run the flame graph generation."""
+        input_file = self.args.input or "perf.data"
+        if input_file != "-" and not os.path.exists(input_file):
+            print(f"Error: {input_file} not found. (try 'perf record' first)", file=sys.stderr)
+            sys.exit(1)
+
+        try:
+            self.session = perf.session(perf.data(input_file),
+                                        sample=self.process_event)
+        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:
+            # perf.session is a C extension type that holds a strong reference to the
+            # bound method self.process_event (which references self) and does not
+            # implement cyclic GC (tp_traverse). Explicitly clear self.session to break
+            # the reference cycle and free the underlying session resources deterministically.
+            self.session = None
+
+        stacks_json = json.dumps(self.stack, default=lambda x: x.to_json())
+        # Escape HTML special characters to prevent XSS
+        stacks_json = stacks_json.replace("<", "\\u003c") \
+            .replace(">", "\\u003e").replace("&", "\\u0026")
+
+        if self.args.format == "html":
+            report_header = self.get_report_header()
+            options = {
+                "colorscheme": self.args.colorscheme,
+                "context": report_header
+            }
+            options_json = json.dumps(options)
+            options_json = options_json.replace("<", "\\u003c") \
+                .replace(">", "\\u003e").replace("&", "\\u0026")
+
+            template = self.args.template
+            template_sha256sum = None
+            output_str = None
+
+            if not os.path.isfile(template):
+                if template.startswith("http://") or template.startswith("https://"):
+                    if not self.args.allow_download:
+                        print("Warning: Downloading templates is disabled. "
+                              "Use --allow-download.", file=sys.stderr)
+                        template = None
+                else:
+                    print(f"Warning: Template file '{template}' not found.", file=sys.stderr)
+                    if self.args.allow_download:
+                        print("Using default CDN template.", file=sys.stderr)
+                        template = (
+                            "https://cdn.jsdelivr.net/npm/[email protected]/dist/templates/"
+                            "d3-flamegraph-base.html"
+                        )
+                        template_sha256sum = (
+                            "f6a4aa7edffda4fb9bd71eb0eb75bc44d0bb34cd9efbd053f6095bc5c28d702b"
+                        )
+                    else:
+                        template = None
+
+            use_minimal = False
+            try:
+                if not template:
+                    use_minimal = True
+                elif template.startswith(("http://", "https://")):
+                    with urllib.request.urlopen(template) as url_template:
+                        output_str = "".join([l.decode("utf-8") for l in url_template.readlines()])
+                else:
+                    with open(template, "r", encoding="utf-8") as f:
+                        output_str = f.read()
+            except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError) as err:
+                print(f"Error reading template {template}: {err}\n", file=sys.stderr)
+                use_minimal = True
+
+            if use_minimal:
+                print("Using internal minimal HTML that refers to d3's web site. JavaScript " +
+                      "loaded this way from a local file may be blocked unless your " +
+                      "browser has relaxed permissions. Run with '--allow-download' to fetch " +
+                      "the full D3 HTML template.", file=sys.stderr)
+                output_str = MINIMAL_HTML
+
+            elif template_sha256sum:
+                assert output_str is not None
+                download_sha256sum = hashlib.sha256(
+                    output_str.encode("utf-8")
+                ).hexdigest()
+                if download_sha256sum != template_sha256sum:
+                    s = None
+                    while s not in ["y", "n"]:
+                        try:
+                            s = input(f"""Unexpected template sha256sum.
+{download_sha256sum} != {template_sha256sum}, for:
+{template}
+continue?[yn] """).lower()
+                        except EOFError:
+                            s = "n"
+                    if s == "n":
+                        sys.exit(1)
+
+            assert output_str is not None
+            replacements = {
+                "/** @options_json **/": options_json,
+                "/** @flamegraph_json **/": stacks_json,
+            }
+            # Use a lambda replacement so re.sub does not interpret backslashes
+            # in the JSON strings (such as \n or \u003c) as regex escape sequences.
+            output_str = re.sub(
+                r"/\*\* @(?:options_json|flamegraph_json) \*\*/",
+                lambda m: replacements[m.group(0)],
+                output_str,
+            )
+            output_fn = self.args.output or "flamegraph.html"
+        else:
+            output_str = stacks_json
+            output_fn = self.args.output or "stacks.json"
+
+        if output_fn == "-":
+            with open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out:
+                out.write(output_str)
+        else:
+            print(f"dumping data to {output_fn}")
+            with open(output_fn, "w", encoding="utf-8") as out:
+                out.write(output_str)
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description="Create flame graphs using perf python module.")
+    parser.add_argument("-f", "--format", default="html", choices=["json", "html"],
+                        help="output file format")
+    parser.add_argument("-o", "--output", help="output file name")
+    parser.add_argument("--template",
+                        default="/usr/share/d3-flame-graph/d3-flamegraph-base.html",
+                        help="path to flame graph HTML template")
+    parser.add_argument("--colorscheme", default="blue-green",
+                        help="flame graph color scheme", choices=["blue-green", "orange"])
+    parser.add_argument("-i", "--input", help="input perf.data file")
+    parser.add_argument("--allow-download", default=False, action="store_true",
+                        help="allow unprompted downloading of HTML template")
+    parser.add_argument("-e", "--event", default="", dest="event_name", type=str,
+                        help="specify the event to generate flamegraph for")
+
+    cli_args = parser.parse_args()
+    cli = FlameGraphCLI(cli_args)
+    cli.run()
tools/perf/tests/shell/test_flamegraph_python.sh new file +122 -0

122 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#345 lore raw

PATCH v4 20/49 perf python: Port gecko to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/gecko.py                  | 411 ++++++++++++++++++++
 tools/perf/tests/shell/test_gecko_python.sh |  95 +++++
 2 files changed, 506 insertions(+)
 create mode 100755 tools/perf/python/gecko.py
 create mode 100755 tools/perf/tests/shell/test_gecko_python.sh
tools/perf/python/gecko.py new file +411 -0
new file mode 100755
index 000000000000..3427e0770156
@@ -0,0 +1,411 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+gecko.py - Convert perf record output to Firefox's gecko profile format
+"""
+from __future__ import annotations
+
+import argparse
+import functools
+import json
+import os
+import sys
+import tempfile
+import threading
+import urllib.parse
+import uuid
+import webbrowser
+from dataclasses import dataclass, field
+from http.server import HTTPServer, SimpleHTTPRequestHandler
+from typing import Dict, List, NamedTuple, Optional, Tuple
+
+import perf
+
+
+# https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L156
+class Frame(NamedTuple):
+    """A single stack frame in the gecko profile format."""
+    string_id: int
+    relevantForJS: bool
+    innerWindowID: int
+    implementation: None
+    optimizations: None
+    line: None
+    column: None
+    category: int
+    subcategory: Optional[int]
+
+
+# https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L216
+class Stack(NamedTuple):
+    """A single stack in the gecko profile format."""
+    prefix_id: Optional[int]
+    frame_id: int
+
+
+# https://github.com/firefox-devtools/profiler/blob/53970305b51b9b472e26d7457fee1d66cd4e2737/src/types/gecko-profile.js#L90
+class Sample(NamedTuple):
+    """A single sample in the gecko profile format."""
+    stack_id: Optional[int]
+    time_ms: float
+    responsiveness: int
+
+
+@dataclass
+class Tables:
+    """Interned tables for the gecko profile format."""
+    frame_table: List[Frame] = field(default_factory=list)
+    string_table: List[str] = field(default_factory=list)
+    string_map: Dict[str, int] = field(default_factory=dict)
+    stack_table: List[Stack] = field(default_factory=list)
+    stack_map: Dict[Tuple[Optional[int], int], int] = field(default_factory=dict)
+    frame_map: Dict[str, int] = field(default_factory=dict)
+
+
+@dataclass
+class Thread:
+    """A builder for a profile of the thread."""
+    comm: str
+    pid: int
+    tid: int
+    user_category: int
+    kernel_category: int
+    samples: List[Sample] = field(default_factory=list)
+    tables: Tables = field(default_factory=Tables)
+
+    def _intern_stack(self, frame_id: int, prefix_id: Optional[int]) -> int:
+        """Gets a matching stack, or saves the new stack. Returns a Stack ID."""
+        key = (prefix_id, frame_id)
+        stack_id = self.tables.stack_map.get(key)
+        if stack_id is None:
+            stack_id = len(self.tables.stack_table)
+            self.tables.stack_table.append(Stack(prefix_id=prefix_id, frame_id=frame_id))
+            self.tables.stack_map[key] = stack_id
+        return stack_id
+
+    def _intern_string(self, string: str) -> int:
+        """Gets a matching string, or saves the new string. Returns a String ID."""
+        string_id = self.tables.string_map.get(string)
+        if string_id is not None:
+            return string_id
+        string_id = len(self.tables.string_table)
+        self.tables.string_table.append(string)
+        self.tables.string_map[string] = string_id
+        return string_id
+
+    def _intern_frame(self, frame_str: str) -> int:
+        """Gets a matching stack frame, or saves the new frame. Returns a Frame ID."""
+        frame_id = self.tables.frame_map.get(frame_str)
+        if frame_id is not None:
+            return frame_id
+        frame_id = len(self.tables.frame_table)
+        self.tables.frame_map[frame_str] = frame_id
+        string_id = self._intern_string(frame_str)
+
+        category = self.user_category
+        if (frame_str.find('kallsyms') != -1 or
+                frame_str.find('/vmlinux') != -1 or
+                frame_str.endswith('.ko)')):
+            category = self.kernel_category
+
+        self.tables.frame_table.append(Frame(
+            string_id=string_id,
+            relevantForJS=False,
+            innerWindowID=0,
+            implementation=None,
+            optimizations=None,
+            line=None,
+            column=None,
+            category=category,
+            subcategory=None,
+        ))
+        return frame_id
+
+    def add_sample(self, comm: str, stack: List[str], time_ms: float) -> None:
+        """Add a timestamped stack trace sample to the thread builder."""
+        if self.comm != comm:
+            self.comm = comm
+
+        prefix_stack_id: Optional[int] = None
+        for frame in stack:
+            frame_id = self._intern_frame(frame)
+            prefix_stack_id = self._intern_stack(frame_id, prefix_stack_id)
+
+        if prefix_stack_id is not None:
+            self.samples.append(Sample(stack_id=prefix_stack_id,
+                                       time_ms=time_ms,
+                                       responsiveness=0))
+
+    def to_json_dict(self) -> Dict:
+        """Converts current Thread to GeckoThread JSON format."""
+        return {
+            "tid": self.tid,
+            "pid": self.pid,
+            "name": self.comm,
+            "markers": {
+                "schema": {
+                    "name": 0,
+                    "startTime": 1,
+                    "endTime": 2,
+                    "phase": 3,
+                    "category": 4,
+                    "data": 5,
+                },
+                "data": [],
+            },
+            "samples": {
+                "schema": {
+                    "stack": 0,
+                    "time": 1,
+                    "responsiveness": 2,
+                },
+                "data": self.samples
+            },
+            "frameTable": {
+                "schema": {
+                    "location": 0,
+                    "relevantForJS": 1,
+                    "innerWindowID": 2,
+                    "implementation": 3,
+                    "optimizations": 4,
+                    "line": 5,
+                    "column": 6,
+                    "category": 7,
+                    "subcategory": 8,
+                },
+                "data": self.tables.frame_table,
+            },
+            "stackTable": {
+                "schema": {
+                    "prefix": 0,
+                    "frame": 1,
+                },
+                "data": self.tables.stack_table,
+            },
+            "stringTable": self.tables.string_table,
+            "registerTime": 0,
+            "unregisterTime": None,
+            "processType": "default",
+        }
+
+
+class CORSRequestHandler(SimpleHTTPRequestHandler):
+    """Enable CORS for requests from profiler.firefox.com."""
+    def __init__(self, *args, allowed_file: Optional[str] = None, **kwargs):
+        self.allowed_file = allowed_file
+        super().__init__(*args, **kwargs)
+
+    def end_headers(self):
+        self.send_header('Access-Control-Allow-Origin', 'https://profiler.firefox.com')
+        super().end_headers()
+
+    def do_GET(self):
+        if self.allowed_file and self.path.split('?')[0] != f'/{self.allowed_file}':
+            self.send_error(403, "Access denied")
+            return
+        super().do_GET()
+
+    def list_directory(self, path):
+        self.send_error(403, "Directory listing forbidden")
+        return None
+
+
+@dataclass
+class CategoryData:
+    """Category configuration for the gecko profile."""
+    user_index: int = 0
+    kernel_index: int = 1
+    categories: List[Dict] = field(default_factory=list)
+
+
+class GeckoCLI:
+    """Command-line interface for converting perf data to Gecko format."""
+    def __init__(self, args: argparse.Namespace) -> None:
+        self.args = args
+        self.tid_to_thread: Dict[int, Thread] = {}
+        self.start_time_ms: Optional[float] = None
+        self.session: Optional[perf.session] = None
+        self.product = args.product
+        self.cat_data = CategoryData(
+            categories=[
+                {
+                    "name": 'User',
+                    "color": args.user_color,
+                    "subcategories": ['Other']
+                },
+                {
+                    "name": 'Kernel',
+                    "color": args.kernel_color,
+                    "subcategories": ['Other']
+                },
+            ]
+        )
+
+    def process_event(self, sample) -> None:
+        """Process a single perf sample event."""
+        if self.args.event_name and self.args.event_name not in str(sample.evsel):
+            return
+
+        # sample_time is in nanoseconds. Gecko wants milliseconds.
+        time_ms = sample.sample_time / 1000000.0
+        pid = sample.sample_pid
+        tid = sample.sample_tid
+
+        if self.start_time_ms is None:
+            self.start_time_ms = time_ms
+
+        try:
+            thread_info = self.session.find_thread(sample.sample_pid, sample.sample_tid) if self.session else None
+            comm = (thread_info.comm() if thread_info is not None else None) or "[unknown]"
+        except AttributeError:
+            comm = "[unknown]"
+
+        stack = []
+        callchain = sample.callchain
+        if callchain:
+            for entry in callchain:
+                symbol = entry.symbol or "[unknown]"
+                dso = entry.dso or "[unknown]"
+                stack.append(f"{symbol} (in {dso})")
+            # Reverse because Gecko wants root first.
+            stack.reverse()
+        else:
+            symbol = sample.symbol or "[unknown]"
+            dso = sample.dso or "[unknown]"
+            stack.append(f"{symbol} (in {dso})")
+
+        thread = self.tid_to_thread.get(tid)
+        if thread is None:
+            thread = Thread(comm=comm, pid=pid, tid=tid,
+                            user_category=self.cat_data.user_index,
+                            kernel_category=self.cat_data.kernel_index)
+            self.tid_to_thread[tid] = thread
+        thread.add_sample(comm=comm, stack=stack, time_ms=time_ms)
+
+    def run(self) -> None:
+        """Run the conversion process."""
+        input_file = self.args.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)
+
+        try:
+            self.session = perf.session(perf.data(input_file), sample=self.process_event)
+        except (OSError, RuntimeError, ValueError) as e:
+            print(f"Error opening session: {e}", file=sys.stderr)
+            sys.exit(1)
+
+        if self.session:
+            try:
+                self.session.process_events()
+            finally:
+                # Break the reference cycle between perf.session and self.process_event
+                # because perf.session lacks cyclic GC support (tp_traverse).
+                self.session = None
+
+        threads = [t.to_json_dict() for t in self.tid_to_thread.values()]
+
+        gecko_profile = {
+            "meta": {
+                "interval": 1,
+                "processType": 0,
+                "product": self.product,
+                "stackwalk": 1,
+                "debug": 0,
+                "gcpoison": 0,
+                "asyncstack": 1,
+                "startTime": self.start_time_ms or 0,
+                "shutdownTime": None,
+                "version": 24,
+                "presymbolicated": True,
+                "categories": self.cat_data.categories,
+                "markerSchema": [],
+            },
+            "libs": [],
+            "threads": threads,
+            "processes": [],
+            "pausedRanges": [],
+        }
+
+        output_file = self.args.save_only
+        if output_file is None:
+            self._write_and_launch(gecko_profile)
+        else:
+            print(f'[ perf gecko: Captured and wrote into {output_file} ]')
+            with open(output_file, 'w', encoding='utf-8') as f:
+                json.dump(gecko_profile, f, indent=2)
+
+    def _write_and_launch(self, profile: Dict) -> None:
+        """Write the profile to a file and launch the Firefox profiler."""
+        print("Starting Firefox Profiler on your default browser...")
+
+        with tempfile.TemporaryDirectory() as tmp_dir_name:
+            profile_name = f'gecko_profile_{uuid.uuid4().hex}.json'
+            filename = os.path.join(tmp_dir_name, profile_name)
+
+            with open(filename, 'w', encoding='utf-8') as f:
+                json.dump(profile, f, indent=2)
+
+            handler = functools.partial(
+                CORSRequestHandler, directory=tmp_dir_name, allowed_file=profile_name
+            )
+            try:
+                httpd = HTTPServer(('127.0.0.1', 0), handler)
+            except OSError as e:
+                print(f"Error starting HTTP server: {e}", file=sys.stderr)
+                sys.exit(1)
+
+            port = httpd.server_port
+
+            def start_server():
+                httpd.serve_forever()
+
+            thread = threading.Thread(target=start_server, daemon=True)
+            thread.start()
+
+            safe_string = urllib.parse.quote_plus(f'http://127.0.0.1:{port}/{profile_name}')
+            url = f'https://profiler.firefox.com/from-url/{safe_string}'
+            print(f"Please open the following URL in your browser to view the profile:\n\n{url}\n")
+            launch_html = os.path.join(tmp_dir_name, 'launch.html')
+            with open(launch_html, 'w', encoding='utf-8') as f:
+                f.write(f'<!DOCTYPE html><html><head>'
+                        f'<meta http-equiv="refresh" content="0;url={url}">'
+                        f'<script>window.location.replace({json.dumps(url)});</script>'
+                        f'</head><body>Redirecting to Firefox Profiler...</body></html>')
+            if not webbrowser.open(f'file://{launch_html}'):
+                print("Failed to open browser, please open the URL manually.")
+
+            print(f'[ perf gecko: Captured and wrote into {filename} ]')
+            print("Press Ctrl+C to stop the local server.")
+            try:
+                # Keep the main thread alive so the daemon thread can serve requests
+                stop_event = threading.Event()
+                while True:
+                    stop_event.wait(1)
+            except KeyboardInterrupt:
+                print("\nStopping server...")
+                httpd.shutdown()
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(
+        description="Convert perf.data to Firefox's Gecko Profile format"
+    )
+    parser.add_argument('--product', default='perf', help='Product name (e.g. perf)')
+    parser.add_argument('--user-color', default='yellow',
+                        help='Color for the User category',
+                        choices=['yellow', 'blue', 'purple', 'green', 'orange', 'red',
+                                 'grey', 'magenta'])
+    parser.add_argument('--kernel-color', default='orange',
+                        help='Color for the Kernel category',
+                        choices=['yellow', 'blue', 'purple', 'green', 'orange', 'red',
+                                 'grey', 'magenta'])
+    parser.add_argument('--save-only',
+                        help='Save the output to a file instead of opening Firefox\'s profiler')
+    parser.add_argument("-i", "--input", help="input perf.data file")
+    parser.add_argument("-e", "--event", default="", dest="event_name", type=str,
+                        help="specify the event to generate gecko profile for")
+
+    cli_args = parser.parse_args()
+    cli = GeckoCLI(cli_args)
+    cli.run()
tools/perf/tests/shell/test_gecko_python.sh new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#347 lore raw

PATCH v4 21/49 perf python: Port event_analyzing_sample to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/event_analyzing_sample.py   | 332 ++++++++++++++++++
 .../test_event_analyzing_sample_python.sh     |  62 ++++
 2 files changed, 394 insertions(+)
 create mode 100755 tools/perf/python/event_analyzing_sample.py
 create mode 100755 tools/perf/tests/shell/test_event_analyzing_sample_python.sh
tools/perf/python/event_analyzing_sample.py new file +332 -0
new file mode 100755
index 000000000000..15b47cff9fa3
@@ -0,0 +1,332 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+General event handler in Python, using SQLite to analyze events.
+
+The 2 database related functions in this script just show how to gather
+the basic information, and users can modify and write their own functions
+according to their specific requirement.
+
+The first function "show_general_events" just does a basic grouping for all
+generic events with the help of sqlite, and the 2nd one "show_pebs_ll" is
+for a x86 HW PMU event: PEBS with load latency data.
+
+Ported from tools/perf/scripts/python/event_analyzing_sample.py
+"""
+from __future__ import annotations
+
+import argparse
+import math
+import os
+import sqlite3
+import struct
+import tempfile
+from typing import Any
+import perf
+
+# Event types, user could add more here
+EVTYPE_GENERIC  = 0
+EVTYPE_PEBS     = 1     # Basic PEBS event
+EVTYPE_PEBS_LL  = 2     # PEBS event with load latency info
+EVTYPE_IBS      = 3
+
+#
+# Currently we don't have good way to tell the event type, but by
+# the size of raw buffer, raw PEBS event with load latency data's
+# size is 176 bytes, while the pure PEBS event's size is 144 bytes.
+#
+def create_event(name, comm, dso, symbol, raw_buf):
+    """Create an event object based on raw buffer size."""
+    if len(raw_buf) == 144:
+        event = PebsEvent(name, comm, dso, symbol, raw_buf)
+    elif len(raw_buf) == 176:
+        event = PebsNHM(name, comm, dso, symbol, raw_buf)
+    else:
+        event = PerfEvent(name, comm, dso, symbol, raw_buf)
+
+    return event
+
+class PerfEvent:
+    """Base class for all perf event samples."""
+    event_num = 0
+    def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_GENERIC):
+        self.name       = name
+        self.comm       = comm
+        self.dso        = dso
+        self.symbol     = symbol
+        self.raw_buf    = raw_buf
+        self.ev_type    = ev_type
+        PerfEvent.event_num += 1
+
+    def show(self):
+        """Display PMU event info."""
+        print(f"PMU event: name={self.name:12s}, symbol={self.symbol:24s}, "
+              f"comm={self.comm:8s}, dso={self.dso:12s}")
+
+#
+# Basic Intel PEBS (Precise Event-based Sampling) event, whose raw buffer
+# contains the context info when that event happened: the EFLAGS and
+# linear IP info, as well as all the registers.
+#
+class PebsEvent(PerfEvent):
+    """Intel PEBS event."""
+    pebs_num = 0
+    def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS):
+        tmp_buf = raw_buf[0:80]
+        flags, ip, ax, bx, cx, dx, si, di, bp, sp = struct.unpack('<QQQQQQQQQQ', tmp_buf)
+        self.flags = flags
+        self.ip    = ip
+        self.ax    = ax
+        self.bx    = bx
+        self.cx    = cx
+        self.dx    = dx
+        self.si    = si
+        self.di    = di
+        self.bp    = bp
+        self.sp    = sp
+
+        super().__init__(name, comm, dso, symbol, raw_buf, ev_type)
+        PebsEvent.pebs_num += 1
+        del tmp_buf
+
+#
+# Intel Nehalem and Westmere support PEBS plus Load Latency info which lie
+# in the four 64 bit words write after the PEBS data:
+#       Status: records the IA32_PERF_GLOBAL_STATUS register value
+#       DLA:    Data Linear Address (EIP)
+#       DSE:    Data Source Encoding, where the latency happens, hit or miss
+#               in L1/L2/L3 or IO operations
+#       LAT:    the actual latency in cycles
+#
+class PebsNHM(PebsEvent):
+    """Intel Nehalem/Westmere PEBS event with load latency."""
+    pebs_nhm_num = 0
+    def __init__(self, name, comm, dso, symbol, raw_buf, ev_type=EVTYPE_PEBS_LL):
+        tmp_buf = raw_buf[144:176]
+        status, dla, dse, lat = struct.unpack('<QQQQ', tmp_buf)
+        self.status = status
+        self.dla = dla
+        self.dse = dse
+        self.lat = lat
+
+        super().__init__(name, comm, dso, symbol, raw_buf, ev_type)
+        PebsNHM.pebs_nhm_num += 1
+        del tmp_buf
+
+session: Any = None
+
+class _DB:
+    con: sqlite3.Connection | None = None
+    temp_path: str | None = None
+
+def trace_begin(db_path: str | None = None) -> None:
+    """Initialize database tables."""
+    print("In trace_begin:\n")
+    if not db_path:
+        fd, db_path = tempfile.mkstemp(prefix="perf_events_", suffix=".db")
+        os.close(fd)
+        _DB.temp_path = db_path
+    con = sqlite3.connect(db_path)
+    try:
+        # Drop any pre-existing tables so repeated runs do not accumulate duplicate events.
+        con.execute("drop table if exists gen_events;")
+        con.execute("drop table if exists pebs_ll;")
+
+        # Will create several tables at the start, pebs_ll is for PEBS data with
+        # load latency info, while gen_events is for general event.
+        con.execute("""
+            create table if not exists gen_events (
+                    name text,
+                    symbol text,
+                    comm text,
+                    dso text
+            );""")
+        con.execute("""
+            create table if not exists pebs_ll (
+                    name text,
+                    symbol text,
+                    comm text,
+                    dso text,
+                    flags integer,
+                    ip integer,
+                    status integer,
+                    dse integer,
+                    dla integer,
+                    lat integer
+            );""")
+    except BaseException:
+        con.close()
+        raise
+    _DB.con = con
+
+def insert_db(event: Any) -> None:
+    """Insert event into database."""
+    con = _DB.con
+    assert con is not None
+    if event.ev_type == EVTYPE_GENERIC:
+        con.execute("insert into gen_events values(?, ?, ?, ?)",
+                    (event.name, event.symbol, event.comm, event.dso))
+    elif event.ev_type == EVTYPE_PEBS_LL:
+        # Python's sqlite3 module only accepts signed 64-bit integers (-2^63 to 2^63 - 1)
+        # and raises OverflowError for unsigned 64-bit kernel pointers (> 0x7fffffffffffffff).
+        # Convert unsigned 64-bit addresses to signed 64-bit two's-complement integers.
+        ip = event.ip - 0x10000000000000000 if event.ip > 0x7fffffffffffffff else event.ip
+        status = (event.status - 0x10000000000000000
+                  if event.status > 0x7fffffffffffffff else event.status)
+        dla = event.dla - 0x10000000000000000 if event.dla > 0x7fffffffffffffff else event.dla
+        con.execute("insert into pebs_ll values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+                    (event.name, event.symbol, event.comm, event.dso, event.flags,
+                     ip, status, event.dse, dla, event.lat))
+
+def process_event(sample: perf.sample_event) -> None:
+    """Callback for processing events."""
+    # Create and insert event object to a database so that user could
+    # do more analysis with simple database commands.
+
+    # Resolve comm, symbol, dso
+    comm = "Unknown_comm"
+    try:
+        if session is not None:
+            proc = session.find_thread(sample.sample_pid, sample.sample_tid)
+            if proc:
+                comm = proc.comm() or "Unknown_comm"
+    except TypeError:
+        pass
+
+    # Symbol and dso info are not always resolved
+    dso = sample.dso if hasattr(sample, 'dso') and sample.dso else "Unknown_dso"
+    symbol = sample.symbol if hasattr(sample, 'symbol') and sample.symbol else "Unknown_symbol"
+    name = str(sample.evsel)
+    if name.startswith("evsel("):
+        name = name[6:-1]
+
+    # Create the event object and insert it to the right table in database
+    try:
+        event = create_event(name, comm, dso, symbol, sample.raw_buf)
+        insert_db(event)
+    except (sqlite3.Error, ValueError, TypeError, OverflowError) as e:
+        print(f"Error creating/inserting event: {e}")
+
+def num2sym(num: int) -> str:
+    """Convert number to a histogram symbol (log2)."""
+    # As the event number may be very big, so we can't use linear way
+    # to show the histogram in real number, but use a log2 algorithm.
+    if num <= 0:
+        return ""
+    snum = '#' * (int(math.log(num, 2)) + 1)
+    return snum
+
+def show_general_events() -> None:
+    """Display statistics for general events."""
+    con = _DB.con
+    assert con is not None
+    count = con.execute("select count(*) from gen_events")
+    for t in count:
+        print(f"There is {t[0]} records in gen_events table")
+        if t[0] == 0:
+            return
+
+    print("Statistics about the general events grouped by thread/symbol/dso: \n")
+
+    # Group by thread
+    commq = con.execute("""
+        select comm, count(comm) from gen_events
+        group by comm order by -count(comm)
+    """)
+    print(f"\n{ 'comm':>16} {'number':>8} {'histogram':>16}\n{'='*42}")
+    for row in commq:
+        print(f"{row[0]:>16} {row[1]:>8}     {num2sym(row[1])}")
+
+    # Group by symbol
+    print(f"\n{'symbol':>32} {'number':>8} {'histogram':>16}\n{'='*58}")
+    symbolq = con.execute("""
+        select symbol, count(symbol) from gen_events
+        group by symbol order by -count(symbol)
+    """)
+    for row in symbolq:
+        print(f"{row[0]:>32} {row[1]:>8}     {num2sym(row[1])}")
+
+    # Group by dso
+    print(f"\n{'dso':>40} {'number':>8} {'histogram':>16}\n{'='*74}")
+    dsoq = con.execute("select dso, count(dso) from gen_events group by dso order by -count(dso)")
+    for row in dsoq:
+        print(f"{row[0]:>40} {row[1]:>8}     {num2sym(row[1])}")
+
+def show_pebs_ll() -> None:
+    """Display statistics for PEBS load latency events."""
+    con = _DB.con
+    assert con is not None
+    # This function just shows the basic info, and we could do more with the
+    # data in the tables, like checking the function parameters when some
+    # big latency events happen.
+    count = con.execute("select count(*) from pebs_ll")
+    for t in count:
+        print(f"There is {t[0]} records in pebs_ll table")
+        if t[0] == 0:
+            return
+
+    print("Statistics about the PEBS Load Latency events grouped by thread/symbol/dse/latency: \n")
+
+    # Group by thread
+    commq = con.execute("select comm, count(comm) from pebs_ll group by comm order by -count(comm)")
+    print(f"\n{'comm':>16} {'number':>8} {'histogram':>16}\n{'='*42}")
+    for row in commq:
+        print(f"{row[0]:>16} {row[1]:>8}     {num2sym(row[1])}")
+
+    # Group by symbol
+    print(f"\n{'symbol':>32} {'number':>8} {'histogram':>16}\n{'='*58}")
+    symbolq = con.execute("""
+        select symbol, count(symbol) from pebs_ll
+        group by symbol order by -count(symbol)
+    """)
+    for row in symbolq:
+        print(f"{row[0]:>32} {row[1]:>8}     {num2sym(row[1])}")
+
+    # Group by dse
+    dseq = con.execute("select dse, count(dse) from pebs_ll group by dse order by -count(dse)")
+    print(f"\n{'dse':>32} {'number':>8} {'histogram':>16}\n{'='*58}")
+    for row in dseq:
+        print(f"{row[0]:>32} {row[1]:>8}     {num2sym(row[1])}")
+
+    # Group by latency
+    latq = con.execute("select lat, count(lat) from pebs_ll group by lat order by lat")
+    print(f"\n{'latency':>32} {'number':>8} {'histogram':>16}\n{'='*58}")
+    for row in latq:
+        print(f"{str(row[0]):>32} {row[1]:>8}     {num2sym(row[1])}")
+
+def trace_end() -> None:
+    """Called at the end of trace processing."""
+    print("In trace_end:\n")
+    try:
+        if _DB.con:
+            try:
+                _DB.con.commit()
+                show_general_events()
+                show_pebs_ll()
+            finally:
+                _DB.con.close()
+                _DB.con = None
+    finally:
+        if _DB.temp_path and os.path.exists(_DB.temp_path):
+            try:
+                os.remove(_DB.temp_path)
+            except OSError:
+                pass
+            _DB.temp_path = None
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(description="Analyze events with SQLite")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    ap.add_argument("-d", "--db", "--database", dest="database", default=None,
+                    help="Database file name (defaults to a temporary file cleaned up on exit)")
+    args = ap.parse_args()
+
+    try:
+        trace_begin(args.database)
+        session = perf.session(perf.data(args.input), sample=process_event)
+        session.process_events()
+    finally:
+        # Break the reference cycle between session and process_event (whose module
+        # globals reference session) so the C session object is deterministically freed.
+        session = None
+        trace_end()
tools/perf/tests/shell/test_event_analyzing_sample_python.sh new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#350 lore raw

PATCH v4 22/49 perf python: Port syscall-counts to perf module

In reply to Ian Rogers

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
...
real    0m0.953s
user    0m0.905s
sys     0m0.048s
```

Additional improvements compared to the legacy script:
- Resolve syscall names using perf.syscall_name(id, session.e_machine) instead of host python-audit / Util.py tables, enabling accurate cross-architecture perf.data analysis without external dependencies.
- Support both raw_syscalls:sys_enter (sample.id) and individual syscalls:sys_enter_* tracepoints (sample.__syscall_nr / sample.nr), filtering out invalid/corrupt (> 0xffff or negative) syscall numbers.
- Add argparse CLI options (-i/--input and optional comm filter).

Add a shell test (test_syscall_counts_python.sh) to verify the standalone script. The legacy script and its bin wrapper are retained temporarily during the transition to maintain bisectability and are removed once all scripts are migrated.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/syscall-counts.py           | 95 +++++++++++++++++++
 .../tests/shell/test_syscall_counts_python.sh | 81 ++++++++++++++++
 2 files changed, 176 insertions(+)
 create mode 100755 tools/perf/python/syscall-counts.py
 create mode 100755 tools/perf/tests/shell/test_syscall_counts_python.sh
tools/perf/python/syscall-counts.py new file +95 -0
new file mode 100755
index 000000000000..3ddb1bcc27e0
@@ -0,0 +1,95 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Displays system-wide system call totals, broken down by syscall.
+
+If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+from typing import DefaultDict
+import perf
+
+syscalls: DefaultDict[int, int] = defaultdict(int)
+for_comm = None
+session = None
+
+
+def print_syscall_totals():
+    """Print aggregated statistics."""
+    if for_comm is not None:
+        print(f"\nsyscall events for {for_comm}:\n")
+    else:
+        print("\nsyscall events:\n")
+
+    print(f"{'event':<40} {'count':>10}")
+    print("---------------------------------------- -----------")
+
+    for sc_id, val in sorted(syscalls.items(),
+                             key=lambda kv: (-kv[1], kv[0])):
+        e_machine = getattr(session, "e_machine", 0) or 0
+        # Mask out the x86_64 x32 ABI bit (__X32_SYSCALL_BIT = 0x40000000) before
+        # resolving the syscall number in the architecture's syscall table.
+        raw_sc_id = sc_id & ~0x40000000
+        if e_machine:
+            name = perf.syscall_name(raw_sc_id, e_machine) or str(sc_id)
+        else:
+            name = perf.syscall_name(raw_sc_id) or str(sc_id)
+        print(f"{name:<40} {val:>10}")
+
+
+def process_event(sample):
+    """Process a single sample event."""
+    event_name = str(sample.evsel)
+    # Per-syscall syscalls:sys_enter_* tracepoints expose '__syscall_nr' (or 'nr')
+    # and may have an unrelated syscall argument named 'id' (e.g. sys_enter_clock_gettime),
+    # whereas raw_syscalls:sys_enter (and legacy pre-2.6.35 syscalls:sys_enter) expose 'id'.
+    if event_name.startswith("evsel(syscalls:sys_enter_"):
+        sc_id = getattr(sample, "__syscall_nr", None)
+        if sc_id is not None and not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = None
+        if sc_id is None:
+            sc_id = getattr(sample, "nr", -1)
+    elif event_name.startswith(("evsel(raw_syscalls:sys_enter", "evsel(syscalls:sys_enter")):
+        sc_id = getattr(sample, "id", -1)
+        if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = getattr(sample, "__syscall_nr", -1)
+        if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = getattr(sample, "nr", -1)
+    else:
+        return
+
+    # Mask out __X32_SYSCALL_BIT (0x40000000) when validating the syscall ID range.
+    if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+        return
+
+    comm = "unknown"
+    try:
+        if session:
+            proc = session.find_thread(sample.sample_pid, sample.sample_tid)
+            if proc:
+                comm = proc.comm() or "unknown"
+    except (TypeError, AttributeError):
+        pass
+
+    if for_comm and comm != for_comm:
+        return
+    syscalls[sc_id] += 1
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser()
+    ap.add_argument("comm", nargs="?", help="Only report syscalls for comm")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    args = ap.parse_args()
+    for_comm = args.comm
+    try:
+        session = perf.session(perf.data(args.input), sample=process_event)
+        session.process_events()
+        print_syscall_totals()
+    finally:
+        # Break the reference cycle between session and process_event (whose module
+        # globals reference session) since perf.session lacks cyclic GC (tp_traverse).
+        session = None
tools/perf/tests/shell/test_syscall_counts_python.sh new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#352 lore raw

PATCH v4 23/49 perf python: Port syscall-counts-by-pid to perf module

In reply to Ian Rogers

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
...
real    0m1.011s
user    0m0.963s
sys     0m0.048s
```

Additional improvements compared to the legacy script:
- Resolve architecture-specific syscall names via perf.syscall_name(id, session.e_machine) instead of host python-audit tables.
- Support both raw_syscalls:sys_enter and individual syscalls:sys_enter_* tracepoints, and filter out invalid (> 0xffff or negative) syscall IDs.
- Support filtering by numeric PID as well as command name (comm), and resolve process command names via session.find_thread(pid).

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/syscall-counts-by-pid.py    | 112 ++++++++++++++++++
 .../test_syscall_counts_by_pid_python.sh      |  92 ++++++++++++++
 2 files changed, 204 insertions(+)
 create mode 100755 tools/perf/python/syscall-counts-by-pid.py
 create mode 100755 tools/perf/tests/shell/test_syscall_counts_by_pid_python.sh
tools/perf/python/syscall-counts-by-pid.py new file +112 -0
new file mode 100755
index 000000000000..fc0ec1bf7ca1
@@ -0,0 +1,112 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Displays system-wide system call totals, broken down by syscall.
+If a [comm] arg is specified, only syscalls called by [comm] are displayed.
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+from typing import (Dict, Tuple)
+import perf
+
+syscalls: Dict[Tuple[str, int, int], int] = defaultdict(int)
+for_comm = None
+for_pid = None
+session = None
+
+
+def print_syscall_totals():
+    """Print aggregated statistics."""
+    if for_comm is not None:
+        print(f"\nsyscall events for {for_comm}:\n")
+    elif for_pid is not None:
+        print(f"\nsyscall events for PID {for_pid}:\n")
+    else:
+        print("\nsyscall events:\n")
+
+    print(f"{'comm [pid]/syscalls':<40} {'count':>10}")
+    print("---------------------------------------- -----------")
+
+    sorted_keys = sorted(syscalls.keys(), key=lambda k: (k[0], k[1], -syscalls[k], k[2]))
+    current_comm_pid = None
+    for comm, pid, sc_id in sorted_keys:
+        if current_comm_pid != (comm, pid):
+            print(f"\n{comm} [{pid}]")
+            current_comm_pid = (comm, pid)
+        e_machine = getattr(session, "e_machine", 0) or 0
+        # Mask out the x86_64 x32 ABI bit (__X32_SYSCALL_BIT = 0x40000000) before
+        # resolving the syscall number in the architecture's syscall table.
+        raw_sc_id = sc_id & ~0x40000000
+        if e_machine:
+            name = perf.syscall_name(raw_sc_id, e_machine) or str(sc_id)
+        else:
+            name = perf.syscall_name(raw_sc_id) or str(sc_id)
+        print(f"  {name:<38} {syscalls[(comm, pid, sc_id)]:>10}")
+
+
+def process_event(sample):
+    """Process a single sample event."""
+    event_name = str(sample.evsel)
+    # Per-syscall syscalls:sys_enter_* tracepoints expose '__syscall_nr' (or 'nr')
+    # and may have an unrelated syscall argument named 'id', whereas
+    # raw_syscalls:sys_enter (and legacy pre-2.6.35 syscalls:sys_enter) expose 'id'.
+    if event_name.startswith("evsel(syscalls:sys_enter_"):
+        sc_id = getattr(sample, "__syscall_nr", None)
+        if sc_id is not None and not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = None
+        if sc_id is None:
+            sc_id = getattr(sample, "nr", -1)
+    elif event_name.startswith(("evsel(raw_syscalls:sys_enter", "evsel(syscalls:sys_enter")):
+        sc_id = getattr(sample, "id", -1)
+        if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = getattr(sample, "__syscall_nr", -1)
+        if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+            sc_id = getattr(sample, "nr", -1)
+    else:
+        return
+
+    # Mask out __X32_SYSCALL_BIT (0x40000000) when validating the syscall ID range.
+    if not (0 <= (sc_id & ~0x40000000) <= 0xffff):
+        return
+
+    pid = sample.sample_pid
+
+    if for_pid is not None and pid != for_pid:
+        return
+
+    comm = "unknown"
+    try:
+        if session:
+            proc = session.find_thread(sample.sample_pid, sample.sample_tid)
+            if proc:
+                comm = proc.comm() or "unknown"
+    except (TypeError, AttributeError):
+        pass
+
+    if for_comm and comm != for_comm:
+        return
+    syscalls[(comm, pid, sc_id)] += 1
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser()
+    ap.add_argument("filter", nargs="?", help="COMM or PID to filter by")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    args = ap.parse_args()
+
+    if args.filter:
+        try:
+            for_pid = int(args.filter)
+        except ValueError:
+            for_comm = args.filter
+
+    try:
+        session = perf.session(perf.data(args.input), sample=process_event)
+        session.process_events()
+        print_syscall_totals()
+    finally:
+        # Break the reference cycle between session and process_event (whose module
+        # globals reference session) since perf.session lacks cyclic GC (tp_traverse).
+        session = None
tools/perf/tests/shell/test_syscall_counts_by_pid_python.sh new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#354 lore raw

PATCH v4 24/49 perf python: Port failed-syscalls-by-pid to perf module

In reply to Ian Rogers

Port failed-syscalls-by-pid.py to a standalone script in tools/perf/python/ using the perf module:
- Resolve architecture-aware syscall names using perf.syscall_name(id, session.e_machine) instead of host python-audit tables, supporting cross-architecture perf.data files without external dependencies.
- Translate negative syscall return values into symbolic E* error names using perf.arch_strerrno(ret, session.e_machine), leveraging the auto-generated trace beauty architecture errno tables.
- Encapsulate aggregation in a SyscallAnalyzer class using collections.defaultdict and add argparse filtering by comm, PID, and -i/--input.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/failed-syscalls-by-pid.py   | 161 ++++++++++++++++++
 .../test_failed_syscalls_by_pid_python.sh     |  95 +++++++++++
 2 files changed, 256 insertions(+)
 create mode 100755 tools/perf/python/failed-syscalls-by-pid.py
 create mode 100755 tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh
tools/perf/python/failed-syscalls-by-pid.py new file +161 -0
new file mode 100755
index 000000000000..51824a929ea4
@@ -0,0 +1,161 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+Displays system-wide failed system call totals, broken down by pid.
+If a [comm] or [pid] arg is specified, only syscalls called by it are displayed.
+
+Ported from tools/perf/scripts/python/failed-syscalls-by-pid.py
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import os
+import sys
+from typing import Optional
+import perf
+
+
+def strerror(nr: int, e_machine: Optional[int] = None) -> str:
+    """Return error string for a given errno, accounting for target e_machine."""
+    return perf.arch_strerrno(nr, e_machine) or f"Unknown {nr} errno"
+
+
+class SyscallAnalyzer:
+    """Analyzes failed syscalls and aggregates counts."""
+
+    def __init__(self, for_comm: Optional[str] = None, for_pid: Optional[int] = None):
+        self.for_comm = for_comm
+        self.for_pid = for_pid
+        self.session: Optional[perf.session] = None
+        self.syscalls: dict[tuple[str, int, int, int], int] = defaultdict(int)
+        machine = os.uname().machine
+        self.host_64_bit = ("64" in machine or machine in ("s390x", "alpha")
+                            or sys.maxsize > 0xffffffff)
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process raw_syscalls:sys_exit and syscalls:sys_exit events."""
+        event_name = str(sample.evsel)
+        if "raw_syscalls:sys_exit" not in event_name and "syscalls:sys_exit" not in event_name:
+            return
+
+        pid = sample.sample_tid
+        comm = "Unknown"
+        if hasattr(self, 'session') and self.session:
+            try:
+                thread = self.session.find_thread(sample.sample_pid, pid)
+                if thread:
+                    comm = thread.comm() or "Unknown"
+            except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+                pass
+
+        if self.for_comm is not None and comm != self.for_comm:
+            return
+        if self.for_pid is not None and pid != self.for_pid:
+            return
+
+        ret = getattr(sample, "ret", 0)
+        is_64_bit = (getattr(self.session, "is_64_bit", self.host_64_bit)
+                     if self.session else self.host_64_bit)
+        # Tracepoint fields may be exposed as unsigned 32-bit or 64-bit integers.
+        # Guard the 32-bit 0xfffff000..0xffffffff conversion with 'not is_64_bit' so valid
+        # 64-bit user-space addresses in that range (e.g. mmap/brk) are not misclassified.
+        if ret > 0:
+            if not is_64_bit and 0xfffff000 <= ret <= 0xffffffff:
+                ret -= 0x100000000
+            elif ret >= 0xfffffffffffff000:
+                ret -= 0x10000000000000000
+
+        # Linux kernel syscalls only return error codes in the [-4095, -1] (-MAX_ERRNO)
+        # range; other negative signed values are valid unsigned user-space pointers.
+        if -4095 <= ret < 0:
+            syscall_id = getattr(sample, "__syscall_nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "sys_id", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "id", -1)
+
+            # Mask out the x86_64 x32 ABI bit (__X32_SYSCALL_BIT = 0x40000000).
+            raw_sc_id = syscall_id & ~0x40000000
+            if 0 <= raw_sc_id <= 0xffff:
+                self.syscalls[(comm, pid, syscall_id, ret)] += 1
+
+    def print_summary(self) -> None:
+        """Print aggregated statistics."""
+        if self.for_comm is not None:
+            print(f"\nsyscall errors for {self.for_comm}:\n")
+        elif self.for_pid is not None:
+            print(f"\nsyscall errors for PID {self.for_pid}:\n")
+        else:
+            print("\nsyscall errors:\n")
+
+        print(f"{'comm [pid]':<30}  {'count':>10}")
+        print(f"{'-' * 30:<30}  {'-' * 10:>10}")
+
+        sorted_keys = sorted(
+            self.syscalls.keys(),
+            key=lambda k: (k[0], k[1], k[2], -self.syscalls[k])
+        )
+        current_comm_pid = None
+        current_syscall = None
+        emach = getattr(self.session, "e_machine", 0) or 0
+        for comm, pid, syscall_id, ret in sorted_keys:
+            if current_comm_pid != (comm, pid):
+                print(f"\n{comm} [{pid}]")
+                current_comm_pid = (comm, pid)
+                current_syscall = None
+            if current_syscall != syscall_id:
+                raw_sc_id = syscall_id & ~0x40000000
+                try:
+                    if emach:
+                        name = perf.syscall_name(raw_sc_id, emach) or str(syscall_id)
+                    else:
+                        name = perf.syscall_name(raw_sc_id) or str(syscall_id)
+                except AttributeError:
+                    name = str(syscall_id)
+                print(f"  syscall: {name:<16}")
+                current_syscall = syscall_id
+            err_str = strerror(ret, emach)
+            count = self.syscalls[(comm, pid, syscall_id, ret)]
+            print(f"    err = {err_str:<20}  {count:10d}")
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(
+        description="Displays system-wide failed system call totals, "
+                    "broken down by pid.")
+    ap.add_argument("-i", "--input", default="perf.data",
+                    help="Input file name")
+    ap.add_argument("filter", nargs="?", help="COMM or PID to filter by")
+    args = ap.parse_args()
+
+    F_COMM = None
+    F_PID = None
+
+    if args.filter:
+        try:
+            F_PID = int(args.filter)
+        except ValueError:
+            F_COMM = args.filter
+
+    analyzer = SyscallAnalyzer(F_COMM, F_PID)
+    session = None
+
+    try:
+        print("Press control+C to stop and show the summary")
+        session = perf.session(perf.data(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)
+    finally:
+        # Break reference cycles between perf.session and analyzer.process_event
+        # because perf.session is a C extension type without cyclic GC (tp_traverse).
+        analyzer.session = None
+        session = None
tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#356 lore raw

PATCH v4 25/49 perf python: Port failed-syscalls from Perl to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/failed-syscalls.py          | 94 +++++++++++++++++++
 .../shell/test_failed_syscalls_python.sh      | 80 ++++++++++++++++
 2 files changed, 174 insertions(+)
 create mode 100755 tools/perf/python/failed-syscalls.py
 create mode 100755 tools/perf/tests/shell/test_failed_syscalls_python.sh
tools/perf/python/failed-syscalls.py new file +94 -0
new file mode 100755
index 000000000000..6d1fa4e5ef89
@@ -0,0 +1,94 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""Failed system call counts."""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import sys
+from typing import Optional
+import perf
+
+class FailedSyscalls:
+    """Tracks and displays failed system call totals."""
+    def __init__(self, comm: Optional[str] = None) -> None:
+        self.failed_syscalls: dict[str, int] = defaultdict(int)
+        self.for_comm = comm
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process sys_exit events."""
+        event_name = str(sample.evsel)
+        if not event_name.startswith("evsel(syscalls:sys_exit") and \
+           not event_name.startswith("evsel(raw_syscalls:sys_exit"):
+            return
+
+        try:
+            ret = sample.ret
+        except AttributeError:
+            print("ERROR: tracepoint fields missed", file=sys.stderr)
+            sys.exit(1)
+
+        if ret > 0:
+            if ret >= 0xfffffffffffff000:  # 64-bit negative errors
+                ret -= 0x10000000000000000
+            elif 0xfffff000 <= ret <= 0xffffffff:  # 32-bit negative errors
+                assert self.session is not None
+                if not self.session.is_64_bit:
+                    ret -= 0x100000000
+
+        # Linux kernel syscalls only return error codes in the [-4095, -1] (-MAX_ERRNO)
+        # range; other negative signed values are valid unsigned user-space addresses.
+        if not -4095 <= ret < 0:
+            return
+
+        assert self.session is not None
+        try:
+            thread = self.session.find_thread(sample.sample_pid, sample.sample_tid)
+            comm = (thread.comm() if thread else None) or "unknown"
+        except (TypeError, AttributeError):
+            # find_thread returns None when the thread isn't known.
+            comm = "unknown"
+
+        if self.for_comm and comm != self.for_comm:
+            return
+
+        self.failed_syscalls[comm] += 1
+
+    def print_totals(self) -> None:
+        """Print summary table."""
+        print("\nfailed syscalls by comm:\n")
+        print(f"{'comm':<20s}  {'# errors':>10s}")
+        print(f"{'-'*20}  {'-'*10}")
+
+        for comm, val in sorted(self.failed_syscalls.items(),
+                                key=lambda kv: (-kv[1], kv[0])):
+            print(f"{comm:<20s}  {val:10d}")
+
+    def run(self, input_file: str) -> None:
+        """Run the session."""
+        self.session = perf.session(perf.data(input_file), sample=self.process_event)
+        try:
+            self.session.process_events()
+        finally:
+            # Break the reference cycle between perf.session and self.process_event
+            # because perf.session lacks cyclic GC support (tp_traverse).
+            self.session = None
+        self.print_totals()
+
+def main() -> None:
+    """Main function."""
+    parser = argparse.ArgumentParser(description="Trace failed syscalls")
+    parser.add_argument("comm", nargs="?", help="Filter by command name")
+    parser.add_argument("-i", "--input", default="perf.data", help="Input file")
+    args = parser.parse_args()
+
+    analyzer = FailedSyscalls(args.comm)
+    try:
+        analyzer.run(args.input)
+    except IOError as e:
+        print(e, file=sys.stderr)
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_failed_syscalls_python.sh new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#358 lore raw

PATCH v4 26/49 perf python: Port sctop to perf module

In reply to Ian Rogers

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').

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/sctop.py                  | 250 ++++++++++++++++++++
 tools/perf/tests/shell/test_sctop_python.sh |  78 ++++++
 2 files changed, 328 insertions(+)
 create mode 100755 tools/perf/python/sctop.py
 create mode 100755 tools/perf/tests/shell/test_sctop_python.sh
tools/perf/python/sctop.py new file +250 -0
new file mode 100755
index 000000000000..42e95ecfffad
@@ -0,0 +1,250 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""
+System call top
+
+Periodically displays system-wide system call totals, broken down by
+syscall.  If a [comm] arg is specified, only syscalls called by
+[comm] are displayed. If an [interval] arg is specified, the display
+will be refreshed every [interval] seconds.  The default interval is
+3 seconds.
+
+Ported from tools/perf/scripts/python/sctop.py
+"""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import os
+import sys
+import threading
+from typing import Optional
+import perf
+from perf_live import LiveSession
+
+
+class SCTopAnalyzer:
+    """Periodically displays system-wide system call totals."""
+
+    def __init__(self, for_comm: Optional[str], interval: int, offline: bool = False):
+        self.for_comm = for_comm
+        self.interval = interval
+        self.syscalls: dict[int, int] = defaultdict(int)
+        self.comm_cache: dict[int, str] = {}
+        self.lock = threading.Lock()
+        self.stop_event = threading.Event()
+        self.thread = threading.Thread(target=self.print_syscall_totals)
+        self.offline = offline
+        self.own_pid = os.getpid()
+        self.last_print_time: Optional[int] = None
+        self.session: Optional[perf.session] = None
+        self.e_machine: Optional[int] = None
+
+    def syscall_name(self, syscall_id: int) -> str:
+        """Lookup syscall name by ID."""
+        # Mask out the x86_64 x32 ABI bit (__X32_SYSCALL_BIT = 0x40000000) before
+        # resolving the syscall number in the architecture's syscall table.
+        raw_sc_id = syscall_id & ~0x40000000
+        try:
+            e_machine = getattr(self.session, "e_machine", self.e_machine)
+            if e_machine is not None:
+                name = perf.syscall_name(raw_sc_id, e_machine)
+            else:
+                name = perf.syscall_name(raw_sc_id)
+            if name is not None:
+                return name
+        except (TypeError, OverflowError):
+            pass
+        return str(syscall_id)
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Collect syscall events."""
+        if not self.offline and sample.sample_pid == self.own_pid:
+            return
+
+        name = str(sample.evsel)
+        # Per-syscall syscalls:sys_enter_* tracepoints expose '__syscall_nr' (or 'nr')
+        # and may have an unrelated syscall argument named 'id', whereas
+        # raw_syscalls:sys_enter (and legacy pre-2.6.35 syscalls:sys_enter) expose 'id'.
+        if name.startswith("evsel(syscalls:sys_enter_"):
+            syscall_id = getattr(sample, "__syscall_nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "nr", -1)
+        elif name.startswith(("evsel(raw_syscalls:sys_enter", "evsel(syscalls:sys_enter")):
+            syscall_id = getattr(sample, "id", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "__syscall_nr", -1)
+            if not (0 <= (syscall_id & ~0x40000000) <= 0xffff):
+                syscall_id = getattr(sample, "nr", -1)
+        else:
+            syscall_id = -1
+
+        skip = False
+        with self.lock:
+            if self.for_comm is not None:
+                is_execve = (0 <= (syscall_id & ~0x40000000) <= 0xffff and
+                             self.syscall_name(syscall_id) in ("execve", "execveat"))
+                if is_execve:
+                    self.comm_cache.pop(sample.sample_pid, None)
+
+                comm = "Unknown"
+                if hasattr(self, 'session') and self.session:
+                    # In offline perf.data mode, query session.find_thread() directly
+                    # so PERF_RECORD_COMM updates after execve (e.g. perf -> sleep)
+                    # are reflected immediately rather than returning a stale cached comm.
+                    try:
+                        proc = self.session.find_thread(sample.sample_pid, sample.sample_tid)
+                        if proc:
+                            comm = proc.comm() or "Unknown"
+                    except TypeError:
+                        pass
+                    if comm != "Unknown" and not is_execve:
+                        self.comm_cache[sample.sample_pid] = comm
+                    elif sample.sample_pid in self.comm_cache:
+                        comm = self.comm_cache[sample.sample_pid]
+                elif sample.sample_pid in self.comm_cache:
+                    comm = self.comm_cache[sample.sample_pid]
+                else:
+                    try:
+                        with open(f"/proc/{sample.sample_pid}/comm", "r",
+                                  encoding="utf-8", errors="replace") as f:
+                            comm = f.read().strip()
+                    except OSError:
+                        comm = "Unknown"
+                    # Cache both matching and non-matching comms (including "Unknown"
+                    # when a PID has exited or is inaccessible) so live system-wide
+                    # tracing does not re-open /proc/<pid>/comm on every syscall.
+                    # Do not cache during sys_enter(execve/execveat) since /proc/<pid>/comm
+                    # still holds the pre-exec command name until the syscall completes.
+                    if not is_execve:
+                        self.comm_cache[sample.sample_pid] = comm
+
+                if comm != self.for_comm:
+                    skip = True
+
+            is_enter = (name.startswith("evsel(raw_syscalls:sys_enter") or
+                        name.startswith("evsel(syscalls:sys_enter"))
+            if not skip and is_enter and 0 <= (syscall_id & ~0x40000000) <= 0xffff:
+                self.syscalls[syscall_id] += 1
+
+        if self.offline and hasattr(sample, "sample_time"):
+            interval_ns = self.interval * (10 ** 9)
+            if self.last_print_time is None:
+                self.last_print_time = sample.sample_time
+            elif sample.sample_time - self.last_print_time >= interval_ns:
+                self.print_current_totals()
+                self.last_print_time = sample.sample_time
+
+    def print_current_totals(self):
+        """Print current syscall totals."""
+        # Clear terminal
+        if not self.offline:
+            print("\x1b[2J\x1b[H", end="")
+        else:
+            print()
+
+        with self.lock:
+            for_comm = self.for_comm
+        if for_comm is not None:
+            print(f"\nsyscall events for {for_comm}:\n")
+        else:
+            print("\nsyscall events:\n")
+
+        print(f"{'event':40s}  {'count':10s}")
+        print(f"{'-' * 40:40s}  {'-' * 10:10s}")
+
+        with self.lock:
+            current_syscalls = list(self.syscalls.items())
+            self.syscalls.clear()
+            self.comm_cache.clear()
+
+        current_syscalls.sort(key=lambda kv: (-kv[1], kv[0]))
+
+        for syscall_id, val in current_syscalls:
+            print(f"{self.syscall_name(syscall_id):<40s}  {val:10d}")
+
+    def print_syscall_totals(self):
+        """Periodically print syscall totals."""
+        while not self.stop_event.is_set():
+            self.print_current_totals()
+            self.stop_event.wait(self.interval)
+        # Print final batch
+        self.print_current_totals()
+
+    def start(self):
+        """Start the background thread."""
+        self.thread.start()
+
+    def stop(self):
+        """Stop the background thread."""
+        self.stop_event.set()
+        self.thread.join()
+
+
+def main():
+    """Main function."""
+    ap = argparse.ArgumentParser(description="System call top")
+    ap.add_argument("args", nargs="*", help="[comm] [interval] or [interval]")
+    ap.add_argument("-i", "--input", help="Input file name")
+    args = ap.parse_args()
+
+    for_comm = None
+    default_interval = 3
+    interval = default_interval
+
+    if len(args.args) > 2:
+        print("Usage: python sctop.py [comm] [interval]")
+        sys.exit(1)
+
+    if len(args.args) > 1:
+        for_comm = args.args[0]
+        try:
+            interval = int(args.args[1])
+        except ValueError:
+            print(f"Invalid interval: {args.args[1]}")
+            sys.exit(1)
+    elif len(args.args) > 0:
+        try:
+            interval = int(args.args[0])
+        except ValueError:
+            for_comm = args.args[0]
+            interval = default_interval
+
+    analyzer = SCTopAnalyzer(for_comm, interval, offline=bool(args.input))
+    session = None
+
+    try:
+        if args.input:
+            session = perf.session(perf.data(args.input), sample=analyzer.process_event)
+            analyzer.session = session
+            session.process_events()
+            analyzer.e_machine = getattr(session, "e_machine", None)
+        else:
+            try:
+                live_session = LiveSession(
+                    "raw_syscalls:sys_enter", sample_callback=analyzer.process_event
+                )
+            except OSError:
+                live_session = LiveSession(
+                    "syscalls:sys_enter_*", sample_callback=analyzer.process_event
+                )
+            analyzer.start()
+            live_session.run()
+    except KeyboardInterrupt:
+        pass
+    except (OSError, IOError) as e:
+        print(f"Error: {e}", file=sys.stderr)
+        sys.exit(1)
+    finally:
+        if args.input:
+            analyzer.print_current_totals()
+            # Break the reference cycle between perf.session and analyzer.process_event
+            # because perf.session lacks cyclic GC support (tp_traverse).
+            analyzer.session = None
+            session = None
+        elif analyzer.thread.is_alive():
+            analyzer.stop()
+
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_sctop_python.sh new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#360 lore raw

PATCH v4 27/49 perf python: Port rw-by-file from Perl to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/rw-by-file.py               | 113 ++++++++++++++++++
 .../tests/shell/test_rw_by_file_python.sh     |  68 +++++++++++
 2 files changed, 181 insertions(+)
 create mode 100755 tools/perf/python/rw-by-file.py
 create mode 100755 tools/perf/tests/shell/test_rw_by_file_python.sh
tools/perf/python/rw-by-file.py new file +113 -0
new file mode 100755
index 000000000000..b33330261606
@@ -0,0 +1,113 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+"""Display r/w activity for files read/written to for a given program."""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import sys
+from typing import Optional, Dict
+import perf
+
+class RwByFile:
+    """Tracks and displays read/write activity by file descriptor."""
+    def __init__(self, comm: str) -> None:
+        self.for_comm = comm
+        self.reads: Dict[int, Dict[str, int]] = defaultdict(
+            lambda: {"bytes_requested": 0, "total_reads": 0}
+        )
+        self.writes: Dict[int, Dict[str, int]] = defaultdict(
+            lambda: {"bytes_written": 0, "total_writes": 0}
+        )
+        self.unhandled: Dict[str, int] = defaultdict(int)
+        self.session: Optional[perf.session] = None
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process events."""
+        event_name = str(sample.evsel)
+        if event_name.startswith("evsel(") and event_name.endswith(")"):
+            event_name = event_name[6:-1]
+        event_name = "".join(c if c.isprintable() else "?" for c in event_name)
+
+        pid = sample.sample_pid
+        assert self.session is not None
+        try:
+            thread = self.session.find_thread(pid, sample.sample_tid)
+            comm = (thread.comm() if thread else None) or "unknown"
+        except (TypeError, AttributeError):
+            comm = "unknown"
+
+        if comm != self.for_comm:
+            return
+
+        if event_name == "syscalls:sys_enter_read":
+            try:
+                fd = sample.fd
+                count = sample.count
+                self.reads[fd]["bytes_requested"] += count
+                self.reads[fd]["total_reads"] += 1
+            except AttributeError:
+                self.unhandled[event_name] += 1
+        elif event_name == "syscalls:sys_enter_write":
+            try:
+                fd = sample.fd
+                count = sample.count
+                self.writes[fd]["bytes_written"] += count
+                self.writes[fd]["total_writes"] += 1
+            except AttributeError:
+                self.unhandled[event_name] += 1
+        else:
+            self.unhandled[event_name] += 1
+
+    def print_totals(self) -> None:
+        """Print summary tables."""
+        print(f"file read counts for {self.for_comm}:\n")
+        print(f"{'fd':>6s}  {'# reads':>10s}  {'bytes_requested':>15s}")
+        print(f"{'-'*6}  {'-'*10}  {'-'*15}")
+
+        for fd, data in sorted(self.reads.items(),
+                               key=lambda kv: kv[1]["bytes_requested"], reverse=True):
+            print(f"{fd:6d}  {data['total_reads']:10d}  {data['bytes_requested']:15d}")
+
+        print(f"\nfile write counts for {self.for_comm}:\n")
+        print(f"{'fd':>6s}  {'# writes':>10s}  {'bytes_written':>15s}")
+        print(f"{'-'*6}  {'-'*10}  {'-'*15}")
+
+        for fd, data in sorted(self.writes.items(),
+                               key=lambda kv: kv[1]["bytes_written"], reverse=True):
+            print(f"{fd:6d}  {data['total_writes']:10d}  {data['bytes_written']:15d}")
+
+        if self.unhandled:
+            print("\nunhandled events:\n")
+            print(f"{'event':<40s}  {'count':>10s}")
+            print(f"{'-'*40}  {'-'*10}")
+            for event_name, count in self.unhandled.items():
+                print(f"{event_name:<40s}  {count:10d}")
+
+    def run(self, input_file: str) -> None:
+        """Run the session."""
+        self.session = perf.session(perf.data(input_file), sample=self.process_event)
+        try:
+            self.session.process_events()
+        finally:
+            # Break the reference cycle between perf.session and self.process_event
+            # because perf.session lacks cyclic GC support (tp_traverse).
+            self.session = None
+        self.print_totals()
+
+def main() -> None:
+    """Main function."""
+    parser = argparse.ArgumentParser(description="Trace r/w activity by file")
+    parser.add_argument("comm", help="Filter by command name")
+    parser.add_argument("-i", "--input", default="perf.data", help="Input file")
+    args = parser.parse_args()
+
+    analyzer = RwByFile(args.comm)
+    try:
+        analyzer.run(args.input)
+    except IOError as e:
+        print(e, file=sys.stderr)
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_rw_by_file_python.sh new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#362 lore raw

PATCH v4 28/49 perf python: Port rw-by-pid from Perl to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/rw-by-pid.py                | 205 ++++++++++++++++++
 .../perf/tests/shell/test_rw_by_pid_python.sh |  75 +++++++
 2 files changed, 280 insertions(+)
 create mode 100755 tools/perf/python/rw-by-pid.py
 create mode 100755 tools/perf/tests/shell/test_rw_by_pid_python.sh
tools/perf/python/rw-by-pid.py new file +205 -0
new file mode 100755
index 000000000000..8e2d1dbd9a08
@@ -0,0 +1,205 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+"""Display r/w activity for all processes."""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import sys
+from typing import Optional, Dict, List, Tuple, Any
+import perf
+
+class RwByPid:
+    """Tracks and displays read/write activity by PID."""
+    def __init__(self) -> None:
+        self.reads: Dict[int, Dict[str, Any]] = defaultdict(
+            lambda: {
+                "bytes_requested": 0,
+                "bytes_read": 0,
+                "total_reads": 0,
+                "comm": "",
+                "errors": defaultdict(int),
+            }
+        )
+        self.writes: Dict[int, Dict[str, Any]] = defaultdict(
+            lambda: {
+                "bytes_requested": 0,
+                "bytes_written": 0,
+                "total_writes": 0,
+                "comm": "",
+                "errors": defaultdict(int),
+            }
+        )
+        self.unhandled: Dict[str, int] = defaultdict(int)
+        self.session: Optional[perf.session] = None
+
+    def _handle_sys_enter_read(self, pid: int, comm: str, sample: perf.sample_event) -> None:
+        try:
+            count = sample.count
+            self.reads[pid]["bytes_requested"] += count
+            self.reads[pid]["total_reads"] += 1
+            self.reads[pid]["comm"] = comm
+        except AttributeError:
+            self.unhandled["sys_enter_read_attr_err"] += 1
+
+    def _handle_sys_exit_read(self, pid: int, sample: perf.sample_event, comm: str = "") -> None:
+        try:
+            ret = sample.ret
+            # Convert unsigned 64-bit or 32-bit tracepoint return values to signed integers.
+            # Because kernel read/write transfers are bounded by MAX_RW_COUNT (0x7ffff000 < 2 GiB),
+            # 0xfffff000..0xffffffff is always a 32-bit negative errno even on 64-bit hosts.
+            if ret >= 0x8000000000000000:
+                ret -= 0x10000000000000000
+            elif 0xfffff000 <= ret <= 0xffffffff:
+                ret -= 0x100000000
+            if not self.reads[pid]["comm"] and comm:
+                self.reads[pid]["comm"] = comm
+            if ret >= 0:
+                self.reads[pid]["bytes_read"] += ret
+            else:
+                self.reads[pid]["errors"][ret] += 1
+        except AttributeError:
+            self.unhandled["sys_exit_read_attr_err"] += 1
+
+    def _handle_sys_enter_write(self, pid: int, comm: str, sample: perf.sample_event) -> None:
+        try:
+            count = sample.count
+            self.writes[pid]["bytes_requested"] += count
+            self.writes[pid]["total_writes"] += 1
+            self.writes[pid]["comm"] = comm
+        except AttributeError:
+            self.unhandled["sys_enter_write_attr_err"] += 1
+
+    def _handle_sys_exit_write(self, pid: int, sample: perf.sample_event, comm: str = "") -> None:
+        try:
+            ret = sample.ret
+            # Convert unsigned 64-bit or 32-bit tracepoint return values to signed integers.
+            # Because kernel read/write transfers are bounded by MAX_RW_COUNT (0x7ffff000 < 2 GiB),
+            # 0xfffff000..0xffffffff is always a 32-bit negative errno even on 64-bit hosts.
+            if ret >= 0x8000000000000000:
+                ret -= 0x10000000000000000
+            elif 0xfffff000 <= ret <= 0xffffffff:
+                ret -= 0x100000000
+            if not self.writes[pid]["comm"] and comm:
+                self.writes[pid]["comm"] = comm
+            if ret >= 0:
+                self.writes[pid]["bytes_written"] += ret
+            else:
+                self.writes[pid]["errors"][ret] += 1
+        except AttributeError:
+            self.unhandled["sys_exit_write_attr_err"] += 1
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process events."""
+        event_name = str(sample.evsel)
+        if event_name.startswith("evsel(") and event_name.endswith(")"):
+            event_name = event_name[6:-1]
+        event_name = "".join(c if c.isprintable() else "?" for c in event_name)
+        pid = sample.sample_pid
+
+        assert self.session is not None
+        try:
+            thread = self.session.find_thread(pid, sample.sample_tid)
+            comm = (thread.comm() if thread else None) or "unknown"
+        except (TypeError, AttributeError):
+            comm = "unknown"
+        comm = "".join(c if c.isprintable() else "?" for c in comm)
+
+        if event_name == "syscalls:sys_enter_read":
+            self._handle_sys_enter_read(pid, comm, sample)
+        elif event_name == "syscalls:sys_exit_read":
+            self._handle_sys_exit_read(pid, sample, comm)
+        elif event_name == "syscalls:sys_enter_write":
+            self._handle_sys_enter_write(pid, comm, sample)
+        elif event_name == "syscalls:sys_exit_write":
+            self._handle_sys_exit_write(pid, sample, comm)
+        else:
+            self.unhandled[event_name] += 1
+
+    def print_totals(self) -> None:
+        """Print summary tables."""
+        print("read counts by pid:\n")
+        print(
+            f"{'pid':>6s}  {'comm':<20s}  {'# reads':>10s}  "
+            f"{'bytes_requested':>15s}  {'bytes_read':>10s}"
+        )
+        print(f"{'-'*6}  {'-'*20}  {'-'*10}  {'-'*15}  {'-'*10}")
+
+        for pid, data in sorted(self.reads.items(),
+                                key=lambda kv: kv[1]["bytes_read"], reverse=True):
+            print(
+                f"{pid:6d}  {data['comm']:<20s}  {data['total_reads']:10d}  "
+                f"{data['bytes_requested']:15d}  {data['bytes_read']:10d}"
+            )
+
+        print("\nfailed reads by pid:\n")
+        print(f"{'pid':>6s}  {'comm':<20s}  {'error #':>7s}  {'# errors':>10s}")
+        print(f"{'-'*6}  {'-'*20}  {'-'*7}  {'-'*10}")
+
+        errcounts: List[Tuple[int, str, int, int]] = []
+        for pid, data in self.reads.items():
+            for error, count in data["errors"].items():
+                errcounts.append((pid, data["comm"], error, count))
+
+        for pid, comm, error, count in sorted(errcounts, key=lambda x: x[3], reverse=True):
+            print(f"{pid:6d}  {comm:<20s}  {error:7d}  {count:10d}")
+
+        print("\nwrite counts by pid:\n")
+        print(
+            f"{'pid':>6s}  {'comm':<20s}  {'# writes':>10s}  "
+            f"{'bytes_requested':>15s}  {'bytes_written':>15s}"
+        )
+        print(f"{'-'*6}  {'-'*20}  {'-'*10}  {'-'*15}  {'-'*15}")
+
+        for pid, data in sorted(self.writes.items(),
+                                key=lambda kv: kv[1]["bytes_written"], reverse=True):
+            print(
+                f"{pid:6d}  {data['comm']:<20s}  {data['total_writes']:10d}  "
+                f"{data['bytes_requested']:15d}  {data['bytes_written']:15d}"
+            )
+
+        print("\nfailed writes by pid:\n")
+        print(f"{'pid':>6s}  {'comm':<20s}  {'error #':>7s}  {'# errors':>10s}")
+        print(f"{'-'*6}  {'-'*20}  {'-'*7}  {'-'*10}")
+
+        errcounts = []
+        for pid, data in self.writes.items():
+            for error, count in data["errors"].items():
+                errcounts.append((pid, data["comm"], error, count))
+
+        for pid, comm, error, count in sorted(errcounts, key=lambda x: x[3], reverse=True):
+            print(f"{pid:6d}  {comm:<20s}  {error:7d}  {count:10d}")
+
+        if self.unhandled:
+            print("\nunhandled events:\n")
+            print(f"{'event':<40s}  {'count':>10s}")
+            print(f"{'-'*40}  {'-'*10}")
+            for event_name, count in self.unhandled.items():
+                print(f"{event_name:<40s}  {count:10d}")
+
+    def run(self, input_file: str) -> None:
+        """Run the session."""
+        self.session = perf.session(perf.data(input_file), sample=self.process_event)
+        try:
+            self.session.process_events()
+        finally:
+            # Break the reference cycle between perf.session and self.process_event
+            # because perf.session lacks cyclic GC support (tp_traverse).
+            self.session = None
+        self.print_totals()
+
+def main() -> None:
+    """Main function."""
+    parser = argparse.ArgumentParser(description="Trace r/w activity by PID")
+    parser.add_argument("-i", "--input", default="perf.data", help="Input file")
+    args = parser.parse_args()
+
+    analyzer = RwByPid()
+    try:
+        analyzer.run(args.input)
+    except IOError as e:
+        print(e, file=sys.stderr)
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_rw_by_pid_python.sh new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#364 lore raw

PATCH v4 29/49 perf python: Port rwtop from Perl to perf module

In reply to Ian Rogers

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.
- 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.
- Sanitize non-printable characters in /proc/<pid>/comm to prevent terminal control sequence injection.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/rwtop.py                  | 256 ++++++++++++++++++++
 tools/perf/tests/shell/test_rwtop_python.sh |  74 ++++++
 2 files changed, 330 insertions(+)
 create mode 100755 tools/perf/python/rwtop.py
 create mode 100755 tools/perf/tests/shell/test_rwtop_python.sh
tools/perf/python/rwtop.py new file +256 -0
new file mode 100755
index 000000000000..86674d7c8f6f
@@ -0,0 +1,256 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+"""Periodically displays system-wide r/w call activity, broken down by pid."""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+import os
+import sys
+from typing import Optional, Dict, Any
+import perf
+from perf_live import LiveSession
+
+class RwTop:
+    """Periodically displays system-wide r/w call activity."""
+    def __init__(self, interval: int = 3, nlines: int = 20) -> None:
+        self.offline = False
+        self.interval_ns = interval * 1000000000
+        self.nlines = nlines
+        self.reads: Dict[int, Dict[str, Any]] = defaultdict(
+            lambda: {
+                "bytes_requested": 0,
+                "bytes_read": 0,
+                "total_reads": 0,
+                "comm": "",
+                "errors": defaultdict(int),
+            }
+        )
+        self.writes: Dict[int, Dict[str, Any]] = defaultdict(
+            lambda: {
+                "bytes_requested": 0,
+                "bytes_written": 0,
+                "total_writes": 0,
+                "comm": "",
+                "errors": defaultdict(int),
+            }
+        )
+        self.unhandled: Dict[str, int] = defaultdict(int)
+        self.comm_cache: Dict[int, str] = {}
+        self.session: Optional[perf.session] = None
+        self.last_print_time: int = 0
+
+    def get_comm(self, pid: int, tid: Optional[int] = None) -> str:
+        """Resolve and cache the comm(and) of a pid."""
+        comm = None
+        if self.session:
+            # In offline mode, query session.find_thread() directly so PERF_RECORD_COMM
+            # updates after execve are reflected immediately instead of returning a
+            # stale pre-exec comm from comm_cache.
+            try:
+                thread = (self.session.find_thread(pid, tid)
+                          if tid is not None else self.session.find_thread(pid))
+                comm = thread.comm() if thread else None
+            except (TypeError, AttributeError):
+                pass
+            if not comm:
+                comm = self.comm_cache.get(pid)
+        else:
+            comm = self.comm_cache.get(pid)
+            if comm:
+                return comm
+            try:
+                with open(f"/proc/{pid}/comm", "r", encoding="utf-8", errors="replace") as f:
+                    comm = f.read().strip()
+            except OSError:
+                # The thread may have exited before /proc could be read.
+                pass
+        if not comm:
+            comm = f"PID({pid})"
+        comm = ''.join(c if c.isprintable() else '?' for c in comm)
+        self.comm_cache[pid] = comm
+        return comm
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process events."""
+        event_name = str(sample.evsel)
+        if event_name.startswith("evsel(") and event_name.endswith(")"):
+            event_name = event_name[6:-1]
+        event_name = "".join(c if c.isprintable() else "?" for c in event_name)
+        pid = sample.sample_pid
+        sample_time = getattr(sample, "sample_time", 0) or 0
+
+        if sample_time > 0:
+            if self.last_print_time == 0:
+                self.last_print_time = sample_time
+            elif (sample_time > self.last_print_time and
+                  sample_time - self.last_print_time >= self.interval_ns):
+                self.print_totals()
+                self.last_print_time = sample_time
+
+        # Map each event onto the totals it updates. "enter" events count the
+        # requested bytes, "exit" events the transferred bytes or the error.
+        handlers = {
+            "syscalls:sys_enter_read": (self.reads, "total_reads", None),
+            "syscalls:sys_exit_read": (self.reads, None, "bytes_read"),
+            "syscalls:sys_enter_write": (self.writes, "total_writes", None),
+            "syscalls:sys_exit_write": (self.writes, None, "bytes_written"),
+        }
+        handler = handlers.get(event_name)
+        if not handler:
+            self.unhandled[event_name] += 1
+            return
+
+        totals, count_key, bytes_key = handler
+        try:
+            value = sample.count if count_key else sample.ret
+        except AttributeError:
+            self.unhandled[event_name] += 1
+            return
+
+        data = totals[pid]
+        data["comm"] = self.get_comm(pid, getattr(sample, "sample_tid", None))
+        if count_key:
+            data["bytes_requested"] += value
+            data[count_key] += 1
+        elif bytes_key:
+            # Convert unsigned 32-bit or 64-bit kernel error return values to signed integers.
+            # Because kernel read/write transfers are bounded by MAX_RW_COUNT (0x7ffff000 < 2 GiB),
+            # 0xfffff000..0xffffffff is always a 32-bit negative errno even on 64-bit hosts.
+            if value > 0:
+                if 0xfffff000 <= value <= 0xffffffff:
+                    value -= 0x100000000
+                elif value >= 0x8000000000000000:
+                    value -= 0x10000000000000000
+            if value >= 0:
+                data[bytes_key] += value
+            else:
+                data["errors"][value] += 1
+
+    def print_totals(self) -> None:
+        """Print summary tables."""
+        if not self.offline:
+            print('\x1b[H\x1b[2J', end='')
+        print("read counts by pid:\n")
+        print(
+            f"{'pid':>6s}  {'comm':<20s}  {'# reads':>10s}  "
+            f"{'bytes_req':>10s}  {'bytes_read':>10s}"
+        )
+        print(f"{'-'*6}  {'-'*20}  {'-'*10}  {'-'*10}  {'-'*10}")
+
+        count = 0
+        for pid, data in sorted(self.reads.items(),
+                                key=lambda kv: kv[1]["bytes_read"], reverse=True):
+            print(
+                f"{pid:6d}  {data['comm']:<20s}  {data['total_reads']:10d}  "
+                f"{data['bytes_requested']:10d}  {data['bytes_read']:10d}"
+            )
+            count += 1
+            if count >= self.nlines:
+                break
+
+        print("\nfailed reads by pid:\n")
+        print(f"{'pid':>6s}  {'comm':<20s}  {'error #':>7s}  {'# errors':>10s}")
+        print(f"{'-'*6}  {'-'*20}  {'-'*7}  {'-'*10}")
+
+        errcounts = []
+        for pid, data in self.reads.items():
+            for error, cnt in data["errors"].items():
+                errcounts.append((pid, data["comm"], error, cnt))
+
+        sorted_errcounts = sorted(errcounts, key=lambda x: x[3], reverse=True)
+        for pid, comm, error, cnt in sorted_errcounts[:self.nlines]:
+            print(f"{pid:6d}  {comm:<20s}  {error:7d}  {cnt:10d}")
+
+        print("\nwrite counts by pid:\n")
+        print(
+            f"{'pid':>6s}  {'comm':<20s}  {'# writes':>10s}  "
+            f"{'bytes_req':>10s}  {'bytes_written':>13s}"
+        )
+        print(f"{'-'*6}  {'-'*20}  {'-'*10}  {'-'*10}  {'-'*13}")
+
+        count = 0
+        for pid, data in sorted(self.writes.items(),
+                                key=lambda kv: kv[1]["bytes_written"], reverse=True):
+            print(
+                f"{pid:6d}  {data['comm']:<20s}  {data['total_writes']:10d}  "
+                f"{data['bytes_requested']:10d}  {data['bytes_written']:13d}"
+            )
+            count += 1
+            if count >= self.nlines:
+                break
+
+        print("\nfailed writes by pid:\n")
+        print(f"{'pid':>6s}  {'comm':<20s}  {'error #':>7s}  {'# errors':>10s}")
+        print(f"{'-'*6}  {'-'*20}  {'-'*7}  {'-'*10}")
+
+        errcounts = []
+        for pid, data in self.writes.items():
+            for error, cnt in data["errors"].items():
+                errcounts.append((pid, data["comm"], error, cnt))
+
+        sorted_errcounts = sorted(errcounts, key=lambda x: x[3], reverse=True)
+        for pid, comm, error, cnt in sorted_errcounts[:self.nlines]:
+            print(f"{pid:6d}  {comm:<20s}  {error:7d}  {cnt:10d}")
+
+        # Reset counts
+        self.reads.clear()
+        self.writes.clear()
+        self.comm_cache.clear()
+
+    def run(self, input_file: str) -> None:
+        """Run the session."""
+        self.session = perf.session(perf.data(input_file), sample=self.process_event)
+        try:
+            self.session.process_events()
+        finally:
+            # Break the reference cycle between perf.session and self.process_event
+            # because perf.session lacks cyclic GC support (tp_traverse).
+            self.session = None
+
+        # Print final totals if there are any left
+        if self.reads or self.writes:
+            self.print_totals()
+
+        if self.unhandled:
+            print("\nunhandled events:\n")
+            print(f"{'event':<40s}  {'count':>10s}")
+            print(f"{'-'*40}  {'-'*10}")
+            for event_name, count in self.unhandled.items():
+                print(f"{event_name:<40s}  {count:10d}")
+
+def main() -> None:
+    """Main function."""
+    parser = argparse.ArgumentParser(description="Trace r/w activity by PID")
+    parser.add_argument(
+        "interval", type=int, nargs="?", default=3, help="Refresh interval in seconds"
+    )
+    parser.add_argument("-i", "--input", default="perf.data", help="Input file")
+    parser.add_argument("-l", "--live", action="store_true", help="Run in live mode")
+    args = parser.parse_args()
+
+    analyzer = RwTop(args.interval)
+    try:
+        if args.live or (not os.path.exists(args.input) and args.input == "perf.data"):
+            # Live mode
+            events = (
+                "syscalls:sys_enter_read,syscalls:sys_exit_read,"
+                "syscalls:sys_enter_write,syscalls:sys_exit_write"
+            )
+            live_session = LiveSession(events, sample_callback=analyzer.process_event)
+            print("Live mode started. Press Ctrl+C to stop.", file=sys.stderr)
+            live_session.run()
+        else:
+            analyzer.offline = True
+            analyzer.run(args.input)
+    except IOError as e:
+        print(e, file=sys.stderr)
+        sys.exit(1)
+    except KeyboardInterrupt:
+        if not analyzer.offline:
+            print("\nStopping live mode...", file=sys.stderr)
+        if analyzer.reads or analyzer.writes:
+            analyzer.print_totals()
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_rwtop_python.sh new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#366 lore raw

PATCH v4 30/49 perf python: Port futex-contention to perf module

In reply to Ian Rogers

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
user    0m0.259s
sys     0m0.056s
```

Additional improvements compared to the legacy script:
- Consolidate per-(tid, uaddr) contention count, total_time, min_time, and max_time into a single LockStats class instead of maintaining three separate dictionaries.
- Validate that uaddr and op tracepoint attributes are present on syscalls:sys_enter_futex samples rather than silently attributing missing fields to uaddr=0, op=0 (FUTEX_WAIT).
- Add -i/--input CLI option support via argparse and full type annotations.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/futex-contention.py         | 107 ++++++++++++++++
 .../shell/test_futex_contention_python.sh     | 115 ++++++++++++++++++
 2 files changed, 222 insertions(+)
 create mode 100755 tools/perf/python/futex-contention.py
 create mode 100755 tools/perf/tests/shell/test_futex_contention_python.sh
tools/perf/python/futex-contention.py new file +107 -0
new file mode 100755
index 000000000000..097663d929b7
@@ -0,0 +1,107 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+"""Measures futex contention."""
+from __future__ import annotations
+
+import argparse
+from collections import defaultdict
+from typing import Dict, Tuple
+import perf
+
+class LockStats:
+    """Aggregate lock contention information."""
+    def __init__(self) -> None:
+        self.count = 0
+        self.total_time = 0
+        self.min_time = 0
+        self.max_time = 0
+
+    def add(self, duration: int) -> None:
+        """Add a new duration measurement."""
+        self.count += 1
+        self.total_time += duration
+        if self.count == 1:
+            self.min_time = duration
+            self.max_time = duration
+        else:
+            self.min_time = min(self.min_time, duration)
+            self.max_time = max(self.max_time, duration)
+
+    def avg(self) -> float:
+        """Return average duration."""
+        return self.total_time / self.count if self.count > 0 else 0.0
+
+process_names: Dict[int, str] = {}
+start_times: Dict[int, Tuple[int, int]] = {}
+session = None
+durations: Dict[Tuple[int, int], LockStats] = defaultdict(LockStats)
+
+FUTEX_WAIT = 0
+FUTEX_PRIVATE_FLAG = 128
+FUTEX_CLOCK_REALTIME = 256
+# Mask out FUTEX_PRIVATE_FLAG and FUTEX_CLOCK_REALTIME so variants such as
+# FUTEX_WAIT_PRIVATE (0 | 128) match the base FUTEX_WAIT command.
+FUTEX_CMD_MASK = ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
+
+
+def handle_start(pid: int, tid: int, uaddr: int, op: int, start_time: int) -> None:
+    """Handle a futex sys_enter event."""
+    if (op & FUTEX_CMD_MASK) != FUTEX_WAIT:
+        return
+    try:
+        if session:
+            process = session.find_thread(pid, tid)
+            if process:
+                process_names[tid] = process.comm() or "unknown"
+    except (TypeError, AttributeError):
+        pass
+    if tid not in process_names:
+        process_names[tid] = "unknown"
+
+    start_times[tid] = (uaddr, start_time)
+
+def handle_end(tid: int, end_time: int) -> None:
+    """Handle a futex sys_exit event."""
+    if tid not in start_times:
+        return
+    (uaddr, start_time) = start_times[tid]
+    del start_times[tid]
+    durations[(tid, uaddr)].add(end_time - start_time)
+
+def process_event(sample: perf.sample_event) -> None:
+    """Process a single sample event."""
+    event_name = str(sample.evsel)
+    if event_name.startswith("evsel(") and event_name.endswith(")"):
+        event_name = event_name[6:-1]
+    if event_name.startswith("syscalls:sys_enter_futex"):
+        uaddr = getattr(sample, "uaddr", None)
+        op = getattr(sample, "op", None)
+        if uaddr is None or op is None:
+            return  # Tracepoint fields missing, skip silent attribution to 0
+        handle_start(getattr(sample, "sample_pid", sample.sample_tid),
+                     sample.sample_tid, uaddr, op, sample.sample_time)
+    elif event_name.startswith("syscalls:sys_exit_futex"):
+        handle_end(sample.sample_tid, sample.sample_time)
+
+
+if __name__ == "__main__":
+    ap = argparse.ArgumentParser(description="Measure futex contention")
+    ap.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    args = ap.parse_args()
+
+    try:
+        session = perf.session(perf.data(args.input), sample=process_event)
+        try:
+            session.process_events()
+        except KeyboardInterrupt:
+            pass
+
+        for ((t, u), stats) in sorted(durations.items()):
+            avg_ns = stats.avg()
+            print(f"{process_names.get(t, 'unknown')}[{t}] lock {u:x} contended "
+                  f"{stats.count} times, {avg_ns:.0f} avg ns "
+                  f"[max: {stats.max_time} ns, min {stats.min_time} ns]")
+    finally:
+        # Break the reference cycle between the global session and the
+        # process_event callback so the C perf.session object is freed.
+        session = None
tools/perf/tests/shell/test_futex_contention_python.sh new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#368 lore raw

PATCH v4 31/49 perf python: Port task-analyzer to perf module

In reply to Ian Rogers

Port task-analyzer.py from tools/perf/scripts/python/ to a standalone script in tools/perf/python/ refactored into a class-based architecture.

Improvements compared to the legacy script:
- Support both offline perf.data file analysis (using perf.session) and live trace capture (using evlist.read_on_cpu), accessing sched:sched_switch tracepoint fields directly from sample objects.
- Automatically disable ANSI terminal color escape sequences when --csv or --csv-summary is enabled so CSV column headers ('Comm,', 'Time Out-Out,', etc.) are never polluted by color codes when running interactively on a TTY (e.g. as root).
- Sanitize non-printable characters and leading CSV formula characters (=, +, -, @) in task comm strings.
- Emit a one-time warning to stderr if sched:sched_switch samples are missing tracepoint fields (such as when recorded without libtraceevent support).

Update test_task_analyzer.sh to invoke the standalone script.

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/task-analyzer.py           | 908 +++++++++++++++++++
 tools/perf/tests/shell/test_task_analyzer.sh |  96 +-
 2 files changed, 971 insertions(+), 33 deletions(-)
 create mode 100755 tools/perf/python/task-analyzer.py
tools/perf/python/task-analyzer.py new file +908 -0
new file mode 100755
index 000000000000..fedba36b5ced
@@ -0,0 +1,908 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+# task-analyzer.py - comprehensive perf tasks analysis
+# Copyright (c) 2022, Hagen Paul Pfeifer <[email protected]>
+# Licensed under the terms of the GNU GPL License version 2
+#
+# Usage:
+#
+#     perf record -e sched:sched_switch -a -- sleep 10
+#     ./task-analyzer.py
+#
+"""Comprehensive perf tasks analysis."""
+from __future__ import annotations
+
+import argparse
+from contextlib import contextmanager
+import decimal
+
+from typing import List, Dict, Union
+
+def _median(numbers: List[decimal.Decimal]) -> decimal.Decimal:
+    """phython3 hat statistics module - we have nothing"""
+    n = len(numbers)
+    index = n // 2
+    if n % 2:
+        return sorted(numbers)[index]
+    return sum(sorted(numbers)[index - 1 : index + 1]) / decimal.Decimal(2)
+
+def _mean(numbers: List[decimal.Decimal]) -> decimal.Decimal:
+    return sum(numbers) / decimal.Decimal(len(numbers))
+
+import os
+import string
+import sys
+from typing import Any, Optional
+import perf
+
+
+# Columns will have a static size to align everything properly
+# Support of 116 days of active update with nano precision
+LEN_SWITCHED_IN = len("9999999.999999999")
+LEN_SWITCHED_OUT = len("9999999.999999999")
+LEN_CPU = len("000")
+LEN_PID = len("maxvalue")
+LEN_TID = len("maxvalue")
+LEN_COMM = len("max-comms-length")
+LEN_RUNTIME = len("999999.999")
+# Support of 3.45 hours of timespans
+LEN_OUT_IN = len("99999999999.999")
+LEN_OUT_OUT = len("99999999999.999")
+LEN_IN_IN = len("99999999999.999")
+LEN_IN_OUT = len("99999999999.999")
+
+class Timespans:
+    """Tracks elapsed time between occurrences of the same task."""
+    def __init__(self, args: argparse.Namespace, time_unit: str) -> None:
+        self.args = args
+        self.time_unit = time_unit
+        self._last_start: Optional[decimal.Decimal] = None
+        self._last_finish: Optional[decimal.Decimal] = None
+        self.current = {
+            'out_out': decimal.Decimal(-1),
+            'in_out': decimal.Decimal(-1),
+            'out_in': decimal.Decimal(-1),
+            'in_in': decimal.Decimal(-1)
+        }
+        if args.summary_extended:
+            self._time_in: decimal.Decimal = decimal.Decimal(-1)
+            self.max_vals = {
+                'out_in': decimal.Decimal(-1),
+                'at': decimal.Decimal(-1),
+                'in_out': decimal.Decimal(-1),
+                'in_in': decimal.Decimal(-1),
+                'out_out': decimal.Decimal(-1)
+            }
+
+    def feed(self, task: 'Task') -> None:
+        """Calculate timespans from chronological task occurrences."""
+        if not self._last_finish:
+            self._last_start = task.time_in(self.time_unit)
+            self._last_finish = task.time_out(self.time_unit)
+            return
+        assert self._last_start is not None
+        assert self._last_finish is not None
+        self._time_in = task.time_in()
+        time_in = task.time_in(self.time_unit)
+        time_out = task.time_out(self.time_unit)
+        self.current['in_in'] = time_in - self._last_start
+        self.current['out_in'] = time_in - self._last_finish
+        self.current['in_out'] = time_out - self._last_start
+        self.current['out_out'] = time_out - self._last_finish
+        if self.args.summary_extended:
+            self.update_max_entries()
+        self._last_finish = task.time_out(self.time_unit)
+        self._last_start = task.time_in(self.time_unit)
+
+    def update_max_entries(self) -> None:
+        """Update maximum timespans."""
+        self.max_vals['in_in'] = max(self.max_vals['in_in'], self.current['in_in'])
+        self.max_vals['out_out'] = max(self.max_vals['out_out'], self.current['out_out'])
+        self.max_vals['in_out'] = max(self.max_vals['in_out'], self.current['in_out'])
+        if self.current['out_in'] > self.max_vals['out_in']:
+            self.max_vals['out_in'] = self.current['out_in']
+            self.max_vals['at'] = self._time_in
+
+class Task:
+    """Handles information of a given task."""
+    def __init__(self, task_id: str, tid: int, cpu: int, comm: str) -> None:
+        self.id = task_id
+        self.tid = tid
+        self.cpu = cpu
+        self.comm = comm
+        self.pid: Optional[int] = None
+        self._time_in: Optional[decimal.Decimal] = None
+        self._time_out: Optional[decimal.Decimal] = None
+
+    def schedule_in_at(self, time_ns: int) -> None:
+        """Set schedule in time."""
+        self._time_in = decimal.Decimal(time_ns) / decimal.Decimal(1e9)
+
+    def schedule_out_at(self, time_ns: int) -> None:
+        """Set schedule out time."""
+        self._time_out = decimal.Decimal(time_ns) / decimal.Decimal(1e9)
+
+    def time_out(self, unit: str = "s") -> decimal.Decimal:
+        """Return schedule out time."""
+        factor = TaskAnalyzer.time_uniter(unit)
+        return self._time_out * decimal.Decimal(factor) if self._time_out else decimal.Decimal(0)
+
+    def time_in(self, unit: str = "s") -> decimal.Decimal:
+        """Return schedule in time."""
+        factor = TaskAnalyzer.time_uniter(unit)
+        return self._time_in * decimal.Decimal(factor) if self._time_in else decimal.Decimal(0)
+
+    def runtime(self, unit: str = "us") -> decimal.Decimal:
+        """Return runtime."""
+        factor = TaskAnalyzer.time_uniter(unit)
+        if self._time_out is not None and self._time_in is not None:
+            return (self._time_out - self._time_in) * decimal.Decimal(factor)
+        return decimal.Decimal(0)
+
+    def update_pid(self, pid: int) -> None:
+        """Update PID."""
+        self.pid = pid
+
+class Summary:
+    """
+    Primary instance for calculating the summary output. Processes the whole trace to
+    find and memorize relevant data such as mean, max et cetera. This instance handles
+    dynamic alignment aspects for summary output.
+    """
+
+    def __init__(self, analyzer):
+        self.analyzer = analyzer
+        self.args = analyzer.args
+        self.db = analyzer.db
+        self.time_unit = analyzer.time_unit
+        self.fd_sum = analyzer.fd_sum
+        self._body = []
+
+    class AlignmentHelper:
+        """
+        Used to calculated the alignment for the output of the summary.
+        """
+        def __init__(self, pid, tid, comm, runs, acc, mean,
+                    median, min_val, max_val, max_at):
+            self.pid = pid
+            self.tid = tid
+            self.comm = comm
+            self.runs = runs
+            self.acc = acc
+            self.mean = mean
+            self.median = median
+            self.min = min_val
+            self.max = max_val
+            self.max_at = max_at
+            self.out_in = None
+            self.inter_at = None
+            self.out_out = None
+            self.in_in = None
+            self.in_out = None
+
+    def _print_header(self):
+        '''
+        Output is aligned using the formatted column widths in self.db.
+        '''
+        len_tasks = max(
+            len("Task Information"),
+            sum(self.db["task_info"].values()) + len(self.db["task_info"]) - 1,
+        )
+        fmt = "{{:^{}}}".format(len_tasks)
+        fmt += " {{:^{}}}".format(
+            sum(self.db["runtime_info"].values()) + len(self.db["runtime_info"]) - 1
+        )
+        _header = ("Task Information", "Runtime Information")
+
+        if self.args.summary_extended:
+            fmt += " {{:^{}}}".format(
+                sum(self.db["inter_times"].values()) + len(self.db["inter_times"]) - 1
+            )
+            _header += ("Max Inter Task Times",)
+        self.fd_sum.write(fmt.format(*_header) + "\n")
+
+    def _column_titles(self):
+        """
+        Cells are being processed and displayed in different way so an alignment adjust
+        is implemented depeding on the choice of the timeunit. The positions of the max
+        values are being displayed in grey. Thus in their format two additional {},
+        are placed for color set and reset.
+        """
+        separator, fix_csv_align = self.analyzer.prepare_fmt_sep(is_summary=True)
+        fmt = "{{:>{}}}".format(self.db["task_info"]["pid"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(separator, self.db["task_info"]["tid"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(separator, self.db["task_info"]["comm"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(separator, self.db["runtime_info"]["runs"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(separator, self.db["runtime_info"]["acc"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(separator, self.db["runtime_info"]["mean"] * fix_csv_align)
+        fmt += "{}{{:>{}}}".format(
+            separator, self.db["runtime_info"]["median"] * fix_csv_align
+        )
+        fmt += "{}{{:>{}}}".format(
+            separator, self.db["runtime_info"]["min"] * fix_csv_align
+        )
+        fmt += "{}{{:>{}}}".format(
+            separator, self.db["runtime_info"]["max"] * fix_csv_align
+        )
+        fmt += "{}{{}}{{:>{}}}{{}}".format(
+            separator, self.db["runtime_info"]["max_at"] * fix_csv_align
+        )
+
+        grey = "" if self.args.csv_summary else TaskAnalyzer.COLORS["grey"]
+        reset = "" if self.args.csv_summary else TaskAnalyzer.COLORS["reset"]
+        column_titles = ("PID", "TID", "Comm")
+        column_titles += ("Runs", "Accumulated", "Mean", "Median", "Min", "Max")
+        column_titles += (grey, "Max At", reset)
+
+        if self.args.summary_extended:
+            fmt += "{}{{:>{}}}".format(
+                separator,
+                self.db["inter_times"]["out_in"] * fix_csv_align
+            )
+            fmt += "{}{{}}{{:>{}}}{{}}".format(
+                separator,
+                self.db["inter_times"]["inter_at"] * fix_csv_align
+            )
+            fmt += "{}{{:>{}}}".format(
+                separator,
+                self.db["inter_times"]["out_out"] * fix_csv_align
+            )
+            fmt += "{}{{:>{}}}".format(
+                separator,
+                self.db["inter_times"]["in_in"] * fix_csv_align
+            )
+            fmt += "{}{{:>{}}}".format(
+                separator,
+                self.db["inter_times"]["in_out"] * fix_csv_align
+            )
+
+            column_titles += (
+                "Out-In", grey, "Max At",
+                reset, "Out-Out", "In-In", "In-Out"
+            )
+
+        self.fd_sum.write(fmt.format(*column_titles) + "\n")
+
+    def _task_stats(self):
+        """calculates the stats of every task and constructs the printable summary"""
+        grey = "" if self.args.csv_summary else TaskAnalyzer.COLORS["grey"]
+        reset = "" if self.args.csv_summary else TaskAnalyzer.COLORS["reset"]
+        for tid in sorted(self.db["tid"]):
+            color_one_sample = grey
+            color_reset = reset
+            no_executed = 0
+            runtimes = []
+            time_in = []
+            timespans = Timespans(self.args, self.time_unit)
+            for task in self.db["tid"][tid]:
+                pid = task.pid
+                comm = task.comm
+                no_executed += 1
+                runtimes.append(task.runtime(self.time_unit))
+                time_in.append(task.time_in())
+                timespans.feed(task)
+            if len(runtimes) > 1:
+                color_one_sample = ""
+                color_reset = ""
+            time_max = max(runtimes)
+            time_min = min(runtimes)
+            max_at = time_in[runtimes.index(max(runtimes))]
+
+            # The size of the decimal after sum,mean and median varies, thus we cut
+            # the decimal number, by rounding it. It has no impact on the output,
+            # because we have a precision of the decimal points at the output.
+            time_sum = round(sum(runtimes), 3)
+            time_mean = round(_mean(runtimes), 3)
+            time_median = round(_median(runtimes), 3)
+
+            align_helper = self.AlignmentHelper(pid, tid, comm, no_executed, time_sum,
+                                    time_mean, time_median, time_min, time_max, max_at)
+            self._body.append([
+                pid, tid, comm, no_executed, time_sum, color_one_sample,
+                time_mean, time_median, time_min, time_max,
+                grey, max_at,
+                reset, color_reset
+            ])
+            if self.args.summary_extended:
+                self._body[-1].extend([timespans.max_vals['out_in'],
+                                grey, timespans.max_vals['at'],
+                                reset, timespans.max_vals['out_out'],
+                                timespans.max_vals['in_in'],
+                                timespans.max_vals['in_out']])
+                align_helper.out_in = timespans.max_vals['out_in']
+                align_helper.inter_at = timespans.max_vals['at']
+                align_helper.out_out = timespans.max_vals['out_out']
+                align_helper.in_in = timespans.max_vals['in_in']
+                align_helper.in_out = timespans.max_vals['in_out']
+            self._calc_alignments_summary(align_helper)
+
+    def _format_stats(self):
+        separator, fix_csv_align = self.analyzer.prepare_fmt_sep(is_summary=True)
+        decimal_precision, time_precision = self.analyzer.prepare_fmt_precision()
+        len_pid = self.db["task_info"]["pid"] * fix_csv_align
+        len_tid = self.db["task_info"]["tid"] * fix_csv_align
+        len_comm = self.db["task_info"]["comm"] * fix_csv_align
+        len_runs = self.db["runtime_info"]["runs"] * fix_csv_align
+        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 = self.db["runtime_info"]["min"] * fix_csv_align
+        len_max = self.db["runtime_info"]["max"] * fix_csv_align
+        len_max_at = self.db["runtime_info"]["max_at"] * fix_csv_align
+        if self.args.summary_extended:
+            len_out_in = self.db["inter_times"]["out_in"] * fix_csv_align
+            len_inter_at = self.db["inter_times"]["inter_at"] * fix_csv_align
+            len_out_out = self.db["inter_times"]["out_out"] * fix_csv_align
+            len_in_in = self.db["inter_times"]["in_in"] * fix_csv_align
+            len_in_out = self.db["inter_times"]["in_out"] * fix_csv_align
+
+        fmt = "{{:{}d}}".format(len_pid)
+        fmt += "{}{{:{}d}}".format(separator, len_tid)
+        fmt += "{}{{:>{}}}".format(separator, len_comm)
+        fmt += "{}{{:{}d}}".format(separator, len_runs)
+        fmt += "{}{{:{}.{}f}}".format(separator, len_acc, time_precision)
+        fmt += "{}{{}}{{:{}.{}f}}".format(separator, len_mean, time_precision)
+        fmt += "{}{{:{}.{}f}}".format(separator, len_median, time_precision)
+        fmt += "{}{{:{}.{}f}}".format(separator, len_min, time_precision)
+        fmt += "{}{{:{}.{}f}}".format(separator, len_max, time_precision)
+        fmt += "{}{{}}{{:{}.{}f}}{{}}{{}}".format(
+            separator, len_max_at, decimal_precision
+        )
+        if self.args.summary_extended:
+            fmt += "{}{{:{}.{}f}}".format(separator, len_out_in, time_precision)
+            fmt += "{}{{}}{{:{}.{}f}}{{}}".format(
+                separator, len_inter_at, decimal_precision
+            )
+            fmt += "{}{{:{}.{}f}}".format(separator, len_out_out, time_precision)
+            fmt += "{}{{:{}.{}f}}".format(separator, len_in_in, time_precision)
+            fmt += "{}{{:{}.{}f}}".format(separator, len_in_out, time_precision)
+        return fmt
+
+    def _calc_alignments_summary(self, align_helper):
+        # Measure the formatted string widths (e.g. f"{val:.{decimal_precision}f}"
+        # and f"{val:.{time_precision}f}") rather than str(val), because Decimal's
+        # default string representation may include unrounded fractional digits or
+        # omit trailing zeros that _format_stats() renders with fixed precision.
+        decimal_precision, time_precision = self.analyzer.prepare_fmt_precision()
+        for key in self.db["task_info"]:
+            val_len = len(str(getattr(align_helper, key)))
+            if val_len > self.db["task_info"][key]:
+                self.db["task_info"][key] = val_len
+        for key in self.db["runtime_info"]:
+            val = getattr(align_helper, key)
+            if key == "runs":
+                val_len = len(str(val))
+            elif key == "max_at":
+                val_len = len(f"{val:.{decimal_precision}f}")
+            else:
+                val_len = len(f"{val:.{time_precision}f}")
+            if val_len > self.db["runtime_info"][key]:
+                self.db["runtime_info"][key] = val_len
+        if self.args.summary_extended:
+            for key in self.db["inter_times"]:
+                val = getattr(align_helper, key)
+                if key == "inter_at":
+                    val_len = len(f"{val:.{decimal_precision}f}")
+                else:
+                    val_len = len(f"{val:.{time_precision}f}")
+                if val_len > self.db["inter_times"][key]:
+                    self.db["inter_times"][key] = val_len
+
+    def print(self):
+        self._task_stats()
+        fmt = self._format_stats()
+
+        if not self.args.csv_summary:
+            print("\nSummary", file=self.fd_sum)
+            self._print_header()
+        self._column_titles()
+        for i in range(len(self._body)):
+            self.fd_sum.write(fmt.format(*tuple(self._body[i])) + "\n")
+
+
+class TaskAnalyzer:
+
+    """Main class for task analysis."""
+
+    COLORS = {
+        "grey": "\033[90m",
+        "red": "\033[91m",
+        "green": "\033[92m",
+        "yellow": "\033[93m",
+        "blue": "\033[94m",
+        "violet": "\033[95m",
+        "reset": "\033[0m",
+    }
+
+    def __init__(self, args: argparse.Namespace) -> None:
+        self.args = args
+        self.db: Dict[str, Any] = {}
+        self.session: Optional[perf.session] = None
+        self._tgid_cache: Dict[int, int] = {}
+        self.time_unit = "us"
+        if args.ns:
+            self.time_unit = "ns"
+        elif args.ms:
+            self.time_unit = "ms"
+        self._init_db()
+        self._check_color()
+        self.fd_task = sys.stdout
+        self.fd_sum = sys.stdout
+
+    @contextmanager
+    def open_output(self, filename: str, default: Any):
+        """Context manager for file or stdout."""
+        if filename:
+            with open(filename, "w", encoding="utf-8") as f:
+                yield f
+        else:
+            yield default
+
+    def _init_db(self) -> None:
+        self.db["running"] = {}
+        self.db["tid"] = {}
+        self.db["global"] = []
+        if (self.args.summary or self.args.summary_extended or
+                self.args.summary_only or self.args.csv_summary):
+            self.db["task_info"] = {}
+            self.db["runtime_info"] = {}
+            self.db["task_info"]["pid"] = len("PID")
+            self.db["task_info"]["tid"] = len("TID")
+            self.db["task_info"]["comm"] = len("Comm")
+            self.db["runtime_info"]["runs"] = len("Runs")
+            self.db["runtime_info"]["acc"] = len("Accumulated")
+            self.db["runtime_info"]["max"] = len("Max")
+            self.db["runtime_info"]["max_at"] = len("Max At")
+            self.db["runtime_info"]["min"] = len("Min")
+            self.db["runtime_info"]["mean"] = len("Mean")
+            self.db["runtime_info"]["median"] = len("Median")
+            if self.args.summary_extended:
+                self.db["inter_times"] = {}
+                self.db["inter_times"]["out_in"] = len("Out-In")
+                self.db["inter_times"]["inter_at"] = len("Max At")
+                self.db["inter_times"]["out_out"] = len("Out-Out")
+                self.db["inter_times"]["in_in"] = len("In-In")
+                self.db["inter_times"]["in_out"] = len("In-Out")
+
+    def _check_color(self) -> None:
+        """Check if color should be enabled."""
+        if self.args.csv:
+            TaskAnalyzer.COLORS = {k: "" for k in TaskAnalyzer.COLORS}
+            return
+        if sys.stdout.isatty() and self.args.stdio_color != "never":
+            return
+        if self.args.stdio_color == "always":
+            return
+        TaskAnalyzer.COLORS = {k: "" for k in TaskAnalyzer.COLORS}
+
+    @staticmethod
+    def time_uniter(unit: str) -> float:
+        """Return time unit factor."""
+        picker = {"s": 1, "ms": 1e3, "us": 1e6, "ns": 1e9}
+        return picker[unit]
+
+    def _task_id(self, pid: int, cpu: int) -> str:
+        return f"{pid}-{cpu}"
+
+    def _filter_non_printable(self, unfiltered: Union[str, bytearray]) -> str:
+        # Strip non-printable characters, whitespace control characters
+        # (\r\n\t\x0b\x0c), quotes (", '), and ';' because --csv and
+        # --csv-summary emit unquoted ';'-delimited columns. Also replace a
+        # leading '=', '+', '-', or '@' with '_' to mitigate CSV formula / DDE
+        # injection attacks if an attacker-controlled process comm is opened in
+        # a spreadsheet program.
+        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
+
+    def prepare_fmt_precision(self) -> tuple[int, int]:
+        if self.args.ns:
+            return 9, 0
+        return 6, 3
+
+    def prepare_fmt_sep(self, is_summary: bool = False) -> tuple[str, int]:
+        # fix_csv_align multiplies format field widths: 0 in CSV mode collapses
+        # {:>N} padding to {:>0} (no extra spaces around ';'), while 1 in
+        # standard text mode preserves fixed-width column alignment.
+        if (is_summary and self.args.csv_summary) or (not is_summary and self.args.csv):
+            return ";", 0
+        return " ", 1
+
+    def _fmt_header(self) -> str:
+        separator, fix_csv_align = self.prepare_fmt_sep()
+        fmt = f"{{:>{LEN_SWITCHED_IN*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_SWITCHED_OUT*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_CPU*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_PID*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_TID*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_COMM*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_RUNTIME*fix_csv_align}}}"
+        fmt += f"{separator}{{:>{LEN_OUT_IN*fix_csv_align}}}"
+        if self.args.extended_times:
+            fmt += f"{separator}{{:>{LEN_OUT_OUT*fix_csv_align}}}"
+            fmt += f"{separator}{{:>{LEN_IN_IN*fix_csv_align}}}"
+            fmt += f"{separator}{{:>{LEN_IN_OUT*fix_csv_align}}}"
+        return fmt
+
+    def _fmt_body(self) -> str:
+        separator, fix_csv_align = self.prepare_fmt_sep()
+        decimal_precision, time_precision = self.prepare_fmt_precision()
+        fmt = f"{{}}{{:{LEN_SWITCHED_IN*fix_csv_align}.{decimal_precision}f}}"
+        fmt += f"{separator}{{:{LEN_SWITCHED_OUT*fix_csv_align}.{decimal_precision}f}}"
+        fmt += f"{separator}{{:{LEN_CPU*fix_csv_align}d}}"
+        fmt += f"{separator}{{:{LEN_PID*fix_csv_align}d}}"
+        fmt += f"{separator}{{}}{{:{LEN_TID*fix_csv_align}d}}{{}}"
+        fmt += f"{separator}{{}}{{:>{LEN_COMM*fix_csv_align}}}"
+        fmt += f"{separator}{{:{LEN_RUNTIME*fix_csv_align}.{time_precision}f}}"
+        if self.args.extended_times:
+            fmt += f"{separator}{{:{LEN_OUT_IN*fix_csv_align}.{time_precision}f}}"
+            fmt += f"{separator}{{:{LEN_OUT_OUT*fix_csv_align}.{time_precision}f}}"
+            fmt += f"{separator}{{:{LEN_IN_IN*fix_csv_align}.{time_precision}f}}"
+            fmt += f"{separator}{{:{LEN_IN_OUT*fix_csv_align}.{time_precision}f}}{{}}"
+        else:
+            fmt += f"{separator}{{:{LEN_OUT_IN*fix_csv_align}.{time_precision}f}}{{}}"
+        return fmt
+
+    def _print_header(self) -> None:
+        fmt = self._fmt_header()
+        header = ["Switched-In", "Switched-Out", "CPU", "PID", "TID", "Comm",
+                  "Runtime", "Time Out-In"]
+        if self.args.extended_times:
+            header += ["Time Out-Out", "Time In-In", "Time In-Out"]
+        self.fd_task.write(fmt.format(*header) + "\n")
+
+    def _print_task_finish(self, task: Task) -> None:
+        c_row_set = ""
+        c_row_reset = ""
+        out_in: Any = -1
+        out_out: Any = -1
+        in_in: Any = -1
+        in_out: Any = -1
+        fmt = self._fmt_body()
+
+        if str(task.tid) in self.args.highlight_tasks_map:
+            c_row_set = TaskAnalyzer.COLORS.get(self.args.highlight_tasks_map[str(task.tid)], '')
+            c_row_reset = TaskAnalyzer.COLORS["reset"]
+        if task.comm in self.args.highlight_tasks_map:
+            c_row_set = TaskAnalyzer.COLORS.get(self.args.highlight_tasks_map[task.comm], '')
+            c_row_reset = TaskAnalyzer.COLORS["reset"]
+
+        c_tid_set = ""
+        c_tid_reset = ""
+        if task.pid == task.tid:
+            c_tid_set = TaskAnalyzer.COLORS["grey"]
+            c_tid_reset = TaskAnalyzer.COLORS["reset"]
+
+        if task.tid in self.db["tid"]:
+            last_tid_task = self.db["tid"][task.tid][-1]
+            timespan_gap_tid = Timespans(self.args, self.time_unit)
+            timespan_gap_tid.feed(last_tid_task)
+            timespan_gap_tid.feed(task)
+            out_in = timespan_gap_tid.current['out_in']
+            out_out = timespan_gap_tid.current['out_out']
+            in_in = timespan_gap_tid.current['in_in']
+            in_out = timespan_gap_tid.current['in_out']
+
+        if self.args.extended_times:
+            line_out = fmt.format(c_row_set, task.time_in(), task.time_out(), task.cpu,
+                            task.pid, c_tid_set, task.tid, c_tid_reset, c_row_set, task.comm,
+                            task.runtime(self.time_unit), out_in, out_out, in_in, in_out,
+                            c_row_reset) + "\n"
+        else:
+            line_out = fmt.format(c_row_set, task.time_in(), task.time_out(), task.cpu,
+                            task.pid, c_tid_set, task.tid, c_tid_reset, c_row_set, task.comm,
+                            task.runtime(self.time_unit), out_in, c_row_reset) + "\n"
+        self.fd_task.write(line_out)
+
+    def _record_cleanup(self, _list: list[Any]) -> list[Any]:
+        need_summary = (self.args.summary or self.args.summary_extended or
+                        self.args.summary_only or self.args.csv_summary)
+        if not need_summary and len(_list) > 1:
+            return _list[len(_list) - 1:]
+        return _list
+
+    def _record_by_tid(self, task: Task) -> None:
+        tid = task.tid
+        if tid not in self.db["tid"]:
+            self.db["tid"][tid] = []
+        self.db["tid"][tid].append(task)
+        self.db["tid"][tid] = self._record_cleanup(self.db["tid"][tid])
+
+    def _record_global(self, task: Task) -> None:
+        self.db["global"].append(task)
+        self.db["global"] = self._record_cleanup(self.db["global"])
+
+    def _handle_task_finish(self, tid: int, cpu: int, time_ns: int, pid: int) -> None:
+        if tid == 0 and (not self.args.tid_renames or 0 not in self.args.tid_renames):
+            return
+        _id = self._task_id(tid, cpu)
+        if _id not in self.db["running"]:
+            return
+        task = self.db["running"][_id]
+        task.schedule_out_at(time_ns)
+        task.update_pid(pid)
+        del self.db["running"][_id]
+
+        if not self._limit_filtered(tid, pid, task.comm):
+            if not self.args.summary_only:
+                self._print_task_finish(task)
+            self._record_by_tid(task)
+            self._record_global(task)
+
+    def _handle_task_start(self, tid: int, cpu: int, comm: str, time_ns: int) -> None:
+        if tid == 0 and (not self.args.tid_renames or 0 not in self.args.tid_renames):
+            return
+        if tid in self.args.tid_renames:
+            comm = self._filter_non_printable(self.args.tid_renames[tid])
+        _id = self._task_id(tid, cpu)
+        if _id in self.db["running"]:
+            return
+        task = Task(_id, tid, cpu, comm)
+        task.schedule_in_at(time_ns)
+        self.db["running"][_id] = task
+
+    def _limit_filtered(self, tid: int, pid: int, comm: str) -> bool:
+        """Filter tasks based on CLI arguments."""
+        match_filter = False
+        if self.args.filter_tasks:
+            if (str(tid) in self.args.filter_tasks or
+                str(pid) in self.args.filter_tasks or
+                comm in self.args.filter_tasks):
+                match_filter = True
+
+        match_limit = False
+        if self.args.limit_to_tasks:
+            if (str(tid) in self.args.limit_to_tasks or
+                str(pid) in self.args.limit_to_tasks or
+                comm in self.args.limit_to_tasks):
+                match_limit = True
+
+        if self.args.filter_tasks and match_filter:
+            return True
+        if self.args.limit_to_tasks and not match_limit:
+            return True
+        return False
+
+    def _is_within_timelimit(self, time_ns: int) -> bool:
+        if not self.args.time_limit:
+            return True
+        time_s = decimal.Decimal(time_ns) / decimal.Decimal(1e9)
+        bounds = self.args.time_limit.split(":")
+        lower_bound = bounds[0] if len(bounds) > 0 else ""
+        upper_bound = bounds[1] if len(bounds) > 1 else ""
+        if lower_bound and time_s < decimal.Decimal(lower_bound):
+            return False
+        if upper_bound and time_s > decimal.Decimal(upper_bound):
+            return False
+        return True
+
+    def process_event(self, sample: perf.sample_event) -> None:
+        """Process sched:sched_switch events."""
+        if "sched:sched_switch" not in str(sample.evsel):
+            return
+
+        time_ns = sample.sample_time
+        if not self._is_within_timelimit(time_ns):
+            return
+
+        # Access tracepoint fields directly from sample object
+        try:
+            prev_pid = sample.prev_pid
+            next_pid = sample.next_pid
+            next_comm = sample.next_comm
+            common_cpu = sample.sample_cpu
+        except AttributeError:
+            if not self.db.get("warned_missing_fields"):
+                self.db["warned_missing_fields"] = True
+                print("Warning: sched:sched_switch sample missing tracepoint fields "
+                      "(is libtraceevent support enabled?).", file=sys.stderr)
+            return
+
+        next_comm = self._filter_non_printable(next_comm)
+
+        # Task finish for previous task. Resolve TGID (process ID) from TID:
+        # in file mode we query perf.session.find_thread() (checking thread.pid > 0
+        # so an unresolved -1 does not overwrite the prev_pid fallback), whereas in
+        # live mode (self.session is None) we read /proc/<tid>/status and cache
+        # the lookup in _tgid_cache.
+        prev_tgid = prev_pid  # Fallback
+        if prev_pid != 0:
+            if self.session:
+                try:
+                    thread = self.session.find_thread(-1, prev_pid)
+                    if thread and thread.pid > 0:
+                        prev_tgid = thread.pid
+                except (OSError, ValueError, KeyError, RuntimeError, TypeError, AttributeError):
+                    pass
+            elif prev_pid in self._tgid_cache:
+                prev_tgid = self._tgid_cache[prev_pid]
+            else:
+                if len(self._tgid_cache) >= 4096:
+                    self._tgid_cache.pop(next(iter(self._tgid_cache)))
+                self._tgid_cache[prev_pid] = prev_tgid
+                try:
+                    with open(f"/proc/{prev_pid}/status", encoding="utf-8") as f:
+                        for line in f:
+                            if line.startswith("Tgid:"):
+                                prev_tgid = int(line.split()[1])
+                                self._tgid_cache[prev_pid] = prev_tgid
+                                break
+                except (OSError, ValueError, KeyError, RuntimeError):
+                    pass
+        self._handle_task_finish(prev_pid, common_cpu, time_ns, prev_tgid)
+        # Task start for next task
+        self._handle_task_start(next_pid, common_cpu, next_comm, time_ns)
+
+    def print_summary(self) -> None:
+        """Calculate and print summary."""
+        need_summary = (self.args.summary or self.args.summary_extended or
+                        self.args.summary_only or self.args.csv_summary)
+        if not need_summary:
+            return
+
+        Summary(self).print()
+
+    def _run_file(self) -> None:
+        if not self.args.summary_only:
+            self._print_header()
+
+        session = perf.session(perf.data(self.args.input), sample=self.process_event)
+        self.session = session
+        try:
+            session.process_events()
+        except KeyboardInterrupt:
+            pass
+        finally:
+            # Break the reference cycle between self.session and the bound
+            # self.process_event callback so the C perf.session object is freed.
+            self.session = None
+
+        if not self.db["global"]:
+            print(f"Warning: No sched:sched_switch trace events found in '{self.args.input}'.",
+                  file=sys.stderr)
+
+        self.print_summary()
+
+    def _run_live(self) -> None:
+        if not self.args.summary_only:
+            self._print_header()
+
+        cpus = perf.cpu_map()
+        threads = perf.thread_map(-1)
+        evlist = perf.parse_events("sched:sched_switch", cpus, threads)
+        evlist.config()
+
+        evlist.open()
+        evlist.mmap()
+        evlist.enable()
+
+        # Per-CPU perf ring buffers are drained sequentially in a loop, so events
+        # read from CPU N may have timestamps slightly earlier than events just
+        # read from CPU 0. Buffer events in pending_events and only dispatch those
+        # older than a 50 ms watermark behind max_ts so cross-CPU migrations and
+        # switches are processed in global timestamp order.
+        pending_events: list[perf.sample_event] = []
+        print("Live mode started. Press Ctrl+C to stop.", file=sys.stderr)
+        try:
+            while True:
+                try:
+                    evlist.poll(timeout=100)
+                except InterruptedError:
+                    continue
+                for cpu in cpus:
+                    while True:
+                        event = evlist.read_on_cpu(cpu)
+                        if not event:
+                            break
+                        if not isinstance(event, perf.sample_event):
+                            continue
+                        pending_events.append(event)
+                if pending_events:
+                    pending_events.sort(
+                        key=lambda e: getattr(e, 'sample_time', getattr(e, 'time', 0))
+                    )
+                    max_ts = getattr(
+                        pending_events[-1], 'sample_time', getattr(pending_events[-1], 'time', 0)
+                    )
+                    cutoff_ts = max_ts - 50_000_000
+                    ready_idx = 0
+                    for event in pending_events:
+                        ev_ts = getattr(event, 'sample_time', getattr(event, 'time', 0))
+                        if ev_ts <= cutoff_ts:
+                            self.process_event(event)
+                            ready_idx += 1
+                        else:
+                            break
+                    del pending_events[:ready_idx]
+        except KeyboardInterrupt:
+            print("\nStopping live mode...", file=sys.stderr)
+        finally:
+            pending_events.sort(key=lambda e: getattr(e, 'sample_time', getattr(e, 'time', 0)))
+            for event in pending_events:
+                self.process_event(event)
+            evlist.close()
+            self.print_summary()
+
+    def run(self) -> None:
+        """Run the session."""
+        is_live = (self.args.live or
+                   (not os.path.exists(self.args.input) and self.args.input == "perf.data"))
+        with self.open_output(self.args.csv, sys.stdout) as fd_task:
+            if self.args.csv and self.args.csv == self.args.csv_summary:
+                self.fd_task = fd_task
+                self.fd_sum = fd_task
+                if is_live:
+                    self._run_live()
+                else:
+                    self._run_file()
+            else:
+                with self.open_output(self.args.csv_summary, sys.stdout) as fd_sum:
+                    self.fd_task = fd_task
+                    self.fd_sum = fd_sum
+                    if is_live:
+                        self._run_live()
+                    else:
+                        self._run_file()
+
+def main() -> None:
+    """Main function."""
+    parser = argparse.ArgumentParser(description="Analyze tasks behavior")
+    parser.add_argument("-i", "--input", default="perf.data", help="Input file name")
+    parser.add_argument("--time-limit", default="", help="print tasks only in time window")
+    parser.add_argument("--summary", action="store_true",
+                        help="print additional runtime information")
+    parser.add_argument("--summary-only", action="store_true",
+                        help="print only summary without traces")
+    parser.add_argument("--summary-extended", action="store_true",
+                        help="print extended summary")
+    parser.add_argument("--ns", action="store_true", help="show timestamps in nanoseconds")
+    parser.add_argument("--ms", action="store_true", help="show timestamps in milliseconds")
+    parser.add_argument("--live", action="store_true",
+                        help="force live mode (ignores -i/perf.data)")
+    parser.add_argument("--extended-times", action="store_true",
+                        help="Show elapsed times between schedule in/out")
+    parser.add_argument("--filter-tasks", default="", help="filter tasks by tid, pid or comm")
+    parser.add_argument("--limit-to-tasks", default="", help="limit output to selected tasks")
+    parser.add_argument("--highlight-tasks", default="", help="colorize special tasks")
+    parser.add_argument("--rename-comms-by-tids", default="", help="rename task names by using tid")
+    parser.add_argument("--stdio-color", default="auto", choices=["always", "never", "auto"],
+                        help="configure color output")
+    parser.add_argument("--csv", default="", help="Write trace to file")
+    parser.add_argument("--csv-summary", default="", help="Write summary to file")
+
+    args = parser.parse_args()
+    args.tid_renames = {}
+    args.highlight_tasks_map = {}
+    args.filter_tasks = args.filter_tasks.split(",") if args.filter_tasks else []
+    args.limit_to_tasks = args.limit_to_tasks.split(",") if args.limit_to_tasks else []
+
+    if args.rename_comms_by_tids:
+        for item in args.rename_comms_by_tids.split(","):
+            try:
+                tid, name = item.split(":", 1)
+                args.tid_renames[int(tid)] = name
+            except ValueError:
+                print(f"Error: Invalid format for --rename-comms-by-tids '{item}', "
+                      "expected tid:name", file=sys.stderr)
+                sys.exit(1)
+
+    if args.highlight_tasks:
+        for item in args.highlight_tasks.split(","):
+            parts = item.split(":")
+            if len(parts) == 1:
+                parts.append("red")
+            key, color = parts[0], parts[1]
+            args.highlight_tasks_map[key] = color
+
+    analyzer = TaskAnalyzer(args)
+    analyzer.run()
+
+if __name__ == "__main__":
+    main()
tools/perf/tests/shell/test_task_analyzer.sh +63 -33

174 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author reviewer
#370 lore raw

PATCH v4 32/49 perf python: Port sched-migration and SchedGui to perf module

In reply to Ian Rogers

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.

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

Assisted-by Antigravity:gemini-3.1-pro
---
 tools/perf/python/SchedGui.py                 | 246 +++++++++
 tools/perf/python/sched-migration.py          | 496 ++++++++++++++++++
 .../shell/test_sched_migration_python.sh      |  72 +++
 3 files changed, 814 insertions(+)
 create mode 100755 tools/perf/python/SchedGui.py
 create mode 100755 tools/perf/python/sched-migration.py
 create mode 100755 tools/perf/tests/shell/test_sched_migration_python.sh
tools/perf/python/SchedGui.py new file +246 -0
new file mode 100755
index 000000000000..fa0d010fe528
@@ -0,0 +1,246 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+# SchedGui.py - Python extension for perf script, basic GUI code for
+#		traces drawing and overview.
+#
+# Copyright (C) 2010 by Frederic Weisbecker <[email protected]>
+#
+# Ported to modern directory structure.
+
+"""SchedGui.py - Python extension for perf script, basic GUI code for traces drawing and overview."""
+from __future__ import annotations
+
+import importlib
+from typing import Any
+
+class _DummyWx:
+    """Dummy wx module fallback when wxPython is not installed."""
+    Frame = object
+
+
+try:
+    wx: Any = importlib.import_module("wx")
+    WX_AVAILABLE = True
+except ImportError:
+    wx = _DummyWx
+    WX_AVAILABLE = False
+
+
+class RootFrame(wx.Frame):
+    """Main window frame for scheduling trace visualization."""
+    Y_OFFSET = 100
+    RECT_HEIGHT = 100
+    RECT_SPACE = 50
+    EVENT_MARKING_WIDTH = 5
+
+    def __init__(self, sched_tracer, title, parent=None, win_id=-1):
+        if not WX_AVAILABLE:
+            raise ImportError("You need to install the wxpython lib for this script")
+        wx.Frame.__init__(self, parent, win_id, title)
+
+        self.dc = None
+        (self.screen_width, self.screen_height) = wx.GetDisplaySize()
+        self.screen_width -= 10
+        self.screen_height -= 10
+        self.zoom = 0.5
+        self.scroll_scale = 20
+        self.sched_tracer = sched_tracer
+        self.sched_tracer.set_root_win(self)
+        (self.ts_start, self.ts_end) = sched_tracer.interval()
+        self.update_width_virtual()
+        self.nr_rects = sched_tracer.nr_rectangles() + 1
+        self.height_virtual = RootFrame.Y_OFFSET + \
+            (self.nr_rects * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))
+
+        # whole window panel
+        self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height))
+
+        # scrollable container
+        # Create SplitterWindow
+        self.splitter = wx.SplitterWindow(self.panel, style=wx.SP_3D)
+
+        # scrollable container (Top)
+        self.scroll = wx.ScrolledWindow(self.splitter)
+        self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale,
+                                  int(self.width_virtual // self.scroll_scale),
+                                  int(self.height_virtual // self.scroll_scale))
+        self.scroll.EnableScrolling(True, True)
+        self.scroll.SetFocus()
+
+        # scrollable drawing area
+        self.scroll_panel = wx.Panel(self.scroll,
+                                     size=(self.screen_width - 15, self.screen_height // 2))
+        self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint)
+        self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
+        self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
+        self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press)
+        self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)
+
+        self.scroll_panel.SetSize(int(self.width_virtual), int(self.height_virtual))
+
+        # Create a separate panel for text (Bottom)
+        self.text_panel = wx.Panel(self.splitter)
+        self.text_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.txt = wx.TextCtrl(self.text_panel, -1, "Click a bar to see details",
+                               style=wx.TE_MULTILINE)
+        self.text_sizer.Add(self.txt, 1, wx.EXPAND | wx.ALL, 5)
+        self.text_panel.SetSizer(self.text_sizer)
+
+        # Split the window
+        self.splitter.SplitHorizontally(self.scroll, self.text_panel, (self.screen_height * 3) // 4)
+
+        # Main sizer to layout splitter
+        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.main_sizer.Add(self.splitter, 1, wx.EXPAND)
+        self.panel.SetSizer(self.main_sizer)
+
+        self.scroll.Fit()
+        self.Fit()
+
+        self.Show(True)
+
+    def us_to_px(self, val):
+        """Convert microseconds to pixels."""
+        return val / (10 ** 3) * self.zoom
+
+    def px_to_us(self, val):
+        """Convert pixels to microseconds."""
+        return (val / self.zoom) * (10 ** 3)
+
+    def scroll_start(self):
+        """Get scroll start position in pixels."""
+        (x, y) = self.scroll.GetViewStart()
+        return (x * self.scroll_scale, y * self.scroll_scale)
+
+    def scroll_start_us(self):
+        """Get scroll start position in microseconds."""
+        (x, _) = self.scroll_start()
+        return self.px_to_us(x)
+
+    def paint_rectangle_zone(self, nr, color, top_color, start, end):
+        """Draw a rectangle zone for a CPU."""
+        offset_px = self.us_to_px(start - self.ts_start)
+        width_px = self.us_to_px(end - start)
+
+        offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))
+        width_py = RootFrame.RECT_HEIGHT
+
+        dc = self.dc
+
+        if top_color is not None:
+            (r, g, b) = top_color
+            top_color = wx.Colour(r, g, b)
+            brush = wx.Brush(top_color, wx.SOLID)
+            dc.SetBrush(brush)
+            dc.DrawRectangle(int(offset_px), int(offset_py),
+                             int(width_px), RootFrame.EVENT_MARKING_WIDTH)
+            width_py -= RootFrame.EVENT_MARKING_WIDTH
+            offset_py += RootFrame.EVENT_MARKING_WIDTH
+
+        (r, g, b) = color
+        color = wx.Colour(r, g, b)
+        brush = wx.Brush(color, wx.SOLID)
+        dc.SetBrush(brush)
+        dc.DrawRectangle(int(offset_px), int(offset_py), int(width_px), int(width_py))
+
+    def update_rectangles(self, start, end):
+        """Update rectangles in the given time window."""
+        start += self.ts_start
+        end += self.ts_start
+        self.sched_tracer.fill_zone(start, end)
+
+    def on_paint(self, event):
+        """Handle paint event."""
+        window = event.GetEventObject()
+        dc = wx.PaintDC(window)
+
+        # Clear background to avoid ghosting
+        dc.SetBackground(wx.Brush(window.GetBackgroundColour()))
+        dc.Clear()
+
+        self.dc = dc
+        try:
+            width = min(self.width_virtual, self.screen_width)
+            (x, _) = self.scroll_start()
+            start = self.px_to_us(x)
+            end = self.px_to_us(x + width)
+            self.update_rectangles(start, end)
+
+            # Draw CPU labels at the left edge of the visible area
+            (x_scroll, _) = self.scroll_start()
+            for nr in range(self.nr_rects):
+                offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))
+                dc.DrawText(f"CPU {nr}", x_scroll + 10, offset_py + 10)
+        finally:
+            self.dc = None
+
+    def rect_from_ypixel(self, y):
+        y -= RootFrame.Y_OFFSET
+        rect = y // (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)
+        height = y % (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)
+
+        if rect < 0 or rect > self.nr_rects - 1 or height > RootFrame.RECT_HEIGHT:
+            return -1
+
+        return rect
+
+    def update_summary(self, txt):
+        self.txt.SetValue(txt)
+        self.text_panel.Layout()
+        self.splitter.Layout()
+        self.text_panel.Refresh()
+
+    def on_mouse_down(self, event):
+        pos = event.GetPosition()
+        x, y = pos.x, pos.y
+        rect = self.rect_from_ypixel(y)
+        if rect == -1:
+            return
+
+        t = self.px_to_us(x) + self.ts_start
+
+        self.sched_tracer.mouse_down(rect, t)
+
+    def update_width_virtual(self):
+        self.width_virtual = self.us_to_px(self.ts_end - self.ts_start)
+
+    def __zoom(self, x):
+        self.update_width_virtual()
+        (xpos, ypos) = self.scroll.GetViewStart()
+        xpos = int(self.us_to_px(x) // self.scroll_scale)
+        self.scroll_panel.SetSize((int(self.width_virtual), int(self.height_virtual)))
+        self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale,
+                                  int(self.width_virtual // self.scroll_scale),
+                                  int(self.height_virtual // self.scroll_scale),
+                                  xpos, ypos)
+        self.Refresh()
+
+    def zoom_in(self):
+        x = self.scroll_start_us()
+        self.zoom *= 2
+        self.__zoom(x)
+
+    def zoom_out(self):
+        x = self.scroll_start_us()
+        self.zoom /= 2
+        self.__zoom(x)
+
+    def on_key_press(self, event):
+        key = event.GetRawKeyCode()
+        if key == ord("+"):
+            self.zoom_in()
+            return
+        if key == ord("-"):
+            self.zoom_out()
+            return
+
+        key = event.GetKeyCode()
+        (x, y) = self.scroll.GetViewStart()
+        if key == wx.WXK_RIGHT:
+            self.scroll.Scroll(x + 1, y)
+        elif key == wx.WXK_LEFT:
+            self.scroll.Scroll(x - 1, y)
+        elif key == wx.WXK_DOWN:
+            self.scroll.Scroll(x, y + 1)
+        elif key == wx.WXK_UP:
+            self.scroll.Scroll(x, y - 1)
tools/perf/python/sched-migration.py new file +496 -0

496 lines not on this page · raw message

tools/perf/tests/shell/test_sched_migration_python.sh new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog