Skip to content

Cacheline demote to improve performance #11101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Zend/zend_cpuinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,15 @@ static inline int zend_cpu_supports_pclmul(void) {
}
#endif

/* __builtin_cpu_supports has cldemote from gcc11 */
#if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
ZEND_NO_SANITIZE_ADDRESS
static inline int zend_cpu_supports_cldemote(void) {
#if PHP_HAVE_BUILTIN_CPU_INIT
__builtin_cpu_init();
#endif
return __builtin_cpu_supports("cldemote");
}
#endif

#endif
35 changes: 35 additions & 0 deletions ext/opcache/jit/zend_jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,31 @@ static zend_jit_trace_info *zend_jit_get_current_trace_info(void);
static uint32_t zend_jit_trace_find_exit_point(const void* addr);
#endif

#if ZEND_JIT_TARGET_X86 && defined(__linux__)
# if PHP_HAVE_BUILTIN_CPU_SUPPORTS && defined(__GNUC__) && (ZEND_GCC_VERSION >= 11000)
# define ZEND_JIT_SUPPORT_CLDEMOTE 1
# else
# define ZEND_JIT_SUPPORT_CLDEMOTE 0
# endif
#endif

#if ZEND_JIT_SUPPORT_CLDEMOTE
#include <immintrin.h>
#pragma GCC push_options
#pragma GCC target("cldemote")
// check cldemote by CPUID when JIT startup
static int cpu_support_cldemote = 0;
static inline void shared_cacheline_demote(uintptr_t start, size_t size) {
uintptr_t cache_line_base = start & ~0x3F;
do {
_cldemote((void *)cache_line_base);
// next cacheline start size
cache_line_base += 64;
} while (cache_line_base < start + size);
}
#pragma GCC pop_options
#endif

static int zend_jit_assign_to_variable(dasm_State **Dst,
const zend_op *opline,
zend_jit_addr var_use_addr,
Expand Down Expand Up @@ -973,6 +998,12 @@ static void *dasm_link_and_encode(dasm_State **dasm_state,

/* flush the hardware I-cache */
JIT_CACHE_FLUSH(entry, entry + size);
/* hint to the hardware to push out the cache line that contains the linear address */
#if ZEND_JIT_SUPPORT_CLDEMOTE
if (cpu_support_cldemote && JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
shared_cacheline_demote((uintptr_t)entry, size);
}
#endif
Comment on lines +1001 to +1006
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to do CLDEMOTE only for TRACEs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to do CLDEMOTE only for TRACEs?

You mentioned that Function JIT may produce more than 10M of code at a single request and CLDEMOTE takes time. So we support tracing JIT.


if (trace_num) {
zend_jit_trace_add_code(entry, dasm_getpclabel(dasm_state, 1));
Expand Down Expand Up @@ -4901,6 +4932,10 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, bool reattached)
zend_jit_gdb_init();
#endif

#if ZEND_JIT_SUPPORT_CLDEMOTE
cpu_support_cldemote = zend_cpu_supports_cldemote();
#endif

#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
zend_write_protect = pthread_jit_write_protect_supported_np();
#endif
Expand Down