【Android 性能引擎】文件碎片

整理碎片,提升整机性能。

入口:

PMS#performFstrimIfNeeded

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public void performFstrimIfNeeded() {
PackageManagerServiceUtils.enforceSystemOrRoot("Only the system can request fstrim");

// Before everything else, see whether we need to fstrim.
try {
IStorageManager sm = InstallLocationUtils.getStorageManager();
if (sm != null) {
boolean doTrim = false;
final long interval = android.provider.Settings.Global.getLong(
mContext.getContentResolver(),
android.provider.Settings.Global.FSTRIM_MANDATORY_INTERVAL,
DEFAULT_MANDATORY_FSTRIM_INTERVAL);
if (interval > 0) {
final long timeSinceLast = System.currentTimeMillis() - sm.lastMaintenance();
if (timeSinceLast > interval) {
doTrim = true;
Slog.w(TAG, "No disk maintenance in " + timeSinceLast
+ "; running immediately");
}
}
if (doTrim) {
if (!isFirstBoot()) {
if (mDexOptHelper.isDexOptDialogShown()) {
try {
ActivityManager.getService().showBootMessage(
mContext.getResources().getString(
R.string.android_upgrading_fstrim), true);
} catch (RemoteException e) {
}
}
}
sm.runMaintenance();
}
} else {
Slog.e(TAG, "storageManager service unavailable!");
}
} catch (RemoteException e) {
// Can't happen; StorageManagerService is local
}
}

StorageManagerService#runIdleMaint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void runIdleMaint(Runnable callback) {
enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
try {
// Block based checkpoint process runs fstrim. So, if checkpoint is in progress
// (first boot after OTA), We skip idle maintenance and make sure the last
// fstrim time is still updated. If file based checkpoints are used, we run
// idle maintenance (GC + fstrim) regardless of checkpoint status.
if (!needsCheckpoint() || !supportsBlockCheckpoint()) {
mVold.runIdleMaint(mNeedGC, new IVoldTaskListener.Stub() {
@Override
public void onStatus(int status, PersistableBundle extras) {
// Not currently used
}
@Override
public void onFinished(int status, PersistableBundle extras) {
if (callback != null) {
BackgroundThread.getHandler().post(callback);
}
}
});
} else {
Slog.i(TAG, "Skipping idle maintenance - block based checkpoint in progress");
}
} catch (Exception e) {
Slog.wtf(TAG, e);
}
}

Android只在手机特定场景下触发磁盘整理,未兼容到其他设备。

其他终端可在自定义场景内触发磁盘整理,提升性能,如:

  1. 屏保场景
  2. 系统升级场景
  3. 开机场景
  4. 恢复出厂场景
  5. App提供主动触发入口

测试数据:

IO读写影响:提升10~20%

应用启动影响:提升5~10%

测试环境:

芯片:MTK9669

备注:不同芯片测试数据差异较大。

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2017-2023 Jacky

所有的相遇,都是久别重逢

支付宝
微信