Skip to content

gh-109329: Count tier2 miss opcodes #110561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/cpython/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ typedef struct _gc_stats {

typedef struct _uop_stats {
uint64_t execution_count;
uint64_t miss;
} UOpStats;

#define _Py_UOP_HIST_SIZE 32
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ extern int _PyStaticCode_Init(PyCodeObject *co);
do { if (_Py_stats && PyFunction_Check(callable)) _Py_stats->call_stats.eval_calls[name]++; } while (0)
#define GC_STAT_ADD(gen, name, n) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0)
#define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
#define UOP_EXE_INC(opname) do { if (_Py_stats) _Py_stats->optimization_stats.opcode[opname].execution_count++; } while (0)
#define UOP_STAT_INC(opname, name) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have named this arg opcode (or OPCODE, per recent Discourse thread), since to me opname is a variable of type char * giving the opcode's name; opcode is the (named) constant representing the opcode, which it is. (I did a small double take when I saw assert(opname < 512). :-) But it's too late, and opname is used in several places above as well, so let's keep it this way.

#define OPT_UNSUPPORTED_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.unsupported_opcode[opname]++; } while (0)
#define OPT_HIST(length, name) \
do { \
Expand All @@ -308,7 +308,7 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
#define EVAL_CALL_STAT_INC_IF_FUNCTION(name, callable) ((void)0)
#define GC_STAT_ADD(gen, name, n) ((void)0)
#define OPT_STAT_INC(name) ((void)0)
#define UOP_EXE_INC(opname) ((void)0)
#define UOP_STAT_INC(opname, name) ((void)0)
#define OPT_UNSUPPORTED_OPCODE(opname) ((void)0)
#define OPT_HIST(length, name) ((void)0)
#endif // !Py_STATS
Expand Down
3 changes: 2 additions & 1 deletion Python/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#undef DEOPT_IF
#define DEOPT_IF(COND, INSTNAME) \
if ((COND)) { \
UOP_STAT_INC(INSTNAME, miss); \
goto deoptimize; \
}

Expand Down Expand Up @@ -93,7 +94,7 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
(int)(stack_pointer - _PyFrame_Stackbase(frame)));
pc++;
OPT_STAT_INC(uops_executed);
UOP_EXE_INC(opcode);
UOP_STAT_INC(opcode, execution_count);
#ifdef Py_STATS
trace_uop_execution_counter++;
#endif
Expand Down
3 changes: 3 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ print_optimization_stats(FILE *out, OptimizationStats *stats)
if (stats->opcode[i].execution_count) {
fprintf(out, "uops[%s].execution_count : %" PRIu64 "\n", names[i], stats->opcode[i].execution_count);
}
if (stats->opcode[i].miss) {
fprintf(out, "uops[%s].specialization.miss : %" PRIu64 "\n", names[i], stats->opcode[i].miss);
}
}

for (int i = 0; i < 256; i++) {
Expand Down
2 changes: 1 addition & 1 deletion Tools/scripts/summarize_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ def iter_optimization_tables(base_stats: Stats, head_stats: Stats | None = None)
],
)
yield Section(
"Uop stats",
"Uop execution stats",
"",
[
Table(
Expand Down