|
《高性能 Linux 服务器构建实战:运维监控、性能调优与集群应用》 学习笔记
一 优化原理
1 在编译Nginx 时,默认以debug模式进行,会比非debug 模式下多出跟踪和ASSERT 之类的信息,
取消debug编译,Nginx会小很多。
2 编译Nginx时,针对服务器使用的cpu 进行编译,可以提升性能。
3 TCMalloc(Thread-Caching Malloc) 比标准的glibc 库的malloc 内存分配效率和速度上要高很多,可以提
高服务器在高并发情况下的性能。
4 如果服务器是单纯运行Nginx 的服务器,可以针对Nginx 在内核方面进行优化。
二 安装
1 安装TCMalloc
(1)如果是64位系统,需要安装 libunwind 库,32位的跳过。点此下载
tar -zxvf libunwind-1.1.tar.gz
cd libunwind-1.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
(2) 安装 google-perftools
TCMalloc 是google-perftools 的一部分,点此下载
tar gperftools-2.0.tar.gz
cd gperftools-2.0
./configure
make
make install
echo "/usr/local/lib" >> /etc/ld.so.conf.d/usr_local_lib.conf
(3) 安装Nginx
点此下载
1 tar -zxvf nginx-1.2.7.tar.gz
取消debug 模式
2 cd nginx-1.2.7
vim /auto/cc/gcc
找到
#debug
CFLAGS="$CFLANGS -g "
注释
3 获取cpu 型号
cat /proc/cpuinfo|grep "model name"
4 mkdir -p /opt/nginx
./configure --with-google_perftools_module --with-http_stub_status_module --prefix=/opt/nginx \
--with-cc-opt='-O3' --with-cpu-opt="刚刚获取的cpu型号"
cpu 有效型号:pentium, pentiumpro, pentium3,pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
make && make install
5 为 google-perftools 添加线程目录
mkdir /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
6 修改Nginx 主要配置文件
cd /opt/nginx/conf
vim nginx.conf
在 pid 后面加上一行,如下面
#pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
7 打开nginx 验证运行状态
/opt/nginx/sbin/nginx
lsof -n |grep tcmalloc 判断nginx 是否使用了 tcmalloc
|
|
|