From 9c160b61fe9e0b464c1ad30d31eb094b9f0a1a33 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Wed, 13 Jan 2021 15:12:28 -0800 Subject: [PATCH] Refactor common implementation for health storage HAL. Create a new library, libhealth_storage_impl_common, that's useful for common health storage HAL implementation. Test: TH Change-Id: I07c51d0b9c2e2262bf327dc3deabc7b41c89355d --- health/storage/1.0/default/Android.bp | 1 + health/storage/1.0/default/Storage.cpp | 90 +------------ health/storage/impl_common/Android.bp | 47 +++++++ health/storage/impl_common/impl_common.cpp | 118 ++++++++++++++++++ .../include/health-storage-impl/common.h | 31 +++++ 5 files changed, 200 insertions(+), 87 deletions(-) create mode 100644 health/storage/impl_common/Android.bp create mode 100644 health/storage/impl_common/impl_common.cpp create mode 100644 health/storage/impl_common/include/health-storage-impl/common.h diff --git a/health/storage/1.0/default/Android.bp b/health/storage/1.0/default/Android.bp index 3156dfef1c..3834244cea 100644 --- a/health/storage/1.0/default/Android.bp +++ b/health/storage/1.0/default/Android.bp @@ -38,6 +38,7 @@ cc_binary { ], static_libs: [ + "libhealth_storage_impl_common", "libfstab", ], diff --git a/health/storage/1.0/default/Storage.cpp b/health/storage/1.0/default/Storage.cpp index 561deaaed6..02b6a3dbf0 100644 --- a/health/storage/1.0/default/Storage.cpp +++ b/health/storage/1.0/default/Storage.cpp @@ -18,11 +18,8 @@ #include -#include -#include #include -#include -#include +#include namespace android { namespace hardware { @@ -31,69 +28,9 @@ namespace storage { namespace V1_0 { namespace implementation { -using base::ReadFileToString; -using base::Timer; -using base::Trim; -using base::WriteStringToFd; -using base::WriteStringToFile; -using fs_mgr::Fstab; -using fs_mgr::ReadDefaultFstab; - -std::string getGarbageCollectPath() { - Fstab fstab; - ReadDefaultFstab(&fstab); - - for (const auto& entry : fstab) { - if (!entry.sysfs_path.empty()) { - return entry.sysfs_path + "/manual_gc"; - } - } - - return ""; -} - Return Storage::garbageCollect(uint64_t timeoutSeconds, const sp& cb) { - Result result = Result::SUCCESS; - std::string path = getGarbageCollectPath(); - - if (path.empty()) { - LOG(WARNING) << "Cannot find Dev GC path"; - result = Result::UNKNOWN_ERROR; - } else { - Timer timer; - LOG(INFO) << "Start Dev GC on " << path; - while (1) { - std::string require; - if (!ReadFileToString(path, &require)) { - PLOG(WARNING) << "Reading manual_gc failed in " << path; - result = Result::IO_ERROR; - break; - } - require = Trim(require); - if (require == "" || require == "off" || require == "disabled") { - LOG(DEBUG) << "No more to do Dev GC"; - break; - } - LOG(DEBUG) << "Trigger Dev GC on " << path; - if (!WriteStringToFile("1", path)) { - PLOG(WARNING) << "Start Dev GC failed on " << path; - result = Result::IO_ERROR; - break; - } - if (timer.duration() >= std::chrono::seconds(timeoutSeconds)) { - LOG(WARNING) << "Dev GC timeout"; - // Timeout is not treated as an error. Try next time. - break; - } - sleep(2); - } - LOG(INFO) << "Stop Dev GC on " << path; - if (!WriteStringToFile("0", path)) { - PLOG(WARNING) << "Stop Dev GC failed on " << path; - result = Result::IO_ERROR; - } - } + Result result = GarbageCollect(timeoutSeconds); if (cb != nullptr) { auto ret = cb->onFinish(result); @@ -110,28 +47,7 @@ Return Storage::debug(const hidl_handle& handle, const hidl_vecdata[0]; - std::stringstream output; - - std::string path = getGarbageCollectPath(); - if (path.empty()) { - output << "Cannot find Dev GC path"; - } else { - std::string require; - - if (ReadFileToString(path, &require)) { - output << path << ":" << require << std::endl; - } - - if (WriteStringToFile("0", path)) { - output << "stop success" << std::endl; - } - } - - if (!WriteStringToFd(output.str(), fd)) { - PLOG(WARNING) << "debug: cannot write to fd"; - } - - fsync(fd); + DebugDump(fd); return Void(); } diff --git a/health/storage/impl_common/Android.bp b/health/storage/impl_common/Android.bp new file mode 100644 index 0000000000..e1149c0cb7 --- /dev/null +++ b/health/storage/impl_common/Android.bp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2021 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. + */ + +// Common implementation between HIDL and AIDL HAL. +cc_library_static { + name: "libhealth_storage_impl_common", + vendor: true, + srcs: [ + "impl_common.cpp", + ], + export_include_dirs: [ + "include", + ], + + cflags: [ + "-Wall", + "-Werror", + ], + + shared_libs: [ + "libbase", + "libhidlbase", + "liblog", + "android.hardware.health.storage@1.0", + ], + + static_libs: [ + "libfstab", + ], + + export_shared_lib_headers: [ + "android.hardware.health.storage@1.0", + ], +} diff --git a/health/storage/impl_common/impl_common.cpp b/health/storage/impl_common/impl_common.cpp new file mode 100644 index 0000000000..6e753d4655 --- /dev/null +++ b/health/storage/impl_common/impl_common.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2021 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. + */ +#include + +#include +#include +#include +#include +#include + +using ::android::base::ReadFileToString; +using ::android::base::Timer; +using ::android::base::Trim; +using ::android::base::WriteStringToFd; +using ::android::base::WriteStringToFile; +using ::android::fs_mgr::Fstab; +using ::android::fs_mgr::ReadDefaultFstab; +using ::android::hardware::health::storage::V1_0::Result; + +namespace android::hardware::health::storage { + +static std::string GetGarbageCollectPath() { + Fstab fstab; + ReadDefaultFstab(&fstab); + + for (const auto& entry : fstab) { + if (!entry.sysfs_path.empty()) { + return entry.sysfs_path + "/manual_gc"; + } + } + + return ""; +} + +Result GarbageCollect(uint64_t timeout_seconds) { + std::string path = GetGarbageCollectPath(); + + if (path.empty()) { + LOG(WARNING) << "Cannot find Dev GC path"; + return Result::UNKNOWN_ERROR; + } + + Result result = Result::SUCCESS; + Timer timer; + LOG(INFO) << "Start Dev GC on " << path; + while (1) { + std::string require; + if (!ReadFileToString(path, &require)) { + PLOG(WARNING) << "Reading manual_gc failed in " << path; + result = Result::IO_ERROR; + break; + } + require = Trim(require); + if (require == "" || require == "off" || require == "disabled") { + LOG(DEBUG) << "No more to do Dev GC"; + break; + } + LOG(DEBUG) << "Trigger Dev GC on " << path; + if (!WriteStringToFile("1", path)) { + PLOG(WARNING) << "Start Dev GC failed on " << path; + result = Result::IO_ERROR; + break; + } + if (timer.duration() >= std::chrono::seconds(timeout_seconds)) { + LOG(WARNING) << "Dev GC timeout"; + // Timeout is not treated as an error. Try next time. + break; + } + sleep(2); + } + LOG(INFO) << "Stop Dev GC on " << path; + if (!WriteStringToFile("0", path)) { + PLOG(WARNING) << "Stop Dev GC failed on " << path; + result = Result::IO_ERROR; + } + + return result; +} + +void DebugDump(int fd) { + std::stringstream output; + + std::string path = GetGarbageCollectPath(); + if (path.empty()) { + output << "Cannot find Dev GC path"; + } else { + std::string require; + + if (ReadFileToString(path, &require)) { + output << path << ":" << require << std::endl; + } + + if (WriteStringToFile("0", path)) { + output << "stop success" << std::endl; + } + } + + if (!WriteStringToFd(output.str(), fd)) { + PLOG(WARNING) << "debug: cannot write to fd"; + } + + fsync(fd); +} + +} // namespace android::hardware::health::storage diff --git a/health/storage/impl_common/include/health-storage-impl/common.h b/health/storage/impl_common/include/health-storage-impl/common.h new file mode 100644 index 0000000000..c84a6a94a4 --- /dev/null +++ b/health/storage/impl_common/include/health-storage-impl/common.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021 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 +#include + +namespace android::hardware::health::storage { + +// Run debug on fd +void DebugDump(int fd); + +// Run garbage collection on GetGarbageCollectPath(). Blocks until garbage +// collect finishes or |timeout_seconds| has reached. +V1_0::Result GarbageCollect(uint64_t timeout_seconds); + +} // namespace android::hardware::health::storage