通过配置CPU参数 worker_cpu_affinity 提升nginx性能

简介
Nginx默认没有开启利用多核cpu,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核cpu的性能。cpu是任务处理,计算最关键的资源,cpu核越多,性能就越好。

规则设定

(1)cpu有多少个核,就有几位数,1代表内核开启,0代表内核关闭

(2)worker_processes最多开启8个,8个以上性能就不会再提升了,而且稳定性会变的更低,因此8个进程够用了

示例:linode VPS 最低配,8核CPU,nginx配置信息:

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

 配置1:4 CPU(4 Core) + 4 worker_processes(每个worker_processes 使用1个CPU)

[root@AY1301180424258d59678 ~]#  cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3

 Nginx可以进行下面配置,每个CPU分配一个:

worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;

 配置2:8 CPU(8 Core) + 8 worker_processes(每个worker_processes 使用1个CPU)

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

 配置3:16 CPU(16 Core) + 16 worker_processes(每个worker_processes 使用1个CPU)

worker_processes 16;
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;

 配置4:2 CPU(2 Core) + 8 worker_processes(每个worker_processes 使用1个CPU)

worker_processes  8;  
worker_cpu_affinity 01 10 01 10 01 10 01 10;

 配置5:8 CPU(8 Core) +2 worker_processes(每个worker_processes 使用1个CPU)

worker_processes  2;  
worker_cpu_affinity 10101010 01010101;

说明:10101010表示开启了第2,4,6,8内核,01010101表示开始了1,3,5,7内核 
通过apache 的ab测试查看nginx对CPU的使用状况:

top - 11:16:01 up 188 days, 19:50,  2 users,  load average: 0.43, 0.72, 0.71
Tasks: 185 total,   1 running, 184 sleeping,   0 stopped,   0 zombie
Cpu0  : 26.0%us, 10.5%sy,  0.0%ni, 42.9%id, 19.3%wa,  0.7%hi,  0.7%si,  0.0%st
Cpu1  :  7.7%us,  5.4%sy,  0.0%ni, 87.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  9.7%us,  3.3%sy,  0.0%ni, 85.0%id,  1.7%wa,  0.0%hi,  0.3%si,  0.0%st
Cpu3  :  6.0%us,  4.0%sy,  0.0%ni, 90.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   8058060k total,  4953400k used,  3104660k free,   104856k buffers
Swap:   490488k total,    55468k used,   435020k free,  1610876k cached

如果多个CPU内核的利用率都相差不多,证明nginx己经成功的利用了多核CPU。
测试结束后,CPU内核的负载应该都同时降低。

 

参考:

http://www.freewil.net/blog/archives/198

http://luy.li/2009/12/30/nginx-user_agent-limit_rate/

 

posted @ 2015-09-07 11:17  踏雪无痕SS  阅读(4504)  评论(0编辑  收藏  举报