Skip to content

Commit 7dd4d93

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.
1 parent dd2ffaa commit 7dd4d93

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

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)