利用TCMalloc优化Nginx的性能

TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。与标准的glibc库的Malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况下的性能,从而降低了系统的负载。下面简单介绍如何为Nginx添加TCMalloc库支持。


要安装TCMalloc库,需要安装libunwind(32位操作系统不需要安装)和google-perftools两个软件包,libunwind库为基于64位CPU和操作系统的程序提供了基本函数调用链和函数调用寄存器功能。下面介绍利用TCMalloc优化Nginx的具体操作过程。


Google-perftools的项目:http://code.google.com/p/google-perftools/


1.安装libunwind


[root@htuidc src]# wget http://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz
[root@htuidc libunwind-1.1]# CFLAGS=-fPIC ./configure
[root@htuidc libunwind-1.1]# make CFLAGS=-fPIC
2.安装gperftools
[root@htuidc src]# wget https://gperftools.googlecode.com/files/gperftools-2.1.tar.gz
[root@htuidc src]# tar zxvf gperftools-2.1.tar.gz
[root@htuidc src]# cd gperftools-2.1
[root@htuidc gperftools-2.1]# ./configure
[root@htuidc gperftools-2.1]# make && make install


至此,google-perftools安装完成。


3.重新编译Nginx


为了使Nginx支持google-perftools,需要在安装过程中添加“–with-google_perftools_module”选项重新编译Nginx。安装代码如下:


[root@htuidc nginx-1.5.4]# ./configure   --with-google_perftools_module  --with-http_stub_status_module  --prefix=/usr/local/nginx1.5
[root@htuidc nginx-1.5.4]# make && make install
到这里Nginx安装完成。
4.为google-perftools添加线程目录


创建一个线程目录,这里将文件放在/tmp/tcmalloc下。操作如下:


[root@htuidc nginx-1.5.4]# mkdir /tmp/tcmalloc
[root@htuidc nginx-1.5.4]# chmod 0777 /tmp/tcmalloc
5.修改Nginx主配置文件


修改nginx.conf文件,在pid这行的下面添加如下代码:


#pid        logs/nginx.pid;  
google_perftools_profiles /tmp/tcmalloc;


接着,重启Nginx即可完成google-perftools的加载。
[root@htuidc logs]# kill -Quit 16314
启动过程遇到两个错误:
1.[root@htuidc logs]# /usr/local/nginx1.5/sbin/nginx
/usr/local/nginx1.5/sbin/nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
查看libprofiler.so.0库存在:
[root@htuidc logs]# whereis libprofiler.so.0
libprofiler.so: /usr/local/lib/libprofiler.so /usr/local/lib/libprofiler.so.0
因为我的是64位系统,所以创建一个软连接到 /lib64/和/usr/lib64/下就可以了,命令
[root@htuidc conf]# ln -s /usr/local/lib/libprofiler.so.0 /lib/
[root@htuidc conf]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/
2.[root@htuidc conf]#  /usr/local/nginx1.5/sbin/nginx
/usr/local/nginx1.5/sbin/nginx: error while loading shared libraries: libunwind.so.8: cannot open shared object file: No such file or directory
解决办法同上:
[root@htuidc conf]# ln /usr/local/lib/libunwind.so.8 /lib64
[root@htuidc conf]# ln /usr/local/lib/libunwind.so.8 /usr/lib64
正常启动:
[root@htuidc lib64]#  /usr/local/nginx1.5/sbin/nginx
[root@htuidc lib64]# netstat -tnlpu|grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7154/nginx


验证google-perftools正常加载,可通过如下命令查看:
[root@htuidc lib64]# lsof -n|grep tcmalloc
nginx      7155  nobody    9w      REG              253,0        0     393594 /tmp/tcmalloc.7155
由于在Nginx配置文件中设置worker_processes的值为1,因此开启了1个Nginx线程,每个线程会有一行记录。每个线程文件后面的数字值就是启动的Nginx的pid值。


至此,利用TCMalloc优化Nginx的操作完成。


最后,为MySQL添加TCMalloc库,提高MySQL在高并发情况下的性能
修改MySQL启动脚本(根据你的MySQL安装位置而定):
vi /usr/local/mysql/bin/mysqld_safe
在# executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
保存后退出,然后重启MySQL服务器。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1:首先下载libunwind-1.1 # wget http://down.yunwei8.com/soft/linux/libunwind-1.1.tar.gz 然后安装 # tar zxvf libunwind-1.1.tar.gz # cd libunwind-1.1 # CFLAGS=-fPIC ./configure # make CFLAGS=-fPIC # make CFLAGS=-fPIC install 2:安装google-perftools: 首先下载gperftools-2.0 # wget http://down.yunwei8.com/soft/linux/gperftools-2.0.tar.gz 然后安装 # tar zxvf 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 # /sbin/ldconfig 3:重新编译nginx 不会安装的看这里 http://www.yunwei8.com/nginx124/ # cd /soft/nginx-1.2.4 # ./configure --user=www --group=www --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-google_perftools_module # make && make install 安装参数说明看这里:http://www.yunwei8.com/nginxcs 4:为tcmalloc添加目录,并且赋予适当权限 # mkdir -p /tmp/tcmalloc/ # chown -R www:www /tmp/tcmalloc/ 5:修改 nginx.conf ,令nginx可以 google-perftools实现加速 # vi /usr/local/server/nginx/conf/nginx.conf 修改前面几行为: user www www; worker_processes 8; error_log /web/logs/nginx_error.log crit; pid logs/nginx.pid; google_perftools_profiles /tmp/tcmalloc/; events{ use epoll; worker_connections 65535; } 6:测试和运行 # /usr/local/server/nginx/sbin/nginx -t 如果显示下面信息,即表示配置没问题 nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful 输入代码运行nginx服务 # /usr/local/server/nginx/sbin/nginx # ps aux|grep [n]ginx 如果显以类似下面的信息,即表示nginx已经启动 root 22900 0.0 0.1 43216 1576 ? Ss 08:23 0:00 nginx: master process /usr/local/server/nginx/sbin/nginx www 23019 0.0 2.6 68816 27160 ? S 08:48 0:00 nginx: worker process www 23020 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23021 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23022 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23023 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23024 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23025 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process www 23026 0.0 2.6 68816 26828 ? S 08:48 0:00 nginx: worker process 输入代码检测是否支持加速 # lsof -n | grep tcmalloc 如果显示类似下面的信息,即表示支持tcmalloc加速 (nginx八个线程都支持) nginx 31655 www 9w REG 8,1 0 479533 /tmp/tcmalloc/.31655 nginx 31656 www 11w REG 8,1 0 479534 /tmp/tcmalloc/.31656 nginx 31657 www 13w REG 8,1 0 479535 /tmp/tcmalloc/.31657 nginx 31658 www 15w REG 8,1 0 479536 /tmp/tcmalloc/.31658 nginx 31659 www 17w REG 8,1 0 479537 /tmp/tcmalloc/.31659 nginx 31660 www 19w REG 8,1 0 479540 /tmp/tcmalloc/.31660 nginx 31661 www 21w REG 8,1 0 479538 /tmp/tcmalloc/.31661 nginx 31662 www 23w REG 8,1 0 479539 /tmp/tcmalloc/.31662 优化完成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值