8

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

  • Is it possible for PHP to use more than one core simultaneously?
  • Might this be related to the virtualisation?
  • Follow-Up: What could be the reason for the system cpu usage being dominant?
PvB
  • 508
  • 1
  • 4
  • 16
  • 2
    This could definitely be caused by the code running inside PHP as well. What's it running? – Will Jan 24 '16 at 12:19
  • Thanks for your input, Will. I've actually tested the behavior using a simple for loop and could confirm it. I added the test case to the question. – PvB Jan 24 '16 at 12:37
  • 1
    Ah, got it! Thanks. Honestly, if we've ruled out the code, we might need to migrate this to ServerFault, as it may get the right kind of eyes there. As far as I know, a single PHP FastCGI worker cannot use multiple cores. But, in theory, the Linux scheduler could switch the process between CPUs. Hmm... – Will Jan 24 '16 at 12:41
  • Another thing to try--can you reproduce this same behavior without FastCGI by just running a PHP CLI script? – Will Jan 24 '16 at 12:43
  • 1
    I'll put in a migrate vote :) I'm over there too so I'll keep following this. – Will Jan 24 '16 at 12:47
  • 1
    Thanks for your suggestion. I've done that too. It shows the same usage of cores. But not the dominance of system cpu which must be related to the applications code. – PvB Jan 24 '16 at 12:47
  • 1
    @Will: I've flagged the question to be moved too. – PvB Jan 24 '16 at 12:56

1 Answers1

0
  • 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.

JesusTheHun
  • 1,217
  • 1
  • 10
  • 19
  • OP edited the post and explained that it's happening even with a simple `for` loop. So I don't think pthreads are involved. But maybe the Linux scheduler is switching it across CPUs for some reason? The script doesn't appear to be multi-core in any way. – Will Jan 24 '16 at 12:43
  • @JesusTheHun: Thanks for your answer. Pthread is out of the question in this case. It's even doing it with the simple for loop. But I'll migrate the question to server fault as Will has suggested as this is actually not code related. – PvB Jan 24 '16 at 12:45
  • Could you please post here the result of your investigation ? I'm interested in the answer. – JesusTheHun Jan 24 '16 at 12:54
  • @JesusTheHun: I've flagged the question to be moved to ServerFault. I'm not sure, whether I can post the results back here after the migration. – PvB Jan 24 '16 at 12:56