向 Android 添加 16 kb 页面大小
Adding 16 kb page size to Android

原始链接: https://android-developers.googleblog.com/2024/08/adding-16-kb-page-size-to-android.html

Android 操作系统 v15 支持两种页面大小 - 4 KB 和 16 KB。 从历史上看,Android 系统可以在 4 KB 页面大小下高效运行,因为大多数现代 CPU(包括 ARM)都支持此功能。 不过,我们进行了一项更改,通过将页面大小增加到 16 KB 来提高性能,从而使性能提高 5-10%,同时内存使用量增加约 9%。 较大的页面大小可以减少操作系统处理的簿记任务,从而可以更好地分配资源,从而提高视频质量、游戏玩法和流畅的应用程序功能。 为了适应此更新,Android 15 允许用户通过特定设备上提供的开发者选项选择 4 KB 和 16 KB 页面大小,例如 Google Pixel 8 和 8 Pro 的 QPR1 beta 版本。 此外,还为使用 Android Studio 的开发人员提供了 16 KB 模拟器目标。 过渡需要调整内存管理单元 (MMU) 和页表,确保正确的内存映射,并更改 EROFS、F2FS 和 UFS 存储层等文件系统,以促进 16 KB 兼容性。 由于 16 KB 对齐需要额外的填充,文件大小可能会略有增加,因此需要优化技术。 包含本机代码或依赖项的应用程序需要重新编译才能与 16 KB 页面大小兼容。 要开发专门针对 16 KB 设备的应用程序,开发人员可以在执行擦除并解锁设备的引导加载程序后在重新启动时切换相应的选项。 对于非 ARM 平台,例如 x86_64 台式电脑,Android Studio 提供了模拟的 16 KB 环境,使开发人员能够继续工作,而无需直接访问 ARM 硬件。

应用程序应调整其内存行为以优化透明大页 (THP) 的优势。 目标是最大限度地减少小型内存事务,因为它们会在虚拟到物理的转换过程中造成过多的开销,从而导致速度减慢。 为了实现这一目标,可以采取多种方法: 1.显式方法:利用“hugetlbfs”,一种专为大内存分配而设计的特殊文件系统。 应用程序可以通过该接口请求特定的大小,确保获得所需的大页面。 但这种方法需要开发人员手动干预,并未被广泛采用。 2.隐式方法:利用Linux内核提供的透明HugePages。 通过启用此功能,操作系统会尽可能自动为 mmap 调用分配大页面,从而为内存密集型应用程序提供显着改进。 要启用透明大页,请发出命令“echo never > /sys/kernel/mm/transparent_hugepage/defrag”,然后发出“echo madvise >> /proc/sys/vm/compactpages”。 然后,根据系统的可用资源修改“/proc/sys/vm/hugepages_total”和“/proc/sys/vm/hugepages_surphide”设置。 或者,您可以通过保留默认设置来让系统管理大页面。 使用大页面时,请务必注意库和垃圾收集器可能需要调整以处理更大的内存单元。 较小的页面大小(例如数据库使用的页面大小)在迁移到透明大页面时可能需要仔细考虑。 除此之外,根据请求的页面大小,TLB 大小也有所不同。 用户空间可以自由分配内存,最多可达相应 TLB 支持的最大值。 至于论文,不幸的是我找不到公开版本。
相关文章

原文
Posted by Steven Moreland – Staff Software Engineer, Sandeep Patil – Principal Software Engineer

A page is the granularity at which an operating system manages memory. Most CPUs today support a 4 KB page size and so the Android OS and applications have historically been built and optimized to run with a 4 KB page size. ARM CPUs support the larger 16 KB page size. When Android uses this larger page size, we observe an overall performance boost of 5-10% while using ~9% additional memory.

In order to improve the operating system performance overall and to give device manufacturers an option to make this trade-off, Android 15 can run with 4 KB or 16 KB page sizes.

The very first 16 KB enabled Android system will be made available on select devices as a developer option. This is so you can use the developer option to test and fix (if needed) your applications to prepare for Android devices with 16 KB page sizes in the near future.

Details

In most CPUs, dedicated hardware called memory management units (MMUs) translate addresses from what a program is using to a physical location in memory. This translation is done on a page-size basis. Every time a program needs more memory, the operating system needs to get involved and fill out a “page table” entry, assigning that piece of memory to a process. When the page size is 4 times larger, there is 4 times less bookkeeping. So, the system can spend more time making sure your videos look great, games play well, and applications run smoothly, and less time filling out low-level operating system paperwork.

Unlike 32-bit/64-bit mode, a page size is not an Application Binary Interface (ABI). In other words, once an application is fixed to be page size agnostic, the same application binary can run on both 4 KB and 16 KB devices.

In Android 15, we’ve refactored Android from the ground up to support running at different page sizes, thus making it page-size agnostic.

Major OS Changes

On new Android 15 based devices:

    • All OS binaries are 16 KB aligned (-Wl,-z,max-page-size=16384). 3rd party applications / libraries may not be 16 KB aligned.
    • All OS binaries are built with separate loadable segments (-Wl,-z,separate-loadable-segments) to ensure all memory regions mapped into a process are readable, which some applications depend on.

Many of our other OS components have been rewritten to avoid assuming the page size and to optimize for larger page size when available.

Filesystems

For performant operation, file system block size must match the page size. EROFS and F2FS file systems have been made 16 KB compatible, as has the UFS storage layer.

On 4 KB systems, ELF executable file size increases due to additional padding added for 16 KB alignment (-Wl,-z,max-page-size=16384 option), but several optimizations help us avoid this cost.

  1. Sparse read-only file systems ensure that zero pages created for additional padding for 16 KB alignment are not written to disk. For example, EROFS knows a certain range of a file is zero filled, and it will not need to do any IO if this part of the file is accessed.
  2. Read-writeable file systems handle zero pages on a case-by-case basis. For example, In Android 15, for files installed as part of applications PackageManager reclaims this space.

Memory Management

  1. The Linux page cache has been modified not to read ahead for these extra padding spaces, thereby saving unnecessary memory load.
  2. These pages are blank padding, and programs never read this. It’s the space in-between usable parts of the program, purely for alignment reasons.

Linux Kernel

The Linux kernel is deeply tied to a specific page size, so we must choose which page size to use when building the kernel, while the rest of the operating system remains the same.

Android Applications

All applications with native code or dependencies need to be recompiled for compatibility with 16 KB page size devices.

Since most native code within Android applications and SDKs have been built with 4 KB page size in mind, they need to be re-aligned to 16 KB so the binaries are compatible with both 4 KB and 16 KB devices. For most applications and SDKs, this is a 2 step process:

  1. Rebuild the native code with 16 KB alignment.
  2. Test and fix on a 16 KB device/emulator in case there are hardcode assumptions about page size.

Please see our developer documentation for more information.

NOTE: If you are an SDK or tools developer, you should add 16 KB support as soon as possible so that applications can work on 16 KB using your SDK or tools.

Developing for 16 KB devices

There are no production Android devices available today or expected for the Android 15 release that support a 16 KB page size. In order to fix this problem, we are taking steps to work with our partners to make a developer option available on existing devices. This developer option is meant for application development and testing. We are also making a 16 KB emulator target available for developers in Android Studio.

16 KB Developer option on device

In Android 15, we implemented a developer option that lets users switch between 16 KB and 4 KB page size on the device in order to test their application with either of the page sizes. This option is available on Pixel 8 and Pixel 8 Pro starting in the Android 15 QPR1 Beta, and we're collaborating closely with SoC and OEM partners to enable the option on additional devices soon.

When built for 16 KB pages, the same binary will work with 4 KB and 16 KB devices, however the Linux kernel has to be separate. In order to solve this problem, we’ve added a way to include an extra kernel you can switch to as a developer option. Incrementally compressed, with one copy for each page size and takes ~12-16 MB of space on disk.

Using the 16 KB developer option will require wiping the device once and an unlocked bootloader. Following flashing, developers will be able to switch between a 4 KB and 16 KB mode by toggling the developer option over a reboot.

If you are a device manufacturer or SoC developer, see our instructions on how to enable and use this.

16 KB on x86_64 desktops

While 16 KB pages are an ARM-only feature, we recognize that many developers are using emulators on x86_64 hardware. In order to bridge this gap for developers, we’ve added support to emulate 16 KB page size for applications on x86_64 emulators. In this mode, the Kernel runs in 4 KB mode, but all addresses exposed to applications are aligned to 16 KB, and arguments to function calls such as mmap(...MAP_FIXED...) are verified to be 16 KB aligned.

To get started, you can download and run the 16 KB pages emulator inside the Android Studio SDK manager. This way, even if you don’t have access to ARM hardware, you can still ensure your applications will work with 16 KB page size.

Future

In this post, we’ve discussed the technical details of how we are restructuring memory in Android to get faster, more performant devices. Android 15 and AOSP work with 16 KB pages, and devices can now implement 16 KB pages as a development option. This required changes from the bottom to the top of the operating system, in our development tooling, and throughout the Android ecosystem.

We are looking forward to application and SDK developers now to take advantage of these options and prepare for more performant and efficient Android devices in near future.

联系我们 contact @ memedata.com