How to Make Good Use of Multicore Cpus in Your PHP/MySQL Applications

How do you make good use of multicore CPUs in your PHP/MySQL applications?

PHP is not quite oriented towards multi-threading : as you already noticed, each page is served by one PHP process -- that does one thing at a time, including just "waiting" while an SQL query is executed on the database server.

There is not much you can do about that, unfortunately : it's the way PHP works.


Still, here's a couple of thoughts :

  • First of all, you'll probably have more that 1 user at a time on your server, which means you'll serve several pages at the same time, which, in turn, means you'll have several PHP processes and SQL queries running at the same time... which means several cores of your server will be used.

    • Each PHP process will run on one core, in response to the request of one user, but there are several sub-processes of Apache running in parallel (one for each request, up to a couple of dozens or hundreds, depending on your configuration)
    • The MySQL server is multi-threaded, which means it can use several distinct cores to answer several concurrent requests -- even if each request cannot be served by more that one core.

So, in fact, your server's 8 core will end up being used ;-)


And, if you think your pages are taking too long to generate, a possible solution is to separate your calculations in two groups :

  • On one hand, the things that have to be done to generate the page : for those, there is not much you can do
  • On the other hand, the things that have to be run sometimes, but not necessarily immediately

    • For instance, I am think about some statistics calculations : you want them to be quite up to date, but if they lag a couple of minutes behind, that's generally quite OK.
    • Same for e-mail sending : anyway, several minutes will pass before your users receive/read their mail, so there is no need to send them immediately.

For the kind of situations in my second point, as you don't need those things done immediately... Well, just don't do them immediately ;-)

A solution that I often use is some queuing mechanism :

  • The web application store things in a "todo-list"
  • And that "todo-list" is de-queued by some batches that are run frequently via a cronjob

And for some other manipulations, you just want them run every X minutes -- and, here too, a cronjob is the perfect tool.

Using more than one CPU in PHP

The only way to use multiple cores is to run multiple threads or processes.

For web applications there is usually one PHP process per running script, so this is already the case: Multiple requests will use multiple cores automatically.

You can't use multiple cores for the same request though, but usually this doesn't matter for a web app.

Unexpected cores synchronization in pcntl_fork()

I've had chance to repeat experiment on machine with 4 physical cores. lscpu output about machine CPU specification:

CPU(s): 4

On-line CPU(s) list: 0-3

Thread(s) per core: 1

Core(s) per socket: 4

Socket(s): 1

And I've got such CPU load graph when running experiment:
Sample Image

So it can be seen that in this case there is no cross-core synchronization and each core is loaded with about twice as many work as previous one.
Confirmed that above mentioned effect is related that test machine had 4 virtual cores (2 physical x 2 hyper-threads) and not just 4 physical ones.

Is it possible to make MYSQL single thread to use multicore

MySQL...

  • In general, no more than 1 core is used for one connection.
  • There are a few 'background' threads that make limited use of extra cores. (Example: keeping background I/O tasks moving.)
  • In MySQL 8.0, a very few special cases will use multiple cores. https://www.percona.com/blog/2019/01/23/mysql-8-0-14-a-road-to-parallel-query-execution-is-wide-open/
  • If your table(s) is not fully cached, using multiple cores will be futile if it is already I/O-bound.
  • The A.1.7 quote is misleading -- It is only saying that multiple connections can take advantage of multiple cores. And "do the right thing" when they try to modify the same row(s).
  • I filed https://bugs.mysql.com/bug.php?id=100892 in hopes of getting them to fix the misleading nature of the entry.

PHP calling several functions at once?

You can use pThread , here is a good place to start :

  • How can one use multi threading in PHP applications
  • Improving HTML scraper efficiency with pcntl_fork()
  • How do you make good use of multicore CPUs in your PHP/MySQL applications?
  • pcntl runs the same code several times, assistance required
  • Calling rand/mt_rand on forked children yields identical results

Example

$ts = array();
$ts[] = new Call("function1");
$ts[] = new Call("function2");
$ts[] = new Call("function3");

foreach($ts as $t) {
$t->start();
}

foreach($ts as $t) {
$t->join();
}

Simple Thread Class

class Call extends Thread {

function __construct($func) {
$this->func = $func;
}

function run() {
call_user_func($this->func);
}
}


Related Topics



Leave a reply



Submit