Debian Codegraph @

Modules/clinic/_lsprof.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
10 #include "pycore_modsupport.h" // _PyArg_CheckPositional()
11
12 PyDoc_STRVAR(_lsprof_Profiler_getstats__doc__,
13 "getstats($self, /)\n"
14 "--\n"
15 "\n"
16 "list of profiler_entry objects.\n"
17 "\n"
18 "getstats() -> list of profiler_entry objects\n"
19 "\n"
20 "Return all information collected by the profiler.\n"
21 "Each profiler_entry is a tuple-like object with the\n"
22 "following attributes:\n"
23 "\n"
24 " code code object\n"
25 " callcount how many times this was called\n"
26 " reccallcount how many times called recursively\n"
27 " totaltime total time in this entry\n"
28 " inlinetime inline time in this entry (not in subcalls)\n"
29 " calls details of the calls\n"
30 "\n"
31 "The calls attribute is either None or a list of\n"
32 "profiler_subentry objects:\n"
33 "\n"
34 " code called code object\n"
35 " callcount how many times this is called\n"
36 " reccallcount how many times this is called recursively\n"
37 " totaltime total time spent in this call\n"
38 " inlinetime inline time (not in further subcalls)");
39
40 #define _LSPROF_PROFILER_GETSTATS_METHODDEF \
41 {"getstats", _PyCFunction_CAST(_lsprof_Profiler_getstats), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _lsprof_Profiler_getstats__doc__},
42
43 static PyObject *
44 _lsprof_Profiler_getstats_impl(ProfilerObject *self, PyTypeObject *cls);
45
46 static PyObject *
47 _lsprof_Profiler_getstats(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
48 {
49 PyObject *return_value = NULL;
50
51 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
52 PyErr_SetString(PyExc_TypeError, "getstats() takes no arguments");
53 goto exit;
54 }
55 Py_BEGIN_CRITICAL_SECTION(self);
56 return_value = _lsprof_Profiler_getstats_impl((ProfilerObject *)self, cls);
57 Py_END_CRITICAL_SECTION();
58
59 exit:
60 return return_value;
61 }
62
63 PyDoc_STRVAR(_lsprof_Profiler__pystart_callback__doc__,
64 "_pystart_callback($self, code, instruction_offset, /)\n"
65 "--\n"
66 "\n");
67
68 #define _LSPROF_PROFILER__PYSTART_CALLBACK_METHODDEF \
69 {"_pystart_callback", _PyCFunction_CAST(_lsprof_Profiler__pystart_callback), METH_FASTCALL, _lsprof_Profiler__pystart_callback__doc__},
70
71 static PyObject *
72 _lsprof_Profiler__pystart_callback_impl(ProfilerObject *self, PyObject *code,
73 PyObject *instruction_offset);
74
75 static PyObject *
76 _lsprof_Profiler__pystart_callback(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
77 {
78 PyObject *return_value = NULL;
79 PyObject *code;
80 PyObject *instruction_offset;
81
82 if (!_PyArg_CheckPositional("_pystart_callback", nargs, 2, 2)) {
83 goto exit;
84 }
85 code = args[0];
86 instruction_offset = args[1];
87 Py_BEGIN_CRITICAL_SECTION(self);
88 return_value = _lsprof_Profiler__pystart_callback_impl((ProfilerObject *)self, code, instruction_offset);
89 Py_END_CRITICAL_SECTION();
90
91 exit:
92 return return_value;
93 }
94
95 PyDoc_STRVAR(_lsprof_Profiler__pythrow_callback__doc__,
96 "_pythrow_callback($self, code, instruction_offset, exception, /)\n"
97 "--\n"
98 "\n");
99
100 #define _LSPROF_PROFILER__PYTHROW_CALLBACK_METHODDEF \
101 {"_pythrow_callback", _PyCFunction_CAST(_lsprof_Profiler__pythrow_callback), METH_FASTCALL, _lsprof_Profiler__pythrow_callback__doc__},
102
103 static PyObject *
104 _lsprof_Profiler__pythrow_callback_impl(ProfilerObject *self, PyObject *code,
105 PyObject *instruction_offset,
106 PyObject *exception);
107
108 static PyObject *
109 _lsprof_Profiler__pythrow_callback(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
110 {
111 PyObject *return_value = NULL;
112 PyObject *code;
113 PyObject *instruction_offset;
114 PyObject *exception;
115
116 if (!_PyArg_CheckPositional("_pythrow_callback", nargs, 3, 3)) {
117 goto exit;
118 }
119 code = args[0];
120 instruction_offset = args[1];
121 exception = args[2];
122 Py_BEGIN_CRITICAL_SECTION(self);
123 return_value = _lsprof_Profiler__pythrow_callback_impl((ProfilerObject *)self, code, instruction_offset, exception);
124 Py_END_CRITICAL_SECTION();
125
126 exit:
127 return return_value;
128 }
129
130 PyDoc_STRVAR(_lsprof_Profiler__pyreturn_callback__doc__,
131 "_pyreturn_callback($self, code, instruction_offset, retval, /)\n"
132 "--\n"
133 "\n");
134
135 #define _LSPROF_PROFILER__PYRETURN_CALLBACK_METHODDEF \
136 {"_pyreturn_callback", _PyCFunction_CAST(_lsprof_Profiler__pyreturn_callback), METH_FASTCALL, _lsprof_Profiler__pyreturn_callback__doc__},
137
138 static PyObject *
139 _lsprof_Profiler__pyreturn_callback_impl(ProfilerObject *self,
140 PyObject *code,
141 PyObject *instruction_offset,
142 PyObject *retval);
143
144 static PyObject *
145 _lsprof_Profiler__pyreturn_callback(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
146 {
147 PyObject *return_value = NULL;
148 PyObject *code;
149 PyObject *instruction_offset;
150 PyObject *retval;
151
152 if (!_PyArg_CheckPositional("_pyreturn_callback", nargs, 3, 3)) {
153 goto exit;
154 }
155 code = args[0];
156 instruction_offset = args[1];
157 retval = args[2];
158 Py_BEGIN_CRITICAL_SECTION(self);
159 return_value = _lsprof_Profiler__pyreturn_callback_impl((ProfilerObject *)self, code, instruction_offset, retval);
160 Py_END_CRITICAL_SECTION();
161
162 exit:
163 return return_value;
164 }
165
166 PyDoc_STRVAR(_lsprof_Profiler__ccall_callback__doc__,
167 "_ccall_callback($self, code, instruction_offset, callable, self_arg, /)\n"
168 "--\n"
169 "\n");
170
171 #define _LSPROF_PROFILER__CCALL_CALLBACK_METHODDEF \
172 {"_ccall_callback", _PyCFunction_CAST(_lsprof_Profiler__ccall_callback), METH_FASTCALL, _lsprof_Profiler__ccall_callback__doc__},
173
174 static PyObject *
175 _lsprof_Profiler__ccall_callback_impl(ProfilerObject *self, PyObject *code,
176 PyObject *instruction_offset,
177 PyObject *callable, PyObject *self_arg);
178
179 static PyObject *
180 _lsprof_Profiler__ccall_callback(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
181 {
182 PyObject *return_value = NULL;
183 PyObject *code;
184 PyObject *instruction_offset;
185 PyObject *callable;
186 PyObject *self_arg;
187
188 if (!_PyArg_CheckPositional("_ccall_callback", nargs, 4, 4)) {
189 goto exit;
190 }
191 code = args[0];
192 instruction_offset = args[1];
193 callable = args[2];
194 self_arg = args[3];
195 Py_BEGIN_CRITICAL_SECTION(self);
196 return_value = _lsprof_Profiler__ccall_callback_impl((ProfilerObject *)self, code, instruction_offset, callable, self_arg);
197 Py_END_CRITICAL_SECTION();
198
199 exit:
200 return return_value;
201 }
202
203 PyDoc_STRVAR(_lsprof_Profiler__creturn_callback__doc__,
204 "_creturn_callback($self, code, instruction_offset, callable, self_arg,\n"
205 " /)\n"
206 "--\n"
207 "\n");
208
209 #define _LSPROF_PROFILER__CRETURN_CALLBACK_METHODDEF \
210 {"_creturn_callback", _PyCFunction_CAST(_lsprof_Profiler__creturn_callback), METH_FASTCALL, _lsprof_Profiler__creturn_callback__doc__},
211
212 static PyObject *
213 _lsprof_Profiler__creturn_callback_impl(ProfilerObject *self, PyObject *code,
214 PyObject *instruction_offset,
215 PyObject *callable,
216 PyObject *self_arg);
217
218 static PyObject *
219 _lsprof_Profiler__creturn_callback(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
220 {
221 PyObject *return_value = NULL;
222 PyObject *code;
223 PyObject *instruction_offset;
224 PyObject *callable;
225 PyObject *self_arg;
226
227 if (!_PyArg_CheckPositional("_creturn_callback", nargs, 4, 4)) {
228 goto exit;
229 }
230 code = args[0];
231 instruction_offset = args[1];
232 callable = args[2];
233 self_arg = args[3];
234 Py_BEGIN_CRITICAL_SECTION(self);
235 return_value = _lsprof_Profiler__creturn_callback_impl((ProfilerObject *)self, code, instruction_offset, callable, self_arg);
236 Py_END_CRITICAL_SECTION();
237
238 exit:
239 return return_value;
240 }
241
242 PyDoc_STRVAR(_lsprof_Profiler_enable__doc__,
243 "enable($self, /, subcalls=True, builtins=True)\n"
244 "--\n"
245 "\n"
246 "Start collecting profiling information.\n"
247 "\n"
248 " subcalls\n"
249 " If True, also records for each function\n"
250 " statistics separated according to its current caller.\n"
251 " builtins\n"
252 " If True, records the time spent in\n"
253 " built-in functions separately from their caller.");
254
255 #define _LSPROF_PROFILER_ENABLE_METHODDEF \
256 {"enable", _PyCFunction_CAST(_lsprof_Profiler_enable), METH_FASTCALL|METH_KEYWORDS, _lsprof_Profiler_enable__doc__},
257
258 static PyObject *
259 _lsprof_Profiler_enable_impl(ProfilerObject *self, int subcalls,
260 int builtins);
261
262 static PyObject *
263 _lsprof_Profiler_enable(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
264 {
265 PyObject *return_value = NULL;
266 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
267
268 #define NUM_KEYWORDS 2
269 static struct {
270 PyGC_Head _this_is_not_used;
271 PyObject_VAR_HEAD
272 Py_hash_t ob_hash;
273 PyObject *ob_item[NUM_KEYWORDS];
274 } _kwtuple = {
275 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
276 .ob_hash = -1,
277 .ob_item = { &_Py_ID(subcalls), &_Py_ID(builtins), },
278 };
279 #undef NUM_KEYWORDS
280 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
281
282 #else // !Py_BUILD_CORE
283 # define KWTUPLE NULL
284 #endif // !Py_BUILD_CORE
285
286 static const char * const _keywords[] = {"subcalls", "builtins", NULL};
287 static _PyArg_Parser _parser = {
288 .keywords = _keywords,
289 .fname = "enable",
290 .kwtuple = KWTUPLE,
291 };
292 #undef KWTUPLE
293 PyObject *argsbuf[2];
294 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
295 int subcalls = 1;
296 int builtins = 1;
297
298 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
299 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
300 if (!args) {
301 goto exit;
302 }
303 if (!noptargs) {
304 goto skip_optional_pos;
305 }
306 if (args[0]) {
307 subcalls = PyObject_IsTrue(args[0]);
308 if (subcalls < 0) {
309 goto exit;
310 }
311 if (!--noptargs) {
312 goto skip_optional_pos;
313 }
314 }
315 builtins = PyObject_IsTrue(args[1]);
316 if (builtins < 0) {
317 goto exit;
318 }
319 skip_optional_pos:
320 Py_BEGIN_CRITICAL_SECTION(self);
321 return_value = _lsprof_Profiler_enable_impl((ProfilerObject *)self, subcalls, builtins);
322 Py_END_CRITICAL_SECTION();
323
324 exit:
325 return return_value;
326 }
327
328 PyDoc_STRVAR(_lsprof_Profiler_disable__doc__,
329 "disable($self, /)\n"
330 "--\n"
331 "\n"
332 "Stop collecting profiling information.");
333
334 #define _LSPROF_PROFILER_DISABLE_METHODDEF \
335 {"disable", (PyCFunction)_lsprof_Profiler_disable, METH_NOARGS, _lsprof_Profiler_disable__doc__},
336
337 static PyObject *
338 _lsprof_Profiler_disable_impl(ProfilerObject *self);
339
340 static PyObject *
341 _lsprof_Profiler_disable(PyObject *self, PyObject *Py_UNUSED(ignored))
342 {
343 PyObject *return_value = NULL;
344
345 Py_BEGIN_CRITICAL_SECTION(self);
346 return_value = _lsprof_Profiler_disable_impl((ProfilerObject *)self);
347 Py_END_CRITICAL_SECTION();
348
349 return return_value;
350 }
351
352 PyDoc_STRVAR(_lsprof_Profiler_clear__doc__,
353 "clear($self, /)\n"
354 "--\n"
355 "\n"
356 "Clear all profiling information collected so far.");
357
358 #define _LSPROF_PROFILER_CLEAR_METHODDEF \
359 {"clear", (PyCFunction)_lsprof_Profiler_clear, METH_NOARGS, _lsprof_Profiler_clear__doc__},
360
361 static PyObject *
362 _lsprof_Profiler_clear_impl(ProfilerObject *self);
363
364 static PyObject *
365 _lsprof_Profiler_clear(PyObject *self, PyObject *Py_UNUSED(ignored))
366 {
367 PyObject *return_value = NULL;
368
369 Py_BEGIN_CRITICAL_SECTION(self);
370 return_value = _lsprof_Profiler_clear_impl((ProfilerObject *)self);
371 Py_END_CRITICAL_SECTION();
372
373 return return_value;
374 }
375
376 PyDoc_STRVAR(profiler_init__doc__,
377 "Profiler(timer=None, timeunit=0.0, subcalls=True, builtins=True)\n"
378 "--\n"
379 "\n"
380 "Build a profiler object using the specified timer function.\n"
381 "\n"
382 "The default timer is a fast built-in one based on real time.\n"
383 "For custom timer functions returning integers, \'timeunit\' can\n"
384 "be a float specifying a scale (that is, how long each integer unit\n"
385 "is, in seconds).");
386
387 static int
388 profiler_init_impl(ProfilerObject *self, PyObject *timer, double timeunit,
389 int subcalls, int builtins);
390
391 static int
392 profiler_init(PyObject *self, PyObject *args, PyObject *kwargs)
393 {
394 int return_value = -1;
395 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
396
397 #define NUM_KEYWORDS 4
398 static struct {
399 PyGC_Head _this_is_not_used;
400 PyObject_VAR_HEAD
401 Py_hash_t ob_hash;
402 PyObject *ob_item[NUM_KEYWORDS];
403 } _kwtuple = {
404 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
405 .ob_hash = -1,
406 .ob_item = { &_Py_ID(timer), &_Py_ID(timeunit), &_Py_ID(subcalls), &_Py_ID(builtins), },
407 };
408 #undef NUM_KEYWORDS
409 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
410
411 #else // !Py_BUILD_CORE
412 # define KWTUPLE NULL
413 #endif // !Py_BUILD_CORE
414
415 static const char * const _keywords[] = {"timer", "timeunit", "subcalls", "builtins", NULL};
416 static _PyArg_Parser _parser = {
417 .keywords = _keywords,
418 .fname = "Profiler",
419 .kwtuple = KWTUPLE,
420 };
421 #undef KWTUPLE
422 PyObject *argsbuf[4];
423 PyObject * const *fastargs;
424 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
425 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
426 PyObject *timer = NULL;
427 double timeunit = 0.0;
428 int subcalls = 1;
429 int builtins = 1;
430
431 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
432 /*minpos*/ 0, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
433 if (!fastargs) {
434 goto exit;
435 }
436 if (!noptargs) {
437 goto skip_optional_pos;
438 }
439 if (fastargs[0]) {
440 timer = fastargs[0];
441 if (!--noptargs) {
442 goto skip_optional_pos;
443 }
444 }
445 if (fastargs[1]) {
446 if (PyFloat_CheckExact(fastargs[1])) {
447 timeunit = PyFloat_AS_DOUBLE(fastargs[1]);
448 }
449 else
450 {
451 timeunit = PyFloat_AsDouble(fastargs[1]);
452 if (timeunit == -1.0 && PyErr_Occurred()) {
453 goto exit;
454 }
455 }
456 if (!--noptargs) {
457 goto skip_optional_pos;
458 }
459 }
460 if (fastargs[2]) {
461 subcalls = PyObject_IsTrue(fastargs[2]);
462 if (subcalls < 0) {
463 goto exit;
464 }
465 if (!--noptargs) {
466 goto skip_optional_pos;
467 }
468 }
469 builtins = PyObject_IsTrue(fastargs[3]);
470 if (builtins < 0) {
471 goto exit;
472 }
473 skip_optional_pos:
474 return_value = profiler_init_impl((ProfilerObject *)self, timer, timeunit, subcalls, builtins);
475
476 exit:
477 return return_value;
478 }
479 /*[clinic end generated code: output=af26a0b0ddcc3351 input=a9049054013a1b77]*/