InnoDB启用大内存页

在 Linux 操作系统上运行内存需求量较大的应用程序时,由于其采用的默认页面大小为 4KB,因而将会产生较多 TLB Miss 和缺页中断,从而大大影响应用程序的性能。当操作系统以 2MB 甚至更大作为分页的单位时,将会大大减少 TLB Miss 和缺页中断的数量,显著提高应用程序的性能。这也正是 Linux 内核引入大页面支持的直接原因。好处是很明显的,假设应用程序需要 2MB 的内存,如果操作系统以 4KB 作为分页的单位,则需要 512 个页面,进而在 TLB 中需要 512 个表项,同时也需要 512 个页表项,操作系统需要经历至少 512 次 TLB Miss 和 512 次缺页中断才能将 2MB 应用程序空间全部映射到物理内存;然而,当操作系统采用 2MB 作为分页的基本单位时,只需要一次 TLB Miss 和一次缺页中断,就可以为 2MB 的应用程序空间建立虚实映射,并在运行过程中无需再经历 TLB Miss 和缺页中断(假设未发生 TLB 项替换和 Swap)。

为了能以最小的代价实现大页面支持,Linux 操作系统采用了基于 hugetlbfs 特殊文件系统 2M 字节大页面支持。这种采用特殊文件系统形式支持大页面的方式,使得应用程序可以根据需要灵活地选择虚存页面大小,而不会被强制使用 2MB 大页面。

在mysql中,innodb可以使用large pages来分配buffer pool跟additional memory pool.
下面来看下mysql使用large pages具体的实现方法

1:查看系统是否支持huge-pages

InnoDB启用大内存页
[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 ~]# 
InnoDB启用大内存页

Hugepagesize有值,说明系统支持large pages(如不支持,系统需要重新编译来支持)

2.修改预分配的hugepages大小

[root@localhost ~]# sysctl -w vm.nr_hugepages=512
vm.nr_hugepages = 512
[root@localhost ~]# 
InnoDB启用大内存页
[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 ~]# 
InnoDB启用大内存页

3.查看mysql用户的组信息

[root@localhost ~]# id mysql
uid=500(mysql) gid=501(mysql) groups=501(mysql)
[root@localhost ~]# 

4.配置使用大页内存的用户组

[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 是最大的共享内存段的大小,单位是字节,默认32M,肯定是不够的,这个应该比innodb_buffer_pool要大。shmall是共享内存的总大小,单位是页,默认2097152(8G)。可以使用sysctl -w或者在/etc/sysctl.conf中设置。

sysctl -p使其设置生效。

7.修改my.cnf

[mysqld]
large-pages

8.重启mysql

InnoDB启用大内存页
[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
InnoDB启用大内存页

可以看见没有报错,假如我们调小nr_hugepages为200看看

InnoDB启用大内存页
[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]# 
InnoDB启用大内存页

上面出现了如下警告:

InnoDB: HugeTLB: Warning: Failed to allocate 274726912 bytes. errno 12
InnoDB HugeTLB: Warning: Using conventional memory pool

因为hugepage分配内存的时候,是一次性、且独占的, 一次性指在mysqld起来的时候,所有buffer pool需要的内存总和一次性的被分配,而且这些分配的内存不能被其他进程占用。所以就是一次性且独占。而当buffer pool所需要的内存被一次性的分配了之后,那么必然就不会使用swap了。
using conventional memory pool是什么意思呢?因为你的hugepage内存不够你的buffer pool大小,所以就用常规内存了。

 

开启大页内存的好处:

1.减少内存置换

2.减少TLB miss次数

3.减少swap

 

在启动mysql的时候,最容易报的错误是:

InnoDB: HugeTLB: Warning: Failed to allocate 274726912 bytes. errno 12
InnoDB HugeTLB: Warning: Using conventional memory pool

这是由于上面配置的两点所致。
1)nr_hugepages 的值*2M应大于(innodb_buffer_pool_size+innodb_additional_mem_pool_size)因为以上谈到 innodb可以使用large pages来分配buffer pool跟additional memory pool.
2)memlock 的设置,在启动mysql时,一定要先查看用ulimit -a 来查看max locked memory 设置是否合理,可以尝试用以上两种方法来设置该值。还有一点,/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

InnoDB启用大内存页,布布扣,bubuko.com

InnoDB启用大内存页

上一篇:c# 将字符串转换为逻辑表达式(字符串转换布尔)


下一篇:window docker的安装