Skip to content

Commit

Permalink
feat: add function cpu_cores
Browse files Browse the repository at this point in the history
Returns the number of processors which are currently online

Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed May 20, 2023
1 parent a94fe87 commit 27e18d9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
23 changes: 23 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2624,3 +2624,26 @@ PHP_FUNCTION(sys_getloadavg)
}
/* }}} */
#endif

PHP_FUNCTION(cpu_cores)
{
ZEND_PARSE_PARAMETERS_NONE();

#if defined(_SC_NPROCESSORS_ONLN)
int nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs > 0) {
RETURN_LONG(nprocs);
}
#endif


#if defined _WIN32 && ! defined __CYGWIN__
SYSTEM_INFO system_info;
GetSystemInfo (&system_info);
if (system_info.dwNumberOfProcessors > 0) {
RETURN_LONG(system_info.dwNumberOfProcessors);
}
#endif

RETURN_LONG(1);
}
2 changes: 2 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3749,3 +3749,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 cpu_cores(): int {}
6 changes: 5 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.

16 changes: 16 additions & 0 deletions ext/standard/tests/general_functions/cpu_cores.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
cpu_cores() tests
--SKIPIF--
<?php
if (!function_exists("cpu_cores")) die("skip");
?>
--FILE--
<?php

var_dump(cpu_cores ());

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

0 comments on commit 27e18d9

Please sign in to comment.