zfc@s$dZddlZddlZddlZddlZddlmZddddgZeddZ edd Z d Z e ed rej d Znd Zy+ddlZdZedZdZWnek rnXdfdYZdZdZedkr endS(s Class for profiling Python code.iN(t OptionParsertruntrunctxthelptProfilecCs]t}y|j|}Wntk r/nX|dk rL|j|n |j|SdS(sRun statement under profiler optionally saving results in filename This function takes a single argument that can be passed to the "exec" statement, and an optional file name. In all cases this routine attempts to "exec" its first argument and gather profiling statistics from the execution. If no file name is present, then this function automatically prints a simple profiling report, sorted by the standard name string (file/line/function-name) that is presented in each line. N(RRt SystemExittNonet dump_statst print_stats(t statementtfilenametsorttprof((s/usr/lib64/python2.7/profile.pyR0s   cCsct}y|j|||}Wntk r5nX|dk rR|j|n |j|SdS(sRun statement under profiler, supplying your own globals and locals, optionally saving results in filename. statement and filename have the same semantics as profile.run N(RRRRRR(R tglobalstlocalsR R R ((s/usr/lib64/python2.7/profile.pyREs   cCsdGHdGHdS(Ns2Documentation for the profile module can be found s?in the Python Library Reference, section 'The Python Profiler'.((((s/usr/lib64/python2.7/profile.pyRWsttimescCs|}|d|dS(Nii((ttimertt((s/usr/lib64/python2.7/profile.pyt_get_time_times\s icCstjtjS(N(tresourcet getrusaget RUSAGE_SELF(((s/usr/lib64/python2.7/profile.pytgtcCs|}|d|dS(Nii((RR((s/usr/lib64/python2.7/profile.pyt_get_time_resourcehs icBs3eZdZdZd"d"dZdZdZdZdZ dZ dZ d Z d Z ie d 6e d 6e d 6e d6e d6e d6ZdZdd#dYZdd$dYZdZdZddZdZdZdZdZdZdZdd Zd!ZRS(%sProfiler class. self.cur is always a tuple. Each such tuple corresponds to a stack frame that is currently active (self.cur[-2]). The following are the definitions of its members. We use this external "parallel stack" to avoid contaminating the program that we are profiling. (old profiler used to write into the frames local dictionary!!) Derived classes can change the definition of some entries, as long as they leave [-2:] intact (frame and previous tuple). In case an internal error is detected, the -3 element is used as the function name. [ 0] = Time that needs to be charged to the parent frame's function. It is used so that a function call will not have to access the timing data for the parent frame. [ 1] = Total time spent in this frame's function, excluding time in subfunctions (this latter is tallied in cur[2]). [ 2] = Total time spent in subfunctions, excluding time executing the frame's function (this latter is tallied in cur[1]). [-3] = Name of the function that corresponds to this frame. [-2] = Actual frame that we correspond to (used to sync exception handling). [-1] = Our parent 6-tuple (corresponds to frame.f_back). Timing data for each function is stored as a 5-tuple in the dictionary self.timings[]. The index is always the name stored in self.cur[-3]. The following are the definitions of the members: [0] = The number of times this function was called, not counting direct or indirect recursion, [1] = Number of times this function appears on the stack, minus one [2] = Total time spent internal to this function [3] = Cumulative time that this function was present on the stack. In non-recursive functions, this is the total execution time from start to finish of each invocation of a function, including time spent in all subfunctions. [4] = A dictionary indicating for each function name, the number of times it was called by us. icCsi|_d|_d|_d|_|dkr<|j}n||_|strrt|_|j |_ t |_ qt tdrtj|_|_ |j|_ qt tdrtj|_|j |_ t|_ qtj|_|_ |j|_ n||_|j}yt|}Wn&tk rE||_ |j|_ n@X|dkra|j |_ n |j|_ |td}||_ |j |_|jddS(NRtclockRicSs ||S(N((Rtsum((s/usr/lib64/python2.7/profile.pytget_time_timerstprofiler(ttimingsRtcurtcmdt c_func_nametbiast_has_rest resgetrusageRttrace_dispatcht dispatcherRtget_timethasattrttimeRttrace_dispatch_itosRRtlent TypeErrorttrace_dispatch_lRRt simulate_call(tselfRR!RtlengthR((s/usr/lib64/python2.7/profile.pyt__init__sF                    cCs|j}|}|d|d|j|j}|dkrM|j|_n|j||||r|}|d|d|_n"|}|d|d||_dS(Niitc_call(RRR!t__name__R tdispatch(R/tframeteventtargRRtr((s/usr/lib64/python2.7/profile.pyR$s      cCsw|j}||j|j}|dkr;|j|_n|j||||rc||_n|||_dS(NR2(RRR!R3R R4(R/R5R6R7RR((s/usr/lib64/python2.7/profile.pyR)s  cCs|j}|d|j|j}|dkr?|j|_n|j||||rk|d|_n|d||_dS(NgN@R2(RRR!R3R R4(R/R5R6R7RR((s/usr/lib64/python2.7/profile.pyttrace_dispatch_macs  cCsw|j}||j|j}|dkr;|j|_n|j||||rc||_n|||_dS(NR2(R&RR!R3R R4(R/R5R6R7R&R((s/usr/lib64/python2.7/profile.pyR-s  c Cs`|j\}}}}}}||k r=|r=|j||S|||||||f|_dS(Ni(Rttrace_dispatch_return( R/R5Rtrpttrittrettrfntrframetrcur((s/usr/lib64/python2.7/profile.pyttrace_dispatch_exception s cCs |jrb|j|jdk rb|j\}}}}}}t|tjsb|j|dqbn|j} | j| j| j f} |dd| ||jf|_|j } | | kr| | \} } }}}| | d|||f| | R?R@tfcodetfnRtcctnsttttcttcallers((s/usr/lib64/python2.7/profile.pyttrace_dispatch_calls    c Csdd|jf}|dd|||jf|_|j}||kr~||\}}}}} ||d||| f||R@t frame_totaltppttpittpettpfntpframetpcurRRKRLRMRNRO((s/usr/lib64/python2.7/profile.pyR:6s"  #     !tcallt exceptiontreturnR2t c_exceptiontc_returncCs+|jdrdS||_|j|dS(Ni(RRR.(R/R((s/usr/lib64/python2.7/profile.pytset_cmdls  t fake_codecBseZdZdZRS(cCs(||_||_||_d|_dS(Ni(RFtco_lineRHRG(R/R tlinetname((s/usr/lib64/python2.7/profile.pyR1rs   cCst|j|j|jfS(N(treprRFR`RH(R/((s/usr/lib64/python2.7/profile.pyt__repr__xs(R3t __module__R1Rd(((s/usr/lib64/python2.7/profile.pyR_qs RDcBseZdZRS(cCs||_||_dS(N(RERB(R/tcodetprior((s/usr/lib64/python2.7/profile.pyR1|s (R3ReR1(((s/usr/lib64/python2.7/profile.pyRD{scCsa|jdd|}|jr.|jd}nd}|j||}|jd||ddS(NtprofileiiRY(R_RRRDR4(R/RbRfRWR5((s/usr/lib64/python2.7/profile.pyR.s  cCse|j}||j}x5|jdrP|jd||jd|d}qW|||_dS(NiR[ii(R&RRR4(R/R&R((s/usr/lib64/python2.7/profile.pytsimulate_cmd_completes   icCs2ddl}|j|jj|jdS(Ni(tpstatstStatst strip_dirst sort_statsR(R/R Rj((s/usr/lib64/python2.7/profile.pyRs cCs:t|d}|jtj|j||jdS(Ntwb(topent create_statstmarshaltdumptstatstclose(R/tfiletf((s/usr/lib64/python2.7/profile.pyRs cCs|j|jdS(N(Ritsnapshot_stats(R/((s/usr/lib64/python2.7/profile.pyRps c Csi|_x{|jjD]j\}\}}}}}|j}d}x|jD]}||7}qSW|||||f|j|tdefaults-ss--sortR s?Sort order when printing to stdout, based on pstats.Stats classiiiitrbtexect__file__R~R3t __package__(RtFalsetallow_interspersed_argst add_optionRRtargvt print_usagetexitt parse_argsR+tpathtinsertR*tdirnameRotcompiletreadRRR (RtparsertoptionsRtprognametfpRftglobs((s/usr/lib64/python2.7/profile.pytmain@s2        R~(RRR*R(RqtoptparseRt__all__RRRRR'RRR"RR#Rt ImportErrorRRkRR3(((s/usr/lib64/python2.7/profile.pyt s2            !