Compare commits
32 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2f19203ee | ||
|
|
8f2294477c | ||
|
|
0deddadf88 | ||
|
|
afc19f79b6 | ||
|
|
272cb17cf9 | ||
|
|
29fc357a31 | ||
|
|
02dabc0b86 | ||
|
|
834dc815c8 | ||
|
|
966493301c | ||
|
|
02bda05e5b | ||
|
|
9e788b26c6 | ||
|
|
18a680345c | ||
|
|
f8dee1586a | ||
|
|
2346c2b075 | ||
|
|
e570a14243 | ||
|
|
9c057328ae | ||
|
|
f5f0a68c8a | ||
|
|
cf3da2ba2e | ||
|
|
9bba73fa6d | ||
|
|
ac741c5ce9 | ||
|
|
1961a6fedb | ||
|
|
d4406341a4 | ||
|
|
80ef1d7e5e | ||
|
|
5d5d2dd183 | ||
|
|
76959e6b68 | ||
|
|
323e6491c4 | ||
|
|
bc48175929 | ||
|
|
0245f382ba | ||
|
|
0279ad465a | ||
|
|
4cb161ea45 | ||
|
|
27fe66a83c | ||
|
|
5c14f57b31 |
4 changed files with 152 additions and 77 deletions
41
COPYING
41
COPYING
|
|
@ -1,12 +1,12 @@
|
||||||
GNU GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
Version 2, June 1991
|
Version 2, June 1991
|
||||||
|
|
||||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||||
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
of this license document, but changing it is not allowed.
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
Preamble
|
Preamble
|
||||||
|
|
||||||
The licenses for most software are designed to take away your
|
The licenses for most software are designed to take away your
|
||||||
freedom to share and change it. By contrast, the GNU General Public
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
|
@ -15,7 +15,7 @@ software--to make sure the software is free for all its users. This
|
||||||
General Public License applies to most of the Free Software
|
General Public License applies to most of the Free Software
|
||||||
Foundation's software and to any other program whose authors commit to
|
Foundation's software and to any other program whose authors commit to
|
||||||
using it. (Some other Free Software Foundation software is covered by
|
using it. (Some other Free Software Foundation software is covered by
|
||||||
the GNU Library General Public License instead.) You can apply it to
|
the GNU Lesser General Public License instead.) You can apply it to
|
||||||
your programs, too.
|
your programs, too.
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
|
@ -55,8 +55,8 @@ patent must be licensed for everyone's free use or not licensed at all.
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
The precise terms and conditions for copying, distribution and
|
||||||
modification follow.
|
modification follow.
|
||||||
|
|
||||||
GNU GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
0. This License applies to any program or other work which contains
|
0. This License applies to any program or other work which contains
|
||||||
|
|
@ -110,7 +110,7 @@ above, provided that you also meet all of these conditions:
|
||||||
License. (Exception: if the Program itself is interactive but
|
License. (Exception: if the Program itself is interactive but
|
||||||
does not normally print such an announcement, your work based on
|
does not normally print such an announcement, your work based on
|
||||||
the Program is not required to print an announcement.)
|
the Program is not required to print an announcement.)
|
||||||
|
|
||||||
These requirements apply to the modified work as a whole. If
|
These requirements apply to the modified work as a whole. If
|
||||||
identifiable sections of that work are not derived from the Program,
|
identifiable sections of that work are not derived from the Program,
|
||||||
and can be reasonably considered independent and separate works in
|
and can be reasonably considered independent and separate works in
|
||||||
|
|
@ -168,7 +168,7 @@ access to copy from a designated place, then offering equivalent
|
||||||
access to copy the source code from the same place counts as
|
access to copy the source code from the same place counts as
|
||||||
distribution of the source code, even though third parties are not
|
distribution of the source code, even though third parties are not
|
||||||
compelled to copy the source along with the object code.
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
4. You may not copy, modify, sublicense, or distribute the Program
|
4. You may not copy, modify, sublicense, or distribute the Program
|
||||||
except as expressly provided under this License. Any attempt
|
except as expressly provided under this License. Any attempt
|
||||||
otherwise to copy, modify, sublicense or distribute the Program is
|
otherwise to copy, modify, sublicense or distribute the Program is
|
||||||
|
|
@ -225,7 +225,7 @@ impose that choice.
|
||||||
|
|
||||||
This section is intended to make thoroughly clear what is believed to
|
This section is intended to make thoroughly clear what is believed to
|
||||||
be a consequence of the rest of this License.
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
8. If the distribution and/or use of the Program is restricted in
|
8. If the distribution and/or use of the Program is restricted in
|
||||||
certain countries either by patents or by copyrighted interfaces, the
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
original copyright holder who places the Program under this License
|
original copyright holder who places the Program under this License
|
||||||
|
|
@ -255,7 +255,7 @@ make exceptions for this. Our decision will be guided by the two goals
|
||||||
of preserving the free status of all derivatives of our free software and
|
of preserving the free status of all derivatives of our free software and
|
||||||
of promoting the sharing and reuse of software generally.
|
of promoting the sharing and reuse of software generally.
|
||||||
|
|
||||||
NO WARRANTY
|
NO WARRANTY
|
||||||
|
|
||||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||||
|
|
@ -277,9 +277,9 @@ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||||
POSSIBILITY OF SUCH DAMAGES.
|
POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
How to Apply These Terms to Your New Programs
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest
|
If you develop a new program, and you want it to be of the greatest
|
||||||
possible use to the public, the best way to achieve this is to make it
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
|
@ -303,17 +303,16 @@ the "copyright" line and a pointer to where the full notice is found.
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License along
|
||||||
along with this program; if not, write to the Free Software
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
If the program is interactive, make it output a short notice like this
|
If the program is interactive, make it output a short notice like this
|
||||||
when it starts in an interactive mode:
|
when it starts in an interactive mode:
|
||||||
|
|
||||||
Gnomovision version 69, Copyright (C) year name of author
|
Gnomovision version 69, Copyright (C) year name of author
|
||||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it
|
This is free software, and you are welcome to redistribute it
|
||||||
under certain conditions; type `show c' for details.
|
under certain conditions; type `show c' for details.
|
||||||
|
|
@ -336,5 +335,5 @@ necessary. Here is a sample; alter the names:
|
||||||
This General Public License does not permit incorporating your program into
|
This General Public License does not permit incorporating your program into
|
||||||
proprietary programs. If your program is a subroutine library, you may
|
proprietary programs. If your program is a subroutine library, you may
|
||||||
consider it more useful to permit linking proprietary applications with the
|
consider it more useful to permit linking proprietary applications with the
|
||||||
library. If this is what you want to do, use the GNU Library General
|
library. If this is what you want to do, use the GNU Lesser General
|
||||||
Public License instead of this License.
|
Public License instead of this License.
|
||||||
|
|
|
||||||
176
smem
176
smem
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/env python
|
||||||
#
|
#
|
||||||
# smem - a tool for meaningful memory reporting
|
# smem - a tool for meaningful memory reporting
|
||||||
#
|
#
|
||||||
|
|
@ -8,17 +8,20 @@
|
||||||
# the GNU General Public License version 2 or later, incorporated
|
# the GNU General Public License version 2 or later, incorporated
|
||||||
# herein by reference.
|
# herein by reference.
|
||||||
|
|
||||||
import re, os, sys, pwd, grp, optparse, errno, tarfile
|
import re, os, sys, pwd, optparse, errno, tarfile
|
||||||
|
|
||||||
|
warned = False
|
||||||
|
|
||||||
class procdata(object):
|
class procdata(object):
|
||||||
def __init__(self, source):
|
def __init__(self, source):
|
||||||
self._ucache = {}
|
self._ucache = {}
|
||||||
self._gcache = {}
|
self._gcache = {}
|
||||||
self.source = source and source or ""
|
self.source = source and source or ""
|
||||||
|
self._memdata = None
|
||||||
def _list(self):
|
def _list(self):
|
||||||
return os.listdir(self.source + "/proc")
|
return os.listdir(self.source + "/proc")
|
||||||
def _read(self, f):
|
def _read(self, f):
|
||||||
return file(self.source + '/proc/' + f).read()
|
return open(self.source + '/proc/' + f).read()
|
||||||
def _readlines(self, f):
|
def _readlines(self, f):
|
||||||
return self._read(f).splitlines(True)
|
return self._read(f).splitlines(True)
|
||||||
def _stat(self, f):
|
def _stat(self, f):
|
||||||
|
|
@ -31,7 +34,9 @@ class procdata(object):
|
||||||
def mapdata(self, pid):
|
def mapdata(self, pid):
|
||||||
return self._readlines('%s/smaps' % pid)
|
return self._readlines('%s/smaps' % pid)
|
||||||
def memdata(self):
|
def memdata(self):
|
||||||
return self._readlines('meminfo')
|
if self._memdata is None:
|
||||||
|
self._memdata = self._readlines('meminfo')
|
||||||
|
return self._memdata
|
||||||
def version(self):
|
def version(self):
|
||||||
return self._readlines('version')[0]
|
return self._readlines('version')[0]
|
||||||
def pidname(self, pid):
|
def pidname(self, pid):
|
||||||
|
|
@ -48,25 +53,31 @@ class procdata(object):
|
||||||
return '?'
|
return '?'
|
||||||
def piduser(self, pid):
|
def piduser(self, pid):
|
||||||
try:
|
try:
|
||||||
return self._stat('%d/cmdline' % pid).st_uid
|
return self._stat('%d' % pid).st_uid
|
||||||
except:
|
except:
|
||||||
return -1
|
return -1
|
||||||
def pidgroup(self, pid):
|
def pidgroup(self, pid):
|
||||||
try:
|
try:
|
||||||
return self._stat('%d/cmdline' % pid).st_gid
|
return self._stat('%d' % pid).st_gid
|
||||||
except:
|
except:
|
||||||
return -1
|
return -1
|
||||||
def username(self, uid):
|
def username(self, uid):
|
||||||
if uid == -1:
|
if uid == -1:
|
||||||
return '?'
|
return '?'
|
||||||
if uid not in self._ucache:
|
if uid not in self._ucache:
|
||||||
self._ucache[uid] = pwd.getpwuid(uid)[0]
|
try:
|
||||||
|
self._ucache[uid] = pwd.getpwuid(uid)[0]
|
||||||
|
except KeyError:
|
||||||
|
self._ucache[uid] = str(uid)
|
||||||
return self._ucache[uid]
|
return self._ucache[uid]
|
||||||
def groupname(self, gid):
|
def groupname(self, gid):
|
||||||
if gid == -1:
|
if gid == -1:
|
||||||
return '?'
|
return '?'
|
||||||
if gid not in self._gcache:
|
if gid not in self._gcache:
|
||||||
self._gcache[gid] = pwd.getgrgid(gid)[0]
|
try:
|
||||||
|
self._gcache[gid] = pwd.getgrgid(gid)[0]
|
||||||
|
except KeyError:
|
||||||
|
self._gcache[gid] = str(gid)
|
||||||
return self._gcache[gid]
|
return self._gcache[gid]
|
||||||
|
|
||||||
class tardata(procdata):
|
class tardata(procdata):
|
||||||
|
|
@ -83,12 +94,12 @@ class tardata(procdata):
|
||||||
def _readlines(self, f):
|
def _readlines(self, f):
|
||||||
return self.tar.extractfile(f).readlines()
|
return self.tar.extractfile(f).readlines()
|
||||||
def piduser(self, p):
|
def piduser(self, p):
|
||||||
t = self.tar.getmember("%s/cmdline" % p)
|
t = self.tar.getmember("%d" % p)
|
||||||
if t.uname:
|
if t.uname:
|
||||||
self._ucache[t.uid] = t.uname
|
self._ucache[t.uid] = t.uname
|
||||||
return t.uid
|
return t.uid
|
||||||
def pidgroup(self, p):
|
def pidgroup(self, p):
|
||||||
t = self.tar.getmember("%s/cmdline" % p)
|
t = self.tar.getmember("%d" % p)
|
||||||
if t.gname:
|
if t.gname:
|
||||||
self._gcache[t.gid] = t.gname
|
self._gcache[t.gid] = t.gname
|
||||||
return t.gid
|
return t.gid
|
||||||
|
|
@ -128,13 +139,19 @@ def kernelsize():
|
||||||
return _kernelsize
|
return _kernelsize
|
||||||
|
|
||||||
def pidmaps(pid):
|
def pidmaps(pid):
|
||||||
|
global warned
|
||||||
maps = {}
|
maps = {}
|
||||||
start = None
|
start = None
|
||||||
|
seen = False
|
||||||
|
empty = True
|
||||||
for l in src.mapdata(pid):
|
for l in src.mapdata(pid):
|
||||||
|
empty = False
|
||||||
f = l.split()
|
f = l.split()
|
||||||
if f[-1] == 'kB':
|
if f[-1] == 'kB':
|
||||||
|
if f[0].startswith('Pss'):
|
||||||
|
seen = True
|
||||||
maps[start][f[0][:-1].lower()] = int(f[1])
|
maps[start][f[0][:-1].lower()] = int(f[1])
|
||||||
else:
|
elif '-' in f[0] and ':' not in f[0]: # looks like a mapping range
|
||||||
start, end = f[0].split('-')
|
start, end = f[0].split('-')
|
||||||
start = int(start, 16)
|
start = int(start, 16)
|
||||||
name = "<anonymous>"
|
name = "<anonymous>"
|
||||||
|
|
@ -144,10 +161,16 @@ def pidmaps(pid):
|
||||||
offset=int(f[2], 16),
|
offset=int(f[2], 16),
|
||||||
device=f[3], inode=f[4], name=name)
|
device=f[3], inode=f[4], name=name)
|
||||||
|
|
||||||
|
if not empty and not seen and not warned:
|
||||||
|
sys.stderr.write('warning: kernel does not appear to support PSS measurement\n')
|
||||||
|
warned = True
|
||||||
|
if not options.sort:
|
||||||
|
options.sort = 'rss'
|
||||||
|
|
||||||
if options.mapfilter:
|
if options.mapfilter:
|
||||||
f = {}
|
f = {}
|
||||||
for m in maps:
|
for m in maps:
|
||||||
if not filter(options.mapfilter, m, lambda x: maps[x]['name']):
|
if not filters(options.mapfilter, m, lambda x: maps[x]['name']):
|
||||||
f[m] = maps[m]
|
f[m] = maps[m]
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
@ -176,7 +199,7 @@ def units(x):
|
||||||
s = ''
|
s = ''
|
||||||
if x == 0:
|
if x == 0:
|
||||||
return '0'
|
return '0'
|
||||||
for s in ('', 'K', 'M', 'G'):
|
for s in ('', 'K', 'M', 'G', 'T'):
|
||||||
if x < 1024:
|
if x < 1024:
|
||||||
break
|
break
|
||||||
x /= 1024.0
|
x /= 1024.0
|
||||||
|
|
@ -184,7 +207,8 @@ def units(x):
|
||||||
|
|
||||||
def fromunits(x):
|
def fromunits(x):
|
||||||
s = dict(k=2**10, K=2**10, kB=2**10, KB=2**10,
|
s = dict(k=2**10, K=2**10, kB=2**10, KB=2**10,
|
||||||
M=2**20, MB=2**20, G=2**30, GB=2**30)
|
M=2**20, MB=2**20, G=2**30, GB=2**30,
|
||||||
|
T=2**40, TB=2**40)
|
||||||
for k,v in s.items():
|
for k,v in s.items():
|
||||||
if x.endswith(k):
|
if x.endswith(k):
|
||||||
return int(float(x[:-len(k)])*v)
|
return int(float(x[:-len(k)])*v)
|
||||||
|
|
@ -194,14 +218,16 @@ def fromunits(x):
|
||||||
def pidusername(pid):
|
def pidusername(pid):
|
||||||
return src.username(src.piduser(pid))
|
return src.username(src.piduser(pid))
|
||||||
|
|
||||||
def showamount(a):
|
def showamount(a, total):
|
||||||
if options.abbreviate:
|
if options.abbreviate:
|
||||||
return units(a * 1024)
|
return units(a * 1024)
|
||||||
elif options.percent:
|
elif options.percent:
|
||||||
return "%.2f%%" % (100.0 * a / totalmem())
|
if total == 0:
|
||||||
|
return 'N/A'
|
||||||
|
return "%.2f%%" % (100.0 * a / total)
|
||||||
return a
|
return a
|
||||||
|
|
||||||
def filter(opt, arg, *sources):
|
def filters(opt, arg, *sources):
|
||||||
if not opt:
|
if not opt:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -214,7 +240,7 @@ def pidtotals(pid):
|
||||||
maps = pidmaps(pid)
|
maps = pidmaps(pid)
|
||||||
t = dict(size=0, rss=0, pss=0, shared_clean=0, shared_dirty=0,
|
t = dict(size=0, rss=0, pss=0, shared_clean=0, shared_dirty=0,
|
||||||
private_clean=0, private_dirty=0, referenced=0, swap=0)
|
private_clean=0, private_dirty=0, referenced=0, swap=0)
|
||||||
for m in maps.iterkeys():
|
for m in maps:
|
||||||
for k in t:
|
for k in t:
|
||||||
t[k] += maps[m].get(k, 0)
|
t[k] += maps[m].get(k, 0)
|
||||||
|
|
||||||
|
|
@ -226,8 +252,8 @@ def pidtotals(pid):
|
||||||
def processtotals(pids):
|
def processtotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
if (filters(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filters(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
p = pidtotals(pid)
|
p = pidtotals(pid)
|
||||||
|
|
@ -247,7 +273,7 @@ def showpids():
|
||||||
return pidusername(p)
|
return pidusername(p)
|
||||||
|
|
||||||
fields = dict(
|
fields = dict(
|
||||||
pid=('PID', lambda n: n, '% 5s', lambda x: len(p),
|
pid=('PID', lambda n: n, '% 5s', lambda x: len(pt),
|
||||||
'process ID'),
|
'process ID'),
|
||||||
user=('User', showuser, '%-8s', lambda x: len(dict.fromkeys(x)),
|
user=('User', showuser, '%-8s', lambda x: len(dict.fromkeys(x)),
|
||||||
'owner of process'),
|
'owner of process'),
|
||||||
|
|
@ -275,13 +301,13 @@ def showpids():
|
||||||
def maptotals(pids):
|
def maptotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
if (filters(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filters(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
maps = pidmaps(pid)
|
maps = pidmaps(pid)
|
||||||
seen = {}
|
seen = {}
|
||||||
for m in maps.iterkeys():
|
for m in maps:
|
||||||
name = maps[m]['name']
|
name = maps[m]['name']
|
||||||
if name not in totals:
|
if name not in totals:
|
||||||
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
||||||
|
|
@ -297,8 +323,8 @@ def maptotals(pids):
|
||||||
t['pids'] += 1
|
t['pids'] += 1
|
||||||
seen[name] = 1
|
seen[name] = 1
|
||||||
totals[name] = t
|
totals[name] = t
|
||||||
except:
|
except EnvironmentError:
|
||||||
raise
|
continue
|
||||||
return totals
|
return totals
|
||||||
|
|
||||||
def showmaps():
|
def showmaps():
|
||||||
|
|
@ -340,15 +366,15 @@ def showmaps():
|
||||||
def usertotals(pids):
|
def usertotals(pids):
|
||||||
totals = {}
|
totals = {}
|
||||||
for pid in pids:
|
for pid in pids:
|
||||||
if (filter(options.processfilter, pid, src.pidname, src.pidcmd) or
|
if (filters(options.processfilter, pid, src.pidname, src.pidcmd) or
|
||||||
filter(options.userfilter, pid, pidusername)):
|
filters(options.userfilter, pid, pidusername)):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
maps = pidmaps(pid)
|
maps = pidmaps(pid)
|
||||||
if len(maps) == 0:
|
if len(maps) == 0:
|
||||||
continue
|
continue
|
||||||
except:
|
except EnvironmentError:
|
||||||
raise
|
continue
|
||||||
user = src.piduser(pid)
|
user = src.piduser(pid)
|
||||||
if user not in totals:
|
if user not in totals:
|
||||||
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
t = dict(size=0, rss=0, pss=0, shared_clean=0,
|
||||||
|
|
@ -357,7 +383,7 @@ def usertotals(pids):
|
||||||
else:
|
else:
|
||||||
t = totals[user]
|
t = totals[user]
|
||||||
|
|
||||||
for m in maps.iterkeys():
|
for m in maps:
|
||||||
for k in t:
|
for k in t:
|
||||||
t[k] += maps[m].get(k, 0)
|
t[k] += maps[m].get(k, 0)
|
||||||
|
|
||||||
|
|
@ -437,11 +463,44 @@ def showsystem():
|
||||||
showtable(range(len(l)), fields, columns.split(), options.sort or 'order')
|
showtable(range(len(l)), fields, columns.split(), options.sort or 'order')
|
||||||
|
|
||||||
def showfields(fields, f):
|
def showfields(fields, f):
|
||||||
if f != list:
|
if type(f) in (list, set):
|
||||||
print "unknown field", f
|
print("unknown fields: " + " ".join(f))
|
||||||
print "known fields:"
|
else:
|
||||||
for l in sorted(fields.keys()):
|
print("unknown field %s" % f)
|
||||||
print "%-8s %s" % (l, fields[l][-1])
|
print("known fields:")
|
||||||
|
for l in sorted(fields):
|
||||||
|
print("%-8s %s" % (l, fields[l][-1]))
|
||||||
|
|
||||||
|
def autosize(columns, fields, rows):
|
||||||
|
colsizes = {}
|
||||||
|
for c in columns:
|
||||||
|
sizes = [1]
|
||||||
|
|
||||||
|
if not options.no_header:
|
||||||
|
sizes.append(len(fields[c][0]))
|
||||||
|
|
||||||
|
if (options.abbreviate or options.percent) and 'a' in fields[c][2]:
|
||||||
|
sizes.append(7)
|
||||||
|
else:
|
||||||
|
for r in rows:
|
||||||
|
sizes.append(len(str(fields[c][1](r))))
|
||||||
|
|
||||||
|
colsizes[c] = max(sizes)
|
||||||
|
|
||||||
|
overflowcols = set(["command", "map"]) & set(columns)
|
||||||
|
if len(overflowcols) > 0:
|
||||||
|
overflowcol = overflowcols.pop()
|
||||||
|
totnoflow = sum(colsizes.values()) - colsizes[overflowcol]
|
||||||
|
try:
|
||||||
|
ttyrows, ttycolumns = os.popen('stty size', 'r').read().split()
|
||||||
|
ttyrows, ttycolumns = int(ttyrows), int(ttycolumns)
|
||||||
|
except:
|
||||||
|
ttyrows, ttycolumns = (24, 80)
|
||||||
|
maxflowcol = ttycolumns - totnoflow - len(columns)
|
||||||
|
maxflowcol = max(maxflowcol, 10)
|
||||||
|
colsizes[overflowcol] = min(colsizes[overflowcol], maxflowcol)
|
||||||
|
|
||||||
|
return colsizes
|
||||||
|
|
||||||
def showtable(rows, fields, columns, sort):
|
def showtable(rows, fields, columns, sort):
|
||||||
header = ""
|
header = ""
|
||||||
|
|
@ -457,17 +516,31 @@ def showtable(rows, fields, columns, sort):
|
||||||
if options.bar:
|
if options.bar:
|
||||||
columns.append(options.bar)
|
columns.append(options.bar)
|
||||||
|
|
||||||
for n in columns:
|
mt = totalmem()
|
||||||
if n not in fields:
|
st = memory()['swaptotal']
|
||||||
showfields(fields, n)
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
|
missing = set(columns) - set(fields)
|
||||||
|
if len(missing) > 0:
|
||||||
|
showfields(fields, missing)
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
if options.autosize:
|
||||||
|
colsizes = autosize(columns, fields, rows)
|
||||||
|
else:
|
||||||
|
colsizes = {}
|
||||||
|
|
||||||
|
for n in columns:
|
||||||
f = fields[n][2]
|
f = fields[n][2]
|
||||||
if 'a' in f:
|
if 'a' in f:
|
||||||
formatter.append(showamount)
|
if n == 'swap':
|
||||||
|
formatter.append(lambda x: showamount(x, st))
|
||||||
|
else:
|
||||||
|
formatter.append(lambda x: showamount(x, mt))
|
||||||
f = f.replace('a', 's')
|
f = f.replace('a', 's')
|
||||||
else:
|
else:
|
||||||
formatter.append(lambda x: x)
|
formatter.append(lambda x: x)
|
||||||
|
if n in colsizes:
|
||||||
|
f = re.sub(r"[0-9]+", str(colsizes[n]), f)
|
||||||
format += f + " "
|
format += f + " "
|
||||||
header += f % fields[n][0] + " "
|
header += f % fields[n][0] + " "
|
||||||
|
|
||||||
|
|
@ -486,10 +559,10 @@ def showtable(rows, fields, columns, sort):
|
||||||
return
|
return
|
||||||
|
|
||||||
if not options.no_header:
|
if not options.no_header:
|
||||||
print header
|
print(header)
|
||||||
|
|
||||||
for k,r in l:
|
for k,r in l:
|
||||||
print format % tuple([f(v) for f,v in zip(formatter, r)])
|
print(format % tuple([f(v) for f,v in zip(formatter, r)]))
|
||||||
|
|
||||||
if options.totals:
|
if options.totals:
|
||||||
# totals
|
# totals
|
||||||
|
|
@ -501,8 +574,8 @@ def showtable(rows, fields, columns, sort):
|
||||||
else:
|
else:
|
||||||
t.append("")
|
t.append("")
|
||||||
|
|
||||||
print "-" * len(header)
|
print("-" * len(header))
|
||||||
print format % tuple([f(v) for f,v in zip(formatter, t)])
|
print(format % tuple([f(v) for f,v in zip(formatter, t)]))
|
||||||
|
|
||||||
def showpie(l, sort):
|
def showpie(l, sort):
|
||||||
try:
|
try:
|
||||||
|
|
@ -583,12 +656,6 @@ def showbar(l, columns, sort):
|
||||||
pylab.legend([p[0] for p in pl], key)
|
pylab.legend([p[0] for p in pl], key)
|
||||||
pylab.show()
|
pylab.show()
|
||||||
|
|
||||||
def kernel_version_check():
|
|
||||||
kernel_release = src.version().split()[2].split('-')[0]
|
|
||||||
if kernel_release < "2.6.27":
|
|
||||||
name = os.path.basename(sys.argv[0])
|
|
||||||
sys.stderr.write(name + " requires a kernel >= 2.6.27\n")
|
|
||||||
sys.exit(-1)
|
|
||||||
|
|
||||||
parser = optparse.OptionParser("%prog [options]")
|
parser = optparse.OptionParser("%prog [options]")
|
||||||
parser.add_option("-H", "--no-header", action="store_true",
|
parser.add_option("-H", "--no-header", action="store_true",
|
||||||
|
|
@ -597,6 +664,8 @@ parser.add_option("-c", "--columns", type="str",
|
||||||
help="columns to show")
|
help="columns to show")
|
||||||
parser.add_option("-t", "--totals", action="store_true",
|
parser.add_option("-t", "--totals", action="store_true",
|
||||||
help="show totals")
|
help="show totals")
|
||||||
|
parser.add_option("-a", "--autosize", action="store_true",
|
||||||
|
help="size columns to fit terminal size")
|
||||||
|
|
||||||
parser.add_option("-R", "--realmem", type="str",
|
parser.add_option("-R", "--realmem", type="str",
|
||||||
help="amount of physical RAM")
|
help="amount of physical RAM")
|
||||||
|
|
@ -647,8 +716,6 @@ try:
|
||||||
except:
|
except:
|
||||||
src = procdata(options.source)
|
src = procdata(options.source)
|
||||||
|
|
||||||
kernel_version_check()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if options.mappings:
|
if options.mappings:
|
||||||
showmaps()
|
showmaps()
|
||||||
|
|
@ -658,7 +725,8 @@ try:
|
||||||
showsystem()
|
showsystem()
|
||||||
else:
|
else:
|
||||||
showpids()
|
showpids()
|
||||||
except IOError, e:
|
except IOError:
|
||||||
|
_, e, _ = sys.exc_info()
|
||||||
if e.errno == errno.EPIPE:
|
if e.errno == errno.EPIPE:
|
||||||
pass
|
pass
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
|
||||||
7
smem.8
7
smem.8
|
|
@ -95,6 +95,10 @@ User filter regular expression.
|
||||||
|
|
||||||
.SS OUTPUT FORMATTING
|
.SS OUTPUT FORMATTING
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B \-a, \-\-autosize
|
||||||
|
Size columns to fit terminal size.
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.BI "\-c " COLUMNS ", \-\-columns=" COLUMNS
|
.BI "\-c " COLUMNS ", \-\-columns=" COLUMNS
|
||||||
Columns to show.
|
Columns to show.
|
||||||
|
|
@ -144,7 +148,8 @@ Show pie graph.
|
||||||
\fBsmem\fP requires:
|
\fBsmem\fP requires:
|
||||||
|
|
||||||
.IP \(bu 3
|
.IP \(bu 3
|
||||||
Linux kernel 2.6.27 or newer.
|
Linux kernel providing 'Pss' metric in /proc/<pid>/smaps (generally
|
||||||
|
2.6.27 or newer).
|
||||||
.IP \(bu
|
.IP \(bu
|
||||||
Python 2.x (at least 2.4 or so).
|
Python 2.x (at least 2.4 or so).
|
||||||
.IP \(bu
|
.IP \(bu
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,7 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
DIR *d;
|
DIR *d;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
|
struct stat s;
|
||||||
|
|
||||||
chdir("/proc");
|
chdir("/proc");
|
||||||
archivefile("meminfo", 1);
|
archivefile("meminfo", 1);
|
||||||
|
|
@ -101,7 +102,9 @@ int main(int argc, char *argv[])
|
||||||
d = opendir(".");
|
d = opendir(".");
|
||||||
while ((de = readdir(d)))
|
while ((de = readdir(d)))
|
||||||
if (de->d_name[0] >= '0' && de->d_name[0] <= '9') {
|
if (de->d_name[0] >= '0' && de->d_name[0] <= '9') {
|
||||||
writeheader(1, de->d_name, 0555, 0, 0, 0, 0, 5);
|
stat (de->d_name, &s);
|
||||||
|
writeheader(1, de->d_name, 0555, s.st_uid,
|
||||||
|
s.st_gid, 0, s.st_mtime, 5);
|
||||||
archivejoin(de->d_name, "smaps", 1);
|
archivejoin(de->d_name, "smaps", 1);
|
||||||
archivejoin(de->d_name, "cmdline", 1);
|
archivejoin(de->d_name, "cmdline", 1);
|
||||||
archivejoin(de->d_name, "stat", 1);
|
archivejoin(de->d_name, "stat", 1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue