Skip to content

Commit 69a6e7b

Browse files
committed
Change bucketing. Change how execution length is calculated
1 parent 0dac9e5 commit 69a6e7b

File tree

7 files changed

+261
-120
lines changed

7 files changed

+261
-120
lines changed

Include/cpython/pystats.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ typedef struct _uop_stats {
100100
uint64_t execution_count;
101101
} UOpStats;
102102

103-
// Note that these should be set to support the largest possible trace length.
104-
// i.e. log2(MAX_LENGTH) + 1 === (_Py_UOP_HIST_SIZE + _Py_UOP_HIST_BIAS)
105-
#define _Py_UOP_HIST_SIZE 5
106-
#define _Py_UOP_HIST_BIAS 2
103+
#define _Py_UOP_HIST_SIZE 32
107104

108105
typedef struct _optimization_stats {
109106
uint64_t attempts;

Include/internal/pycore_code.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ extern int _PyStaticCode_Init(PyCodeObject *co);
285285
#define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
286286
#define UOP_EXE_INC(opname) do { if (_Py_stats) _Py_stats->optimization_stats.opcode[opname].execution_count++; } while (0)
287287
#define OPT_UNSUPPORTED_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.unsupported_opcode[opname]++; } while (0)
288-
#define OPT_HIST(length, name) do { if (_Py_stats) _Py_stats->optimization_stats.name[_Py_bit_length((length - 1) >> _Py_UOP_HIST_BIAS)]++; } while (0)
288+
#define OPT_HIST(length, name) \
289+
do { \
290+
if (_Py_stats) { \
291+
int bucket = _Py_bit_length(length - 1); \
292+
bucket = (bucket >= _Py_UOP_HIST_SIZE) ? _Py_UOP_HIST_SIZE - 1 : bucket; \
293+
_Py_stats->optimization_stats.name[bucket]++; \
294+
} \
295+
} while (0)
289296

290297
// Export for '_opcode' shared extension
291298
PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);

Python/bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3913,7 +3913,7 @@ dummy_func(
39133913
frame->prev_instr--; // Back up to just before destination
39143914
_PyFrame_SetStackPointer(frame, stack_pointer);
39153915
Py_DECREF(self);
3916-
OPT_HIST(pc, trace_run_length_hist);
3916+
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
39173917
return frame;
39183918
}
39193919

Python/executor.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
7979
int opcode;
8080
int oparg;
8181
uint64_t operand;
82+
#ifdef Py_STATS
83+
uint64_t trace_uop_execution_counter = 0;
84+
#endif
8285

8386
for (;;) {
8487
opcode = self->trace[pc].opcode;
@@ -94,6 +97,10 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
9497
pc++;
9598
OPT_STAT_INC(uops_executed);
9699
UOP_EXE_INC(opcode);
100+
#ifdef Py_STATS
101+
trace_uop_execution_counter++;
102+
#endif
103+
97104
switch (opcode) {
98105

99106
#include "executor_cases.c.h"
@@ -126,7 +133,7 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
126133
// On ERROR_IF we return NULL as the frame.
127134
// The caller recovers the frame from tstate->current_frame.
128135
DPRINTF(2, "Error: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
129-
OPT_HIST(pc, trace_run_length_hist);
136+
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
130137
_PyFrame_SetStackPointer(frame, stack_pointer);
131138
Py_DECREF(self);
132139
return NULL;
@@ -135,7 +142,7 @@ _PyUopExecute(_PyExecutorObject *executor, _PyInterpreterFrame *frame, PyObject
135142
// On DEOPT_IF we just repeat the last instruction.
136143
// This presumes nothing was popped from the stack (nor pushed).
137144
DPRINTF(2, "DEOPT: [Opcode %d, operand %" PRIu64 "]\n", opcode, operand);
138-
OPT_HIST(pc, trace_run_length_hist);
145+
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
139146
frame->prev_instr--; // Back up to just before destination
140147
_PyFrame_SetStackPointer(frame, stack_pointer);
141148
Py_DECREF(self);

Python/executor_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/specialize.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static void
215215
print_histogram(FILE *out, const char *name, uint64_t hist[_Py_UOP_HIST_SIZE])
216216
{
217217
for (int i = 0; i < _Py_UOP_HIST_SIZE; i++) {
218-
fprintf(out, "%s[%d]: %" PRIu64 "\n", name, (1 << (i + _Py_UOP_HIST_BIAS)), hist[i]);
218+
fprintf(out, "%s[%" PRIu64"]: %" PRIu64 "\n", name, (uint64_t)1 << i, hist[i]);
219219
}
220220
}
221221

0 commit comments

Comments
 (0)