michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import os, sys, ctypes, ctypes.wintypes michael@0: michael@0: class VM_COUNTERS(ctypes.Structure): michael@0: _fields_ = [("PeakVirtualSize", ctypes.wintypes.ULONG), michael@0: ("VirtualSize", ctypes.wintypes.ULONG), michael@0: ("PageFaultCount", ctypes.wintypes.ULONG), michael@0: ("PeakWorkingSetSize", ctypes.wintypes.ULONG), michael@0: ("WorkingSetSize", ctypes.wintypes.ULONG), michael@0: ("QuotaPeakPagedPoolUsage", ctypes.wintypes.ULONG), michael@0: ("QuotaPagedPoolUsage", ctypes.wintypes.ULONG), michael@0: ("QuotaPeakNonPagedPoolUsage", ctypes.wintypes.ULONG), michael@0: ("QuotaNonPagedPoolUsage", ctypes.wintypes.ULONG), michael@0: ("PagefileUsage", ctypes.wintypes.ULONG), michael@0: ("PeakPagefileUsage", ctypes.wintypes.ULONG) michael@0: ] michael@0: michael@0: def get_vmsize(handle): michael@0: """ michael@0: Return (peak_virtual_size, virtual_size) for the process |handle|. michael@0: """ michael@0: ProcessVmCounters = 3 michael@0: vmc = VM_COUNTERS() michael@0: if ctypes.windll.ntdll.NtQueryInformationProcess(int(handle), michael@0: ProcessVmCounters, michael@0: ctypes.byref(vmc), michael@0: ctypes.sizeof(vmc), michael@0: None) == 0: michael@0: return (vmc.PeakVirtualSize, vmc.VirtualSize) michael@0: michael@0: return (-1, -1) michael@0: michael@0: if __name__ == '__main__': michael@0: PROCESS_QUERY_INFORMATION = 0x0400 michael@0: for pid in sys.argv[1:]: michael@0: handle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, michael@0: 0, # no inherit michael@0: int(pid)) michael@0: if handle: michael@0: print "Process %s:" % pid michael@0: vsize, peak_vsize = get_vmsize(handle) michael@0: print "peak vsize: %d" % peak_vsize michael@0: ctypes.windll.kernel32.CloseHandle(handle) michael@0: else: michael@0: print "Couldn't open process %s" % pid