mysql large_【转】mysql对large page的支持
昨天同事問我關(guān)于大頁內(nèi)存的事,我也只是有個(gè)模糊的概念,從別的博客轉(zhuǎn)過來的,先記錄下
在 Linux 操作系統(tǒng)上運(yùn)行內(nèi)存需求量較大的應(yīng)用程序時(shí),由于其采用的默認(rèn)頁面大小為 4KB,因而將會產(chǎn)生較多 TLB Miss 和缺頁中斷,從而大大影響應(yīng)用程序的性能。當(dāng)操作系統(tǒng)以 2MB 甚至更大作為分頁的單位時(shí),將會大大減少 TLB Miss 和缺頁中斷的數(shù)量,顯著提高應(yīng)用程序的性能。這也正是 Linux 內(nèi)核引入大頁面支持的直接原因。好處是很明顯的,假設(shè)應(yīng)用程序需要 2MB 的內(nèi)存,如果操作系統(tǒng)以 4KB 作為分頁的單位,則需要 512 個(gè)頁面,進(jìn)而在 TLB 中需要 512 個(gè)表項(xiàng),同時(shí)也需要 512 個(gè)頁表項(xiàng),操作系統(tǒng)需要經(jīng)歷至少 512 次 TLB Miss 和 512 次缺頁中斷才能將 2MB 應(yīng)用程序空間全部映射到物理內(nèi)存;然而,當(dāng)操作系統(tǒng)采用 2MB 作為分頁的基本單位時(shí),只需要一次 TLB Miss 和一次缺頁中斷,就可以為 2MB 的應(yīng)用程序空間建立虛實(shí)映射,并在運(yùn)行過程中無需再經(jīng)歷 TLB Miss 和缺頁中斷(假設(shè)未發(fā)生 TLB 項(xiàng)替換和 Swap)。
為了能以最小的代價(jià)實(shí)現(xiàn)大頁面支持,Linux 操作系統(tǒng)采用了基于 hugetlbfs 特殊文件系統(tǒng) 2M 字節(jié)大頁面支持。這種采用特殊文件系統(tǒng)形式支持大頁面的方式,使得應(yīng)用程序可以根據(jù)需要靈活地選擇虛存頁面大小,而不會被強(qiáng)制使用 2MB 大頁面。
在mysql中,innodb可以使用large pages來分配buffer pool跟additional memory pool.
下面來看下mysql使用large pages具體的實(shí)現(xiàn)方法
1:查看系統(tǒng)是否支持huge-pages
[root@localhost ~]# cat /proc/meminfo | grep -i huge
AnonHugePages: 47708160 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
[root@localhost ~]#
Hugepagesize有值,說明系統(tǒng)支持large pages(如不支持,系統(tǒng)需要重新編譯來支持)
2.修改預(yù)分配的hugepages大小
[root@localhost ~]# sysctl -w vm.nr_hugepages=512
vm.nr_hugepages = 512
[root@localhost ~]#
[root@localhost ~]# cat /proc/meminfo | grep -i huge
AnonHugePages: 79872 kB
HugePages_Total: 512
HugePages_Free: 505
HugePages_Rsvd: 229
HugePages_Surp: 0
Hugepagesize: 2048 kB
[root@localhost ~]#
3.查看mysql用戶的組信息
[root@localhost ~]# id mysql
uid=500(mysql) gid=501(mysql) groups=501(mysql)
[root@localhost ~]#
4.配置使用大頁內(nèi)存的用戶組
[root@localhost ~]# sysctl -w vm.hugetlb_shm_group=501
vm.hugetlb_shm_group = 501
[root@localhost ~]#
5.修改ulimit
vim /etc/security/limits.conf
@mysql soft memlock unlimited
@mysql hard memlock unlimited
6.修改/etc/sysconfig.conf
# Increase the amount of shmem allowed per segment
# This depends upon your memory, remember your
kernel.shmmax = 68719476736
# Increase total amount of shared memory.
kernel.shmall = 4294967296
shmmax 是最大的共享內(nèi)存段的大小,單位是字節(jié),默認(rèn)32M,肯定是不夠的,這個(gè)應(yīng)該比innodb_buffer_pool要大。shmall是共享內(nèi)存的總大小,單位是頁,默認(rèn)2097152(8G)??梢允褂胹ysctl -w或者在/etc/sysctl.conf中設(shè)置。
sysctl -p使其設(shè)置生效。
7.修改my.cnf
[mysqld]
large-pages
8.重啟mysql
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL.... [ OK ]
[root@localhost mysql]# cat localhost.localdomain.err
140320 15:18:12 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
140320 15:18:12 [Note] Event Scheduler: Purging the queue. 0 events
140320 15:18:12 InnoDB: Starting shutdown...
140320 15:18:12 InnoDB: Shutdown completed; log sequence number 3629001490
140320 15:18:12 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
140320 15:18:12 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
140320 15:18:13 mysqld_safe Starting mysqld daemon with databases from /data/mysql
140320 15:18:13 InnoDB: The InnoDB memory heap is disabled
140320 15:18:13 InnoDB: Mutexes and rw_locks use GCC atomic builtins
140320 15:18:13 InnoDB: Compressed tables use zlib 1.2.3
140320 15:18:13 InnoDB: Initializing buffer pool, size = 256.0M
140320 15:18:13 InnoDB: Completed initialization of buffer pool
140320 15:18:13 InnoDB: highest supported file format is Barracuda.
140320 15:18:15 InnoDB: Waiting for the background threads to start
140320 15:18:16 InnoDB: 1.1.8 started; log sequence number 3629001490
140320 15:18:16 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
140320 15:18:16 [Note] - '0.0.0.0' resolves to '0.0.0.0';
140320 15:18:16 [Note] Server socket created on IP: '0.0.0.0'.
140320 15:18:16 [Note] Event Scheduler: Loaded 0 events
140320 15:18:16 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.5.25a-log' socket: '/tmp/mysqld.sock' port: 3306 Source distribution
可以看見沒有報(bào)錯(cuò),假如我們調(diào)小nr_hugepages為200看看
[root@localhost mysql]# sysctl -w vm.nr_hugepages=200
vm.nr_hugepages = 200
[root@localhost mysql]# /etc/init.d/mysqld restart
Shutting down MySQL. [ OK ]
Starting MySQL... [ OK ]
[root@localhost mysql]# cat localhost.localdomain.err
140320 15:20:08 [Note] /usr/local/mysql/bin/mysqld: Normal shutdown
140320 15:20:08 [Note] Event Scheduler: Purging the queue. 0 events
140320 15:20:08 InnoDB: Starting shutdown...
140320 15:20:08 InnoDB: Shutdown completed; log sequence number 3629001490
140320 15:20:08 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
140320 15:20:08 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended
140320 15:20:09 mysqld_safe Starting mysqld daemon with databases from /data/mysql
140320 15:20:09 InnoDB: The InnoDB memory heap is disabled
140320 15:20:09 InnoDB: Mutexes and rw_locks use GCC atomic builtins
140320 15:20:09 InnoDB: Compressed tables use zlib 1.2.3
140320 15:20:09 InnoDB: Initializing buffer pool, size = 256.0M
InnoDB: HugeTLB: Warning: Failed to allocate 274726912 bytes. errno 12
InnoDB HugeTLB: Warning: Using conventional memory pool
140320 15:20:09 InnoDB: Completed initialization of buffer pool
140320 15:20:09 InnoDB: highest supported file format is Barracuda.
140320 15:20:11 InnoDB: Waiting for the background threads to start
140320 15:20:12 InnoDB: 1.1.8 started; log sequence number 3629001490
140320 15:20:12 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
140320 15:20:12 [Note] - '0.0.0.0' resolves to '0.0.0.0';
140320 15:20:12 [Note] Server socket created on IP: '0.0.0.0'.
140320 15:20:12 [Note] Event Scheduler: Loaded 0 events
140320 15:20:12 [Note] /usr/local/mysql/bin/mysqld: ready for connections.
Version: '5.5.25a-log' socket: '/tmp/mysqld.sock' port: 3306 Source distribution
[root@localhost mysql]#
上面出現(xiàn)了如下警告:
InnoDB: HugeTLB: Warning: Failed to allocate 274726912 bytes. errno 12
InnoDB HugeTLB: Warning: Using conventional memory pool
因?yàn)閔ugepage分配內(nèi)存的時(shí)候,是一次性、且獨(dú)占的, 一次性指在mysqld起來的時(shí)候,所有buffer pool需要的內(nèi)存總和一次性的被分配,而且這些分配的內(nèi)存不能被其他進(jìn)程占用。所以就是一次性且獨(dú)占。而當(dāng)buffer pool所需要的內(nèi)存被一次性的分配了之后,那么必然就不會使用swap了。
using conventional memory pool是什么意思呢?因?yàn)槟愕膆ugepage內(nèi)存不夠你的buffer pool大小,所以就用常規(guī)內(nèi)存了。
開啟大頁內(nèi)存的好處:
1.減少內(nèi)存置換
2.減少TLB miss次數(shù)
3.減少swap
在啟動mysql的時(shí)候,最容易報(bào)的錯(cuò)誤是:
InnoDB: HugeTLB: Warning: Failed to allocate 274726912 bytes. errno 12
InnoDB HugeTLB: Warning: Using conventional memory pool
這是由于上面配置的兩點(diǎn)所致。
1)nr_hugepages 的值*2M應(yīng)大于(innodb_buffer_pool_size+innodb_additional_mem_pool_size)因?yàn)橐陨险劦?innodb可以使用large pages來分配buffer pool跟additional memory pool.
2)memlock 的設(shè)置,在啟動mysql時(shí),一定要先查看用ulimit -a 來查看max locked memory 設(shè)置是否合理,可以嘗試用以上兩種方法來設(shè)置該值。還有一點(diǎn),/etc/security/limits.conf配置文件的修改.
參考資料:
http://www.cyberciti.biz/tips/linux-hugetlbfs-and-mysql-performance.html
https://dev.mysql.com/doc/refman/5.0/en/large-page-support.html
轉(zhuǎn)自:
總結(jié)
以上是生活随笔為你收集整理的mysql large_【转】mysql对large page的支持的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx设置跨域问题
- 下一篇: 联发科有没有高端处理器_2021年华为将