a oe@sdZddlZddlZejZiZdddZddejdfddZddejddfdd Zd d Z d d Z e dej fddZ e dkre edZededededeededede dS)aUtilities for the win32 Performance Data Helper module Example: To get a single bit of data: >>> import win32pdhutil >>> win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes") 6053888 >>> 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 (None,None,(eg)"Memory", -1) to do this, the easiest way is often to simply use PerfMon to find out the names. Nc Cstsddl}ddl}||jd}|d}tdt|ddD]@}zt||}WntyhYq>Yn0|t||d <q>t |t| S)Nrz Counter 009) counter_english_mapwin32apiwin32conZRegQueryValueExHKEY_PERFORMANCE_DATArangelenint ValueErrorlowerwin32pdhZLookupPerfNameByIndex)Z english_nameZ machine_namerrZcounter_reg_valueZ counter_listiZ counter_idr7C:\Program Files\Certbot\pkgs\win32\lib\win32pdhutil.pyfind_pdh_counter_localized_name!s    rc Cst|||d||f}t}z^t||}z6t|t||\} } | Wt|Wt|St|0Wt|n t|0dS)N)r MakeCounterPath OpenQuery AddCounterCollectQueryDataGetFormattedCounterValue RemoveCounter CloseQuery) objectcounterinstanceinumformatmachinepathhqhctypevalrrrGetPerformanceAttributes5s     r%c Cs|durtd|}|dur$td|}|r8td|dd|}tdd|d\}}i}|D]4} z|| d|| <Wq\tyd|| <Yq\0q\g} |D]>\} } t| dD](} | |kr| t ||| | ||qq| S)zFind performance 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. NProcess ID Processrrr) rr Z EnumObjectsr EnumObjectItemsKeyErroritemsrappendr%) Z instanceNamerrrrZbRefreshr* instances instance_dictrret max_instancesrrrrFindPerformanceAttributesByNameOs0      r0c Csftd}tdd|tj\}}i}|D]4}z||d||<Wq&tyXd||<Yq&0q&tdg|dd}tdd||D]\}}t|dD]}t }g}|D],} t d||d|| f} | t || qt |tdt |td |dd d d |D]2} t| tj\} } td | d d t| qtt|qqdS)Nr&rrr'z Process Name,g{Gz?z%-15s  )endz%5d)rr r(PERF_DETAIL_WIZARDr)printjoinr*rrrr+rrtimesleepr PDH_FMT_LONGrr)rr*r,r-rr/rr!Zhcsitemr r"r#r$rrrShowAllProcessesxs>     r=c Cs^|D]T}t|\}}}}}}t||||tj|}td||td||||||fqdS)NzValue of '%s' iszDAdded '%s' on object '%s' (machine %s), instance %s(%d)-parent of %sr)r ZParseCounterPathr%ZPDH_FMT_DOUBLEr7) ZcountersrrrrZparentInstanceindexZ counterNameresultrrrBrowseCallBackDemos&r@zPython BrowsercCstjdd|||dddS)NrT)ZReturnMultiple)r ZBrowseCounters)callbacktitlelevelrrrbrowsesrD__main__z Virtual ByteszVirtual Bytes = Zpython)rzAvailable Bytes = ZMemoryzAvailable ByteszBrowsing for counters...)N)__doc__r9r errorrrr;r%r0r=r@r6rD__name__rr7rrrrsD   )+