Back to home page

OSCL-LXR

 
 

    


0001 # mem-phys-addr.py: Resolve physical address samples
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Copyright (c) 2018, Intel Corporation.
0005 
0006 from __future__ import division
0007 from __future__ import print_function
0008 
0009 import os
0010 import sys
0011 import struct
0012 import re
0013 import bisect
0014 import collections
0015 
0016 sys.path.append(os.environ['PERF_EXEC_PATH'] + \
0017     '/scripts/python/Perf-Trace-Util/lib/Perf/Trace')
0018 
0019 #physical address ranges for System RAM
0020 system_ram = []
0021 #physical address ranges for Persistent Memory
0022 pmem = []
0023 #file object for proc iomem
0024 f = None
0025 #Count for each type of memory
0026 load_mem_type_cnt = collections.Counter()
0027 #perf event name
0028 event_name = None
0029 
0030 def parse_iomem():
0031     global f
0032     f = open('/proc/iomem', 'r')
0033     for i, j in enumerate(f):
0034         m = re.split('-|:',j,2)
0035         if m[2].strip() == 'System RAM':
0036             system_ram.append(int(m[0], 16))
0037             system_ram.append(int(m[1], 16))
0038         if m[2].strip() == 'Persistent Memory':
0039             pmem.append(int(m[0], 16))
0040             pmem.append(int(m[1], 16))
0041 
0042 def print_memory_type():
0043     print("Event: %s" % (event_name))
0044     print("%-40s  %10s  %10s\n" % ("Memory type", "count", "percentage"), end='')
0045     print("%-40s  %10s  %10s\n" % ("----------------------------------------",
0046                     "-----------", "-----------"),
0047                     end='');
0048     total = sum(load_mem_type_cnt.values())
0049     for mem_type, count in sorted(load_mem_type_cnt.most_common(), \
0050                     key = lambda kv: (kv[1], kv[0]), reverse = True):
0051         print("%-40s  %10d  %10.1f%%\n" %
0052             (mem_type, count, 100 * count / total),
0053             end='')
0054 
0055 def trace_begin():
0056     parse_iomem()
0057 
0058 def trace_end():
0059     print_memory_type()
0060     f.close()
0061 
0062 def is_system_ram(phys_addr):
0063     #/proc/iomem is sorted
0064     position = bisect.bisect(system_ram, phys_addr)
0065     if position % 2 == 0:
0066         return False
0067     return True
0068 
0069 def is_persistent_mem(phys_addr):
0070     position = bisect.bisect(pmem, phys_addr)
0071     if position % 2 == 0:
0072         return False
0073     return True
0074 
0075 def find_memory_type(phys_addr):
0076     if phys_addr == 0:
0077         return "N/A"
0078     if is_system_ram(phys_addr):
0079         return "System RAM"
0080 
0081     if is_persistent_mem(phys_addr):
0082         return "Persistent Memory"
0083 
0084     #slow path, search all
0085     f.seek(0, 0)
0086     for j in f:
0087         m = re.split('-|:',j,2)
0088         if int(m[0], 16) <= phys_addr <= int(m[1], 16):
0089             return m[2]
0090     return "N/A"
0091 
0092 def process_event(param_dict):
0093     name       = param_dict["ev_name"]
0094     sample     = param_dict["sample"]
0095     phys_addr  = sample["phys_addr"]
0096 
0097     global event_name
0098     if event_name == None:
0099         event_name = name
0100     load_mem_type_cnt[find_memory_type(phys_addr)] += 1