Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d3848b2 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

More robust createAppData() batching.

The perf team noticed that createAppData() is pretty slow, holding up
typical device boot timings because it loops over all installed apps
twice (once for DE, once for CE storage), making blocking installd
calls for each individual app.

There was a createAppDataBatched() method added awhile back, but it
was only wired up for multi-user initialization, and it was fragile
because any failure in the batch would leave the device in a
non-deterministic state.

This change rewrites the installd batch call to return a unique
result object for each request, allowing us to share both detailed
success (returning CE inode values) and detailed error messages,
instead of failing the entire batch request.

On the framework side, we use CompletableFuture to collect multiple
requests for batching, while also allowing "chaining" of follow-up
work, which PackageManagerService heavily relies upon.  (For example,
when a specific package fails, we might want to try destroying and
recreating its data directories.)

Bug: 163861619
Test: atest FrameworksServicesTests:com.android.server.pm
Test: atest android.appsecurity.cts.DirectBootHostTest
Change-Id: Ib903dbbbe98fa453c8a3500338824064ae64f80f
parent 751f920d
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -163,8 +163,7 @@ cc_binary {
filegroup {
    name: "installd_aidl",
    srcs: [
        "binder/android/os/IInstalld.aidl",
        "binder/android/os/storage/CrateMetadata.aidl",
        "binder/**/*.aidl",
    ],
    path: "binder",
}
+32 −27
Original line number Diff line number Diff line
@@ -420,33 +420,6 @@ static bool prepare_app_profile_dir(const std::string& packageName, int32_t appI
    return true;
}

binder::Status InstalldNativeService::createAppDataBatched(
        const std::optional<std::vector<std::optional<std::string>>>& uuids,
        const std::optional<std::vector<std::optional<std::string>>>& packageNames,
        int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
        const std::vector<std::string>& seInfos, const std::vector<int32_t>& targetSdkVersions,
        int64_t* _aidl_return) {
    ENFORCE_UID(AID_SYSTEM);
    std::lock_guard<std::recursive_mutex> lock(mLock);

    ATRACE_BEGIN("createAppDataBatched");
    binder::Status ret;
    for (size_t i = 0; i < uuids->size(); i++) {
        std::optional<std::string> packageName = packageNames->at(i);
        if (!packageName) {
            continue;
        }
        ret = createAppData(uuids->at(i), *packageName, userId, flags, appIds[i],
                seInfos[i], targetSdkVersions[i], _aidl_return);
        if (!ret.isOk()) {
            ATRACE_END();
            return ret;
        }
    }
    ATRACE_END();
    return ok();
}

binder::Status InstalldNativeService::createAppData(const std::optional<std::string>& uuid,
        const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
        const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) {
@@ -528,6 +501,38 @@ binder::Status InstalldNativeService::createAppData(const std::optional<std::str
    return ok();
}


binder::Status InstalldNativeService::createAppData(
        const android::os::CreateAppDataArgs& args,
        android::os::CreateAppDataResult* _aidl_return) {
    ENFORCE_UID(AID_SYSTEM);
    std::lock_guard<std::recursive_mutex> lock(mLock);

    int64_t ceDataInode = -1;
    auto status = createAppData(args.uuid, args.packageName, args.userId, args.flags, args.appId,
                                args.seInfo, args.targetSdkVersion, &ceDataInode);
    _aidl_return->ceDataInode = ceDataInode;
    _aidl_return->exceptionCode = status.exceptionCode();
    _aidl_return->exceptionMessage = status.exceptionMessage();
    return ok();
}

binder::Status InstalldNativeService::createAppDataBatched(
        const std::vector<android::os::CreateAppDataArgs>& args,
        std::vector<android::os::CreateAppDataResult>* _aidl_return) {
    ENFORCE_UID(AID_SYSTEM);
    std::lock_guard<std::recursive_mutex> lock(mLock);

    std::vector<android::os::CreateAppDataResult> results;
    for (auto arg : args) {
        android::os::CreateAppDataResult result;
        createAppData(arg, &result);
        results.push_back(result);
    }
    *_aidl_return = results;
    return ok();
}

binder::Status InstalldNativeService::migrateAppData(const std::optional<std::string>& uuid,
        const std::string& packageName, int32_t userId, int32_t flags) {
    ENFORCE_UID(AID_SYSTEM);
+9 −6
Original line number Diff line number Diff line
@@ -44,15 +44,18 @@ public:
            int32_t userSerial, int32_t flags);
    binder::Status destroyUserData(const std::optional<std::string>& uuid, int32_t userId,
            int32_t flags);
    binder::Status createAppDataBatched(
            const std::optional<std::vector<std::optional<std::string>>>& uuids,
            const std::optional<std::vector<std::optional<std::string>>>& packageNames,
            int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
            const std::vector<std::string>& seInfos, const std::vector<int32_t>& targetSdkVersions,
            int64_t* _aidl_return);

    binder::Status createAppData(const std::optional<std::string>& uuid,
            const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
            const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return);

    binder::Status createAppData(
            const android::os::CreateAppDataArgs& args,
            android::os::CreateAppDataResult* _aidl_return);
    binder::Status createAppDataBatched(
            const std::vector<android::os::CreateAppDataArgs>& args,
            std::vector<android::os::CreateAppDataResult>* _aidl_return);

    binder::Status restoreconAppData(const std::optional<std::string>& uuid,
            const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
            const std::string& seInfo);
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.
 */

package android.os;

/** {@hide} */
parcelable CreateAppDataArgs {
    @nullable @utf8InCpp String uuid;
    @utf8InCpp String packageName;
    int userId;
    int flags;
    int appId;
    @utf8InCpp String seInfo;
    int targetSdkVersion;
}
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.
 */

package android.os;

/** {@hide} */
parcelable CreateAppDataResult {
    long ceDataInode;
    int exceptionCode;
    @utf8InCpp String exceptionMessage;
}
Loading