storaged_service.cpp 7.6 KB

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