From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DCFC4C81 for ; Mon, 20 Apr 2026 00:06:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776643608; cv=none; b=i9OGfVsJ/DmLzddJz3dFTCQMh3DN9ih7jVahuvRtSh+3t7u4EKwfGOhKCppiDdRd7mHzGA+VTo/BCWowZa1TJe18CZp63KNEn1EbXaMmjvUgb4JOOqeyKMFe175pQy+v8HMVphv4jldZv5yPuNA0MkNBvaUBEWXtGw519scOYg8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776643608; c=relaxed/simple; bh=Yp/VpyJcqD2RPLWZalVa+xixDAFI4lAEMRa/FZFIMb0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KHXN2bFoi+ZRwfOY0adaShSgNB88kYNCLi7cFhoCt74wLSKiNJqsL7XUO4z4I9b/Q7btvDCF5PF5SSIuJQfoIXyqDBNcImHt37DEaSNBn/5/8gwH6AC9gb15FwHqLZ6k/Ugw/0tKeMEvNfaYrUOJnqCn2/pULN5F+JPWxbbbwPM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hUBwVltQ; arc=none smtp.client-ip=209.85.161.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hUBwVltQ" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-694867ca247so217014eaf.1 for ; Sun, 19 Apr 2026 17:06:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776643606; x=1777248406; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7WQ7kcwMxBpCOrBoZ47jd2yOsX0OA41N9Sof9Sy1W94=; b=hUBwVltQlhxPKwZ2MIT/G2lO68kvrTYgH3MV43gsVnumCgyxcwec7ypIEWV1gB9+cy NLk1J1mLmVFLd3EuVEMu8LOA9v7KSD3f6EPJAi6AQyHudNR+k5REb0lCfvZpzqTBsV5j 3nJyRAX6gbiYOx/DKaGOE+5LBcQZ9UPQ7AdaInOGa4msk3GrGRctZZMSSDauyK7OBPjT kjZMg2NTQG7DcdgQSpnvYCxBXBMXW1tIlShWn310ph1JVVRO0kLEAuTLRMon6wD+Yele 3PPkMzCmDWGQ/JLTGMnpWE4cBMO4QFYYEJyd6SLDUlRBLJpExU0/aqESU+iD2Xdhx86Y yEog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776643606; x=1777248406; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7WQ7kcwMxBpCOrBoZ47jd2yOsX0OA41N9Sof9Sy1W94=; b=ZsJBfueqduBa3Lmbwb1PvhXxuwf/Etw/bOA62bPeFA8E9vIJTylUHQ0Ad9cyA1OFzZ gnJbPgeuiZU7/3x7ZE3nXaEa2OKJ2KOiLDdeERy8vOl6iedqBkneLrvW1xYdgMwkO3I4 PLoCkXzEsz3kb1Odfk19GpY2X3+w8ipHrrVHgeH5CTDqVtLugiWYXvO0pGy3QNfWfLm3 M7Au4JMaZjwQCRbv3ZQ+rjLFmlrC6l4WtBl7s7xEzktYAh7/wm7KsjyrgMPkTp2DYLWo iX9E3G+TJ8+6A4OMOFMeptxQ3QHQU8q2FeC6a3CHIOUmdN96Ed0kdqP5tvmvmpGqzDJ0 /nwQ== X-Forwarded-Encrypted: i=1; AFNElJ+4/45kd66QGVipSMJsNLE71psCw0nbr7FZPmBvyndQLc2AR7X1uYi5JgT9UrW4CCWyfuw/7+01BfeJeS8=@vger.kernel.org X-Gm-Message-State: AOJu0YxrpO+DX9T/uX9bdd2RWZlAP5NdWwft9qqfAPBVqz+PE8SosFes fHUSfmSS57J0AYK8IRx74XNbQkj3MPNbvO/1lHR444oPppi+I6fdlSI4DodGiPVImrcBj/JuKGc jYTf+KJEV7A== X-Received: from dlag4.prod.google.com ([2002:a05:701b:2504:b0:12b:e83a:8d31]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:7f13:b0:12c:873f:eba3 with SMTP id a92af1059eb24-12c873fef0fmr651782c88.8.1776643220753; Sun, 19 Apr 2026 17:00:20 -0700 (PDT) Date: Sun, 19 Apr 2026 16:58:38 -0700 In-Reply-To: <20260419235911.2186050-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260419235911.2186050-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.rc1.513.gad8abe7a5a-goog Message-ID: <20260419235911.2186050-27-irogers@google.com> Subject: [PATCH v1 26/58] perf mem-phys-addr: Port mem-phys-addr to use python module From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Adrian Hunter , James Clark , Alice Rogers , Suzuki K Poulose , Mike Leach , John Garry , Leo Yan , Yicong Yang , Jonathan Cameron , Nick Terrell , David Sterba , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Alexandre Chartre , Dmitrii Dolgov <9erthalion6@gmail.com>, Yuzhuo Jing , Blake Jones , Changbin Du , Gautam Menghani , Wangyang Guo , Pan Deng , Zhiguo Zhou , Tianyou Li , Thomas Falcon , Athira Rajeev , Collin Funk , Dapeng Mi , Ravi Bangoria , Zecheng Li , tanze , Thomas Richter , Ankur Arora , "Tycho Andersen (AMD)" , Howard Chu , Sun Jian , Derek Foreman , Swapnil Sapkal , Anubhav Shelat , Ricky Ringler , Qinxin Xia , Aditya Bodkhe , Chun-Tse Shao , Stephen Brennan , Yang Li , Chuck Lever , Chen Ni , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org Cc: Ian Rogers Content-Type: text/plain; charset="UTF-8" Give an example of using the perf python session API to load a perf.data file and perform the behavior of tools/perf/scripts/python/mem-phys-addr.py. Signed-off-by: Ian Rogers --- tools/perf/python/mem-phys-addr.py | 116 +++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 tools/perf/python/mem-phys-addr.py diff --git a/tools/perf/python/mem-phys-addr.py b/tools/perf/python/mem-phys-addr.py new file mode 100755 index 000000000000..17d6fd4f1a2b --- /dev/null +++ b/tools/perf/python/mem-phys-addr.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 +"""mem-phys-addr.py: Resolve physical address samples""" + +import bisect +import collections +import os +import perf +import re +import sys +from dataclasses import dataclass +from typing import (Dict, Optional) + +@dataclass(frozen=True) +class IomemEntry: + """Read from a line in /proc/iomem""" + begin: int + end: int + indent: int + label: str + +# 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, set[IomemEntry]] = collections.defaultdict(set) +# Maximum indent seen before an entry in the iomem file. +max_indent: int = 0 +# Count for each range of memory. +load_mem_type_cnt: Dict[IomemEntry, int] = collections.Counter() +# Perf event name set from the first sample in the data. +event_name: Optional[str] = None + +def parse_iomem(): + """Populate iomem from /proc/iomem file""" + global iomem + global max_indent + global children + with open('/proc/iomem', 'r', encoding='ascii') as f: + for line in f: + indent = 0 + while line[indent] == ' ': + indent += 1 + if indent > max_indent: + max_indent = indent + m = re.split('-|:', line, maxsplit=2) + begin = int(m[0], 16) + end = int(m[1], 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].add(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(max_indent, -1, -1): + if i not in iomem: + continue + position = bisect.bisect_right(iomem[i], phys_addr, + key=lambda entry: entry.begin) + if position is None: + continue + iomem_entry = iomem[i][position-1] + if iomem_entry.begin <= phys_addr <= iomem_entry.end: + return iomem_entry + print(f"Didn't find {phys_addr}") + return None + +def print_memory_type(): + 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()) + # Add count from children into the parent. + for i in range(max_indent, -1, -1): + if i not in iomem: + continue + for entry in iomem[i]: + global children + for child in children[entry]: + if load_mem_type_cnt[child] > 0: + load_mem_type_cnt[entry] += load_mem_type_cnt[child] + + def print_entries(entries): + """Print counts from parents down to their children""" + global children + for entry in sorted(entries, + key = lambda entry: load_mem_type_cnt[entry], + reverse = True): + 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]) + + print_entries(iomem[0]) + +if __name__ == "__main__": + def process_event(sample): + phys_addr = sample.sample_phys_addr + entry = find_memory_type(phys_addr) + if entry: + load_mem_type_cnt[entry] += 1 + + global event_name + if event_name is None: + event_name = str(sample.evsel) + + parse_iomem() + perf.session(perf.data("perf.data"), sample=process_event).process_events() + print_memory_type() -- 2.54.0.rc1.513.gad8abe7a5a-goog