From 8f0d45441c05e0c598ffd74a913b670495ca97e3 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Wed, 13 Jan 2021 17:10:47 -0800 Subject: [PATCH] Refactor HIDL HAL Dev GC invocation Prefix all HIDL HAL types with the letter H. Also make the invocation more generic so that we can use the AIDL implementation in the future. Test: TH Bug: 177470478 Change-Id: Iee0af9727c48c532315203c138a4996a08fcaa34 --- IdleMaint.cpp | 57 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/IdleMaint.cpp b/IdleMaint.cpp index e4a1806..466cf19 100644 --- a/IdleMaint.cpp +++ b/IdleMaint.cpp @@ -22,6 +22,7 @@ #include "model/PrivateVolume.h" #include +#include #include #include @@ -48,9 +49,9 @@ using android::base::Timer; using android::base::WriteStringToFile; using android::hardware::Return; using android::hardware::Void; -using android::hardware::health::storage::V1_0::IStorage; -using android::hardware::health::storage::V1_0::IGarbageCollectCallback; -using android::hardware::health::storage::V1_0::Result; +using HStorage = android::hardware::health::storage::V1_0::IStorage; +using HGarbageCollectCallback = android::hardware::health::storage::V1_0::IGarbageCollectCallback; +using HResult = android::hardware::health::storage::V1_0::Result; namespace android { namespace vold { @@ -300,26 +301,33 @@ static void runDevGcFstab(void) { return; } -class GcCallback : public IGarbageCollectCallback { - public: - Return onFinish(Result result) override { +enum class IDL { HIDL }; +std::ostream& operator<<(std::ostream& os, IDL idl) { + return os << "HIDL"; +} + +template +class GcCallbackImpl { + protected: + void onFinishInternal(Result result) { std::unique_lock lock(mMutex); mFinished = true; mResult = result; lock.unlock(); mCv.notify_all(); - return Void(); } + + public: void wait(uint64_t seconds) { std::unique_lock lock(mMutex); mCv.wait_for(lock, std::chrono::seconds(seconds), [this] { return mFinished; }); if (!mFinished) { - LOG(WARNING) << "Dev GC on HAL timeout"; + LOG(WARNING) << "Dev GC on " << idl << " HAL timeout"; } else if (mResult != Result::SUCCESS) { - LOG(WARNING) << "Dev GC on HAL failed with " << toString(mResult); + LOG(WARNING) << "Dev GC on " << idl << " HAL failed with " << toString(mResult); } else { - LOG(INFO) << "Dev GC on HAL successful"; + LOG(INFO) << "Dev GC on " << idl << " HAL successful"; } } @@ -330,25 +338,34 @@ class GcCallback : public IGarbageCollectCallback { Result mResult{Result::UNKNOWN_ERROR}; }; -static void runDevGcOnHal(sp service) { - LOG(DEBUG) << "Start Dev GC on HAL"; - sp cb = new GcCallback(); +class HGcCallbackImpl : public HGarbageCollectCallback, public GcCallbackImpl { + Return onFinish(HResult result) override { + onFinishInternal(result); + return Void(); + } +}; + +template +static void runDevGcOnHal(Service service, GcCallbackImpl cb, GetDescription get_description) { + LOG(DEBUG) << "Start Dev GC on " << idl << " HAL"; auto ret = service->garbageCollect(DEVGC_TIMEOUT_SEC, cb); if (!ret.isOk()) { - LOG(WARNING) << "Cannot start Dev GC on HAL: " << ret.description(); + LOG(WARNING) << "Cannot start Dev GC on " << idl + << " HAL: " << std::invoke(get_description, ret); return; } cb->wait(DEVGC_TIMEOUT_SEC); } static void runDevGc(void) { - auto service = IStorage::getService(); - if (service != nullptr) { - runDevGcOnHal(service); - } else { - // fallback to legacy code path - runDevGcFstab(); + auto hidl_service = HStorage::getService(); + if (hidl_service != nullptr) { + runDevGcOnHal(hidl_service, sp(new HGcCallbackImpl()), + &Return::description); + return; } + // fallback to legacy code path + runDevGcFstab(); } int RunIdleMaint(const android::sp& listener) { -- 2.11.0