Thread: [pywin32-checkins] pywin32/win32/Lib win32pdhutil.py,1.10,1.11
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2006-07-16 09:33:23
|
Update of /cvsroot/pywin32/pywin32/win32/Lib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19165 Modified Files: win32pdhutil.py Log Message: reindent to 4 spaces and wrap some long lines. Index: win32pdhutil.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/Lib/win32pdhutil.py,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** win32pdhutil.py 2 Dec 2005 07:52:23 -0000 1.10 --- win32pdhutil.py 16 Jul 2006 09:33:14 -0000 1.11 *************** *** 8,19 **** >>> win32pdhutil.FindPerformanceAttributesByName("python", counter="Virtual Bytes") [22278144] ! First example returns data which is not associated with any specific instance. ! ! The second example reads data for a specific instance - hence the list return - it would return one result for each instance of Python running. ! In general, it can be tricky finding exactly the "name" of the data you wish to query. ! Although you can use <om win32pdh.EnumObjectItems>(None,None,(eg)"Memory", -1) to do this, the easiest way is often to simply use PerfMon to find out the names. """ --- 8,19 ---- >>> win32pdhutil.FindPerformanceAttributesByName("python", counter="Virtual Bytes") [22278144] ! First example returns data which is not associated with any specific instance. ! ! The second example reads data for a specific instance - hence the list return - it would return one result for each instance of Python running. ! In general, it can be tricky finding exactly the "name" of the data you wish to query. ! Although you can use <om win32pdh.EnumObjectItems>(None,None,(eg)"Memory", -1) to do this, the easiest way is often to simply use PerfMon to find out the names. """ *************** *** 29,145 **** def find_pdh_counter_localized_name(english_name, machine_name = None): ! if not counter_english_map: ! import win32api, win32con ! counter_reg_value = win32api.RegQueryValueEx(win32con.HKEY_PERFORMANCE_DATA, "Counter 009") ! counter_list = counter_reg_value[0] ! for i in range(0, len(counter_list) - 1, 2): ! try: ! counter_id = int(counter_list[i]) ! except ValueError: ! continue ! counter_english_map[counter_list[i+1].lower()] = counter_id ! return win32pdh.LookupPerfNameByIndex(machine_name, counter_english_map[english_name.lower()]) ! def GetPerformanceAttributes(object, counter, instance = None, inum=-1, format = win32pdh.PDH_FMT_LONG, machine=None): ! # NOTE: Many counters require 2 samples to give accurate results, ! # including "% Processor Time" (as by definition, at any instant, a ! # thread's CPU usage is either 0 or 100). To read counters like this, ! # you should copy this function, but keep the counter open, and call ! # CollectQueryData() each time you need to know. ! # See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938 ! # and http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp ! # My older explanation for this was that the "AddCounter" process forced ! # the CPU to 100%, but the above makes more sense :) ! path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) ) ! hq = win32pdh.OpenQuery() ! try: ! hc = win32pdh.AddCounter(hq, path) ! try: ! win32pdh.CollectQueryData(hq) ! type, val = win32pdh.GetFormattedCounterValue(hc, format) ! return val ! finally: ! win32pdh.RemoveCounter(hc) ! finally: ! win32pdh.CloseQuery(hq) ! def FindPerformanceAttributesByName(instanceName, object = None, counter = None, format = win32pdh.PDH_FMT_LONG, machine = None, bRefresh=0): ! """Find peformance attributes by (case insensitive) instance name. ! ! Given a process name, return a list with the requested attributes. ! Most useful for returning a tuple of PIDs given a process name. ! """ ! if object is None: object = find_pdh_counter_localized_name("Process", machine) ! if counter is None: counter = find_pdh_counter_localized_name("ID Process", machine) ! if bRefresh: # PDH docs say this is how you do a refresh. ! win32pdh.EnumObjects(None, machine, 0, 1) ! instanceName = string.lower(instanceName) ! items, instances = win32pdh.EnumObjectItems(None,None,object, -1) ! # Track multiple instances. ! instance_dict = {} ! for instance in instances: ! try: ! instance_dict[instance] = instance_dict[instance] + 1 ! except KeyError: ! instance_dict[instance] = 0 ! ! ret = [] ! for instance, max_instances in instance_dict.items(): ! for inum in xrange(max_instances+1): ! if string.lower(instance) == instanceName: ! ret.append(GetPerformanceAttributes(object, counter, instance, inum, format, machine)) ! return ret def ShowAllProcesses(): ! object = "Process" ! items, instances = win32pdh.EnumObjectItems(None,None,object, win32pdh.PERF_DETAIL_WIZARD) ! # Need to track multiple instances of the same name. ! instance_dict = {} ! for instance in instances: ! try: ! instance_dict[instance] = instance_dict[instance] + 1 ! except KeyError: ! instance_dict[instance] = 0 ! ! # Bit of a hack to get useful info. ! items = ["ID Process"] + items[:5] ! print "Process Name", string.join(items,",") ! for instance, max_instances in instance_dict.items(): ! for inum in xrange(max_instances+1): ! hq = win32pdh.OpenQuery() ! hcs = [] ! for item in items: ! path = win32pdh.MakeCounterPath( (None,object,instance, None, inum, item) ) ! hcs.append(win32pdh.AddCounter(hq, path)) ! win32pdh.CollectQueryData(hq) ! # as per http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938, some "%" based ! # counters need two collections ! time.sleep(0.01) ! win32pdh.CollectQueryData(hq) ! print "%-15s\t" % (instance[:15]), ! for hc in hcs: ! type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG) ! print "%5d" % (val), ! win32pdh.RemoveCounter(hc) ! print ! win32pdh.CloseQuery(hq) def BrowseCallBackDemo(counter): ! machine, object, instance, parentInstance, index, counterName = \ ! win32pdh.ParseCounterPath(counter) ! result = GetPerformanceAttributes(object, counterName, instance, index, win32pdh.PDH_FMT_DOUBLE, machine) ! print "Value of '%s' is" % counter, result ! print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" % (counterName, object, machine, instance, index, parentInstance) ! def browse( callback = BrowseCallBackDemo, title="Python Browser", level=win32pdh.PERF_DETAIL_WIZARD): ! win32pdh.BrowseCounters(None,0, callback, level, title) if __name__=='__main__': ! ShowAllProcesses() ! # Show how to get a couple of attributes by name. ! print "Virtual Bytes = ", FindPerformanceAttributesByName("python", counter="Virtual Bytes") ! print "Available Bytes = ", GetPerformanceAttributes("Memory", "Available Bytes") ! # And a browser. ! print "Browsing for counters..." ! browse() --- 29,157 ---- def find_pdh_counter_localized_name(english_name, machine_name = None): ! if not counter_english_map: ! import win32api, win32con ! counter_reg_value = win32api.RegQueryValueEx(win32con.HKEY_PERFORMANCE_DATA, ! "Counter 009") ! counter_list = counter_reg_value[0] ! for i in range(0, len(counter_list) - 1, 2): ! try: ! counter_id = int(counter_list[i]) ! except ValueError: ! continue ! counter_english_map[counter_list[i+1].lower()] = counter_id ! return win32pdh.LookupPerfNameByIndex(machine_name, counter_english_map[english_name.lower()]) ! def GetPerformanceAttributes(object, counter, instance = None, inum=-1, ! format = win32pdh.PDH_FMT_LONG, machine=None): ! # NOTE: Many counters require 2 samples to give accurate results, ! # including "% Processor Time" (as by definition, at any instant, a ! # thread's CPU usage is either 0 or 100). To read counters like this, ! # you should copy this function, but keep the counter open, and call ! # CollectQueryData() each time you need to know. ! # See http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938 ! # and http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp ! # My older explanation for this was that the "AddCounter" process forced ! # the CPU to 100%, but the above makes more sense :) ! path = win32pdh.MakeCounterPath( (machine,object,instance, None, inum,counter) ) ! hq = win32pdh.OpenQuery() ! try: ! hc = win32pdh.AddCounter(hq, path) ! try: ! win32pdh.CollectQueryData(hq) ! type, val = win32pdh.GetFormattedCounterValue(hc, format) ! return val ! finally: ! win32pdh.RemoveCounter(hc) ! finally: ! win32pdh.CloseQuery(hq) ! def FindPerformanceAttributesByName(instanceName, object = None, ! counter = None, ! format = win32pdh.PDH_FMT_LONG, ! machine = None, bRefresh=0): ! """Find peformance attributes by (case insensitive) instance name. ! ! Given a process name, return a list with the requested attributes. ! Most useful for returning a tuple of PIDs given a process name. ! """ ! if object is None: object = find_pdh_counter_localized_name("Process", machine) ! if counter is None: counter = find_pdh_counter_localized_name("ID Process", machine) ! if bRefresh: # PDH docs say this is how you do a refresh. ! win32pdh.EnumObjects(None, machine, 0, 1) ! instanceName = string.lower(instanceName) ! items, instances = win32pdh.EnumObjectItems(None,None,object, -1) ! # Track multiple instances. ! instance_dict = {} ! for instance in instances: ! try: ! instance_dict[instance] = instance_dict[instance] + 1 ! except KeyError: ! instance_dict[instance] = 0 ! ! ret = [] ! for instance, max_instances in instance_dict.items(): ! for inum in xrange(max_instances+1): ! if string.lower(instance) == instanceName: ! ret.append(GetPerformanceAttributes(object, counter, ! instance, inum, format, ! machine)) ! return ret def ShowAllProcesses(): ! object = "Process" ! items, instances = win32pdh.EnumObjectItems(None,None,object, ! win32pdh.PERF_DETAIL_WIZARD) ! # Need to track multiple instances of the same name. ! instance_dict = {} ! for instance in instances: ! try: ! instance_dict[instance] = instance_dict[instance] + 1 ! except KeyError: ! instance_dict[instance] = 0 ! ! # Bit of a hack to get useful info. ! items = ["ID Process"] + items[:5] ! print "Process Name", string.join(items,",") ! for instance, max_instances in instance_dict.items(): ! for inum in xrange(max_instances+1): ! hq = win32pdh.OpenQuery() ! hcs = [] ! for item in items: ! path = win32pdh.MakeCounterPath( (None,object,instance, ! None, inum, item) ) ! hcs.append(win32pdh.AddCounter(hq, path)) ! win32pdh.CollectQueryData(hq) ! # as per http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262938, some "%" based ! # counters need two collections ! time.sleep(0.01) ! win32pdh.CollectQueryData(hq) ! print "%-15s\t" % (instance[:15]), ! for hc in hcs: ! type, val = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG) ! print "%5d" % (val), ! win32pdh.RemoveCounter(hc) ! print ! win32pdh.CloseQuery(hq) def BrowseCallBackDemo(counter): ! machine, object, instance, parentInstance, index, counterName = \ ! win32pdh.ParseCounterPath(counter) ! result = GetPerformanceAttributes(object, counterName, instance, index, ! win32pdh.PDH_FMT_DOUBLE, machine) ! print "Value of '%s' is" % counter, result ! print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" \ ! % (counterName, object, machine, instance, index, parentInstance) ! def browse(callback = BrowseCallBackDemo, title="Python Browser", ! level=win32pdh.PERF_DETAIL_WIZARD): ! win32pdh.BrowseCounters(None,0, callback, level, title) if __name__=='__main__': ! ShowAllProcesses() ! # Show how to get a couple of attributes by name. ! print "Virtual Bytes = ", FindPerformanceAttributesByName("python", counter="Virtual Bytes") ! print "Available Bytes = ", GetPerformanceAttributes("Memory", "Available Bytes") ! # And a browser. ! print "Browsing for counters..." ! browse() |