Skip to content

Commit 00c2237

Browse files
Move trampoline state to _PyRuntimeState.
1 parent bceb0bb commit 00c2237

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

Include/internal/pycore_ceval_state.h

+40
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,37 @@ extern "C" {
1313
#include "pycore_gil.h" // struct _gil_runtime_state
1414

1515

16+
typedef enum {
17+
PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state
18+
PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized
19+
PERF_STATUS_OK = 1, // Perf trampoline is ready to be executed
20+
} perf_status_t;
21+
22+
23+
#ifdef PY_HAVE_PERF_TRAMPOLINE
24+
struct code_arena_st;
25+
26+
struct trampoline_api_st {
27+
void* (*init_state)(void);
28+
void (*write_state)(void* state, const void *code_addr,
29+
unsigned int code_size, PyCodeObject* code);
30+
int (*free_state)(void* state);
31+
void *state;
32+
};
33+
#endif
34+
1635
struct _ceval_runtime_state {
36+
struct {
37+
#ifdef PY_HAVE_PERF_TRAMPOLINE
38+
perf_status_t status;
39+
Py_ssize_t extra_code_index;
40+
struct code_arena_st *code_arena;
41+
struct trampoline_api_st trampoline_api;
42+
FILE *map_file;
43+
#else
44+
int _not_used;
45+
#endif
46+
} perf;
1747
/* Request for checking signals. It is shared by all interpreters (see
1848
bpo-40513). Any thread of any interpreter can receive a signal, but only
1949
the main thread of the main interpreter can handle signals: see
@@ -22,6 +52,16 @@ struct _ceval_runtime_state {
2252
struct _gil_runtime_state gil;
2353
};
2454

55+
#ifdef PY_HAVE_PERF_TRAMPOLINE
56+
# define _PyEval_RUNTIME_PERF_INIT \
57+
{ \
58+
.status = PERF_STATUS_NO_INIT, \
59+
.extra_code_index = -1, \
60+
}
61+
#else
62+
# define _PyEval_RUNTIME_PERF_INIT {0}
63+
#endif
64+
2565

2666
struct _pending_calls {
2767
int busy;

Include/internal/pycore_runtime_init.h

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern "C" {
4141
.header = 1, \
4242
}, \
4343
}, \
44+
.ceval = { \
45+
.perf = _PyEval_RUNTIME_PERF_INIT, \
46+
}, \
4447
.gilstate = { \
4548
.check_enabled = 1, \
4649
/* A TSS key must be initialized with Py_tss_NEEDS_INIT \

Python/perf_trampoline.c

+14-30
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,6 @@ any DWARF information available for them).
134134
#include "pycore_frame.h"
135135
#include "pycore_interp.h"
136136

137-
typedef enum {
138-
PERF_STATUS_FAILED = -1, // Perf trampoline is in an invalid state
139-
PERF_STATUS_NO_INIT = 0, // Perf trampoline is not initialized
140-
PERF_STATUS_OK = 1, // Perf trampoline is ready to be executed
141-
} perf_status_t;
142137

143138
#ifdef PY_HAVE_PERF_TRAMPOLINE
144139

@@ -190,24 +185,13 @@ struct code_arena_st {
190185
};
191186

192187
typedef struct code_arena_st code_arena_t;
193-
194-
struct trampoline_api_st {
195-
void* (*init_state)(void);
196-
void (*write_state)(void* state, const void *code_addr,
197-
unsigned int code_size, PyCodeObject* code);
198-
int (*free_state)(void* state);
199-
void *state;
200-
};
201-
202188
typedef struct trampoline_api_st trampoline_api_t;
203189

204-
205-
static perf_status_t perf_status = PERF_STATUS_NO_INIT;
206-
static Py_ssize_t extra_code_index = -1;
207-
static code_arena_t *code_arena;
208-
static trampoline_api_t trampoline_api;
209-
210-
static FILE *perf_map_file;
190+
#define perf_status _PyRuntime.ceval.perf.status
191+
#define extra_code_index _PyRuntime.ceval.perf.extra_code_index
192+
#define perf_code_arena _PyRuntime.ceval.perf.code_arena
193+
#define trampoline_api _PyRuntime.ceval.perf.trampoline_api
194+
#define perf_map_file _PyRuntime.ceval.perf.map_file
211195

212196
static void *
213197
perf_map_get_file(void)
@@ -344,17 +328,17 @@ new_code_arena(void)
344328
new_arena->size = mem_size;
345329
new_arena->size_left = mem_size;
346330
new_arena->code_size = code_size;
347-
new_arena->prev = code_arena;
348-
code_arena = new_arena;
331+
new_arena->prev = perf_code_arena;
332+
perf_code_arena = new_arena;
349333
return 0;
350334
}
351335

352336
static void
353337
free_code_arenas(void)
354338
{
355-
code_arena_t *cur = code_arena;
339+
code_arena_t *cur = perf_code_arena;
356340
code_arena_t *prev;
357-
code_arena = NULL; // invalid static pointer
341+
perf_code_arena = NULL; // invalid static pointer
358342
while (cur) {
359343
munmap(cur->start_addr, cur->size);
360344
prev = cur->prev;
@@ -375,14 +359,14 @@ code_arena_new_code(code_arena_t *code_arena)
375359
static inline py_trampoline
376360
compile_trampoline(void)
377361
{
378-
if ((code_arena == NULL) ||
379-
(code_arena->size_left <= code_arena->code_size)) {
362+
if ((perf_code_arena == NULL) ||
363+
(perf_code_arena->size_left <= perf_code_arena->code_size)) {
380364
if (new_code_arena() < 0) {
381365
return NULL;
382366
}
383367
}
384-
assert(code_arena->size_left <= code_arena->size);
385-
return code_arena_new_code(code_arena);
368+
assert(perf_code_arena->size_left <= perf_code_arena->size);
369+
return code_arena_new_code(perf_code_arena);
386370
}
387371

388372
static PyObject *
@@ -405,7 +389,7 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame,
405389
goto default_eval;
406390
}
407391
trampoline_api.write_state(trampoline_api.state, new_trampoline,
408-
code_arena->code_size, co);
392+
perf_code_arena->code_size, co);
409393
_PyCode_SetExtra((PyObject *)co, extra_code_index,
410394
(void *)new_trampoline);
411395
f = new_trampoline;

Tools/c-analyzer/cpython/globals-to-fix.tsv

-5
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ Python/dtoa.c - p5s -
310310
Objects/obmalloc.c new_arena debug_stats -
311311

312312
# others
313-
Python/perf_trampoline.c - perf_map_file -
314313
Objects/unicodeobject.c - ucnhash_capi -
315314

316315
#-----------------------
@@ -321,10 +320,6 @@ Python/suggestions.c levenshtein_distance buffer -
321320

322321
# other
323322
Objects/object.c - _Py_RefTotal -
324-
Python/perf_trampoline.c - perf_status -
325-
Python/perf_trampoline.c - extra_code_index -
326-
Python/perf_trampoline.c - code_arena -
327-
Python/perf_trampoline.c - trampoline_api -
328323
Python/thread_pthread_stubs.h - py_tls_entries -
329324

330325

0 commit comments

Comments
 (0)