Debian Codegraph @

Include/cpython/pystats.h

raw
1 // Statistics on Python performance.
2 //
3 // API:
4 //
5 // - _Py_INCREF_STAT_INC() and _Py_DECREF_STAT_INC() used by Py_INCREF()
6 // and Py_DECREF().
7 // - _Py_stats variable
8 //
9 // Functions of the sys module:
10 //
11 // - sys._stats_on()
12 // - sys._stats_off()
13 // - sys._stats_clear()
14 // - sys._stats_dump()
15 //
16 // Python must be built with ./configure --enable-pystats to define the
17 // Py_STATS macro.
18 //
19 // Define _PY_INTERPRETER macro to increment interpreter_increfs and
20 // interpreter_decrefs. Otherwise, increment increfs and decrefs.
21 //
22 // The number of incref operations counted by `incref` and
23 // `interpreter_incref` is the number of increment operations, which is
24 // not equal to the total of all reference counts. A single increment
25 // operation may increase the reference count of an object by more than
26 // one. For example, see `_Py_RefcntAdd`.
27
28 #ifndef Py_CPYTHON_PYSTATS_H
29 # error "this header file must not be included directly"
30 #endif
31
32 #define PYSTATS_MAX_UOP_ID 512
33
34 #define SPECIALIZATION_FAILURE_KINDS 36
35
36 /* Stats for determining who is calling PyEval_EvalFrame */
37 #define EVAL_CALL_TOTAL 0
38 #define EVAL_CALL_VECTOR 1
39 #define EVAL_CALL_GENERATOR 2
40 #define EVAL_CALL_LEGACY 3
41 #define EVAL_CALL_FUNCTION_VECTORCALL 4
42 #define EVAL_CALL_BUILD_CLASS 5
43 #define EVAL_CALL_SLOT 6
44 #define EVAL_CALL_FUNCTION_EX 7
45 #define EVAL_CALL_API 8
46 #define EVAL_CALL_METHOD 9
47
48 #define EVAL_CALL_KINDS 10
49
50 typedef struct _specialization_stats {
51 uint64_t success;
52 uint64_t failure;
53 uint64_t hit;
54 uint64_t deferred;
55 uint64_t miss;
56 uint64_t deopt;
57 uint64_t failure_kinds[SPECIALIZATION_FAILURE_KINDS];
58 } SpecializationStats;
59
60 typedef struct _opcode_stats {
61 SpecializationStats specialization;
62 uint64_t execution_count;
63 uint64_t pair_count[256];
64 } OpcodeStats;
65
66 typedef struct _call_stats {
67 uint64_t inlined_py_calls;
68 uint64_t pyeval_calls;
69 uint64_t frames_pushed;
70 uint64_t frame_objects_created;
71 uint64_t eval_calls[EVAL_CALL_KINDS];
72 } CallStats;
73
74 typedef struct _object_stats {
75 uint64_t increfs;
76 uint64_t decrefs;
77 uint64_t interpreter_increfs;
78 uint64_t interpreter_decrefs;
79 uint64_t allocations;
80 uint64_t allocations512;
81 uint64_t allocations4k;
82 uint64_t allocations_big;
83 uint64_t frees;
84 uint64_t to_freelist;
85 uint64_t from_freelist;
86 uint64_t inline_values;
87 uint64_t dict_materialized_on_request;
88 uint64_t dict_materialized_new_key;
89 uint64_t dict_materialized_too_big;
90 uint64_t dict_materialized_str_subclass;
91 uint64_t type_cache_hits;
92 uint64_t type_cache_misses;
93 uint64_t type_cache_dunder_hits;
94 uint64_t type_cache_dunder_misses;
95 uint64_t type_cache_collisions;
96 /* Temporary value used during GC */
97 uint64_t object_visits;
98 } ObjectStats;
99
100 typedef struct _gc_stats {
101 uint64_t collections;
102 uint64_t object_visits;
103 uint64_t objects_collected;
104 } GCStats;
105
106 typedef struct _uop_stats {
107 uint64_t execution_count;
108 uint64_t miss;
109 uint64_t pair_count[PYSTATS_MAX_UOP_ID + 1];
110 } UOpStats;
111
112 #define _Py_UOP_HIST_SIZE 32
113
114 typedef struct _optimization_stats {
115 uint64_t attempts;
116 uint64_t traces_created;
117 uint64_t traces_executed;
118 uint64_t uops_executed;
119 uint64_t trace_stack_overflow;
120 uint64_t trace_stack_underflow;
121 uint64_t trace_too_long;
122 uint64_t trace_too_short;
123 uint64_t inner_loop;
124 uint64_t recursive_call;
125 uint64_t low_confidence;
126 uint64_t executors_invalidated;
127 UOpStats opcode[PYSTATS_MAX_UOP_ID + 1];
128 uint64_t unsupported_opcode[256];
129 uint64_t trace_length_hist[_Py_UOP_HIST_SIZE];
130 uint64_t trace_run_length_hist[_Py_UOP_HIST_SIZE];
131 uint64_t optimized_trace_length_hist[_Py_UOP_HIST_SIZE];
132 uint64_t optimizer_attempts;
133 uint64_t optimizer_successes;
134 uint64_t optimizer_failure_reason_no_memory;
135 uint64_t remove_globals_builtins_changed;
136 uint64_t remove_globals_incorrect_keys;
137 uint64_t error_in_opcode[PYSTATS_MAX_UOP_ID + 1];
138 } OptimizationStats;
139
140 typedef struct _rare_event_stats {
141 /* Setting an object's class, obj.__class__ = ... */
142 uint64_t set_class;
143 /* Setting the bases of a class, cls.__bases__ = ... */
144 uint64_t set_bases;
145 /* Setting the PEP 523 frame eval function, _PyInterpreterState_SetFrameEvalFunc() */
146 uint64_t set_eval_frame_func;
147 /* Modifying the builtins, __builtins__.__dict__[var] = ... */
148 uint64_t builtin_dict;
149 /* Modifying a function, e.g. func.__defaults__ = ..., etc. */
150 uint64_t func_modification;
151 /* Modifying a dict that is being watched */
152 uint64_t watched_dict_modification;
153 uint64_t watched_globals_modification;
154 } RareEventStats;
155
156 typedef struct _stats {
157 OpcodeStats opcode_stats[256];
158 CallStats call_stats;
159 ObjectStats object_stats;
160 OptimizationStats optimization_stats;
161 RareEventStats rare_event_stats;
162 GCStats *gc_stats;
163 } PyStats;
164
165
166 // Export for shared extensions like 'math'
167 PyAPI_DATA(PyStats*) _Py_stats;
168
169 #ifdef _PY_INTERPRETER
170 # define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_increfs++; } while (0)
171 # define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.interpreter_decrefs++; } while (0)
172 #else
173 # define _Py_INCREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.increfs++; } while (0)
174 # define _Py_DECREF_STAT_INC() do { if (_Py_stats) _Py_stats->object_stats.decrefs++; } while (0)
175 #endif