eaiovnaovbqoebvqoeavibavo B  fj@sdZddlZddlZddlZddlZddlZddlmZddlm Z ddgZ Gddde eZ GdddZ Gd d d Zd d Zd dZddZddZddZddZddZedkrddlZy ddlZWnek rYnXGdddejZeejdkrejdZndZyTeeZx"ejddD]Z e!e q2We"dej#de$e"d ej#dWne%k rYnXdS)!z3Class for printing reports on profiled python code.N)Enum) cmp_to_keyStatsSortKeyc@s8eZdZdZdZdZdZdZdZdZ dZ d Z d d Z d S) r)callsncalls) cumulativecumtime)filenamemodulelinenamenflpcallsstdname)timetottimecGs>t|}|d|_x|ddD]}||j|<q"W||_|S)Nr)str__new___value__value2member_map_Z _all_values)clsvaluesobjZ other_valuer+/opt/alt/python37/lib64/python3.7/pstats.pyr-s   zSortKey.__new__N) __name__ __module__ __qualname__ZCALLSZ CUMULATIVEZFILENAMEZLINENAMEZNFLZPCALLSZSTDNAMEZTIMErrrrrr"sc@seZdZdZddddZddZdd Zd d Zd d ZddZ dddddddddddddd Z ddZ ddZ ddZ d d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd7d1d2Zd3d4Zd5d6ZdS)8raThis class is used for creating reports from data generated by the Profile class. It is a "friend" of that class, and imports data either by direct access to members of Profile class, or by reading in a dictionary that was emitted (via marshal) from the Profile class. The big change from the previous Profiler (in terms of raw functionality) is that an "add()" method has been provided to combine Stats from several distinct profile runs. Both the constructor and the add() method now take arbitrarily many file names as arguments. All the print methods now take an argument that indicates how many lines to print. If the arg is a floating point number between 0 and 1.0, then it is taken as a decimal percentage of the available lines to be printed (e.g., .1 means print 10% of all available lines). If it is an integer, it is taken to mean the number of lines of data that you wish to have printed. The sort_stats() method now processes some additional options (i.e., in addition to the old -1, 0, 1, or 2 that are respectively interpreted as 'stdname', 'calls', 'time', and 'cumulative'). It takes either an arbitrary number of quoted strings or SortKey enum to select the sort order. For example sort_stats('time', 'name') or sort_stats(SortKey.TIME, SortKey.NAME) sorts on the major key of 'internal function time', and on the minor key of 'the name of the function'. Look at the two tables in sort_stats() and get_sort_arg_defs(self) for more examples. All methods return self, so you can string together commands like: Stats('foo', 'goo').strip_dirs().sort_stats('calls'). print_stats(5).print_callers(5) N)streamcGsF|ptj|_t|sd}n|d}|dd}|||j|dS)Nrr)sysstdoutr!leninitadd)selfr!argsargrrr__init__Ys   zStats.__init__cCsd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ | |y | Wn8t k rtd|jrx|jdnd|jdYnXdS)NrzInvalid timing data %s)file) all_calleesfilesfcn_listtotal_tt total_calls prim_calls max_name_lenset top_levelstats sort_arg_dict load_statsget_top_level_stats Exceptionprintr!)r'r)rrrr%cs"  z Stats.initc Cs|dkri|_dSt|trxt|d}t||_WdQRXy"t|}t |j d|}Wn YnX|g|_ n t |dr| |j|_i|_|jstd|j|fdS)Nrbz create_statsz.Cannot create or construct a %r object from %r)r7 isinstanceropenmarshalloadosstatrZctimest_mtimer/hasattrr> TypeError __class__)r'r)fZ file_statsrrrr9vs(     zStats.load_statscCsx~|jD]p\}\}}}}}|j|7_|j|7_|j|7_d|kr\|j|tt||j kr tt||_ q WdS)N)ZjprofilerZprofiler) r7itemsr2r3r1r6r&r$func_std_stringr4)r'funcccncttctcallersrrrr:s zStats.get_top_level_statscGs|s|Sxt|D]}t|t|kr.t|}|j|j7_|j|j7_|j|j7_|j|j7_x|jD]}|j|qvW|j |j kr|j |_ d|_ xJ|j D]<\}}||j kr|j |}nddddif}t |||j |<qWqW|S)Nr)reversedtyperr/r2r3r1r6r&r4r0r7rJadd_func_stats)r'arg_listitemrLrDZ old_func_statrrrr&s(    z Stats.addc Cs(t|d}t|j|WdQRXdS)z:Write the profile data to a file we know how to load back.wbN)r@rAdumpr7)r'r rIrrr dump_statss zStats.dump_stats)))rr+z call count)))r+zcumulative time)))rz file name)))rz line number)))rz function name)))r]r)r[r)r\rzname/file/line)))rr+zprimitive call count)))rz standard name)))r+z internal time) rrr rr r r r rrrrrcCs|jszi|_}i}xP|jD]B\}}|}x4|r`|s8P||krJd||<P|||<|dd}q.Wq Wx|D] }||=qlW|jS)z)Expand all abbreviations that are unique.rNr+)r8sort_arg_dict_defaultrJ)r'dictZbad_listwordtupZfragmentrrrget_sort_arg_defss    zStats.get_sort_arg_defscGsl|sd|_|St|dkrBt|dtrBddddd|dg}n>t|dkrx0|ddD] }t|t|dkr\td q\W|}d }d |_d }xJ|D]B}t|tr|j }|||d}|j|||d7_d }qWg}xB|j D]4\}\} } } } } | | | | | f|t ||fqW|jtt|jd g|_}x|D]}| |dqPW|S)Nrrrrrr)r+rrr_r_zCan't have mixed argument typerr,z, )keyr+)r0r$r?intrSrGrd sort_typervaluer7rJappendrKsortr TupleCompcompare)r'Zfieldr)Z sort_arg_defsZ sort_tupleZ connectorrbZ stats_listrLrMrNrOrPrQr0tuplerrr sort_statss>      zStats.sort_statscCs|jr|j|S)N)r0reverse)r'rrr reverse_orders zStats.reverse_ordercCs|j}i|_}d}x|D]\}\}}}}} t|} tt| |krTtt| }i} x | D]\} } | | t| <qbW| |krt|| ||||| f|| <q||||| f|| <qW|j}t|_}x|D]}|t|qW||_ d|_ d|_ |S)Nr) r7rJfunc_strip_pathr$rKrTr6r5r&r4r0r.)r'ZoldstatsZnewstatsr4rLrMrNrOrPrQZnewfuncZ newcallersfunc2callerZold_topZnew_toprrr strip_dirss.    zStats.strip_dirsc Cs||jr dSi|_}xb|jD]T\}\}}}}}||krBi||<x0|D]$\}} ||krdi||<| |||<qLWq WdS)N)r.r7rJ) r'r.rLrMrNrOrPrQrrrsrrr calc_callees#s zStats.calc_calleescCs|}t|trpyt|}Wn&tjk rB|d|7}||fSXg}x|D]}|t|rN||qNWnzt|}t|t rd|krdkrnnt ||d}|d|}n2t|t rd|kr|krnn|}|d|}t|t|kr|dt|t||f7}||fS)Nz# gg?g?rz6 List reduced from %r to %r due to restriction <%r> ) r?rrecompileerrorsearchrKrir$floatrf)r'ZsellistmsgZnew_listZrexrLcountrrreval_print_amount6s,    "" zStats.eval_print_amountcCs|j}|jr*|jdd}d|jd}nt|j}d}x|D]}||||\}}qBWt|}|spd|fSt||j d|t|jkrd}x(|D] }tt ||krtt |}qW|d|fS)Nz Ordered by:  z! Random listing order was used r)r-r_) r4r0rgr{r7keysr~r$r<r!rK)r'Zsel_listwidthZ stat_listr|Z selectionr}rLrrrget_print_listPs$  zStats.get_print_listcGsx|jD]}t||jdqW|jr0t|jdd}x"|jD]}t|t||jdqcCs tt|||d|jd|s2t|jddSt|}d}x|D]}t|}||} t| tr| \} } } } | | krd| | f}n d| f}d|dd t |t | t | |f}|d }n$d || t |j |d f}|d }t||||jdd}qHWdS) Nr)rr-)r-r,z%d/%dz%dz %s %s %s %sr^r_rz %s(%r) %srZ) r<rKrr!sortedrr?rmrjustr$f8r7)r'rsourceZ call_dictZarrowZclistrrLr rhrNrMrOrPZsubstatsZ left_widthrrrrs*       zStats.print_call_linecCs"tdd|jdtd|jddS)Nz- ncalls tottime percall cumtime percallr)rr-zfilename:lineno(function))r-)r<r!)r'rrrrszStats.print_titlecCs|j|\}}}}}t|}||kr4|dt|}t|dd|jdtt|d|jd|dkrxtdd|jdntt||d|jdtt|d|jd|dkrtdd|jdntt||d|jdtt||jddS)N/ r)rr-rz )r-)r7rr<rr!rrK)r'rLrMrNrOrPrQcrrrrszStats.print_line)r)rrr__doc__r*r%r9r:r&rYr`rdrnrprtrur~rrrrrrrrrrrrr7sD   '  c@s eZdZdZddZddZdS)rkaThis class provides a generic function for comparing any two tuples. Each instance records a list of tuple-indices (from most significant to least significant), and sort direction (ascending or decending) for each tuple-index. The compare functions can then be used as the function argument to the system sort() function when a list of tuples need to be sorted in the instances order.cCs ||_dS)N)comp_select_list)r'rrrrr*szTupleComp.__init__cCsBx<|jD]2\}}||}||}||kr.| S||kr|SqWdS)Nr)r)r'leftrightindex directionlrrrrrlszTupleComp.compareN)rrrrr*rlrrrrrksrkcCs|\}}}tj|||fS)N)rCpathbasename) func_namer r r rrrrqs rqcCs|dS)Nr_r)rLrrrrsrcCsN|dddkrB|d}|dr<|drz{%s}rr+z %s:%d(%s)) startswithendswith)rr rrrrKs rKc Cs@|\}}}}}|\}}} } } |||||| || t| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrrMrNrOrPrQZt_ccZt_ncZt_ttZt_ctZ t_callersrrrrTsrTcCsi}x|D]\}}|||<qWxb|D]V\}}||krzt|trhtddt|||D||<q|||7<q,|||<q,W|S)z*Combine two caller lists in a single list.css|]\}}||VqdS)Nr).0ijrrr szadd_callers..)rJr?rmzip)rrZ new_callersrLrsrrrr s  " rcCs"d}x|D] }||7}qW|S)z@Sum the caller statistics to get total number of calls received.r)r)rQrNrrrr count_callss rcCsd|S)Nz%8.3fr)xrrrr(sr__main__c@seZdZd6ddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdS)7ProfileBrowserNcCs6tj|d|_d|_tj|_|dk r2||dS)Nz% ) cmdCmdr*promptr7r"r#r!do_read)r'profilerrrr*7s  zProfileBrowser.__init__c Cs|}g}x|D]}y|t|wWntk r>YnXy8t|}|dksZ|dkrjtd|jdw||wWntk rYnX||qW|jrt|j||ntd|jddS)Nrrz#Fraction argument must be in [0, 1])r-zNo statistics object is loaded.) splitrirf ValueErrorrzr<r!r7getattr)r'fnr r(Z processedZtermZfracrrrgeneric?s,  zProfileBrowser.genericcCsXtd|jdtd|jdtd|jdtd|jdtd|jdtd|jddS)NzArguments may be:)r-z0* An integer maximum number of entries to print.z:* A decimal fractional number between 0 and 1, controllingz- what fraction of selected entries to print.z8* A regular expression; only entries with function namesz that match it are printed.)r<r!)r'rrr generic_helpWs zProfileBrowser.generic_helpc Csd|jrRy|j|Wq`tk rN}ztd||f|jdWdd}~XYq`Xntd|jddS)Nz$Failed to load statistics for %s: %s)r-zNo statistics object is loaded.r)r7r&OSErrorr<r!)r'r errrdo_add_s*zProfileBrowser.do_addcCstd|jddS)Nz>Add profile info from given file to current statistics object.)r-)r<r!)r'rrrhelp_addhszProfileBrowser.help_addcCs |d|S)Nr)r)r'r rrr do_calleeskszProfileBrowser.do_calleescCstd|jd|dS)Nz6Print callees statistics from the current stat object.)r-)r<r!r)r'rrr help_calleesmszProfileBrowser.help_calleescCs |d|S)Nr)r)r'r rrr do_callersqszProfileBrowser.do_callerscCstd|jd|dS)Nz6Print callers statistics from the current stat object.)r-)r<r!r)r'rrr help_callerssszProfileBrowser.help_callerscCstd|jddS)Nr,)r-r)r<r!)r'r rrrdo_EOFwszProfileBrowser.do_EOFcCstd|jddS)NzLeave the profile brower.)r-)r<r!)r'rrrhelp_EOFzszProfileBrowser.help_EOFcCsdS)Nrr)r'r rrrdo_quit}szProfileBrowser.do_quitcCstd|jddS)NzLeave the profile brower.)r-)r<r!)r'rrr help_quitszProfileBrowser.help_quitc Cs|ryt||_Wnntk rH}zt|jd|jddSd}~XYn:tk r}zt|jjd||jddSd}~XYnX|d|_ n6t |j dkr|j dd}| |ntd|jddS) Nr)r-:z% r_z1No statistics object is current -- cannot reload.r) rr7rr<r(r!r;rHrrr$r)r'r errrrrrs  zProfileBrowser.do_readcCs td|jdtd|jddS)Nz+Read in profile data from a specified file.)r-z*Without argument, reload the current file.)r<r!)r'rrr help_readszProfileBrowser.help_readcCs$|jr|jntd|jddS)NzNo statistics object is loaded.)r-r)r7rpr<r!)r'r rrr do_reverses zProfileBrowser.do_reversecCstd|jddS)Nz/Reverse the sort order of the profiling report.)r-)r<r!)r'rrr help_reverseszProfileBrowser.help_reversecs|jstd|jddS|j|rRtfdd|DrR|jj|n@td|jdx0tj D]"\}}td||df|jdqlWdS) NzNo statistics object is loaded.)r-c3s|]}|kVqdS)Nr)rr)abbrevsrrrsz)ProfileBrowser.do_sort..z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) r7r<r!rdallrrnrr`rJ)r'r rerhr)rrdo_sorts zProfileBrowser.do_sortcCs td|jdtd|jddS)Nz.Sort profile data according to specified keys.)r-z3(Typing `sort' without arguments lists valid keys.))r<r!)r'rrr help_sortszProfileBrowser.help_sortcsfddtjDS)Ncsg|]}|r|qSr)r)ra)textrr sz0ProfileBrowser.complete_sort..)rr`)r'rr(r)rr complete_sortszProfileBrowser.complete_sortcCs |d|S)Nr)r)r'r rrrdo_statsszProfileBrowser.do_statscCstd|jd|dS)Nz.Print statistics from the current stat object.)r-)r<r!r)r'rrr help_statsszProfileBrowser.help_statscCs$|jr|jntd|jddS)NzNo statistics object is loaded.)r-)r7rtr<r!)r'r rrrdo_strips zProfileBrowser.do_stripcCstd|jddS)NzsR