Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created June 10, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonetheman/9c2299c4762582c3ff1899055c07075f to your computer and use it in GitHub Desktop.
Save tonetheman/9c2299c4762582c3ff1899055c07075f to your computer and use it in GitHub Desktop.
Show swap for linux processes
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