-
-
Save zDEFz/624cccf3b5aeb51fdb1ffb2a2ab41e42 to your computer and use it in GitHub Desktop.
Script to output /proc/iomem size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
SUFFIXES = ('', 'KiB', 'MiB', 'GiB', 'TiB') | |
def decode(filename, human=False): | |
with open(filename, 'r') as infile: | |
for line in infile: | |
line = line[:-1] | |
newline = line.strip() | |
lpad = line[:len(line)-len(newline)] | |
num, rest = newline.split(maxsplit=1) | |
start, end = num.split(sep='-') | |
start = int(start, base=16) | |
end = int(end, base=16) | |
size = end - start + 1 | |
suffix = '' | |
if human: | |
for suffix in SUFFIXES: | |
if size // 1024 > 0: | |
size //= 1024 | |
else: | |
break | |
print("{}{} {}{} {}".format(lpad, hex(start), size, suffix, rest)) | |
if __name__ == '__main__': | |
human = False | |
if len(sys.argv) > 1: | |
if len(sys.argv) > 2 and sys.argv[1] == '-h': | |
human = True | |
filename = sys.argv[2] | |
else: | |
filename = sys.argv[1] | |
decode(filename, human) | |
else: | |
print("USAGE: {} [-h] <filename>".format(sys.argv[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment