Repository navigation
Expand file tree
/
Copy pathSystem.php
More file actions
59 lines (47 loc) · 1.39 KB
/
Copy pathSystem.php
File metadata and controls
59 lines (47 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Support;
class System
{
/**
* Get the number of CPU cores.
*/
public static function getCpuCoresNum(): int
{
if (function_exists('swoole_cpu_num')) {
return swoole_cpu_num();
}
$num = 1;
if (is_file('/proc/cpuinfo')) {
$cpuinfo = file_get_contents('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuinfo, $matches);
$num = count($matches[0]);
} elseif (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$process = @popen('wmic cpu get NumberOfCores', 'rb');
if ($process !== false) {
fgets($process);
$num = intval(fgets($process));
pclose($process);
}
} else {
$process = @popen('sysctl -a', 'rb');
if ($process !== false) {
$output = stream_get_contents($process);
preg_match('/hw.ncpu: (\d+)/', $output, $matches);
if ($matches) {
$num = intval($matches[1][0]);
}
pclose($process);
}
}
return $num;
}
}