@@ -15,39 +15,56 @@ extern "C" {
1515
1616#include "pycore_bitutils.h" // _Py_bit_length
1717
18- #define STAT_INC (opname , name ) do { if (_Py_stats) _Py_stats->opcode_stats[opname].specialization.name++; } while (0)
19- #define STAT_DEC (opname , name ) do { if (_Py_stats) _Py_stats->opcode_stats[opname].specialization.name--; } while (0)
20- #define OPCODE_EXE_INC (opname ) do { if (_Py_stats) _Py_stats->opcode_stats[opname].execution_count++; } while (0)
21- #define CALL_STAT_INC (name ) do { if (_Py_stats) _Py_stats->call_stats.name++; } while (0)
22- #define OBJECT_STAT_INC (name ) do { if (_Py_stats) _Py_stats->object_stats.name++; } while (0)
23- #define OBJECT_STAT_INC_COND (name , cond ) \
24- do { if (_Py_stats && cond) _Py_stats->object_stats.name++; } while (0)
25- #define EVAL_CALL_STAT_INC (name ) do { if (_Py_stats) _Py_stats->call_stats.eval_calls[name]++; } while (0)
26- #define EVAL_CALL_STAT_INC_IF_FUNCTION (name , callable ) \
27- do { if (_Py_stats && PyFunction_Check(callable)) _Py_stats->call_stats.eval_calls[name]++; } while (0)
28- #define GC_STAT_ADD (gen , name , n ) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0)
29- #define OPT_STAT_INC (name ) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
30- #define OPT_STAT_ADD (name , n ) do { if (_Py_stats) _Py_stats->optimization_stats.name += (n); } while (0)
31- #define UOP_STAT_INC (opname , name ) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
32- #define UOP_PAIR_INC (uopcode , lastuop ) \
33- do { \
34- if (lastuop && _Py_stats) { \
35- _Py_stats->optimization_stats.opcode[lastuop].pair_count[uopcode]++; \
36- } \
37- lastuop = uopcode; \
18+ #define STAT_INC (opname , name ) _Py_STATS_EXPR(opcode_stats[opname].specialization.name++)
19+ #define STAT_DEC (opname , name ) _Py_STATS_EXPR(opcode_stats[opname].specialization.name--)
20+ #define OPCODE_EXE_INC (opname ) _Py_STATS_EXPR(opcode_stats[opname].execution_count++)
21+ #define CALL_STAT_INC (name ) _Py_STATS_EXPR(call_stats.name++)
22+ #define OBJECT_STAT_INC (name ) _Py_STATS_EXPR(object_stats.name++)
23+ #define OBJECT_STAT_INC_COND (name , cond ) _Py_STATS_COND_EXPR(cond, object_stats.name++)
24+ #define EVAL_CALL_STAT_INC (name ) _Py_STATS_EXPR(call_stats.eval_calls[name]++)
25+ #define EVAL_CALL_STAT_INC_IF_FUNCTION (name , callable ) _Py_STATS_COND_EXPR(PyFunction_Check(callable), call_stats.eval_calls[name]++)
26+ #define GC_STAT_ADD (gen , name , n ) _Py_STATS_EXPR(gc_stats[(gen)].name += (n))
27+ #define OPT_STAT_INC (name ) _Py_STATS_EXPR(optimization_stats.name++)
28+ #define OPT_STAT_ADD (name , n ) _Py_STATS_EXPR(optimization_stats.name += (n))
29+ #define UOP_STAT_INC (opname , name ) \
30+ do { \
31+ PyStats *s = _PyStats_GET(); \
32+ if (s) { \
33+ assert(opname < 512); \
34+ s->optimization_stats.opcode[opname].name++; \
35+ } \
36+ } while (0)
37+ #define UOP_PAIR_INC (uopcode , lastuop ) \
38+ do { \
39+ PyStats *s = _PyStats_GET(); \
40+ if (lastuop && s) { \
41+ s->optimization_stats.opcode[lastuop].pair_count[uopcode]++; \
42+ } \
43+ lastuop = uopcode; \
3844 } while (0)
39- #define OPT_UNSUPPORTED_OPCODE (opname ) do { if (_Py_stats) _Py_stats-> optimization_stats.unsupported_opcode[opname]++; } while (0 )
40- #define OPT_ERROR_IN_OPCODE (opname ) do { if (_Py_stats) _Py_stats-> optimization_stats.error_in_opcode[opname]++; } while (0 )
45+ #define OPT_UNSUPPORTED_OPCODE (opname ) _Py_STATS_EXPR( optimization_stats.unsupported_opcode[opname]++)
46+ #define OPT_ERROR_IN_OPCODE (opname ) _Py_STATS_EXPR( optimization_stats.error_in_opcode[opname]++)
4147#define OPT_HIST (length , name ) \
4248 do { \
43- if (_Py_stats) { \
49+ PyStats *s = _PyStats_GET(); \
50+ if (s) { \
4451 int bucket = _Py_bit_length(length >= 1 ? length - 1 : 0); \
4552 bucket = (bucket >= _Py_UOP_HIST_SIZE) ? _Py_UOP_HIST_SIZE - 1 : bucket; \
46- _Py_stats ->optimization_stats.name[bucket]++; \
53+ s ->optimization_stats.name[bucket]++; \
4754 } \
4855 } while (0)
49- #define RARE_EVENT_STAT_INC (name ) do { if (_Py_stats) _Py_stats->rare_event_stats.name++; } while (0)
50- #define OPCODE_DEFERRED_INC (opname ) do { if (_Py_stats && opcode == opname) _Py_stats->opcode_stats[opname].specialization.deferred++; } while (0)
56+ #define RARE_EVENT_STAT_INC (name ) _Py_STATS_EXPR(rare_event_stats.name++)
57+ #define OPCODE_DEFERRED_INC (opname ) _Py_STATS_COND_EXPR(opcode==opname, opcode_stats[opname].specialization.deferred++)
58+
59+ #ifdef Py_GIL_DISABLED
60+ #define FT_STAT_MUTEX_SLEEP_INC () _Py_STATS_EXPR(ft_stats.mutex_sleeps++)
61+ #define FT_STAT_QSBR_POLL_INC () _Py_STATS_EXPR(ft_stats.qsbr_polls++)
62+ #define FT_STAT_WORLD_STOP_INC () _Py_STATS_EXPR(ft_stats.world_stops++)
63+ #else
64+ #define FT_STAT_MUTEX_SLEEP_INC ()
65+ #define FT_STAT_QSBR_POLL_INC ()
66+ #define FT_STAT_WORLD_STOP_INC ()
67+ #endif
5168
5269// Export for '_opcode' shared extension
5370PyAPI_FUNC (PyObject * ) _Py_GetSpecializationStats (void );
@@ -71,6 +88,9 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
7188#define OPT_HIST (length , name ) ((void)0)
7289#define RARE_EVENT_STAT_INC (name ) ((void)0)
7390#define OPCODE_DEFERRED_INC (opname ) ((void)0)
91+ #define FT_STAT_MUTEX_SLEEP_INC ()
92+ #define FT_STAT_QSBR_POLL_INC ()
93+ #define FT_STAT_WORLD_STOP_INC ()
7494#endif // !Py_STATS
7595
7696
@@ -90,6 +110,11 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
90110 RARE_EVENT_INTERP_INC(interp, name); \
91111 } while (0); \
92112
113+ PyStatus _PyStats_InterpInit (PyInterpreterState * );
114+ bool _PyStats_ThreadInit (PyInterpreterState * , _PyThreadStateImpl * );
115+ void _PyStats_ThreadFini (_PyThreadStateImpl * );
116+ void _PyStats_Attach (_PyThreadStateImpl * );
117+ void _PyStats_Detach (_PyThreadStateImpl * );
93118
94119#ifdef __cplusplus
95120}
0 commit comments