Advertisement
batijuank

procsmem

Sep 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6.  
  7. """
  8. Show detailed memory usage about all (querable) processes.
  9.  
  10. Processes are sorted by their "USS" (Unique Set Size) memory, which is
  11. probably the most representative metric for determining how much memory
  12. is actually being used by a process.
  13.  
  14. This is similar to "smem" cmdline utility on Linux:
  15. https://www.selenic.com/smem/
  16.  
  17. Author: Giampaolo Rodola' <g.rodola@gmail.com>
  18.  
  19. ~/svn/psutil$ ./scripts/procsmem.py
  20. PID     User    Cmdline                            USS     PSS    Swap     RSS
  21. ==============================================================================
  22. ...
  23. 3986    giampao /usr/bin/python3 /usr/bin/indi   15.3M   16.6M      0B   25.6M
  24. 3906    giampao /usr/lib/ibus/ibus-ui-gtk3       17.6M   18.1M      0B   26.7M
  25. 3991    giampao python /usr/bin/hp-systray -x    19.0M   23.3M      0B   40.7M
  26. 3830    giampao /usr/bin/ibus-daemon --daemoni   19.0M   19.0M      0B   21.4M
  27. 20529   giampao /opt/sublime_text/plugin_host    19.9M   20.1M      0B   22.0M
  28. 3990    giampao nautilus -n                      20.6M   29.9M      0B   50.2M
  29. 3898    giampao /usr/lib/unity/unity-panel-ser   27.1M   27.9M      0B   37.7M
  30. 4176    giampao /usr/lib/evolution/evolution-c   35.7M   36.2M      0B   41.5M
  31. 20712   giampao /usr/bin/python -B /home/giamp   45.6M   45.9M      0B   49.4M
  32. 3880    giampao /usr/lib/x86_64-linux-gnu/hud/   51.6M   52.7M      0B   61.3M
  33. 20513   giampao /opt/sublime_text/sublime_text   65.8M   73.0M      0B   87.9M
  34. 3976    giampao compiz                          115.0M  117.0M      0B  130.9M
  35. 32486   giampao skype                           145.1M  147.5M      0B  149.6M
  36. """
  37.  
  38. import sys
  39.  
  40. import psutil
  41.  
  42.  
  43. if not (psutil.LINUX or psutil.MACOS or psutil.WINDOWS):
  44.     sys.exit("platform not supported")
  45.  
  46.  
  47. def convert_bytes(n):
  48.     symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
  49.     prefix = {}
  50.     for i, s in enumerate(symbols):
  51.         prefix[s] = 1 << (i + 1) * 10
  52.     for s in reversed(symbols):
  53.         if n >= prefix[s]:
  54.             value = float(n) / prefix[s]
  55.             return '%.1f%s' % (value, s)
  56.     return "%sB" % n
  57.  
  58.  
  59. def status():
  60.     ad_pids = []
  61.     procs = []
  62.     for p in psutil.process_iter():
  63.         with p.oneshot():
  64.             try:
  65.                 mem = p.memory_full_info()
  66.                 info = p.as_dict(attrs=["cmdline", "username"])
  67.             except psutil.AccessDenied:
  68.                 ad_pids.append(p.pid)
  69.             except psutil.NoSuchProcess:
  70.                 pass
  71.             else:
  72.                 p._uss = mem.uss
  73.                 p._rss = mem.rss
  74.                 if not p._uss:
  75.                     continue
  76.                 p._pss = getattr(mem, "pss", "")
  77.                 p._swap = getattr(mem, "swap", "")
  78.                 p._info = info
  79.                 procs.append(p)
  80.  
  81.     procs.sort(reverse=True, key=lambda p: p._swap)
  82.     templ = "%-7s %-7s %-30s %7s %7s %7s %7s"
  83.     print(templ % ("PID", "User", "Cmdline", "Swap", "PSS", "USS", "RSS"))
  84.     print("=" * 78)
  85.     for p in procs[:86]:
  86.         line = templ % (
  87.             p.pid,
  88.             p._info["username"][:7],
  89.             " ".join(p._info["cmdline"])[:30],
  90.             convert_bytes(p._swap) if p._swap != "" else "",
  91.             convert_bytes(p._uss),
  92.             convert_bytes(p._pss) if p._pss != "" else "",
  93.  
  94.             convert_bytes(p._rss),
  95.         )
  96.         print(line)
  97.     if ad_pids:
  98.         print("warning: access denied for %s pids" % (len(ad_pids)),
  99.               file=sys.stderr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement