[f@sdZddlZddlZddlZddlZddlZddlmZdgZGdddZ GdddZ dd Z d d Z d d Z ddZddZddZddZedkrddlZyddlZWnek rYnXGdddejZeejdkrPejdZndZykeeZx(ejddD]ZejeqyWeddejej eddejWne!k rYnXndS)z3Class for printing reports on profiled python code.N) cmp_to_keyStatsc@sweZdZdZddddZddZdd Zd d Zd d ZddZ idOd6dSd6dWd6d[d6d^d6dad6ddd6dgd6djd"6dod$6dsd'6dvd*6dzd-6d~d.6Z d/d0Z d1d2Z d3d4Z d5d6Zd7d8Zd9d:Zd;d<Zd=d>Zd?d@ZdAdBZdCdDZdEdFdGZdHdIZdJdKZdS)ra<This 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). 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/usr/lib/python3.4/pstats.py__init__>s    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 Hs"            'z Stats.initcCs|dkri|_dSt|trt|d}tj||_WdQXy-tj|}tj |j d|}WnYnX|g|_ n1t |dr|j |j|_i|_n|jstd|j|fndS)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|krw|jj|ntt||j krtt||_ qqWdS)Njprofilerprofiler)r2rr3) ritemsrrrrr rfunc_std_stringr)r funcccncttctcallersrrrr qs+ zStats.get_top_level_statscGsL|s |Sx;t|D]-}t|t|krDt|}n|j|j7_|j|j7_|j|j7_|j|j7_x!|jD]}|jj|qW|j |j kr|j |_ nd|_ 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]x\}\}}}}}||kr^i|| 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|jkr d}x>|D]3}tt ||krtt |}qqWn|d|fS)Nz Ordered by:  z! Random listing order was used rrrX) rrrartrkeysrwrr"rr5)r Zsel_listwidthZ stat_listruZ selectionrvr6rrrget_print_list/s$      zStats.get_print_listcGsdx$|jD]}t|d|jq W|jrCtd|jnd}x-|jD]"}t|t|d|jqSWt||jdddd|j|j|jkrtd|jddd|jntd|jd|jtd|j|j|\}}|r`|j x|D]}|j |q&Wtd|jtd|jn|S) Nr zfunction callsendz(%d primitive calls)zin %.3f secondsz ) rr"rrfunc_get_function_namerrrr{ print_title print_line)r amountrCindentr6rzrtrrr print_statsGs(  "#  zStats.print_statscGs|j|\}}|r|j|j|dxM|D]E}||jkrn|j|||j|q<|j||iq<Wtd|jtd|jn|S)Nz called...r)r{rnprint_call_headingrprint_call_liner"r)r rrzrtr6rrr print_callees^s  zStats.print_calleesc Gs|j|\}}|r|j|dx@|D]8}|j|\}}}}} |j||| dq2Wtd|jtd|jn|S)Nzwas called by...z<-r)r{rrrr"r) r rrzrtr6r7r8r9r:r;rrr print_callersms zStats.print_callersc Cstdj||d|jd}xW|jjD]F\}}}}}|r6tt|j} t| t}Pq6q6W|rtd|dd|jndS)Nz Function rFr|z ncalls tottime cumtime) r"ljustrrvaluesnextiterr%rf) r name_sizeZ column_titleZ subheaderr7r8r9r:r;valuerrrrxs %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 %srUrXrz %s(%r) %srG) r"r5rrsortedryr%rfrjustrf8r)r rsourceZ call_dictZarrowZclistrr6rRrr8r7r9r: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|}||krK|dt|}nt|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)rrErrrr)rrErrGrr)rrHrrGrr)rrHrJrr)rrKrJrr)rrKrLrr)rrMrJrr)rrKrPrr)rrQrPrrJrrLrrrr)rrSrrrr)rrTrUrr)rrVrrXrr)rrYrrXrr)rrY)__name__ __module__ __qualname____doc__rr rr r rDrZr^rgrirmrnrwr{rrrrrrrrrrrr sH                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 leftrightindexZ directionlrrrrres    zTupleComp.compareN)rrrrrrerrrrrds  rdcCs(|\}}}tjj|||fS)N)r*pathbasename) func_namerCrNrRrrrrjsrjcCs|dS)NrXr)r6rrrrsrcCsc|ddd krW|d}|jdrP|jdrPd|dd S|Snd|SdS) NrX~r<>z{%s}rz %s:%d(%s))rrr) startswithendswith)rrRrrrr5s  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;r8rFrrr count_callssrcCsd|S)Nz%8.3fr)xrrrrsr__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)6ProfileBrowserNcCsNtjj|d|_d|_tj|_|dk rJ|j|ndS)Nz% ) cmdCmdrpromptrrrrdo_read)r profilerrrrs     zProfileBrowser.__init__cCs|j}g}x|D]}y|jt|wWntk rMYnXyQt|}|dksu|dkrtdd|jwn|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 fnrNr Z processedZtermZfracrrrgenerics,       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_help7s zProfileBrowser.generic_helpcCs3|jr|jj|ntdd|jdS)NzNo statistics object is loaded.rr)rr r"r)r rNrrrdo_add?s zProfileBrowser.do_addcCstdd|jdS)Nz>Add profile info from given file to current statistics object.r)r"r)r rrrhelp_addEszProfileBrowser.help_addcCs|jd|S)Nr)r)r rNrrr do_calleesHszProfileBrowser.do_calleescCs!tdd|j|jdS)Nz6Print callees statistics from the current stat object.r)r"rr)r rrr help_calleesJszProfileBrowser.help_calleescCs|jd|S)Nr)r)r rNrrr do_callersNszProfileBrowser.do_callerscCs!tdd|j|jdS)Nz6Print callers statistics from the current stat object.r)r"rr)r rrr help_callersPszProfileBrowser.help_callerscCstdd|jdS)Nrrr)r"r)r rNrrrdo_EOFTszProfileBrowser.do_EOFcCstdd|jdS)NzLeave the profile brower.r)r"r)r rrrhelp_EOFWszProfileBrowser.help_EOFcCsdS)Nrr)r rNrrrdo_quitZszProfileBrowser.do_quitcCstdd|jdS)NzLeave the profile brower.r)r"r)r rrr help_quit\szProfileBrowser.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 rNerrrrrr_s 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_readpszProfileBrowser.help_readcCs0|jr|jjntdd|jdS)NzNo statistics object is loaded.rr)rrir"r)r rNrrr do_reversets zProfileBrowser.do_reversecCstdd|jdS)Nz/Reverse the sort order of the profiling report.r)r"r)r rrr help_reversezszProfileBrowser.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 rNr_rr)rrdo_sort}s +%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 rNrrrdo_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 rNrrrdo_strips zProfileBrowser.do_stripcCstdd|jdS)Nzrrrrrreadline ImportErrorrrrargvZ initprofileZbrowserrrr"rZcmdloopKeyboardInterruptrrrrsH