自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
更快获取存储统计信息
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在早期版本的 Android 中,系统会遍历特定应用拥有的所有文件以测量磁盘使用情况。此手动测量过程可能需要几分钟的计算时间,然后才能在“设置”中向用户显示结果。
此外,清除缓存数据文件的内部算法仅查看所有应用的修改时间。这使得恶意应用可以通过将修改时间设置在遥远的未来以使其不当地拥有高于其他应用的优先级,从而降低整体用户体验。
为了提升这些体验,Android 8.0 会询问是否利用 ext4 文件系统的“配额”支持来几乎即时地返回磁盘使用情况统计信息。此配额功能还可以防止任何单个应用使用超过 90% 的磁盘可用空间或 50% 的索引节点,从而提高系统的稳定性。
实现
配额功能是 installd
默认实现的一部分。在特定文件系统上启用配额功能后,installd
会自动使用该功能。如果在所测量的块存储设备上未启用或不支持配额功能,则系统将自动且透明地恢复手动计算方式。
如需在特定块存储设备上启用配额支持,请执行以下操作:
- 启用
CONFIG_QUOTA
、CONFIG_QFMT_V2
和 CONFIG_QUOTACTL
内核选项。
- 将
quota
选项添加到 fstab 文件中的 userdata 分区:
/dev/block/platform/soc/624000.ufshc/by-name/userdata /data
ext4 noatime,nosuid,nodev,barrier=1,noauto_da_alloc
latemount,wait,check,formattable,fileencryption=ice,quota
您可以在现有设备上安全地启用或停用 fstab
选项。在更改 fstab
选项后的第一次启动过程中,fsmgr
会强制执行 fsck
传递以更新所有配额数据结构,这可能会导致首次启动时间稍长。后续启动不会受到影响。
配额支持仅在 ext4 和 Linux 3.18 或更高版本上进行了测试。如果在其他文件系统或者较旧的内核版本上启用,设备制造商将负责测试和检查统计信息的正确性。
不需要特殊硬件支持。
验证
StorageHostTest
下包含 CTS 测试,它们可使用用于测量磁盘使用情况的公共 API。无论是否启用了配额支持,这些 API 都应返回正确的值。
调试
测试应用通过为空间大小使用唯一的质数来仔细分配磁盘可用空间区域。调试这些测试时,请使用此质数来确定导致任何差异的原因。例如,如果增量为 11MB 的测试失败,请检查 Utils.useSpace()
方法以查看 11MB blob 是否存储在 getExternalCacheDir()
中。
还有一些可能对调试有用的内部测试,但它们可能需要停用安全检查才能通过:
runtest -x frameworks/base/services/tests/servicestests/ \
src/com/android/server/pm/InstallerTest.java
adb shell /data/nativetest64/installd_utils_test/installd_utils_test
adb shell /data/nativetest64/installd_cache_test/installd_cache_test
adb shell /data/nativetest64/installd_service_test/installd_service_test
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Faster storage statistics\n\nIn earlier versions of Android, the system traversed all files owned by a\nparticular app to measure disk usage. This manual measurement could take minutes\nto compute before displaying the results to users in Settings.\n\n\nIn addition, the internal algorithm to clear cached data files only looked at\nmodified time across all apps. This allowed malicious apps to degrade the\noverall user experience by setting modified times far in the future to unfairly\nfavor themselves over other apps.\n\n\nTo improve these experiences, Android 8.0 offers to leverage the ext4 filesystem's\n\"quota\" support to return disk usage statistics almost instantly. This quota\nfeature also improves system stability by preventing any single app from using\nmore than 90% of disk space or 50% of inodes.\n\nImplementation\n--------------\n\n\nThe quota feature is part of the default implementation of `installd`.\n`installd` automatically uses the quota feature when enabled on a\nparticular filesystem. The system automatically and transparently resumes\nmanual calculation when the quota feature isn't enabled or supported on the\nblock device being measured.\n\n\nTo enable quota support on a particular block device:\n\n1. Enable the `CONFIG_QUOTA`, `CONFIG_QFMT_V2`, and `CONFIG_QUOTACTL` kernel options.\n2. Add the `quota` option to your userdata partition in your fstab file: \n\n ```\n /dev/block/platform/soc/624000.ufshc/by-name/userdata /data\n ext4 noatime,nosuid,nodev,barrier=1,noauto_da_alloc\n latemount,wait,check,formattable,fileencryption=ice,quota\n ```\n\n\nThe `fstab` option can safely be enabled or disabled on existing\ndevices. During the first boot after changing the `fstab` option,\n`fsmgr` forces an `fsck` pass to update all quota data\nstructures, which may cause that first boot to take slightly longer. Subsequent\nboots will not be affected.\n\n\nQuota support has only been tested on ext4 and Linux 3.18 or higher. If enabling\non other filesystems, or on older kernel versions, device manufacturers are\nresponsible for testing and vetting for statistics correctness.\n\n\nNo special hardware support is required.\n\nValidation\n----------\n\n\nThere are CTS tests under `StorageHostTest`, which exercise public\nAPIs for measuring disk usage. These APIs are expected to return correct values\nregardless of quota support being enabled or disabled.\n\n### Debugging\n\n\nThe test app carefully allocates disk space regions using unique prime numbers\nfor the size. When debugging these tests, use this to determine the cause of any\ndiscrepancies. For example, if a test fails with a delta of 11MB, examine the\n`Utils.useSpace()` method to see that the 11MB blob was stored in\n`getExternalCacheDir()`.\n\n\nThere are also some internal tests that may be useful for debugging, but they\nmay require disabling security checks to pass: \n\n runtest -x frameworks/base/services/tests/servicestests/ \\\n src/com/android/server/pm/InstallerTest.java\n adb shell /data/nativetest64/installd_utils_test/installd_utils_test\n adb shell /data/nativetest64/installd_cache_test/installd_cache_test\n adb shell /data/nativetest64/installd_service_test/installd_service_test"]]