home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2545 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.3 KB  |  143 lines

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