Skip to content

Commit 489d76e

Browse files
committed
gh-140421: Fix perf trampoline on older macOS
* Use mach_absolute_time() when clock_gettime() is not available. * Define MAP_ANONYMOUS to MAP_ANON if the latter is defined and the former is not. This matches what is done elsewhere in the code base in this situation, e.g. mmapmodule.c. * Don't build perf trampoline on ppc systems, where getting this to work would be a lot more effort.
1 parent 71db05a commit 489d76e

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Python/perf_jit_trampoline.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@
8080
# include <sys/syscall.h> // System call interface
8181
#endif
8282

83+
#if defined(__APPLE__)
84+
#include <AvailabilityMacros.h>
85+
#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) \
86+
&& (!defined(MAC_OS_X_VERSION_10_12) || \
87+
MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12)
88+
#define PY_PERF_JIT_USE_MACH_TIME
89+
#include <inttypes.h>
90+
#include <mach/mach_time.h> // mach_absolute_time
91+
#endif
92+
#endif
93+
8394
// =============================================================================
8495
// CONSTANTS AND CONFIGURATION
8596
// =============================================================================
@@ -284,6 +295,12 @@ static const intptr_t nanoseconds_per_second = 1000000000;
284295
* Returns: Current monotonic time in nanoseconds since an arbitrary epoch
285296
*/
286297
static int64_t get_current_monotonic_ticks(void) {
298+
#if defined(__APPLE__) && defined(PY_PERF_JIT_USE_MACH_TIME)
299+
/* clock_gettime() is not available before macOS 10.12.
300+
* mach_absolute_time() should always return a value in
301+
* nanoseconds on x86. */
302+
return (int64_t)mach_absolute_time();
303+
#else
287304
struct timespec ts;
288305
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
289306
Py_UNREACHABLE(); // Should never fail on supported systems
@@ -295,6 +312,7 @@ static int64_t get_current_monotonic_ticks(void) {
295312
result *= nanoseconds_per_second;
296313
result += ts.tv_nsec;
297314
return result;
315+
#endif
298316
}
299317

300318
/*

Python/perf_trampoline.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ any DWARF information available for them).
145145
#include <unistd.h> // sysconf()
146146
#include <sys/time.h> // gettimeofday()
147147

148+
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
149+
# define MAP_ANONYMOUS MAP_ANON
150+
#endif
148151

149152
#if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
150153
#define PY_HAVE_INVALIDATE_ICACHE

configure

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

configure.ac

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3704,7 +3704,10 @@ AC_MSG_CHECKING([perf trampoline])
37043704
AS_CASE([$PLATFORM_TRIPLET],
37053705
[x86_64-linux-gnu], [perf_trampoline=yes],
37063706
[aarch64-linux-gnu], [perf_trampoline=yes],
3707-
[darwin], [perf_trampoline=yes],
3707+
[darwin], AS_CASE([$host_cpu],
3708+
[p*pc*], [perf_trampoline=no],
3709+
[*], [perf_trampoline=yes]
3710+
),
37083711
[perf_trampoline=no]
37093712
)
37103713
AC_MSG_RESULT([$perf_trampoline])

0 commit comments

Comments
 (0)