1 # mem-phys-addr.py: Resolve physical address samples
2 # SPDX-License-Identifier: GPL-2.0
4 # Copyright (c) 2018, Intel Corporation.
11 from dataclasses import dataclass
12 from typing import (Dict, Optional)
14 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
15 '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
17 @dataclass(frozen=True)
19 """Read from a line in /proc/iomem"""
25 # Physical memory layout from /proc/iomem. Key is the indent and then
27 iomem: Dict[int, list[IomemEntry]] = collections.defaultdict(list)
28 # Child nodes from the iomem parent.
29 children: Dict[IomemEntry, set[IomemEntry]] = collections.defaultdict(set)
30 # Maximum indent seen before an entry in the iomem file.
32 # Count for each range of memory.
33 load_mem_type_cnt: Dict[IomemEntry, int] = collections.Counter()
34 # Perf event name set from the first sample in the data.
35 event_name: Optional[str] = None
38 """Populate iomem from /proc/iomem file"""
42 with open('/proc/iomem', 'r', encoding='ascii') as f:
45 while line[indent] == ' ':
47 if indent > max_indent:
49 m = re.split('-|:', line, 2)
53 entry = IomemEntry(begin, end, indent, label)
54 # Before adding entry, search for a parent node using its begin.
56 parent = find_memory_type(begin)
57 assert parent, f"Given indent expected a parent for {label}"
58 children[parent].add(entry)
59 iomem[indent].append(entry)
61 def find_memory_type(phys_addr) -> Optional[IomemEntry]:
62 """Search iomem for the range containing phys_addr with the maximum indent"""
63 for i in range(max_indent, -1, -1):
66 position = bisect.bisect_right(iomem[i], phys_addr,
67 key=lambda entry: entry.begin)
70 iomem_entry = iomem[i][position-1]
71 if iomem_entry.begin <= phys_addr <= iomem_entry.end:
73 print(f"Didn't find {phys_addr}")
76 def print_memory_type():
77 print(f"Event: {event_name}")
78 print(f"{'Memory type':<40} {'count':>10} {'percentage':>10}")
79 print(f"{'-' * 40:<40} {'-' * 10:>10} {'-' * 10:>10}")
80 total = sum(load_mem_type_cnt.values())
81 # Add count from children into the parent.
82 for i in range(max_indent, -1, -1):
85 for entry in iomem[i]:
87 for child in children[entry]:
88 if load_mem_type_cnt[child] > 0:
89 load_mem_type_cnt[entry] += load_mem_type_cnt[child]
91 def print_entries(entries):
92 """Print counts from parents down to their children"""
94 for entry in sorted(entries,
95 key = lambda entry: load_mem_type_cnt[entry],
97 count = load_mem_type_cnt[entry]
99 mem_type = ' ' * entry.indent + f"{entry.begin:x}-{entry.end:x} : {entry.label}"
100 percent = 100 * count / total
101 print(f"{mem_type:<40} {count:>10} {percent:>10.1f}")
102 print_entries(children[entry])
104 print_entries(iomem[0])
112 def process_event(param_dict):
113 if "sample" not in param_dict:
116 sample = param_dict["sample"]
117 if "phys_addr" not in sample:
120 phys_addr = sample["phys_addr"]
121 entry = find_memory_type(phys_addr)
123 load_mem_type_cnt[entry] += 1
126 if event_name is None:
127 event_name = param_dict["ev_name"]