ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [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 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 22 | #include <android-base/parseint.h> |
| 23 | #include <android-base/parsedouble.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 24 | #include <binder/IBinder.h> |
| 25 | #include <binder/IInterface.h> |
| 26 | |
| 27 | #include <binder/IPCThreadState.h> |
| 28 | #include <binder/IServiceManager.h> |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 29 | #include <binder/PermissionCache.h> |
| 30 | #include <private/android_filesystem_config.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 31 | |
| 32 | #include <storaged.h> |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 33 | #include <storaged_utils.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 34 | #include <storaged_service.h> |
| 35 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 36 | using namespace std; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 37 | using namespace android::base; |
| 38 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 39 | extern sp<storaged_t> storaged_sp; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 40 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 41 | status_t StoragedService::start() { |
| 42 | return BinderService<StoragedService>::publish(); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 43 | } |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 44 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 45 | void StoragedService::dumpUidRecords(int fd, const vector<uid_record>& entries) { |
| 46 | map<string, io_usage> merged_entries = merge_io_usage(entries); |
| 47 | for (const auto& rec : merged_entries) { |
| 48 | dprintf(fd, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 |
| 49 | " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 50 | rec.first.c_str(), |
| 51 | rec.second.bytes[READ][FOREGROUND][CHARGER_OFF], |
| 52 | rec.second.bytes[WRITE][FOREGROUND][CHARGER_OFF], |
| 53 | rec.second.bytes[READ][BACKGROUND][CHARGER_OFF], |
| 54 | rec.second.bytes[WRITE][BACKGROUND][CHARGER_OFF], |
| 55 | rec.second.bytes[READ][FOREGROUND][CHARGER_ON], |
| 56 | rec.second.bytes[WRITE][FOREGROUND][CHARGER_ON], |
| 57 | rec.second.bytes[READ][BACKGROUND][CHARGER_ON], |
| 58 | rec.second.bytes[WRITE][BACKGROUND][CHARGER_ON]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void StoragedService::dumpUidRecordsDebug(int fd, const vector<uid_record>& entries) { |
| 63 | for (const auto& record : entries) { |
| 64 | const io_usage& uid_usage = record.ios.uid_ios; |
| 65 | dprintf(fd, "%s_%d %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 |
| 66 | " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 67 | record.name.c_str(), record.ios.user_id, |
| 68 | uid_usage.bytes[READ][FOREGROUND][CHARGER_OFF], |
| 69 | uid_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF], |
| 70 | uid_usage.bytes[READ][BACKGROUND][CHARGER_OFF], |
| 71 | uid_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF], |
| 72 | uid_usage.bytes[READ][FOREGROUND][CHARGER_ON], |
| 73 | uid_usage.bytes[WRITE][FOREGROUND][CHARGER_ON], |
| 74 | uid_usage.bytes[READ][BACKGROUND][CHARGER_ON], |
| 75 | uid_usage.bytes[WRITE][BACKGROUND][CHARGER_ON]); |
| 76 | |
| 77 | for (const auto& task_it : record.ios.task_ios) { |
| 78 | const io_usage& task_usage = task_it.second; |
| 79 | const string& comm = task_it.first; |
| 80 | dprintf(fd, "-> %s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 |
| 81 | " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 82 | comm.c_str(), |
| 83 | task_usage.bytes[READ][FOREGROUND][CHARGER_OFF], |
| 84 | task_usage.bytes[WRITE][FOREGROUND][CHARGER_OFF], |
| 85 | task_usage.bytes[READ][BACKGROUND][CHARGER_OFF], |
| 86 | task_usage.bytes[WRITE][BACKGROUND][CHARGER_OFF], |
| 87 | task_usage.bytes[READ][FOREGROUND][CHARGER_ON], |
| 88 | task_usage.bytes[WRITE][FOREGROUND][CHARGER_ON], |
| 89 | task_usage.bytes[READ][BACKGROUND][CHARGER_ON], |
| 90 | task_usage.bytes[WRITE][BACKGROUND][CHARGER_ON]); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 95 | status_t StoragedService::dump(int fd, const Vector<String16>& args) { |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 96 | IPCThreadState* self = IPCThreadState::self(); |
| 97 | const int pid = self->getCallingPid(); |
| 98 | const int uid = self->getCallingUid(); |
| 99 | if ((uid != AID_SHELL) && |
| 100 | !PermissionCache::checkPermission( |
| 101 | String16("android.permission.DUMP"), pid, uid)) { |
| 102 | return PERMISSION_DENIED; |
| 103 | } |
| 104 | |
Jin Qian | dd41d6b | 2017-02-10 17:23:16 -0800 | [diff] [blame] | 105 | double hours = 0; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 106 | int time_window = 0; |
| 107 | uint64_t threshold = 0; |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame] | 108 | bool force_report = false; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 109 | bool debug = false; |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 110 | for (size_t i = 0; i < args.size(); i++) { |
| 111 | const auto& arg = args[i]; |
| 112 | if (arg == String16("--hours")) { |
| 113 | if (++i >= args.size()) |
| 114 | break; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 115 | if(!ParseDouble(String8(args[i]).c_str(), &hours)) |
| 116 | return BAD_VALUE; |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 117 | continue; |
| 118 | } |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 119 | if (arg == String16("--time_window")) { |
| 120 | if (++i >= args.size()) |
| 121 | break; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 122 | if(!ParseInt(String8(args[i]).c_str(), &time_window)) |
| 123 | return BAD_VALUE; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | if (arg == String16("--threshold")) { |
| 127 | if (++i >= args.size()) |
| 128 | break; |
Jin Qian | 9b1aeae | 2017-03-14 11:20:02 -0700 | [diff] [blame] | 129 | if(!ParseUint(String8(args[i]).c_str(), &threshold)) |
| 130 | return BAD_VALUE; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 131 | continue; |
| 132 | } |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame] | 133 | if (arg == String16("--force")) { |
| 134 | force_report = true; |
| 135 | continue; |
| 136 | } |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 137 | if (arg == String16("--debug")) { |
| 138 | debug = true; |
| 139 | continue; |
| 140 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 143 | uint64_t last_ts = 0; |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 144 | map<uint64_t, struct uid_records> records = |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 145 | storaged_sp->get_uid_records(hours, threshold, force_report); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 146 | for (const auto& it : records) { |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 147 | if (last_ts != it.second.start_ts) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 148 | dprintf(fd, "%" PRIu64, it.second.start_ts); |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 149 | } |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 150 | dprintf(fd, ",%" PRIu64 "\n", it.first); |
Jin Qian | 8157775 | 2017-02-21 12:09:39 -0800 | [diff] [blame] | 151 | last_ts = it.first; |
| 152 | |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 153 | if (!debug) { |
| 154 | dumpUidRecords(fd, it.second.entries); |
| 155 | } else { |
| 156 | dumpUidRecordsDebug(fd, it.second.entries); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 157 | } |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 158 | } |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 159 | |
| 160 | if (time_window) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 161 | storaged_sp->update_uid_io_interval(time_window); |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 164 | return NO_ERROR; |
| 165 | } |
| 166 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 167 | binder::Status StoragedService::onUserStarted(int32_t userId) { |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 168 | storaged_sp->add_user_ce(userId); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 169 | return binder::Status::ok(); |
| 170 | } |
| 171 | |
| 172 | binder::Status StoragedService::onUserStopped(int32_t userId) { |
Jin Qian | 6df3bc6 | 2017-10-18 17:52:14 -0700 | [diff] [blame] | 173 | storaged_sp->remove_user_ce(userId); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 174 | return binder::Status::ok(); |
| 175 | } |
| 176 | |
Michael Wachenschwanz | 37b912b | 2017-12-14 18:20:26 -0800 | [diff] [blame] | 177 | binder::Status StoragedService::getRecentPerf(int32_t* _aidl_return) { |
| 178 | uint32_t recent_perf = storaged_sp->get_recent_perf(); |
| 179 | if (recent_perf > INT32_MAX) { |
| 180 | *_aidl_return = INT32_MAX; |
| 181 | } else { |
| 182 | *_aidl_return = static_cast<int32_t>(recent_perf); |
| 183 | } |
| 184 | return binder::Status::ok(); |
| 185 | } |
| 186 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 187 | status_t StoragedPrivateService::start() { |
| 188 | return BinderService<StoragedPrivateService>::publish(); |
| 189 | } |
| 190 | |
| 191 | binder::Status StoragedPrivateService::dumpUids( |
| 192 | vector<::android::os::storaged::UidInfo>* _aidl_return) { |
| 193 | unordered_map<uint32_t, uid_info> uids_m = storaged_sp->get_uids(); |
| 194 | |
| 195 | for (const auto& it : uids_m) { |
| 196 | UidInfo uinfo; |
| 197 | uinfo.uid = it.second.uid; |
| 198 | uinfo.name = it.second.name; |
| 199 | uinfo.tasks = it.second.tasks; |
| 200 | memcpy(&uinfo.io, &it.second.io, sizeof(uinfo.io)); |
| 201 | _aidl_return->push_back(uinfo); |
| 202 | } |
| 203 | return binder::Status::ok(); |
| 204 | } |
| 205 | |
| 206 | binder::Status StoragedPrivateService::dumpPerfHistory( |
| 207 | vector<int32_t>* _aidl_return) { |
| 208 | *_aidl_return = storaged_sp->get_perf_history(); |
| 209 | return binder::Status::ok(); |
| 210 | } |
| 211 | |
| 212 | sp<IStoragedPrivate> get_storaged_pri_service() { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 213 | sp<IServiceManager> sm = defaultServiceManager(); |
| 214 | if (sm == NULL) return NULL; |
| 215 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 216 | sp<IBinder> binder = sm->getService(String16("storaged_pri")); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 217 | if (binder == NULL) return NULL; |
| 218 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 219 | return interface_cast<IStoragedPrivate>(binder); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 220 | } |