Skip to content

feat: add function num_cpus (formerly nproc) #11137

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2578,3 +2578,23 @@ PHP_FUNCTION(sys_getloadavg)
}
/* }}} */
#endif

PHP_FUNCTION(num_available_processors)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just like to echo my feedback I just provided in PHP Internals here as well:

  • I think "processor" is a bit too ambiguous. I'd use "cpu_core" instead.
    Yes, technically that's not entirely accurate when hyper-threading is used, but in most cases it's not trivial to distinguish physical cores from logical cores anyway, and "cpu_cores" provides the most understandable abstraction for the vast majority of use cases: deciding how many parallel processes one should use for optimal use of the CPU.

  • Is it necessary to add "available" as a disambiguator?
    Are there future plans to add a function that provides the "total" or "unavailable" number of processors? If not, I'd just drop the "available" part.

  • From a quick search in php-src there doesn't seem to be any existing function name starting with num_. For the sake of consistency with the existing PHP functions, similar functionality in other languages, and following the principle of prefixing by primary concept, I suggest suffixing the function name with _count instead.

tl;dr: I'd like to respectfully propose the following function name instead:

cpu_core_count()

This helps with the semantic grouping of the function, and (to me, at least) gives a more succinct and understandable description of the function.

{
ZEND_PARSE_PARAMETERS_NONE();

#if defined(_SC_NPROCESSORS_ONLN)
int nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs > 0) {
RETURN_LONG(nprocs);
}
#elif defined _WIN32 && ! defined __CYGWIN__
SYSTEM_INFO system_info;
GetSystemInfo (&system_info);
if (system_info.dwNumberOfProcessors > 0) {
RETURN_LONG(system_info.dwNumberOfProcessors);
}
#endif

RETURN_NULL();
}
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3834,3 +3834,5 @@ function sapi_windows_set_ctrl_handler(?callable $handler, bool $add = true): bo

function sapi_windows_generate_ctrl_event(int $event, int $pid = 0): bool {}
#endif

function num_available_processors(): ?int {}
7 changes: 6 additions & 1 deletion ext/standard/basic_functions_arginfo.h

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
num_available_processors() tests
--SKIPIF--
<?php
if (!function_exists("num_available_processors")) die("skip");
?>
--FILE--
<?php

var_dump(num_available_processors());

echo "Done\n";
?>
--EXPECTF--
int(%d)
Done