2013-03-27 48 views
1

覺得這是我的第一個問題我也問過在這裏通常會找到我需要的所有答案,聚集了GDI和用戶對象性能指標(所以在此先感謝)我如何使用python

確定我的問題,我已經寫一個將在線程中監控一個進程並將結果輸出到一個csv文件供以後使用的python程序。此代碼運行良好我正在使用win32pdhutil計數器和WMI,Win32_PerfRawData_PerfProc_Process CPU%時間。我現在被要求監視一個WPF應用程序,特別是監視用戶對象和GDI對象。

這是我遇到問題的地方,就是我似乎無法找到任何python支持來收集這兩個計數器上的指標。這兩個計數器在任務管理器中很容易獲得,我覺得很奇怪,這兩個計數器的信息很少。我專門研究收集這些信息以查看是否有內存泄漏,我不想在已安裝的python以外的其他系統上安裝任何其他內容。請幫助您尋找解決方案。

我使用python 3.3.1,這將在Windows平臺上(主要是Win7和Win8的) 這是我用來收集數據

def gatherIt(self,whoIt,whatIt,type,wiggle,process_info2): 
    #this is the data gathering function thing 
    data=0.0 
    data1="wobble" 
    if type=="counter": 
     #gather data according to the attibutes 
     try: 
      data = win32pdhutil.FindPerformanceAttributesByName(whoIt, counter=whatIt) 
     except: 
      #a problem occoured with process not being there not being there.... 
      data1="N/A" 

    elif type=="cpu": 
     try: 
      process_info={}#used in the gather CPU bassed on service 
      for x in range(2): 
       for procP in wiggle.Win32_PerfRawData_PerfProc_Process(name=whoIt): 
        n1 = int(procP.PercentProcessorTime) 
        d1 = int(procP.Timestamp_Sys100NS) 
        #need to get the process id to change per cpu look... 
        n0, d0 = process_info.get (whoIt, (0, 0))  
        try: 
         percent_processor_time = (float (n1 - n0)/float (d1 - d0)) *100.0 
         #print whoIt, percent_processor_time 
        except ZeroDivisionError: 
         percent_processor_time = 0.0 
        # pass back the n0 and d0 
        process_info[whoIt] = (n1, d1) 
       #end for loop (this should take into account multiple cpu's) 
      # end for range to allow for a current cpu time rather that cpu percent over sampleint 
      if percent_processor_time==0.0: 
       data=0.0 
      else: 
       data=percent_processor_time 
     except: 
      data1="N/A" 

    else: 
     #we have done something wrong so data =0 
     data1="N/A" 
    #endif 
    if data == "[]": 
     data=0.0 
     data1="N/A" 
    if data == "" : 
     data=0.0 
     data1="N/A" 
    if data == " ": 
     data=0.0 
     data1="N/A" 
    if data1!="wobble" and data==0.0: 
     #we have not got the result we were expecting so add a n/a 
     data=data1 
    return data 

歡呼

代碼運行編輯爲正確的CPU時機問題,如果有人試圖運行它:D

+0

哇,沒有人能解決這個問題,它比我想象的更困難... – trevellyon 2013-04-10 12:42:37

回答

2

所以經過漫長的搜索,我能夠搗碎一些東西在一起,讓我需要的信息。

import time 
from ctypes import * 
from ctypes.wintypes import * 
import win32pdh 

# with help from here http://coding.derkeiler.com/Archive/Python/comp.lang.python/2007-10/msg00717.html 
# the following has been mashed together to get the info needed 

def GetProcessID(name): 
    object = "Process" 
    items, instances = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD) 
    val = None 
    if name in instances : 
     tenQuery = win32pdh.OpenQuery() 
     tenarray = [ ] 
     item = "ID Process" 
     path = win32pdh.MakeCounterPath((None, object, name, None, 0, item)) 
     tenarray.append(win32pdh.AddCounter(tenQuery, path)) 
     win32pdh.CollectQueryData(tenQuery) 
     time.sleep(0.01) 
     win32pdh.CollectQueryData(tenQuery) 
     for tencounter in tenarray: 
      type, val = win32pdh.GetFormattedCounterValue(tencounter, win32pdh.PDH_FMT_LONG) 
      win32pdh.RemoveCounter(tencounter) 
     win32pdh.CloseQuery(tenQuery) 
    return val 

processIDs = GetProcessID('OUTLOOK') # Remember this is case sensitive 
PQI = 0x400 
#open a handle on to the process so that we can query it 
OpenProcessHandle = windll.kernel32.OpenProcess(PQI, 0, processIDs) 
# OK so now we have opened the process now we want to query it 
GR_GDIOBJECTS, GR_USEROBJECTS = 0, 1 
print(windll.user32.GetGuiResources(OpenProcessHandle, GR_GDIOBJECTS)) 
print(windll.user32.GetGuiResources(OpenProcessHandle, GR_USEROBJECTS)) 
#so we have what we want we now close the process handle 
windll.kernel32.CloseHandle(OpenProcessHandle) 

希望幫助