| 91 | if os.name == 'nt': |
| 92 | # Code "stolen" from enthought/debug/memusage.py |
| 93 | def GetPerformanceAttributes(object, counter, instance=None, |
| 94 | inum=-1, format=None, machine=None): |
| 95 | # NOTE: Many counters require 2 samples to give accurate results, |
| 96 | # including "% Processor Time" (as by definition, at any instant, a |
| 97 | # thread's CPU usage is either 0 or 100). To read counters like this, |
| 98 | # you should copy this function, but keep the counter open, and call |
| 99 | # CollectQueryData() each time you need to know. |
| 100 | # See http://msdn.microsoft.com/library/en-us/dnperfmo/html/perfmonpt2.asp (dead link) |
| 101 | # My older explanation for this was that the "AddCounter" process |
| 102 | # forced the CPU to 100%, but the above makes more sense :) |
| 103 | import win32pdh |
| 104 | if format is None: |
| 105 | format = win32pdh.PDH_FMT_LONG |
| 106 | path = win32pdh.MakeCounterPath( (machine, object, instance, None, |
| 107 | inum, counter)) |
| 108 | hq = win32pdh.OpenQuery() |
| 109 | try: |
| 110 | hc = win32pdh.AddCounter(hq, path) |
| 111 | try: |
| 112 | win32pdh.CollectQueryData(hq) |
| 113 | type, val = win32pdh.GetFormattedCounterValue(hc, format) |
| 114 | return val |
| 115 | finally: |
| 116 | win32pdh.RemoveCounter(hc) |
| 117 | finally: |
| 118 | win32pdh.CloseQuery(hq) |
| 119 | |
| 120 | def memusage(processName="python", instance=0): |
| 121 | # from win32pdhutil, part of the win32all package |