优化的内存访问 TCMalloc

TCMallocThread-Caching Malloc)是google-perftools工具中的一个,与标准的glibc库的malloc相 比,TCMalloc在内存的分配上效率和速度要高得多,可以提高Mysql服 务器在高并发情况下的性能,降低系统负载。

 

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

google-perftools 包括 TCMalloc heap-checker heap-profiler cpu-profiler 4 个组件,在只 用 TCMalloc 的场景下,可以不编译其他三个组件,使用tcmalloc_minimal 就足够。
下面介绍在Linux SUSE x86 上安装 TCMalloc 动态库的过程。
 
安装 TCMalloc
http://code.google.com/p/google-perftools/ 下载源码包,现在最新版本是1.4 。如果机器联网,直接:
wget  http://google-perftools.googlecode.com/files/google-perftools-1.4.tar.gz
tar zxvf google-perftools-1.4.tar.gz
cd  google-perftools-1.4
Mysql 服 务器只需要用SO 动态库就可以了,没有必须要把其他的文件( 头文件静态库文档等) 也安装到/usr/local/ 里。先安装到一个临时文件夹:
mkdir /tmp/tc
./configure --prefix=/tmp/tc --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal
加上后面的几个参数是指只生成tcmalloc_minimal
如果要生成包含所有组件的 tcmalloc ,可:
./configure --prefix=/tmp/tc
如果要将文件直接安装到文件,就不需要临时目录了,可:
./configure
使用./configure –h 可查看安装选项。
编译安装:
make && make install
ls -alt /tmp/tc/lib/*
使用了最小安装,拷贝tcmalloc_minimal 的动态库到系统库目录:
cp /tmp/tc/lib/ libtcmalloc_minimal.so.0.0.0 /usr/local/lib
建立软连接指向 tcmalloc
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.0.0 /usr/local/lib/libtcmalloc.so
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.0.0 /usr/local/lib/libtcmalloc.so.0
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.0.0 /usr/local/lib/libtcmalloc.so.0.0.0
rm -rf /tmp/tc
 
Mysql 加入动态库
修改 mysql 服 务的启动脚本mysqld_safe ,在“ # executing mysqld_safe ”行后添加行:
export LD_PRELOAD="/usr/local/lib/libtcmalloc.so"
目的是在启动 mysql 前, 加载 tcmalloc 动态库。
重启 Mysql 服 务:
/usr/local/mysql/bin/mysqladmin shutdown
/usr/local/mysql/bin/mysqld_safe –user=mysql &
 
验证
使用lsof 查看 mysql 进 程是否已经加载了 tcmalloc 库:
shell > lsof -n | grep tcmalloc
mysqld    32398      mysql mem       REG        8,3     668454    1477703 /usr/local/lib/libtcmalloc_minimal.so.0.0.0
恭喜,成功安装了 tcmalloc

转载于:https://my.oschina.net/YcOk14itqTKB/blog/2797

  • 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、付费专栏及课程。

余额充值