2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
ストレージ統計情報の高速化
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
以前のバージョンの Android では、ディスク使用量を測定する際に、システムが特定のアプリに帰属するすべてのファイルを走査していました。このような手動での測定では、計算が完了し設定画面でユーザーに結果が表示されるまでに数分を要する場合がありました。
また、キャッシュされたデータファイルを消去する内部アルゴリズムでは、すべてのアプリの変更時刻のみを確認していました。したがって、悪意のあるアプリが変更時刻を大幅に先の時刻に設定し、公平性を損なう手法により他のアプリより有利な状況を得ることで、ユーザー エクスペリエンスが全体的に低下するおそれがありました。
このような事態を改善するため、Android 8.0 では、ext4 ファイルシステムでサポートされている割り当て機能を利用して、ディスク使用量の統計情報がほぼ即座に返されるようにしています。また、この割り当て機能では、1 つのアプリがディスク容量の 90% 超または i ノードの 50% 超を使用しないようにすることで、システムの安定性も改善されます。
実装
割り当て機能は、installd
のデフォルト実装に含まれています。installd
は、特定のファイルシステムで有効にされると、自動的に割り当て機能を使用します。割り当て機能が有効になっていないか、測定対象のブロック デバイスでサポートされていない場合、システムは自動的、透過的に手動計算を再開します。
特定のブロック デバイスで割り当てのサポートを有効にするには:
- カーネル オプション
CONFIG_QUOTA
、CONFIG_QFMT_V2
、CONFIG_QUOTACTL
を有効にします。
- fstab ファイルの userdata パーティションに
quota
オプションを追加します。
/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 以降のみです。他のファイルシステムまたは古いカーネル バージョンで割り当てを有効にする場合は、デバイス メーカーで統計情報の正確性を厳密にテストする必要があります。
特別なハードウェア サポートは必要ありません。
検証
各種の CTS テストが StorageHostTest
に用意されており、公開の API を実行してディスク使用量を測定できます。これらの API は、割り当てのサポートが有効か無効かにかかわらず、適切な値を返すことが期待されます。
デバッグ
テストアプリでは、サイズに一意の素数を使用してディスク領域を慎重に割り当てます。そして、テストのデバッグを行う際には、この素数の割り当てを利用して差異の原因を特定します。たとえば、テストが失敗して 11 MB の差分が生じた場合、Utils.useSpace()
メソッドを使用して、11 MB の 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 および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],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"]]