|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# |
| 4 | +# Roland Pihlakas, 2017 |
| 5 | +# roland@simplify.ee |
| 6 | +# |
| 7 | + |
| 8 | + |
| 9 | +# https://stackoverflow.com/questions/16779497/how-to-set-memory-limit-for-thread-or-process-in-python |
| 10 | + |
| 11 | +import ctypes |
| 12 | +import os |
| 13 | +import psutil |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +PROCESS_SET_QUOTA = 0x100 |
| 18 | +PROCESS_TERMINATE = 0x1 |
| 19 | +JobObjectExtendedLimitInformation = 9 |
| 20 | + |
| 21 | + |
| 22 | +# https://msdn.microsoft.com/en-us/library/windows/desktop/ms684147(v=vs.85).aspx |
| 23 | + |
| 24 | +JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x00000100 |
| 25 | +JOB_OBJECT_LIMIT_JOB_MEMORY = 0x00000200 |
| 26 | + |
| 27 | +JOB_OBJECT_LIMIT_WORKINGSET = 0x00000001 |
| 28 | + |
| 29 | +JOB_OBJECT_LIMIT_PRESERVE_JOB_TIME = 0x00000040 # Preserves any job time limits you previously set. As long as this flag is set, you can establish a per-job time limit once, then alter other limits in subsequent calls. This flag cannot be used with JOB_OBJECT_LIMIT_JOB_TIME. |
| 30 | +JOB_OBJECT_LIMIT_JOB_TIME = 0x00000004 |
| 31 | +JOB_OBJECT_LIMIT_PROCESS_TIME = 0x00000002 |
| 32 | + |
| 33 | +JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000 |
| 34 | + |
| 35 | + |
| 36 | +# https://stackoverflow.com/questions/6266820/working-example-of-createjobobject-setinformationjobobject-pinvoke-in-net |
| 37 | +JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x00000008 |
| 38 | +JOB_OBJECT_LIMIT_AFFINITY = 0x00000010 |
| 39 | +JOB_OBJECT_LIMIT_BREAKAWAY_OK = 0x00000800 |
| 40 | +JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION = 0x00000400 |
| 41 | +JOB_OBJECT_LIMIT_JOB_MEMORY = 0x00000200 |
| 42 | +JOB_OBJECT_LIMIT_JOB_TIME = 0x00000004 |
| 43 | +JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x00002000 |
| 44 | +JOB_OBJECT_LIMIT_PRESERVE_JOB_TIME = 0x00000040 |
| 45 | +JOB_OBJECT_LIMIT_PRIORITY_CLASS = 0x00000020 |
| 46 | +JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x00000100 |
| 47 | +JOB_OBJECT_LIMIT_PROCESS_TIME = 0x00000002 |
| 48 | +JOB_OBJECT_LIMIT_SCHEDULING_CLASS = 0x00000080 |
| 49 | +JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK = 0x00001000 |
| 50 | +JOB_OBJECT_LIMIT_WORKINGSET = 0x00000001 |
| 51 | + |
| 52 | + |
| 53 | +class IO_COUNTERS(ctypes.Structure): |
| 54 | + _fields_ = [('ReadOperationCount', ctypes.c_uint64), |
| 55 | + ('WriteOperationCount', ctypes.c_uint64), |
| 56 | + ('OtherOperationCount', ctypes.c_uint64), |
| 57 | + ('ReadTransferCount', ctypes.c_uint64), |
| 58 | + ('WriteTransferCount', ctypes.c_uint64), |
| 59 | + ('OtherTransferCount', ctypes.c_uint64)] |
| 60 | + |
| 61 | + |
| 62 | +class JOBOBJECT_BASIC_LIMIT_INFORMATION(ctypes.Structure): |
| 63 | + _fields_ = [('PerProcessUserTimeLimit', ctypes.c_int64), |
| 64 | + ('PerJobUserTimeLimit', ctypes.c_int64), |
| 65 | + ('LimitFlags', ctypes.c_uint32), |
| 66 | + ('MinimumWorkingSetSize', ctypes.c_void_p), |
| 67 | + ('MaximumWorkingSetSize', ctypes.c_void_p), |
| 68 | + ('ActiveProcessLimit', ctypes.c_uint32), |
| 69 | + ('Affinity', ctypes.c_void_p), |
| 70 | + ('PriorityClass', ctypes.c_uint32), |
| 71 | + ('SchedulingClass', ctypes.c_uint32)] |
| 72 | + |
| 73 | + |
| 74 | +class JOBOBJECT_EXTENDED_LIMIT_INFORMATION(ctypes.Structure): |
| 75 | + _fields_ = [('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION), |
| 76 | + ('IoInfo', IO_COUNTERS), |
| 77 | + ('ProcessMemoryLimit', ctypes.c_void_p), |
| 78 | + ('JobMemoryLimit', ctypes.c_void_p), |
| 79 | + ('PeakProcessMemoryUsed', ctypes.c_void_p), |
| 80 | + ('PeakJobMemoryUsed', ctypes.c_void_p)] |
| 81 | + |
| 82 | +## https://msdn.microsoft.com/en-us/library/windows/desktop/hh448384(v=vs.85).aspx |
| 83 | +#class JOBOBJECT_CPU_RATE_CONTROL_INFORMATION(ctypes.Structure): |
| 84 | +# _fields_ = [('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION), |
| 85 | +# ('IoInfo', IO_COUNTERS), |
| 86 | +# ('ProcessMemoryLimit', ctypes.c_void_p), |
| 87 | +# ('JobMemoryLimit', ctypes.c_void_p), |
| 88 | +# ('PeakProcessMemoryUsed', ctypes.c_void_p), |
| 89 | +# ('PeakJobMemoryUsed', ctypes.c_void_p)] |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +# Set memory limit for process with specfied 'pid', to specified 'size' in bytes |
| 94 | +def set_mem_commit_limit(pid, size, min_free_swap = None, retry_count = 10): |
| 95 | + |
| 96 | + for i in range(0, retry_count): # assigning to job fails sometimes |
| 97 | + try: |
| 98 | + if (set_mem_commit_limit_worker(pid, size, min_free_swap)): |
| 99 | + break |
| 100 | + except Exception as msg: # possibly already assigned to job object? (this can be done only once) |
| 101 | + print(msg) |
| 102 | + pass |
| 103 | + |
| 104 | +#/ def set_mem_commit_limit(pid, size, min_free_swap, retry_count): |
| 105 | + |
| 106 | + |
| 107 | +def set_mem_commit_limit_worker(pid, size, min_free_swap = None): |
| 108 | + |
| 109 | + |
| 110 | + if min_free_swap: |
| 111 | + swap_status = psutil.swap_memory() |
| 112 | + max_commit_limit = swap_status.free - min_free_swap |
| 113 | + size = min(size, max_commit_limit) # NB! |
| 114 | + |
| 115 | + |
| 116 | + job_info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION() |
| 117 | + out_size = ctypes.c_uint32() |
| 118 | + |
| 119 | + |
| 120 | + # child processes inherit the job object so it is not necessary to check for the parent |
| 121 | + main_pid = os.getpid() # if __name__ == '__main__' else os.getppid() |
| 122 | + |
| 123 | + |
| 124 | + # https://msdn.microsoft.com/en-us/library/windows/desktop/ms682409(v=vs.85).aspx |
| 125 | + # If the object existed before the function call, the function returns a handle to the existing job object and GetLastError returns ERROR_ALREADY_EXISTS. |
| 126 | + job = ctypes.windll.kernel32.CreateJobObjectW(None, None) # NB! CreateJobObjectW not CreateJobObjectA |
| 127 | + # assert job != 0 |
| 128 | + |
| 129 | + success = ctypes.windll.kernel32.QueryInformationJobObject( |
| 130 | + job, |
| 131 | + JobObjectExtendedLimitInformation, |
| 132 | + ctypes.POINTER(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)(job_info), |
| 133 | + ctypes.sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION), |
| 134 | + ctypes.POINTER(ctypes.c_uint32)(out_size) |
| 135 | + ) |
| 136 | + # assert success |
| 137 | + |
| 138 | + |
| 139 | + #job_info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_PROCESS_MEMORY | JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE |
| 140 | + #job_info.ProcessMemoryLimit = size |
| 141 | + |
| 142 | + job_info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_JOB_MEMORY | JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE |
| 143 | + job_info.JobMemoryLimit = size |
| 144 | + |
| 145 | + # job_info.MaximumWorkingSetSize = size |
| 146 | + |
| 147 | + |
| 148 | + success = ctypes.windll.kernel32.SetInformationJobObject( |
| 149 | + job, |
| 150 | + JobObjectExtendedLimitInformation, |
| 151 | + ctypes.POINTER(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)(job_info), |
| 152 | + ctypes.sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION) |
| 153 | + ) |
| 154 | + # assert success |
| 155 | + |
| 156 | + process = ctypes.windll.kernel32.OpenProcess( |
| 157 | + PROCESS_SET_QUOTA | PROCESS_TERMINATE, |
| 158 | + False, pid |
| 159 | + ) |
| 160 | + # assert process != 0 |
| 161 | + |
| 162 | + success_assign = ctypes.windll.kernel32.AssignProcessToJobObject(job, process) |
| 163 | + # assert success |
| 164 | + |
| 165 | + #if __name__ != '__main__': # NB! due to JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE flag |
| 166 | + # success = ctypes.windll.kernel32.CloseHandle(job) |
| 167 | + # # assert success |
| 168 | + |
| 169 | + success = ctypes.windll.kernel32.CloseHandle(process) |
| 170 | + # assert success |
| 171 | + |
| 172 | + return success_assign |
| 173 | + |
| 174 | +#/ def set_mem_commit_limit(pid, size, min_free_swap): |
0 commit comments