home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / native / win / process.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  5.8 KB  |  181 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  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.     
  70.     if not SetProcessWorkingSetSize(hProcess, -1, -1):
  71.         raise WinError()
  72.     
  73.     CloseHandle(hProcess)
  74.  
  75.  
  76. def process_list():
  77.     if not EnumProcesses(byref(aProcesses), N, byref(cBytes)):
  78.         raise WinError
  79.     
  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.  
  102. def memory_info(processID = None, p = False):
  103.     if processID is None:
  104.         processID = os.getpid()
  105.     
  106.     hProcess = OpenProcess(OPEN_PROCESS_FLAGS, False, processID)
  107.     if not hProcess:
  108.         raise WinError()
  109.     
  110.     pmc = PROCESS_MEMORY_COUNTERS()
  111.     if GetProcessMemoryInfo(hProcess, byref(pmc), sizeof(PROCESS_MEMORY_COUNTERS)):
  112.         if p:
  113.             print '\tPageFaultCount:             %10d' % pmc.PageFaultCount
  114.             print '\tPeakWorkingSetSize:         %10d' % pmc.PeakWorkingSetSize
  115.             print '\tWorkingSetSize:             %10d' % pmc.WorkingSetSize
  116.             print '\tQuotaPeakPagedPoolUsage:    %10d' % pmc.QuotaPeakPagedPoolUsage
  117.             print '\tQuotaPagedPoolUsage:        %10d' % pmc.QuotaPagedPoolUsage
  118.             print '\tQuotaPeakNonPagedPoolUsage: %10d' % pmc.QuotaPeakNonPagedPoolUsage
  119.             print '\tQuotaNonPagedPoolUsage:     %10d' % pmc.QuotaNonPagedPoolUsage
  120.             print '\tPagefileUsage:              %10d' % pmc.PagefileUsage
  121.             print '\tPeakPagefileUsage:          %10d' % pmc.PeakPagefileUsage
  122.         
  123.     else:
  124.         raise WinError()
  125.     CloseHandle(hProcess)
  126.     return pmc
  127.  
  128.  
  129. def str_meminfo(pmc):
  130.     return None('\r\n'.join(filter, [
  131.         None,
  132.         '{',
  133.         "\t'PageFaultCount':             %10d," % pmc.PageFaultCount,
  134.         "\t'PeakWorkingSetSize':         %10d," % pmc.PeakWorkingSetSize,
  135.         "\t'WorkingSetSize':             %10d," % pmc.WorkingSetSize,
  136.         "\t'QuotaPeakPagedPoolUsage':    %10d," % pmc.QuotaPeakPagedPoolUsage,
  137.         "\t'QuotaPagedPoolUsage':        %10d," % pmc.QuotaPagedPoolUsage,
  138.         "\t'QuotaPeakNonPagedPoolUsage': %10d," % pmc.QuotaPeakNonPagedPoolUsage,
  139.         "\t'QuotaNonPagedPoolUsage':     %10d," % pmc.QuotaNonPagedPoolUsage,
  140.         "\t'PagefileUsage':              %10d," % pmc.PagefileUsage,
  141.         "\t'PeakPagefileUsage':          %10d," % pmc.PeakPagefileUsage if isinstance(pmc, PROCESS_MEMORY_COUNTERS_EX) else '',
  142.         '}']))
  143.  
  144. GR_GDIOBJECTS = 0
  145. GR_USEROBJECTS = 1
  146. current_process_handle = GetCurrentProcess()
  147.  
  148. def count_gdi_objects():
  149.     return GetGuiResources(current_process_handle, GR_GDIOBJECTS)
  150.  
  151.  
  152. def count_user_objects():
  153.     return GetGuiResources(current_process_handle, GR_USEROBJECTS)
  154.  
  155. THREAD_MODE_BACKGROUND_BEGIN = 65536
  156. THREAD_MODE_BACKGROUND_END = 131072
  157. THREAD_PRIORITY_ABOVE_NORMAL = 1
  158. THREAD_PRIORITY_BELOW_NORMAL = -1
  159. THREAD_PRIORITY_HIGHEST = 2
  160. THREAD_PRIORITY_IDLE = -15
  161. THREAD_PRIORITY_LOWEST = -2
  162. THREAD_PRIORITY_NORMAL = 0
  163. THREAD_PRIORITY_TIME_CRITICAL = 15
  164.  
  165. def set_bgthread(background = True, thread_handle = None):
  166.     if thread_handle is None:
  167.         thread_handle = GetCurrentThread()
  168.     
  169.     priority = None if background else THREAD_PRIORITY_NORMAL
  170.     print thread_handle, priority
  171.     if not SetThreadPriority(thread_handle, priority):
  172.         raise WinError()
  173.     
  174.  
  175. if __name__ == '__main__':
  176.     from time import clock
  177.     now = clock()
  178.     print process_list()
  179.     print clock() - now
  180.  
  181.