Skip to content

Commit 84b4533

Browse files
authored
gh-109329: Count tier2 opcode misses (#110561)
This keeps a separate 'miss' counter for each micro-opcode, incremented whenever a guard uop takes a deoptimization side exit.
1 parent c6fe086 commit 84b4533

File tree

5 files changed

+9
-4
lines changed

5 files changed

+9
-4
lines changed

Include/cpython/pystats.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ typedef struct _gc_stats {
9898

9999
typedef struct _uop_stats {
100100
uint64_t execution_count;
101+
uint64_t miss;
101102
} UOpStats;
102103

103104
#define _Py_UOP_HIST_SIZE 32

Include/internal/pycore_code.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ extern int _PyStaticCode_Init(PyCodeObject *co);
283283
do { if (_Py_stats && PyFunction_Check(callable)) _Py_stats->call_stats.eval_calls[name]++; } while (0)
284284
#define GC_STAT_ADD(gen, name, n) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0)
285285
#define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
286-
#define UOP_EXE_INC(opname) do { if (_Py_stats) _Py_stats->optimization_stats.opcode[opname].execution_count++; } while (0)
286+
#define UOP_STAT_INC(opname, name) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
287287
#define OPT_UNSUPPORTED_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.unsupported_opcode[opname]++; } while (0)
288288
#define OPT_HIST(length, name) \
289289
do { \
@@ -308,7 +308,7 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
308308
#define EVAL_CALL_STAT_INC_IF_FUNCTION(name, callable) ((void)0)
309309
#define GC_STAT_ADD(gen, name, n) ((void)0)
310310
#define OPT_STAT_INC(name) ((void)0)
311-
#define UOP_EXE_INC(opname) ((void)0)
311+
#define UOP_STAT_INC(opname, name) ((void)0)
312312
#define OPT_UNSUPPORTED_OPCODE(opname) ((void)0)
313313
#define OPT_HIST(length, name) ((void)0)
314314
#endif // !Py_STATS

Python/executor.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#undef DEOPT_IF
2626
#define DEOPT_IF(COND, INSTNAME) \
2727
if ((COND)) { \
28+
UOP_STAT_INC(INSTNAME, miss); \
2829
goto deoptimize; \
2930
}
3031

@@ -93,7 +94,7 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
9394
(int)(stack_pointer - _PyFrame_Stackbase(frame)));
9495
pc++;
9596
OPT_STAT_INC(uops_executed);
96-
UOP_EXE_INC(opcode);
97+
UOP_STAT_INC(opcode, execution_count);
9798
#ifdef Py_STATS
9899
trace_uop_execution_counter++;
99100
#endif

Python/specialize.c

+3
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ print_optimization_stats(FILE *out, OptimizationStats *stats)
248248
if (stats->opcode[i].execution_count) {
249249
fprintf(out, "uops[%s].execution_count : %" PRIu64 "\n", names[i], stats->opcode[i].execution_count);
250250
}
251+
if (stats->opcode[i].miss) {
252+
fprintf(out, "uops[%s].specialization.miss : %" PRIu64 "\n", names[i], stats->opcode[i].miss);
253+
}
251254
}
252255

253256
for (int i = 0; i < 256; i++) {

Tools/scripts/summarize_stats.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ def iter_optimization_tables(base_stats: Stats, head_stats: Stats | None = None)
10281028
],
10291029
)
10301030
yield Section(
1031-
"Uop stats",
1031+
"Uop execution stats",
10321032
"",
10331033
[
10341034
Table(

0 commit comments

Comments
 (0)