Justo que iba a comprar un libro, zaasss
Link to database cannot be established: SQLSTATE[28000] [1045] Access denied for user 'utibrq9a_admin2'@'localhost' (using password: YES)
Middleware WEB
martes, 5 de agosto de 2014
jueves, 23 de agosto de 2012
SABER CONSUMO DE MEMORIA DE PROCESOS UNIX
Cuantas veces tenéis la tarea de saber cuanto consume de memoria el apache o tomcat u otro aplicativo. Pues bien a continuación mostraré lo forma de sacarlo de un forma fácil e intuitiva.
A continuación voy a mostrar dos formas de hacerlo, uno mediante comando y de forma fácil y otro utilizando Phyton.
FORMA COMANDO
#ps -ylC httpd --sort:rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 8001 9983 9924 0 76 0 5812 9577 futex_ ? 00:03:40 httpd
S 8001 9994 9924 0 75 0 6440 14660 fcntl_ ? 00:00:00 httpd
S 8001 10001 9924 0 75 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10004 9924 0 76 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10010 9924 0 76 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10013 9924 0 75 0 6444 14660 fcntl_ ? 00:00:00 httpd
[...]
Lo que esta en negrita es rss (Resident Set Size) y lo muestra en KB, por lo que hay que dividirlo entre 1024 si lo quieres en Mb.
9577 Kb / 1024 = 9.35 Mb
FROMA SCRIPT, lo guardas en un fichero y ejecutas python fichero.py
###############################
#!/usr/bin/env python
import sys, os, string
if os.geteuid() != 0:
sys.stderr.write("Sorry, root permission required.\n");
sys.exit(1)
PAGESIZE=os.sysconf("SC_PAGE_SIZE")/1024 #KiB
our_pid=os.getpid()
#(major,minor,release)
def kernel_ver():
kv=open("/proc/sys/kernel/osrelease").readline().split(".")[:3]
for char in "-_":
kv[2]=kv[2].split(char)[0]
return (int(kv[0]), int(kv[1]), int(kv[2]))
kv=kernel_ver()
def getShared(pid):
if os.path.exists("/proc/"+str(pid)+"/smaps"):
shared_lines=[line
for line in open("/proc/"+str(pid)+"/smaps").readlines()
if line.find("Shared")!=-1]
return sum([int(line.split()[1]) for line in shared_lines])
elif (2,6,1) <= kv <= (2,6,9):
return 0 #lots of overestimation, but what can we do?
else:
return int(open("/proc/"+str(pid)+"/statm").readline().split()[2])*PAGESIZE
Espero que os sea útil. Saludos
A continuación voy a mostrar dos formas de hacerlo, uno mediante comando y de forma fácil y otro utilizando Phyton.
FORMA COMANDO
#ps -ylC httpd --sort:rss
S UID PID PPID C PRI NI RSS SZ WCHAN TTY TIME CMD
S 8001 9983 9924 0 76 0 5812 9577 futex_ ? 00:03:40 httpd
S 8001 9994 9924 0 75 0 6440 14660 fcntl_ ? 00:00:00 httpd
S 8001 10001 9924 0 75 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10004 9924 0 76 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10010 9924 0 76 0 6444 14660 fcntl_ ? 00:00:00 httpd
S 8001 10013 9924 0 75 0 6444 14660 fcntl_ ? 00:00:00 httpd
[...]
Lo que esta en negrita es rss (Resident Set Size) y lo muestra en KB, por lo que hay que dividirlo entre 1024 si lo quieres en Mb.
9577 Kb / 1024 = 9.35 Mb
FROMA SCRIPT, lo guardas en un fichero y ejecutas python fichero.py
###############################
#!/usr/bin/env python
import sys, os, string
if os.geteuid() != 0:
sys.stderr.write("Sorry, root permission required.\n");
sys.exit(1)
PAGESIZE=os.sysconf("SC_PAGE_SIZE")/1024 #KiB
our_pid=os.getpid()
#(major,minor,release)
def kernel_ver():
kv=open("/proc/sys/kernel/osrelease").readline().split(".")[:3]
for char in "-_":
kv[2]=kv[2].split(char)[0]
return (int(kv[0]), int(kv[1]), int(kv[2]))
kv=kernel_ver()
def getShared(pid):
if os.path.exists("/proc/"+str(pid)+"/smaps"):
shared_lines=[line
for line in open("/proc/"+str(pid)+"/smaps").readlines()
if line.find("Shared")!=-1]
return sum([int(line.split()[1]) for line in shared_lines])
elif (2,6,1) <= kv <= (2,6,9):
return 0 #lots of overestimation, but what can we do?
else:
return int(open("/proc/"+str(pid)+"/statm").readline().split()[2])*PAGESIZE
cmds={}
shareds={}
count={}
for line in os.popen("ps -e -o rss=,pid=,comm=").readlines():
size, pid, cmd = map(string.strip,line.strip().split(None,2))
if int(pid) == our_pid:
continue #no point counting this process
try:
shared=getShared(pid)
except:
continue #ps gone away
if shareds.get(cmd):
if shareds[cmd] < shared:
shareds[cmd]=shared
else:
shareds[cmd]=shared
#Note shared is always a subset of rss (trs is not always)
cmds[cmd]=cmds.setdefault(cmd,0)+int(size)-shared
if count.has_key(cmd):
count[cmd] += 1
else:
count[cmd] = 1
#Add max shared mem for each program
for cmd in cmds.keys():
cmds[cmd]=cmds[cmd]+shareds[cmd]
sort_list = cmds.items()
sort_list.sort(lambda x,y:cmp(x[1],y[1]))
sort_list=filter(lambda x:x[1],sort_list) #get rid of zero sized processes (kernel threads)
#The following matches "du -h" output
#see also human.py
def human(num, power="Ki"):
powers=["Ki","Mi","Gi","Ti"]
while num >= 1000: #4 digits
num /= 1024.0
power=powers[powers.index(power)+1]
return "%.1f %s" % (num,power)
def cmd_with_count(cmd, count):
if count>1:
return "%s (%u)" % (cmd, count)
else:
return cmd
print " Private + Shared = RAM used\tProgram \n"
for cmd in sort_list:
print "%8sB + %8sB = %8sB\t%s" % (human(cmd[1]-shareds[cmd[0]]), human(shareds[cmd[0]]), human(cmd[1]),
cmd_with_count(cmd[0], count[cmd[0]]))
print "\n Private + Shared = RAM used\tProgram \n"
#Warn of possible inaccuracies
#1 = accurate
#0 = some shared mem not reported
#-1= all shared mem not reported
def shared_val_accurate():
"""http://wiki.apache.org/spamassassin/TopSharedMemoryBug"""
if kv[:2] == (2,4):
if open("/proc/meminfo").read().find("Inact_") == -1:
return 1
return 0
elif kv[:2] == (2,6):
if os.path.exists("/proc/"+str(os.getpid())+"/smaps"):
return 1
if (2,6,1) <= kv <= (2,6,9):
return -1
return 0
else:
return 1
vm_accuracy = shared_val_accurate()
if vm_accuracy == -1:
sys.stderr.write("Warning: Shared memory is not reported by this system.\n")
sys.stderr.write("Values reported will be too large.\n")
elif vm_accuracy == 0:
sys.stderr.write("Warning: Shared memory is not reported accurately by this system.\n")
sys.stderr.write("Values reported could be too large.\n")
#####################
Espero que os sea útil. Saludos
Suscribirse a:
Entradas (Atom)