OLD | NEW |
(Empty) | |
| 1 # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt |
| 3 |
| 4 """OS information for testing.""" |
| 5 |
| 6 from coverage import env |
| 7 |
| 8 |
| 9 if env.WINDOWS: |
| 10 # Windows implementation |
| 11 def process_ram(): |
| 12 """How much RAM is this process using? (Windows)""" |
| 13 import ctypes |
| 14 # From: http://lists.ubuntu.com/archives/bazaar-commits/2009-February/01
1990.html |
| 15 class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure): |
| 16 """Used by GetProcessMemoryInfo""" |
| 17 _fields_ = [ |
| 18 ('cb', ctypes.c_ulong), |
| 19 ('PageFaultCount', ctypes.c_ulong), |
| 20 ('PeakWorkingSetSize', ctypes.c_size_t), |
| 21 ('WorkingSetSize', ctypes.c_size_t), |
| 22 ('QuotaPeakPagedPoolUsage', ctypes.c_size_t), |
| 23 ('QuotaPagedPoolUsage', ctypes.c_size_t), |
| 24 ('QuotaPeakNonPagedPoolUsage', ctypes.c_size_t), |
| 25 ('QuotaNonPagedPoolUsage', ctypes.c_size_t), |
| 26 ('PagefileUsage', ctypes.c_size_t), |
| 27 ('PeakPagefileUsage', ctypes.c_size_t), |
| 28 ('PrivateUsage', ctypes.c_size_t), |
| 29 ] |
| 30 |
| 31 mem_struct = PROCESS_MEMORY_COUNTERS_EX() |
| 32 ret = ctypes.windll.psapi.GetProcessMemoryInfo( |
| 33 ctypes.windll.kernel32.GetCurrentProcess(), |
| 34 ctypes.byref(mem_struct), |
| 35 ctypes.sizeof(mem_struct) |
| 36 ) |
| 37 if not ret: |
| 38 return 0 |
| 39 return mem_struct.PrivateUsage |
| 40 |
| 41 elif env.LINUX: |
| 42 # Linux implementation |
| 43 import os |
| 44 |
| 45 _scale = {'kb': 1024, 'mb': 1024*1024} |
| 46 |
| 47 def _VmB(key): |
| 48 """Read the /proc/PID/status file to find memory use.""" |
| 49 try: |
| 50 # Get pseudo file /proc/<pid>/status |
| 51 with open('/proc/%d/status' % os.getpid()) as t: |
| 52 v = t.read() |
| 53 except IOError: |
| 54 return 0 # non-Linux? |
| 55 # Get VmKey line e.g. 'VmRSS: 9999 kB\n ...' |
| 56 i = v.index(key) |
| 57 v = v[i:].split(None, 3) |
| 58 if len(v) < 3: |
| 59 return 0 # Invalid format? |
| 60 # Convert Vm value to bytes. |
| 61 return int(float(v[1]) * _scale[v[2].lower()]) |
| 62 |
| 63 def process_ram(): |
| 64 """How much RAM is this process using? (Linux implementation)""" |
| 65 return _VmB('VmRSS') |
| 66 |
| 67 else: |
| 68 # Generic implementation. |
| 69 def process_ram(): |
| 70 """How much RAM is this process using? (stdlib implementation)""" |
| 71 import resource |
| 72 return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss |
OLD | NEW |