TCMalloc优化MySQL、Nginx内存管理

安装方法一、

1. libunwind安装

64位操作系统请先安装 libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。

libunwind-v1.2.1.tar.gz

tar zxvf libunwind-1.1.tar.gz

cd libunwind-1.1

./autogen.sh

./configure

make

make install


./autogen.sh:行7: autoreconf: 未找到命令

autoreconf: automake failed with exit status: 1

这时还要安装一下yum install autoconf automake gnome-common


2. 安装google-perftools

gperftools-2.6.1.tar.gz

tar -zvxf gperftools-2.6.1.tar.gz

cd gperftools-gperftools-2.6.1

./autogen.sh

./configure

make

make install


./libtool: line 1125: g++: command not found 

 make: *** [src/libtcmalloc_minimal_la-tcmalloc.lo] 错误 1

yum install gcc-c++


3. TCMalloc库加载到Linux系统中

echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf

ldconfig



安装方法二、

TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员。

与标准的glibc库的Malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况下的性能,从而降低了系统的负载。

下面简单介绍如何为Nginx添加TCMalloc库支持。

环境:CentOS7.2 nginx1.10.2 php5.6.26 mysql5.6.33

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

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

1.安装libunwind

wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz

cd libunwind-1.1

CFLAGS=-fPIC ./configure

make CFLAGS=-fPIC

make CFLAGS=-fPIC install

2.安装gperftools

wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz

tar zxvf gperftools-2.5.tar.gz

cd gperftools-2.5

./configure

###32位系统,不需要安装libunwind,但是一定要添加--enable-frame-pointers参数,如下: ./configure --enable-frame-pointers

make

make install

至此,google-perftools安装完成。

3.重新编译Nginx

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

./configure  ......      --with-google_perftools_module

若报错:

checking for Google perftools ... not found

checking for Google perftools in /usr/local/ ... not found

checking for Google perftools in /opt/local/ ... not found

./configure: error: the Google perftools module requires the Google perftools

library. You can either do not enable the module or install the library.

把gperftools安装路径lib下的所有文件全部复制到/usr/local/lib目录中即可解决

cp –R * /usr/local/lib

make 

make install(升级则不需要安装,替换原有的即可)

到这里Nginx安装完成。

4.为google-perftools添加线程目录

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

mkdir /tmp/tcmalloc

chmod 0777 /tmp/tcmalloc

5.修改Nginx主配置文件

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

#pid logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

接着,重启Nginx即可完成google-perftools的加载。

systemctl restart nginx.service

验证google-perftools正常加载,可通过如下命令查看:

 centos7默认没有lsof命令,需要安装相应的软件包 yum -y install lsof

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的操作完成。

 

 使用TCMalloc优化MySQL

为MySQL添加TCMalloc库,提高MySQL在高并发情况下的性能

修改MySQL启动脚本(根据你的MySQL安装位置而定):

vi /usr/local/mysql/bin/mysqld_safe

在# executing mysqld_safe的下一行,加上:

export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

保存后退出,然后重启MySQL服务器。

验证加载tcmalloc在MySQL中是否生效,如下:

lsof -n | grep tcmalloc


分享到:
关键词:Linux运维

网友留言(0 条)

发表评论