Skip to content

Commit d8f2900

Browse files
committed
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. Clos GH-13908
1 parent dd2ffaa commit d8f2900

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ PHP NEWS
127127
. Added pcntl_getaffinity/pcntl_setaffinity. (David Carlier)
128128
. Updated pcntl_get_signal_handler signal id upper limit to be
129129
more in line with platforms limits. (David Carlier)
130+
. Added pcntl_getcpu for Linux/FreeBSD. (David Carlier)
130131

131132
- PCRE:
132133
. Upgrade bundled pcre2lib to version 10.43. (nielsdos)

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ PHP 8.4 UPGRADE NOTES
447447
to share resources with other processes within this context.
448448
. Added pcntl_getaffinity to get the cpu(s) bound to a process and
449449
pcntl_setaffinity to bind 1 or more cpus to a process.
450+
. Added pcntl_getcpu to get the cpu id from where the current process runs.
450451

451452
- Sodium:
452453
. Added the sodium_crypto_aead_aegis128l_*() and sodium_crypto_aead_aegis256l_*()

ext/pcntl/config.m4

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ if test "$PHP_PCNTL" != "no"; then
99
AC_CHECK_FUNCS([sigaction], [], [AC_MSG_ERROR([pcntl: sigaction() not supported by this platform])])
1010
AC_CHECK_FUNCS([getpriority setpriority wait3 wait4 sigwaitinfo sigtimedwait unshare rfork forkx pidfd_open sched_setaffinity])
1111

12+
dnl if unsupported, -1 means automatically ENOSYS in this context
13+
AC_MSG_CHECKING([if sched_getcpu is supported])
14+
AC_RUN_IFELSE([AC_LANG_SOURCE([[
15+
#include <sched.h>
16+
int main(void) {
17+
if (sched_getcpu() == -1) {
18+
return 1;
19+
}
20+
return 0;
21+
}
22+
]])],[
23+
AC_MSG_RESULT(yes)
24+
AC_DEFINE([HAVE_SCHED_GETCPU],1,[Whether sched_getcpu is properly supported])
25+
],[
26+
AC_MSG_RESULT(no)
27+
],[
28+
AC_MSG_RESULT([no, cross-compiling])
29+
])
30+
1231
AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include <signal.h>])
1332

1433
PHP_NEW_EXTENSION(pcntl, pcntl.c php_signal.c, $ext_shared, cli, $PCNTL_CFLAGS -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

ext/pcntl/pcntl.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#endif
4343

4444
#include <errno.h>
45-
#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY)
45+
#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY) || defined(HAVE_SCHED_GETCPU)
4646
#include <sched.h>
4747
#if defined(__FreeBSD__)
4848
#include <sys/types.h>
@@ -1613,6 +1613,15 @@ PHP_FUNCTION(pcntl_setcpuaffinity)
16131613
}
16141614
#endif
16151615

1616+
#if defined(HAVE_SCHED_GETCPU)
1617+
PHP_FUNCTION(pcntl_getcpu)
1618+
{
1619+
ZEND_PARSE_PARAMETERS_NONE();
1620+
1621+
RETURN_LONG(sched_getcpu());
1622+
}
1623+
#endif
1624+
16161625
static void pcntl_interrupt_function(zend_execute_data *execute_data)
16171626
{
16181627
pcntl_signal_dispatch();

ext/pcntl/pcntl.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,7 @@ function pcntl_setns(?int $process_id = null, int $nstype = CLONE_NEWNET): bool
999999
function pcntl_getcpuaffinity(?int $process_id = null): array|false {}
10001000
function pcntl_setcpuaffinity(?int $process_id = null, array $cpu_ids = []): bool {}
10011001
#endif
1002+
1003+
#ifdef HAVE_SCHED_GETCPU
1004+
function pcntl_getcpu(): int {}
1005+
#endif

ext/pcntl/pcntl_arginfo.h

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

ext/pcntl/tests/pcntl_getcpu.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
pcntl_getcpu()
3+
--EXTENSIONS--
4+
pcntl
5+
--SKIPIF--
6+
<?php
7+
if (!function_exists("pcntl_getcpu")) die("skip pcntl_getcpu() is not available");
8+
if (!function_exists("pcntl_setcpuaffinity")) die("skip pcntl_setcpuaffinity() is not available");
9+
if (getenv('TRAVIS')) die('skip Currently fails on Travis');
10+
?>
11+
--FILE--
12+
<?php
13+
$pid = pcntl_fork();
14+
if ($pid == -1) {
15+
die("fork failed");
16+
} else if ($pid == 0) {
17+
var_dump(pcntl_setcpuaffinity(null, [1]));
18+
var_dump(pcntl_getcpu());
19+
}
20+
?>
21+
--EXPECTF--
22+
bool(true)
23+
int(1)

0 commit comments

Comments
 (0)