show_maps_range.py
import os
import re
import sys
def get_map_range(map_range):
map_ranges = map_range.split("-")
range_from = int(map_ranges[0], 16)
range_to = int(map_ranges[1], 16)
return (range_from, range_to)
def show_threads_stack_info(pid, arg_range_from, arg_range_to, verbose=False):
proc_map_file_name = "/proc/{}/maps".format(pid)
proc_map_file = open(proc_map_file_name, 'r')
map_ranges = []
while True:
line = proc_map_file.readline()
if not line:
break
map_range = line.split()[0]
(range_from, range_to) = get_map_range(map_range)
map_ranges.append((map_range, range_from, range_to))
totalSizeKB = 0
totalPssKB = 0
for r in map_ranges:
(map_range, range_from, range_to) = r
if range_from>=arg_range_from and range_from<arg_range_to:
cmd = "grep -A5 {} /proc/{}/smaps".format(map_range, pid)
res = os.popen(cmd).read().split("\n")
SizeKB = int(re.split(r"\s+", res[1])[1])
PssKB = int(re.split(r"\s+", res[5])[1])
if verbose:
print(res[0])
print(res[1])
print(res[5])
totalSizeKB += SizeKB
totalPssKB += PssKB
print()
print("totalSize: {} KB, totalPss: {} KB".format(totalSizeKB, totalPssKB) )
def usage():
print("show_map_range.py <pid> <from> <to>")
if __name__ == "__main__":
print(sys.argv)
if len(sys.argv)!=4:
usage()
else:
show_threads_stack_info(sys.argv[1], int(sys.argv[2],16),int(sys.argv[3],16), True)
网友评论