tmpfs加速并降低squid负载(一)

原因:

原squid速度慢,uptime看平均负载很高!

概念:一般负载峰值设为cpu个数的5倍。Load avarage/cpu数目 <3 系统良好,3到5之间可以接受,大于5 则可能有严重的性能问题。举个例子:假设系统有两个cpu,那么负载在10以下都是可以接受的,超过10,就说明有严重问题了。

看cpu个数:grep -c processor /proc/cpuinfo                                                        关于负载的一篇好文章:http://ssorc.tw/logsprint.php?id=59

处理思路:

重新编译,2.5换到2.6

'--prefix=/opt/squid' '--enable-async-io=40' '--enable-storeio=aufs,coss,diskd,ufs' '--enable-useragent-log' '--enable-referer-log' '--enable-kill-parent-hack' '--enable-forward-log' '--enable-snmp' '--enable-cache-digests' '--enable-default-err-language=Simplify_Chinese' '--enable-epoll' '--enable-removal-policies=heap,lru' '--enable-large-cache-files' '--disable-internal-dns' '--enable-x-accelerator-vary' '--enable-follow-x-forwarded-for' '--with-large-files' '--disable-ident-lookups' '--enable-underscore' '--disable-arp-acl' '--with-maxfd=65500'

修改内核参数(因为TIME_WAIT太多)

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.ip_local_port_range = 1024    65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

发现错误:

WARNING: Disk space over limit: 5242944 KB > 5242880 KB

修改缓存策略:

原:

cache_dir aufs /var/squid/cache/cache01 5120 16 32

现:

cache_replacement_policy heap GDSF

memory_replacement_policy heap GDSF

cache_dir aufs /var/squid/cache/cache01 5120 16 128

cache_dir aufs /var/squid/cache/cache02 5120 16 128

等待结果……….

现在发现的WARNING

WARNING:squidaio_queue_request: WARNING - Queue congestion

IO的队列满了, ,重谝一下源代码,加大IO的队列或换一种IO方式。

--enable-async-io=40 (40少了)

WARNING: All dnsserver processes are busy.

WARNING: up to 5 pending requests queued

修改为:

'--prefix=/opt/squid' '--enable-async-io=160' '--enable-storeio=aufs,coss,diskd,ufs' '--enable-useragent-log' '--enable-referer-log' '--enable-kill-parent-hack' '--enable-forward-log' '--enable-snmp' '--enable-cache-digests' '--enable-default-err-language=Simplify_Chinese' '--enable-epoll' '--enable-removal-policies=heap,lru' '--enable-large-cache-files' '--disable-internal-dns' '--enable-x-accelerator-vary' '--enable-follow-x-forwarded-for' '--with-large-files' '--disable-ident-lookups' '--enable-underscore' '--disable-arp-acl' '--with-maxfd=65500'

查看平均负载并未降低:

[root@SQUID99 ~]# vmstat 2

procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----

 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa

 0  0      0 2655000 363884 3002856    0    0     5   157    0     3  1  1 92  6

 0  2      0 2652696 363920 3004380    0    0    78  2286 7076  3115  3  1 86 11

 0  0      0 2652376 363972 3004588    0    0    26  5246 3336  1136  1  0 88 11

 0  5      0 2651288 364000 3005860    0    0    66   792 5888  3395  3  1 87  9

 0  0      0 2650448 364044 3007116    0    0    72  1410 5600  3265  3  1 92  4

 1  0      0 2648128 364092 3008888    0    0   110     0 7257  4573  4  1 94  1

 0  4      0 2647952 364116 3009124    0    0    32  2828 4046  1553  1  0 82 16

bo,对磁盘的写入,这个数字偏大

补充:查看磁盘的性能 使用 iostat -d 2这样的命令:

Device: 设备或者分区名

tps:每秒发送到设备上的IO请求次数  

Blk_read/s:设备每秒钟读的数据(KB/秒)  

Blk_wrtn/s:设备每秒钟写的数据(KB/秒) 

Blk_read:设备读数据的总大小(KB)

Blk_wrtn:设备写数据的总大小(KB)
 

怀疑是磁盘cache文件太多,并且全为小文件

Store Directory #0 (aufs): /var/squid/cache/cache01

FS Block Size 4096 Bytes

First level subdirectories: 16

Second level subdirectories: 128

Maximum Size: 5242880 KB

Current Size: 4618916 KB

Percent Used: 88.10%

Current load metric: 102 / 1000

Filemap bits in use: 1037841 of 1048576 (99%)

Filesystem Space in use: 10222176/82518992 KB (12%)

Filesystem Inodes in use: 2082702/10485760 (20%)

Flags: SELECTED

Accepted object sizes: 0 - (unlimited) bytes

Removal policy: heap

尝试用内存作为cache,取消磁盘cache:

方法转自:

http://www.5ilinux.com/blog/archives/000075.html

巧用tmpfs加速你的linux服务器

原理:使用tmpfs来实现虚拟磁盘来存放squid的缓存文件

默认系统就会加载/dev/shm ,它就是所谓的tmpfs,有人说跟ramdisk(虚拟磁盘),但不一样。象虚拟磁盘一样,tmpfs 可以使用您的 RAM,但它也可以使用您的交换分区来存储。而且传统的虚拟磁盘是个块设备,并需要一个 mkfs 之类的命令才能真正地使用它,tmpfs 是一个文件系统,而不是块设备;您只是安装它,它就可以使用了。

tmpfs有以下优势:
1。动态文件系统的大小,
2。tmpfs 的另一个主要的好处是它闪电般的速度。因为典型的 tmpfs 文件系统会完全驻留在 RAM 中,读写几乎可以是瞬间的。
3。tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载,绑定的操作。

首先在/dev/stm建个tmp文件夹,然后与实际/tmp绑定

mkdir /dev/shm/tmp
chmod 1777 /dev/shm/tmp
mount --bind /dev/shm/tmp /tmp

默认shm是使用实际内存的一半


1。squid的缓存目录设置

vi /etc/squid/squid.conf

修改成
cache_dir ufs /tmp 256 16 256
这里的第一个256表示使用256M内存,我觉得高性能LINUX双效防火墙HOWTO使用ramdisk的方法还不如直接使用tmpfs,至少每次启动不用mkfs,还可以动态改变大小。

然后重启一下服务,ok,现在所有的squid缓存文件都保存倒tmpfs文件系统里了,很快哦。

 运行一天后的结果,效果还是很明显,负载从12下降到了0.3!!并且vmstat查看也有很大变化

[root@SQUID95 ~]# uptime

 09:25:07 up 118 days, 21:19,  2 users,  load average: 0.32, 0.34, 0.27

[root@SQUID95 ~]# vmstat 2
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 0  0    208 3978912 198916 2286424    0    0    11    19    0     0  1  1 95  4
 0  0    208 3978784 198916 2286684    0    0     0    50 7868  4551  2  1 97  0
 0  0    208 3978848 198916 2285904    0    0     0     0 6302  3639  2  0 98  0
 1  1    208 3978656 198944 2286656    0    0     0    40 12740  6353  6  2 92  0
 1  0    208 3978464 198956 2286644    0    0     0   154 10673  5910  4  1 95  0
 0  0    208 3977952 198956 2286904    0    0     0     0 10292  5477  4  1 95  0
 0  0    208 3977904 198956 2287164    0    0     0   152 8622  5113  3  1 96  0

[root@SQUID95 ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/cciss/c0d0p2      29G  1.1G   27G   4% /

/dev/cciss/c0d0p1      99M   12M   82M  13% /boot

none                  4.0G  1.4G  2.6G  35% /dev/shm

/dev/cciss/c0d0p3      20G  1.8G   17G  10% /usr

/dev/cciss/c0d0p6      79G   25G   51G  33% /var

/dev/shm/tmp          4.0G  1.4G  2.6G  35% /tmp

磁盘缓存和RAM缓存比较:

1.磁盘缓存重要在缓存,机器down机或者重启都没有影响,但是不断对磁盘进行读写,负载会高

2.RAM缓存重要在速度,因为是内存读写,速度很快,但是缓存的文件没有保证,机器一旦重启,缓存文件将丢失。

但是选择磁盘缓存还是RAM缓存,还是应该看自己的实际情况,对于我这边,由于是单点,都是本地抓取文件,速度很快,所以缓存就显的不是那么重要。

实际应用出现的问题:

df -h 看

/dev/shm/tmp          4.0G  1.4G  2.6G  35% /tmp 明显空间还很多

但是cache.log中大量报错:

2009/08/11 17:06:47|/tmp/swap.state.new: (28) No space left on device
                   |FATAL: storeDirOpenTmpSwapLog: Failed to open
swap log.

2009/08/11 17:06:47| storeAufsOpenDone: (28) No space left on device
2009/08/11 17:06:47|    /tmp/0C/4B/000325CF
2009/08/11 17:06:47| storeSwapOutFileClosed: dirno 0, swapfile 000325CF, errflag=-1
        (28) No space left on device
2009/08/11 17:06:47| storeAufsOpenDone: (28) No space left on device
2009/08/11 17:06:47|    /tmp/0C/4B/000325CF
2009/08/11 17:06:47| storeSwapOutFileClosed: dirno 0, swapfile 000325CF, errflag=-1
        (28) No space left on device
2009/08/11 17:06:47| storeAufsOpenDone: (28) No space left on device

然后df -i 发现问题:

/dev/shm/tmp          209923  209923       0  100% /tmp
innodes被用光了!

查看一些资料,发现在mount tmp这个虚拟分区的时候可以修改inodes

mount -t tmpfs -o size=1G,nr_inodes=40k,mode=0700 tmpfs /tmp

大小为1G,inodes40K=40960

可是问题又出现了,squid不会自己清cache,直到磁盘用光down掉!

棘手的问题。

原文地址:https://www.cnblogs.com/youlechang123/p/2631169.html