How can a php script get current cpu load in percentage?
On linux platform we have sys_loadavg()[0], that is not present on Windows.
            Asked
            
        
        
            Active
            
        
            Viewed 5,410 times
        
    2 Answers
3
            
            
        So far I know there is three methods:
COM call
    $com = new COM('winmgmts:{impersonationLevel=impersonate}');
    $cpus = $com->execquery('SELECT LoadPercentage FROM Win32_Processor');
    foreach ($cpus as $cpu) { $load = $cpu->LoadPercentage; break; }
com_dotnet extension required.
console call to wmic
    exec('wmic cpu get LoadPercentage', $p);
    return $p[2];
console call to typeperf
    $_ENV['typeperfCounter'] = '\processor(_total)\% processor time';
    exec('typeperf -sc 1 "'.$_ENV['typeperfCounter'].'"', $p);
    $line = explode(',', $p[2]);
    $load = trim($line[1], '"');
- Note: $_ENV['typeperfCounter'] should be set according system locale. For ru_RU it is - $_ENV['typeperfCounter'] = '\Процессор(_total)\% загруженности процессора';
All they running very slowly (be honestly I think they block execution of current thread for 1 sec to measure load).
 
    
    
        wapmorgan
        
- 101
- 1
- 12
0
            
            
        Check here my reply. Function that is compatible with Windows / Linux OS, tested.
In Linux returns an array of 3 values In Windows return an array of 1 value
 
    
    
        Community
        
- 1
- 1
 
    
    
        Stergios Zg.
        
- 652
- 6
- 9