blob: 2cf4883b6eab1892f2b32909793d8642520386ea [file] [log] [blame]
<html devsite><head>
<title>更快地获得存储统计信息</title>
<meta name="project_path" value="/_project.yaml"/>
<meta name="book_path" value="/_book.yaml"/>
</head>
<body>
<!--
Copyright 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<p>
在早期版本的 Android 中,系统会遍历特定应用拥有的所有文件以测量磁盘使用情况。此手动测量过程可能需要几分钟的计算时间,然后才能在“设置”中向用户显示结果。
</p>
<p>
此外,清除缓存数据文件的内部算法仅查看所有应用的修改时间。这使得恶意应用可以通过将修改时间设置在遥远的未来以使其不当地拥有高于其他应用的优先级,从而降低整体用户体验。
</p>
<p>
为了提升这些体验,Android 8.0 会询问是否利用 ext4 文件系统的“配额”支持来几乎即时地返回磁盘使用情况统计信息。此配额功能还可以防止任何单个应用使用超过 90% 的磁盘空间或 50% 的索引节点,从而提高系统的稳定性。
</p>
<h2 id="implementation">实现</h2>
<p>
配额功能是 <code>installd</code> 默认实现的一部分。
在特定文件系统上启用配额功能后,<code>installd</code> 会自动使用该功能。如果在所测量的块设备上未启用或不支持配额功能,则系统将自动且透明地恢复手动计算方式。
</p>
<p>
要在特定块设备上启用配额支持,请执行以下操作:
</p>
<ol>
<li>启用 <code>CONFIG_QUOTA</code><code>CONFIG_QFMT_V2</code><code>CONFIG_QUOTACTL</code> 内核选项。</li>
<li><code>quota</code> 选项添加到 fstab 文件中的 userdata 分区:
<pre>
/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<strong>,quota</strong></pre>
</li>
</ol>
<p>
您可以在现有设备上安全地启用或停用 <code>fstab</code> 选项。在更改 <code>fstab</code> 选项后的第一次启动过程中,<code>fsmgr</code> 会强制执行 <code>fsck</code> 传递以更新所有配额数据结构,这可能会导致首次启动时间稍长。后续启动不会受到影响。
</p>
<p>
配额支持仅在 ext4 和 Linux 3.18 或更高版本上进行了测试。如果在其他文件系统或者较旧的内核版本上启用,设备制造商将负责测试和检查统计信息的正确性。
</p>
<p>
不需要特殊硬件支持。
</p>
<h2 id="validation">验证</h2>
<p>
<code>StorageHostTest</code> 下包含 CTS 测试,它们可使用用于测量磁盘使用情况的公共 API。无论是否启用了配额支持,这些 API 都应返回正确的值。
</p>
<h3 id="debugging">调试</h3>
<p>
测试应用通过为空间大小使用唯一的质数来仔细分配磁盘空间区域。调试这些测试时,请使用此质数来确定任何差异的原因。例如,如果增量为 11MB 的测试失败了,请检查 <code>Utils.useSpace()</code> 方法以查看 11MB blob 是否存储在 <code>getExternalCacheDir()</code> 中。
</p>
<p>
还有一些可能对调试有用的内部测试,但它们可能需要停用安全检查才能通过:
</p>
<pre class="devsite-click-to-copy">
<code class="devsite-terminal">runtest -x frameworks/base/services/tests/servicestests/ \
src/com/android/server/pm/InstallerTest.java</code>
<code class="devsite-terminal">adb shell /data/nativetest64/installd_utils_test/installd_utils_test</code>
<code class="devsite-terminal">adb shell /data/nativetest64/installd_cache_test/installd_cache_test</code>
<code class="devsite-terminal">adb shell /data/nativetest64/installd_service_test/installd_service_test</code>
</pre>
</body></html>