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

Commit d46c87c3 authored by Andy Hung's avatar Andy Hung
Browse files

psh_utils: Update ServiceSingleton code

The new code handles service death.

Flag: EXEMPT Bugfix
Test: atest service_singleton_tests
Test: power stats show when enabled, after stats service killed
Test: atest powerstats_collector_tests
Test: atest audio_powerstats_benchmark
Test: atest audio_token_benchmark
Test: atest audio_powerstatscollector_benchmark
Bug: 350114693
Change-Id: I0783b3c58ed8412b363b613127873f01ef8d6737
parent 7eca0885
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ cc_library {
        "com.android.media.audio-aconfig-cc",
        "libaudioutils",
        "libbase",
        "libbinder",
        "libbinder_ndk",
        "libcutils",
        "liblog",
+2 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
#include <aidl/android/hardware/health/IHealth.h>
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <psh_utils/ServiceSingleton.h>
#include <mediautils/ServiceSingleton.h>

using ::aidl::android::hardware::health::HealthInfo;
using ::aidl::android::hardware::health::IHealth;
@@ -26,7 +26,7 @@ using ::aidl::android::hardware::health::IHealth;
namespace android::media::psh_utils {

static auto getHealthService() {
    return getServiceSingleton<IHealth>();
    return mediautils::getService<IHealth>();
}

status_t HealthStatsDataProvider::fill(PowerStats* stat) const {
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
#include "PowerStatsProvider.h"
#include <aidl/android/hardware/power/stats/IPowerStats.h>
#include <android-base/logging.h>
#include <psh_utils/ServiceSingleton.h>
#include <mediautils/ServiceSingleton.h>
#include <unordered_map>

using ::aidl::android::hardware::power::stats::IPowerStats;
@@ -25,7 +25,7 @@ using ::aidl::android::hardware::power::stats::IPowerStats;
namespace android::media::psh_utils {

static auto getPowerStatsService() {
    return getServiceSingleton<IPowerStats>();
    return mediautils::getService<IPowerStats>();
}

status_t RailEnergyDataProvider::fill(PowerStats *stat) const {
+15 −35
Original line number Diff line number Diff line
@@ -8,10 +8,9 @@ package {
    default_applicable_licenses: ["frameworks_av_license"],
}

cc_benchmark {
    name: "audio_powerstats_benchmark",
cc_defaults {
    name: "audio_psh_utils_benchmark_defaults",

    srcs: ["audio_powerstats_benchmark.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
@@ -22,6 +21,7 @@ cc_benchmark {
    shared_libs: [
        "libaudioutils",
        "libbase",
        "libbinder",
        "libbinder_ndk",
        "libcutils",
        "liblog",
@@ -30,46 +30,26 @@ cc_benchmark {
    ],
}

cc_benchmark {
    name: "audio_powerstats_benchmark",

    defaults: ["audio_psh_utils_benchmark_defaults"],

    srcs: ["audio_powerstats_benchmark.cpp"],
}

cc_benchmark {
    name: "audio_powerstatscollector_benchmark",

    defaults: ["audio_psh_utils_benchmark_defaults"],

    srcs: ["audio_powerstatscollector_benchmark.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    static_libs: [
        "libpshutils",
    ],
    shared_libs: [
        "libaudioutils",
        "libbase",
        "libbinder_ndk",
        "libcutils",
        "liblog",
        "libmediautils",
        "libutils",
    ],
}

cc_benchmark {
    name: "audio_token_benchmark",

    defaults: ["audio_psh_utils_benchmark_defaults"],

    srcs: ["audio_token_benchmark.cpp"],
    cflags: [
        "-Wall",
        "-Werror",
    ],
    static_libs: [
        "libpshutils",
    ],
    shared_libs: [
        "libaudioutils",
        "libbase",
        "libbinder_ndk",
        "libcutils",
        "liblog",
        "libmediautils",
        "libutils",
    ],
}
+0 −67
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.
 */

#pragma once

#include <android/binder_auto_utils.h>
#include <android/binder_manager.h>
#include <android-base/thread_annotations.h>
#include <mutex>
#include <utils/Log.h>
#include <utils/Timers.h>

namespace android::media::psh_utils {

struct DefaultServiceTraits {
    static constexpr int64_t kThresholdRetryNs = 1'000'000'000;
    static constexpr int64_t kMaxRetries = 5;
    static constexpr const char* kServiceVersion = "/default";
    static constexpr bool kShowLog = true;
};

template<typename Service, typename ServiceTraits = DefaultServiceTraits>
std::shared_ptr<Service> getServiceSingleton() {
    [[clang::no_destroy]] static constinit std::mutex m;
    [[clang::no_destroy]] static constinit std::shared_ptr<Service> service GUARDED_BY(m);
    static int64_t nextTryNs GUARDED_BY(m) = 0;
    static int64_t tries GUARDED_BY(m) = 0;

    std::lock_guard l(m);
    if (service
            || tries > ServiceTraits::kMaxRetries  // try too many times
            || systemTime(SYSTEM_TIME_BOOTTIME) < nextTryNs) {  // try too frequently.
        return service;
    }

    const auto serviceName = std::string(Service::descriptor)
            .append(ServiceTraits::kServiceVersion);
    service = Service::fromBinder(
            ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str())));

    if (!service) {
        // If failed, set a time limit before retry.
        // No need to log an error, it is already done.
        nextTryNs = systemTime(SYSTEM_TIME_BOOTTIME) + ServiceTraits::kThresholdRetryNs;
        ALOGV_IF(ServiceTraits::kShowLog, "service:%s  retries:%lld of %lld  nextTryNs:%lld",
                Service::descriptor, (long long)tries,
                (long long)kMaxRetries, (long long)nextTryNs);
        ++tries;
    }

    return service;
}

} // namespace android::media::psh_utils
Loading