Skip to content

Commit 9317f97

Browse files
tiranpablogsal
authored andcommitted
Add autoconf magic
1 parent 95c499a commit 9317f97

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

Include/internal/pycore_ceval.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ struct _ceval_runtime_state;
2727
#include "pycore_pystate.h" // _PyThreadState_GET()
2828
#include "pycore_frame.h" // _PyThreadState_GET()
2929

30+
#ifdef HAVE_PERF_TRAMPOLINE
3031
typedef PyObject* (*py_evaluator)(PyThreadState *, _PyInterpreterFrame *, int throwflag);
3132
typedef PyObject* (*py_trampoline)(py_evaluator, PyThreadState *, _PyInterpreterFrame *, int throwflag);
33+
#endif
3234

3335
extern void _Py_FinishPendingCalls(PyThreadState *tstate);
3436
extern void _PyEval_InitRuntimeState(struct _ceval_runtime_state *);
@@ -74,11 +76,13 @@ _PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int
7476
{
7577
EVAL_CALL_STAT_INC(EVAL_CALL_TOTAL);
7678
if (tstate->interp->eval_frame == NULL) {
79+
#ifdef HAVE_PERF_TRAMPOLINE
7780
PyCodeObject *co = frame->f_code;
7881
py_trampoline f = (py_trampoline)(co->co_trampoline);
7982
if (f) {
8083
return f(_PyEval_EvalFrameDefault, tstate, frame, throwflag);
8184
}
85+
#endif
8286
return _PyEval_EvalFrameDefault(tstate, frame, throwflag);
8387
}
8488
return tstate->interp->eval_frame(tstate, frame, throwflag);

Makefile.pre.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ OBJECT_OBJS= \
443443
Objects/cellobject.o \
444444
Objects/classobject.o \
445445
Objects/codeobject.o \
446-
Objects/asm_trampoline.o \
447446
Objects/complexobject.o \
448447
Objects/descrobject.o \
449448
Objects/enumobject.o \
@@ -476,7 +475,8 @@ OBJECT_OBJS= \
476475
Objects/unicodeobject.o \
477476
Objects/unicodectype.o \
478477
Objects/unionobject.o \
479-
Objects/weakrefobject.o
478+
Objects/weakrefobject.o \
479+
@PERF_TRAMPOLINE_OBJ@
480480

481481
DEEPFREEZE_OBJS = Python/deepfreeze/deepfreeze.o
482482

@@ -2319,6 +2319,9 @@ config.status: $(srcdir)/configure
23192319

23202320
.PRECIOUS: config.status $(BUILDPYTHON) Makefile Makefile.pre
23212321

2322+
Objects/asm_trampoline.o: $(srcdir)/Objects/asm_trampoline.sx
2323+
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $<
2324+
23222325
# Some make's put the object file in the current directory
23232326
.c.o:
23242327
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $<

Objects/asm_trampoline.s renamed to Objects/asm_trampoline.sx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.text
22
.globl _Py_trampoline_func_start
33
_Py_trampoline_func_start:
4+
#ifdef __x86_64__
45
push %rbp
56
mov %rsp,%rbp
67
mov %rdi,%rax
@@ -10,5 +11,9 @@ _Py_trampoline_func_start:
1011
call *%rax
1112
pop %rbp
1213
ret
14+
#endif // __x86_64__
15+
#ifdef __aarch64__
16+
TODO
17+
#endif
1318
.globl _Py_trampoline_func_end
1419
_Py_trampoline_func_end:

Objects/codeobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,13 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
300300
return 0;
301301
}
302302

303+
#ifdef HAVE_PERF_TRAMPOLINE
303304
extern void* _Py_trampoline_func_start;
304305
extern void* _Py_trampoline_func_end;
305306

306307
py_trampoline compile_trampoline(void) {
307308
char *memory = mmap(NULL, // address
308-
4096, // size
309+
PAGESIZE, // size
309310
PROT_READ | PROT_WRITE | PROT_EXEC,
310311
MAP_PRIVATE | MAP_ANONYMOUS,
311312
-1, // fd (not used here)
@@ -347,6 +348,7 @@ void perf_map_write_entry(FILE *method_file, const void* code_addr, unsigned int
347348
}
348349

349350
typedef PyObject* (*py_evaluator)(PyThreadState *, _PyInterpreterFrame *, int throwflag);
351+
#endif
350352

351353
static void
352354
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
@@ -363,6 +365,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
363365
Py_INCREF(con->qualname);
364366
co->co_qualname = con->qualname;
365367

368+
#ifdef HAVE_PERF_TRAMPOLINE
366369
py_trampoline f = compile_trampoline();
367370
FILE* pfile = perf_map_open(getpid());
368371
perf_map_write_entry(pfile, f, 4096,
@@ -371,6 +374,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
371374
perf_map_close(pfile);
372375

373376
co->co_trampoline = f;
377+
#endif
374378
co->co_flags = con->flags;
375379

376380
co->co_firstlineno = con->firstlineno;

configure

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,20 @@ case "$ac_sys_system" in
34253425
esac
34263426
AC_MSG_RESULT($SHLIBS)
34273427

3428+
dnl perf trampoline is Linux specific and requires an arch-specific
3429+
dnl trampoline in asssembly.
3430+
AC_MSG_CHECKING([perf trampoline])
3431+
AS_CASE([$host],
3432+
[x86_64-*-linux-*], [perf_trampoline=yes],
3433+
[perf_trampoline=no]
3434+
)
3435+
AC_MSG_RESULT([$perf_trampoline])
3436+
3437+
AS_VAR_IF([perf_trampoline], [yes], [
3438+
AC_DEFINE([HAVE_PERF_TRAMPOLINE], [1], [Define to 1 if you have the perf trampoline.])
3439+
PERF_TRAMPOLINE_OBJ=Objects/asm_trampoline.o
3440+
])
3441+
AC_SUBST([PERF_TRAMPOLINE_OBJ])
34283442

34293443
# checks for libraries
34303444
AC_CHECK_LIB(sendfile, sendfile)

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,9 @@
872872
/* Define to 1 if you have the `pause' function. */
873873
#undef HAVE_PAUSE
874874

875+
/* Define to 1 if you have the perf trampoline. */
876+
#undef HAVE_PERF_TRAMPOLINE
877+
875878
/* Define to 1 if you have the `pipe' function. */
876879
#undef HAVE_PIPE
877880

0 commit comments

Comments
 (0)