|
| 1 | +# Central Processor Info |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | + |
| 5 | +The CPU info component provides information about the underlying CPU, |
| 6 | +including architecture, number of cores, and other information. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +<tldr> |
| 11 | + <p> |
| 12 | + Via <a href="https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies">Composer</a>: |
| 13 | + </p> |
| 14 | + <p> |
| 15 | + <code lang="bash">composer require boson-php/cpu-info</code> |
| 16 | + </p> |
| 17 | +</tldr> |
| 18 | + |
| 19 | +**Requirements:** |
| 20 | + |
| 21 | +* `PHP ^8.4` |
| 22 | +* `ext-ffi` (optional, provides more detailed and accurate information about the CPU) |
| 23 | +* `ext-com_dotnet` (optional, provides more detailed and accurate information about the CPU) |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +### Basic Information |
| 28 | + |
| 29 | +<secondary-label ref="macos-limitations"/> |
| 30 | + |
| 31 | +```php |
| 32 | +use Boson\Component\CpuInfo\CentralProcessor; |
| 33 | + |
| 34 | +// Get current CPU information |
| 35 | +$cpu = CentralProcessor::createFromGlobals(); |
| 36 | + |
| 37 | +// Access basic information |
| 38 | +echo 'Architecture: ' . $cpu->arch . "\n"; |
| 39 | +echo 'Name: ' . $cpu->name . "\n"; |
| 40 | +echo 'Vendor: ' . ($cpu->vendor ?? '~') . "\n"; |
| 41 | +echo 'Physical Cores: ' . $cpu->physicalCores . "\n"; |
| 42 | +echo 'Logical Cores: ' . $cpu->logicalCores . "\n"; |
| 43 | +echo 'Instruction Sets: ' . implode(', ', $cpu->instructionSets); |
| 44 | +``` |
| 45 | + |
| 46 | +This code will output something like the following information: |
| 47 | + |
| 48 | +<tabs> |
| 49 | + <tab title="Windows / x86_64"> |
| 50 | + <code-block> |
| 51 | + Architecture: amd64 |
| 52 | + Name: Intel(R) Core(TM) i7-10700KF CPU @ 3.80GHz |
| 53 | + Vendor: GenuineIntel |
| 54 | + Physical Cores: 8 |
| 55 | + Logical Cores: 16 |
| 56 | + Instruction Sets: mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, fma3, avx, avx2 |
| 57 | + </code-block> |
| 58 | + </tab> |
| 59 | + <tab title="Linux / x86_64"> |
| 60 | + <code-block> |
| 61 | + Architecture: amd64 |
| 62 | + Name: Intel(R) Core(TM) i7-10700KF CPU @ 3.80GHz |
| 63 | + Vendor: GenuineIntel |
| 64 | + Physical Cores: 2 |
| 65 | + Logical Cores: 4 |
| 66 | + Instruction Sets: mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2 |
| 67 | + </code-block> |
| 68 | + <note>This is an example output from VirtualBox, so the number of cores is |
| 69 | + different. Also, VirtualBox does not support AVX instruction set, so |
| 70 | + these instructions are not in the list.</note> |
| 71 | + </tab> |
| 72 | +</tabs> |
| 73 | + |
| 74 | +### CPU Architecture |
| 75 | + |
| 76 | +You can get the CPU architecture information from the CPU information |
| 77 | +object (`$cpu->arch`). However, if you do not need all the CPU information, |
| 78 | +it is enough to get the architecture separately using the |
| 79 | +`Architecture::createFromGlobals()` method. |
| 80 | + |
| 81 | +```php |
| 82 | +use Boson\Component\CpuInfo\Architecture; |
| 83 | + |
| 84 | +// Get current CPU architecture |
| 85 | +$arch = Architecture::createFromGlobals(); |
| 86 | + |
| 87 | +// Check for specific architecture |
| 88 | +if ($arch === Architecture::Amd64) { |
| 89 | + // Only amd64 (x86_64) specific code |
| 90 | +} |
| 91 | + |
| 92 | +// Check for сompatible architectures |
| 93 | +if ($arch->is(Architecture::x86)) { |
| 94 | + // x86 and x86-like, for example: |
| 95 | + // - x86 |
| 96 | + // - amd64 (x86_64) |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +The component supports the following architectures: |
| 101 | +- `Architecture::x86` |
| 102 | +- `Architecture::Amd64` |
| 103 | +- `Architecture::Arm` |
| 104 | +- `Architecture::Arm64` |
| 105 | +- `Architecture::Itanium` |
| 106 | +- `Architecture::RiscV32` |
| 107 | +- `Architecture::RiscV64` |
| 108 | +- `Architecture::Mips` |
| 109 | +- `Architecture::Mips64` |
| 110 | +- `Architecture::PowerPc` |
| 111 | +- `Architecture::PowerPc64` |
| 112 | +- `Architecture::Sparc` |
| 113 | +- `Architecture::Sparc64` |
| 114 | + |
| 115 | +### Instruction Sets |
| 116 | + |
| 117 | +<secondary-label ref="macos-limitations"/> |
| 118 | + |
| 119 | +The component provides information about supported CPU instruction sets. |
| 120 | +You can check if a specific instruction set is supported: |
| 121 | + |
| 122 | +```php |
| 123 | +use Boson\Component\CpuInfo\CentralProcessor; |
| 124 | +use Boson\Component\CpuInfo\InstructionSet; |
| 125 | + |
| 126 | +$cpu = CentralProcessor::createFromGlobals(); |
| 127 | + |
| 128 | +// Check if specific instruction set is supported |
| 129 | +if ($cpu->isSupports(InstructionSet::AVX2)) { |
| 130 | + // AVX2 specific code |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +<tabs> |
| 135 | + <tab title="x86 / amd64 (x86_64)"> |
| 136 | + <list> |
| 137 | + <li><code>InstructionSet::MMX</code> - MultiMedia eXtensions</li> |
| 138 | + <li><code>InstructionSet::SSE</code> - Streaming SIMD Extensions</li> |
| 139 | + <li><code>InstructionSet::SSE2</code> - Streaming SIMD Extensions 2</li> |
| 140 | + <li><code>InstructionSet::SSE3</code> - Streaming SIMD Extensions 3</li> |
| 141 | + <li><code>InstructionSet::SSSE3</code> - Supplemental Streaming SIMD Extensions 3</li> |
| 142 | + <li><code>InstructionSet::SSE4_1</code> - Streaming SIMD Extensions 4.1</li> |
| 143 | + <li><code>InstructionSet::SSE4_2</code> - Streaming SIMD Extensions 4.2</li> |
| 144 | + <li><code>InstructionSet::FMA3</code> - Fused Multiply-Add 3</li> |
| 145 | + <li><code>InstructionSet::AVX</code> - Advanced Vector Extensions</li> |
| 146 | + <li><code>InstructionSet::AVX2</code> - Advanced Vector Extensions 2</li> |
| 147 | + <li><code>InstructionSet::AVX512</code> - Advanced Vector Extensions 512</li> |
| 148 | + </list> |
| 149 | + </tab> |
| 150 | + <tab title="arm / arm64 (aarch64)"> |
| 151 | + There is currently no support for checking instructions for this platform |
| 152 | + </tab> |
| 153 | +</tabs> |
| 154 | + |
| 155 | +<tabs> |
| 156 | +<tab title="MacOS"> |
| 157 | +<warning> |
| 158 | +Please note that the instruction set information in macOS may not be |
| 159 | +accurate due to virtualization and testing issues. |
| 160 | + |
| 161 | +Full implementation of macOS support is possible in the future. |
| 162 | +</warning> |
| 163 | +</tab> |
| 164 | +</tabs> |
0 commit comments