-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2578,3 +2578,23 @@ PHP_FUNCTION(sys_getloadavg) | |||||
} | ||||||
/* }}} */ | ||||||
#endif | ||||||
|
||||||
PHP_FUNCTION(num_available_processors) | ||||||
{ | ||||||
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__ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The check for cygwin is superfluous because |
||||||
SYSTEM_INFO system_info; | ||||||
GetSystemInfo (&system_info); | ||||||
if (system_info.dwNumberOfProcessors > 0) { | ||||||
RETURN_LONG(system_info.dwNumberOfProcessors); | ||||||
} | ||||||
#endif | ||||||
|
||||||
RETURN_NULL(); | ||||||
} |
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 |
There was a problem hiding this comment.
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 withnum_
. 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.