/ 中存储网

Linux系统中优化Nginx服务器CPU参数

2013-11-04 08:15:01 来源:IT技术网

Nginx性能优化CPU参数worker_cpu_affinity使用说明

官方说明:

worker_cpu_affinity

Syntax:worker_cpu_affinity cpumask [cpumask...]

Default:none

Linux only.

With this option you can bind the worker process to a CPU, it calls sched_setaffinity().

For example,

worker_processes     4;

worker_cpu_affinity 0001 0010 0100 1000;

Bind each worker process to one CPU only.

worker_processes     2;

worker_cpu_affinity 0101 1010;

Bind the first worker to CPU0/CPU2, bind the second worker to CPU1/CPU3. This is suitable for HTT.

一个服务器为Q9300单cpu,4核心,于是可以直接用官方的配置:worker_cpu_affinity 0001 0010 0100 1000;

效果如下,看起来每个cpu核心都比较平均:

top - 21:17:56 up 4 days, 7:41, 3 users, load average: 0.25, 0.44, 0.47

Tasks: 238 total, 1 running, 237 sleeping, 0 stopped, 0 zombie

Cpu0 : 0.3%us, 0.3%sy, 0.0%ni, 94.2%id, 5.2%wa, 0.0%hi, 0.0%si, 0.0%st

Cpu1 : 0.3%us, 0.0%sy, 0.0%ni, 96.2%id, 0.6%wa, 0.6%hi, 2.2%si, 0.0%st

Cpu2 : 0.3%us, 0.0%sy, 0.0%ni, 93.5%id, 5.5%wa, 0.0%hi, 0.6%si, 0.0%st

Cpu3 : 0.0%us, 0.0%sy, 0.0%ni, 96.5%id, 3.5%wa, 0.0%hi, 0.0%si, 0.0%st

Mem: 8119372k total, 3641940k used, 4477432k free, 504900k buffers

Swap: 8385888k total, 10664k used, 8375224k free, 2407512k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

30743 apache 20 0 6632 3800 812 S 0.3 0.0 0:00.51 nginx

30745 apache 20 0 7156 4384 812 S 0.3 0.1 0:00.85 nginx

以下文字来源于:http://www.chengyongxu.com/blog/nginx的worker_cpu_affinity详解/

配置文件中的worker_cpu_affinity可以用来绑定每个nginx进程所使用的CPU

官方的解释是:

#----------------------------引用文字-开始----------------------------

Syntax: worker_cpu_affinity cpumask [cpumask...]

Default: none

Linux only.

With this option you can bind the worker process to a CPU, it calls sched_setaffinity().

For example,

worker_processes 4;

worker_cpu_affinity 0001 0010 0100 1000;

Bind each worker process to one CPU only.

worker_processes 2;

worker_cpu_affinity 0101 1010;

Bind the first worker to CPU0/CPU2, bind the second worker to CPU1/CPU3. This is suitable for HTT.

#----------------------------引用文字-结束----------------------------

最关键的地方没说清楚,怎样来表示每个CPU?

经过漫天的搜索和多次尝试发现

详解

那么,16核的cpu每个进程分配到一个cpu就应该是

配置

测试一下,用别的服务器发出大量请求

ab -n 20000 -k http://您的ip或域名/index.php

在被测试的服务器上查看cpu状态

top

然后按1

16cpu信息

可以看到每个cpu都在工作了。