Voting

: max(eight, nine)?
(Example: nine)

The Note You're Voting On

wowabbs+php at gmail dot com
4 years ago
<?php // Usage processor in Windows using FFI
if(!dl("ffi")) // Load the extension
throw new Exception('Cound not load the FFI extension.');

function
GetSystemTimes()
{
static
$Kernel32;
$Kernel32??=FFI::cdef(<<<'IDL'
bool GetSystemTimes(long long *IdleTime, long long *KernelTime, long long *UserTime);
int GetLastError();
IDL, 'Kernel32.dll');

$IdleTime = FFI::new('long long');
$KernelTime = FFI::new('long long'); // already has idle time
$UserTime = FFI::new('long long');

if(
$Kernel32->GetSystemTimes(FFI::addr($IdleTime), FFI::addr($KernelTime), FFI::addr($UserTime)))
return [
'Idle' =>$IdleTime ->cdata,
'Kernel' =>$KernelTime ->cdata, // already has idle time
'User' =>$UserTime ->cdata,
];
return [];
}

function
GetSystemTimesDelta()
{
static
$Last=null;
static
$Delta=[0,1];
$Times=GetSystemTimes();
$Last??=$Times;
$Idle =$Times['Idle' ]-$Last['Idle' ];
$All =$Times['Kernel' ]-$Last['Kernel' ]+ // Kernel already has idle time
$Times['User' ]-$Last['User' ];
if(
$All>1_000_000) // 100ms
{
$Delta=[$Idle, $All];
$Last=$Times;
}
return
$Delta;
}

function
ProcessorLoad()
{
static
$Load=0;
[
$Idle, $All]=GetSystemTimesDelta();
if(
$All>0.0)
$Load=1-$Idle/$All;
return
$Load;
}

function
KbHit()
{
Static
$Sys;
$Sys??=FFI::cdef('bool _kbhit();', 'msvcrt.dll');
return
$Sys->_kbhit();
}

while(!
KbHit())
{
echo
str_pad((string)round(ProcessorLoad()*100), 3, ' ', STR_PAD_LEFT), "%\n";
sleep(1);
}
?>

<< Back to user notes page

To Top