Das Skript läuft nur auf Linux-Servern - und ich würde gerne mal wissen auf wie vielen - oder wenigen 😟
- Läuft.
- Läuft nicht.
- Mangels öffentlicher URL nicht zeigbar: Auf einem Rechner mit AMD-CPU läuft es sehr wohl...
Bevor gefragt wird: Das Skript ändert nichts am System, es gibt nur - wenn die Voraussetzungen erfüllt sind - die möglichen Arbeitsfrequenzen und deren Nutzungszeit (je nach Auswahl menschen- und maschinenlesbar) aus.
<?php
class cpustat {
protected $governor;
protected $minfreq;
protected $curfreq;
protected $maxfreq;
protected $timestates;
protected $calcstates;
protected $sysruntime;
protected $cpuTimefactor = 100;
protected $hmFactor = 1000000;
protected $hmString = 'GHz';
function __construct() {
$this -> read_from_system();
}
function read_from_system() {
$this -> governor = trim( file_get_contents( '/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor' ) );
$this -> minfreq = trim( file_get_contents( '/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq' ) );
$this -> curfreq = trim( file_get_contents( '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq' ) );
$this -> maxfreq = trim( file_get_contents( '/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq' ) );
$this -> timestates = file_get_contents( '/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state' );
$this -> calcstates = $this -> calcTimestates();
}
function human_readble ( $z ) {
$z = $z / $this -> hmFactor;
return sprintf("%0.3f %s", $z, $this -> hmString );
}
private function getValue ( $v, $hm ) {
if ( $hm ) {
return $this -> human_readble( $v );
} else {
return $v;
}
}
function calcTimestates() {
$lines = explode ( "\n", $this -> timestates );
$sum = 0;
$arr = [];
foreach ( $lines as $line ) {
$line = trim( $line );
if ( $line ) {
list( $f, $t ) = explode ( ' ', $line );
$t = floor( $t / $this->cpuTimefactor );
if ( $t != 0 ) {
$sum += $t;
$f = $this -> human_readble( $f);
$arr[$f]['t_abs'] = $t;
$arr[$f]['t_hum'] = Seconds2Human( $t );
}
}
}
$this -> sysruntime = $sum;
foreach ( array_keys( $arr ) as $f ) {
$arr[$f]['t_rel'] = round( $arr[$f]['t_abs'] / $sum , 3 ) ;
$arr[$f]['t_100'] = round( $arr[$f]['t_abs'] / $sum * 100 , 1 ) . '%';
}
return $arr;
}
function getMinFreq( $hm=false ) {
return $this -> getValue ( $this -> minfreq, $hm );
}
function getCurFreq( $hm=false ) {
return $this -> getValue ( $this -> curfreq, $hm );
}
function getMaxFreq( $hm=false ) {
return $this -> getValue ( $this -> maxfreq, $hm );
}
function getSysRunTime( $hm=false ) {
if ( $hm ) {
return Seconds2Human( $this -> sysruntime );
} else {
$this -> sysruntime;
}
}
function getCalcstates () {
return $this -> calcstates;
}
function getGovernor () {
return $this -> governor;
}
}
function Seconds2Human( $s, $daystring="d" ) {
$s = (int) $s;
# Days
if ( $s > 86400 ) {
$d = floor( $s / 86400 );
$s = $s - $d * 86400;
} else {
$d = 0;
}
# Houres
if ( $s > 3600 ) {
$h = floor( $s / 3600 );
$s = $s - $h * 3600;
} else {
$h = 0;
}
# Minutes
if ( $s > 60 ) {
$m = floor( $s / 60 );
$s = $s - $m * 60;
} else {
$m = 0;
}
return sprintf( '%d%s %02d:%02d:%02d', $d, $daystring, $h, $m, $s );
}
header ( 'Content-Type: text/plain; charset=utf-8' );
error_reporting( E_ALL );
ini_set('display_errors', 1);
if (! is_file( '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq' ) ) {
echo "Leider wird Ihr System nicht unterstützt. Ende.";
exit;
}
$cpustat = new cpustat();
echo 'Governor: ' . $cpustat-> getGovernor( ) . PHP_EOL;
echo 'Geringste Frequenz: ' . $cpustat-> getMinFreq( true ) . PHP_EOL;
echo 'Geringste Frequenz: ' . $cpustat-> getMinFreq( true ) . PHP_EOL;
echo 'Gegenwärtige Frequenz: ' . $cpustat-> getCurFreq( true ) . PHP_EOL;
echo 'Maximale Frequenz: ' . $cpustat-> getMaxFreq( true ) . PHP_EOL;
echo 'System läuft seit: ' . $cpustat-> getSysRunTime( true ) . PHP_EOL . PHP_EOL;
echo 'Zeitliche Frequenznutzung:';
print_r( $cpustat-> getCalcstates() ) ;
echo PHP_EOL;