summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJin Qian2017-07-28 20:45:59 -0500
committerJin Qian2017-08-08 15:59:03 -0500
commit9c54268dce4c82f066e24dfc40803192f537e35f (patch)
tree16e2d3922f8fa6e2cb7c7013c721f16cd8cae4da /storaged
parent1146fa1379c2e16b61edd3b156c33b36bc5050bb (diff)
downloadplatform-system-core-9c54268dce4c82f066e24dfc40803192f537e35f.tar.gz
platform-system-core-9c54268dce4c82f066e24dfc40803192f537e35f.tar.xz
platform-system-core-9c54268dce4c82f066e24dfc40803192f537e35f.zip
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
Diffstat (limited to 'storaged')
-rw-r--r--storaged/Android.mk1
-rw-r--r--storaged/storaged_uid_monitor.cpp66
2 files changed, 46 insertions, 21 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 := \
9 libcutils \ 9 libcutils \
10 liblog \ 10 liblog \
11 libsysutils \ 11 libsysutils \
12 libpackagelistparser \
13 libbatteryservice \ 12 libbatteryservice \
14 13
15include $(CLEAR_VARS) 14include $(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 @@
22#include <string> 22#include <string>
23#include <unordered_map> 23#include <unordered_map>
24 24
25#include <android/content/pm/IPackageManagerNative.h>
25#include <android-base/file.h> 26#include <android-base/file.h>
26#include <android-base/logging.h> 27#include <android-base/logging.h>
27#include <android-base/macros.h> 28#include <android-base/macros.h>
28#include <android-base/parseint.h> 29#include <android-base/parseint.h>
29#include <android-base/strings.h> 30#include <android-base/strings.h>
30#include <android-base/stringprintf.h> 31#include <android-base/stringprintf.h>
32#include <binder/IServiceManager.h>
31#include <log/log_event_list.h> 33#include <log/log_event_list.h>
32#include <packagelistparser/packagelistparser.h>
33 34
34#include "storaged.h" 35#include "storaged.h"
35#include "storaged_uid_monitor.h" 36#include "storaged_uid_monitor.h"
36 37
37using namespace android; 38using namespace android;
38using namespace android::base; 39using namespace android::base;
40using namespace android::content::pm;
39 41
40static bool packagelist_parse_cb(pkg_info* info, void* userdata) 42static bool refresh_uid_names;
41{
42 std::unordered_map<uint32_t, struct uid_info>* uids =
43 reinterpret_cast<std::unordered_map<uint32_t, struct uid_info>*>(userdata);
44
45 if (uids->find(info->uid) != uids->end()) {
46 (*uids)[info->uid].name = info->name;
47 }
48
49 packagelist_free(info);
50 return true;
51}
52 43
53std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats() 44std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats()
54{ 45{
@@ -56,6 +47,38 @@ std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats()
56 return get_uid_io_stats_locked(); 47 return get_uid_io_stats_locked();
57}; 48};
58 49
50static void get_uid_names(const vector<int>& uids, const vector<std::string*>& uid_names)
51{
52 sp<IServiceManager> sm = defaultServiceManager();
53 if (sm == NULL) {
54 LOG_TO(SYSTEM, ERROR) << "defaultServiceManager failed";
55 return;
56 }
57
58 sp<IBinder> binder = sm->getService(String16("package_native"));
59 if (binder == NULL) {
60 LOG_TO(SYSTEM, ERROR) << "getService package_native failed";
61 return;
62 }
63
64 sp<IPackageManagerNative> package_mgr = interface_cast<IPackageManagerNative>(binder);
65 std::vector<std::string> names;
66 binder::Status status = package_mgr->getNamesForUids(uids, &names);
67 if (!status.isOk()) {
68 LOG_TO(SYSTEM, ERROR) << "package_native::getNamesForUids failed: "
69 << status.exceptionMessage();
70 return;
71 }
72
73 for (uint32_t i = 0; i < uid_names.size(); i++) {
74 if (!names[i].empty()) {
75 *uid_names[i] = names[i];
76 }
77 }
78
79 refresh_uid_names = false;
80}
81
59std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats_locked() 82std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats_locked()
60{ 83{
61 std::unordered_map<uint32_t, struct uid_info> uid_io_stats; 84 std::unordered_map<uint32_t, struct uid_info> uid_io_stats;
@@ -67,7 +90,8 @@ std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats_lock
67 90
68 std::vector<std::string> io_stats = Split(buffer, "\n"); 91 std::vector<std::string> io_stats = Split(buffer, "\n");
69 struct uid_info u; 92 struct uid_info u;
70 bool refresh_uid = false; 93 vector<int> uids;
94 vector<std::string*> uid_names;
71 95
72 for (uint32_t i = 0; i < io_stats.size(); i++) { 96 for (uint32_t i = 0; i < io_stats.size(); i++) {
73 if (io_stats[i].empty()) { 97 if (io_stats[i].empty()) {
@@ -91,17 +115,19 @@ std::unordered_map<uint32_t, struct uid_info> uid_monitor::get_uid_io_stats_lock
91 continue; 115 continue;
92 } 116 }
93 117
118 uid_io_stats[u.uid] = u;
119 uid_io_stats[u.uid].name = std::to_string(u.uid);
120 uids.push_back(u.uid);
121 uid_names.push_back(&uid_io_stats[u.uid].name);
94 if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) { 122 if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) {
95 refresh_uid = true; 123 refresh_uid_names = true;
96 u.name = std::to_string(u.uid);
97 } else { 124 } else {
98 u.name = last_uid_io_stats[u.uid].name; 125 uid_io_stats[u.uid].name = last_uid_io_stats[u.uid].name;
99 } 126 }
100 uid_io_stats[u.uid] = u;
101 } 127 }
102 128
103 if (refresh_uid) { 129 if (!uids.empty() && refresh_uid_names) {
104 packagelist_parse(packagelist_parse_cb, &uid_io_stats); 130 get_uid_names(uids, uid_names);
105 } 131 }
106 132
107 return uid_io_stats; 133 return uid_io_stats;