Profiling and tracing
The Python interpreter provides some low-level support for attaching profiling and execution tracing facilities. These are used for profiling, debugging, and coverage analysis tools.
This C interface allows the profiling or tracing code to avoid the overhead of calling through Python-level callable objects, making a direct C function call instead. The essential attributes of the facility have not changed; the interface allows trace functions to be installed per-thread, and the basic events reported to the trace function are the same as had been reported to the Python-level trace functions in previous versions.
The type of the trace function registered using
PyEval_SetProfileandPyEval_SetTrace. The first parameter is the object passed to the registration function as obj, frame is the frame object to which the event pertains, what is one of the constantsPyTrace_CALL,PyTrace_EXCEPTION,PyTrace_LINE,PyTrace_RETURN,PyTrace_C_CALL,PyTrace_C_EXCEPTION,PyTrace_C_RETURN, orPyTrace_OPCODE, and arg depends on the value of what:
Value of what Meaning of arg PyTrace_CALLAlways Py_None.PyTrace_EXCEPTIONException information as returned by sys.exc_info.PyTrace_LINEAlways Py_None.PyTrace_RETURNValue being returned to the caller, or NULLif caused by an exception.PyTrace_C_CALLFunction object being called. PyTrace_C_EXCEPTIONFunction object being called. PyTrace_C_RETURNFunction object being called. PyTrace_OPCODEAlways Py_None.
The value of the what parameter to a
Py_tracefuncfunction when a new call to a function or method is being reported, or a new entry into a generator. Note that the creation of the iterator for a generator function is not reported as there is no control transfer to the Python bytecode in the corresponding frame.
The value of the what parameter to a
Py_tracefuncfunction when an exception has been raised. The callback function is called with this value for what when after any bytecode is processed after which the exception becomes set within the frame being executed. The effect of this is that as exception propagation causes the Python stack to unwind, the callback is called upon return to each frame as the exception propagates. Only trace functions receive these events; they are not needed by the profiler.
The value passed as the what parameter to a
Py_tracefuncfunction (but not a profiling function) when a line-number event is being reported. It may be disabled for a frame by setting~frame.f_trace_linesto 0 on that frame.
The value for the what parameter to
Py_tracefuncfunctions when a call is about to return.
The value for the what parameter to
Py_tracefuncfunctions when a C function is about to be called.
The value for the what parameter to
Py_tracefuncfunctions when a C function has raised an exception.
The value for the what parameter to
Py_tracefuncfunctions when a C function has returned.
The value for the what parameter to
Py_tracefuncfunctions (but not profiling functions) when a new opcode is about to be executed. This event is not emitted by default: it must be explicitly requested by setting~frame.f_trace_opcodesto 1 on the frame.
Set the profiler function to func. The obj parameter is passed to the function as its first parameter, and may be any Python object, or
NULL. If the profile function needs to maintain state, using a different value for obj for each thread provides a convenient and thread-safe place to store it. The profile function is called for all monitored events exceptPyTrace_LINEPyTrace_OPCODEandPyTrace_EXCEPTION.See also the
sys.setprofilefunction.The caller must have an
attached thread state.
Like
PyEval_SetProfilebut sets the profile function in all running threads belonging to the current interpreter instead of the setting it only on the current thread.The caller must have an
attached thread state.As
PyEval_SetProfile, this function ignores any exceptions raised while setting the profile functions in all threads.
3.12
Set the tracing function to func. This is similar to
PyEval_SetProfile, except the tracing function does receive line-number events and per-opcode events, but does not receive any event related to C function objects being called. Any trace function registered usingPyEval_SetTracewill not receivePyTrace_C_CALL,PyTrace_C_EXCEPTIONorPyTrace_C_RETURNas a value for the what parameter.See also the
sys.settracefunction.The caller must have an
attached thread state.
Like
PyEval_SetTracebut sets the tracing function in all running threads belonging to the current interpreter instead of the setting it only on the current thread.The caller must have an
attached thread state.As
PyEval_SetTrace, this function ignores any exceptions raised while setting the trace functions in all threads.
3.12
Reference tracing
3.13
The type of the trace function registered using
PyRefTracer_SetTracer. The first parameter is a Python object that has been just created (when event is set toPyRefTracer_CREATE) or about to be destroyed (when event is set toPyRefTracer_DESTROY). The data argument is the opaque pointer that was provided whenPyRefTracer_SetTracerwas called.If a new tracing function is registered replacing the current one, a call to the trace function will be made with the object set to NULL and event set to
PyRefTracer_TRACKER_REMOVED. This will happen just before the new function is registered.
3.13
The value for the event parameter to
PyRefTracerfunctions when a Python object has been created.
The value for the event parameter to
PyRefTracerfunctions when a Python object has been destroyed.
The value for the event parameter to
PyRefTracerfunctions when the current tracer is about to be replaced by a new one.3.14
Register a reference tracer function. The function will be called when a new Python object has been created or when an object is going to be destroyed. If data is provided it must be an opaque pointer that will be provided when the tracer function is called. Return
0on success. Set an exception and return-1on error.Note that tracer functions must not create Python objects inside or otherwise the call will be re-entrant. The tracer also must not clear any existing exception or set an exception. A
thread statewill be active every time the tracer function is called.There must be an
attached thread statewhen calling this function.If another tracer function was already registered, the old function will be called with event set to
PyRefTracer_TRACKER_REMOVEDjust before the new function is registered.
3.13
Get the registered reference tracer function and the value of the opaque data pointer that was registered when
PyRefTracer_SetTracerwas called. If no tracer was registered this function will return NULL and will set the data pointer to NULL.There must be an
attached thread statewhen calling this function.
3.13
Performance statistics
Largest micro-operation identifier for which CPython allocates performance statistics counters. Available only in builds configured with
--enable-pystats.This constant is intended for internal use only and is not useful to extension modules.
next
谢谢你的反馈!