Skip to content

Commit b726290

Browse files
committed
A bit more refactoring
1 parent eae5079 commit b726290

File tree

7 files changed

+91
-87
lines changed

7 files changed

+91
-87
lines changed

Include/internal/pycore_backoff.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ extern "C" {
1010
#endif
1111

1212
#include <assert.h>
13-
#include <stdbool.h>
14-
#include <stdint.h>
15-
16-
17-
typedef struct {
18-
uint16_t value_and_backoff;
19-
} _Py_BackoffCounter;
20-
13+
#include "pycore_structs.h" // _Py_BackoffCounter
2114

2215
/* 16-bit countdown counters using exponential backoff.
2316

Include/internal/pycore_code.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,13 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_structs.h" // _Py_CODEUNIT
1112
#include "pycore_stackref.h" // _PyStackRef
1213
#include "pycore_lock.h" // PyMutex
1314
#include "pycore_backoff.h" // _Py_BackoffCounter
1415
#include "pycore_tstate.h" // _PyThreadStateImpl
1516

1617

17-
/* Each instruction in a code object is a fixed-width value,
18-
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
19-
* opcode allows for larger values but the current limit is 3 uses
20-
* of EXTENDED_ARG (see Python/compile.c), for a maximum
21-
* 32-bit value. This aligns with the note in Python/compile.c
22-
* (compiler_addop_i_line) indicating that the max oparg value is
23-
* 2**32 - 1, rather than INT_MAX.
24-
*/
25-
26-
typedef union {
27-
uint16_t cache;
28-
struct {
29-
uint8_t code;
30-
uint8_t arg;
31-
} op;
32-
_Py_BackoffCounter counter; // First cache entry of specializable op
33-
} _Py_CODEUNIT;
34-
3518
#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
3619
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
3720

Include/internal/pycore_instruments.h

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# error "this header requires Py_BUILD_CORE define"
66
#endif
77

8-
#include "pycore_frame.h" // _PyInterpreterFrame
8+
#include "pycore_structs.h" // _Py_CODEUNIT
99

1010
#ifdef __cplusplus
1111
extern "C" {
@@ -34,39 +34,92 @@ int _PyMonitoring_GetLocalEvents(PyCodeObject *code, int tool_id, _PyMonitoringE
3434

3535
extern int
3636
_Py_call_instrumentation(PyThreadState *tstate, int event,
37-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
37+
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
3838

3939
extern int
40-
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame,
40+
_Py_call_instrumentation_line(PyThreadState *tstate, struct _PyInterpreterFrame* frame,
4141
_Py_CODEUNIT *instr, _Py_CODEUNIT *prev);
4242

4343
extern int
4444
_Py_call_instrumentation_instruction(
45-
PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
45+
PyThreadState *tstate, struct _PyInterpreterFrame* frame, _Py_CODEUNIT *instr);
4646

4747
_Py_CODEUNIT *
4848
_Py_call_instrumentation_jump(
4949
_Py_CODEUNIT *instr, PyThreadState *tstate, int event,
50-
_PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
50+
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *src, _Py_CODEUNIT *dest);
5151

5252
extern int
5353
_Py_call_instrumentation_arg(PyThreadState *tstate, int event,
54-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
54+
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg);
5555

5656
extern int
5757
_Py_call_instrumentation_2args(PyThreadState *tstate, int event,
58-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
58+
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
5959

6060
extern void
6161
_Py_call_instrumentation_exc2(PyThreadState *tstate, int event,
62-
_PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
62+
struct _PyInterpreterFrame *frame, _Py_CODEUNIT *instr, PyObject *arg0, PyObject *arg1);
6363

6464
extern int
6565
_Py_Instrumentation_GetLine(PyCodeObject *code, int index);
6666

6767
extern PyObject _PyInstrumentation_MISSING;
6868
extern PyObject _PyInstrumentation_DISABLE;
6969

70+
71+
/* Total tool ids available */
72+
#define PY_MONITORING_TOOL_IDS 8
73+
/* Count of all local monitoring events */
74+
#define _PY_MONITORING_LOCAL_EVENTS 11
75+
/* Count of all "real" monitoring events (not derived from other events) */
76+
#define _PY_MONITORING_UNGROUPED_EVENTS 16
77+
/* Count of all monitoring events */
78+
#define _PY_MONITORING_EVENTS 19
79+
80+
/* Tables of which tools are active for each monitored event. */
81+
typedef struct _Py_LocalMonitors {
82+
uint8_t tools[_PY_MONITORING_LOCAL_EVENTS];
83+
} _Py_LocalMonitors;
84+
85+
typedef struct _Py_GlobalMonitors {
86+
uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
87+
} _Py_GlobalMonitors;
88+
89+
/* Ancillary data structure used for instrumentation.
90+
Line instrumentation creates this with sufficient
91+
space for one entry per code unit. The total size
92+
of the data will be `bytes_per_entry * Py_SIZE(code)` */
93+
typedef struct {
94+
uint8_t bytes_per_entry;
95+
uint8_t data[1];
96+
} _PyCoLineInstrumentationData;
97+
98+
99+
/* Main data structure used for instrumentation.
100+
* This is allocated when needed for instrumentation
101+
*/
102+
typedef struct _PyCoMonitoringData {
103+
/* Monitoring specific to this code object */
104+
_Py_LocalMonitors local_monitors;
105+
/* Monitoring that is active on this code object */
106+
_Py_LocalMonitors active_monitors;
107+
/* The tools that are to be notified for events for the matching code unit */
108+
uint8_t *tools;
109+
/* The version of tools when they instrument the code */
110+
uintptr_t tool_versions[PY_MONITORING_TOOL_IDS];
111+
/* Information to support line events */
112+
_PyCoLineInstrumentationData *lines;
113+
/* The tools that are to be notified for line events for the matching code unit */
114+
uint8_t *line_tools;
115+
/* Information to support instruction events */
116+
/* The underlying instructions, which can themselves be instrumented */
117+
uint8_t *per_instruction_opcodes;
118+
/* The tools that are to be notified for instruction events for the matching code unit */
119+
uint8_t *per_instruction_tools;
120+
} _PyCoMonitoringData;
121+
122+
70123
#ifdef __cplusplus
71124
}
72125
#endif

Include/internal/pycore_runtime_structs.h

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,6 @@ extern "C" {
1717
#define FUNC_MAX_WATCHERS 8
1818
#define TYPE_MAX_WATCHERS 8
1919

20-
/******** Monitoring ********/
21-
22-
/* Total tool ids available */
23-
#define PY_MONITORING_TOOL_IDS 8
24-
/* Count of all local monitoring events */
25-
#define _PY_MONITORING_LOCAL_EVENTS 11
26-
/* Count of all "real" monitoring events (not derived from other events) */
27-
#define _PY_MONITORING_UNGROUPED_EVENTS 16
28-
/* Count of all monitoring events */
29-
#define _PY_MONITORING_EVENTS 19
30-
31-
/* Tables of which tools are active for each monitored event. */
32-
typedef struct _Py_LocalMonitors {
33-
uint8_t tools[_PY_MONITORING_LOCAL_EVENTS];
34-
} _Py_LocalMonitors;
35-
36-
typedef struct _Py_GlobalMonitors {
37-
uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
38-
} _Py_GlobalMonitors;
39-
40-
/* Ancillary data structure used for instrumentation.
41-
Line instrumentation creates this with sufficient
42-
space for one entry per code unit. The total size
43-
of the data will be `bytes_per_entry * Py_SIZE(code)` */
44-
typedef struct {
45-
uint8_t bytes_per_entry;
46-
uint8_t data[1];
47-
} _PyCoLineInstrumentationData;
48-
49-
50-
/* Main data structure used for instrumentation.
51-
* This is allocated when needed for instrumentation
52-
*/
53-
typedef struct _PyCoMonitoringData {
54-
/* Monitoring specific to this code object */
55-
_Py_LocalMonitors local_monitors;
56-
/* Monitoring that is active on this code object */
57-
_Py_LocalMonitors active_monitors;
58-
/* The tools that are to be notified for events for the matching code unit */
59-
uint8_t *tools;
60-
/* The version of tools when they instrument the code */
61-
uintptr_t tool_versions[PY_MONITORING_TOOL_IDS];
62-
/* Information to support line events */
63-
_PyCoLineInstrumentationData *lines;
64-
/* The tools that are to be notified for line events for the matching code unit */
65-
uint8_t *line_tools;
66-
/* Information to support instruction events */
67-
/* The underlying instructions, which can themselves be instrumented */
68-
uint8_t *per_instruction_opcodes;
69-
/* The tools that are to be notified for instruction events for the matching code unit */
70-
uint8_t *per_instruction_tools;
71-
} _PyCoMonitoringData;
72-
7320
typedef int (*_Py_pending_call_func)(void *);
7421

7522
struct _pending_call {
@@ -746,6 +693,8 @@ struct _Py_interp_static_objects {
746693
} singletons;
747694
};
748695

696+
#include "pycore_instruments.h"
697+
749698
/* PyInterpreterState holds the global state for one of the runtime's
750699
interpreters. Typically the initial (main) interpreter is the only one.
751700

Include/internal/pycore_structs.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ extern "C" {
1212
#include <stdint.h>
1313
#include <stdbool.h>
1414

15+
16+
typedef struct {
17+
uint16_t value_and_backoff;
18+
} _Py_BackoffCounter;
19+
20+
/* Each instruction in a code object is a fixed-width value,
21+
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
22+
* opcode allows for larger values but the current limit is 3 uses
23+
* of EXTENDED_ARG (see Python/compile.c), for a maximum
24+
* 32-bit value. This aligns with the note in Python/compile.c
25+
* (compiler_addop_i_line) indicating that the max oparg value is
26+
* 2**32 - 1, rather than INT_MAX.
27+
*/
28+
typedef union {
29+
uint16_t cache;
30+
struct {
31+
uint8_t code;
32+
uint8_t arg;
33+
} op;
34+
_Py_BackoffCounter counter; // First cache entry of specializable op
35+
} _Py_CODEUNIT;
36+
37+
1538
/* Abstract tree node. */
1639
typedef struct {
1740
PyObject_HEAD

Objects/genobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define _PY_INTERPRETER
44

55
#include "Python.h"
6+
#include "pycore_genobject.h"
7+
68
#include "pycore_call.h" // _PyObject_CallNoArgs()
79
#include "pycore_ceval.h" // _PyEval_EvalFrame()
810
#include "pycore_frame.h" // _PyInterpreterFrame

Objects/listobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
1414
#include "pycore_object.h" // _PyObject_GC_TRACK(), _PyDebugAllocatorStats()
1515
#include "pycore_tuple.h" // _PyTuple_FromArray()
16+
#include "pycore_typeobject.h" // _Py_TYPE_VERSION_LIST
1617
#include "pycore_setobject.h" // _PySet_NextEntry()
1718
#include <stddef.h>
1819

0 commit comments

Comments
 (0)