Yfg@sdZddlZddlZddlZddlZddlZddlmZdgZGdddZ GdddZ dd Z d d Z d d Z ddZddZddZddZedkrddlZyddlZWnek rYnXGdddejZeejdkrPejdZndZykeeZx(ejddD]ZejeqyWeddejej eddejWne!k rYnXdS)z3Class for printing reports on profiled python code.N) cmp_to_keyStatsc@sieZdZdZddddZddZdd Zd d Zd d ZddZ ddOddSddWdd[dd^ddadddddgd djd#dod%dsd(dvd+dzd.d~iZ d/d0Z d1d2Z d3d4Z d5d6Zd7d8Zd9d:Zd;d<Zd=d>Zd?d@ZdAdBZdCdDZdEdFdGZdHdIZdJdKZdS)raThis 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 an arbitrary number of quoted strings to select the sort order. For example sort_stats('time', '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) streamNcGs_|p tj|_t|s'd}n|d}|dd}|j||j|dS)Nr)sysstdoutrleninitadd)selfrargsargr+/opt/alt/python35/lib64/python3.5/pstats.py__init__As    zStats.__init__c Csd|_g|_d|_d|_d|_d|_d|_t|_i|_ i|_ |j |y|j WnBt k rtd|jr|jdndd|jYnXdS)NrzInvalid timing data %srfile) 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 Ks"            'z Stats.initcCs|dkri|_dSt|trt|d}tj||_WdQRXy-tj|}tj |j d|}WnYnX|g|_ n.t |dr|j |j|_i|_|jstd|j|fdS)Nrbz create_statsz.Cannot create or construct a %r object from %r)r isinstancestropenmarshalloadosstattimeZctimest_mtimerhasattrr$ TypeError __class__)r r fZ file_statsrrrr^s(      zStats.load_statscCsx|jjD]\}\}}}}}|j|7_|j|7_|j|7_d|krt|jj|tt||j krtt||_ qWdS)Njprofilerprofiler)r2rr3) ritemsrrrrr rfunc_std_stringr)r funcccncttctcallersrrrr ts+ zStats.get_top_level_statscGsF|s |Sx5t|D]'}t|t|krAt|}|j|j7_|j|j7_|j|j7_|j|j7_x!|jD]}|jj|qW|j |j kr|j |_ d|_ xg|j j D]V\}}||j kr|j |}nddddif}t |||j |rrr rrr)r ZoldstatsZnewstatsrr6r7r8r9r:r;ZnewfuncZ newcallersfunc2callerZold_topZnew_toprrr strip_dirss.  (       zStats.strip_dirsc Cs|jr dSi|_}x|jjD]r\}\}}}}}||kr[i|||jD]0\}} ||kri||<| ||| gg?g?rz6 List reduced from %r to %r due to restriction <%r> ) r%r&recompileerrorsearchr5rbrfloatr`)r ZsellistmsgZnew_listZrexr6countrrreval_print_amounts,  ++zStats.eval_print_amountcCs|j}|jr9|jdd}d|jd}nt|jj}d}x)|D]!}|j|||\}}q[Wt|}|sd|fSt|d|j |t|jkrd}x8|D]0}tt ||krtt |}qW|d|fS)Nz Ordered by:  z! Random listing order was used rrrX) rrrartrkeysrwrr"rr5)r Zsel_listwidthZ stat_listruZ selectionrvr6rrrget_print_list2s$      zStats.get_print_listcGs[x$|jD]}t|d|jq W|jr@td|jd}x-|jD]"}t|t|d|jqPWt||jdddd|j|j|jkrtd|jddd|jtd|jd|jtd|j|j|\}}|rW|j x|D]}|j |q Wtd|jtd|j|S) Nr zfunction callsendz(%d primitive calls)zin %.3f secondsz ) rr"rrfunc_get_function_namerrrr{ print_title print_line)r amountrCindentr6rzrtrrr print_statsJs(  "   zStats.print_statscGs|j|\}}|r|j|j|dxM|D]E}||jkrn|j|||j|q<|j||iq<Wtd|jtd|j|S)Nz called...r)r{rnprint_call_headingrprint_call_liner"r)r rrzrtr6rrr print_calleesas  zStats.print_calleesc Gs|j|\}}|r|j|dx@|D]8}|j|\}}}}} |j||| dq2Wtd|jtd|j|S)Nzwas called by...z<-r)r{rrrr"r) r rrzrtr6r7r8r9r:r;rrr print_callersps zStats.print_callersc Cstdj||d|jd}xT|jjD]C\}}}}}|r6tt|j} t| t}Pq6W|rtd|dd|jdS)Nz Function rFr|z ncalls tottime cumtime) r"ljustrrvaluesnextiterr%rf) r name_sizeZ column_titleZ subheaderr7r8r9r:r;valuerrrr{s %zStats.print_call_headingz->cCsktt|j||ddd|j|sFtd|jdSt|j}d}x|D]}t|}||} t| tr| \} } } } | | krd| | f}n d| f}d|jdd t |t | t | |f}|d }n.d || t |j |d f}|d }t|||d|jd}qeWdS) Nr~r|rrz%d/%dz%dz %s %s %s %srVrXrz %s(%r) %srG) r"r5rrsortedryr%rfrjustrf8r)r rsourceZ call_dictZarrowZclistrr6rPrr8r7r9r:ZsubstatsZ left_widthrrrrs*,      $ zStats.print_call_linecCs0tdddd|jtdd|jdS)Nz- ncalls tottime percall cumtime percallr~r|rzfilename:lineno(function))r"r)r rrrrszStats.print_titlecCs[|j|\}}}}}t|}||krH|dt|}t|jdddd|jtt|ddd|j|dkrtdddd|jn#tt||ddd|jtt|ddd|j|dkrtd ddd|jn#tt||ddd|jtt|d|jdS) N/ r~r|rrr}z z )rr&r"rrrr5)r r6r7r8r9r:r;crrrrs  " # #zStats.print_linerrrr)rrFrrrr)rrFrrGrr)rrHrrGrr)rrHrJrr)rrKrJrr)rrKrMrr)rrNrJrr)rrKrQrr)rrRrQrrJrrMrrrr)rrSrrrr)rrTrVrr)rrWrrXrr)rrYrrXrr)rrY)__name__ __module__ __qualname____doc__rr rr r rDrZr^rgrirmrnrwr{rrrrrrrrrrrr sF                c@s.eZdZdZddZddZdS)rdaThis 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 rrrrrszTupleComp.__init__cCsSxL|jD]A\}}||}||}||kr;| S||kr |Sq WdS)Nr)r)r leftrightindex directionlrrrrres    zTupleComp.compareN)rrrrrrerrrrrds  rdcCs(|\}}}tjj|||fS)N)r*pathbasename) func_namerCrLrPrrrrjsrjcCs|dS)NrXr)r6rrrrsrcCsc|ddd krW|d}|jdrP|jdrPd|dd S|Snd|SdS) NrX~r<>z{%s}rz %s:%d(%s))rrr) startswithendswith)rrPrrrr5s  r5c CsV|\}}}}}|\}}} } } |||||| || t| |fS)z3Add together all the stats for two profile entries.) add_callers) targetrr7r8r9r:r;Zt_ccZt_ncZt_ttZt_ctZ t_callersrrrr>sr>cCsi}x$|jD]\}}|||s zadd_callers..)r4r%rfzip)rrZ new_callersr6rlrrrrs  !rcCs+d}x|jD]}||7}qW|S)z@Sum the caller statistics to get total number of calls received.r)r)r;r8rErrr count_callssrcCsd|S)Nz%8.3fr)xrrrr sr__main__c@sKeZdZdddZddZddZdd 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)6ProfileBrowserNcCsKtjj|d|_d|_tj|_|dk rG|j|dS)Nz% ) cmdCmdrpromptrrrrdo_read)r profilerrrrs     zProfileBrowser.__init__cCs|j}g}x|D]}y|jt|wWntk rMYnXyNt|}|dksu|dkrtdd|jw|j|wWntk rYnX|j|qW|jrt|j||ntdd|jdS)Nrrz#Fraction argument must be in [0, 1]rzNo statistics object is loaded.) splitrbr` ValueErrorrsr"rrgetattr)r fnrLr Z processedZtermZfracrrrgeneric"s,       zProfileBrowser.genericcCsvtdd|jtdd|jtdd|jtdd|jtdd|jtdd|jdS)NzArguments may be:rz0* 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_help:s zProfileBrowser.generic_helpcCs||jrey|jj|Wqxtk ra}z"td||fd|jWYdd}~XqxXntdd|jdS)Nz$Failed to load statistics for %s: %srzNo statistics object is loaded.r)rr IOErrorr"r)r rLerrrdo_addBs 3zProfileBrowser.do_addcCstdd|jdS)Nz>Add profile info from given file to current statistics object.r)r"r)r rrrhelp_addKszProfileBrowser.help_addcCs|jd|S)Nr)r)r rLrrr do_calleesNszProfileBrowser.do_calleescCs!tdd|j|jdS)Nz6Print callees statistics from the current stat object.r)r"rr)r rrr help_calleesPszProfileBrowser.help_calleescCs|jd|S)Nr)r)r rLrrr do_callersTszProfileBrowser.do_callerscCs!tdd|j|jdS)Nz6Print callers statistics from the current stat object.r)r"rr)r rrr help_callersVszProfileBrowser.help_callerscCstdd|jdS)Nrrr)r"r)r rLrrrdo_EOFZszProfileBrowser.do_EOFcCstdd|jdS)NzLeave the profile brower.r)r"r)r rrrhelp_EOF]szProfileBrowser.help_EOFcCsdS)Nrr)r rLrrrdo_quit`szProfileBrowser.do_quitcCstdd|jdS)NzLeave the profile brower.r)r"r)r rrr help_quitbszProfileBrowser.help_quitcCs|ryt||_Wntk r^}z#t|jdd|jdSWYdd}~XnItk r}z)t|jjd|d|jdSWYdd}~XnX|d|_ nKt |j dkr|j dd}|j |ntdd|jdS) Nrr:z% rXz1No statistics object is current -- cannot reload.r) rrOSErrorr"r rr!r0rrrr)r rLerrrrrres zProfileBrowser.do_readcCs*tdd|jtdd|jdS)Nz+Read in profile data from a specified file.rz*Without argument, reload the current file.)r"r)r rrr help_readvszProfileBrowser.help_readcCs0|jr|jjntdd|jdS)NzNo statistics object is loaded.rr)rrir"r)r rLrrr do_reversezs zProfileBrowser.do_reversecCstdd|jdS)Nz/Reverse the sort order of the profiling report.r)r"r)r rrr help_reverseszProfileBrowser.help_reversecs|js tdd|jdS|jj|rstfdd|jDrs|jj|jnTtdd|jx>tjj D]-\}}td||dfd|jqWdS) NzNo statistics object is loaded.rc3s|]}|kVqdS)Nr)rr)abbrevsrr sz)ProfileBrowser.do_sort..z/Valid sort keys (unique prefixes are accepted):z%s -- %srr) rr"rr^allrrgrrZr4)r rLr_rr)rrdo_sorts +%zProfileBrowser.do_sortcCs*tdd|jtdd|jdS)Nz.Sort profile data according to specified keys.rz3(Typing `sort' without arguments lists valid keys.))r"r)r rrr help_sortszProfileBrowser.help_sortcsfddtjDS)Ncs%g|]}|jr|qSr)r)ra)textrrrs z0ProfileBrowser.complete_sort..)rrZ)r rr r)rr complete_sortszProfileBrowser.complete_sortcCs|jd|S)Nr)r)r rLrrrdo_statsszProfileBrowser.do_statscCs!tdd|j|jdS)Nz.Print statistics from the current stat object.r)r"rr)r rrr help_statsszProfileBrowser.help_statscCs0|jr|jjntdd|jdS)NzNo statistics object is loaded.r)rrmr"r)r rLrrrdo_strips zProfileBrowser.do_stripcCstdd|jdS)Nzrrrrrreadline ImportErrorrrrargvZ initprofileZbrowserrrr"rZcmdloopKeyboardInterruptrrrrsH