/usr/share/phoronix-test-suite/pts-core/objects/phodevi/components/phodevi_cpu.php is in phoronix-test-suite 4.8.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | <?php
/*
Phoronix Test Suite
URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
Copyright (C) 2008 - 2013, Phoronix Media
Copyright (C) 2008 - 2013, Michael Larabel
phodevi_cpu.php: The PTS Device Interface object for the CPU / processor
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class phodevi_cpu extends phodevi_device_interface
{
// TODO XXX: $cpuinfo is now useless and needs to be replaced by the VFS layer... update OpenBenchmarking.org accordingly
public static $cpuinfo = false;
private static $cpu_flags = -1;
public static function read_property($identifier)
{
switch($identifier)
{
case 'identifier':
$property = new phodevi_device_property('cpu_string', phodevi::smart_caching);
break;
case 'model':
$property = new phodevi_device_property('cpu_model', phodevi::smart_caching);
break;
case 'mhz-default-frequency':
$property = new phodevi_device_property('cpu_default_frequency_mhz', phodevi::smart_caching);
break;
case 'default-frequency':
$property = new phodevi_device_property(array('cpu_default_frequency', 0), phodevi::smart_caching);
break;
case 'core-count':
$property = new phodevi_device_property('cpu_core_count', phodevi::smart_caching);
break;
case 'scaling-governor':
$property = new phodevi_device_property('cpu_scaling_governor', phodevi::std_caching);
break;
}
return $property;
}
public static function cpu_string()
{
$model = phodevi::read_property('cpu', 'model');
// Append the processor frequency to string
if(($freq = phodevi::read_property('cpu', 'default-frequency')) > 0)
{
$model = str_replace($freq . 'GHz', null, $model); // we'll replace it if it's already in the string
$model .= ' @ ' . $freq . 'GHz';
}
$core_count = phodevi::read_property('cpu', 'core-count');
return $model . ' (' . $core_count . ' Core' . ($core_count > 1 ? 's' : null) . ')';
}
public static function cpu_core_count()
{
$info = null;
if(getenv('NUMBER_OF_PROCESSORS') && is_numeric(getenv('NUMBER_OF_PROCESSORS')))
{
// Should be used by Windows they have NUMBER_OF_PROCESSORS set and use this as an easy way to override CPUs exposed
$info = getenv('NUMBER_OF_PROCESSORS');
}
else if(phodevi::is_linux())
{
if(is_file('/sys/devices/system/cpu/online'))
{
$present = pts_file_io::file_get_contents('/sys/devices/system/cpu/online');
if(isset($present[2]) && substr($present, 0, 2) == '0-')
{
$present = substr($present, 2);
if(is_numeric($present))
{
$info = $present + 1;
}
}
}
}
else if(phodevi::is_solaris())
{
$info = count(explode(PHP_EOL, trim(shell_exec('psrinfo'))));
}
else if(phodevi::is_bsd())
{
$info = intval(phodevi_bsd_parser::read_sysctl(array('hw.ncpufound', 'hw.ncpu')));
}
else if(phodevi::is_macosx())
{
$info = phodevi_osx_parser::read_osx_system_profiler('SPHardwareDataType', 'TotalNumberOfCores');
}
else if(phodevi::is_windows())
{
// Should be hit by the first NUMBER_OF_PROCESSORS env check...
//$info = getenv('NUMBER_OF_PROCESSORS');
}
if($info == null && isset(phodevi::$vfs->cpuinfo))
{
$info = self::cpuinfo_core_count();
}
return (is_numeric($info) && $info > 0 ? $info : 1);
}
public static function cpu_default_frequency_mhz()
{
return self::cpu_default_frequency() * 1000;
}
public static function cpu_scaling_governor()
{
$scaling_governor = false;
if(is_file('/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver'))
{
$scaling_governor = pts_file_io::file_get_contents('/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver') . ' ';
}
if(is_file('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor'))
{
$scaling_governor .= pts_file_io::file_get_contents('/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor');
}
return trim($scaling_governor);
}
public static function is_genuine($cpu)
{
/*
Real/Genuine CPUs should have:
1. Contain more than one word in string
2. Check vendor (to avoid QEMU, Virtual CPU, etc): Intel, VIA, AMD, ARM, SPARC
*/
return strpos($cpu, ' ') !== false && strpos($cpu, ' ') != strrpos($cpu, ' ') && pts_strings::has_in_istring($cpu, array('Intel', 'VIA', 'AMD', 'ARM', 'SPARC', 'Transmeta')) && stripos($cpu, 'unknown') === false;
}
public static function cpu_default_frequency($cpu_core = 0)
{
// Find out the processor frequency
$info = null;
// First, the ideal way, with modern CPUs using CnQ or EIST and cpuinfo reporting the current
if(is_file('/sys/devices/system/cpu/cpu' . $cpu_core . '/cpufreq/scaling_max_freq'))
{
$info = pts_file_io::file_get_contents('/sys/devices/system/cpu/cpu' . $cpu_core . '/cpufreq/scaling_max_freq');
$info = intval($info) / 1000000;
if($info > 9)
{
// For some reason on Linux 3.10 the scaling_max_freq is reported as 25GHz...
$info = null;
}
}
if($info == null && isset(phodevi::$vfs->cpuinfo)) // fall back for those without cpufreq
{
$cpu_mhz = self::read_cpuinfo_line('cpu MHz');
$info = $cpu_mhz / 1000;
}
else if($info == null && phodevi::is_bsd())
{
$info = phodevi_bsd_parser::read_sysctl(array('dev.cpu.0.freq_levels'));
if($info != null)
{
// Popping the top speed off of dev.cpu.0.freq_levels should be the default/highest supported frequency
$info = pts_arrays::first_element(explode(' ', str_replace('/', ' ', $info)));
if(!is_numeric($info))
{
$info = null;
}
}
if($info == null)
{
$info = phodevi_bsd_parser::read_sysctl(array('hw.acpi.cpu.px_global', 'machdep.est.frequency.target', 'hw.cpuspeed'));
}
if($info == null)
{
// dev.cpu.0.freq seems to be the real/current frequency, affected by power management, etc so only use as last fallback
$info = phodevi_bsd_parser::read_sysctl(array('dev.cpu.0.freq'));
}
if(is_numeric($info))
{
$info = $info / 1000;
}
else
{
$info = null;
}
}
else if($info == null && phodevi::is_windows())
{
$info = phodevi_windows_parser::read_cpuz('Processor 1', 'Stock frequency');
if($info != null)
{
if(($e = strpos($info, ' MHz')) !== false)
{
$info = substr($info, 0, $e);
}
$info = $info / 1000;
}
}
else if($info == null)
{
$info = phodevi::read_sensor(array('cpu', 'freq'));
if($info > 1000)
{
// Convert from MHz to GHz
$info = $info / 1000;
}
}
return pts_math::set_precision($info, 2);
}
public static function cpu_model()
{
// Returns the processor name / frequency information
$info = null;
if(isset(phodevi::$vfs->cpuinfo))
{
$physical_cpu_ids = phodevi_linux_parser::read_cpuinfo('physical id');
$physical_cpu_count = count(array_unique($physical_cpu_ids));
$cpu_strings = phodevi_linux_parser::read_cpuinfo(array('model name', 'Processor'));
$cpu_strings_unique = array_unique($cpu_strings);
if($physical_cpu_count == 1 || empty($physical_cpu_count))
{
// Just one processor
if(($cut = strpos($cpu_strings[0], ' (')) !== false)
{
$cpu_strings[0] = substr($cpu_strings[0], 0, $cut);
}
$info = $cpu_strings[0];
if(strpos($info, 'ARM') !== false)
{
if(is_dir('/sys/devices/system/exynos-core/') && stripos($info, 'Exynos') === false)
{
$info = 'Exynos ' . $info;
}
}
}
else if($physical_cpu_count > 1 && count($cpu_strings_unique) == 1)
{
// Multiple processors, same model
$info = $physical_cpu_count . ' x ' . $cpu_strings[0];
}
else if($physical_cpu_count > 1 && count($cpu_strings_unique) > 1)
{
// Multiple processors, different models
$current_id = -1;
$current_string = $cpu_strings[0];
$current_count = 0;
$cpus = array();
for($i = 0; $i < count($physical_cpu_ids); $i++)
{
if($current_string != $cpu_strings[$i] || $i == (count($physical_cpu_ids) - 1))
{
array_push($cpus, $current_count . ' x ' . $current_string);
$current_string = $cpu_strings[$i];
$current_count = 0;
}
if($physical_cpu_ids[$i] != $current_id)
{
$current_count++;
$current_id = $physical_cpu_ids[$i];
}
}
$info = implode(', ', $cpus);
}
}
else if(phodevi::is_solaris())
{
$dmi_cpu = phodevi_solaris_parser::read_sun_ddu_dmi_info('CPUType', '-C');
if(count($dmi_cpu) == 0)
{
$dmi_cpu = phodevi_solaris_parser::read_sun_ddu_dmi_info('ProcessorName');
}
if(count($dmi_cpu) > 0)
{
$info = $dmi_cpu[0];
}
else
{
$info = trim(shell_exec('dmesg 2>&1 | grep cpu0'));
$info = trim(substr($info, strrpos($info, 'cpu0:') + 6));
if(empty($info))
{
$info = array_pop(phodevi_solaris_parser::read_sun_ddu_dmi_info('ProcessorManufacturer'));
}
}
//TODO: Add in proper support for reading multiple CPUs, similar to the code from above
$physical_cpu_count = count(phodevi_solaris_parser::read_sun_ddu_dmi_info('ProcessorSocketType'));
if($physical_cpu_count > 1 && !empty($info))
{
// TODO: For now assuming when multiple CPUs are installed, that they are of the same type
$info = $physical_cpu_count . ' x ' . $info;
}
}
else if(phodevi::is_bsd())
{
$info = phodevi_bsd_parser::read_sysctl('hw.model');
}
else if(phodevi::is_macosx())
{
$info = phodevi_osx_parser::read_osx_system_profiler('SPHardwareDataType', 'ProcessorName');
}
else if(phodevi::is_windows())
{
$info = phodevi_windows_parser::read_cpuz('Processor 1', 'Name');
if(!$info)
{
$info = getenv('PROCESSOR_IDENTIFIER');
}
}
if(empty($info))
{
$info = 'Unknown';
}
else
{
if(($strip_point = strpos($info, '@')) > 0)
{
$info = trim(substr($info, 0, $strip_point)); // stripping out the reported freq, since the CPU could be overclocked, etc
}
$info = pts_strings::strip_string($info);
// It seems Intel doesn't report its name when reporting Pentium hardware
if(strpos($info, 'Pentium') !== false && strpos($info, 'Intel') === false)
{
$info = 'Intel ' . $info;
}
if(substr($info, 0, 5) == 'Intel')
{
$cpu_words = explode(' ', $info);
$cpu_words_count = count($cpu_words);
// Convert strings like 'Intel Core i7 M 620' -> 'Intel Core i7 620M' and 'Intel Core i7 X 990' -> 'Intel Core i7 990X' to better reflect Intel product marketing names
if($cpu_words_count > 4 && is_numeric($cpu_words[($cpu_words_count - 1)]) && strlen($cpu_words[($cpu_words_count - 2)]) == 1 && strlen($cpu_words[($cpu_words_count - 3)]) == 2)
{
$cpu_words[($cpu_words_count - 1)] .= $cpu_words[($cpu_words_count - 2)];
unset($cpu_words[($cpu_words_count - 2)]);
$info = implode(' ', $cpu_words);
}
}
}
return $info;
}
public static function get_cpu_feature_constants()
{
return array(
'sse2' => (1 << 1), // SSE 2
'sse3' => (1 << 2), // SSE 3
'sse4a' => (1 << 3), // SSE 4a
'sse4_1' => (1 << 4), // SSE 4.1
'sse4_2' => (1 << 5), // SSE 4.2
'sse5' => (1 << 6), // SSE 5
'avx' => (1 << 7), // AVX
'avx2' => (1 << 8), // AVX2
'aes' => (1 << 9), // AES
'epb' => (1 << 10), // EPB
'svm' => (1 << 11), // AMD SVM (Virtualization)
'vmx' => (1 << 12), // Intel Virtualization
'xop' => (1 << 13), // AMD XOP Instruction Set
'fma3' => (1 << 14), // FMA3 Instruction Set
'fma4' => (1 << 15), // FMA4 Instruction Set
'rdrand' => (1 << 16), // Intel Bull Mountain RDRAND - Ivy Bridge
'fsgsbase' => (1 << 17), // FSGSBASE - Ivy Bridge AVX
'bmi2' => (1 << 18) // Intel Haswell has BMI2
);
}
public static function get_cpu_feature_constant($constant)
{
$features = self::get_cpu_feature_constants();
return isset($features[$constant]) ? $features[$constant] : -1;
}
public static function read_cpuinfo_line($key, $from_start = true)
{
$line = false;
$key .= "\t";
if(isset(phodevi::$vfs->cpuinfo) && ($from_start && ($key_pos = strpos(phodevi::$vfs->cpuinfo, PHP_EOL . $key)) !== false) || ($key_pos = strrpos(phodevi::$vfs->cpuinfo, PHP_EOL . $key)) !== false)
{
$line = substr(phodevi::$vfs->cpuinfo, $key_pos);
$line = substr($line, strpos($line, ':') + 1);
$line = trim(substr($line, 0, strpos($line, PHP_EOL)));
}
return $line;
}
public static function set_cpu_feature_flags()
{
$flags = explode(' ', self::read_cpuinfo_line('flags'));
self::$cpu_flags = 0;
foreach(self::get_cpu_feature_constants() as $feature => $value)
{
if(in_array($feature, $flags))
{
// The feature is supported on the CPU
self::$cpu_flags |= $value;
}
}
}
public static function get_cpu_flags()
{
if(self::$cpu_flags === -1)
{
self::set_cpu_feature_flags();
}
return self::$cpu_flags;
}
public static function instruction_set_extensions()
{
$constants = self::get_cpu_feature_constants();
self::set_cpu_feature_flags();
$cpu_flags = self::get_cpu_flags();
$extension_string = null;
foreach($constants as $feature => $value)
{
// find maximum SSE version
if(substr($feature, 0, 3) == 'sse' && ($cpu_flags & $value))
{
$extension_string = 'SSE ' . str_replace('_', '.', substr($feature, 3));
}
}
// Check for other instruction sets
foreach(array('avx2', 'avx', 'xop', 'fma3', 'fma4', 'rdrand', 'fsgsbase') as $instruction_set)
{
if(($cpu_flags & self::get_cpu_feature_constant($instruction_set)))
{
$extension_string .= ($extension_string != null ? ' + ' : null) . strtoupper($instruction_set);
}
}
return $extension_string;
}
public static function virtualization_technology()
{
$constants = self::get_cpu_feature_constants();
$cpu_flags = self::get_cpu_flags();
$virtualitzation_technology = false;
if(($cpu_flags & self::get_cpu_feature_constant('vmx')))
{
$virtualitzation_technology = 'VT-x';
}
else if(($cpu_flags & self::get_cpu_feature_constant('svm')))
{
$virtualitzation_technology = 'AMD-V';
}
return $virtualitzation_technology;
}
public static function lscpu_l2_cache()
{
$l2_cache = false;
if(isset(phodevi::$vfs->lscpu) && ($t = strpos(phodevi::$vfs->lscpu, 'L2 cache:')))
{
$lscpu = substr(phodevi::$vfs->lscpu, $t + strlen('L2 cache:') + 1);
$lscpu = substr($lscpu, 0, strpos($lscpu, PHP_EOL));
$l2_cache = trim($lscpu);
}
return $l2_cache;
}
public static function cpuinfo_core_count()
{
$core_count = self::read_cpuinfo_line('cpu cores');
if($core_count == false || !is_numeric($core_count))
{
$core_count = self::read_cpuinfo_line('core id', false);
if(is_numeric($core_count))
{
// cpuinfo 'core id' begins counting at 0
$core_count += 1;
}
}
if($core_count == false || !is_numeric($core_count))
{
$core_count = self::cpuinfo_thread_count();
}
return $core_count;
}
public static function cpuinfo_thread_count()
{
$thread_count = self::read_cpuinfo_line('processor', false);
if(is_numeric($thread_count))
{
// cpuinfo 'processor' begins counting at 0
$thread_count += 1;
}
return $thread_count;
}
public static function cpuinfo_cache_size()
{
// CPU cache size in KB
$cache_size = self::read_cpuinfo_line('cache size');
if(substr($cache_size, -3) == ' KB')
{
$cache_size = substr($cache_size, 0, -3);
}
else
{
$cache_size = null;
}
return $cache_size;
}
}
?>
|