Skip to content

ext/pcntl: cpu affinity api introduction. #13893

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion ext/pcntl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ if test "$PHP_PCNTL" != "no"; then
AC_CHECK_FUNCS([fork], [], [AC_MSG_ERROR([pcntl: fork() not supported by this platform])])
AC_CHECK_FUNCS([waitpid], [], [AC_MSG_ERROR([pcntl: waitpid() not supported by this platform])])
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])
AC_CHECK_FUNCS([
getpriority
setpriority
wait3
wait4
sigwaitinfo
sigtimedwait
unshare
rfork
forkx
pidfd_open
sched_setaffinity])

AC_CHECK_TYPE([siginfo_t],[PCNTL_CFLAGS="-DHAVE_STRUCT_SIGINFO_T"],,[#include <signal.h>])

Expand Down
104 changes: 103 additions & 1 deletion ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@
#endif

#include <errno.h>
#ifdef HAVE_UNSHARE
#if defined(HAVE_UNSHARE) || defined(HAVE_SCHED_SETAFFINITY)
#include <sched.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/cpuset.h>
typedef cpuset_t cpu_set_t;
#endif
#endif

#ifdef HAVE_PIDFD_OPEN
Expand Down Expand Up @@ -1476,6 +1481,103 @@ PHP_FUNCTION(pcntl_setns)
}
#endif

#ifdef HAVE_SCHED_SETAFFINITY
PHP_FUNCTION(pcntl_getcpuaffinity)
{
zend_long pid;
bool pid_is_null = 1;
cpu_set_t mask;
zend_ulong i, maxcpus;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
ZEND_PARSE_PARAMETERS_END();

// 0 == getpid in this context, we re just saving a syscall
pid = pid_is_null ? 0 : pid;

CPU_ZERO(&mask);

if (sched_getaffinity(pid, sizeof(mask), &mask) != 0) {
PCNTL_G(last_error) = errno;
switch (errno) {
case ESRCH:
zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
RETURN_THROWS();
case EPERM:
php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
break;
default:
php_error_docref(NULL, E_WARNING, "Error %d", errno);
}

RETURN_EMPTY_ARRAY();
}

maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_ONLN);
array_init(return_value);

for (i = 0; i < maxcpus; i ++) {
if (CPU_ISSET(i, &mask)) {
add_next_index_long(return_value, i);
}
}
}

PHP_FUNCTION(pcntl_setcpuaffinity)
{
zend_long pid;
bool pid_is_null = 1;
cpu_set_t mask;
zval *hmask = NULL, *cpu;
zend_ulong maxcpus;

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(pid, pid_is_null)
Z_PARAM_ARRAY(hmask)
ZEND_PARSE_PARAMETERS_END();

if (!hmask || Z_ARRVAL_P(hmask)->nNumOfElements == 0) {
zend_argument_value_error(2, "must not be empty");
RETURN_THROWS();
}

// 0 == getpid in this context, we re just saving a syscall
pid = pid_is_null ? 0 : pid;
maxcpus = sysconf(_SC_NPROCESSORS_ONLN);
CPU_ZERO(&mask);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), cpu) {
if (Z_TYPE_P(cpu) == IS_LONG && Z_LVAL_P(cpu) >= 0 &&
Z_LVAL_P(cpu) < maxcpus && !CPU_ISSET(Z_LVAL_P(cpu), &mask)) {
CPU_SET(Z_LVAL_P(cpu), &mask);
}
} ZEND_HASH_FOREACH_END();

if (!CPU_COUNT(&mask)) {
zend_argument_value_error(2, "invalid cpu affinity mapping");
RETURN_THROWS();
}

if (sched_setaffinity(pid, sizeof(mask), &mask) != 0) {
PCNTL_G(last_error) = errno;
switch (errno) {
case ESRCH:
zend_argument_value_error(1, "invalid process (" ZEND_LONG_FMT ")", pid);
RETURN_THROWS();
case EPERM:
php_error_docref(NULL, E_WARNING, "Calling process not having the proper privileges");
break;
}
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
#endif

static void pcntl_interrupt_function(zend_execute_data *execute_data)
{
pcntl_signal_dispatch();
Expand Down
5 changes: 5 additions & 0 deletions ext/pcntl/pcntl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,8 @@ function pcntl_forkx(int $flags): int{}
#ifdef HAVE_PIDFD_OPEN
function pcntl_setns(?int $process_id = null, int $nstype = CLONE_NEWNET): bool {}
#endif

#ifdef HAVE_SCHED_SETAFFINITY
function pcntl_getcpuaffinity(?int $process_id = null): array {}
function pcntl_setcpuaffinity(?int $process_id = null, array $cpu_ids = []): bool {}
#endif
27 changes: 26 additions & 1 deletion ext/pcntl/pcntl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions ext/pcntl/tests/pcntl_cpuaffinity.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
pcntl_getcpuaffinity() and pcntl_setcpuaffinity()
--EXTENSIONS--
pcntl
--SKIPIF--
<?php
if (!function_exists("pcntl_setcpuaffinity")) die("skip pcntl_setcpuaffinity is not available");
?>
--FILE--
<?php
$mask = [0, 1];
var_dump(pcntl_setcpuaffinity(null, $mask));
$act_mask = pcntl_getcpuaffinity();
var_dump(array_diff($mask, $act_mask));
$n_act_mask = pcntl_getcpuaffinity();
var_dump(array_diff($act_mask, $n_act_mask));

try {
pcntl_setcpuaffinity(null, []);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
pcntl_setcpuaffinity(null, ["abc" => "def", 0 => "cpuid"]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
pcntl_setcpuaffinity(null, [PHP_INT_MAX]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
pcntl_setcpuaffinity(null, [-1024, 64, -2]);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}

try {
pcntl_getcpuaffinity(-1024);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECT--
bool(true)
array(0) {
}
array(0) {
}
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) must not be empty
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) invalid cpu affinity mapping
pcntl_getcpuaffinity(): Argument #1 ($process_id) invalid process (-1024)