/usr/share/phoronix-test-suite/pts-core/objects/phodevi/phodevi.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 560 561 562 563 564 | <?php
/*
Phoronix Test Suite
URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
Copyright (C) 2009 - 2013, Phoronix Media
Copyright (C) 2009 - 2013, Michael Larabel
phodevi.php: The object for interacting with the PTS device framework
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 extends phodevi_base
{
public static $vfs = false;
private static $device_cache = null;
private static $smart_cache = null;
private static $sensors = null;
private static $operating_system = null;
private static $graphics = array(
'mesa' => false,
'ati' => false,
'nvidia' => false
);
private static $operating_systems = array(
'linux' => false,
'macosx' => false,
'solaris' => false,
'bsd' => false,
'hurd' => false,
'minix' => false,
'windows' => false
);
// A variable that modules can use to override Phodevi caching support, etc
public static $allow_phodevi_caching = true;
const no_caching = 1;
const std_caching = 2;
const smart_caching = 3;
public static function read_name($device)
{
return phodevi::read_property($device, 'identifier');
}
public static function load_sensors()
{
foreach(glob(dirname(__FILE__) . '/sensors/*') as $sensor_obj_file)
{
$sensor_obj_name = basename($sensor_obj_file, '.php');
if(!class_exists($sensor_obj_name, false))
{
include($sensor_obj_file);
}
$type = call_user_func(array($sensor_obj_name, 'get_type'));
$sensor = call_user_func(array($sensor_obj_name, 'get_sensor'));
if($type != null && $sensor != null)
{
self::$sensors[$type][$sensor] = $sensor_obj_name;
}
}
}
public static function available_sensors()
{
static $available_sensors = null;
if($available_sensors == null)
{
$available_sensors = array();
foreach(self::$sensors as $sensor_type => &$sensor)
{
foreach(array_keys($sensor) as $sensor_senses)
{
array_push($available_sensors, array($sensor_type, $sensor_senses));
}
}
}
return $available_sensors;
}
public static function supported_sensors()
{
static $supported_sensors = null;
if($supported_sensors == null)
{
$supported_sensors = array();
foreach(self::available_sensors() as $sensor)
{
if(self::sensor_supported($sensor))
{
array_push($supported_sensors, $sensor);
}
}
}
return $supported_sensors;
}
public static function unsupported_sensors()
{
static $unsupported_sensors = null;
if($unsupported_sensors == null)
{
$unsupported_sensors = array();
$supported_sensors = self::supported_sensors();
foreach(self::available_sensors() as $sensor)
{
if(!in_array($sensor, $supported_sensors))
{
array_push($unsupported_sensors, $sensor);
}
}
}
return $unsupported_sensors;
}
public static function read_sensor($sensor)
{
$value = false;
if(isset(self::$sensors[$sensor[0]][$sensor[1]]))
{
$value = call_user_func(array(self::$sensors[$sensor[0]][$sensor[1]], 'read_sensor'));
}
return $value;
}
public static function read_sensor_unit($sensor)
{
return call_user_func(array(self::$sensors[$sensor[0]][$sensor[1]], 'get_unit'));
}
public static function sensor_supported($sensor)
{
return isset(self::$sensors[$sensor[0]][$sensor[1]]) && call_user_func(array(self::$sensors[$sensor[0]][$sensor[1]], 'support_check'));
}
public static function sensor_identifier($sensor)
{
return $sensor[0] . '.' . $sensor[1];
}
public static function sensor_name($sensor)
{
$type = call_user_func(array(self::$sensors[$sensor[0]][$sensor[1]], 'get_type'));
$sensor = call_user_func(array(self::$sensors[$sensor[0]][$sensor[1]], 'get_sensor'));
if(strlen($type) < 4)
{
$formatted = strtoupper($type);
}
else
{
$formatted = ucwords($type);
}
switch($formatted)
{
case 'SYS':
$formatted = 'System';
break;
case 'HDD':
$formatted = 'Drive';
break;
}
$formatted .= ' ';
switch($sensor)
{
case 'temp':
$formatted .= 'Temperature';
break;
case 'freq':
$formatted .= 'Frequency';
break;
case 'memory':
$formatted .= 'Memory Usage';
break;
case 'power':
$formatted .= 'Power Consumption';
break;
default:
$formatted .= ucwords(str_replace('-', ' ', $sensor));
break;
}
return $formatted;
}
public static function system_hardware($return_as_string = true)
{
return self::system_information_parse(self::available_hardware_devices(), $return_as_string);
}
public static function system_software($return_as_string = true)
{
return self::system_information_parse(self::available_software_components(), $return_as_string);
}
public static function system_id_string()
{
$components = array(phodevi::read_property('cpu', 'model'), phodevi::read_name('motherboard'), phodevi::read_property('system', 'operating-system'), phodevi::read_property('system', 'compiler'));
return base64_encode(implode('__', $components));
}
public static function read_device_notes($device)
{
$devices = phodevi::available_hardware_devices();
if(isset($devices[$device]))
{
$notes_r = call_user_func(array('phodevi_' . $devices[$device], 'device_notes'));
}
else
{
$notes_r = array();
}
return is_array($notes_r) ? $notes_r : array();
}
public static function read_property($device, $read_property)
{
$value = false;
if(method_exists('phodevi_' . $device, 'read_property'))
{
$property = call_user_func(array('phodevi_' . $device, 'read_property'), $read_property);
if(!($property instanceof phodevi_device_property))
{
return false;
}
$cache_code = $property->cache_code();
if($cache_code != phodevi::no_caching && phodevi::$allow_phodevi_caching && isset(self::$device_cache[$device][$read_property]))
{
$value = self::$device_cache[$device][$read_property];
}
else
{
$dev_function_r = pts_arrays::to_array($property->get_device_function());
$dev_function = $dev_function_r[0];
$function_pass = array();
for($i = 1; $i < count($dev_function_r); $i++)
{
array_push($function_pass, $dev_function_r[$i]);
}
if(method_exists('phodevi_' . $device, $dev_function))
{
$value = call_user_func_array(array('phodevi_' . $device, $dev_function), $function_pass);
if(!is_array($value) && $value != null)
{
$value = pts_strings::strip_string($value);
if(function_exists('preg_replace'))
{
$value = preg_replace('/[^(\x20-\x7F)]*/','', $value);
}
}
if($cache_code != phodevi::no_caching)
{
self::$device_cache[$device][$read_property] = $value;
if($cache_code == phodevi::smart_caching)
{
// TODO: For now just copy the smart cache to other var, but come up with better yet efficient way
self::$smart_cache[$device][$read_property] = $value;
}
}
}
}
}
return $value;
}
public static function set_property($device, $set_property, $pass_args = array())
{
$return_value = false;
if(method_exists('phodevi_' . $device, 'set_property'))
{
$return_value = call_user_func(array('phodevi_' . $device, 'set_property'), $set_property, $pass_args);
}
return $return_value;
}
public static function create_vfs()
{
self::$vfs = new phodevi_vfs();
}
public static function initial_setup()
{
// Operating System Detection
$supported_operating_systems = pts_types::operating_systems();
$uname_s = strtolower(php_uname('s'));
foreach($supported_operating_systems as $os_check)
{
for($i = 0; $i < count($os_check); $i++)
{
if(strpos($uname_s, strtolower($os_check[$i])) !== false) // Check for OS
{
self::$operating_system = $os_check[0];
self::$operating_systems[strtolower($os_check[0])] = true;
break;
}
}
if(self::$operating_system != null)
{
break;
}
}
if(self::operating_system() == false)
{
self::$operating_system = 'Unknown';
}
// OpenGL / graphics detection
$graphics_detection = array('NVIDIA', array('ATI', 'AMD', 'fglrx'), array('Mesa', 'SGI'));
$opengl_driver = phodevi::read_property('system', 'opengl-vendor') . ' ' . phodevi::read_property('system', 'opengl-driver') . ' ' . phodevi::read_property('system', 'dri-display-driver');
$opengl_driver = trim(str_replace('Corporation', null, $opengl_driver)); // Prevents a possible false positive for ATI being in CorporATIon
foreach($graphics_detection as $gpu_check)
{
if(!is_array($gpu_check))
{
$gpu_check = array($gpu_check);
}
for($i = 0; $i < count($gpu_check); $i++)
{
if(stripos($opengl_driver, $gpu_check[$i]) !== false) // Check for GPU
{
self::$graphics[(strtolower($gpu_check[0]))] = true;
break;
}
}
}
self::load_sensors();
}
public static function set_device_cache($cache_array)
{
if(is_array($cache_array) && !empty($cache_array))
{
self::$smart_cache = array_merge(self::$smart_cache, $cache_array);
self::$device_cache = array_merge(self::$device_cache, $cache_array);
}
}
public static function clear_cache()
{
self::$smart_cache = array();
self::$device_cache = array();
}
public static function get_phodevi_cache_object($store_dir, $client_version = 0)
{
return new phodevi_cache(self::$smart_cache, $store_dir, $client_version);
}
protected static function system_information_parse($component_array, $return_as_string = true)
{
// Returns string of hardware information
$info = array();
foreach($component_array as $string => $id)
{
if(is_array($id) && count($id) == 2)
{
$value = self::read_property($id[0], $id[1]);
}
else
{
$value = self::read_name($id);
}
if($value != -1 && !empty($value))
{
$info[$string] = $value;
}
}
if($return_as_string)
{
$info_array = $info;
$info = null;
foreach($info_array as $type => $value)
{
if($info != null)
{
$info .= ', ';
}
$info .= $type . ': ' . $value;
}
}
return $info;
}
public static function system_uptime()
{
// Returns the system's uptime in seconds
$uptime = 1;
if(is_file('/proc/uptime'))
{
$uptime = pts_strings::first_in_string(pts_file_io::file_get_contents('/proc/uptime'));
}
else if(($uptime_cmd = pts_client::executable_in_path('uptime')) != false)
{
$uptime_counter = 0;
$uptime_output = shell_exec($uptime_cmd . ' 2>&1');
$uptime_output = substr($uptime_output, strpos($uptime_output, ' up') + 3);
$uptime_output = substr($uptime_output, 0, strpos($uptime_output, ' user'));
$uptime_output = substr($uptime_output, 0, strrpos($uptime_output, ',')) . ' ';
if(($day_end_pos = strpos($uptime_output, ' day')) !== false)
{
$day_output = substr($uptime_output, 0, $day_end_pos);
$day_output = substr($day_output, strrpos($day_output, ' ') + 1);
if(is_numeric($day_output))
{
$uptime_counter += $day_output * 86400;
}
}
if(($mins_end_pos = strpos($uptime_output, ' mins')) !== false)
{
$mins_output = substr($uptime_output, 0, $day_end_pos);
$mins_output = substr($mins_output, strrpos($mins_output, ' ') + 1);
if(is_numeric($mins_output))
{
$uptime_counter += $mins_output * 60;
}
}
if(($time_split_pos = strpos($uptime_output, ':')) !== false)
{
$hours_output = substr($uptime_output, 0, $time_split_pos);
$hours_output = substr($hours_output, strrpos($hours_output, ' ') + 1);
$mins_output = substr($uptime_output, $time_split_pos + 1);
$mins_output = substr($mins_output, 0, strpos($mins_output, ' '));
if(is_numeric($hours_output))
{
$uptime_counter += $hours_output * 3600;
}
if(is_numeric($mins_output))
{
$uptime_counter += $mins_output * 60;
}
}
if(is_numeric($uptime_counter) && $uptime_counter > 0)
{
$uptime = $uptime_counter;
}
}
return intval($uptime);
}
public static function cpu_arch_compatible($check_against)
{
$compatible = true;
$this_arch = phodevi::read_property('system', 'kernel-architecture');
$check_against = pts_arrays::to_array($check_against);
if(isset($this_arch[2]) && substr($this_arch, -2) == '86')
{
$this_arch = 'x86';
}
if(!in_array($this_arch, $check_against))
{
$compatible = false;
}
return $compatible;
}
public static function is_vendor_string($vendor)
{
return isset($vendor[2]) && pts_strings::string_only_contains($vendor, (pts_strings::CHAR_LETTER | pts_strings::CHAR_NUMERIC | pts_strings::CHAR_DECIMAL | pts_strings::CHAR_SPACE | pts_strings::CHAR_DASH)) && !pts_strings::has_in_istring($vendor, array('manufacturer', 'vendor', 'unknown', 'generic', 'warning')) && (!isset($vendor[7]) || strpos($vendor, ' ') !== false || pts_strings::times_occurred($vendor, pts_strings::CHAR_NUMERIC) == 0) && pts_strings::string_contains($vendor, pts_strings::CHAR_LETTER) && (isset($vendor[4]) || pts_strings::times_occurred($vendor, pts_strings::CHAR_LETTER) > 1) && substr($vendor, -1) != '-';
}
public static function is_product_string($product)
{
return phodevi::is_vendor_string($product) && !pts_strings::has_in_istring($product, array('VBOX', 'QEMU', 'Virtual', 'Family', '440BX', 'VMware', ' Gen', 'Core IGP'));
}
public static function operating_system()
{
return self::$operating_system;
}
public static function is_linux()
{
return self::$operating_systems['linux'];
}
public static function is_minix()
{
return self::$operating_systems['minix'];
}
public static function is_solaris()
{
return self::$operating_systems['solaris'];
}
public static function is_bsd()
{
return self::$operating_systems['bsd'];
}
public static function is_macosx()
{
return self::$operating_systems['macosx'];
}
public static function is_hurd()
{
return self::$operating_systems['hurd'];
}
public static function is_windows()
{
return self::$operating_systems['windows'];
}
public static function is_mesa_graphics()
{
return self::$graphics['mesa'];
}
public static function is_ati_graphics()
{
return self::$graphics['ati'];
}
public static function is_nvidia_graphics()
{
return self::$graphics['nvidia'];
}
public static function is_root()
{
return phodevi::read_property('system', 'username') == 'root';
}
}
phodevi::create_vfs();
if(PTS_IS_CLIENT)
{
phodevi::initial_setup();
}
?>
|