Profile.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /////////////// Profile.proto ///////////////
  2. //@requires: Exceptions.c::PyErrFetchRestore
  3. //@substitute: naming
  4. // Note that cPython ignores PyTrace_EXCEPTION,
  5. // but maybe some other profilers don't.
  6. #ifndef CYTHON_PROFILE
  7. #if CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_PYSTON
  8. #define CYTHON_PROFILE 0
  9. #else
  10. #define CYTHON_PROFILE 1
  11. #endif
  12. #endif
  13. #ifndef CYTHON_TRACE_NOGIL
  14. #define CYTHON_TRACE_NOGIL 0
  15. #else
  16. #if CYTHON_TRACE_NOGIL && !defined(CYTHON_TRACE)
  17. #define CYTHON_TRACE 1
  18. #endif
  19. #endif
  20. #ifndef CYTHON_TRACE
  21. #define CYTHON_TRACE 0
  22. #endif
  23. #if CYTHON_TRACE
  24. #undef CYTHON_PROFILE_REUSE_FRAME
  25. #endif
  26. #ifndef CYTHON_PROFILE_REUSE_FRAME
  27. #define CYTHON_PROFILE_REUSE_FRAME 0
  28. #endif
  29. #if CYTHON_PROFILE || CYTHON_TRACE
  30. #include "compile.h"
  31. #include "frameobject.h"
  32. #include "traceback.h"
  33. #if PY_VERSION_HEX >= 0x030b00a6
  34. #ifndef Py_BUILD_CORE
  35. #define Py_BUILD_CORE 1
  36. #endif
  37. #include "internal/pycore_frame.h"
  38. #endif
  39. #if CYTHON_PROFILE_REUSE_FRAME
  40. #define CYTHON_FRAME_MODIFIER static
  41. #define CYTHON_FRAME_DEL(frame)
  42. #else
  43. #define CYTHON_FRAME_MODIFIER
  44. #define CYTHON_FRAME_DEL(frame) Py_CLEAR(frame)
  45. #endif
  46. #define __Pyx_TraceDeclarations \
  47. static PyCodeObject *$frame_code_cname = NULL; \
  48. CYTHON_FRAME_MODIFIER PyFrameObject *$frame_cname = NULL; \
  49. int __Pyx_use_tracing = 0;
  50. #define __Pyx_TraceFrameInit(codeobj) \
  51. if (codeobj) $frame_code_cname = (PyCodeObject*) codeobj;
  52. #if PY_VERSION_HEX >= 0x030b00a2
  53. #if PY_VERSION_HEX >= 0x030c00b1
  54. #define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
  55. ((!(check_tracing) || !(tstate)->tracing) && \
  56. (!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
  57. #else
  58. #define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
  59. (unlikely((tstate)->cframe->use_tracing) && \
  60. (!(check_tracing) || !(tstate)->tracing) && \
  61. (!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
  62. #endif
  63. #define __Pyx_EnterTracing(tstate) PyThreadState_EnterTracing(tstate)
  64. #define __Pyx_LeaveTracing(tstate) PyThreadState_LeaveTracing(tstate)
  65. #elif PY_VERSION_HEX >= 0x030a00b1
  66. #define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
  67. (unlikely((tstate)->cframe->use_tracing) && \
  68. (!(check_tracing) || !(tstate)->tracing) && \
  69. (!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
  70. #define __Pyx_EnterTracing(tstate) \
  71. do { tstate->tracing++; tstate->cframe->use_tracing = 0; } while (0)
  72. #define __Pyx_LeaveTracing(tstate) \
  73. do { \
  74. tstate->tracing--; \
  75. tstate->cframe->use_tracing = ((CYTHON_TRACE && tstate->c_tracefunc != NULL) \
  76. || tstate->c_profilefunc != NULL); \
  77. } while (0)
  78. #else
  79. #define __Pyx_IsTracing(tstate, check_tracing, check_funcs) \
  80. (unlikely((tstate)->use_tracing) && \
  81. (!(check_tracing) || !(tstate)->tracing) && \
  82. (!(check_funcs) || (tstate)->c_profilefunc || (CYTHON_TRACE && (tstate)->c_tracefunc)))
  83. #define __Pyx_EnterTracing(tstate) \
  84. do { tstate->tracing++; tstate->use_tracing = 0; } while (0)
  85. #define __Pyx_LeaveTracing(tstate) \
  86. do { \
  87. tstate->tracing--; \
  88. tstate->use_tracing = ((CYTHON_TRACE && tstate->c_tracefunc != NULL) \
  89. || tstate->c_profilefunc != NULL); \
  90. } while (0)
  91. #endif
  92. #ifdef WITH_THREAD
  93. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) \
  94. if (nogil) { \
  95. if (CYTHON_TRACE_NOGIL) { \
  96. PyThreadState *tstate; \
  97. PyGILState_STATE state = PyGILState_Ensure(); \
  98. tstate = __Pyx_PyThreadState_Current; \
  99. if (__Pyx_IsTracing(tstate, 1, 1)) { \
  100. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  101. } \
  102. PyGILState_Release(state); \
  103. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  104. } \
  105. } else { \
  106. PyThreadState* tstate = PyThreadState_GET(); \
  107. if (__Pyx_IsTracing(tstate, 1, 1)) { \
  108. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  109. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  110. } \
  111. }
  112. #else
  113. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) \
  114. { PyThreadState* tstate = PyThreadState_GET(); \
  115. if (__Pyx_IsTracing(tstate, 1, 1)) { \
  116. __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&$frame_code_cname, &$frame_cname, tstate, funcname, srcfile, firstlineno); \
  117. if (unlikely(__Pyx_use_tracing < 0)) goto_error; \
  118. } \
  119. }
  120. #endif
  121. #define __Pyx_TraceException() \
  122. if (likely(!__Pyx_use_tracing)); else { \
  123. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  124. if (__Pyx_IsTracing(tstate, 0, 1)) { \
  125. __Pyx_EnterTracing(tstate); \
  126. PyObject *exc_info = __Pyx_GetExceptionTuple(tstate); \
  127. if (exc_info) { \
  128. if (CYTHON_TRACE && tstate->c_tracefunc) \
  129. tstate->c_tracefunc( \
  130. tstate->c_traceobj, $frame_cname, PyTrace_EXCEPTION, exc_info); \
  131. tstate->c_profilefunc( \
  132. tstate->c_profileobj, $frame_cname, PyTrace_EXCEPTION, exc_info); \
  133. Py_DECREF(exc_info); \
  134. } \
  135. __Pyx_LeaveTracing(tstate); \
  136. } \
  137. }
  138. static void __Pyx_call_return_trace_func(PyThreadState *tstate, PyFrameObject *frame, PyObject *result) {
  139. PyObject *type, *value, *traceback;
  140. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  141. __Pyx_EnterTracing(tstate);
  142. if (CYTHON_TRACE && tstate->c_tracefunc)
  143. tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_RETURN, result);
  144. if (tstate->c_profilefunc)
  145. tstate->c_profilefunc(tstate->c_profileobj, frame, PyTrace_RETURN, result);
  146. CYTHON_FRAME_DEL(frame);
  147. __Pyx_LeaveTracing(tstate);
  148. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  149. }
  150. #ifdef WITH_THREAD
  151. #define __Pyx_TraceReturn(result, nogil) \
  152. if (likely(!__Pyx_use_tracing)); else { \
  153. if (nogil) { \
  154. if (CYTHON_TRACE_NOGIL) { \
  155. PyThreadState *tstate; \
  156. PyGILState_STATE state = PyGILState_Ensure(); \
  157. tstate = __Pyx_PyThreadState_Current; \
  158. if (__Pyx_IsTracing(tstate, 0, 0)) { \
  159. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  160. } \
  161. PyGILState_Release(state); \
  162. } \
  163. } else { \
  164. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  165. if (__Pyx_IsTracing(tstate, 0, 0)) { \
  166. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  167. } \
  168. } \
  169. }
  170. #else
  171. #define __Pyx_TraceReturn(result, nogil) \
  172. if (likely(!__Pyx_use_tracing)); else { \
  173. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  174. if (__Pyx_IsTracing(tstate, 0, 0)) { \
  175. __Pyx_call_return_trace_func(tstate, $frame_cname, (PyObject*)result); \
  176. } \
  177. }
  178. #endif
  179. static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno); /*proto*/
  180. static int __Pyx_TraceSetupAndCall(PyCodeObject** code, PyFrameObject** frame, PyThreadState* tstate, const char *funcname, const char *srcfile, int firstlineno); /*proto*/
  181. #else
  182. #define __Pyx_TraceDeclarations
  183. #define __Pyx_TraceFrameInit(codeobj)
  184. // mark error label as used to avoid compiler warnings
  185. #define __Pyx_TraceCall(funcname, srcfile, firstlineno, nogil, goto_error) if ((1)); else goto_error;
  186. #define __Pyx_TraceException()
  187. #define __Pyx_TraceReturn(result, nogil)
  188. #endif /* CYTHON_PROFILE */
  189. #if CYTHON_TRACE
  190. // see call_trace_protected() in CPython's ceval.c
  191. static int __Pyx_call_line_trace_func(PyThreadState *tstate, PyFrameObject *frame, int lineno) {
  192. int ret;
  193. PyObject *type, *value, *traceback;
  194. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  195. __Pyx_PyFrame_SetLineNumber(frame, lineno);
  196. __Pyx_EnterTracing(tstate);
  197. ret = tstate->c_tracefunc(tstate->c_traceobj, frame, PyTrace_LINE, NULL);
  198. __Pyx_LeaveTracing(tstate);
  199. if (likely(!ret)) {
  200. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  201. } else {
  202. Py_XDECREF(type);
  203. Py_XDECREF(value);
  204. Py_XDECREF(traceback);
  205. }
  206. return ret;
  207. }
  208. #ifdef WITH_THREAD
  209. #define __Pyx_TraceLine(lineno, nogil, goto_error) \
  210. if (likely(!__Pyx_use_tracing)); else { \
  211. if (nogil) { \
  212. if (CYTHON_TRACE_NOGIL) { \
  213. int ret = 0; \
  214. PyThreadState *tstate; \
  215. PyGILState_STATE state = PyGILState_Ensure(); \
  216. tstate = __Pyx_PyThreadState_Current; \
  217. if (__Pyx_IsTracing(tstate, 0, 0) && tstate->c_tracefunc && $frame_cname->f_trace) { \
  218. ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  219. } \
  220. PyGILState_Release(state); \
  221. if (unlikely(ret)) goto_error; \
  222. } \
  223. } else { \
  224. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  225. if (__Pyx_IsTracing(tstate, 0, 0) && tstate->c_tracefunc && $frame_cname->f_trace) { \
  226. int ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  227. if (unlikely(ret)) goto_error; \
  228. } \
  229. } \
  230. }
  231. #else
  232. #define __Pyx_TraceLine(lineno, nogil, goto_error) \
  233. if (likely(!__Pyx_use_tracing)); else { \
  234. PyThreadState* tstate = __Pyx_PyThreadState_Current; \
  235. if (__Pyx_IsTracing(tstate, 0, 0) && tstate->c_tracefunc && $frame_cname->f_trace) { \
  236. int ret = __Pyx_call_line_trace_func(tstate, $frame_cname, lineno); \
  237. if (unlikely(ret)) goto_error; \
  238. } \
  239. }
  240. #endif
  241. #else
  242. // mark error label as used to avoid compiler warnings
  243. #define __Pyx_TraceLine(lineno, nogil, goto_error) if ((1)); else goto_error;
  244. #endif
  245. /////////////// Profile ///////////////
  246. //@substitute: naming
  247. #if CYTHON_PROFILE
  248. static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
  249. PyFrameObject** frame,
  250. PyThreadState* tstate,
  251. const char *funcname,
  252. const char *srcfile,
  253. int firstlineno) {
  254. PyObject *type, *value, *traceback;
  255. int retval;
  256. if (*frame == NULL || !CYTHON_PROFILE_REUSE_FRAME) {
  257. if (*code == NULL) {
  258. *code = __Pyx_createFrameCodeObject(funcname, srcfile, firstlineno);
  259. if (*code == NULL) return 0;
  260. }
  261. *frame = PyFrame_New(
  262. tstate, /*PyThreadState *tstate*/
  263. *code, /*PyCodeObject *code*/
  264. $moddict_cname, /*PyObject *globals*/
  265. 0 /*PyObject *locals*/
  266. );
  267. if (*frame == NULL) return 0;
  268. if (CYTHON_TRACE && (*frame)->f_trace == NULL) {
  269. // this enables "f_lineno" lookup, at least in CPython ...
  270. Py_INCREF(Py_None);
  271. (*frame)->f_trace = Py_None;
  272. }
  273. #if PY_VERSION_HEX < 0x030400B1
  274. } else {
  275. (*frame)->f_tstate = tstate;
  276. #endif
  277. }
  278. __Pyx_PyFrame_SetLineNumber(*frame, firstlineno);
  279. retval = 1;
  280. __Pyx_EnterTracing(tstate);
  281. __Pyx_ErrFetchInState(tstate, &type, &value, &traceback);
  282. #if CYTHON_TRACE
  283. if (tstate->c_tracefunc)
  284. retval = tstate->c_tracefunc(tstate->c_traceobj, *frame, PyTrace_CALL, NULL) == 0;
  285. if (retval && tstate->c_profilefunc)
  286. #endif
  287. retval = tstate->c_profilefunc(tstate->c_profileobj, *frame, PyTrace_CALL, NULL) == 0;
  288. __Pyx_LeaveTracing(tstate);
  289. if (retval) {
  290. __Pyx_ErrRestoreInState(tstate, type, value, traceback);
  291. return __Pyx_IsTracing(tstate, 0, 0) && retval;
  292. } else {
  293. Py_XDECREF(type);
  294. Py_XDECREF(value);
  295. Py_XDECREF(traceback);
  296. return -1;
  297. }
  298. }
  299. static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno) {
  300. PyCodeObject *py_code = 0;
  301. #if PY_MAJOR_VERSION >= 3
  302. py_code = PyCode_NewEmpty(srcfile, funcname, firstlineno);
  303. // make CPython use a fresh dict for "f_locals" at need (see GH #1836)
  304. if (likely(py_code)) {
  305. py_code->co_flags |= CO_OPTIMIZED | CO_NEWLOCALS;
  306. }
  307. #else
  308. PyObject *py_srcfile = 0;
  309. PyObject *py_funcname = 0;
  310. py_funcname = PyString_FromString(funcname);
  311. if (unlikely(!py_funcname)) goto bad;
  312. py_srcfile = PyString_FromString(srcfile);
  313. if (unlikely(!py_srcfile)) goto bad;
  314. py_code = PyCode_New(
  315. 0, /*int argcount,*/
  316. 0, /*int nlocals,*/
  317. 0, /*int stacksize,*/
  318. // make CPython use a fresh dict for "f_locals" at need (see GH #1836)
  319. CO_OPTIMIZED | CO_NEWLOCALS, /*int flags,*/
  320. $empty_bytes, /*PyObject *code,*/
  321. $empty_tuple, /*PyObject *consts,*/
  322. $empty_tuple, /*PyObject *names,*/
  323. $empty_tuple, /*PyObject *varnames,*/
  324. $empty_tuple, /*PyObject *freevars,*/
  325. $empty_tuple, /*PyObject *cellvars,*/
  326. py_srcfile, /*PyObject *filename,*/
  327. py_funcname, /*PyObject *name,*/
  328. firstlineno, /*int firstlineno,*/
  329. $empty_bytes /*PyObject *lnotab*/
  330. );
  331. bad:
  332. Py_XDECREF(py_srcfile);
  333. Py_XDECREF(py_funcname);
  334. #endif
  335. return py_code;
  336. }
  337. #endif /* CYTHON_PROFILE */