user2001803
user2001803

Reputation: 11

How to get the load of each CPU Core in PHP on Windows?

Is there a way to do so? I tried using WMI, and the Win32_PerfRawData_PerfOS_Processor class, i end up having the PercentProcessorTime and Timestamp_PerfTime which would require to sleep a while to calculate the percentage, but thats not what i need, i need something that does not require PHP to wait a couple second, it should print out the load almost instantly

Upvotes: 1

Views: 957

Answers (3)

Ash Will
Ash Will

Reputation: 51

I Know This Is A Old Post But I Was In The Same Position A While Back But Managed To Figure It Out.

Instead Of Using

Win32_PerfRawData_PerfOS_Processor

Use

Win32_PerfFormattedData_PerfOS_Processor

For Example

$data['core']  = array();
$wmi           = new COM("winmgmts:\\");
$cpu_cores     = $wmi->execquery("SELECT PercentProcessorTime FROM Win32_PerfFormattedData_PerfOS_Processor");

foreach ($cpu_cores as $core) {
    $data['core'][] = $core->PercentProcessorTime;
}

// If CPU Is A Dual Core 
echo $data['core'][0].'%'; // CORE 1 LOAD
echo $data['core'][1].'%'; // CORE 2 LOAD
echo $data['core'][2].'%'; // TOTAL CPU LOAD

// If CPU Is A Quad Core 
echo $data['core'][0].'%'; // CORE 1 LOAD
echo $data['core'][1].'%'; // CORE 2 LOAD
echo $data['core'][2].'%'; // CORE 3 LOAD
echo $data['core'][3].'%'; // CORE 4 LOAD
echo $data['core'][4].'%'; // TOTAL CPU LOAD

And So On 

Upvotes: 0

user399666
user399666

Reputation: 19899

You could use XPerf, log the CPU activity and then access those log files via PHP? There also other suggestions in threads such as this one, which suggest giving Linfo a try.

Upvotes: 0

h00ligan
h00ligan

Reputation: 1481

One way would be to have another program that continuously checks and logs the processor load somewhere where PHP would be able to read the values directly. There would probably be a slight delay in the logging but nothing like a couple of seconds.

You could probably do that in a separate PHP script. Just make sure you sleep() for a short while between the checks, otherwise the processor reading script would push the processor to maximum load immediately.

Upvotes: 0

Related Questions