|
| 1 | +# core.base.cpu |
| 2 | + |
| 3 | +This module provides CPU information and detection capabilities for the current system. |
| 4 | + |
| 5 | +::: tip TIP |
| 6 | +To use this module, you need to import it first: `import("core.base.cpu")` |
| 7 | +::: |
| 8 | + |
| 9 | +This module allows you to query CPU vendor, model, architecture, features, and performance metrics across different platforms (macOS, Linux, Windows, BSD). |
| 10 | + |
| 11 | +## cpu.vendor |
| 12 | + |
| 13 | +- Get CPU vendor ID |
| 14 | + |
| 15 | +#### Function Prototype |
| 16 | + |
| 17 | +::: tip API |
| 18 | +```lua |
| 19 | +cpu.vendor() |
| 20 | +``` |
| 21 | +::: |
| 22 | + |
| 23 | +#### Parameter Description |
| 24 | + |
| 25 | +No parameters |
| 26 | + |
| 27 | +#### Return Value |
| 28 | + |
| 29 | +| Type | Description | |
| 30 | +|------|-------------| |
| 31 | +| string | Returns the CPU vendor ID (e.g., "GenuineIntel", "AuthenticAMD") | |
| 32 | + |
| 33 | +#### Usage |
| 34 | + |
| 35 | +```lua |
| 36 | +import("core.base.cpu") |
| 37 | + |
| 38 | +local vendor = cpu.vendor() |
| 39 | +print("CPU Vendor:", vendor) -- Output: "GenuineIntel" or "AuthenticAMD" |
| 40 | +``` |
| 41 | + |
| 42 | +## cpu.model |
| 43 | + |
| 44 | +- Get CPU model number |
| 45 | + |
| 46 | +#### Function Prototype |
| 47 | + |
| 48 | +::: tip API |
| 49 | +```lua |
| 50 | +cpu.model() |
| 51 | +``` |
| 52 | +::: |
| 53 | + |
| 54 | +#### Parameter Description |
| 55 | + |
| 56 | +No parameters |
| 57 | + |
| 58 | +#### Return Value |
| 59 | + |
| 60 | +| Type | Description | |
| 61 | +|------|-------------| |
| 62 | +| number | Returns the CPU model number | |
| 63 | + |
| 64 | +#### Usage |
| 65 | + |
| 66 | +```lua |
| 67 | +import("core.base.cpu") |
| 68 | + |
| 69 | +local model = cpu.model() |
| 70 | +print("CPU Model:", model) |
| 71 | +``` |
| 72 | + |
| 73 | +## cpu.family |
| 74 | + |
| 75 | +- Get CPU family |
| 76 | + |
| 77 | +#### Function Prototype |
| 78 | + |
| 79 | +::: tip API |
| 80 | +```lua |
| 81 | +cpu.family() |
| 82 | +``` |
| 83 | +::: |
| 84 | + |
| 85 | +#### Parameter Description |
| 86 | + |
| 87 | +No parameters |
| 88 | + |
| 89 | +#### Return Value |
| 90 | + |
| 91 | +| Type | Description | |
| 92 | +|------|-------------| |
| 93 | +| number | Returns the CPU family number | |
| 94 | + |
| 95 | +#### Usage |
| 96 | + |
| 97 | +```lua |
| 98 | +import("core.base.cpu") |
| 99 | + |
| 100 | +local family = cpu.family() |
| 101 | +print("CPU Family:", family) |
| 102 | +``` |
| 103 | + |
| 104 | +## cpu.model_name |
| 105 | + |
| 106 | +- Get CPU model name |
| 107 | + |
| 108 | +#### Function Prototype |
| 109 | + |
| 110 | +::: tip API |
| 111 | +```lua |
| 112 | +cpu.model_name() |
| 113 | +``` |
| 114 | +::: |
| 115 | + |
| 116 | +#### Parameter Description |
| 117 | + |
| 118 | +No parameters |
| 119 | + |
| 120 | +#### Return Value |
| 121 | + |
| 122 | +| Type | Description | |
| 123 | +|------|-------------| |
| 124 | +| string | Returns the full CPU model name (e.g., "Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz") | |
| 125 | + |
| 126 | +#### Usage |
| 127 | + |
| 128 | +```lua |
| 129 | +import("core.base.cpu") |
| 130 | + |
| 131 | +local name = cpu.model_name() |
| 132 | +print("CPU Name:", name) |
| 133 | +``` |
| 134 | + |
| 135 | +## cpu.features |
| 136 | + |
| 137 | +- Get CPU features |
| 138 | + |
| 139 | +#### Function Prototype |
| 140 | + |
| 141 | +::: tip API |
| 142 | +```lua |
| 143 | +cpu.features() |
| 144 | +``` |
| 145 | +::: |
| 146 | + |
| 147 | +#### Parameter Description |
| 148 | + |
| 149 | +No parameters |
| 150 | + |
| 151 | +#### Return Value |
| 152 | + |
| 153 | +| Type | Description | |
| 154 | +|------|-------------| |
| 155 | +| string | Returns a string containing all CPU features/flags separated by spaces | |
| 156 | + |
| 157 | +#### Usage |
| 158 | + |
| 159 | +```lua |
| 160 | +import("core.base.cpu") |
| 161 | + |
| 162 | +local features = cpu.features() |
| 163 | +print("CPU Features:", features) |
| 164 | +``` |
| 165 | + |
| 166 | +## cpu.has_feature |
| 167 | + |
| 168 | +- Check if CPU has a specific feature |
| 169 | + |
| 170 | +#### Function Prototype |
| 171 | + |
| 172 | +::: tip API |
| 173 | +```lua |
| 174 | +cpu.has_feature(name: <string>) |
| 175 | +``` |
| 176 | +::: |
| 177 | + |
| 178 | +#### Parameter Description |
| 179 | + |
| 180 | +| Parameter | Description | |
| 181 | +|-----------|-------------| |
| 182 | +| name | Required. Feature name to check (e.g., "sse", "avx", "avx2") | |
| 183 | + |
| 184 | +#### Return Value |
| 185 | + |
| 186 | +| Type | Description | |
| 187 | +|------|-------------| |
| 188 | +| boolean | Returns true if the CPU has the feature, false otherwise | |
| 189 | + |
| 190 | +#### Usage |
| 191 | + |
| 192 | +```lua |
| 193 | +import("core.base.cpu") |
| 194 | + |
| 195 | +if cpu.has_feature("avx2") then |
| 196 | + print("AVX2 supported") |
| 197 | +end |
| 198 | + |
| 199 | +if cpu.has_feature("sse") then |
| 200 | + print("SSE supported") |
| 201 | +end |
| 202 | +``` |
| 203 | + |
| 204 | +## cpu.march |
| 205 | + |
| 206 | +- Get CPU micro-architecture |
| 207 | + |
| 208 | +#### Function Prototype |
| 209 | + |
| 210 | +::: tip API |
| 211 | +```lua |
| 212 | +cpu.march() |
| 213 | +``` |
| 214 | +::: |
| 215 | + |
| 216 | +#### Parameter Description |
| 217 | + |
| 218 | +No parameters |
| 219 | + |
| 220 | +#### Return Value |
| 221 | + |
| 222 | +| Type | Description | |
| 223 | +|------|-------------| |
| 224 | +| string | Returns the CPU micro-architecture name (e.g., "Skylake", "Zen 2", "Alder Lake") | |
| 225 | + |
| 226 | +#### Usage |
| 227 | + |
| 228 | +```lua |
| 229 | +import("core.base.cpu") |
| 230 | + |
| 231 | +local march = cpu.march() |
| 232 | +print("CPU Architecture:", march) -- Output: "Skylake", "Zen 2", etc. |
| 233 | +``` |
| 234 | + |
| 235 | +## cpu.number |
| 236 | + |
| 237 | +- Get number of CPU cores |
| 238 | + |
| 239 | +#### Function Prototype |
| 240 | + |
| 241 | +::: tip API |
| 242 | +```lua |
| 243 | +cpu.number() |
| 244 | +``` |
| 245 | +::: |
| 246 | + |
| 247 | +#### Parameter Description |
| 248 | + |
| 249 | +No parameters |
| 250 | + |
| 251 | +#### Return Value |
| 252 | + |
| 253 | +| Type | Description | |
| 254 | +|------|-------------| |
| 255 | +| number | Returns the number of CPU cores | |
| 256 | + |
| 257 | +#### Usage |
| 258 | + |
| 259 | +```lua |
| 260 | +import("core.base.cpu") |
| 261 | + |
| 262 | +local cores = cpu.number() |
| 263 | +print("CPU Cores:", cores) |
| 264 | +``` |
| 265 | + |
| 266 | +## cpu.usagerate |
| 267 | + |
| 268 | +- Get CPU usage rate |
| 269 | + |
| 270 | +#### Function Prototype |
| 271 | + |
| 272 | +::: tip API |
| 273 | +```lua |
| 274 | +cpu.usagerate() |
| 275 | +``` |
| 276 | +::: |
| 277 | + |
| 278 | +#### Parameter Description |
| 279 | + |
| 280 | +No parameters |
| 281 | + |
| 282 | +#### Return Value |
| 283 | + |
| 284 | +| Type | Description | |
| 285 | +|------|-------------| |
| 286 | +| number | Returns the CPU usage rate as a decimal (0.0 to 1.0, where 1.0 = 100%) | |
| 287 | + |
| 288 | +#### Usage |
| 289 | + |
| 290 | +```lua |
| 291 | +import("core.base.cpu") |
| 292 | + |
| 293 | +local usage = cpu.usagerate() |
| 294 | +print("CPU Usage:", math.floor(usage * 100) .. "%") |
| 295 | +``` |
| 296 | + |
| 297 | +## cpu.info |
| 298 | + |
| 299 | +- Get all CPU information |
| 300 | + |
| 301 | +#### Function Prototype |
| 302 | + |
| 303 | +::: tip API |
| 304 | +```lua |
| 305 | +cpu.info(name?: <string>) |
| 306 | +``` |
| 307 | +::: |
| 308 | + |
| 309 | +#### Parameter Description |
| 310 | + |
| 311 | +| Parameter | Description | |
| 312 | +|-----------|-------------| |
| 313 | +| name | Optional. Specific field name to retrieve | |
| 314 | + |
| 315 | +#### Return Value |
| 316 | + |
| 317 | +| Type | Description | |
| 318 | +|------|-------------| |
| 319 | +| table | Returns a table containing all CPU information if name is not provided | |
| 320 | +| any | Returns the specific field value if name is provided | |
| 321 | + |
| 322 | +#### Usage |
| 323 | + |
| 324 | +```lua |
| 325 | +import("core.base.cpu") |
| 326 | + |
| 327 | +-- Get all CPU information |
| 328 | +local info = cpu.info() |
| 329 | +print("Vendor:", info.vendor) |
| 330 | +print("Model:", info.model) |
| 331 | +print("Family:", info.family) |
| 332 | +print("Architecture:", info.march) |
| 333 | +print("Cores:", info.ncpu) |
| 334 | +print("Features:", info.features) |
| 335 | +print("Usage Rate:", info.usagerate) |
| 336 | +print("Model Name:", info.model_name) |
| 337 | + |
| 338 | +-- Get specific field |
| 339 | +local vendor = cpu.info("vendor") |
| 340 | +local cores = cpu.info("ncpu") |
| 341 | +``` |
| 342 | + |
0 commit comments