diff options
author | Kalesh Singh <kaleshsingh@google.com> | 2020-12-07 01:51:39 -0500 |
---|---|---|
committer | Kalesh Singh <kaleshsingh@google.com> | 2020-12-22 20:49:10 +0000 |
commit | 2e12c78fbca041d5262f019c62dfb53168c45f8c (patch) | |
tree | 7a75a66773a0fa7e5af32778e67aaf3c76efd723 /memtrack.cpp | |
parent | 762a69d031bb23baa0472dd499e253d12c03a62c (diff) | |
download | libmemtrack-2e12c78fbca041d5262f019c62dfb53168c45f8c.tar.gz |
Reland: libmemtrack: Add support for AIDL memtrack HAL
________________
Regarding the issues caused by the reverted version of these changes.
Root cause of the reported issues is that ServiceManager cannot get
the IMemtrack AIDL service, and getService() will busy loop for 5 seconds
trying to get the service.
```
12-22 12:22:22.501 1827 10885 W ServiceManager: Service android.hardware.memtrack.IMemtrack/default didn't start. Returning NULL
12-22 12:22:22.501 1827 10885 I ServiceManager: Waiting for service 'android.hardware.memtrack.IMemtrack/default' on '/dev/binder'...
```
Also because the results of this getService() was not cached, we will
attempt to get this service each time there is a new request from
libmemtrack, causing another 5 seconds delay each time.
Fix:
We check if the service is declared before attempting to retrieve it.
We use the more efficient waitForService(), instead of getService()
which can busy loop for 5 seconds.
Cache the results of these operations so that we don’t have to retrieve
the service each time there is a request from libmemtrack.
Boot Time Regression Test
Test: adb shell logcat -b all | grep TotalBootTime
Old version of this topic: 44209ms
LKGB: 19823ms
New version of this topic: 20058ms
Cts Graphics Test
Test: atest CtsGraphicsTestCases:android.graphics.cts.BitmapTest#testWrappedHardwareBufferBitmapNotLeaking
Old version of this topic: Fails
LKGB: Passes
New version of this topic: Passes
Memoinfo Test
Test: adb shell dumpsys meminfo
Old version of this topic: `*** SERVICE 'meminfo' DUMP TIMEOUT (10000ms) EXPIRED ***`
LKGB: <Expected output>
New version of this topic: <Expected output>
Battery Saver Test
Test: Unplug Device; Tap Battery Save Icon from the pull down menu
Old version of this topic: Battery Saver Icon does not become enabled (blue highlight)
LKGB: Battery Saver Icon Enabled
New version of this topic: Battery Saver Icon Enabled
Bug: 175021432
Test: memtrack_test
Change-Id: I6ce5c454e6a5c9b718277b4b3004f600dd0f6786
Diffstat (limited to 'memtrack.cpp')
-rw-r--r-- | memtrack.cpp | 76 |
1 files changed, 73 insertions, 3 deletions
diff --git a/memtrack.cpp b/memtrack.cpp index 3fc24c8..807a5c3 100644 --- a/memtrack.cpp +++ b/memtrack.cpp @@ -14,14 +14,18 @@ * limitations under the License. */ #define LOG_TAG "memtrack" + +#include <aidl/android/hardware/memtrack/IMemtrack.h> +#include <aidl/android/hardware/memtrack/MemtrackType.h> +#include <android/binder_manager.h> #include <android/hardware/memtrack/1.0/IMemtrack.h> #include <memtrack/memtrack.h> #include <errno.h> #include <malloc.h> -#include <vector> #include <string.h> #include <mutex> +#include <vector> #include <log/log.h> @@ -33,6 +37,41 @@ using android::hardware::memtrack::V1_0::MemtrackStatus; using android::hardware::hidl_vec; using android::hardware::Return; +namespace V1_0 = android::hardware::memtrack::V1_0; +namespace V_aidl = aidl::android::hardware::memtrack; + +// Check Memtrack Flags +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SMAPS_ACCOUNTED) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SMAPS_ACCOUNTED)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SMAPS_UNACCOUNTED) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SMAPS_UNACCOUNTED)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SHARED) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SHARED)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SHARED_PSS) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SHARED_PSS)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::PRIVATE) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_PRIVATE)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SYSTEM) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SYSTEM)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::DEDICATED) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_DEDICATED)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::NONSECURE) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_NONSECURE)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackFlag::SECURE) == + static_cast<uint32_t>(V_aidl::MemtrackRecord::FLAG_SECURE)); + +// Check Memtrack Types +static_assert(static_cast<uint32_t>(V1_0::MemtrackType::OTHER) == + static_cast<uint32_t>(V_aidl::MemtrackType::OTHER)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackType::GL) == + static_cast<uint32_t>(V_aidl::MemtrackType::GL)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackType::GRAPHICS) == + static_cast<uint32_t>(V_aidl::MemtrackType::GRAPHICS)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackType::MULTIMEDIA) == + static_cast<uint32_t>(V_aidl::MemtrackType::MULTIMEDIA)); +static_assert(static_cast<uint32_t>(V1_0::MemtrackType::CAMERA) == + static_cast<uint32_t>(V_aidl::MemtrackType::CAMERA)); + struct memtrack_proc_type { MemtrackType type; std::vector<MemtrackRecord> records; @@ -44,7 +83,7 @@ struct memtrack_proc { }; //TODO(b/31632518) -static android::sp<IMemtrack> get_instance() { +static android::sp<V1_0::IMemtrack> get_hidl_instance() { static android::sp<IMemtrack> module = IMemtrack::getService(); static bool logged = false; if (module == nullptr && !logged) { @@ -54,6 +93,17 @@ static android::sp<IMemtrack> get_instance() { return module; } +static std::shared_ptr<V_aidl::IMemtrack> get_aidl_instance() { + const auto instance = std::string() + V_aidl::IMemtrack::descriptor + "/default"; + static bool declared = AServiceManager_isDeclared(instance.c_str()); + if (!declared) { + return nullptr; + } + static ndk::SpAIBinder memtrack_binder = + ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())); + return V_aidl::IMemtrack::fromBinder(memtrack_binder); +} + memtrack_proc *memtrack_proc_new(void) { return new memtrack_proc(); @@ -68,7 +118,27 @@ static int memtrack_proc_get_type(memtrack_proc_type *t, pid_t pid, MemtrackType type) { int err = 0; - android::sp<IMemtrack> memtrack = get_instance(); + + std::shared_ptr<V_aidl::IMemtrack> service= get_aidl_instance(); + if (service) { + std::vector<V_aidl::MemtrackRecord> records; + auto status = service->getMemory( + pid, static_cast<V_aidl::MemtrackType>(static_cast<uint32_t>(type)), &records); + + if (!status.isOk()) { + return -1; + } + + t->records.resize(records.size()); + for (size_t i = 0; i < records.size(); i++) { + t->records[i].sizeInBytes = records[i].sizeInBytes; + t->records[i].flags = records[i].flags; + } + + return err; + } + + android::sp<V1_0::IMemtrack> memtrack = get_hidl_instance(); if (memtrack == nullptr) return -1; |