Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 23 | #include <unordered_map> |
David Anderson | 0026a14 | 2018-07-26 14:30:35 -0700 | [diff] [blame] | 24 | #include <unordered_set> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 25 | |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 26 | #include <android/content/pm/IPackageManagerNative.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 27 | #include <android-base/file.h> |
| 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/macros.h> |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 30 | #include <android-base/parseint.h> |
Jin Qian | e83a610 | 2017-03-02 16:16:54 -0800 | [diff] [blame] | 31 | #include <android-base/strings.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 34 | #include <log/log_event_list.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 35 | |
| 36 | #include "storaged.h" |
| 37 | #include "storaged_uid_monitor.h" |
| 38 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 39 | using namespace android; |
| 40 | using namespace android::base; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 41 | using namespace android::content::pm; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 42 | using namespace android::os::storaged; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 43 | using namespace storaged_proto; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 44 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | bool refresh_uid_names; |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 48 | const char* UID_IO_STATS_PATH = "/proc/uid_io/stats"; |
| 49 | |
| 50 | } // namepsace |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 51 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 52 | std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats() |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 53 | { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 54 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 55 | return get_uid_io_stats_locked(); |
| 56 | }; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 57 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 58 | /* return true on parse success and false on failure */ |
| 59 | bool 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 Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 74 | LOG(WARNING) << "Invalid uid I/O stats: \"" << s << "\""; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /* return true on parse success and false on failure */ |
| 81 | bool task_info::parse_task_io_stats(std::string&& s) |
| 82 | { |
| 83 | std::vector<std::string> fields = Split(s, ","); |
Jin Qian | 0e02687 | 2017-08-29 11:42:06 -0700 | [diff] [blame] | 84 | 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 Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 97 | LOG(WARNING) << "Invalid task I/O stats: \"" << s << "\""; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
Jin Qian | 0e02687 | 2017-08-29 11:42:06 -0700 | [diff] [blame] | 100 | comm = Join(std::vector<std::string>( |
| 101 | fields.begin() + 1, fields.end() - 11), ','); |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool 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 Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 118 | namespace { |
| 119 | |
| 120 | void get_uid_names(const vector<int>& uids, const vector<std::string*>& uid_names) |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 121 | { |
| 122 | sp<IServiceManager> sm = defaultServiceManager(); |
| 123 | if (sm == NULL) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 124 | LOG(ERROR) << "defaultServiceManager failed"; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | sp<IBinder> binder = sm->getService(String16("package_native")); |
| 129 | if (binder == NULL) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 130 | LOG(ERROR) << "getService package_native failed"; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 131 | 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 Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 138 | LOG(ERROR) << "package_native::getNamesForUids failed: " << status.exceptionMessage(); |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 139 | 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 Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 151 | } // namespace |
| 152 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 153 | std::unordered_map<uint32_t, uid_info> uid_monitor::get_uid_io_stats_locked() |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 154 | { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 155 | std::unordered_map<uint32_t, uid_info> uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 156 | std::string buffer; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 157 | if (!ReadFileToString(UID_IO_STATS_PATH, &buffer)) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 158 | PLOG(ERROR) << UID_IO_STATS_PATH << ": ReadFileToString failed"; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 159 | return uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 162 | std::vector<std::string> io_stats = Split(std::move(buffer), "\n"); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 163 | uid_info u; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 164 | vector<int> uids; |
| 165 | vector<std::string*> uid_names; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 166 | |
Jin Qian | e83a610 | 2017-03-02 16:16:54 -0800 | [diff] [blame] | 167 | for (uint32_t i = 0; i < io_stats.size(); i++) { |
| 168 | if (io_stats[i].empty()) { |
| 169 | continue; |
| 170 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 171 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 172 | if (io_stats[i].compare(0, 4, "task")) { |
| 173 | if (!u.parse_uid_io_stats(std::move(io_stats[i]))) |
| 174 | continue; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 175 | uid_io_stats[u.uid] = u; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 176 | 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 Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 179 | if (last_uid_io_stats_.find(u.uid) == last_uid_io_stats_.end()) { |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 180 | refresh_uid_names = true; |
| 181 | } else { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 182 | uid_io_stats[u.uid].name = last_uid_io_stats_[u.uid].name; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 183 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 184 | } else { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 185 | task_info t; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 186 | if (!t.parse_task_io_stats(std::move(io_stats[i]))) |
| 187 | continue; |
| 188 | uid_io_stats[u.uid].tasks[t.pid] = t; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 189 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 192 | if (!uids.empty() && refresh_uid_names) { |
| 193 | get_uid_names(uids, uid_names); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 196 | return uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 199 | namespace { |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 200 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 201 | inline size_t history_size( |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 202 | const std::map<uint64_t, struct uid_records>& history) |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 203 | { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 204 | size_t count = 0; |
| 205 | for (auto const& it : history) { |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 206 | count += it.second.entries.size(); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 207 | } |
| 208 | return count; |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 211 | } // namespace |
| 212 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 213 | void uid_monitor::add_records_locked(uint64_t curr_ts) |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 214 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 215 | // remove records more than 5 days old |
| 216 | if (curr_ts > 5 * DAY_TO_SEC) { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 217 | auto it = io_history_.lower_bound(curr_ts - 5 * DAY_TO_SEC); |
| 218 | io_history_.erase(io_history_.begin(), it); |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 221 | struct uid_records new_records; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 222 | for (const auto& p : curr_io_stats_) { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 223 | struct uid_record record = {}; |
| 224 | record.name = p.first; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 225 | if (!p.second.uid_ios.is_zero()) { |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 226 | record.ios.user_id = p.second.user_id; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 227 | 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 Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 232 | new_records.entries.push_back(record); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 233 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 234 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 235 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 236 | curr_io_stats_.clear(); |
| 237 | new_records.start_ts = start_ts_; |
| 238 | start_ts_ = curr_ts; |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 239 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 240 | if (new_records.entries.empty()) |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 241 | return; |
| 242 | |
| 243 | // make some room for new records |
David Anderson | dec6a88 | 2018-07-26 18:01:03 -0700 | [diff] [blame] | 244 | maybe_shrink_history_for_items(new_records.entries.size()); |
| 245 | |
| 246 | io_history_[curr_ts] = new_records; |
| 247 | } |
| 248 | |
| 249 | void uid_monitor::maybe_shrink_history_for_items(size_t nitems) { |
| 250 | ssize_t overflow = history_size(io_history_) + nitems - MAX_UID_RECORDS_SIZE; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 251 | while (overflow > 0 && io_history_.size() > 0) { |
| 252 | auto del_it = io_history_.begin(); |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 253 | overflow -= del_it->second.entries.size(); |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 254 | io_history_.erase(io_history_.begin()); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 255 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 258 | std::map<uint64_t, struct uid_records> uid_monitor::dump( |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 259 | double hours, uint64_t threshold, bool force_report) |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 260 | { |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame] | 261 | if (force_report) { |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 262 | report(nullptr); |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame] | 263 | } |
| 264 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 265 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 266 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 267 | std::map<uint64_t, struct uid_records> dump_records; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 268 | uint64_t first_ts = 0; |
| 269 | |
| 270 | if (hours != 0) { |
Jin Qian | dd41d6b | 2017-02-10 17:23:16 -0800 | [diff] [blame] | 271 | first_ts = time(NULL) - hours * HOUR_TO_SEC; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 272 | } |
| 273 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 274 | for (auto it = io_history_.lower_bound(first_ts); it != io_history_.end(); ++it) { |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 275 | const std::vector<struct uid_record>& recs = it->second.entries; |
| 276 | struct uid_records filtered; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 277 | |
| 278 | for (const auto& rec : recs) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 279 | 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 Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 288 | filtered.entries.push_back(rec); |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 291 | |
| 292 | if (filtered.entries.empty()) |
| 293 | continue; |
| 294 | |
| 295 | filtered.start_ts = it->second.start_ts; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 296 | dump_records.insert( |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 297 | std::pair<uint64_t, struct uid_records>(it->first, filtered)); |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 298 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 299 | |
| 300 | return dump_records; |
| 301 | } |
| 302 | |
| 303 | void uid_monitor::update_curr_io_stats_locked() |
| 304 | { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 305 | std::unordered_map<uint32_t, uid_info> uid_io_stats = |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 306 | get_uid_io_stats_locked(); |
| 307 | if (uid_io_stats.empty()) { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | for (const auto& it : uid_io_stats) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 312 | const uid_info& uid = it.second; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 313 | if (curr_io_stats_.find(uid.name) == curr_io_stats_.end()) { |
| 314 | curr_io_stats_[uid.name] = {}; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 315 | } |
| 316 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 317 | struct uid_io_usage& usage = curr_io_stats_[uid.name]; |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 318 | usage.user_id = multiuser_get_user_id(uid.uid); |
| 319 | |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 320 | int64_t fg_rd_delta = uid.io[FOREGROUND].read_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 321 | last_uid_io_stats_[uid.uid].io[FOREGROUND].read_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 322 | int64_t bg_rd_delta = uid.io[BACKGROUND].read_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 323 | last_uid_io_stats_[uid.uid].io[BACKGROUND].read_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 324 | int64_t fg_wr_delta = uid.io[FOREGROUND].write_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 325 | last_uid_io_stats_[uid.uid].io[FOREGROUND].write_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 326 | int64_t bg_wr_delta = uid.io[BACKGROUND].write_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 327 | last_uid_io_stats_[uid.uid].io[BACKGROUND].write_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 328 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 329 | usage.uid_ios.bytes[READ][FOREGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 330 | (fg_rd_delta < 0) ? 0 : fg_rd_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 331 | usage.uid_ios.bytes[READ][BACKGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 332 | (bg_rd_delta < 0) ? 0 : bg_rd_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 333 | usage.uid_ios.bytes[WRITE][FOREGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 334 | (fg_wr_delta < 0) ? 0 : fg_wr_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 335 | usage.uid_ios.bytes[WRITE][BACKGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 336 | (bg_wr_delta < 0) ? 0 : bg_wr_delta; |
| 337 | |
| 338 | for (const auto& task_it : uid.tasks) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 339 | const task_info& task = task_it.second; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 340 | 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 Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 343 | last_uid_io_stats_[uid.uid].tasks[pid].io[FOREGROUND].read_bytes; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 344 | int64_t task_bg_rd_delta = task.io[BACKGROUND].read_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 345 | last_uid_io_stats_[uid.uid].tasks[pid].io[BACKGROUND].read_bytes; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 346 | int64_t task_fg_wr_delta = task.io[FOREGROUND].write_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 347 | last_uid_io_stats_[uid.uid].tasks[pid].io[FOREGROUND].write_bytes; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 348 | int64_t task_bg_wr_delta = task.io[BACKGROUND].write_bytes - |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 349 | last_uid_io_stats_[uid.uid].tasks[pid].io[BACKGROUND].write_bytes; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 350 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 351 | io_usage& task_usage = usage.task_ios[comm]; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 352 | task_usage.bytes[READ][FOREGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 353 | (task_fg_rd_delta < 0) ? 0 : task_fg_rd_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 354 | task_usage.bytes[READ][BACKGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 355 | (task_bg_rd_delta < 0) ? 0 : task_bg_rd_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 356 | task_usage.bytes[WRITE][FOREGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 357 | (task_fg_wr_delta < 0) ? 0 : task_fg_wr_delta; |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 358 | task_usage.bytes[WRITE][BACKGROUND][charger_stat_] += |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 359 | (task_bg_wr_delta < 0) ? 0 : task_bg_wr_delta; |
| 360 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 361 | } |
| 362 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 363 | last_uid_io_stats_ = uid_io_stats; |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 366 | void uid_monitor::report(unordered_map<int, StoragedProto>* protos) |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 367 | { |
Jin Qian | a853332 | 2017-10-13 18:15:34 -0700 | [diff] [blame] | 368 | if (!enabled()) return; |
| 369 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 370 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 371 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 372 | update_curr_io_stats_locked(); |
| 373 | add_records_locked(time(NULL)); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 374 | |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 375 | if (protos) { |
| 376 | update_uid_io_proto(protos); |
| 377 | } |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 380 | namespace { |
| 381 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 382 | void set_io_usage_proto(IOUsage* usage_proto, const io_usage& usage) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 383 | { |
| 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 Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 394 | void get_io_usage_proto(io_usage* usage, const IOUsage& io_proto) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 395 | { |
| 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 Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 406 | } // namespace |
| 407 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 408 | void uid_monitor::update_uid_io_proto(unordered_map<int, StoragedProto>* protos) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 409 | { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 410 | for (const auto& item : io_history_) { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 411 | const uint64_t& end_ts = item.first; |
| 412 | const struct uid_records& recs = item.second; |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 413 | unordered_map<userid_t, UidIOItem*> user_items; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 414 | |
| 415 | for (const auto& entry : recs.entries) { |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 416 | 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 Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 428 | UidRecord* rec_proto = recs_proto->add_entries(); |
| 429 | rec_proto->set_uid_name(entry.name); |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 430 | rec_proto->set_user_id(user_id); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 431 | |
| 432 | IOUsage* uid_io_proto = rec_proto->mutable_uid_io(); |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 433 | const io_usage& uio_ios = entry.ios.uid_ios; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 434 | 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 Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 438 | const io_usage& task_ios = task_io.second; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 439 | |
| 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 Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 448 | void uid_monitor::clear_user_history(userid_t user_id) |
| 449 | { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 450 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 451 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 452 | for (auto& item : io_history_) { |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 453 | 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 Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 461 | for (auto it = io_history_.begin(); it != io_history_.end(); ) { |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 462 | if (it->second.entries.empty()) { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 463 | it = io_history_.erase(it); |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 464 | } else { |
| 465 | it++; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
David Anderson | 0026a14 | 2018-07-26 14:30:35 -0700 | [diff] [blame] | 470 | void uid_monitor::load_uid_io_proto(userid_t user_id, const UidIOUsage& uid_io_proto) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 471 | { |
Jin Qian | a853332 | 2017-10-13 18:15:34 -0700 | [diff] [blame] | 472 | if (!enabled()) return; |
| 473 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 474 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | 94b64ef | 2017-11-09 15:07:18 -0800 | [diff] [blame] | 475 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 476 | for (const auto& item_proto : uid_io_proto.uid_io_items()) { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 477 | const UidIORecords& records_proto = item_proto.records(); |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 478 | struct uid_records* recs = &io_history_[item_proto.end_ts()]; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 479 | |
David Anderson | 0026a14 | 2018-07-26 14:30:35 -0700 | [diff] [blame] | 480 | // 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 Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 491 | recs->start_ts = records_proto.start_ts(); |
| 492 | for (const auto& rec_proto : records_proto.entries()) { |
David Anderson | 0026a14 | 2018-07-26 14:30:35 -0700 | [diff] [blame] | 493 | if (existing_uids.find(rec_proto.uid_name()) != existing_uids.end()) { |
| 494 | continue; |
| 495 | } |
| 496 | |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 497 | struct uid_record record; |
| 498 | record.name = rec_proto.uid_name(); |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 499 | record.ios.user_id = rec_proto.user_id(); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 500 | 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 Anderson | dec6a88 | 2018-07-26 18:01:03 -0700 | [diff] [blame] | 509 | |
| 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 Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 515 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void uid_monitor::set_charger_state(charger_stat_t stat) |
| 519 | { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 520 | Mutex::Autolock _l(uidm_mutex_); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 521 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 522 | if (charger_stat_ == stat) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 526 | update_curr_io_stats_locked(); |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 527 | charger_stat_ = stat; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 528 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 529 | |
Jin Qian | a853332 | 2017-10-13 18:15:34 -0700 | [diff] [blame] | 530 | void uid_monitor::init(charger_stat_t stat) |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 531 | { |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 532 | charger_stat_ = stat; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 533 | |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 534 | start_ts_ = time(NULL); |
| 535 | last_uid_io_stats_ = get_uid_io_stats(); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | uid_monitor::uid_monitor() |
David Anderson | 3488d9f | 2018-07-26 13:20:26 -0700 | [diff] [blame] | 539 | : enabled_(!access(UID_IO_STATS_PATH, R_OK)) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 540 | } |