Syntax: | worker_cpu_affinity cpumask ...; |
---|---|
Default: | — |
Context: | main |
Binds worker processes to the sets of CPUs. Each CPU set is represented by a bitmask of allowed CPUs. There should be a separate set defined for each of the worker processes. By default, worker processes are not bound to any specific CPUs.
For example,
worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000;
binds each worker process to a separate CPU, while
worker_processes 2; worker_cpu_affinity 0101 1010;
binds the first worker process to CPU0/CPU2, and the second worker process to CPU1/CPU3. The second example is suitable for hyper-threading.
The directive is only available on FreeBSD and Linux.NGINX默认没有启用多CPU关联绑定,可以使用worker_cpu_affinity来充分利用CPU
[root@192-168-171-147 ~]# cat /proc/cpuinfo |grep processor
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
processor : 6
processor : 7
可以看到当前cpu为8核
[root@192-168-171-147 ~]# vim /usr/local/nginx/nginx.conf
添加:
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 000100000 00100000 01000000 10000000;
这里有几个核心就有几位,如8核使用00000000,4核使用0000,2术使用00.
核心位数中的1代表开启此核,0表示关闭.如开启8核中的第5核 00010000,第4核00001000,其它规则相同.
有人测试过,worker_processes最多开启8核,多开后对性能提升不大,设置过大反而对性能有所影响.
使用TOP查看CPU利用,比较平均.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24276 nobody 20 0 293m 271m 868 R 8.6 3.5 6:06.83 nginx
24275 nobody 20 0 291m 269m 868 S 8.0 3.4 5:57.41 nginx
24277 nobody 20 0 293m 271m 868 S 7.6 3.5 6:13.12 nginx
24278 nobody 20 0 292m 270m 868 S 7.6 3.4 6:07.87 nginx
24280 nobody 20 0 292m 270m 868 S 7.6 3.4 6:09.81 nginx
24279 nobody 20 0 293m 271m 868 S 7.3 3.5 6:23.12 nginx
24281 nobody 20 0 292m 270m 868 S 7.3 3.4 6:19.66 nginx
24282 nobody 20 0 291m 269m 868 S 7.3 3.4 5:44.48 nginx