home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / win32pdhutil.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  4.2 KB  |  135 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import win32pdh
  5. import time
  6. error = win32pdh.error
  7. counter_english_map = { }
  8.  
  9. def find_pdh_counter_localized_name(english_name, machine_name = None):
  10.     if not counter_english_map:
  11.         import win32api
  12.         import win32con
  13.         counter_reg_value = win32api.RegQueryValueEx(win32con.HKEY_PERFORMANCE_DATA, 'Counter 009')
  14.         counter_list = counter_reg_value[0]
  15.         for i in xrange(0, len(counter_list) - 1, 2):
  16.             
  17.             try:
  18.                 counter_id = int(counter_list[i])
  19.             except ValueError:
  20.                 continue
  21.  
  22.             counter_english_map[counter_list[i + 1].lower()] = counter_id
  23.         
  24.     return win32pdh.LookupPerfNameByIndex(machine_name, counter_english_map[english_name.lower()])
  25.  
  26.  
  27. def GetPerformanceAttributes(object, counter, instance = None, inum = -1, format = win32pdh.PDH_FMT_LONG, machine = None):
  28.     path = win32pdh.MakeCounterPath((machine, object, instance, None, inum, counter))
  29.     hq = win32pdh.OpenQuery()
  30.     
  31.     try:
  32.         hc = win32pdh.AddCounter(hq, path)
  33.         
  34.         try:
  35.             win32pdh.CollectQueryData(hq)
  36.             (type, val) = win32pdh.GetFormattedCounterValue(hc, format)
  37.             return val
  38.         finally:
  39.             win32pdh.RemoveCounter(hc)
  40.  
  41.     finally:
  42.         win32pdh.CloseQuery(hq)
  43.  
  44.  
  45.  
  46. def FindPerformanceAttributesByName(instanceName, object = None, counter = None, format = win32pdh.PDH_FMT_LONG, machine = None, bRefresh = 0):
  47.     if object is None:
  48.         object = find_pdh_counter_localized_name('Process', machine)
  49.     if counter is None:
  50.         counter = find_pdh_counter_localized_name('ID Process', machine)
  51.     if bRefresh:
  52.         win32pdh.EnumObjects(None, machine, 0, 1)
  53.     instanceName = instanceName.lower()
  54.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, -1)
  55.     instance_dict = { }
  56.     for instance in instances:
  57.         
  58.         try:
  59.             instance_dict[instance] = instance_dict[instance] + 1
  60.         continue
  61.         except KeyError:
  62.             instance_dict[instance] = 0
  63.             continue
  64.         
  65.  
  66.     
  67.     ret = []
  68.     for instance, max_instances in instance_dict.iteritems():
  69.         for inum in xrange(max_instances + 1):
  70.             if instance.lower() == instanceName:
  71.                 ret.append(GetPerformanceAttributes(object, counter, instance, inum, format, machine))
  72.                 continue
  73.     
  74.     return ret
  75.  
  76.  
  77. def ShowAllProcesses():
  78.     object = find_pdh_counter_localized_name('Process')
  79.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD)
  80.     instance_dict = { }
  81.     for instance in instances:
  82.         
  83.         try:
  84.             instance_dict[instance] = instance_dict[instance] + 1
  85.         continue
  86.         except KeyError:
  87.             instance_dict[instance] = 0
  88.             continue
  89.         
  90.  
  91.     
  92.     items = [
  93.         find_pdh_counter_localized_name('ID Process')] + items[:5]
  94.     print 'Process Name', ','.join(items)
  95.     for instance, max_instances in instance_dict.iteritems():
  96.         for inum in xrange(max_instances + 1):
  97.             hq = win32pdh.OpenQuery()
  98.             hcs = []
  99.             for item in items:
  100.                 path = win32pdh.MakeCounterPath((None, object, instance, None, inum, item))
  101.                 hcs.append(win32pdh.AddCounter(hq, path))
  102.             
  103.             win32pdh.CollectQueryData(hq)
  104.             time.sleep(0.01)
  105.             win32pdh.CollectQueryData(hq)
  106.             print '%-15s\t' % instance[:15],
  107.             for hc in hcs:
  108.                 (type, val) = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
  109.                 print '%5d' % val,
  110.                 win32pdh.RemoveCounter(hc)
  111.             
  112.             print 
  113.             win32pdh.CloseQuery(hq)
  114.         
  115.     
  116.  
  117.  
  118. def BrowseCallBackDemo(counter):
  119.     (machine, object, instance, parentInstance, index, counterName) = win32pdh.ParseCounterPath(counter)
  120.     result = GetPerformanceAttributes(object, counterName, instance, index, win32pdh.PDH_FMT_DOUBLE, machine)
  121.     print "Value of '%s' is" % counter, result
  122.     print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" % (counterName, object, machine, instance, index, parentInstance)
  123.  
  124.  
  125. def browse(callback = BrowseCallBackDemo, title = 'Python Browser', level = win32pdh.PERF_DETAIL_WIZARD):
  126.     win32pdh.BrowseCounters(None, 0, callback, level, title)
  127.  
  128. if __name__ == '__main__':
  129.     ShowAllProcesses()
  130.     counter = find_pdh_counter_localized_name('Virtual Bytes')
  131.     print 'Virtual Bytes = ', FindPerformanceAttributesByName('python', counter = counter)
  132.     print 'Available Bytes = ', GetPerformanceAttributes(find_pdh_counter_localized_name('Memory'), find_pdh_counter_localized_name('Available Bytes'))
  133.     print 'Browsing for counters...'
  134.     browse()
  135.