Re: [PHP] Command-line PHP script CPU usage goes sky-high, stays there--why?



René Fournier wrote:
Hello,

I have a command-line PHP script--called Listener--that is designed to
run indefinitely with a predictable CPU usage and memory footprint. In a
nutshell, it's a multi-client socket server that waits for incoming
connections, processes incoming data, stores results in a MySQL
database, and basically gets on with its life. And it works. No errors
or serious problems to speak of. And I've been running it for a couple
years on an Xserve dual-G5 2GHz w/ OS X Server 10.4.11). Six months ago,
the program would run for days, even a couple weeks, without a hitch.
The only reason I would stop the script is for some other purpose, like
a software update.

However, the number of socket clients connecting in the past 3-4 months
has steadily increased, and this seems to have exposed (if not created)
a strange performance "issue" with PHP 5.2.4, MySQL 5.0.45 and/or Mac OS
X Server 10.4.11. (I say "and/or" because I am unsure where the
problem's cause really lies.) Basically, after the script has been
running for a day or so (processing essentially the amount data that
used to take two weeks), the CPU usage of the machine goes from 30%
(normal) to 80-90%. This appears to be irrespective of the number of
clients connected to the server at the time, but rather the amount of
time the script has been running (and therefore cumulative cycles it's
operated, data processed, MySQL queries executed, etc.). And the CPU
usage stays high, even when the actual load (number of clients)
decreases. At this time, if I run top, I get the following info:

22512 mysqld 91.6% 8:22:12 31 106 125 305M+ 3.20M 260M
475M
17104 php 7.2% 81:14.01 1 15 145 5.08M 7.80M 10.9M-
87.5M
22537 Terminal 6.6% 2:59:59 22 176 240 12.3M 21.2M 18.3M-
236M
[...]

At first I thought, okay, it's MySQL's fault. Maybe a lot of
slow-running queries. But the slow query log is pretty clean. So maybe
it's a combination of Mac OS X and MySQL and PHP? I Googled for a
similar problem, and finally ran across this article:

http://www.shawnhogan.com/2005/10/mysql-problems-on-mac-os-x-server.html

...where the author describes a very similar CPU usage pattern. I tried
his suggested fixes and they seemed to have helped a little (or maybe
it's my wishful thinking--hard to tell), since the high CPU load issue
doesn't appear to happen as soon... But it still happens.

Anyway, I'm really stumped as to what to do next, where to look, etc. If
I stop the script, and restart it (but not the MySQL itself), CPU usage
goes back to normal--for about a day or two.

The only thing I thought might be connected is how many short-lived PHP
child processes Listener creates--around 20-30,000 per day. Sounds
higher, but on average it's just one every 2-3 seconds. Anyway, although
the number of child processes isn't concurrent, would there be a problem
with the number of historical child processes in view of ulimits or
kern.maxfilesperproc?

Anyway suggestions, tips, or links are much appreciated. Thanks.

...Rene


I have a server that "listens" like yours does. I get 80k - 85k connections a day to it.

When I first started it, I was only getting about 3k of connections aday. Then I upped the
listening pattern and it tanked. I noticed that all my mail/web/db connections just sat there.

When I investigated, I found that the number of connections to the server was being overloaded. So
I increased the kern.maxfilesperproc setting to 32000. All the problems went away.

I have about have the horse power you do, running OpenBSD 4.1, and it runs great now as my
"listener" / web / ftp / mail / named / database / spam filter / etc...

One question about the "listener" program, does it maintain a connection to the DB or does it
open/close a connection upon each socket connection? If it does the latter, you might look into
using a constant connection rather then opening/closing on a per connection basis.

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
.



Relevant Pages

  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... How does it relate to e.g. a script performing a large mysql query? ... 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: Does apache stop a script mid execution ?
    ... If the user calls a time consuming script and then stops or refreshes ... the way the client is implemented in the php mysql extension and 2.b) how the connection is set up. ... the server stops the script execution. ...
    (comp.lang.php)
  • Re: hitting the limits
    ... relatively straight forward MySql database. ... 10K visitors a month and an 80Mb database are nothing. ... heavily loaded server. ... But the connection will hang around until the garbage ...
    (comp.lang.php)
  • Re: VFP8 & MySQL
    ... > VFP functions like NVL to a MySQL one, definitely it made the switch very ... > About your connection problem, I have mine setup as default for 100 ... As for the server, we left it ... The server crashed and corrupted the database. ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Shorewall and MySQL problem - solved
    ... Well, now the system accepts any connection besides mysql for ip,ip,ip ... It is a solution for mysql, but your server is now more at risk from ... firewall via poor programming practices. ...
    (comp.os.linux.networking)