Created
June 10, 2020 14:25
-
-
Save tonetheman/9c2299c4762582c3ff1899055c07075f to your computer and use it in GitHub Desktop.
Show swap for linux processes
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
import os,glob,string,time,sys | |
all_the_things = [] | |
class P: | |
def __init__(self,pid,name,swap): | |
self.pid = pid | |
self.name = name | |
self.swap = swap | |
def __repr__(self): | |
return str(self.pid) + " " + str(self.name) + " " + str(self.swap) | |
print time.ctime(),"this will take some time..." | |
count = 0 | |
for filename in glob.glob("/proc/*/status"): | |
count = count + 1 | |
if count%10==0: | |
print ">", | |
sys.stdout.flush() | |
pid = 0 | |
try: | |
pid = int(filename.split("/")[2]) | |
except: | |
continue | |
cmd = "grep \"Name:\" {0} 2>/dev/null".format(filename) | |
namedata = os.popen(cmd).read().strip() | |
# print("\t",namedata) | |
namedata = namedata.split("\t") | |
namedata = map(string.strip,namedata) | |
if len(namedata)==1: | |
continue | |
process_name = namedata[1] | |
cmd = "grep \"VmSwap:\" {0} 2>/dev/null".format(filename) | |
swapdata = os.popen(cmd).read().strip() | |
swapdata = swapdata.split("\t") | |
swapdata = map(string.strip,swapdata) | |
if len(swapdata)==1: | |
continue | |
# int value for the amount of kswap | |
swap_in_k = int(swapdata[1].split(" ")[0]) | |
# print(pid,process_name,swap_in_k) | |
all_the_things.append(P(pid,process_name,swap_in_k)) | |
def SortFunc(p): | |
return p.swap | |
print("all done sorted by swapspace in K bytes") | |
all_the_things.sort(key=SortFunc) | |
for p in all_the_things: | |
print(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment