51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

通过TCMalloc来优化nginx

TCMalloc(Thread-Caching Malloc)是google开发的开源工具gperftools中的一个成员,tcmalloc比glibc中的malloc内存分配效率要快,tcmalloc特别对多线程做了优化,很大程度提高了服务器在高并发下的性能,从而降低了系统的负载。

tcmalloc属于gperftools,安装前提必须安装libunwind(32位系统不需要安装),libunwind库为基于64位cpu和操作系统的程序提供了基本函数调用链和函数调用寄存器功能。

一、下载相关软件包

官方下载地址:

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

https://download-mirror.savannah.gnu.org/releases/libunwind/libunwind-1.2.tar.gz

本地下载地址:

https://down.whsir.com/downloads/gperftools-2.7.tar.gz

https://down.whsir.com/downloads/libunwind-1.2.tar.gz
cd /usr/local/src/ wget http://nginx.org/download/nginx-1.14.0.tar.gz wget https://down.whsir.com/downloads/gperftools-2.7.tar.gz wget https://down.whsir.com/downloads/libunwind-1.2.tar.gz

|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 | cd /usr/local/src/ wget http://nginx.org/download/nginx-1.14.0.tar.gz wget https://down.whsir.com/downloads/gperftools-2.7.tar.gz wget https://down.whsir.com/downloads/libunwind-1.2.tar.gz |

二、安装所需依赖
yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel

|---|-------------------------------------------------------------------------------| | 1 | yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel |

三、创建nginx启动用户
useradd -s /bin/false -M www

|---|------------------------------| | 1 | useradd -s /bin/false -M www |

四、解压编译安装libunwind和gperftools
tar zxf libunwind-1.2.tar.gz cd libunwind-1.2 CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install cd .. tar zxf gperftools-2.7.tar.gz cd gperftools-2.7 ./configure make make install echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig

|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 | tar zxf libunwind-1.2.tar.gz cd libunwind-1.2 CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install cd .. tar zxf gperftools-2.7.tar.gz cd gperftools-2.7 ./configure make make install echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig |

五、编译nginx

编译时要添加--with-google_perftools_module参数,我这里使用的全新nginx编译,如果已有编译好的nginx,只需要执行前面第二步后单独添加一个--with-google_perftools_module参数重新编译即可
cd .. tar zxf nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-google_perftools_module make make install

|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 | cd .. tar zxf nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-google_perftools_module make make install |

设置个软连接
ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/

|---|-----------------------------------------------------| | 1 | ln -sv /usr/local/nginx/sbin/nginx /usr/local/sbin/ |

设置systemctl
vi /usr/lib/systemd/system/nginx.service

|---|------------------------------------------| | 1 | vi /usr/lib/systemd/system/nginx.service |


[Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target

|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 | [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target |

六、为gperftools创建一个线程目录
mkdir /tmp/tcmalloc chmod 777 /tmp/tcmalloc/

|-----|----------------------------------------------| | 1 2 | mkdir /tmp/tcmalloc chmod 777 /tmp/tcmalloc/ |

七、配置nginx.conf

在#pid logs/nginx.pid;下面增加一行
google_perftools_profiles /tmp/tcmalloc;

|---|------------------------------------------| | 1 | google_perftools_profiles /tmp/tcmalloc; |

八、最后启动nginx并验证
nginx -t systemctl start nginx systemctl enable nginx

|-------|-------------------------------------------------------| | 1 2 3 | nginx -t systemctl start nginx systemctl enable nginx |


lsof -n | grep tcmalloc nginx 16534 www 10w REG 0,37 0 80796 /tmp/tcmalloc.16534

|-----|-----------------------------------------------------------------------------------| | 1 2 | lsof -n | grep tcmalloc nginx 16534 www 10w REG 0,37 0 80796 /tmp/tcmalloc.16534 |

这里看到有多少条记录是根据nginx的worker_processes线程数来的

赞(0)
未经允许不得转载:工具盒子 » 通过TCMalloc来优化nginx