main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #define LOG_TAG "storaged"
  17. #define KLOG_LEVEL 6
  18. #include <fcntl.h>
  19. #include <getopt.h>
  20. #include <pthread.h>
  21. #include <stdio.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <vector>
  25. #include <android-base/macros.h>
  26. #include <android-base/logging.h>
  27. #include <android-base/stringprintf.h>
  28. #include <binder/ProcessState.h>
  29. #include <binder/IServiceManager.h>
  30. #include <binder/IPCThreadState.h>
  31. #include <cutils/android_get_control_file.h>
  32. #include <cutils/sched_policy.h>
  33. #include <private/android_filesystem_config.h>
  34. #include <storaged.h>
  35. #include <storaged_service.h>
  36. #include <storaged_utils.h>
  37. using namespace std;
  38. using namespace android;
  39. sp<storaged_t> storaged_sp;
  40. // Function of storaged's main thread
  41. void* storaged_main(void* /* unused */) {
  42. storaged_sp = new storaged_t();
  43. storaged_sp->init();
  44. storaged_sp->report_storage_info();
  45. LOG_TO(SYSTEM, INFO) << "storaged: Start";
  46. for (;;) {
  47. storaged_sp->event_checked();
  48. storaged_sp->pause();
  49. }
  50. return NULL;
  51. }
  52. void help_message(void) {
  53. printf("usage: storaged [OPTION]\n");
  54. printf(" -u --uid Dump uid I/O usage to stdout\n");
  55. printf(" -t --task Dump task I/O usage to stdout\n");
  56. printf(" -p --perf Dump I/O perf history to stdout\n");
  57. printf(" -s --start Start storaged (default)\n");
  58. fflush(stdout);
  59. }
  60. int main(int argc, char** argv) {
  61. bool flag_main_service = false;
  62. bool flag_dump_uid = false;
  63. bool flag_dump_task = false;
  64. bool flag_dump_perf = false;
  65. int opt;
  66. for (;;) {
  67. int opt_idx = 0;
  68. static struct option long_options[] = {
  69. {"perf", no_argument, nullptr, 'p'},
  70. {"start", no_argument, nullptr, 's'},
  71. {"task", no_argument, nullptr, 't'},
  72. {"uid", no_argument, nullptr, 'u'},
  73. {nullptr, 0, nullptr, 0}
  74. };
  75. opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
  76. if (opt == -1) {
  77. break;
  78. }
  79. switch (opt) {
  80. case 'p':
  81. flag_dump_perf = true;
  82. break;
  83. case 's':
  84. flag_main_service = true;
  85. break;
  86. case 't':
  87. flag_dump_task = true;
  88. break;
  89. case 'u':
  90. flag_dump_uid = true;
  91. break;
  92. default:
  93. help_message();
  94. return 0;
  95. }
  96. }
  97. if (argc == 1) {
  98. flag_main_service = true;
  99. }
  100. if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
  101. fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
  102. help_message();
  103. return -1;
  104. }
  105. if (flag_main_service) { // start main thread
  106. // Start the main thread of storaged
  107. pthread_t storaged_main_thread;
  108. errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
  109. if (errno != 0) {
  110. PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
  111. return -1;
  112. }
  113. if (StoragedService::start() != android::OK ||
  114. StoragedPrivateService::start() != android::OK) {
  115. PLOG_TO(SYSTEM, ERROR) << "Failed to start storaged service";
  116. return -1;
  117. }
  118. android::ProcessState::self()->startThreadPool();
  119. IPCThreadState::self()->joinThreadPool();
  120. pthread_join(storaged_main_thread, NULL);
  121. return 0;
  122. }
  123. sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
  124. if (storaged_service == NULL) {
  125. fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
  126. return -1;
  127. }
  128. if (flag_dump_uid || flag_dump_task) {
  129. vector<UidInfo> uid_io;
  130. binder::Status status = storaged_service->dumpUids(&uid_io);
  131. if (!status.isOk() || uid_io.size() == 0) {
  132. fprintf(stderr, "UID I/O info is not available.\n");
  133. return 0;
  134. }
  135. sort_running_uids_info(uid_io);
  136. log_console_running_uids_info(uid_io, flag_dump_task);
  137. }
  138. if (flag_dump_perf) {
  139. vector<int> perf_history;
  140. binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
  141. if (!status.isOk() || perf_history.size() == 0) {
  142. fprintf(stderr, "I/O perf history is not available.\n");
  143. return 0;
  144. }
  145. log_console_perf_history(perf_history);
  146. }
  147. return 0;
  148. }