TCMalloc优化MySQL、Nginx、Redis内存管理
TCMalloc(Thread-Caching Malloc)与标准glibc库的malloc实现一样的功能,但是TCMalloc在效率和速度效率都比标准malloc高很多。TCMalloc是google-perftools工具中的一个(gperftools四个工具分别是:TCMalloc、heap-checker、heap-profiler和cpu-profiler),这个工具是开源的,以源码形式发布。如果觉得自己维护一个内存分配器麻烦的话,可以考虑将TCMalloc静态库连接到你的程序中。使用的时候和glibc中的malloc调用方式一模一样。你需要做的只是把TCMalloc的动态库或者静态库连接进你的程序中,你就可以获得一个高效,快速,安全的内存分配器。
与标准的glibc库的malloc相比,TCMalloc在内存的分配效率和速度要高,可以在高并发的情况下很好的控制内存的使用,提高服务器的性能,降低负载。TCMalloc的实现原理和测试报告请见一篇文章:《TCMalloc:线程缓存的Malloc》
tcmalloc作为可选项已经添加到《lnmp最新源码一键安装包》
安装libunwind库:
如果系统是64位的需要先安装libunwind库,32位系统则不需要安装。
libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz tar zxf libunwind-1.1.tar.gz cd libunwind-1.1 CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install cd ../
gperftools的安装:
wget http://gperftools.googlecode.com/files/gperftools-2.1.tar.gz tar xzf gperftools-2.1.tar.gz cd gperftools-2.1
可以加入参数只编译tcmalloc(./configure –enable-minimal、–disable-cpu-profiler、–disable-heap-profiler、–disable-heap-checker、–disable-debugalloc),64位操作系统不安装libunwind也不会报错,注意生成的库文件是libtcmalloc_minimal.*
64位操作系统,如下
./configure
32位系统,不需要安装libunwind,但是一定要添加–enable-frame-pointers参数,如下
./configure --enable-frame-pointers
make && make install
编译安装后,输入以下命令:
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf ldconfig #必须执行
使用TCMalloc优化MySQL
MySQL 5.1静态编译方法,./configure预编译时假设下面参数
--with-mysqld-ldflags=-ltcmalloc
MySQL 5.5静态编译方法,cmake预编译时加上下面参数
-DCMAKE_EXE_LINKER_FLAGS="-ltcmalloc" -DWITH_SAFEMALLOC=OFF
采用动态加载
sed -i 's@executing mysqld_safe@executing mysqld_safe\nexport LD_PRELOAD=/usr/local/lib/libtcmalloc.so@' /usr/local/mysql/bin/mysqld_safe service mysqld restart
验证加载tcmalloc在MySQL中是否生效,如下:
lsof -n | grep tcmalloc
使用TCMalloc优化Nginx
为了使nginx支持google-perftools,需要在安装过程中添加”–with-google_perftools_module”选项重新编译nginx。安装如下:
cd lnmp/src/nginx-1.4.2 make clean ./configure --prefix=/usr/local/nginx --user=www --group=www \ --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module \ --with-http_gzip_static_module --with-google_perftools_module make && make install
为添加线程目录:
mkdir /tmp/tcmalloc chown -R www.www /tmp/tcmalloc vi /usr/local/nginx/conf/nginx.conf #pid下一行添加 google_perftools_profiles /tmp/tcmalloc;
验证tcmalloc是否在Nginx中生效
lsof -n | grep tcmalloc
每个线程(work_processes的值)会有一行记录。每个线程文件后面的数字值就是启动的nginx的pid值。
使用TCMalloc优化redis
注意:redis-2.4以上自带jemalloc,你不需要加任何参数,通过zmalloc.c源码中我们可以看到,Redis在编译时,会先判断是否使用tcmalloc,如果是,会用tcmalloc对应的函数替换掉标准的libc中的函数实现。其次会判断jemalloc是否使得,最后如果都没有使用才会用标准的libc中的内存管理函数。所以用tcmalloc优化请谨慎使用,这两着分配器碎片率相差不大,建议用自带jemalloc
cd lnmp/src/redis-2.6.16 make USE_TCMALLOC=yes FORCE_LIBC_MALLOC=yes /bin/cp src/{redis-benchmark,redis-check-aof,redis-check-dump,redis-cli,redis-sentinel,redis-server} /usr/local/redis/bin>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Nginx目录别名(Alias)支持PHP的配置
转载自:http://wangyan.org/blog/nginx-alias-php.html
需求:通过 example.com 访问 /var/data/www,但通过 example.com/pa 访问的却是 /var/data/phpmyadmin,即保护phpmyadmin不暴露在www目录下。
一、方法一:(不推荐)
简介:这是网上普遍采用的 Rewrite 方式。
缺陷:简单的php程序还能应付,复杂一点的程序就"No input file specified"
server {
listen 80;
server_name example.com;
root /var/data/www;
index index.html index.php;
location /pa {
alias /var/data/phpmyadmin;
index index.html index.php;
}
location ~ /pa/.+\.php$ {
rewrite /pa/(.+\.php) /$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/data/phpmyadmin/$fastcgi_script_name;
include fastcgi_params;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
二、方法二:(推荐)
简介:完美实现,无副作用。
特点:使用了一个叫"$valid_fastcgi_script_name"的变量
server {
listen 80;
server_name example.com;
root /var/data/www;
index index.html index.php;
location /pa {
alias /var/data/phpmyadmin;
index index.html index.php;
}
location ~ /pa/.+\.php.*$ {
if ($fastcgi_script_name ~ /pa/(.+\.php.*)$) {
set $valid_fastcgi_script_name $1;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/data/phpmyadmin/$valid_fastcgi_script_name;
include fastcgi_params;
}
location ~ .+\.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
二、方法三:
简介:在 zhigang.net 上看到的创意方法,即一个站加两个server字段,然后通过反代的方式实现。
特定:方法有创意,稍微麻烦点。
OpenResty 一个通过扩展 Nginx 实现的强大的 web 应用服务器
OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器,它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。
OpenResty 通过汇聚各种设计精良的 Nginx 模块,
从而将 Nginx 有效的变成一个强大的 Web 应用服务器,
这样, Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种C以及Lua 模块,
快速构造出足以胜任 10K+ 并发连接响应的超高性能Web 应用系统.
OpenResty 的目标是让你的Web服务直接跑在 Nginx 服务内部,
充分利用 Nginx 的非阻塞 I/O 模型,
不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如
MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Bash Shell PS1: 自定义你的linux提示符十例
1. 提示符中显示用户名,主机名和当前工作目录
这个例子中PS1将在提示符中显示以下三种信息
- \u – 用户名
- \h – 主机名
- \w – 当前工作目录的绝对路径
1 -bash-3.2$ export PS1="\u@\h \w> "
1 ramesh@dev-db ~> cd /etc/mail
1 ramesh@dev-db /etc/mail>
2. 提示符中显示当前时间
在PS1环境变量中,你可以使用$(linux_command)这种格式直接执行任何linux指令,在下面的例子中,通过执行指令$(date)在提示符中显示当前时间
1 ramesh@dev-db ~> export PS1="\u@\h [\$(date +%k:%M:%S)]> "
1 ramesh@dev-db [11:09:56]>
你也可以使用\t以hh:mm:ss格式显示当前的时间
1 ramesh@dev-db ~> export PS1="\u@\h [\t]> "
1 ramesh@dev-db [12:42:55]>
你也可以像下面的示例一样使用\@ 以12小时am/pm格式显示当前时间
1 ramesh@dev-db ~> export PS1="[\@] \u@\h> "
1 [04:12 PM] ramesh@dev-db>
3. 在提示符中显示任一linux指令输出
你可以在提示符中显示任何linux指令的输出.下面的例子在提示符中显示三项用|(pipe)分割的信息
- \!: 显示历史指令记录数
- \h: 主机名
- $kernel_version: uname -r 指令的输出
- \$?: 上一条指令的状态
1 ramesh@dev-db ~> kernel_version=$(uname -r)
1 ramesh@dev-db ~> export PS1="\!|\h|$kernel_version|\$?> "
1 473|dev-db|2.6.25-14.fc9.i686|0>
4. 更改提示符的前景色
Display prompt in blue color, along with username, host and current directory information
使用蓝色显示提示符,包括用户名,主机名和当前工作目录信息
$ export PS1="\e[0;34m\u@\h \w> \e[m" [Note: This is for light blue prompt] $ export PS1="\e[1;34m\u@\h \w> \e[m" [Note: This is for dark blue prompt]
- \e[ - 提示符颜色的开始处
- x;ym - 颜色值., 使用下面提到的数值
- \e[m - 提示符颜色的结束处
Color Code Table:
Black 0;30 Blue 0;34 Green 0;32 Cyan 0;36 Red 0;31 Purple 0;35 Brown 0;33 [Note: Replace 0 with 1 for dark color]
将下面指令添加到.bash_profile或者.bashrc中,使其永久生效
1 STARTCOLOR='\e[0;34m';
1 ENDCOLOR="\e[0m"
1 export PS1="$STARTCOLOR\u@\h \w> $ENDCOLOR"
5. 更改提示符背景色
Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.
想下面的例子一样在PS1中指定\e[{code}m 的值以更改背景色
[Note: This is for Light Gray background]
1 $ export PS1="\e[47m\u@\h \w> \e[m"
组合背景色和前景色
[Note: This is for Light Blue foreground and Light Gray background]
1 export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"
将下面的代码添加到.bash_profile或者.bashrc中使背景色和前景色永久生效.
STARTFGCOLOR='\e[0;34m'; STARTBGCOLOR="\e[47m" ENDCOLOR="\e[0m" export PS1="$STARTFGCOLOR$STARTBGCOLOR\u@\h \w> $ENDCOLOR"
从下面的背景色中选择最符合你口味儿的颜色.
- \e[40m
- \e[41m
- \e[42m
- \e[43m
- \e[44m
- \e[45m
- \e[46m
- \e[47m
6. 在提示符中显示混合色
你也可以在同一提示符中显示混合色,将下面的函数添加到.bash_profile中
function prompt { local BLUE="\[\033[0;34m\]" local DARK_BLUE="\[\033[1;34m\]" local RED="\[\033[0;31m\]" local DARK_RED="\[\033[1;31m\]" local NO_COLOR="\[\033[0m\]" case $TERM in xterm*|rxvt*) TITLEBAR='\[\033]0;\u@\h:\w\007\]' ;; *) TITLEBAR="" ;; esac PS1="\u@\h [\t]> " PS1="${TITLEBAR}\ $BLUE\u@\h $RED[\t]>$NO_COLOR " PS2='continue-> ' PS4='$0.$LINENO+ ' }
你可以重新登陆使修改生效,或者像下面一样source .bash_profile
$. ./.bash_profile $ prompt ramesh@dev-db [13:02:13]>
7. 用tput更改提示符颜色
你也可以像下面一样在PS1中使用tput更改颜色
$ export PS1="\[$(tput bold)$(tput setb 4)$(tput setaf 7)\]\u@\h:\w $ \[$(tput sgr0)\]"
tput Color Capabilities:
- tput setab [1-7] - Set a background color using ANSI escape
- tput setb [1-7] - Set a background color
- tput setaf [1-7] - Set a foreground color using ANSI escape
- tput setf [1-7] - Set a foreground color
tput Text Mode Capabilities:
- tput bold - Set bold mode
- tput dim - turn on half-bright mode
- tput smul - begin underline mode
- tput rmul - exit underline mode
- tput rev - Turn on reverse mode
- tput smso - Enter standout mode (bold on rxvt)
- tput rmso - Exit standout mode
- tput sgr0 - Turn off all attributes
Color Code for tput:
- 0 - Black
- 1 - Red
- 2 - Green
- 3 - Yellow
- 4 - Blue
- 5 - Magenta
- 6 - Cyan
- 7 - White
8. 使用下面的PS1变量可用代码制作个性化的提示符
- \a an ASCII bell character (07)
- \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
- \D{format} - the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
- \e an ASCII escape character (033)
- \h the hostname up to the first part
- \H the hostname
- \j the number of jobs currently managed by the shell
- \l the basename of the shell's terminal device name
- \n newline
- \r carriage return
- \s the name of the shell, the basename of $0 (the portion following the final slash)
- \t the current time in 24-hour HH:MM:SS format
- \T the current time in 12-hour HH:MM:SS format
- \@ the current time in 12-hour am/pm format
- \A the current time in 24-hour HH:MM format
- \u the username of the current user
- \v the version of bash (e.g., 2.00)
- \V the release of bash, version + patch level (e.g., 2.00.0)
- \w the current working directory, with $HOME abbreviated with a tilde
- \W the basename of the current working directory, with $HOME abbreviated with a tilde
- \! the history number of this command
- \# the command number of this command
- \$ if the effective UID is 0, a #, otherwise a $
- \nnn the character corresponding to the octal number nnn
- \\ a backslash
- \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
- \] end a sequence of non-printing character
9. 在PS1变量中使用bash shell函数
你也可以在PS1中像下面一样调用一个bash shell函数
ramesh@dev-db ~> function httpdcount { > ps aux | grep httpd | grep -v grep | wc -l > } ramesh@dev-db ~> export PS1="\u@\h [`httpdcount`]> " ramesh@dev-db [12]> [Note: This displays the total number of running httpd processes]
你可以将下面的代码添加到.bash_profile或者.bashrc中使其永久生效
function httpdcount { ps aux | grep httpd | grep -v grep | wc -l } export PS1='\u@\h [`httpdcount`]> '
10. 在PS1变量中使用shell脚本
你也可以在PS1中调用一个shell脚本,在下面的例子中,~/bin/totalfilesize.sh这个脚本被PS1变量调用,以显示当前目录总的文件数,
11. IP地址示例
1 ramesh@dev-db ~> cat ~/bin/totalfilesize.sh
1 for filesize in $(ls -l . | grep "^-" | awk '{print $5}')
1 do
1 let totalsize=$totalsize+$filesize
1 done
1 echo -n "$totalsize"
1 ramesh@dev-db ~> export PATH=$PATH:~/bin
1 ramesh@dev-db ~> export PS1="\u@\h [\$(totalfilesize.sh) bytes]> "
1 ramesh@dev-db [534 bytes]> cd /etc/mail
1 ramesh@dev-db [167997 bytes]>
my_ip=$(/sbin/ip addr show dev eth1 | perl -ne '/inet ([\d.]+)/ && print $1')
export PROMPT_COMMAND='echo -ne "\033]0;$my_ip\007"'
export PS1="\[\e[1;36m\][\H]\[\e[31;1m\]\u\[\e[0m\]@\[\e[32;1m\]$my_ip\[\e[0m\]:\[\e[35;1m\]\w\[\e[0m\]\\$ "
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>