From 47d9a8eaa062c58f0689d625b80031a4c7edc686 Mon Sep 17 00:00:00 2001 From: Likhitha Korrapati Date: Fri, 15 Sep 2023 03:34:50 -0500 Subject: [PATCH] tools/perf/scripts: Fix pylint warnings on mem_phys_addr.py Patch fixes pylint warnings bad indentation, unused import, snake_case naming convention, missing doc_string, string formating. Signed-off-by: Likhitha Korrapati --- tools/perf/scripts/python/mem-phys-addr.py | 100 ----------------- tools/perf/scripts/python/mem_phys_addr.py | 121 +++++++++++++++++++++ 2 files changed, 121 insertions(+), 100 deletions(-) delete mode 100644 tools/perf/scripts/python/mem-phys-addr.py create mode 100644 tools/perf/scripts/python/mem_phys_addr.py diff --git a/tools/perf/scripts/python/mem-phys-addr.py b/tools/perf/scripts/python/mem-phys-addr.py deleted file mode 100644 index 1f332e72b9b0f3..00000000000000 --- a/tools/perf/scripts/python/mem-phys-addr.py +++ /dev/null @@ -1,100 +0,0 @@ -# mem-phys-addr.py: Resolve physical address samples -# SPDX-License-Identifier: GPL-2.0 -# -# Copyright (c) 2018, Intel Corporation. - -from __future__ import division -from __future__ import print_function - -import os -import sys -import struct -import re -import bisect -import collections - -sys.path.append(os.environ['PERF_EXEC_PATH'] + \ - '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') - -#physical address ranges for System RAM -system_ram = [] -#physical address ranges for Persistent Memory -pmem = [] -#file object for proc iomem -f = None -#Count for each type of memory -load_mem_type_cnt = collections.Counter() -#perf event name -event_name = None - -def parse_iomem(): - global f - f = open('/proc/iomem', 'r') - for i, j in enumerate(f): - m = re.split('-|:',j,2) - if m[2].strip() == 'System RAM': - system_ram.append(int(m[0], 16)) - system_ram.append(int(m[1], 16)) - if m[2].strip() == 'Persistent Memory': - pmem.append(int(m[0], 16)) - pmem.append(int(m[1], 16)) - -def print_memory_type(): - print("Event: %s" % (event_name)) - print("%-40s %10s %10s\n" % ("Memory type", "count", "percentage"), end='') - print("%-40s %10s %10s\n" % ("----------------------------------------", - "-----------", "-----------"), - end=''); - total = sum(load_mem_type_cnt.values()) - for mem_type, count in sorted(load_mem_type_cnt.most_common(), \ - key = lambda kv: (kv[1], kv[0]), reverse = True): - print("%-40s %10d %10.1f%%\n" % - (mem_type, count, 100 * count / total), - end='') - -def trace_begin(): - parse_iomem() - -def trace_end(): - print_memory_type() - f.close() - -def is_system_ram(phys_addr): - #/proc/iomem is sorted - position = bisect.bisect(system_ram, phys_addr) - if position % 2 == 0: - return False - return True - -def is_persistent_mem(phys_addr): - position = bisect.bisect(pmem, phys_addr) - if position % 2 == 0: - return False - return True - -def find_memory_type(phys_addr): - if phys_addr == 0: - return "N/A" - if is_system_ram(phys_addr): - return "System RAM" - - if is_persistent_mem(phys_addr): - return "Persistent Memory" - - #slow path, search all - f.seek(0, 0) - for j in f: - m = re.split('-|:',j,2) - if int(m[0], 16) <= phys_addr <= int(m[1], 16): - return m[2] - return "N/A" - -def process_event(param_dict): - name = param_dict["ev_name"] - sample = param_dict["sample"] - phys_addr = sample["phys_addr"] - - global event_name - if event_name == None: - event_name = name - load_mem_type_cnt[find_memory_type(phys_addr)] += 1 diff --git a/tools/perf/scripts/python/mem_phys_addr.py b/tools/perf/scripts/python/mem_phys_addr.py new file mode 100644 index 00000000000000..1760358c079030 --- /dev/null +++ b/tools/perf/scripts/python/mem_phys_addr.py @@ -0,0 +1,121 @@ +""" +mem-phys-addr.py: Resolve physical address samples +""" +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2018, Intel Corporation. + +from __future__ import division +from __future__ import print_function + +import os +import sys +import re +import bisect +import collections + +sys.path.append(os.environ['PERF_EXEC_PATH'] + \ + '/scripts/python/Perf-Trace-Util/lib/Perf/Trace') + +#physical address ranges for System RAM +system_ram = [] +#physical address ranges for Persistent Memory +pmem = [] +#file object for proc iomem +F = None +#Count for each type of memory +load_mem_type_cnt = collections.Counter() +#perf event name +EVENT_NAME = None + +def parse_iomem(): + """ + Parse the /proc/iomem file to extract information about System RAM and Persistent Memory. + """ + global F + with open('/proc/iomem', 'r', encoding='utf-8') as F: + for j in enumerate(F): + mem = re.split('-|:',j,2) + if mem[2].strip() == 'System RAM': + system_ram.append(int(mem[0], 16)) + system_ram.append(int(mem[1], 16)) + if mem[2].strip() == 'Persistent Memory': + pmem.append(int(mem[0], 16)) + pmem.append(int(mem[1], 16)) + +def print_memory_type(): + """ + Print summary of memory types, counts, and percentages. + """ + print(f"Event: {EVENT_NAME}") + print(f"{'Memory type':<40} {'count':>10} {'percentage':>10}\n", end='') + print(f"{'-' * 40:<40} {'-' * 10:>10} {'-' * 10:>10}\n", end='') + total = sum(load_mem_type_cnt.values()) + for mem_type, count in sorted(load_mem_type_cnt.most_common(), \ + key = lambda kv: (kv[1], kv[0]), reverse = True): + print(f"{mem_type:<40} {count:>10} {100 * count / total:>10.1f}%") + +def trace_begin(): + """ + Begin tracing process by parsing /proc/iomem. + """ + parse_iomem() + +def trace_end(): + """ + End tracing process and print memory type summary. + """ + print_memory_type() + F.close() + +def is_system_ram(phys_addr): + """ + Check if a physical address belongs to System RAM. + """ + #/proc/iomem is sorted + position = bisect.bisect(system_ram, phys_addr) + if position % 2 == 0: + return False + return True + +def is_persistent_mem(phys_addr): + """ + Check if a physical address belongs to Persistent Memory. + """ + position = bisect.bisect(pmem, phys_addr) + if position % 2 == 0: + return False + return True + +def find_memory_type(phys_addr): + """ + Find the memory type of a physical address. + """ + if phys_addr == 0: + return "N/A" + if is_system_ram(phys_addr): + return "System RAM" + + if is_persistent_mem(phys_addr): + return "Persistent Memory" + + #slow path, search all + F.seek(0, 0) + for j in F: + mem = re.split('-|:',j,2) + if int(mem[0], 16) <= phys_addr <= int(mem[1], 16): + return mem[2] + return "N/A" + +def process_event(param_dict): + """ + Process an event and update memory type count. + """ + name = param_dict["ev_name"] + sample = param_dict["sample"] + phys_addr = sample["phys_addr"] + + global EVENT_NAME + if EVENT_NAME is None: + EVENT_NAME = name + load_mem_type_cnt[find_memory_type(phys_addr)] += 1