一、TCMalloc介绍
1、TCMalloc介绍
TCMalloc的全称是Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。与标准的glibc库的Malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程序上提高了服务器在高并发情况下的性能,从而降低系统的负载。
如何为Nginx添加TCMalloc库支持,要安装TCMalloc库,需要安装libunwind和gperftools两个软件包,libunwind库为基于64为CPU操作系统的程序提供了基本函数调用链和函数调用函数寄存器功能,32位操作系统不需要安装。
2、文件介绍
libunwind
libunwind官网:http://www.nongnu.org/libunwind/
libunwind下载:
http://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz
gperftools
gperftools官网:http://code.google.com/p/gperftools/downloads/list
gperftools下载:http://down.shshenchu.com/gperftools-2.1.tar.gz
https://codeload.github.com/gperftools/gperftools/zip/master
二、安装配置
1.安装libunwind库
tar -zxvflibunwind-1.1.tar.gz //解压libunwind
cdlibunwind-1.1
CFLAGS=-fPIC ./configure
makeCFLAGS=-fPIC
makeCFLAGS=-fPIC install
2.安装gperftools
tar -zxvfgperftools-2.1.tar.gz //解压gperftools
cdgperftools-2.1
./configure
make && makeinstall
echo "/usr/local/lib">>/etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
支持gperftools安装完成
3.编译Nginx
编译的时候添加–with-google_perftools_module
4.为gperftools添加线程目录
创建一个线程目录
mkdir /tmp/tcmalloc
chmod 0777/tmp/tcmalloc
5.修改nginx主配置文件,在pid这行的下面添加以下代码
google_perftools_profiles/tmp/tcmalloc;
6.验证运行状态
为了验证gperftools已经正常加载,可以如下命令查看:
[root@localhostpublic_html]# lsof -n | greptcmalloc
nginx 32572 www 14w REG 8,1 0 27102/tmp/tcmalloc.32572
nginx 32573 www 18w REG 8,1 0 27106/tmp/tcmalloc.32573
由于在Nginx配置文件中设置worker_processes的值为2,因此就开启了2个Nginx的线程,每个线程都会有一行记录。每个线程文件后面的数字值就是启动Nginx的pid值。
至此,利用TCMalloc优化Nginx的操作完成。