PDA

View Full Version : Apache - high cpu usage



keiko
24th February 2006, 01:52 PM
I have apache 2.0.51 on FC2 server, where I'am running a few virtualhost hosts
For some reason Cpu usage is very high from time to time and therefore load average is dropping.

There are enough resources (RAM, CPU) on the server but still apache
is slowing down. I tried to disable all logging of php and apache. Unfortunately it is not possible to upgrade the system or apache?

Is the problem in my apache or perhaps in the kernel (2.6.12-xenU/)?

top - 13:43:21 up 2 days, 12:34, 1 user, load average: 0.54, 0.18, 0.11
Tasks: 71 total, 3 running, 68 sleeping, 0 stopped, 0 zombie
Cpu(s): 87.4% us, 2.7% sy, 0.0% ni, 8.6% id, 0.0% wa, 0.0% hi, 1.3% si
Mem: 775296k total, 721472k used, 53824k free, 103068k buffers
Swap: 307192k total, 0k used, 307192k free, 385992k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
28650 apache 16 0 23176 14m 5500 S 39.5 2.0 0:02.28 httpd
28652 apache 16 0 20764 12m 5336 R 27.9 1.6 0:00.86 httpd

httpd.conf



Timeout 60
KeepAlive on
MaxKeepAliveRequests 100
KeepAliveTimeout 15


<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 1000
</IfModule>


<IfModule worker.c>
StartServers 5
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>


<IfModule perchild.c>
NumServers 5
StartThreads 5
MinSpareThreads 5
MaxSpareThreads 10
MaxThreadsPerChild 20
MaxRequestsPerChild 0
</IfModule>

foobar47
24th February 2006, 04:08 PM
Decrease StartServers options...

keiko
24th February 2006, 05:00 PM
Dropping Startservers option makes things even worse

top - 17:55:48 up 2 days, 16:46, 1 user, load average: 1.45, 0.64, 0.30
Tasks: 42 total, 2 running, 40 sleeping, 0 stopped, 0 zombie
Cpu(s): 84.0% us, 1.3% sy, 0.0% ni, 7.3% id, 0.0% wa, 0.0% hi, 7.3% si
Mem: 775296k total, 637244k used, 138052k free, 103416k buffers
Swap: 307192k total, 0k used, 307192k free, 389328k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9267 apache 16 0 23056 14m 5496 S 43.3 2.0 0:03.65 httpd
9205 apache 15 0 23192 14m 5472 S 25.0 2.0 0:02.24 httpd
9202 apache 15 0 23048 14m 5468 S 5.3 2.0 0:04.43 httpd
9250 apache 15 0 17660 9536 5312 S 3.7 1.2 0:00.48 httpd
9201 apache 15 0 17740 9592 5316 S 2.3 1.2 0:00.31 httpd
9249 apache 15 0 17632 9492 5316 S 2.0 1.2 0:00.56 httpd
9270 apache 15 0 22944 14m 5512 S 1.0 1.9 0:02.34 httpd

foobar47
27th February 2006, 10:25 AM
You still have 7 httpd services !
Decrease StartServers by 4 :
StartServers 4

keiko
27th February 2006, 11:35 AM
You still have 7 httpd services !
Decrease StartServers by 4 :
StartServers 4

Dropping StartServers option wont help. Dropped it to 4, but CPU still jumps too high. I tried to decrese CPU usage with RLimitCPU - parameter. It did not make any difference either. Is this some kind of kernel-problem?

top - 12:17:31 up 2 days, 12:16, 1 user, load average: 0.86, 0.48, 0.22
Tasks: 36 total, 3 running, 33 sleeping, 0 stopped, 0 zombie
Cpu(s): 81.4% us, 4.0% sy, 0.0% ni, 13.0% id, 0.0% wa, 0.0% hi, 1.7% si
Mem: 775296k total, 639728k used, 135568k free, 82404k buffers
Swap: 307192k total, 0k used, 307192k free, 444788k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7875 apache 16 0 23232 14m 5544 R 67.8 2.0 0:04.70 httpd
7754 apache 15 0 19180 11m 5572 S 4.0 1.5 0:10.23 httpd
7836 apache 15 0 22624 14m 5540 S 3.0 1.9 0:04.82 httpd
8199 apache 15 0 17660 9504 5288 S 2.3 1.2 0:00.17 httpd

foobar47
27th February 2006, 11:41 AM
Arg, thinking it was a memory problem... sorry !
But what is your CPU ?

foobar47
27th February 2006, 11:47 AM
I tried to decrese CPU usage with RLimitCPU - parameter. It did not make any difference either.
This parameter is for child process only, not parent !! ;)

keiko
27th February 2006, 12:14 PM
MainServer is Virtual Server (3 GHz CPU and 2000 MB RAM). There are only 2 VirtualSever on this hardware and there is a lot of CPU-capacity available. Changing options in httpd.conf wont help. Is it PHP which is causing this "leak"? . I'am running CMS and PHPCGI on this server.

foobar47
27th February 2006, 03:52 PM
I think CGI is the problem...

keiko
27th February 2006, 04:37 PM
I think CGI is the problem...
PHP is slower in CGI-mode, but it should not take that much CPU-time.

foobar47
28th February 2006, 11:30 AM
Are you running PHP as CGI or as an Apache module ?
PHP as CGI is not a good thing to do, it will eat plenty of ressources.
For each page request a new process is started.

For sure, PHP-CGI is more secure, flexible... but if you want security and still have a reasonable speed of execution use mod_fastcgi

Moreover, beware about statistics trends (if you have some) is heavy on database queries.
You should enable stat only for admin on the site...

Hope that help...

dougcsd
28th March 2006, 03:29 AM
Start servers won't cause a high load. Min with a small max can often do this by spawning and dropping processes to fast. Changing the max to a higher number and raising the MaxRequests will decrease the rate of forking/killing processes.

Try these settings.

<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 50
MaxClients 150
MaxRequestsPerChild 5000
</IfModule>

keiko
28th March 2006, 09:51 PM
Tuning Apache wont help much. I even uninstalled PHP Cgi mode and loads are still too high. A few klicks on the site and CPU usage jumps to 80%. However loads ares dropping back to normal level much faster now. I have only few (dynamic) sites on the server and loads really should not be that high.

These are my setting now:


Timeout 40
KeepAlive on
MaxKeepAliveRequests 200
KeepAliveTimeout 1
#
#
RLimitCPU 30 30
RLimitMEM 10000000 10000000
RLimitNPROC 25 25
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 120
MaxClients 120
MaxRequestsPerChild 500
</IfModule>

# worker MPM

<IfModule worker.c>
StartServers 2
ServerLimit 120
MaxClients 120
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 500
</IfModule>

# perchild MPM

<IfModule perchild.c>
NumServers 5
StartThreads 5
MinSpareThreads 5
MaxSpareThreads 10
MaxThreadsPerChild 20
MaxRequestPerChild 500
</IfModule>

Loads after a few klicks on the page:


- 23:46:51 up 2 days, 22:37, 1 user, load average: 0.41, 0.10, 0.03
Tasks: 38 total, 3 running, 35 sleeping, 0 stopped, 0 zombie
Cpu(s): 80.9% us, 0.0% sy, 0.0% ni, 17.5% id, 0.0% wa, 0.0% hi, 1.7% si
Mem: 775288k total, 349312k used, 425976k free, 82488k buffers
Swap: 307192k total, 0k used, 307192k free, 146148k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16711 apache 16 0 23364 15m 5556 R 69.8 2.0 0:06.06 httpd

dougcsd
28th March 2006, 11:04 PM
I would recommend the settings listed still. You have it setup to fork a maximum of five connections at startup. In reality on a page with images this is good for 1 client. After one client connects each additional client will spawn 5 more processes (forking requires more resources than serving the content). This is all handled by the parent process. That is why you see one apache process at a high load (managing ie (forking/killing) connections) and the rest at low loads just passing content. The additional problem you have is you only allow 10 total threads. This means you start forking and killing threads even at a moderate load with only three simultanious clients. Change your Max to 50 or more and see if this cuts the load on your parent process. You will see more theads waiting around to handle requests which should be fine with the amount of memory you listed. Those initial settings are very conservative and work well for sites with no load only.

Note system load and apache are not necessarily related. You may be running other processes in your scripts that are contributing to your load. This all depends on other config/cgi issues that I cant account for here.

Also note, if your dynamic sites are pulling from a database (ie mysql) with poor indexing or badly written queries this will also boost your system load, but has nothing to do with apache.