home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / gui / native / win / process.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  5.9 KB  |  189 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from util.ffi import cimport
  5. from ctypes.wintypes import DWORD, HMODULE, MAX_PATH
  6. from ctypes import byref, WinError, sizeof, create_unicode_buffer, Structure, c_ulong
  7. import os
  8. SIZE_T = c_ulong
  9. cimport(Psapi = [
  10.     'GetModuleBaseNameW',
  11.     'EnumProcesses',
  12.     'EnumProcessModules',
  13.     'GetModuleBaseNameW',
  14.     'GetProcessMemoryInfo',
  15.     'EmptyWorkingSet'], kernel32 = [
  16.     'OpenProcess',
  17.     'SetProcessWorkingSetSize',
  18.     'CloseHandle',
  19.     'GetCurrentProcess',
  20.     'GetCurrentThread',
  21.     'SetThreadPriority'], user32 = [
  22.     'GetGuiResources'])
  23. N = 500
  24. dword_array = DWORD * N
  25. aProcesses = dword_array()
  26. cBytes = DWORD()
  27. PROCESS_QUERY_INFORMATION = 1024
  28. PROCESS_VM_READ = 16
  29. PROCESS_SET_QUOTA = 256
  30. OPEN_PROCESS_FLAGS = PROCESS_QUERY_INFORMATION | PROCESS_VM_READ
  31. szProcessName = create_unicode_buffer(MAX_PATH)
  32. hMod = HMODULE()
  33. cBytesNeeded = DWORD()
  34.  
  35. class PROCESS_MEMORY_COUNTERS(Structure):
  36.     _fields_ = [
  37.         ('cb', DWORD),
  38.         ('PageFaultCount', DWORD),
  39.         ('PeakWorkingSetSize', SIZE_T),
  40.         ('WorkingSetSize', SIZE_T),
  41.         ('QuotaPeakPagedPoolUsage', SIZE_T),
  42.         ('QuotaPagedPoolUsage', SIZE_T),
  43.         ('QuotaPeakNonPagedPoolUsage', SIZE_T),
  44.         ('QuotaNonPagedPoolUsage', SIZE_T),
  45.         ('PagefileUsage', SIZE_T),
  46.         ('PeakPagefileUsage', SIZE_T)]
  47.  
  48.  
  49. class PROCESS_MEMORY_COUNTERS_EX(Structure):
  50.     _fields_ = [
  51.         ('cb', DWORD),
  52.         ('PageFaultCount', DWORD),
  53.         ('PeakWorkingSetSize', SIZE_T),
  54.         ('WorkingSetSize', SIZE_T),
  55.         ('QuotaPeakPagedPoolUsage', SIZE_T),
  56.         ('QuotaPagedPoolUsage', SIZE_T),
  57.         ('QuotaPeakNonPagedPoolUsage', SIZE_T),
  58.         ('QuotaNonPagedPoolUsage', SIZE_T),
  59.         ('PagefileUsage', SIZE_T),
  60.         ('PeakPagefileUsage', SIZE_T),
  61.         ('PrivateUsage', SIZE_T)]
  62.  
  63.  
  64. def page_out_ram():
  65.     print 'paging out ram'
  66.     hProcess = OpenProcess(PROCESS_SET_QUOTA, False, os.getpid())
  67.     if not hProcess:
  68.         raise WinError()
  69.     hProcess
  70.     if not SetProcessWorkingSetSize(hProcess, -1, -1):
  71.         raise WinError()
  72.     SetProcessWorkingSetSize(hProcess, -1, -1)
  73.     CloseHandle(hProcess)
  74.  
  75.  
  76. def process_list():
  77.     if not EnumProcesses(byref(aProcesses), N, byref(cBytes)):
  78.         raise WinError
  79.     EnumProcesses(byref(aProcesses), N, byref(cBytes))
  80.     processes = []
  81.     for processID in aProcesses:
  82.         if processID == 0:
  83.             continue
  84.         
  85.         hProcess = OpenProcess(OPEN_PROCESS_FLAGS, False, processID)
  86.         if not hProcess:
  87.             continue
  88.         
  89.         
  90.         try:
  91.             if EnumProcessModules(hProcess, byref(hMod), sizeof(hMod), byref(cBytesNeeded)):
  92.                 if GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName) / 2):
  93.                     processes.append(szProcessName.value)
  94.                 
  95.         finally:
  96.             CloseHandle(hProcess)
  97.  
  98.     
  99.     return processes
  100.  
  101. _no_pmc_ex = False
  102. pmc = PROCESS_MEMORY_COUNTERS_EX()
  103.  
  104. def memory_info(processID = None, p = False):
  105.     global _no_pmc_ex
  106.     if processID is None:
  107.         processID = os.getpid()
  108.     
  109.     hProcess = OpenProcess(OPEN_PROCESS_FLAGS, False, processID)
  110.     if not hProcess:
  111.         raise WinError()
  112.     hProcess
  113.     if _no_pmc_ex or not GetProcessMemoryInfo(hProcess, byref(pmc), sizeof(PROCESS_MEMORY_COUNTERS_EX)):
  114.         _no_pmc_ex = True
  115.         ret = GetProcessMemoryInfo(hProcess, byref(pmc), sizeof(PROCESS_MEMORY_COUNTERS_EX))
  116.     else:
  117.         ret = True
  118.     if ret:
  119.         if p:
  120.             print '\tPageFaultCount:             %10d' % pmc.PageFaultCount
  121.             print '\tPeakWorkingSetSize:         %10d' % pmc.PeakWorkingSetSize
  122.             print '\tWorkingSetSize:             %10d' % pmc.WorkingSetSize
  123.             print '\tQuotaPeakPagedPoolUsage:    %10d' % pmc.QuotaPeakPagedPoolUsage
  124.             print '\tQuotaPagedPoolUsage:        %10d' % pmc.QuotaPagedPoolUsage
  125.             print '\tQuotaPeakNonPagedPoolUsage: %10d' % pmc.QuotaPeakNonPagedPoolUsage
  126.             print '\tQuotaNonPagedPoolUsage:     %10d' % pmc.QuotaNonPagedPoolUsage
  127.             print '\tPagefileUsage:              %10d' % pmc.PagefileUsage
  128.             print '\tPeakPagefileUsage:          %10d' % pmc.PeakPagefileUsage
  129.             print '\tPrivateUsage:               %10d' % pmc.PrivateUsage
  130.         
  131.     else:
  132.         raise WinError()
  133.     ret(hProcess)
  134.     return pmc
  135.  
  136.  
  137. def str_meminfo(pmc):
  138.     return None('\r\n'.join(filter, [
  139.         None,
  140.         '{',
  141.         "\t'PageFaultCount':             %10d," % pmc.PageFaultCount,
  142.         "\t'PeakWorkingSetSize':         %10d," % pmc.PeakWorkingSetSize,
  143.         "\t'WorkingSetSize':             %10d," % pmc.WorkingSetSize,
  144.         "\t'QuotaPeakPagedPoolUsage':    %10d," % pmc.QuotaPeakPagedPoolUsage,
  145.         "\t'QuotaPagedPoolUsage':        %10d," % pmc.QuotaPagedPoolUsage,
  146.         "\t'QuotaPeakNonPagedPoolUsage': %10d," % pmc.QuotaPeakNonPagedPoolUsage,
  147.         "\t'QuotaNonPagedPoolUsage':     %10d," % pmc.QuotaNonPagedPoolUsage,
  148.         "\t'PagefileUsage':              %10d," % pmc.PagefileUsage,
  149.         "\t'PeakPagefileUsage':          %10d," % pmc.PeakPagefileUsage if isinstance(pmc, PROCESS_MEMORY_COUNTERS_EX) else '',
  150.         '}']))
  151.  
  152. GR_GDIOBJECTS = 0
  153. GR_USEROBJECTS = 1
  154. current_process_handle = GetCurrentProcess()
  155.  
  156. def count_gdi_objects():
  157.     return GetGuiResources(current_process_handle, GR_GDIOBJECTS)
  158.  
  159.  
  160. def count_user_objects():
  161.     return GetGuiResources(current_process_handle, GR_USEROBJECTS)
  162.  
  163. THREAD_MODE_BACKGROUND_BEGIN = 65536
  164. THREAD_MODE_BACKGROUND_END = 131072
  165. THREAD_PRIORITY_ABOVE_NORMAL = 1
  166. THREAD_PRIORITY_BELOW_NORMAL = -1
  167. THREAD_PRIORITY_HIGHEST = 2
  168. THREAD_PRIORITY_IDLE = -15
  169. THREAD_PRIORITY_LOWEST = -2
  170. THREAD_PRIORITY_NORMAL = 0
  171. THREAD_PRIORITY_TIME_CRITICAL = 15
  172.  
  173. def set_bgthread(background = True, thread_handle = None):
  174.     if thread_handle is None:
  175.         thread_handle = GetCurrentThread()
  176.     
  177.     priority = None if background else THREAD_PRIORITY_NORMAL
  178.     print thread_handle, priority
  179.     if not SetThreadPriority(thread_handle, priority):
  180.         raise WinError()
  181.     SetThreadPriority(thread_handle, priority)
  182.  
  183. if __name__ == '__main__':
  184.     from time import clock
  185.     now = clock()
  186.     print process_list()
  187.     print clock() - now
  188.  
  189.