-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProcMapsSizeDump.py
61 lines (48 loc) · 1.57 KB
/
ProcMapsSizeDump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
###################################################
#
# Scans /proc/id/maps file and adds two columns
# - Size in bytes
# - Size in human readable form (B, KB, MB, GB)
# Then passes it to output
#
# Usage: python ProcMapsSizeDump.py < /proc/id/maps
#
###################################################
# Function converting large number to human-readable
# 1024 -> 1.00Kb
# 1024 * 1024 -> 1.00Mb
# etc
def human_format(number):
formats = ['b', 'Kb', 'Mb', 'Gb']
exponent = 0
while(number >= 1024):
exponent += 1;
number /= 1024
return "%.2f%s" % (number, formats[exponent])
# Iterate over standard input until we got EOF, then break
while(True):
try:
map_line = raw_input()
except EOFError:
break
# Skip empty lines
if(map_line == ''):
continue
# Split line by spaces
map_line_entries = map_line.split()
# First token contains memory range aaaaaaa-bbbbbbbbb, extract a (start), b (end)
map_line_size_entries = map_line_entries[0].split("-")
# Convert Hex to Dec
map_line_size_start = int(map_line_size_entries[0], 16)
map_line_size_end = int(map_line_size_entries[1], 16)
# Calculate size
map_line_size = map_line_size_end - map_line_size_start
# Make it human readable
map_line_size_str = human_format(map_line_size)
# Insert results to original maps entry
map_line_entries.insert(1, str(map_line_size).rjust(16, ' '))
map_line_entries.insert(2, map_line_size_str.rjust(16, ' '))
# Make it string again
map_line = " ".join(map_line_entries)
# Out
print(map_line)