From 0544db690df34322ecd36897449d42e85e3ea0c8 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 7 Apr 2024 17:08:46 +0100 Subject: [PATCH] ext/pcntl: adding pcntl_getcpu. using sched_getcpu under the hood (Linux and FreeBSD). Returns the current cpu id for the current process. For Linux, we need to see beyond the sole presence of the symbol to consider it. Mostly useful, for now, in the cpu affinity context since the os can migrate processes as it sees fits otherwise. --- ext/pcntl/config.m4 | 19 +++++++++++++++++++ ext/pcntl/pcntl.c | 11 ++++++++++- ext/pcntl/pcntl.stub.php | 4 ++++ ext/pcntl/pcntl_arginfo.h | 13 ++++++++++++- ext/pcntl/tests/pcntl_getcpu.phpt | 23 +++++++++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 ext/pcntl/tests/pcntl_getcpu.phpt diff --git a/ext/pcntl/config.m4 b/ext/pcntl/config.m4 index 89475c44e6f85..c4e70a50039de 100644 --- a/ext/pcntl/config.m4 +++ b/ext/pcntl/config.m4 @@ -9,6 +9,25 @@ if test "$PHP_PCNTL" != "no"; then AC_CHECK_FUNCS([sigaction], [], [AC_MSG_ERROR([pcntl: sigaction() not supported by this platform])]) AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx pidfd_open sched_setaffinity]) + dnl if unsupported, -1 means automatically ENOSYS in this context + AC_MSG_CHECKING([if sched_getcpu is supported]) + AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#include +int main(void) { + if (sched_getcpu() == -1) { + return 1; + } + return 0; +} + ]])],[ + AC_MSG_RESULT(yes) + AC_DEFINE([HAVE_SCHED_GETCPU],1,[Whether sched_getcpu is properly supported]) + ],[ + AC_MSG_RESULT(no) + ],[ + AC_MSG_RESULT([no, cross-compiling]) + ]) + AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include ]) PHP_NEW_EXTENSION(pcntl, pcntl.c php_signal.c, $ext_shared, cli, $PCNTL_CFLAGS -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) diff --git a/ext/pcntl/pcntl.c b/ext/pcntl/pcntl.c index 69b3b1f19acd7..da9af786b4aba 100644 --- a/ext/pcntl/pcntl.c +++ b/ext/pcntl/pcntl.c @@ -42,7 +42,7 @@ #endif #include -#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) +#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_SCHED_GETCPU) #include #if defined(__FreeBSD__) #include @@ -1613,6 +1613,15 @@ PHP_FUNCTION(pcntl_setcpuaffinity) } #endif +#if defined(HAVE_SCHED_GETCPU) +PHP_FUNCTION(pcntl_getcpu) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + RETURN_LONG(sched_getcpu()); +} +#endif + static void pcntl_interrupt_function(zend_execute_data *execute_data) { pcntl_signal_dispatch(); diff --git a/ext/pcntl/pcntl.stub.php b/ext/pcntl/pcntl.stub.php index da90057e437d0..39f9db2d8b91c 100644 --- a/ext/pcntl/pcntl.stub.php +++ b/ext/pcntl/pcntl.stub.php @@ -999,3 +999,7 @@ function pcntl_setns(?int $process_id = null, int $nstype = CLONE_NEWNET): bool function pcntl_getcpuaffinity(?int $process_id = null): array|false {} function pcntl_setcpuaffinity(?int $process_id = null, array $cpu_ids = []): bool {} #endif + +#ifdef HAVE_SCHED_GETCPU +function pcntl_getcpu(): int {} +#endif diff --git a/ext/pcntl/pcntl_arginfo.h b/ext/pcntl/pcntl_arginfo.h index b9ac54657abbe..c51128f1a8a98 100644 --- a/ext/pcntl/pcntl_arginfo.h +++ b/ext/pcntl/pcntl_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a61b0327f5c36ca91e19c5f370377794b7950dee */ + * Stub hash: 75eacf08a17e18c30fb2111bb742c36b18aa9ead */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_fork, 0, 0, IS_LONG, 0) ZEND_END_ARG_INFO() @@ -152,6 +152,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_setcpuaffinity, 0, 0, _IS_ ZEND_END_ARG_INFO() #endif +#if defined(HAVE_SCHED_GETCPU) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_pcntl_getcpu, 0, 0, IS_LONG, 0) +ZEND_END_ARG_INFO() +#endif + ZEND_FUNCTION(pcntl_fork); ZEND_FUNCTION(pcntl_waitpid); ZEND_FUNCTION(pcntl_wait); @@ -205,6 +210,9 @@ ZEND_FUNCTION(pcntl_getcpuaffinity); #if defined(HAVE_SCHED_SETAFFINITY) ZEND_FUNCTION(pcntl_setcpuaffinity); #endif +#if defined(HAVE_SCHED_GETCPU) +ZEND_FUNCTION(pcntl_getcpu); +#endif static const zend_function_entry ext_functions[] = { ZEND_FE(pcntl_fork, arginfo_pcntl_fork) @@ -260,6 +268,9 @@ static const zend_function_entry ext_functions[] = { #endif #if defined(HAVE_SCHED_SETAFFINITY) ZEND_FE(pcntl_setcpuaffinity, arginfo_pcntl_setcpuaffinity) +#endif +#if defined(HAVE_SCHED_GETCPU) + ZEND_FE(pcntl_getcpu, arginfo_pcntl_getcpu) #endif ZEND_FE_END }; diff --git a/ext/pcntl/tests/pcntl_getcpu.phpt b/ext/pcntl/tests/pcntl_getcpu.phpt new file mode 100644 index 0000000000000..3275af5b953ff --- /dev/null +++ b/ext/pcntl/tests/pcntl_getcpu.phpt @@ -0,0 +1,23 @@ +--TEST-- +pcntl_getcpu() +--EXTENSIONS-- +pcntl +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +bool(true) +int(1)