64位中使用AWE分配内存

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: We have already talked about Windows AWE mechanism on 32 bit and how SQL Server utilizes it. Today I would like to go over AWE & related mechanism on 64 bit platforms. 我们已经谈过windows AWE 在32bit 中sql server 如何利用。

We have already talked about Windows AWE mechanism on 32 bit and how SQL Server utilizes it. Today I would like to go over AWE & related mechanism on 64 bit platforms.

我们已经谈过windows AWE 在32bit 中sql server 如何利用。下面是AWE在64位环境的相关性
 
To some people it comes as a surprise that AWE mechanism is still present and actually could be useful on 64 bit platforms. As you remember the mechanism consists of two parts allocating physical memory and mapping it to the given process's VAS. The advantage of allocation mechanism is that once physical memory is allocated operating system can't reclaim it until either the process is terminated or the process frees memory back to the OS. This feature allows an application to control and even avoid paging altogether. Advantage of mapping/unmapping mechanism is that the same physical page could be mapped into different VAS's regions. As you imaging unmapping is not necessary on 64 bit platforms since we have enough VAS to accommodate all of existing physical memory.
 
对于有些人会很惊奇AWE机制任然存在并且在64位中是可用的。这个机制分为2部分分配内存和映射到给定的VAS中。这种分配机制的好处是不会被系统回收,直到进程终止或者进程内存被释放会操作系统。这特性允许应用程序控制并且避免分页。映射和非映射机制的好处是相同的物理页可以被映射到不同VAS空间。你可以想象在64bit中非映射是没有必要的,因为64bit 有足够的vas容纳所有物理内存。
 

From Operating System theory, OS implements a page table entry, PTE, to describe a mapping of a page in VAS to physical page. Internally physical page is described by page frame number, PFN. Given PFN one can derive complete information about physical page it represents. For example PFN shows to which NUMA node the particular page belongs. OS has a database, collection of PFNs that it manages.  If page in VAS is committed, it has PTE which might or might not point to given PFN.  Conceptually, page that PTE represents can be either in memory or not, for example swapped out to disk. In the former case it is bound to a given PFN and in latter it is not. In its turn, once a physical page is bound to page in VAS, its PFN points back to PTE.

来自操作系统的理论,操作系统引入一个页表项,PTE,描述了一个在VAS中的页到物理页的映射。物理页被页帧号描述,PFN。给定的PFN可以导出所有它代表的物理页。例如PFN显示了指定的物理也属于那个NUMA节点。OS有一个数据库收集了PFN并且管理它。如果页在VAS中已经被提交了,PTE可能指向了一个给定的PFN。理论上PTE上的页可能在内存上也可能没有,如切换到了磁盘上。先前的例子页绑定了一个PFN,并且之后不绑定了。一旦物理页被绑定到VAS中,PFN就反向指向到PTE。
 

When OS commits, frees, pages out/in a given PTE or needs to derive some information about it, for example NUMA residency, it has to acquire process's working set lock - to guarantee stability of PTE to PFN binding. This lock is a rather expensive and might hurt scalability of the process. Latter versions of Windows made this lock as light as possible but avoiding still will benefit application's scalability..

当OS提交,释放,切换一个给定的PTE或者取一些页的信息。比如NUMA位置,页不得不请求进程工作集锁,来保证PTE到PFN之间的稳定性。这个锁定比较昂贵并且可能会损害进程的扩展性。之后的windows版本会使这个锁越来越轻量但是不能保证会对程序的扩展性有好处。
 

When allocating physical pages utilizing AWE mechanism we are given a set of PFN entries directly from PFN database - remember that you should not manipulate or modify set of entries you get back  nor can you rely on values you get back. OS is required to take a PFN database lock when allocating PFN entries. Using AWE map mechanism you can map allocated PFN entries to the process's VAS. When mapping occurs PTEs are allocated, bound to PFNs and marked as locked. In this case OS needs to acquire process's working set lock only ones. When mapping regular pages, OS does it on demand and hence will have to acquire both working set and PFN database lock for every page. Since pages are locked in memory, OS will ignore these PTEs during  paging process.

当物理内存通过AWE分配的时候,我们获取的PFN项直接来至于PFN数据库——记住你不能删除或者修改你获取的项,也不能依赖你获取的值。当PFN被分配的时候,OS会请求锁定PFN数据库。使用AWE映射机制你可以把PFN映射到进程的VAS中。当映射发生PTE就会被分配,绑定到PFN并且标记被锁定。这个时候OS只需要获取进程的工作集锁。当映射常规也的时候,OS按需求,并且会请求工作集锁和PFN数据库锁。因为页在内存中是锁定的,在分页进程的时候OS会忽略这些PTE。
 

On 64 bit platforms it is better to refer to such pages as locked pages - please don't confuse them with pages locked through VirtualLock API. As described above locked pages have two major properties - they are not considered for paging by OS and during allocation they acquire both working set and PFN database lock only ones. 

在64bit平台上, 这些页被锁定会更好——不要喝通过VirtualLock API混淆。页锁定有2个主要的属性——他们不会被OS分页并且在分配的时候只会请求工作集锁和PFN锁的一个。
 

The first property has implicit implication on high end hardware such as NUMA. It provides explicit memory residency. Remember that OS commits a page on demand. To allocate physical memory, it will use a node on which a thread touching memory is running.  Latter on, the page can be swapped out by OS. Next time it will be brought up into memory, OS will again allocate physical page from the node a thread touching memory is running on. In this case a node could be completely different  from original one. Such behavior makes hard for applications to rely on page's NUMA residency. Locked pages solve this problem by removing themselves from paging altogether.  Moreover Windows 2003 SP1 introduced a new API - QueryWorkingSetEx. It allows to query extended  information about PTE's PFN.  In order to find out real page residency you should use this API. When pages are locked you need to it only ones. Otherwise you will have to do it periodically since residency of the page can actually change.

 第一个属性隐约的涉及到了高端的硬件如NUMA。它提供了显示的内存位置。记住OS按需求提交了页。分配内存的时候会在线程触及到的内存节点中分配内存。
 

The second property - taking both working set and PFN's database lock only ones enables applications to perform faster and better scalable ramp up.

 第二个属性——只要获取工作集锁和PFN数据库锁一个,可以让应用程序运行的更快并且有更好的扩展性。
 

On NUMA SQL Server' Buffer Pool marks each allocated page with its node residency. It leverages QueryWorkingSetEx to accomplish it.  Once page is allocated it calls the API to find  out page residency.   It does it only once. Therefore enabling locked pages for SQL server on 64 bit platform  will improve SQL Server ramp up time and will improve performance & scalability over longer period of time. When running SQL Server with locked pages enabled you shouldn't be worried about overall system performance due to memory starvation - SQL Server participates in OS's paging mechanism by listening on OS's memory notification API's and shrinks its working set accordingly.

 在NUMA SQL Server buffer pool 中标记了每个分配也的节点位置。使用QueryWorkingSetEx来完成。一旦页分配,就会调用api查找出页所在节点,只运行一次。因此SQL Server 64位平台会提高SQL Server 加速并且会提高性能和可扩展性。当运行sql server锁定页你不需要担心因为内存不足引起的性能问题——sql server 会监听OS内存通知api参与分页并且压缩工作集。
 

Let us summarize now - on 64 bit platform, locked pages, awe mechanism, enable better application's scalability and performance both during ramp up time and over long period of time. However, keep in mind that an application is still required to implement a way of responding to memory pressure to avoid starving the whole system for memory.

总结一下:在64位的平台上,锁定页,AWE分配机制,可以让应用程序有更好的扩展性和性能。但是记住一个应用程序也要增强相应内存压力的能力来避免整个系统的内存不足。
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
1月前
|
存储 程序员 C语言
内存管理——内存分配函数
内存管理——内存分配函数
19 0
|
2月前
|
Kubernetes 网络性能优化 调度
|
9月前
|
存储 Linux C++
内存管理 - 物理内存
内存管理 - 物理内存
157 0
|
10月前
|
存储 安全 程序员
内存分配理解
内存分配理解
116 0
|
缓存 C语言
内存分配
内存分配
156 0
|
存储 C语言
内存分配与编译处理
内存分配与编译处理
92 0
内存分配与编译处理
|
容器
一.空间分配器
# 一.空间分配器 ### 分配内存: * 当容器需要空间来存放元素时,需要空间配置器(也就是分配器)分配内存,当分配的内存大于128个字节时,调用第一级配置器,调用malloc为其分配内存,当分配内存小于128个字节时,调用第二级配置器,检查对应的free-list上是否有可用区块,如果有的话,直接拿来用,如果没有的话调用rfill。
66 0
|
Java Android开发
Android内存分配/回收的一个问题-为什么内存使用很少的时候也GC
Android内存分配/回收的一个问题-为什么内存使用很少的时候也GC
300 0
Android内存分配/回收的一个问题-为什么内存使用很少的时候也GC
内存占用高-AWE
分享一个AWE占用内存高的案例
内存占用高-AWE
|
Java
v8内存分配浅谈
### 前言 本文会通过V8中对String对象的内存分配开始分析,对中间出现的源码进行解读。对heap内存的新生代分配和老生代内存分配的过程解读。首先,我们来看一张流程图,该流程图给出整个分配过程中的前期流程图,其中省略了一些步骤,只给出了关键的步骤。 ![image1](http://blog.magicare.me/content/images/2018/09/v8_string
1480 0