设为首页收藏本站

追梦Linux

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1063|回复: 0

之前用的Linux内核优化脚本

[复制链接]

482

主题

485

帖子

16万

积分

CEO

Rank: 9Rank: 9Rank: 9

积分
169037

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

QQ
发表于 2018-6-6 15:43:11 | 显示全部楼层 |阅读模式
[Bash shell] 纯文本查看 复制代码
#!/bin/bash

# TODO 调优sysctl.net.tcp_*, udp_*配置

#===================================================#
# XXX 非常重要, 需要确保 nf_conntrack_max 生效:
# 1.modprobe ip_conntrack
# 2.lsmod |grep conn -- if you see entries it means modules have been loaded correctly
# 3.sysctl -w -- to write the changes you made under /etc/sysctl.conf
# 4.sysctl -p -- to view the changes and see if it was actually loaded.

#sudo sysctl net.ipv4.ip_conntrack_max=1020000
#sudo sysctl net.ipv4.netfilter.ip_conntrack_max=1020000
sudo bash -c "echo 1020000 > /proc/sys/net/nf_conntrack_max"
#===================================================#

#
sudo sysctl kernel.shmmax=2000000000
sudo sysctl fs.file-max=1020000
# 修改 /etc/security/limits.conf 
sudo sysctl -w net.ipv4.ip_local_port_range="10000 39999"  # 生成环境
#sudo sysctl -w net.ipv4.ip_local_port_range="10000 64000" # 性能测试客户端
sudo sysctl -w net.ipv4.inet_peer_threshold=1020000
sudo echo "net.core.somaxconn = 2048  
net.core.rmem_default = 262144  
net.core.wmem_default = 262144  
net.core.rmem_max = 16777216  
net.core.wmem_max = 16777216  
net.ipv4.tcp_rmem = 4096 4096 16777216  
net.ipv4.tcp_wmem = 4096 4096 16777216  
net.ipv4.tcp_mem = 786432 2097152 3145728  
net.ipv4.tcp_max_syn_backlog = 16384  
net.core.netdev_max_backlog = 20000  
net.ipv4.tcp_fin_timeout = 15  
net.ipv4.tcp_max_syn_backlog = 16384  
net.ipv4.tcp_tw_reuse = 1  
net.ipv4.tcp_tw_recycle = 1  
net.ipv4.tcp_max_orphans = 131072" > /etc/sysctl.conf
sudo sysctl -p

#[url=http://blog.cloudflare.com/optimizing-the-linux-stack-for-mobile-web-per]http://blog.cloudflare.com/optim ... -for-mobile-web-per[/url]
#[url=http://blog.netzhou.net/?p=272]http://blog.netzhou.net/?p=272[/url]
sudo sysctl -w net.ipv4.tcp_no_metrics_save=1
sudo sysctl -w net.ipv4.tcp_slow_start_after_idle=0
ip route | while read p; do ip route change $p initcwnd 10 initrwnd 10; done



[AppleScript] 纯文本查看 复制代码
#vim
sed -i "8 s/^/alias vi='vim'/" /root/.bashrc
echo 'syntax on' > /root/.vimrc

#zh_cn
sed -i -e 's/^LANG=.*/LANG="zh_CN.UTF-8"/' /etc/sysconfig/i18n

#优化Linux系统文件描述符
echo "* soft nofile 65535
* hard nofile 65535" >> /etc/security/limits.conf

#设置ulimit
ulimit -HSn 65536
#echo "ulimit -HSn 65536 ">> /etc/rc.local
sed -i '$i\ulimit -HSn 65536' /etc/rc.local

##tunoff services
##--------------------------------------------------------------------------------
#cat << EOF
#+--------------------------------------------------------------+
#| === Welcome to Tunoff services === |
#+--------------------------------------------------------------+
#EOF
##---------------------------------------------------------------------------------
#for i in `ls /etc/rc3.d/S*`
#do
#CURSRV=`echo $i|cut -c 15-`
#
#echo $CURSRV
#case $CURSRV in
# crond | network | sshd | syslog )
##这个启动的系统服务根据具体的应用情况设置,其中crond,network、sshd、syslog是必须要启动的系统服务!
#echo "Base services, Skip!"
#;;
#*)
#echo "change $CURSRV to off"
#chkconfig --level 235 $CURSRV off
#service $CURSRV stop
#;;
#esac
#done

#优化Linux内核参数
echo "net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 134217728
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.ip_local_port_range = 1024 65534
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_fin_timeout = 3
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save = 1
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_keepalive_time = 30
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
vm.swappiness = 6" > /etc/sysctl.conf
/sbin/sysctl -p
echo "sysctl set OK!!"

#close ctrl+alt+del
#sed -i "s/ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/#ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now/" /etc/inittab
sed -i 's/^/#/g' /etc/init/control-alt-delete.conf

#禁用暂时不使用的账号。
#user=`cat /etc/passwd |awk -F":" '{print $1,$7}'|grep -v nologin| grep -v hadoop | grep -v work | grep bash | grep -v root | awk '{print $1}'`
#for i in $user
#do 
#passwd -l $i 
#done


[Bash shell] 纯文本查看 复制代码
set_affinity()
{
    printf "%s mask=%X for /proc/irq/%d/smp_affinity\n" $DEV $MASK $IRQ
    printf "%X" $MASK > /proc/irq/$IRQ/smp_affinity
}

set_intel_broadcom()
{
        #  Intel##########
        #  Assuming a device with two RX and TX queues.
        #  This script will assign: 
        #
        #        eth0-rx-0  CPU0
        #        eth0-rx-1  CPU1
        #        eth0-tx-0  CPU0
        #        eth0-tx-1  CPU1
        # Intel###########
        
        # broadcom########
        # bnx2:
        # eth0-0 eth0-1 eth0-2 eth0-3 eth0-4
        # eth0-0 eth0-1 eth0-2 eth0-3
        # bnx2x:
        # eth0-rx-0 eth0-rx-1 ...
        # eth0-tx-0 eth0-tx-1 ...
        # broadcom########

        local MAX_CPU=$(( $(grep processor /proc/cpuinfo | wc -l) ))
        cd /proc/irq
        ls -d */eth* | sed 's/[^0-9][^0-9]*/ /g' > /tmp/irqaffinity
        while read irq eth queue2
        do
                if [ -n "${queue}" ]; then
                        #queue=$(($[queue%${MAX_CPU}]))
                        let queue=$queue2%${MAX_CPU}
                        MASK=$((1<<$((queue))))
                        IRQ=$irq
                        DEV="eth"$eth
                        set_affinity
                fi
        done < /tmp/irqaffinity
}
set_chelsio()
{
        local MAX_CPU=$(( $(grep processor /proc/cpuinfo | wc -l)-1 ))
        cpu=$((1))
        for DEV in $(ifconfig -a | grep 00:07:43 | awk '{print $1}'); do
                for IRQ in $(egrep "rdma|ofld|${DEV}" /proc/interrupts | awk '{printf "%d\n",$1}'); do
                        MASK=${cpu}
                        set_affinity
                        cpu=$((${cpu}<<1))
                        if [[ ${cpu} -gt $((1<<${MAX_CPU})) ]]; then
                                cpu=$((1))
                        fi
                done
                ethtool -K $DEV gro off
        done        
}


if [ "$1" = "--version" -o "$1" = "-V" ]; then
        echo "version: $version"
        exit 0
elif [ -n "$1" ]; then
        echo "Description:"
        echo "    This script attempts to bind each queue of a multi-queue NIC"
        echo "    to the same numbered core, ie tx0|rx0 --> cpu0, tx1|rx1 --> cpu1"
        echo "usage:"
        echo "    $0 "
        exit 0
fi

# Set up the desired devices.
mutiqueue=`ls -d /proc/irq/*/eth* | grep eth.*-.*1`
if [ -n "${mutiqueue}" ]; then
        # check for irqbalance running
        IRQBALANCE_ON=`ps ax | grep -v grep | grep -q irqbalance; echo $?`
        #if [ "$IRQBALANCE_ON" == "0" ] ; then
        #        echo " WARNING: irqbalance is running and will"
        #        echo "          likely override this script's affinitization."
        #        echo "          So I stopped the irqbalance service by"
        #        echo "          'killall irqbalance'"
        #        killall irqbalance
        #fi

        result=$(ifconfig -a | grep "00:07:43")
        if [ -n "${result}" ]; then
                set_chelsio
        else
                set_intel_broadcom
        fi
fi



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

小黑屋|手机版|Archiver|追梦Linux    点击这里给我发消息

GMT+8, 2021-6-15 20:53 , Processed in 0.978197 second(s), 32 queries .

Powered by 追梦Linux! X3.3 Licensed

© 2015-2017 追梦Linux!.

快速回复 返回顶部 返回列表