PvB
PvB

Reputation: 508

How is PHP using more than 100% CPU?

Checking a client's application on a third party virtual server I noticed an intriguing phenomenon: Single PHP processes seem to use more than one core. As far as I know, PHP can't do that.

Behavior description
Here's the output of htop when handling a single request to the web application: Output of htop
Output of htop, green being user cpu and red being system cpu

All four cores are at 100%, there is only one PHP process at 396% CPU and memory usage is low.
I've already investigated the high system cpu using strace or pidstat but couldn't find any IO problems.

Server information
The virtual server is a Debain LAMP environment using Intel's VT-X Virtualization and Virtuozzo providing 4 cores at 560 MHz:

Output of lscpu
Output of lscpu

The server is running PHP 5.5.9 in fast CGI served by apache 2.4.7 (prefork). Nginx 1.9.4 serves as reverese proxy, Plesk 12.5 is used to configure the server.

Sample PHP code (EDIT)
To rule out the applications code as the source I confirmed the behavior using a simple piece of code:

$array = array('z', 'y', 'x', 'h', 'd', 's', 'w', 'q', 'a');
for ($i=0; $i < 9999999; $i++) {
    sort($array); // Delaying execution
}
echo 'loop ended';

Two or more PHP processes (EDIT) If two or more request are handled simultaneously each process runs on as many cores as possible (2 processes => 2 cores each, 3 processes => 1.33 cores each).

Questions

Upvotes: 8

Views: 2869

Answers (1)

JesusTheHun
JesusTheHun

Reputation: 1237

  • Is it possible for PHP to use more than one core simultaneously?

Yes, it's called pthreads. It's designed to do true multi-threading in PHP.

  • Might this be related to the virtualisation?

Unlikely. I think the same as Will said, it's probably a sub-process launched by php.

  • Follow-Up: What could be the reason for the system cpu usage being dominant?

It's completely tied to up the application. But any multi-core task launched by php could show up like that. A java app for rasterizing a bucket of svg image for exemple.

Upvotes: 0

Related Questions