Nginx配置多核CPU的配置项worker_cpu_affinity使用方法

Song128 次浏览0个评论2021年07月16日
Nginx默认没有开启利用多核cpu,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核cpu的性能

cpu是任务处理,计算最关键的资源,cpu核越多,性能就越好。

默认没有开启?那怎么开启?可以通过增加worker_cpu_affinity配置参数来充分利用多核cpu的性能?那是光配置这个worker_cpu_affinity配置参数吗?

我们的服务器只配置了worker_processes,但在进行压测的时候各CPU的使用率会同步上升,worker_processes配置是指定启动多少个nginx进程,linux会自动进行任务分配,所以压力上来后各CPU都会进行负载,所以worker_processes配置肯定是启到了启用多核CPU的作用。worker_cpu_affinity配置我认为是可以实现更自由的指定CPU分配,可以将进程和CPU核心对应起来。我没有进行压力测试,在线上服务器加入了worker_cpu_affinity配置,但并没有感觉到有什么效果,相反感觉负载还升高了。当然我这里的服务器本身负载较低,可能是这个原因。以下的服务器的配置和启用worker_cpu_affinity配置的结果。

#服务器的配置:
[dev@ER-01 conf]$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                12
On-line CPU(s) list:   0-11
Thread(s) per core:    1
Core(s) per socket:    6
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 63
Model name:            Intel(R) Xeon(R) CPU E5-2603 v3 @ 1.60GHz
Stepping:              2
CPU MHz:               1598.083
BogoMIPS:              3195.97
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              15360K
NUMA node0 CPU(s):     0-2,6-8
NUMA node1 CPU(s):     3-5,9-11

#用网上的说法 worker_processes最多开启8个,8个以上性能提升不会再提升了,我这里用的开启8个进程的负载结果。看来负载升高了
[dev@ER-01 ~]$ top
top - 16:54:52 up 1057 days,  3:14,  3 users,  load average: 0.93, 0.88, 0.38
Tasks: 461 total,   2 running, 459 sleeping,   0 stopped,   0 zombie
Cpu0  :  4.0%us,  0.7%sy,  0.0%ni, 93.0%id,  1.7%wa,  0.0%hi,  0.7%si,  0.0%st
Cpu1  :  0.7%us,  0.3%sy,  0.0%ni, 95.1%id,  3.6%wa,  0.0%hi,  0.3%si,  0.0%st
Cpu2  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.3%us,  0.0%sy,  0.0%ni, 99.0%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu4  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu5  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu6  :  0.0%us,  0.0%sy,  0.0%ni, 99.7%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu7  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu8  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu9  :  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu10 :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu11 :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16204548k total, 15229220k used,   975328k free,  4639952k buffers
Swap:  4194300k total,   460300k used,  3734000k free,  5479856k cached

#这是只使用worker_processes  12;配置的效果,负载明显比上面低了,而且同样有时各CPU都有一些负载
[dev@ER-01 ~]$ top
top - 17:15:14 up 1057 days,  3:34,  3 users,  load average: 0.02, 0.04, 0.14
Tasks: 465 total,   1 running, 464 sleeping,   0 stopped,   0 zombie
Cpu0  :  7.7%us,  1.3%sy,  0.0%ni, 83.3%id,  7.0%wa,  0.0%hi,  0.7%si,  0.0%st
Cpu1  :  2.0%us,  0.3%sy,  0.0%ni, 97.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.7%us,  0.0%sy,  0.0%ni, 99.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu4  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu5  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu6  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu7  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu8  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu9  :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu10 :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu11 :  0.3%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16204548k total, 15492652k used,   711896k free,  4675160k buffers
Swap:  4194300k total,   460128k used,  3734172k free,  5664280k cached

#这是使用worker_processes  24;为CPU的2倍的效果,负载居中
[dev@ER-01 ~]$ top
top - 16:51:00 up 1057 days,  3:10,  3 users,  load average: 0.20, 0.07, 0.04
Tasks: 477 total,   1 running, 476 sleeping,   0 stopped,   0 zombie
Cpu0  :  9.7%us,  1.0%sy,  0.0%ni, 85.9%id,  2.3%wa,  0.0%hi,  1.0%si,  0.0%st
Cpu1  :  2.3%us,  0.7%sy,  0.0%ni, 96.0%id,  1.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  0.7%us,  0.3%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu4  :  0.0%us,  0.7%sy,  0.0%ni, 99.3%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu5  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu6  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu7  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu8  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu9  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu10 :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu11 :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16204548k total, 15286568k used,   917980k free,  4639556k buffers
Swap:  4194300k total,   542896k used,  3651404k free,  5459528k cached总之,在负载并不高的情况下,我并没有感觉到worker_cpu_affinity配置有什么效果。还不如不使用这个worker_cpu_affinity配置,而只使用worker_processes让系统自动分配。当然或者在负载高的时候会有意外的效果。记一下一些配置,可以留待后面使用。
#2核cpu,开启2个进程
worker_processes     2;
worker_cpu_affinity 01 10;

#2核cpu,开启4个进程,
worker_processes     4;
worker_cpu_affinity 01 10 01 10;

#4核cpu,开启2个进程,0101表示开启第一个和第三个内核,1010表示开启第二个和第四个内核;
worker_processes     2;
worker_cpu_affinity 0101 1010;

#4个cpu,开启4个进程
worker_processes     4;
worker_cpu_affinity 0001 0010 0100 1000;

#8核cpu,开启8个进程
worker_processes     8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

#16核cpu,开启16个进程
worker_processes 16;
worker_cpu_affinity 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000
 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;
请登录后评论
    当前暂无评价,快来发表您的观点吧...
    公告提示
    本站主要用于学习交流Laravel技术,分享Laravel优秀案例;同时非常欢迎大家投稿哦!如果有招聘信息也可以联系我我帮忙发布哦!