From 9c54268dce4c82f066e24dfc40803192f537e35f Mon Sep 17 00:00:00 2001 From: Jin Qian Date: Fri, 28 Jul 2017 18:45:59 -0700 Subject: [PATCH] storaged: call getNamesForUids to get uid names Calls getNamesForUids when one of the two conditions are true. 1. entries for new uids are reported from io stats 2. previous getNamesForUids failed Bug: 62805090 Merged-In: I120b81e1857b2aa0a90d0fb85c8d749e985df78e Change-Id: I120b81e1857b2aa0a90d0fb85c8d749e985df78e --- storaged/Android.mk | 1 - storaged/storaged_uid_monitor.cpp | 70 +++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/storaged/Android.mk b/storaged/Android.mk index 5e6a3c0a1..a1abe0fd1 100644 --- a/storaged/Android.mk +++ b/storaged/Android.mk @@ -9,7 +9,6 @@ LIBSTORAGED_SHARED_LIBRARIES := \ libcutils \ liblog \ libsysutils \ - libpackagelistparser \ libbatteryservice \ include $(CLEAR_VARS) diff --git a/storaged/storaged_uid_monitor.cpp b/storaged/storaged_uid_monitor.cpp index dd398b5ca..dd8bdd6e8 100644 --- a/storaged/storaged_uid_monitor.cpp +++ b/storaged/storaged_uid_monitor.cpp @@ -22,33 +22,24 @@ #include #include +#include #include #include #include #include #include #include +#include #include -#include #include "storaged.h" #include "storaged_uid_monitor.h" using namespace android; using namespace android::base; +using namespace android::content::pm; -static bool packagelist_parse_cb(pkg_info* info, void* userdata) -{ - std::unordered_map* uids = - reinterpret_cast*>(userdata); - - if (uids->find(info->uid) != uids->end()) { - (*uids)[info->uid].name = info->name; - } - - packagelist_free(info); - return true; -} +static bool refresh_uid_names; std::unordered_map uid_monitor::get_uid_io_stats() { @@ -56,6 +47,38 @@ std::unordered_map uid_monitor::get_uid_io_stats() return get_uid_io_stats_locked(); }; +static void get_uid_names(const vector& uids, const vector& uid_names) +{ + sp sm = defaultServiceManager(); + if (sm == NULL) { + LOG_TO(SYSTEM, ERROR) << "defaultServiceManager failed"; + return; + } + + sp binder = sm->getService(String16("package_native")); + if (binder == NULL) { + LOG_TO(SYSTEM, ERROR) << "getService package_native failed"; + return; + } + + sp package_mgr = interface_cast(binder); + std::vector names; + binder::Status status = package_mgr->getNamesForUids(uids, &names); + if (!status.isOk()) { + LOG_TO(SYSTEM, ERROR) << "package_native::getNamesForUids failed: " + << status.exceptionMessage(); + return; + } + + for (uint32_t i = 0; i < uid_names.size(); i++) { + if (!names[i].empty()) { + *uid_names[i] = names[i]; + } + } + + refresh_uid_names = false; +} + std::unordered_map uid_monitor::get_uid_io_stats_locked() { std::unordered_map uid_io_stats; @@ -67,7 +90,8 @@ std::unordered_map uid_monitor::get_uid_io_stats_lock std::vector io_stats = Split(buffer, "\n"); struct uid_info u; - bool refresh_uid = false; + vector uids; + vector uid_names; for (uint32_t i = 0; i < io_stats.size(); i++) { if (io_stats[i].empty()) { @@ -91,17 +115,19 @@ std::unordered_map uid_monitor::get_uid_io_stats_lock continue; } - if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) { - refresh_uid = true; - u.name = std::to_string(u.uid); - } else { - u.name = last_uid_io_stats[u.uid].name; - } uid_io_stats[u.uid] = u; + uid_io_stats[u.uid].name = std::to_string(u.uid); + uids.push_back(u.uid); + uid_names.push_back(&uid_io_stats[u.uid].name); + if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) { + refresh_uid_names = true; + } else { + uid_io_stats[u.uid].name = last_uid_io_stats[u.uid].name; + } } - if (refresh_uid) { - packagelist_parse(packagelist_parse_cb, &uid_io_stats); + if (!uids.empty() && refresh_uid_names) { + get_uid_names(uids, uid_names); } return uid_io_stats;