Nginx 配置 CPU 亲和性
Ningx使用worker_cpu_affinity绑定CPU,提高Nginx工作效率。
# 环境说明
- CentOS 7 4c4g
# 默认的配置
修改 /etc/nginx/nginx.conf,默认的 CPU 参数为:
worker_processes auto;
1
# CPU 绑定关系
启动服务后,Nginx 进程与 CPU 亲和关系:
$ systemctl start nginx
$ ps -ef|grep nginx
root 1889 1 0 10:02 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 1890 1889 0 10:02 ? 00:00:00 nginx: worker process
nginx 1891 1889 0 10:02 ? 00:00:00 nginx: worker process
nginx 1892 1889 0 10:02 ? 00:00:00 nginx: worker process
nginx 1893 1889 0 10:02 ? 00:00:00 nginx: worker process
root 1896 1745 0 10:02 pts/0 00:00:00 grep --color=auto nginx
$ taskset -c -p 1890
pid 1890's current affinity list: 0-2
$ taskset -c -p 1891
pid 1891's current affinity list: 0-2
$ taskset -c -p 1892
pid 1892's current affinity list: 0-2
$ taskset -c -p 1893
pid 1893's current affinity list: 0-2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
通过 taskset 命令,我们可以看到每一个 nginx 子进程都 0-2 的 CPU 和绑定。
# 配置 worker_cpu_affinity
修改 worker_processes auto 为:
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
1
2
2
说明:
worker_processes分配给 Nginx 的 CPU 数量worker_cpu_affinity二进制,0001表示启用第一个 CPU,依次类推
# CPU 绑定关系
Nginx 进程与 CPU 亲和关系:
$ ps -ef|grep nginx
root 1930 1 0 10:03 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 1931 1930 0 10:03 ? 00:00:00 nginx: worker process
nginx 1932 1930 0 10:03 ? 00:00:00 nginx: worker process
nginx 1933 1930 0 10:03 ? 00:00:00 nginx: worker process
nginx 1934 1930 0 10:03 ? 00:00:00 nginx: worker process
root 1936 1745 0 10:03 pts/0 00:00:00 grep --color=auto nginx
$ taskset -c -p 1931
pid 1931's current affinity list: 0
$ taskset -c -p 1932
pid 1932's current affinity list: 1
$ taskset -c -p 1933
pid 1933's current affinity list: 2
$ taskset -c -p 1934
pid 1934's current affinity list: 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
通过 taskset 命令,我们可以看到每一个 nginx 子进程都和指定的 CPU 核绑定。
# 压测验证
用 wrk 工具,进行性能测试,在服务器上执行 top,然后按 1,可以看到 4 个 cpu 内核的利用率差不多,证明 nginx 已经成功利用了多核 cpu。
编辑 (opens new window)
上次更新: 2024/04/19, 08:52:45
- 01
- idea 热部署插件 JRebel 安装及破解,不生效问题解决04-10
- 02
- spark中代码的执行位置(Driver or Executer)12-12
- 03
- 大数据技术之 SparkStreaming12-12