blob: f47bf72b3c0fe1cccefcfbb83d7b0679eec8174b [file] [log] [blame]
Jin Qianbcd6e3b2016-12-28 15:43:51 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "storaged"
18
19#include <stdint.h>
20#include <time.h>
21
22#include <string>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080023#include <unordered_map>
David Anderson0026a142018-07-26 14:30:35 -070024#include <unordered_set>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080025
Jin Qian2d7bcce2017-07-28 18:45:59 -070026#include <android/content/pm/IPackageManagerNative.h>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080027#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/macros.h>
Jin Qian9b1aeae2017-03-14 11:20:02 -070030#include <android-base/parseint.h>
Jin Qiane83a6102017-03-02 16:16:54 -080031#include <android-base/strings.h>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080032#include <android-base/stringprintf.h>
Jin Qian2d7bcce2017-07-28 18:45:59 -070033#include <binder/IServiceManager.h>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080034#include <log/log_event_list.h>
Jin Qianbcd6e3b2016-12-28 15:43:51 -080035
36#include "storaged.h"
37#include "storaged_uid_monitor.h"
38
Jin Qianbcd6e3b2016-12-28 15:43:51 -080039using namespace android;
40using namespace android::base;
Jin Qian2d7bcce2017-07-28 18:45:59 -070041using namespace android::content::pm;
Jin Qianb049d182017-10-12 17:02:17 -070042using namespace android::os::storaged;
Jin Qianebf031b2017-08-14 16:41:24 -070043using namespace storaged_proto;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080044
Jin Qian65dea712017-08-29 16:48:20 -070045namespace {
46
47bool refresh_uid_names;
Jin Qian65dea712017-08-29 16:48:20 -070048const char* UID_IO_STATS_PATH = "/proc/uid_io/stats";
49
50} // namepsace
Jin Qianebf031b2017-08-14 16:41:24 -070051
Jin Qianb049d182017-10-12 17:02:17 -070052std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats()
Jin Qianbcd6e3b2016-12-28 15:43:51 -080053{
David Anderson3488d9f2018-07-26 13:20:26 -070054 Mutex::Autolock _l(uidm_mutex_);
Jin Qian5b962c62017-01-30 14:48:38 -080055 return get_uid_io_stats_locked();
56};
Jin Qianbcd6e3b2016-12-28 15:43:51 -080057
Yang Jin3906c892017-06-22 15:18:21 -070058/* return true on parse success and false on failure */
59bool uid_info::parse_uid_io_stats(std::string&& s)
60{
61 std::vector<std::string> fields = Split(s, " ");
62 if (fields.size() < 11 ||
63 !ParseUint(fields[0], &uid) ||
64 !ParseUint(fields[1], &io[FOREGROUND].rchar) ||
65 !ParseUint(fields[2], &io[FOREGROUND].wchar) ||
66 !ParseUint(fields[3], &io[FOREGROUND].read_bytes) ||
67 !ParseUint(fields[4], &io[FOREGROUND].write_bytes) ||
68 !ParseUint(fields[5], &io[BACKGROUND].rchar) ||
69 !ParseUint(fields[6], &io[BACKGROUND].wchar) ||
70 !ParseUint(fields[7], &io[BACKGROUND].read_bytes) ||
71 !ParseUint(fields[8], &io[BACKGROUND].write_bytes) ||
72 !ParseUint(fields[9], &io[FOREGROUND].fsync) ||
73 !ParseUint(fields[10], &io[BACKGROUND].fsync)) {
Tom Cherryb8c11472020-01-10 17:08:15 -080074 LOG(WARNING) << "Invalid uid I/O stats: \"" << s << "\"";
Yang Jin3906c892017-06-22 15:18:21 -070075 return false;
76 }
77 return true;
78}
79
80/* return true on parse success and false on failure */
81bool task_info::parse_task_io_stats(std::string&& s)
82{
83 std::vector<std::string> fields = Split(s, ",");
Jin Qian0e026872017-08-29 11:42:06 -070084 size_t size = fields.size();
85 if (size < 13 ||
86 !ParseInt(fields[size - 11], &pid) ||
87 !ParseUint(fields[size - 10], &io[FOREGROUND].rchar) ||
88 !ParseUint(fields[size - 9], &io[FOREGROUND].wchar) ||
89 !ParseUint(fields[size - 8], &io[FOREGROUND].read_bytes) ||
90 !ParseUint(fields[size - 7], &io[FOREGROUND].write_bytes) ||
91 !ParseUint(fields[size - 6], &io[BACKGROUND].rchar) ||
92 !ParseUint(fields[size - 5], &io[BACKGROUND].wchar) ||
93 !ParseUint(fields[size - 4], &io[BACKGROUND].read_bytes) ||
94 !ParseUint(fields[size - 3], &io[BACKGROUND].write_bytes) ||
95 !ParseUint(fields[size - 2], &io[FOREGROUND].fsync) ||
96 !ParseUint(fields[size - 1], &io[BACKGROUND].fsync)) {
Tom Cherryb8c11472020-01-10 17:08:15 -080097 LOG(WARNING) << "Invalid task I/O stats: \"" << s << "\"";
Yang Jin3906c892017-06-22 15:18:21 -070098 return false;
99 }
Jin Qian0e026872017-08-29 11:42:06 -0700100 comm = Join(std::vector<std::string>(
101 fields.begin() + 1, fields.end() - 11), ',');
Yang Jin3906c892017-06-22 15:18:21 -0700102 return true;
103}
104
105bool io_usage::is_zero() const
106{
107 for (int i = 0; i < IO_TYPES; i++) {
108 for (int j = 0; j < UID_STATS; j++) {
109 for (int k = 0; k < CHARGER_STATS; k++) {
110 if (bytes[i][j][k])
111 return false;
112 }
113 }
114 }
115 return true;
116}
117
Jin Qian65dea712017-08-29 16:48:20 -0700118namespace {
119
120void get_uid_names(const vector<int>& uids, const vector<std::string*>& uid_names)
Jin Qian2d7bcce2017-07-28 18:45:59 -0700121{
122 sp<IServiceManager> sm = defaultServiceManager();
123 if (sm == NULL) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800124 LOG(ERROR) << "defaultServiceManager failed";
Jin Qian2d7bcce2017-07-28 18:45:59 -0700125 return;
126 }
127
128 sp<IBinder> binder = sm->getService(String16("package_native"));
129 if (binder == NULL) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800130 LOG(ERROR) << "getService package_native failed";
Jin Qian2d7bcce2017-07-28 18:45:59 -0700131 return;
132 }
133
134 sp<IPackageManagerNative> package_mgr = interface_cast<IPackageManagerNative>(binder);
135 std::vector<std::string> names;
136 binder::Status status = package_mgr->getNamesForUids(uids, &names);
137 if (!status.isOk()) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800138 LOG(ERROR) << "package_native::getNamesForUids failed: " << status.exceptionMessage();
Jin Qian2d7bcce2017-07-28 18:45:59 -0700139 return;
140 }
141
142 for (uint32_t i = 0; i < uid_names.size(); i++) {
143 if (!names[i].empty()) {
144 *uid_names[i] = names[i];
145 }
146 }
147
148 refresh_uid_names = false;
149}
150
Jin Qian65dea712017-08-29 16:48:20 -0700151} // namespace
152
Jin Qianb049d182017-10-12 17:02:17 -0700153std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats_locked()
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800154{
Jin Qianb049d182017-10-12 17:02:17 -0700155 std::unordered_map<uint32_t, uid_info> uid_io_stats;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800156 std::string buffer;
Jin Qian9b1aeae2017-03-14 11:20:02 -0700157 if (!ReadFileToString(UID_IO_STATS_PATH, &buffer)) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800158 PLOG(ERROR) << UID_IO_STATS_PATH << ": ReadFileToString failed";
Jin Qian5b962c62017-01-30 14:48:38 -0800159 return uid_io_stats;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800160 }
161
Yang Jin3906c892017-06-22 15:18:21 -0700162 std::vector<std::string> io_stats = Split(std::move(buffer), "\n");
Jin Qianb049d182017-10-12 17:02:17 -0700163 uid_info u;
Jin Qian2d7bcce2017-07-28 18:45:59 -0700164 vector<int> uids;
165 vector<std::string*> uid_names;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800166
Jin Qiane83a6102017-03-02 16:16:54 -0800167 for (uint32_t i = 0; i < io_stats.size(); i++) {
168 if (io_stats[i].empty()) {
169 continue;
170 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800171
Yang Jin3906c892017-06-22 15:18:21 -0700172 if (io_stats[i].compare(0, 4, "task")) {
173 if (!u.parse_uid_io_stats(std::move(io_stats[i])))
174 continue;
Yang Jin3906c892017-06-22 15:18:21 -0700175 uid_io_stats[u.uid] = u;
Jin Qian2d7bcce2017-07-28 18:45:59 -0700176 uid_io_stats[u.uid].name = std::to_string(u.uid);
177 uids.push_back(u.uid);
178 uid_names.push_back(&uid_io_stats[u.uid].name);
David Anderson3488d9f2018-07-26 13:20:26 -0700179 if (last_uid_io_stats_.find(u.uid) == last_uid_io_stats_.end()) {
Jin Qian2d7bcce2017-07-28 18:45:59 -0700180 refresh_uid_names = true;
181 } else {
David Anderson3488d9f2018-07-26 13:20:26 -0700182 uid_io_stats[u.uid].name = last_uid_io_stats_[u.uid].name;
Jin Qian2d7bcce2017-07-28 18:45:59 -0700183 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800184 } else {
Jin Qianb049d182017-10-12 17:02:17 -0700185 task_info t;
Yang Jin3906c892017-06-22 15:18:21 -0700186 if (!t.parse_task_io_stats(std::move(io_stats[i])))
187 continue;
188 uid_io_stats[u.uid].tasks[t.pid] = t;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800189 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800190 }
191
Jin Qian2d7bcce2017-07-28 18:45:59 -0700192 if (!uids.empty() && refresh_uid_names) {
193 get_uid_names(uids, uid_names);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800194 }
195
Jin Qian5b962c62017-01-30 14:48:38 -0800196 return uid_io_stats;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800197}
198
Jin Qian65dea712017-08-29 16:48:20 -0700199namespace {
Jin Qiana2e5bd12017-01-24 16:23:13 -0800200
Jin Qian65dea712017-08-29 16:48:20 -0700201inline size_t history_size(
Jin Qianebf031b2017-08-14 16:41:24 -0700202 const std::map<uint64_t, struct uid_records>& history)
Jin Qiana2e5bd12017-01-24 16:23:13 -0800203{
Jin Qianebf031b2017-08-14 16:41:24 -0700204 size_t count = 0;
205 for (auto const& it : history) {
Jin Qian81577752017-02-21 12:09:39 -0800206 count += it.second.entries.size();
Jin Qian5b962c62017-01-30 14:48:38 -0800207 }
208 return count;
Jin Qiana2e5bd12017-01-24 16:23:13 -0800209}
210
Jin Qian65dea712017-08-29 16:48:20 -0700211} // namespace
212
Jin Qian5b962c62017-01-30 14:48:38 -0800213void uid_monitor::add_records_locked(uint64_t curr_ts)
Jin Qiana2e5bd12017-01-24 16:23:13 -0800214{
Jin Qian5b962c62017-01-30 14:48:38 -0800215 // remove records more than 5 days old
216 if (curr_ts > 5 * DAY_TO_SEC) {
David Anderson3488d9f2018-07-26 13:20:26 -0700217 auto it = io_history_.lower_bound(curr_ts - 5 * DAY_TO_SEC);
218 io_history_.erase(io_history_.begin(), it);
Jin Qian9cdfdd32017-01-31 17:33:20 -0800219 }
220
Jin Qian81577752017-02-21 12:09:39 -0800221 struct uid_records new_records;
David Anderson3488d9f2018-07-26 13:20:26 -0700222 for (const auto& p : curr_io_stats_) {
Jin Qian5b962c62017-01-30 14:48:38 -0800223 struct uid_record record = {};
224 record.name = p.first;
Yang Jin3906c892017-06-22 15:18:21 -0700225 if (!p.second.uid_ios.is_zero()) {
Jin Qian6df3bc62017-10-18 17:52:14 -0700226 record.ios.user_id = p.second.user_id;
Yang Jin3906c892017-06-22 15:18:21 -0700227 record.ios.uid_ios = p.second.uid_ios;
228 for (const auto& p_task : p.second.task_ios) {
229 if (!p_task.second.is_zero())
230 record.ios.task_ios[p_task.first] = p_task.second;
231 }
Jin Qian81577752017-02-21 12:09:39 -0800232 new_records.entries.push_back(record);
Jin Qian5b962c62017-01-30 14:48:38 -0800233 }
Jin Qian9cdfdd32017-01-31 17:33:20 -0800234 }
Jin Qian9cdfdd32017-01-31 17:33:20 -0800235
David Anderson3488d9f2018-07-26 13:20:26 -0700236 curr_io_stats_.clear();
237 new_records.start_ts = start_ts_;
238 start_ts_ = curr_ts;
Jin Qian9cdfdd32017-01-31 17:33:20 -0800239
Jin Qian81577752017-02-21 12:09:39 -0800240 if (new_records.entries.empty())
Jin Qian5b962c62017-01-30 14:48:38 -0800241 return;
242
243 // make some room for new records
David Andersondec6a882018-07-26 18:01:03 -0700244 maybe_shrink_history_for_items(new_records.entries.size());
245
246 io_history_[curr_ts] = new_records;
247}
248
249void uid_monitor::maybe_shrink_history_for_items(size_t nitems) {
250 ssize_t overflow = history_size(io_history_) + nitems - MAX_UID_RECORDS_SIZE;
David Anderson3488d9f2018-07-26 13:20:26 -0700251 while (overflow > 0 && io_history_.size() > 0) {
252 auto del_it = io_history_.begin();
Jin Qian81577752017-02-21 12:09:39 -0800253 overflow -= del_it->second.entries.size();
David Anderson3488d9f2018-07-26 13:20:26 -0700254 io_history_.erase(io_history_.begin());
Jin Qian5b962c62017-01-30 14:48:38 -0800255 }
Jin Qian5b962c62017-01-30 14:48:38 -0800256}
257
Jin Qian81577752017-02-21 12:09:39 -0800258std::map<uint64_t, struct uid_records> uid_monitor::dump(
Jin Qian94b64ef2017-11-09 15:07:18 -0800259 double hours, uint64_t threshold, bool force_report)
Jin Qian5b962c62017-01-30 14:48:38 -0800260{
Jin Qian1275b1b2017-02-06 14:02:50 -0800261 if (force_report) {
Jin Qian94b64ef2017-11-09 15:07:18 -0800262 report(nullptr);
Jin Qian1275b1b2017-02-06 14:02:50 -0800263 }
264
David Anderson3488d9f2018-07-26 13:20:26 -0700265 Mutex::Autolock _l(uidm_mutex_);
Jin Qian5b962c62017-01-30 14:48:38 -0800266
Jin Qian81577752017-02-21 12:09:39 -0800267 std::map<uint64_t, struct uid_records> dump_records;
Jin Qian5b962c62017-01-30 14:48:38 -0800268 uint64_t first_ts = 0;
269
270 if (hours != 0) {
Jin Qiandd41d6b2017-02-10 17:23:16 -0800271 first_ts = time(NULL) - hours * HOUR_TO_SEC;
Jin Qian5b962c62017-01-30 14:48:38 -0800272 }
273
David Anderson3488d9f2018-07-26 13:20:26 -0700274 for (auto it = io_history_.lower_bound(first_ts); it != io_history_.end(); ++it) {
Jin Qian81577752017-02-21 12:09:39 -0800275 const std::vector<struct uid_record>& recs = it->second.entries;
276 struct uid_records filtered;
Jin Qiane5ea17c2017-02-10 10:50:03 -0800277
278 for (const auto& rec : recs) {
Yang Jin3906c892017-06-22 15:18:21 -0700279 const io_usage& uid_usage = rec.ios.uid_ios;
280 if (uid_usage.bytes[READ][FOREGROUND][CHARGER_ON] +
281 uid_usage.bytes[READ][FOREGROUND][CHARGER_OFF] +
282 uid_usage.bytes[READ][BACKGROUND][CHARGER_ON] +
283 uid_usage.bytes[READ][BACKGROUND][CHARGER_OFF] +
284 uid_usage.bytes[WRITE][FOREGROUND][CHARGER_ON] +
285 uid_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF] +
286 uid_usage.bytes[WRITE][BACKGROUND][CHARGER_ON] +
287 uid_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF] > threshold) {
Jin Qian81577752017-02-21 12:09:39 -0800288 filtered.entries.push_back(rec);
Jin Qiane5ea17c2017-02-10 10:50:03 -0800289 }
290 }
Jin Qian81577752017-02-21 12:09:39 -0800291
292 if (filtered.entries.empty())
293 continue;
294
295 filtered.start_ts = it->second.start_ts;
Jin Qiane5ea17c2017-02-10 10:50:03 -0800296 dump_records.insert(
Jin Qian81577752017-02-21 12:09:39 -0800297 std::pair<uint64_t, struct uid_records>(it->first, filtered));
Jin Qiane5ea17c2017-02-10 10:50:03 -0800298 }
Jin Qian5b962c62017-01-30 14:48:38 -0800299
300 return dump_records;
301}
302
303void uid_monitor::update_curr_io_stats_locked()
304{
Jin Qianb049d182017-10-12 17:02:17 -0700305 std::unordered_map<uint32_t, uid_info> uid_io_stats =
Jin Qian5b962c62017-01-30 14:48:38 -0800306 get_uid_io_stats_locked();
307 if (uid_io_stats.empty()) {
308 return;
309 }
310
311 for (const auto& it : uid_io_stats) {
Jin Qianb049d182017-10-12 17:02:17 -0700312 const uid_info& uid = it.second;
David Anderson3488d9f2018-07-26 13:20:26 -0700313 if (curr_io_stats_.find(uid.name) == curr_io_stats_.end()) {
314 curr_io_stats_[uid.name] = {};
Jin Qian5b962c62017-01-30 14:48:38 -0800315 }
316
David Anderson3488d9f2018-07-26 13:20:26 -0700317 struct uid_io_usage& usage = curr_io_stats_[uid.name];
Jin Qian6df3bc62017-10-18 17:52:14 -0700318 usage.user_id = multiuser_get_user_id(uid.uid);
319
Jin Qianbaff6402017-02-16 18:34:31 -0800320 int64_t fg_rd_delta = uid.io[FOREGROUND].read_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700321 last_uid_io_stats_[uid.uid].io[FOREGROUND].read_bytes;
Jin Qianbaff6402017-02-16 18:34:31 -0800322 int64_t bg_rd_delta = uid.io[BACKGROUND].read_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700323 last_uid_io_stats_[uid.uid].io[BACKGROUND].read_bytes;
Jin Qianbaff6402017-02-16 18:34:31 -0800324 int64_t fg_wr_delta = uid.io[FOREGROUND].write_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700325 last_uid_io_stats_[uid.uid].io[FOREGROUND].write_bytes;
Jin Qianbaff6402017-02-16 18:34:31 -0800326 int64_t bg_wr_delta = uid.io[BACKGROUND].write_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700327 last_uid_io_stats_[uid.uid].io[BACKGROUND].write_bytes;
Jin Qianbaff6402017-02-16 18:34:31 -0800328
David Anderson3488d9f2018-07-26 13:20:26 -0700329 usage.uid_ios.bytes[READ][FOREGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700330 (fg_rd_delta < 0) ? 0 : fg_rd_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700331 usage.uid_ios.bytes[READ][BACKGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700332 (bg_rd_delta < 0) ? 0 : bg_rd_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700333 usage.uid_ios.bytes[WRITE][FOREGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700334 (fg_wr_delta < 0) ? 0 : fg_wr_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700335 usage.uid_ios.bytes[WRITE][BACKGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700336 (bg_wr_delta < 0) ? 0 : bg_wr_delta;
337
338 for (const auto& task_it : uid.tasks) {
Jin Qianb049d182017-10-12 17:02:17 -0700339 const task_info& task = task_it.second;
Yang Jin3906c892017-06-22 15:18:21 -0700340 const pid_t pid = task_it.first;
341 const std::string& comm = task_it.second.comm;
342 int64_t task_fg_rd_delta = task.io[FOREGROUND].read_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700343 last_uid_io_stats_[uid.uid].tasks[pid].io[FOREGROUND].read_bytes;
Yang Jin3906c892017-06-22 15:18:21 -0700344 int64_t task_bg_rd_delta = task.io[BACKGROUND].read_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700345 last_uid_io_stats_[uid.uid].tasks[pid].io[BACKGROUND].read_bytes;
Yang Jin3906c892017-06-22 15:18:21 -0700346 int64_t task_fg_wr_delta = task.io[FOREGROUND].write_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700347 last_uid_io_stats_[uid.uid].tasks[pid].io[FOREGROUND].write_bytes;
Yang Jin3906c892017-06-22 15:18:21 -0700348 int64_t task_bg_wr_delta = task.io[BACKGROUND].write_bytes -
David Anderson3488d9f2018-07-26 13:20:26 -0700349 last_uid_io_stats_[uid.uid].tasks[pid].io[BACKGROUND].write_bytes;
Yang Jin3906c892017-06-22 15:18:21 -0700350
Jin Qian6df3bc62017-10-18 17:52:14 -0700351 io_usage& task_usage = usage.task_ios[comm];
David Anderson3488d9f2018-07-26 13:20:26 -0700352 task_usage.bytes[READ][FOREGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700353 (task_fg_rd_delta < 0) ? 0 : task_fg_rd_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700354 task_usage.bytes[READ][BACKGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700355 (task_bg_rd_delta < 0) ? 0 : task_bg_rd_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700356 task_usage.bytes[WRITE][FOREGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700357 (task_fg_wr_delta < 0) ? 0 : task_fg_wr_delta;
David Anderson3488d9f2018-07-26 13:20:26 -0700358 task_usage.bytes[WRITE][BACKGROUND][charger_stat_] +=
Yang Jin3906c892017-06-22 15:18:21 -0700359 (task_bg_wr_delta < 0) ? 0 : task_bg_wr_delta;
360 }
Jin Qian5b962c62017-01-30 14:48:38 -0800361 }
362
David Anderson3488d9f2018-07-26 13:20:26 -0700363 last_uid_io_stats_ = uid_io_stats;
Jin Qiana2e5bd12017-01-24 16:23:13 -0800364}
365
Jin Qian6df3bc62017-10-18 17:52:14 -0700366void uid_monitor::report(unordered_map<int, StoragedProto>* protos)
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800367{
Jin Qiana8533322017-10-13 18:15:34 -0700368 if (!enabled()) return;
369
David Anderson3488d9f2018-07-26 13:20:26 -0700370 Mutex::Autolock _l(uidm_mutex_);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800371
Jin Qian5b962c62017-01-30 14:48:38 -0800372 update_curr_io_stats_locked();
373 add_records_locked(time(NULL));
Jin Qianebf031b2017-08-14 16:41:24 -0700374
Jin Qian94b64ef2017-11-09 15:07:18 -0800375 if (protos) {
376 update_uid_io_proto(protos);
377 }
Jin Qianebf031b2017-08-14 16:41:24 -0700378}
379
Jin Qian65dea712017-08-29 16:48:20 -0700380namespace {
381
Jin Qian6df3bc62017-10-18 17:52:14 -0700382void set_io_usage_proto(IOUsage* usage_proto, const io_usage& usage)
Jin Qianebf031b2017-08-14 16:41:24 -0700383{
384 usage_proto->set_rd_fg_chg_on(usage.bytes[READ][FOREGROUND][CHARGER_ON]);
385 usage_proto->set_rd_fg_chg_off(usage.bytes[READ][FOREGROUND][CHARGER_OFF]);
386 usage_proto->set_rd_bg_chg_on(usage.bytes[READ][BACKGROUND][CHARGER_ON]);
387 usage_proto->set_rd_bg_chg_off(usage.bytes[READ][BACKGROUND][CHARGER_OFF]);
388 usage_proto->set_wr_fg_chg_on(usage.bytes[WRITE][FOREGROUND][CHARGER_ON]);
389 usage_proto->set_wr_fg_chg_off(usage.bytes[WRITE][FOREGROUND][CHARGER_OFF]);
390 usage_proto->set_wr_bg_chg_on(usage.bytes[WRITE][BACKGROUND][CHARGER_ON]);
391 usage_proto->set_wr_bg_chg_off(usage.bytes[WRITE][BACKGROUND][CHARGER_OFF]);
392}
393
Jin Qian6df3bc62017-10-18 17:52:14 -0700394void get_io_usage_proto(io_usage* usage, const IOUsage& io_proto)
Jin Qianebf031b2017-08-14 16:41:24 -0700395{
396 usage->bytes[READ][FOREGROUND][CHARGER_ON] = io_proto.rd_fg_chg_on();
397 usage->bytes[READ][FOREGROUND][CHARGER_OFF] = io_proto.rd_fg_chg_off();
398 usage->bytes[READ][BACKGROUND][CHARGER_ON] = io_proto.rd_bg_chg_on();
399 usage->bytes[READ][BACKGROUND][CHARGER_OFF] = io_proto.rd_bg_chg_off();
400 usage->bytes[WRITE][FOREGROUND][CHARGER_ON] = io_proto.wr_fg_chg_on();
401 usage->bytes[WRITE][FOREGROUND][CHARGER_OFF] = io_proto.wr_fg_chg_off();
402 usage->bytes[WRITE][BACKGROUND][CHARGER_ON] = io_proto.wr_bg_chg_on();
403 usage->bytes[WRITE][BACKGROUND][CHARGER_OFF] = io_proto.wr_bg_chg_off();
404}
405
Jin Qian65dea712017-08-29 16:48:20 -0700406} // namespace
407
Jin Qian6df3bc62017-10-18 17:52:14 -0700408void uid_monitor::update_uid_io_proto(unordered_map<int, StoragedProto>* protos)
Jin Qianebf031b2017-08-14 16:41:24 -0700409{
David Anderson3488d9f2018-07-26 13:20:26 -0700410 for (const auto& item : io_history_) {
Jin Qianebf031b2017-08-14 16:41:24 -0700411 const uint64_t& end_ts = item.first;
412 const struct uid_records& recs = item.second;
Jin Qian6df3bc62017-10-18 17:52:14 -0700413 unordered_map<userid_t, UidIOItem*> user_items;
Jin Qianebf031b2017-08-14 16:41:24 -0700414
415 for (const auto& entry : recs.entries) {
Jin Qian6df3bc62017-10-18 17:52:14 -0700416 userid_t user_id = entry.ios.user_id;
417 UidIOItem* item_proto = user_items[user_id];
418 if (item_proto == nullptr) {
419 item_proto = (*protos)[user_id].mutable_uid_io_usage()
420 ->add_uid_io_items();
421 user_items[user_id] = item_proto;
422 }
423 item_proto->set_end_ts(end_ts);
424
425 UidIORecords* recs_proto = item_proto->mutable_records();
426 recs_proto->set_start_ts(recs.start_ts);
427
Jin Qianebf031b2017-08-14 16:41:24 -0700428 UidRecord* rec_proto = recs_proto->add_entries();
429 rec_proto->set_uid_name(entry.name);
Jin Qian6df3bc62017-10-18 17:52:14 -0700430 rec_proto->set_user_id(user_id);
Jin Qianebf031b2017-08-14 16:41:24 -0700431
432 IOUsage* uid_io_proto = rec_proto->mutable_uid_io();
Jin Qian6df3bc62017-10-18 17:52:14 -0700433 const io_usage& uio_ios = entry.ios.uid_ios;
Jin Qianebf031b2017-08-14 16:41:24 -0700434 set_io_usage_proto(uid_io_proto, uio_ios);
435
436 for (const auto& task_io : entry.ios.task_ios) {
437 const std::string& task_name = task_io.first;
Jin Qian6df3bc62017-10-18 17:52:14 -0700438 const io_usage& task_ios = task_io.second;
Jin Qianebf031b2017-08-14 16:41:24 -0700439
440 TaskIOUsage* task_io_proto = rec_proto->add_task_io();
441 task_io_proto->set_task_name(task_name);
442 set_io_usage_proto(task_io_proto->mutable_ios(), task_ios);
443 }
444 }
445 }
Jin Qianebf031b2017-08-14 16:41:24 -0700446}
447
Jin Qian94b64ef2017-11-09 15:07:18 -0800448void uid_monitor::clear_user_history(userid_t user_id)
449{
David Anderson3488d9f2018-07-26 13:20:26 -0700450 Mutex::Autolock _l(uidm_mutex_);
Jin Qian94b64ef2017-11-09 15:07:18 -0800451
David Anderson3488d9f2018-07-26 13:20:26 -0700452 for (auto& item : io_history_) {
Jin Qian94b64ef2017-11-09 15:07:18 -0800453 vector<uid_record>* entries = &item.second.entries;
454 entries->erase(
455 remove_if(entries->begin(), entries->end(),
456 [user_id](const uid_record& rec) {
457 return rec.ios.user_id == user_id;}),
458 entries->end());
459 }
460
David Anderson3488d9f2018-07-26 13:20:26 -0700461 for (auto it = io_history_.begin(); it != io_history_.end(); ) {
Jin Qian94b64ef2017-11-09 15:07:18 -0800462 if (it->second.entries.empty()) {
David Anderson3488d9f2018-07-26 13:20:26 -0700463 it = io_history_.erase(it);
Jin Qian94b64ef2017-11-09 15:07:18 -0800464 } else {
465 it++;
466 }
467 }
468}
469
David Anderson0026a142018-07-26 14:30:35 -0700470void uid_monitor::load_uid_io_proto(userid_t user_id, const UidIOUsage& uid_io_proto)
Jin Qianebf031b2017-08-14 16:41:24 -0700471{
Jin Qiana8533322017-10-13 18:15:34 -0700472 if (!enabled()) return;
473
David Anderson3488d9f2018-07-26 13:20:26 -0700474 Mutex::Autolock _l(uidm_mutex_);
Jin Qian94b64ef2017-11-09 15:07:18 -0800475
Jin Qiand691d6e2017-09-28 16:02:22 -0700476 for (const auto& item_proto : uid_io_proto.uid_io_items()) {
Jin Qianebf031b2017-08-14 16:41:24 -0700477 const UidIORecords& records_proto = item_proto.records();
David Anderson3488d9f2018-07-26 13:20:26 -0700478 struct uid_records* recs = &io_history_[item_proto.end_ts()];
Jin Qianebf031b2017-08-14 16:41:24 -0700479
David Anderson0026a142018-07-26 14:30:35 -0700480 // It's possible that the same uid_io_proto file gets loaded more than
481 // once, for example, if system_server crashes. In this case we avoid
482 // adding duplicate entries, so we build a quick way to check for
483 // duplicates.
484 std::unordered_set<std::string> existing_uids;
485 for (const auto& rec : recs->entries) {
486 if (rec.ios.user_id == user_id) {
487 existing_uids.emplace(rec.name);
488 }
489 }
490
Jin Qianebf031b2017-08-14 16:41:24 -0700491 recs->start_ts = records_proto.start_ts();
492 for (const auto& rec_proto : records_proto.entries()) {
David Anderson0026a142018-07-26 14:30:35 -0700493 if (existing_uids.find(rec_proto.uid_name()) != existing_uids.end()) {
494 continue;
495 }
496
Jin Qianebf031b2017-08-14 16:41:24 -0700497 struct uid_record record;
498 record.name = rec_proto.uid_name();
Jin Qian6df3bc62017-10-18 17:52:14 -0700499 record.ios.user_id = rec_proto.user_id();
Jin Qianebf031b2017-08-14 16:41:24 -0700500 get_io_usage_proto(&record.ios.uid_ios, rec_proto.uid_io());
501
502 for (const auto& task_io_proto : rec_proto.task_io()) {
503 get_io_usage_proto(
504 &record.ios.task_ios[task_io_proto.task_name()],
505 task_io_proto.ios());
506 }
507 recs->entries.push_back(record);
508 }
David Andersondec6a882018-07-26 18:01:03 -0700509
510 // We already added items, so this will just cull down to the maximum
511 // length. We do not remove anything if there is only one entry.
512 if (io_history_.size() > 1) {
513 maybe_shrink_history_for_items(0);
514 }
Jin Qianebf031b2017-08-14 16:41:24 -0700515 }
Jin Qian5b962c62017-01-30 14:48:38 -0800516}
517
518void uid_monitor::set_charger_state(charger_stat_t stat)
519{
David Anderson3488d9f2018-07-26 13:20:26 -0700520 Mutex::Autolock _l(uidm_mutex_);
Jin Qian5b962c62017-01-30 14:48:38 -0800521
David Anderson3488d9f2018-07-26 13:20:26 -0700522 if (charger_stat_ == stat) {
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800523 return;
524 }
525
Jin Qian5b962c62017-01-30 14:48:38 -0800526 update_curr_io_stats_locked();
David Anderson3488d9f2018-07-26 13:20:26 -0700527 charger_stat_ = stat;
Jin Qian5b962c62017-01-30 14:48:38 -0800528}
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800529
Jin Qiana8533322017-10-13 18:15:34 -0700530void uid_monitor::init(charger_stat_t stat)
Jin Qian5b962c62017-01-30 14:48:38 -0800531{
David Anderson3488d9f2018-07-26 13:20:26 -0700532 charger_stat_ = stat;
Jin Qianebf031b2017-08-14 16:41:24 -0700533
David Anderson3488d9f2018-07-26 13:20:26 -0700534 start_ts_ = time(NULL);
535 last_uid_io_stats_ = get_uid_io_stats();
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800536}
537
538uid_monitor::uid_monitor()
David Anderson3488d9f2018-07-26 13:20:26 -0700539 : enabled_(!access(UID_IO_STATS_PATH, R_OK)) {
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800540}