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 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
2 changes: 1 addition & 1 deletion ext/pcntl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ 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
124 changes: 123 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,123 @@ 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_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_FALSE;
}

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

for (zend_ulong 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, *ncpu;

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 || zend_hash_num_elements(Z_ARRVAL_P(hmask)) == 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;
zend_ulong maxcpus = (zend_ulong)sysconf(_SC_NPROCESSORS_CONF);
CPU_ZERO(&mask);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(hmask), ncpu) {
ZVAL_DEREF(ncpu);
zend_long cpu;
if (Z_TYPE_P(ncpu) != IS_LONG) {
if (Z_TYPE_P(ncpu) == IS_STRING) {
zend_ulong tmp;
if (!ZEND_HANDLE_NUMERIC(Z_STR_P(ncpu), tmp)) {
zend_argument_value_error(2, "cpu id invalid value (%s)", ZSTR_VAL(Z_STR_P(ncpu)));
RETURN_THROWS();
}

cpu = (zend_long)tmp;
} else {
zend_string *wcpu = zval_get_string_func(ncpu);
zend_argument_value_error(2, "cpu id invalid type (%s)", ZSTR_VAL(wcpu));
zend_string_release(wcpu);
RETURN_THROWS();
}
} else {
cpu = Z_LVAL_P(ncpu);
}

if (cpu < 0 || cpu >= maxcpus) {
zend_argument_value_error(2, "cpu id must be between 0 and " ZEND_ULONG_FMT " (" ZEND_LONG_FMT ")", maxcpus, cpu);
RETURN_THROWS();
}

if (!CPU_ISSET(cpu, &mask)) {
CPU_SET(cpu, &mask);
}
} ZEND_HASH_FOREACH_END();

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;
default:
php_error_docref(NULL, E_WARNING, "Error %d", errno);
}
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|false {}
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.

69 changes: 69 additions & 0 deletions ext/pcntl/tests/pcntl_cpuaffinity.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--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));
var_dump(pcntl_setcpuaffinity(null, ["0", "1"]));

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() . PHP_EOL;
}

try {
pcntl_setcpuaffinity(null, [1, array(1)]);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
bool(true)
array(0) {
}
array(0) {
}
bool(true)
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) must not be empty
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id invalid value (def)
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and %d (%d)
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id must be between 0 and %d (-1024)
pcntl_getcpuaffinity(): Argument #1 ($process_id) invalid process (-1024)

Warning: Array to string conversion in %s on line %d
pcntl_setcpuaffinity(): Argument #2 ($cpu_ids) cpu id invalid type (Array)