利用 gperftools 對nginx 與 mysql 進行 記憶體管理 效能優化 降低負載.
Gperftools 是由谷歌開發。官方對gperftools 的介紹為:
These tools are for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.
- 首先下載軟體安裝包:
由於我們的系統為CentOS 5.8 x64的系統, 64位系統需要先安裝 libunwind 支援庫。
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
gperftools 2.0版本
wget http://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
- 安裝軟體包
tar zxvf libunwind-1.1.tar.gz
cd libunwind-1.1
./configure
make && make install
tar zxvf gperftools-2.0.tar.gz
cd gperftools-2.0
./configure --enable-frame-pointers
make && make install
增加 gperftools 執行緒目錄
mkdir /tmp/tcmalloc
設定許可權
chmod 777 /tmp/tcmalloc
配置lib 庫檔案目錄
vim /etc/ld.so.conf
增加
/usr/local/lib
/sbin/ldconfig 使之生效
- 重新編譯nginx 使其支援gperftools
tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
./configure --user=upload --group=upload --prefix=/opt/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-google_perftools_module
make && make install
安裝完畢以後, 修改 nginx 配置檔案 nginx.conf
vim /opt/local/nginx/con/nginx.conf
在pid 下面增加
#gperftools
google_perftools_profiles /tmp/tcmalloc;
然後儲存,啟動nginx
/opt/local/nginx/sbin/nginx -t 檢視配置是否正常
lsof -n | grep tcmalloc 檢視是否執行
nginx 18454 upload 10w REG 8,2 0 10780681 /tmp/tcmalloc.18454
nginx 18455 upload 12w REG 8,2 0 10780682 /tmp/tcmalloc.18455
---------------------------------------------------------------------------------
優化Mysql 則不需要重新編譯mysql 只需要找到 mysqld_safe 這個檔案
在# executing mysqld_safe 下面加上
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
然後重啟mysql 就可以啦.