|
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 |
|
5 import os, sys, ctypes, ctypes.wintypes |
|
6 |
|
7 class VM_COUNTERS(ctypes.Structure): |
|
8 _fields_ = [("PeakVirtualSize", ctypes.wintypes.ULONG), |
|
9 ("VirtualSize", ctypes.wintypes.ULONG), |
|
10 ("PageFaultCount", ctypes.wintypes.ULONG), |
|
11 ("PeakWorkingSetSize", ctypes.wintypes.ULONG), |
|
12 ("WorkingSetSize", ctypes.wintypes.ULONG), |
|
13 ("QuotaPeakPagedPoolUsage", ctypes.wintypes.ULONG), |
|
14 ("QuotaPagedPoolUsage", ctypes.wintypes.ULONG), |
|
15 ("QuotaPeakNonPagedPoolUsage", ctypes.wintypes.ULONG), |
|
16 ("QuotaNonPagedPoolUsage", ctypes.wintypes.ULONG), |
|
17 ("PagefileUsage", ctypes.wintypes.ULONG), |
|
18 ("PeakPagefileUsage", ctypes.wintypes.ULONG) |
|
19 ] |
|
20 |
|
21 def get_vmsize(handle): |
|
22 """ |
|
23 Return (peak_virtual_size, virtual_size) for the process |handle|. |
|
24 """ |
|
25 ProcessVmCounters = 3 |
|
26 vmc = VM_COUNTERS() |
|
27 if ctypes.windll.ntdll.NtQueryInformationProcess(int(handle), |
|
28 ProcessVmCounters, |
|
29 ctypes.byref(vmc), |
|
30 ctypes.sizeof(vmc), |
|
31 None) == 0: |
|
32 return (vmc.PeakVirtualSize, vmc.VirtualSize) |
|
33 |
|
34 return (-1, -1) |
|
35 |
|
36 if __name__ == '__main__': |
|
37 PROCESS_QUERY_INFORMATION = 0x0400 |
|
38 for pid in sys.argv[1:]: |
|
39 handle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, |
|
40 0, # no inherit |
|
41 int(pid)) |
|
42 if handle: |
|
43 print "Process %s:" % pid |
|
44 vsize, peak_vsize = get_vmsize(handle) |
|
45 print "peak vsize: %d" % peak_vsize |
|
46 ctypes.windll.kernel32.CloseHandle(handle) |
|
47 else: |
|
48 print "Couldn't open process %s" % pid |