TCMalloc优化Nginx性能
TCMalloc全称是Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。与标准的glibc库的Malloc相比,TCMalloc库在内存分配效率和速度上要高很多,提高了服务器在高并发情况下的性能,从而降低了系统负载。要安装TCMalloc库,需要安装libunwind(32位操作系统不需要安装)和google-perftools两个软件包。
1,安装libunwind库
wgethttp://download.savannah.gnu.org/releases/libunwind下载相应的libunwind版本。
#tar zxvf libunwind-0.99-alpha.tar.gz
#cd libunwind-0.99-alpha/
#CFLAGS=-fPIC./configure
#make CFLAGS=-fPIC
#make CFAGS=-fPICinstall
2,安装google=perftools
wget http://google-perftools.googlecode.com下载对应版本。
#tar zxvf google-perftools-1.8.tar.gz
#cd google-perftools-1.8/
#./configure
#make && make install
#echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# ldconfig
至此,oogle=perftools安装完成。
3,重新编译nginx
为了是nginx支持oogle=perftools,需要安装过程中添加“-with-google_perftools_module”选项重新编译nginx。
# ./configure --with-google_perftools_module--with-http_stub_status_module--prefix=/opt/nginx
#make
#make install
到这里nginx安装完成。
4,为google-perftools添加线程目录
#mkdir/tmp/tcmalloc
#chmod 0777 /tmp/tcmalloc
5,修改nginx主配置文件
修改nginx.conf文件,在pid行下面添加如下代码:
#pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
重启nginx即可加载google_perftools 。
6,验证运行状态
#lsof-n | grep tcmalloc
nginx 2395nobody 9wREG 8,8 0159940/tmp/tcmalloc.2395
nginx 2396nobody 11wREG 8,8 0159943/tmp/tcmalloc.2396
nginx 2397nobody 13wREG 8,8 0159941/tmp/tcmalloc.2397
nginx 2398nobody 15wREG 8,8 0159942/tmp/tcmalloc.2398
由于nginx配置文件设置worker_processes的值为4,因此开启了4个线程。
至此,利用TCMalloc优化nginx操作完成。
页:
[1]