Skip to content

Commit 7547a29

Browse files
committed
Add perf trampoline support for macOS
1 parent 180b3eb commit 7547a29

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

Python/asm_trampoline.S

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
.text
2+
#if defined(__APPLE__)
3+
.globl __Py_trampoline_func_start
4+
#else
25
.globl _Py_trampoline_func_start
6+
#endif
37
# The following assembly is equivalent to:
48
# PyObject *
59
# trampoline(PyThreadState *ts, _PyInterpreterFrame *f,
610
# int throwflag, py_evaluator evaluator)
711
# {
812
# return evaluator(ts, f, throwflag);
913
# }
14+
#if defined(__APPLE__)
15+
__Py_trampoline_func_start:
16+
#else
1017
_Py_trampoline_func_start:
18+
#endif
1119
#ifdef __x86_64__
1220
#if defined(__CET__) && (__CET__ & 1)
1321
endbr64
@@ -35,9 +43,14 @@ _Py_trampoline_func_start:
3543
addi sp,sp,16
3644
jr ra
3745
#endif
46+
#if defined(__APPLE__)
47+
.globl __Py_trampoline_func_end
48+
__Py_trampoline_func_end:
49+
#else
3850
.globl _Py_trampoline_func_end
3951
_Py_trampoline_func_end:
4052
.section .note.GNU-stack,"",@progbits
53+
#endif
4154
# Note for indicating the assembly code supports CET
4255
#if defined(__x86_64__) && defined(__CET__) && (__CET__ & 1)
4356
.section .note.gnu.property,"a"

Python/perf_jit_trampoline.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,19 @@
6666
#ifdef PY_HAVE_PERF_TRAMPOLINE
6767

6868
/* Standard library includes for perf jitdump implementation */
69-
#include <elf.h> // ELF architecture constants
69+
#if defined(__linux__)
70+
# include <elf.h> // ELF architecture constants
71+
#endif
7072
#include <fcntl.h> // File control operations
7173
#include <stdio.h> // Standard I/O operations
7274
#include <stdlib.h> // Standard library functions
7375
#include <sys/mman.h> // Memory mapping functions (mmap)
7476
#include <sys/types.h> // System data types
7577
#include <unistd.h> // System calls (sysconf, getpid)
7678
#include <sys/time.h> // Time functions (gettimeofday)
77-
#include <sys/syscall.h> // System call interface
79+
#if defined(__linux__)
80+
# include <sys/syscall.h> // System call interface
81+
#endif
7882

7983
// =============================================================================
8084
// CONSTANTS AND CONFIGURATION
@@ -101,6 +105,16 @@
101105
* based on the actual unwind information requirements.
102106
*/
103107

108+
109+
/* These constants are defined inside <elf.h>, which we can't use outside of linux. */
110+
#if !defined(__linux__)
111+
# define EM_386 3
112+
# define EM_X86_64 62
113+
# define EM_ARM 40
114+
# define EM_AARCH64 183
115+
# define EM_RISCV 243
116+
#endif
117+
104118
/* Convenient access to the global trampoline API state */
105119
#define trampoline_api _PyRuntime.ceval.perf.trampoline_api
106120

@@ -194,7 +208,7 @@ struct BaseEvent {
194208
typedef struct {
195209
struct BaseEvent base; // Common event header
196210
uint32_t process_id; // Process ID where code was generated
197-
uint32_t thread_id; // Thread ID where code was generated
211+
uint64_t thread_id; // Thread ID where code was generated
198212
uint64_t vma; // Virtual memory address where code is loaded
199213
uint64_t code_address; // Address of the actual machine code
200214
uint64_t code_size; // Size of the machine code in bytes
@@ -1263,7 +1277,11 @@ static void perf_map_jit_write_entry(void *state, const void *code_addr,
12631277
ev.base.size = sizeof(ev) + (name_length+1) + size;
12641278
ev.base.time_stamp = get_current_monotonic_ticks();
12651279
ev.process_id = getpid();
1280+
#if defined(__APPLE__)
1281+
pthread_threadid_np(NULL, &ev.thread_id);
1282+
#else
12661283
ev.thread_id = syscall(SYS_gettid); // Get thread ID via system call
1284+
#endif
12671285
ev.vma = base; // Virtual memory address
12681286
ev.code_address = base; // Same as VMA for our use case
12691287
ev.code_size = size;

configure

Lines changed: 2 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3692,12 +3692,13 @@ case "$ac_sys_system" in
36923692
esac
36933693
AC_MSG_RESULT([$SHLIBS])
36943694

3695-
dnl perf trampoline is Linux specific and requires an arch-specific
3695+
dnl perf trampoline is Linux and macOS specific and requires an arch-specific
36963696
dnl trampoline in assembly.
36973697
AC_MSG_CHECKING([perf trampoline])
36983698
AS_CASE([$PLATFORM_TRIPLET],
36993699
[x86_64-linux-gnu], [perf_trampoline=yes],
37003700
[aarch64-linux-gnu], [perf_trampoline=yes],
3701+
[darwin], [perf_trampoline=yes],
37013702
[perf_trampoline=no]
37023703
)
37033704
AC_MSG_RESULT([$perf_trampoline])

0 commit comments

Comments
 (0)