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> |
| 24 | |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 25 | #include <android/content/pm/IPackageManagerNative.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/macros.h> |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 29 | #include <android-base/parseint.h> |
Jin Qian | e83a610 | 2017-03-02 16:16:54 -0800 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 32 | #include <binder/IServiceManager.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 33 | #include <log/log_event_list.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 34 | |
| 35 | #include "storaged.h" |
| 36 | #include "storaged_uid_monitor.h" |
| 37 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 38 | using namespace android; |
| 39 | using namespace android::base; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 40 | using namespace android::content::pm; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 41 | using namespace android::os::storaged; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 42 | using namespace storaged_proto; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 43 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | bool refresh_uid_names; |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 47 | const char* UID_IO_STATS_PATH = "/proc/uid_io/stats"; |
| 48 | |
| 49 | } // namepsace |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 50 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 51 | 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] | 52 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 53 | std::unique_ptr<lock_t> lock(new lock_t(&um_lock)); |
| 54 | return get_uid_io_stats_locked(); |
| 55 | }; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 56 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 57 | /* return true on parse success and false on failure */ |
| 58 | bool uid_info::parse_uid_io_stats(std::string&& s) |
| 59 | { |
| 60 | std::vector<std::string> fields = Split(s, " "); |
| 61 | if (fields.size() < 11 || |
| 62 | !ParseUint(fields[0], &uid) || |
| 63 | !ParseUint(fields[1], &io[FOREGROUND].rchar) || |
| 64 | !ParseUint(fields[2], &io[FOREGROUND].wchar) || |
| 65 | !ParseUint(fields[3], &io[FOREGROUND].read_bytes) || |
| 66 | !ParseUint(fields[4], &io[FOREGROUND].write_bytes) || |
| 67 | !ParseUint(fields[5], &io[BACKGROUND].rchar) || |
| 68 | !ParseUint(fields[6], &io[BACKGROUND].wchar) || |
| 69 | !ParseUint(fields[7], &io[BACKGROUND].read_bytes) || |
| 70 | !ParseUint(fields[8], &io[BACKGROUND].write_bytes) || |
| 71 | !ParseUint(fields[9], &io[FOREGROUND].fsync) || |
| 72 | !ParseUint(fields[10], &io[BACKGROUND].fsync)) { |
Jin Qian | 0e02687 | 2017-08-29 11:42:06 -0700 | [diff] [blame] | 73 | LOG_TO(SYSTEM, WARNING) << "Invalid uid I/O stats: \"" |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 74 | << s << "\""; |
| 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)) { |
| 97 | LOG_TO(SYSTEM, WARNING) << "Invalid task I/O stats: \"" |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 98 | << s << "\""; |
| 99 | return false; |
| 100 | } |
Jin Qian | 0e02687 | 2017-08-29 11:42:06 -0700 | [diff] [blame] | 101 | comm = Join(std::vector<std::string>( |
| 102 | fields.begin() + 1, fields.end() - 11), ','); |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool io_usage::is_zero() const |
| 107 | { |
| 108 | for (int i = 0; i < IO_TYPES; i++) { |
| 109 | for (int j = 0; j < UID_STATS; j++) { |
| 110 | for (int k = 0; k < CHARGER_STATS; k++) { |
| 111 | if (bytes[i][j][k]) |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 119 | namespace { |
| 120 | |
| 121 | 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] | 122 | { |
| 123 | sp<IServiceManager> sm = defaultServiceManager(); |
| 124 | if (sm == NULL) { |
| 125 | LOG_TO(SYSTEM, ERROR) << "defaultServiceManager failed"; |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | sp<IBinder> binder = sm->getService(String16("package_native")); |
| 130 | if (binder == NULL) { |
| 131 | LOG_TO(SYSTEM, ERROR) << "getService package_native failed"; |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | sp<IPackageManagerNative> package_mgr = interface_cast<IPackageManagerNative>(binder); |
| 136 | std::vector<std::string> names; |
| 137 | binder::Status status = package_mgr->getNamesForUids(uids, &names); |
| 138 | if (!status.isOk()) { |
| 139 | LOG_TO(SYSTEM, ERROR) << "package_native::getNamesForUids failed: " |
| 140 | << status.exceptionMessage(); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | for (uint32_t i = 0; i < uid_names.size(); i++) { |
| 145 | if (!names[i].empty()) { |
| 146 | *uid_names[i] = names[i]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | refresh_uid_names = false; |
| 151 | } |
| 152 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 153 | } // namespace |
| 154 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 155 | 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] | 156 | { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 157 | std::unordered_map<uint32_t, uid_info> uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 158 | std::string buffer; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 159 | if (!ReadFileToString(UID_IO_STATS_PATH, &buffer)) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 160 | PLOG_TO(SYSTEM, ERROR) << UID_IO_STATS_PATH << ": ReadFileToString failed"; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 161 | return uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 164 | std::vector<std::string> io_stats = Split(std::move(buffer), "\n"); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 165 | uid_info u; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 166 | vector<int> uids; |
| 167 | vector<std::string*> uid_names; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 168 | |
Jin Qian | e83a610 | 2017-03-02 16:16:54 -0800 | [diff] [blame] | 169 | for (uint32_t i = 0; i < io_stats.size(); i++) { |
| 170 | if (io_stats[i].empty()) { |
| 171 | continue; |
| 172 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 173 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 174 | if (io_stats[i].compare(0, 4, "task")) { |
| 175 | if (!u.parse_uid_io_stats(std::move(io_stats[i]))) |
| 176 | continue; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 177 | uid_io_stats[u.uid] = u; |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 178 | uid_io_stats[u.uid].name = std::to_string(u.uid); |
| 179 | uids.push_back(u.uid); |
| 180 | uid_names.push_back(&uid_io_stats[u.uid].name); |
| 181 | if (last_uid_io_stats.find(u.uid) == last_uid_io_stats.end()) { |
| 182 | refresh_uid_names = true; |
| 183 | } else { |
| 184 | uid_io_stats[u.uid].name = last_uid_io_stats[u.uid].name; |
| 185 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 186 | } else { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 187 | task_info t; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 188 | if (!t.parse_task_io_stats(std::move(io_stats[i]))) |
| 189 | continue; |
| 190 | uid_io_stats[u.uid].tasks[t.pid] = t; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 191 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jin Qian | 2d7bcce | 2017-07-28 18:45:59 -0700 | [diff] [blame] | 194 | if (!uids.empty() && refresh_uid_names) { |
| 195 | get_uid_names(uids, uid_names); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 198 | return uid_io_stats; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 201 | namespace { |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 202 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 203 | const int MAX_UID_RECORDS_SIZE = 1000 * 48; // 1000 uids in 48 hours |
| 204 | |
| 205 | inline size_t history_size( |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 206 | const std::map<uint64_t, struct uid_records>& history) |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 207 | { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 208 | size_t count = 0; |
| 209 | for (auto const& it : history) { |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 210 | count += it.second.entries.size(); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 211 | } |
| 212 | return count; |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 215 | } // namespace |
| 216 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 217 | void uid_monitor::add_records_locked(uint64_t curr_ts) |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 218 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 219 | // remove records more than 5 days old |
| 220 | if (curr_ts > 5 * DAY_TO_SEC) { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 221 | auto it = io_history.lower_bound(curr_ts - 5 * DAY_TO_SEC); |
| 222 | io_history.erase(io_history.begin(), it); |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 225 | struct uid_records new_records; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 226 | for (const auto& p : curr_io_stats) { |
| 227 | struct uid_record record = {}; |
| 228 | record.name = p.first; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 229 | if (!p.second.uid_ios.is_zero()) { |
| 230 | record.ios.uid_ios = p.second.uid_ios; |
| 231 | for (const auto& p_task : p.second.task_ios) { |
| 232 | if (!p_task.second.is_zero()) |
| 233 | record.ios.task_ios[p_task.first] = p_task.second; |
| 234 | } |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 235 | new_records.entries.push_back(record); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 236 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 237 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 238 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 239 | curr_io_stats.clear(); |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 240 | new_records.start_ts = start_ts; |
| 241 | start_ts = curr_ts; |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 242 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 243 | if (new_records.entries.empty()) |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 244 | return; |
| 245 | |
| 246 | // make some room for new records |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 247 | ssize_t overflow = history_size(io_history) + |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 248 | new_records.entries.size() - MAX_UID_RECORDS_SIZE; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 249 | while (overflow > 0 && io_history.size() > 0) { |
| 250 | auto del_it = io_history.begin(); |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 251 | overflow -= del_it->second.entries.size(); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 252 | io_history.erase(io_history.begin()); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 255 | io_history[curr_ts] = new_records; |
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 | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 259 | double hours, uint64_t threshold, bool force_report, UidIOUsage* uid_io_proto) |
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 | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 262 | report(uid_io_proto); |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 265 | std::unique_ptr<lock_t> lock(new lock_t(&um_lock)); |
| 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 | |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -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; |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 313 | |
| 314 | if (curr_io_stats.find(uid.name) == curr_io_stats.end()) { |
| 315 | curr_io_stats[uid.name] = {}; |
| 316 | } |
| 317 | |
| 318 | struct uid_io_usage& usage = curr_io_stats[uid.name]; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 319 | int64_t fg_rd_delta = uid.io[FOREGROUND].read_bytes - |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 320 | last_uid_io_stats[uid.uid].io[FOREGROUND].read_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 321 | int64_t bg_rd_delta = uid.io[BACKGROUND].read_bytes - |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 322 | last_uid_io_stats[uid.uid].io[BACKGROUND].read_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 323 | int64_t fg_wr_delta = uid.io[FOREGROUND].write_bytes - |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 324 | last_uid_io_stats[uid.uid].io[FOREGROUND].write_bytes; |
Jin Qian | baff640 | 2017-02-16 18:34:31 -0800 | [diff] [blame] | 325 | int64_t bg_wr_delta = uid.io[BACKGROUND].write_bytes - |
| 326 | last_uid_io_stats[uid.uid].io[BACKGROUND].write_bytes; |
| 327 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 328 | usage.uid_ios.bytes[READ][FOREGROUND][charger_stat] += |
| 329 | (fg_rd_delta < 0) ? 0 : fg_rd_delta; |
| 330 | usage.uid_ios.bytes[READ][BACKGROUND][charger_stat] += |
| 331 | (bg_rd_delta < 0) ? 0 : bg_rd_delta; |
| 332 | usage.uid_ios.bytes[WRITE][FOREGROUND][charger_stat] += |
| 333 | (fg_wr_delta < 0) ? 0 : fg_wr_delta; |
| 334 | usage.uid_ios.bytes[WRITE][BACKGROUND][charger_stat] += |
| 335 | (bg_wr_delta < 0) ? 0 : bg_wr_delta; |
| 336 | |
| 337 | for (const auto& task_it : uid.tasks) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 338 | const task_info& task = task_it.second; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 339 | const pid_t pid = task_it.first; |
| 340 | const std::string& comm = task_it.second.comm; |
| 341 | int64_t task_fg_rd_delta = task.io[FOREGROUND].read_bytes - |
| 342 | last_uid_io_stats[uid.uid].tasks[pid].io[FOREGROUND].read_bytes; |
| 343 | int64_t task_bg_rd_delta = task.io[BACKGROUND].read_bytes - |
| 344 | last_uid_io_stats[uid.uid].tasks[pid].io[BACKGROUND].read_bytes; |
| 345 | int64_t task_fg_wr_delta = task.io[FOREGROUND].write_bytes - |
| 346 | last_uid_io_stats[uid.uid].tasks[pid].io[FOREGROUND].write_bytes; |
| 347 | int64_t task_bg_wr_delta = task.io[BACKGROUND].write_bytes - |
| 348 | last_uid_io_stats[uid.uid].tasks[pid].io[BACKGROUND].write_bytes; |
| 349 | |
| 350 | struct io_usage& task_usage = usage.task_ios[comm]; |
| 351 | task_usage.bytes[READ][FOREGROUND][charger_stat] += |
| 352 | (task_fg_rd_delta < 0) ? 0 : task_fg_rd_delta; |
| 353 | task_usage.bytes[READ][BACKGROUND][charger_stat] += |
| 354 | (task_bg_rd_delta < 0) ? 0 : task_bg_rd_delta; |
| 355 | task_usage.bytes[WRITE][FOREGROUND][charger_stat] += |
| 356 | (task_fg_wr_delta < 0) ? 0 : task_fg_wr_delta; |
| 357 | task_usage.bytes[WRITE][BACKGROUND][charger_stat] += |
| 358 | (task_bg_wr_delta < 0) ? 0 : task_bg_wr_delta; |
| 359 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | last_uid_io_stats = uid_io_stats; |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 365 | void uid_monitor::report(UidIOUsage* proto) |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 366 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 367 | std::unique_ptr<lock_t> lock(new lock_t(&um_lock)); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 368 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 369 | update_curr_io_stats_locked(); |
| 370 | add_records_locked(time(NULL)); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 371 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 372 | update_uid_io_proto(proto); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 375 | namespace { |
| 376 | |
| 377 | void set_io_usage_proto(IOUsage* usage_proto, const struct io_usage& usage) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 378 | { |
| 379 | usage_proto->set_rd_fg_chg_on(usage.bytes[READ][FOREGROUND][CHARGER_ON]); |
| 380 | usage_proto->set_rd_fg_chg_off(usage.bytes[READ][FOREGROUND][CHARGER_OFF]); |
| 381 | usage_proto->set_rd_bg_chg_on(usage.bytes[READ][BACKGROUND][CHARGER_ON]); |
| 382 | usage_proto->set_rd_bg_chg_off(usage.bytes[READ][BACKGROUND][CHARGER_OFF]); |
| 383 | usage_proto->set_wr_fg_chg_on(usage.bytes[WRITE][FOREGROUND][CHARGER_ON]); |
| 384 | usage_proto->set_wr_fg_chg_off(usage.bytes[WRITE][FOREGROUND][CHARGER_OFF]); |
| 385 | usage_proto->set_wr_bg_chg_on(usage.bytes[WRITE][BACKGROUND][CHARGER_ON]); |
| 386 | usage_proto->set_wr_bg_chg_off(usage.bytes[WRITE][BACKGROUND][CHARGER_OFF]); |
| 387 | } |
| 388 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 389 | void get_io_usage_proto(struct io_usage* usage, const IOUsage& io_proto) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 390 | { |
| 391 | usage->bytes[READ][FOREGROUND][CHARGER_ON] = io_proto.rd_fg_chg_on(); |
| 392 | usage->bytes[READ][FOREGROUND][CHARGER_OFF] = io_proto.rd_fg_chg_off(); |
| 393 | usage->bytes[READ][BACKGROUND][CHARGER_ON] = io_proto.rd_bg_chg_on(); |
| 394 | usage->bytes[READ][BACKGROUND][CHARGER_OFF] = io_proto.rd_bg_chg_off(); |
| 395 | usage->bytes[WRITE][FOREGROUND][CHARGER_ON] = io_proto.wr_fg_chg_on(); |
| 396 | usage->bytes[WRITE][FOREGROUND][CHARGER_OFF] = io_proto.wr_fg_chg_off(); |
| 397 | usage->bytes[WRITE][BACKGROUND][CHARGER_ON] = io_proto.wr_bg_chg_on(); |
| 398 | usage->bytes[WRITE][BACKGROUND][CHARGER_OFF] = io_proto.wr_bg_chg_off(); |
| 399 | } |
| 400 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 401 | } // namespace |
| 402 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 403 | void uid_monitor::update_uid_io_proto(UidIOUsage* uid_io_proto) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 404 | { |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 405 | uid_io_proto->Clear(); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 406 | |
| 407 | for (const auto& item : io_history) { |
| 408 | const uint64_t& end_ts = item.first; |
| 409 | const struct uid_records& recs = item.second; |
| 410 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 411 | UidIOItem* item_proto = uid_io_proto->add_uid_io_items(); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 412 | item_proto->set_end_ts(end_ts); |
| 413 | |
| 414 | UidIORecords* recs_proto = item_proto->mutable_records(); |
| 415 | recs_proto->set_start_ts(recs.start_ts); |
| 416 | |
| 417 | for (const auto& entry : recs.entries) { |
| 418 | UidRecord* rec_proto = recs_proto->add_entries(); |
| 419 | rec_proto->set_uid_name(entry.name); |
| 420 | |
| 421 | IOUsage* uid_io_proto = rec_proto->mutable_uid_io(); |
| 422 | const struct io_usage& uio_ios = entry.ios.uid_ios; |
| 423 | set_io_usage_proto(uid_io_proto, uio_ios); |
| 424 | |
| 425 | for (const auto& task_io : entry.ios.task_ios) { |
| 426 | const std::string& task_name = task_io.first; |
| 427 | const struct io_usage& task_ios = task_io.second; |
| 428 | |
| 429 | TaskIOUsage* task_io_proto = rec_proto->add_task_io(); |
| 430 | task_io_proto->set_task_name(task_name); |
| 431 | set_io_usage_proto(task_io_proto->mutable_ios(), task_ios); |
| 432 | } |
| 433 | } |
| 434 | } |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 437 | void uid_monitor::load_uid_io_proto(const UidIOUsage& uid_io_proto) |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 438 | { |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 439 | for (const auto& item_proto : uid_io_proto.uid_io_items()) { |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 440 | const UidIORecords& records_proto = item_proto.records(); |
| 441 | struct uid_records* recs = &io_history[item_proto.end_ts()]; |
| 442 | |
| 443 | recs->start_ts = records_proto.start_ts(); |
| 444 | for (const auto& rec_proto : records_proto.entries()) { |
| 445 | struct uid_record record; |
| 446 | record.name = rec_proto.uid_name(); |
| 447 | get_io_usage_proto(&record.ios.uid_ios, rec_proto.uid_io()); |
| 448 | |
| 449 | for (const auto& task_io_proto : rec_proto.task_io()) { |
| 450 | get_io_usage_proto( |
| 451 | &record.ios.task_ios[task_io_proto.task_name()], |
| 452 | task_io_proto.ios()); |
| 453 | } |
| 454 | recs->entries.push_back(record); |
| 455 | } |
| 456 | } |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void uid_monitor::set_charger_state(charger_stat_t stat) |
| 460 | { |
| 461 | std::unique_ptr<lock_t> lock(new lock_t(&um_lock)); |
| 462 | |
| 463 | if (charger_stat == stat) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 464 | return; |
| 465 | } |
| 466 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 467 | update_curr_io_stats_locked(); |
| 468 | charger_stat = stat; |
| 469 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 470 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 471 | void uid_monitor::init(charger_stat_t stat, const UidIOUsage& proto) |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 472 | { |
| 473 | charger_stat = stat; |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 474 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 475 | load_uid_io_proto(proto); |
Jin Qian | ebf031b | 2017-08-14 16:41:24 -0700 | [diff] [blame] | 476 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 477 | start_ts = time(NULL); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 478 | last_uid_io_stats = get_uid_io_stats(); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | uid_monitor::uid_monitor() |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 482 | : enable(!access(UID_IO_STATS_PATH, R_OK)) |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 483 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 484 | sem_init(&um_lock, 0, 1); |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | uid_monitor::~uid_monitor() |
| 488 | { |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 489 | sem_destroy(&um_lock); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 490 | } |