为InnoDB存储引擎配置内存分配器

2018/03/22 posted in  Tech
Tags: 

这是一篇MySQL官方文档的翻译,是我在查阅InnoDB额外内存池相关资料时找到的

原文链接

14.6.4 Configuring the Memory Allocator for InnoDB

14.6.4 为InnoDB存储引擎配置内存分配器

​When InnoDB was developed, the memory allocators supplied with operating systems and run-time libraries were often lacking in performance and scalability. At that time, there were no memory allocator libraries tuned for multi-core CPUs. Therefore, InnoDB implemented its own memory allocator in the mem subsystem. This allocator is guarded by a single mutex, which may become a bottleneck. InnoDB also implements a wrapper interface around the system allocator (malloc and free) that is likewise guarded by a single mutex.

在InnoDB存储引擎被开发时,操作系统和运行时库所提供的内存分配器在性能和扩展性上都表现欠佳。当时并没有针对多核CPU调优的内存分配器库。因此,InnoDB在mem子系统中自己实现了一个内存分配器。这个分配器被一个单一的互斥锁保护着,而这则可能会导致瓶颈。InnoDB也实现了一个基于系统分配器(malloc和free)的包装接口,同样的,也被单一互斥锁保护着。

Today, as multi-core systems have become more widely available, and as operating systems have matured, significant improvements have been made in the memory allocators provided with operating systems. These new memory allocators perform better and are more scalable than they were in the past. Most workloads, especially those where memory is frequently allocated and released (such as multi-table joins), benefit from using a more highly tuned memory allocator as opposed to the internal, InnoDB-specific memory allocator.

现在,随着多核系统的广泛应用和操作系统的不断成熟,操作系统所提供的内存分配器也取得了重大的进步。新的内存分配器在性能和扩展性方面都比之前的要更加优异。在大多数的工作负载(尤其是需要频繁的分配和释放内存的)场景下,使用经过高度调优的内存分配器要比使用InnoDB特定的内置的内存分配器效果更好。

​You can control whether InnoDB uses its own memory allocator or an allocator of the operating system, by setting the value of the system configuration parameterinnodb_use_sys_malloc in the MySQL option file (my.cnf or my.ini). If set to ON or 1 (the default), InnoDB uses the malloc and freefunctions of the underlying system rather than manage memory pools itself. This parameter is not dynamic, and takes effect only when the system is started. To continue to use the InnoDB memory allocator, set innodb_use_sys_malloc to 0.

你可以通过配置MySQL配置文件(my.cnf or my.ini)中的innodb_use_sys_malloc参数来控制InnoDB是使用自带的内存分配器还是使用操作系统提供的内存分配器。如果参数被设置为ON或者1(这也是默认值),InnoDB会使用当前系统下的malloc和free函数,而不是维护一个自己的内存池。这个参数并非是动态的,它会随着系统的启动而生效。将innodb_use_sys_malloc设置为0,则会继续使用InnoDB的内存分配器。

​When the InnoDB memory allocator is disabled, InnoDB ignores the value of the parameter innodb_additional_mem_pool_size. The InnoDB memory allocator uses an additional memory pool for satisfying allocation requests without having to fall back to the system memory allocator. When the InnoDB memory allocator is disabled, all such allocation requests are fulfilled by the system memory allocator.

当InnoDB内存分配器被关闭是,InnoDB会忽略innodb_additional_mem_pool_size参数的值。InnoDB内存分配器使用一个额外的内存池来满足分配请求,而不必依赖于系统的内存分配器。而当InnoDB内存分配器不可用时,所有的分配请求都将由系统的内存分配器来满足。

​On Unix-like systems that use dynamic linking, replacing the memory allocator may be as easy as making the environment variable LD_PRELOAD or LD_LIBRARY_PATH point to the dynamic library that implements the allocator. On other systems, some relinking may be necessary. Please refer to the documentation of the memory allocator library of your choice.

在使用动态链接的类Unix系统上,替换内存分配器很简单,只要将环境变量LD_PRELOAD or LD_LIBRARY_PATH指向实现了分配器的动态库就行。在其他系统中则需要重新连接。请参考你所选择的内存分配器库的相关文档。

​Since InnoDB cannot track all memory use when the system memory allocator is used (innodb_use_sys_malloc is ON), the section “BUFFER POOL AND MEMORY” in the output of the SHOW ENGINE INNODB STATUS command only includes the buffer pool statistics in the “Total memory allocated”. Any memory allocated using the mem subsystem or using ut_malloc is excluded.

当使用系统内存分配器(innodb_use_sys_malloc设为ON)时,InnoDB无法跟踪内存的使用,因此在使用SHOW ENGINE INNODB STATUS命令查看输出中的BUFFER POOL AND MEMORY信息时“Total memory allocated”仅包含了缓冲池(buffer pool)的统计。而没有使用mem子系统或者ut_malloc的内存分配。

Note

​innodb_use_sys_malloc and innodb_additional_mem_pool_size were deprecated in MySQL 5.6 and removed in MySQL 5.7.

innodb_use_sys_malloc和innodb_additional_mem_pool_size在MySQL 5.6中被弃用,在MySQL 5.7中被移除。