Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-humanize and remove root verification #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions ps_mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def parse_options():
'help',
'total',
'discriminate-by-pid',
'swap'
'swap',
'no-humanize'
]
opts, args = getopt.getopt(sys.argv[1:], "shtdSp:w:", long_options)
except getopt.GetoptError:
Expand All @@ -157,6 +158,7 @@ def parse_options():
show_swap = False
watch = None
only_total = False
humanize = True

for o, a in opts:
if o in ('-s', '--split-args'):
Expand All @@ -182,17 +184,19 @@ def parse_options():
except:
sys.stderr.write(help())
sys.exit(3)
if o in ('--no-humanize'):
humanize = False

return (
split_args,
pids_to_show,
watch,
only_total,
discriminate_by_pid,
show_swap
show_swap,
humanize
)


def help():
help_msg = 'Usage: ps_mem [OPTION]...\n' \
'Show program core memory usage\n' \
Expand Down Expand Up @@ -505,24 +509,27 @@ def print_header(show_swap, discriminate_by_pid):


def print_memory_usage(sorted_cmds, shareds, count, total, swaps, total_swap,
shared_swaps, total_shared_swap, show_swap):
shared_swaps, total_shared_swap, show_swap, humanize):
for cmd in sorted_cmds:

output_string = "%9s + %9s = %9s"
output_data = (human(cmd[1]-shareds[cmd[0]]),
human(shareds[cmd[0]]), human(cmd[1]))
output_data = (cmd[1]-shareds[cmd[0]], shareds[cmd[0]], cmd[1])

if show_swap:
if have_swap_pss:
output_string += "\t%9s"
output_data += (human(shared_swaps[cmd[0]]),)
output_data += (shared_swaps[cmd[0]],)
output_string += " %9s"
output_data += (human(swaps[cmd[0]]),)
output_string += "\t%s\n"
output_data += (swaps[cmd[0]],)
if humanize:
output_data = tuple(human(ele) for ele in output_data)
output_string += "\t%s\n"
else:
output_string = ",".join("%s" for i in range(len(output_data))) + ",%s\n"
output_data += (cmd_with_count(cmd[0], count[cmd[0]]),)

sys.stdout.write(output_string % output_data)

if have_pss:
if have_pss and humanize:
if show_swap:
if have_swap_pss:
sys.stdout.write("%s\n%s%9s%s%9s%s%9s\n%s\n" %
Expand All @@ -537,13 +544,7 @@ def print_memory_usage(sorted_cmds, shareds, count, total, swaps, total_swap,
sys.stdout.write("%s\n%s%9s\n%s\n" %
("-" * 33, " " * 24, human(total), "=" * 33))


def verify_environment():
if os.geteuid() != 0:
sys.stderr.write("Sorry, root permission required.\n")
sys.stderr.close()
sys.exit(1)

try:
kernel_ver()
except (IOError, OSError):
Expand All @@ -558,13 +559,12 @@ def verify_environment():

def main():
split_args, pids_to_show, watch, only_total, discriminate_by_pid, \
show_swap = parse_options()
show_swap, humanize = parse_options()

verify_environment()

if not only_total:
if not only_total and humanize:
print_header(show_swap, discriminate_by_pid)

if watch is not None:
try:
sorted_cmds = True
Expand All @@ -578,8 +578,7 @@ def main():
elif not only_total:
print_memory_usage(sorted_cmds, shareds, count, total,
swaps, total_swap, shared_swaps,
total_shared_swap, show_swap)

total_shared_swap, show_swap, humanize)
sys.stdout.flush()
time.sleep(watch)
else:
Expand All @@ -596,7 +595,7 @@ def main():
elif not only_total:
print_memory_usage(sorted_cmds, shareds, count, total, swaps,
total_swap, shared_swaps, total_shared_swap,
show_swap)
show_swap, humanize)

# We must close explicitly, so that any EPIPE exception
# is handled by our excepthook, rather than the default
Expand All @@ -606,4 +605,5 @@ def main():
vm_accuracy = shared_val_accuracy()
show_shared_val_accuracy( vm_accuracy, only_total )

if __name__ == '__main__': main()
if __name__ == '__main__':
main()