Skip to content

Commit 7adbe7d

Browse files
committed
Move availableProcessors() to Environment class
1 parent abc3fdb commit 7adbe7d

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

src/main/php/lang/Environment.class.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,53 @@ public static function platform(): string {
111111
;
112112
}
113113

114+
/**
115+
* Returns the number of processors available to the the runtime. First checks
116+
* for the `NUMBER_OF_PROCESSORS` environment variables, then uses platform-
117+
* specific files and tools. Returns NULL if discovery fails.
118+
*
119+
* @see https://stackoverflow.com/q/6481005 (Linux)
120+
* @see https://stackoverflow.com/q/1715580 (Mac OS)
121+
* @see https://stackoverflow.com/q/22919076 (Windows)
122+
* @see https://stackoverflow.com/a/49152519 (Docker w/ `--cpus=<n>`)
123+
* @see https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem
124+
* @return ?int|float
125+
*/
126+
public static function availableProcessors() {
127+
if ($n= getenv('NUMBER_OF_PROCESSORS')) {
128+
return (int)$n;
129+
} else if (class_exists(\Com::class)) {
130+
$c= new \Com('winmgmts://./root/cimv2');
131+
foreach ($c->instancesOf('Win32_ComputerSystem') as $sys) {
132+
return $sys->NumberOfProcessors;
133+
}
134+
} else if (is_readable($f= '/sys/fs/cgroup/cpu/cpu.cfs_quota_us') && ($fd= fopen($f, 'r'))) {
135+
fscanf($fd, '%d', $n);
136+
fclose($fd);
137+
if ($n > 0) return (float)($n / 100000);
138+
// Fall through
139+
}
140+
141+
if (is_readable($f= '/proc/cpuinfo') && ($fd= fopen($f, 'r'))) {
142+
$n= 0;
143+
do {
144+
$line= fgets($fd, 1024);
145+
if (0 === strncmp($line, 'processor', 9)) $n++;
146+
} while (!feof($fd));
147+
fclose($fd);
148+
return $n;
149+
} else {
150+
$paths= explode(PATH_SEPARATOR, getenv('PATH'));
151+
foreach (['nproc' => '', 'sysctl' => ' -n hw.ncpu'] as $command => $args) {
152+
foreach ($paths as $path) {
153+
$binary= $path.DIRECTORY_SEPARATOR.$command;
154+
if (is_executable($binary)) return (int)exec($binary.$args);
155+
}
156+
}
157+
}
158+
return null;
159+
}
160+
114161
/**
115162
* Returns a path for display for a given directory. Will replace current
116163
* and parent directories with `.` and `..`, the user's home directory with

src/main/php/lang/Runtime.class.php

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,6 @@ public static function getInstance() {
2525
return self::$instance;
2626
}
2727

28-
/**
29-
* Returns the number of processors available to the the runtime. First checks
30-
* for the `NUMBER_OF_PROCESSORS` environment variables, then uses platform-
31-
* specific files and tools. Returns NULL if discovery fails.
32-
*
33-
* @see https://stackoverflow.com/q/6481005 (Linux)
34-
* @see https://stackoverflow.com/q/1715580 (Mac OS)
35-
* @see https://stackoverflow.com/q/22919076 (Windows)
36-
* @see https://stackoverflow.com/a/49152519 (Docker w/ `--cpus=<n>`)
37-
* @see https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem
38-
* @return ?int|float
39-
*/
40-
public function availableProcessors() {
41-
if ($n= getenv('NUMBER_OF_PROCESSORS')) {
42-
return (int)$n;
43-
} else if (class_exists(\Com::class)) {
44-
$c= new \Com('winmgmts://./root/cimv2');
45-
foreach ($c->instancesOf('Win32_ComputerSystem') as $sys) {
46-
return $sys->NumberOfProcessors;
47-
}
48-
} else if (is_readable($f= '/sys/fs/cgroup/cpu/cpu.cfs_quota_us') && ($fd= fopen($f, 'r'))) {
49-
fscanf($fd, '%d', $n);
50-
fclose($fd);
51-
if ($n > 0) return (float)($n / 100000);
52-
// Fall through
53-
}
54-
55-
if (is_readable($f= '/proc/cpuinfo') && ($fd= fopen($f, 'r'))) {
56-
$n= 0;
57-
do {
58-
$line= fgets($fd, 1024);
59-
if (0 === strncmp($line, 'processor', 9)) $n++;
60-
} while (!feof($fd));
61-
fclose($fd);
62-
return $n;
63-
} else {
64-
$paths= explode(PATH_SEPARATOR, getenv('PATH'));
65-
foreach (['nproc' => '', 'sysctl' => ' -n hw.ncpu'] as $command => $args) {
66-
foreach ($paths as $path) {
67-
$binary= $path.DIRECTORY_SEPARATOR.$command;
68-
if (is_executable($binary)) return (int)exec($binary.$args);
69-
}
70-
}
71-
}
72-
return null;
73-
}
74-
7528
/**
7629
* Returns the total amount of memory available to the runtime. If there
7730
* is no limit zero will be returned.

src/test/php/lang/unittest/EnvironmentTest.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public function platform() {
115115
Assert::notEquals('', Environment::platform());
116116
}
117117

118+
#[Test]
119+
public function available_processors() {
120+
Assert::notEquals(0, Environment::availableProcessors());
121+
}
122+
118123
#[Test]
119124
public function current_path() {
120125
Assert::equals('.', Environment::path());

0 commit comments

Comments
 (0)