public static function read_sensor()
 {
     // Determine current percentage for processor usage
     if (phodevi::is_linux() || phodevi::is_bsd()) {
         $start_load = self::cpu_load_array(-1);
         sleep(1);
         $end_load = self::cpu_load_array(-1);
         for ($i = 0; $i < count($end_load); $i++) {
             $end_load[$i] -= $start_load[$i];
         }
         $percent = ($sum = array_sum($end_load)) == 0 ? 0 : 100 - $end_load[count($end_load) - 1] * 100 / $sum;
     } else {
         if (phodevi::is_solaris()) {
             // TODO: Add support for monitoring load on a per-core basis (through mpstat maybe?)
             $info = explode(' ', pts_strings::trim_spaces(pts_arrays::last_element(explode("\n", trim(shell_exec('sar -u 1 1 2>&1'))))));
             $percent = $info[1] + $info[2];
         } else {
             if (phodevi::is_macosx()) {
                 // CPU usage for user
                 $top = shell_exec('top -n 1 -l 1 2>&1');
                 $top = substr($top, strpos($top, 'CPU usage: ') + 11);
                 $percent = substr($top, 0, strpos($top, '%'));
             } else {
                 $percent = null;
             }
         }
     }
     if (!is_numeric($percent) || $percent < 0 || $percent > 100) {
         $percent = -1;
     }
     return pts_math::set_precision($percent, 2);
 }
 protected static function parse_result_process(&$test_run_request, $parse_xml_file, $log_file, $pts_test_arguments, $extra_arguments, $is_numeric_check = true, $prefix = null)
 {
     $test_identifier = $test_run_request->test_profile->get_identifier();
     $results_parser_xml = new pts_parse_results_nye_XmlReader($parse_xml_file);
     $result_match_test_arguments = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/MatchToTestArguments');
     $result_scale = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultScale');
     $result_proportion = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultProportion');
     $result_precision = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultPrecision');
     $result_template = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/OutputTemplate');
     $result_key = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultKey');
     $result_line_hint = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/LineHint');
     $result_line_before_hint = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/LineBeforeHint');
     $result_line_after_hint = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/LineAfterHint');
     $result_before_string = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultBeforeString');
     $result_after_string = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/ResultAfterString');
     $result_divide_by = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/DivideResultBy');
     $result_multiply_by = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/MultiplyResultBy');
     $strip_from_result = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/StripFromResult');
     $strip_result_postfix = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/StripResultPostfix');
     $multi_match = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/MultiMatch');
     $file_format = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ResultsParser/FileFormat');
     $test_result = false;
     if ($prefix != null && substr($prefix, -1) != '_') {
         $prefix .= '_';
     }
     for ($i = 0; $i < count($result_template); $i++) {
         if (!empty($result_match_test_arguments[$i]) && strpos($pts_test_arguments, $result_match_test_arguments[$i]) === false) {
             // This is not the ResultsParser XML section to use as the MatchToTestArguments does not match the PTS test arguments
             continue;
         }
         if ($result_key[$i] == null) {
             $result_key[$i] = '#_' . $prefix . 'RESULT_#';
         } else {
             switch ($result_key[$i]) {
                 case 'PTS_TEST_ARGUMENTS':
                     $result_key[$i] = '#_' . $prefix . str_replace(' ', '', $pts_test_arguments) . '_#';
                     break;
                 case 'PTS_USER_SET_ARGUMENTS':
                     $result_key[$i] = '#_' . $prefix . str_replace(' ', '', $extra_arguments) . '_#';
                     break;
             }
         }
         // The actual parsing here
         $start_result_pos = strrpos($result_template[$i], $result_key[$i]);
         if ($prefix != null && $start_result_pos === false && $result_template[$i] != 'csv-dump-frame-latencies' && $result_template[$i] != 'libframetime-output') {
             // XXX: technically the $prefix check shouldn't be needed, verify whether safe to have this check be unconditional on start_result_pos failing...
             return false;
         }
         $space_out_chars = array('(', ')', "\t");
         if (isset($file_format[$i]) && $file_format[$i] == 'CSV') {
             array_push($space_out_chars, ',');
         }
         if (isset($result_template[$i][$start_result_pos - 1]) && $result_template[$i][$start_result_pos - 1] == '/' || isset($result_template[$i][$start_result_pos + strlen($result_key[$i])]) && $result_template[$i][$start_result_pos + strlen($result_key[$i])] == '/') {
             array_push($space_out_chars, '/');
         }
         $end_result_pos = $start_result_pos + strlen($result_key[$i]);
         $end_result_line_pos = strpos($result_template[$i], "\n", $end_result_pos);
         $result_template_line = substr($result_template[$i], 0, $end_result_line_pos === false ? strlen($result_template[$i]) : $end_result_line_pos);
         $result_template_line = substr($result_template_line, strrpos($result_template_line, "\n"));
         $result_template_r = explode(' ', pts_strings::trim_spaces(str_replace($space_out_chars, ' ', str_replace('=', ' = ', $result_template_line))));
         $result_template_r_pos = array_search($result_key[$i], $result_template_r);
         if ($result_template_r_pos === false) {
             // Look for an element that partially matches, if like a '.' or '/sec' or some other pre/post-fix is present
             foreach ($result_template_r as $x => $r_check) {
                 if (isset($result_key[$x]) && strpos($r_check, $result_key[$x]) !== false) {
                     $result_template_r_pos = $x;
                     break;
                 }
             }
         }
         $search_key = null;
         $line_before_key = null;
         if (isset($result_line_hint[$i]) && $result_line_hint[$i] != null && strpos($result_template_line, $result_line_hint[$i]) !== false) {
             $search_key = $result_line_hint[$i];
         } else {
             if (isset($result_line_before_hint[$i]) && $result_line_before_hint[$i] != null && strpos($result_template[$i], $result_line_hint[$i]) !== false) {
                 $search_key = null;
                 // doesn't really matter what this value is
             } else {
                 if (isset($result_line_after_hint[$i]) && $result_line_after_hint[$i] != null && strpos($result_template[$i], $result_line_hint[$i]) !== false) {
                     $search_key = null;
                     // doesn't really matter what this value is
                 } else {
                     foreach ($result_template_r as $line_part) {
                         if (strpos($line_part, ':') !== false && strlen($line_part) > 1) {
                             // add some sort of && strrpos($result_template[$i], $line_part)  to make sure there isn't two of the same $search_key
                             $search_key = $line_part;
                             break;
                         }
                     }
                     if ($search_key == null && isset($result_key[$i])) {
                         // Just try searching for the first part of the string
                         /*
                         for($i = 0; $i < $result_template_r_pos; $i++)
                         {
                         	$search_key .= $result_template_r[$i] . ' ';
                         }
                         */
                         // This way if there are ) or other characters stripped, the below method will work where the above one will not
                         $search_key = substr($result_template_line, 0, strpos($result_template_line, $result_key[$i]));
                     }
                 }
             }
         }
         if (is_file($log_file)) {
             $result_output = file_get_contents($log_file);
         } else {
             // Nothing to parse
             return false;
         }
         $test_results = array();
         $already_processed = false;
         $frame_time_values = null;
         if ($result_template[$i] == 'libframetime-output') {
             $already_processed = true;
             $frame_time_values = array();
             $line_values = explode(PHP_EOL, $result_output);
             if (!empty($line_values) && isset($line_values[3])) {
                 foreach ($line_values as &$v) {
                     if (substr($v, -3) == ' us' && substr($v, 0, 10) == 'Frametime ') {
                         $frametime = substr($v, 10);
                         $frametime = substr($frametime, 0, -3);
                         if ($frametime > 2000) {
                             $frametime = $frametime / 1000;
                             array_push($frame_time_values, $frametime);
                         }
                     }
                 }
                 $frame_time_values = pts_math::remove_outliers($frame_time_values);
             }
         } else {
             if ($result_template[$i] == 'csv-dump-frame-latencies') {
                 $already_processed = true;
                 $frame_time_values = explode(',', $result_output);
             }
         }
         if (!empty($frame_time_values) && isset($frame_time_values[3])) {
             // Get rid of the first value
             array_shift($frame_time_values);
             foreach ($frame_time_values as $f => &$v) {
                 if (!is_numeric($v) || $v == 0) {
                     unset($frame_time_values[$f]);
                     continue;
                 }
                 $v = 1000 / $v;
             }
             switch ($prefix) {
                 case 'MIN_':
                     $val = min($frame_time_values);
                     break;
                 case 'MAX_':
                     $val = max($frame_time_values);
                     break;
                 case 'AVG_':
                 default:
                     $val = array_sum($frame_time_values) / count($frame_time_values);
                     break;
             }
             if ($val != 0) {
                 array_push($test_results, $val);
             }
         }
         if (($search_key != null || isset($result_line_before_hint[$i]) && $result_line_before_hint[$i] != null || (isset($result_line_after_hint[$i]) && $result_line_after_hint[$i]) != null || isset($result_key[$i]) && $result_template_r[0] == $result_key[$i]) && $already_processed == false) {
             $is_multi_match = !empty($multi_match[$i]) && $multi_match[$i] != 'NONE';
             do {
                 $result_count = count($test_results);
                 if ($result_line_before_hint[$i] != null) {
                     pts_client::test_profile_debug_message('Result Parsing Line Before Hint: ' . $result_line_before_hint[$i]);
                     $result_line = substr($result_output, strpos($result_output, "\n", strrpos($result_output, $result_line_before_hint[$i])));
                     $result_line = substr($result_line, 0, strpos($result_line, "\n", 1));
                     $result_output = substr($result_output, 0, strrpos($result_output, "\n", strrpos($result_output, $result_line_before_hint[$i]))) . "\n";
                 } else {
                     if ($result_line_after_hint[$i] != null) {
                         pts_client::test_profile_debug_message('Result Parsing Line After Hint: ' . $result_line_after_hint[$i]);
                         $result_line = substr($result_output, 0, strrpos($result_output, "\n", strrpos($result_output, $result_line_before_hint[$i])));
                         $result_line = substr($result_line, strrpos($result_line, "\n", 1) + 1);
                         $result_output = null;
                     } else {
                         if ($search_key != null) {
                             $search_key = trim($search_key);
                             pts_client::test_profile_debug_message('Result Parsing Search Key: ' . $search_key);
                             $result_line = substr($result_output, 0, strpos($result_output, "\n", strrpos($result_output, $search_key)));
                             $start_of_line = strrpos($result_line, "\n");
                             $result_output = substr($result_line, 0, $start_of_line) . "\n";
                             $result_line = substr($result_line, $start_of_line + 1);
                         } else {
                             // Condition $result_template_r[0] == $result_key[$i], include entire file since there is nothing to search
                             pts_client::test_profile_debug_message('No Result Parsing Hint, Including Entire Result Output');
                             $result_line = trim($result_output);
                         }
                     }
                 }
                 pts_client::test_profile_debug_message('Result Line: ' . $result_line);
                 $result_r = explode(' ', pts_strings::trim_spaces(str_replace($space_out_chars, ' ', str_replace('=', ' = ', $result_line))));
                 $result_r_pos = array_search($result_key[$i], $result_r);
                 if (!empty($result_before_string[$i])) {
                     // Using ResultBeforeString tag
                     $result_before_this = array_search($result_before_string[$i], $result_r);
                     if ($result_before_this !== false) {
                         array_push($test_results, $result_r[$result_before_this - 1]);
                     }
                 } else {
                     if (!empty($result_after_string[$i])) {
                         // Using ResultBeforeString tag
                         $result_after_this = array_search($result_after_string[$i], $result_r);
                         if ($result_after_this !== false) {
                             $result_after_this++;
                             for ($f = $result_after_this; $f < count($result_r); $f++) {
                                 if (in_array($result_r[$f], array(':', ',', '-', '='))) {
                                     continue;
                                 }
                                 array_push($test_results, $result_r[$f]);
                                 break;
                             }
                         }
                     } else {
                         if (isset($result_r[$result_template_r_pos])) {
                             array_push($test_results, $result_r[$result_template_r_pos]);
                         }
                     }
                 }
             } while ($is_multi_match && count($test_results) != $result_count && !empty($result_output));
         }
         foreach ($test_results as $x => &$test_result) {
             if ($strip_from_result[$i] != null) {
                 $test_result = str_replace($strip_from_result[$i], null, $test_result);
             }
             if ($strip_result_postfix[$i] != null && substr($test_result, 0 - strlen($strip_result_postfix[$i])) == $strip_result_postfix[$i]) {
                 $test_result = substr($test_result, 0, 0 - strlen($strip_result_postfix[$i]));
             }
             // Expand validity checking here
             if ($is_numeric_check == true && is_numeric($test_result) == false) {
                 unset($test_results[$x]);
                 continue;
             }
             if ($result_divide_by[$i] != null && is_numeric($result_divide_by[$i]) && $result_divide_by[$i] != 0) {
                 $test_result = $test_result / $result_divide_by[$i];
             }
             if ($result_multiply_by[$i] != null && is_numeric($result_multiply_by[$i]) && $result_multiply_by[$i] != 0) {
                 $test_result = $test_result * $result_multiply_by[$i];
             }
         }
         if (empty($test_results)) {
             continue;
         }
         switch ($multi_match[$i]) {
             case 'REPORT_ALL':
                 $test_result = implode(',', $test_results);
                 break;
             case 'AVERAGE':
             default:
                 if ($is_numeric_check) {
                     $test_result = array_sum($test_results) / count($test_results);
                 }
                 break;
         }
         if ($test_result != false) {
             if ($result_scale[$i] != null) {
                 $test_run_request->test_profile->set_result_scale($result_scale[$i]);
             }
             if ($result_proportion[$i] != null) {
                 $test_run_request->test_profile->set_result_proportion($result_proportion[$i]);
             }
             if ($result_precision[$i] != null) {
                 $test_run_request->set_result_precision($result_precision[$i]);
             }
             break;
         }
     }
     return $test_result;
 }
 private function cpu_usage_solaris()
 {
     //TODO test this on Solaris
     //TODO: Add support for monitoring load on a per-core basis (through mpstat maybe?)
     $info = explode(' ', pts_strings::trim_spaces(pts_arrays::last_element(explode("\n", trim(shell_exec('sar -u 1 1 2>&1'))))));
     $percent = $info[1] + $info[2];
     return $percent;
 }
 public static function read_ati_overdrive($attribute, $adapter = 0)
 {
     // Read ATI OverDrive information
     // OverDrive supported in fglrx 8.52+ drivers
     $value = false;
     if ($amdconfig = self::find_amdconfig()) {
         if ($attribute == 'Temperature') {
             $info = shell_exec($amdconfig . ' --adapter=' . $adapter . ' --od-gettemperature 2>&1');
             if (($start = strpos($info, 'Temperature -')) !== false) {
                 $info = substr($info, $start + 14);
                 $value = substr($info, 0, strpos($info, ' C'));
             }
         } else {
             if ($attribute == 'FanSpeed') {
                 // Right now there is no standardized interface to get the fan speed through besides the pplib command
                 $info = shell_exec($amdconfig . ' --adapter=' . $adapter . ' --pplib-cmd \'get fanspeed 0\' 2>&1');
                 if (($start = strpos($info, 'Fan Speed:')) !== false) {
                     $info = substr($info, $start + 11);
                     $info = substr($info, 0, strpos($info, '%'));
                     if (is_numeric($info)) {
                         $value = $info;
                     }
                 }
             } else {
                 $info = shell_exec($amdconfig . ' --adapter=' . $adapter . ' --od-getclocks 2>&1');
                 if (strpos($info, 'GPU') !== false) {
                     foreach (explode("\n", $info) as $line) {
                         $line_r = pts_strings::colon_explode($line);
                         if (count($line_r) == 2) {
                             $od_option = str_replace(' ', null, $line_r[0]);
                             if ($od_option == $attribute) {
                                 $od_value = pts_strings::trim_spaces($line_r[1]);
                                 $od_value = str_replace(array('%'), null, $od_value);
                                 $od_value_r = explode(' ', $od_value);
                                 $value = count($od_value_r) == 1 ? $od_value_r[0] : $od_value_r;
                             }
                         }
                     }
                 }
             }
         }
     }
     return $value;
 }
 public static function strip_string($str)
 {
     // Clean a string containing hardware information of some common things to change/strip out
     $change_phrases = array('MCH' => 'Memory Controller Hub', 'AMD' => 'Advanced Micro Devices', 'MSI' => 'MICRO-STAR INTERNATIONAL', 'SiS' => 'Silicon Integrated Systems', 'Abit' => 'http://www.abit.com.tw/', 'ASUS' => 'ASUSTeK', 'HP' => 'Hewlett-Packard', 'NVIDIA' => 'nVidia', 'HDD' => 'HARDDISK', 'Intel' => 'Intel64', 'HD' => 'High Definition', 'IGP' => array('Integrated Graphics Controller', 'Express Graphics Controller', 'Integrated Graphics Device', 'Chipset Integrated'));
     foreach ($change_phrases as $new_phrase => $original_phrase) {
         $str = str_ireplace($original_phrase, $new_phrase, $str);
     }
     $remove_phrases = array('incorporation', 'corporation', 'corp.', 'invalid', 'technologies', 'technology', 'version', ' project ', 'computer', 'To Be Filled By', 'ODM', 'O.E.M.', 'Desktop Reference Platform', 'small form factor', 'convertible', ' group', 'chipset', 'community', 'reference', 'communications', 'semiconductor', 'processor', 'host bridge', 'adapter', ' CPU', 'platform', 'international', 'express', 'graphics', 'none', 'electronics', 'integrated', 'alternate', 'quad-core', 'memory', 'series', 'network', 'motherboard', 'electric ', 'industrial ', 'serverengines', 'Manufacturer', 'x86/mmx/sse2', '/AGP/SSE/3DNOW!', '/AGP/SSE2', 'controller', '(extreme graphics innovation)', 'pci-e_gfx and ht3 k8 part', 'pci-e_gfx and ht1 k8 part', 'Northbridge only', 'dual slot', 'dual-core', 'dual core', 'microsystems', 'not specified', 'single slot', 'genuine', 'unknown device', 'systemberatung', 'gmbh', 'graphics adapter', 'video device', 'http://', 'www.', '.com', '.tw/', '/pci/sse2/3dnow!', '/pcie/sse2', '/pci/sse2', 'balloon', 'network connection', 'ethernet', 'limited.', ' system', 'compliant', 'co. ltd', 'co.', 'ltd.', 'LTD ', ' LTD', '\\AE', '(r)', '(tm)', 'inc.', 'inc', '6.00 PG', ',', '\'', '_ ', '_ ', 'corp', 'product name', 'base board', 'mainboard', 'pci to pci', ' release ', 'nee ', ' AG ', 'with Radeon HD', '/DRAM');
     $str = str_ireplace($remove_phrases, ' ', $str);
     if (($w = stripos($str, 'WARNING')) !== false) {
         // to get rid of Scheisse like 'Gtk-WARNING **: Unable'
         $str = substr($str, 0, strrpos($str, ' ', 0 - (strlen($str) - $w)));
     }
     $str = pts_strings::trim_spaces($str);
     // Fixes an AMD string issue like 'FX -4100' due to stripping (TM) from in between characters, possibly other cases too
     $str = str_replace(' -', '-', $str);
     return $str;
 }
 public static function mem_usage($TYPE = 'TOTAL', $READ = 'USED')
 {
     // Reads system memory usage
     $mem_usage = -1;
     if (pts_client::executable_in_path('free') != false) {
         $mem = explode("\n", shell_exec('free -t -m 2>&1'));
         $grab_line = null;
         $buffers_and_cache = 0;
         for ($i = 0; $i < count($mem); $i++) {
             $line_parts = pts_strings::colon_explode($mem[$i]);
             if (count($line_parts) == 2) {
                 $line_type = $line_parts[0];
                 if ($TYPE == 'MEMORY' && $line_type == 'Mem') {
                     $grab_line = $line_parts[1];
                 } else {
                     if ($TYPE == 'SWAP' && $line_type == 'Swap') {
                         $grab_line = $line_parts[1];
                     } else {
                         if ($TYPE == 'TOTAL' && $line_type == 'Total') {
                             $grab_line = $line_parts[1];
                         } else {
                             if ($line_type == '-/+ buffers/cache' && $TYPE != 'SWAP') {
                                 $buffers_and_cache = pts_arrays::first_element(explode(' ', pts_strings::trim_spaces($line_parts[1])));
                             }
                         }
                     }
                 }
             }
         }
         if (!empty($grab_line)) {
             $grab_line = pts_strings::trim_spaces($grab_line);
             $mem_parts = explode(' ', $grab_line);
             if ($READ == 'USED') {
                 if (count($mem_parts) >= 2 && is_numeric($mem_parts[1])) {
                     $mem_usage = $mem_parts[1] - $buffers_and_cache;
                 }
             } else {
                 if ($READ == 'TOTAL') {
                     if (count($mem_parts) >= 1 && is_numeric($mem_parts[0])) {
                         $mem_usage = $mem_parts[0];
                     }
                 } else {
                     if ($READ == 'FREE') {
                         if (count($mem_parts) >= 3 && is_numeric($mem_parts[2])) {
                             $mem_usage = $mem_parts[2];
                         }
                     }
                 }
             }
         }
     } else {
         if (pts_client::executable_in_path('vm_stat') != false) {
             $vmstats = explode("\n", shell_exec('vm_stat 2>&1'));
             $grab_line = null;
             // buffers_and_cache
             foreach ($vmstats as $vmstat_line) {
                 $line_parts = pts_strings::colon_explode($vmstat_line);
                 if (self::$page_size == -1) {
                     strtok($vmstat_line, ':');
                     $tok = strtok(' ');
                     while (self::$page_size == -1) {
                         if (is_numeric($tok)) {
                             self::$page_size = $tok;
                         } else {
                             $tok = strtok(' ');
                         }
                     }
                     continue;
                 }
                 //$line_parts[1] = pts_strings::trim_spaces($line_parts[1]);
                 $line_type = strtok($vmstat_line, ':');
                 $line_value = strtok(' .');
                 if ($TYPE == 'MEMORY') {
                     if ($line_type == 'Pages active' && $READ == 'USED') {
                         $mem_usage = $line_value / (1048576 / self::$page_size);
                         break;
                     }
                     if ($line_type == 'Pages free' && $READ == 'FREE') {
                         $mem_usage = $line_value / (1048576 / self::$page_size);
                         break;
                     }
                 }
             }
             $mem_usage = pts_math::set_precision($mem_usage);
         }
     }
     return $mem_usage;
 }