procmem.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import os, sys, ctypes, ctypes.wintypes
  5. class VM_COUNTERS(ctypes.Structure):
  6. _fields_ = [("PeakVirtualSize", ctypes.wintypes.ULONG),
  7. ("VirtualSize", ctypes.wintypes.ULONG),
  8. ("PageFaultCount", ctypes.wintypes.ULONG),
  9. ("PeakWorkingSetSize", ctypes.wintypes.ULONG),
  10. ("WorkingSetSize", ctypes.wintypes.ULONG),
  11. ("QuotaPeakPagedPoolUsage", ctypes.wintypes.ULONG),
  12. ("QuotaPagedPoolUsage", ctypes.wintypes.ULONG),
  13. ("QuotaPeakNonPagedPoolUsage", ctypes.wintypes.ULONG),
  14. ("QuotaNonPagedPoolUsage", ctypes.wintypes.ULONG),
  15. ("PagefileUsage", ctypes.wintypes.ULONG),
  16. ("PeakPagefileUsage", ctypes.wintypes.ULONG)
  17. ]
  18. def get_vmsize(handle):
  19. """
  20. Return (peak_virtual_size, virtual_size) for the process |handle|.
  21. """
  22. ProcessVmCounters = 3
  23. vmc = VM_COUNTERS()
  24. if ctypes.windll.ntdll.NtQueryInformationProcess(int(handle),
  25. ProcessVmCounters,
  26. ctypes.byref(vmc),
  27. ctypes.sizeof(vmc),
  28. None) == 0:
  29. return (vmc.PeakVirtualSize, vmc.VirtualSize)
  30. return (-1, -1)
  31. if __name__ == '__main__':
  32. PROCESS_QUERY_INFORMATION = 0x0400
  33. for pid in sys.argv[1:]:
  34. handle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION,
  35. 0, # no inherit
  36. int(pid))
  37. if handle:
  38. print "Process %s:" % pid
  39. vsize, peak_vsize = get_vmsize(handle)
  40. print "peak vsize: %d" % peak_vsize
  41. ctypes.windll.kernel32.CloseHandle(handle)
  42. else:
  43. print "Couldn't open process %s" % pid