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 | |
| 17 | #define LOG_TAG "storaged" |
| 18 | #define KLOG_LEVEL 6 |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdio.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include <android-base/macros.h> |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 29 | #include <android-base/logging.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
| 31 | #include <binder/ProcessState.h> |
| 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/IPCThreadState.h> |
| 34 | #include <cutils/android_get_control_file.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> |
| 36 | #include <private/android_filesystem_config.h> |
| 37 | |
| 38 | #include <storaged.h> |
| 39 | #include <storaged_service.h> |
| 40 | #include <storaged_utils.h> |
| 41 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 42 | using namespace std; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 43 | using namespace android; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 44 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 45 | sp<storaged_t> storaged_sp; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 46 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 47 | // Function of storaged's main thread |
Jin Qian | 4882eab | 2017-04-03 18:05:01 -0700 | [diff] [blame] | 48 | void* storaged_main(void* /* unused */) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 49 | LOG(INFO) << "storaged: Start"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 50 | |
| 51 | for (;;) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 52 | storaged_sp->event_checked(); |
| 53 | storaged_sp->pause(); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 54 | } |
| 55 | return NULL; |
| 56 | } |
| 57 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 58 | void help_message(void) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 59 | printf("usage: storaged [OPTION]\n"); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 60 | printf(" -u --uid Dump uid I/O usage to stdout\n"); |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 61 | printf(" -t --task Dump task I/O usage to stdout\n"); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 62 | printf(" -p --perf Dump I/O perf history to stdout\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 63 | printf(" -s --start Start storaged (default)\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 64 | fflush(stdout); |
| 65 | } |
| 66 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 67 | int main(int argc, char** argv) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 68 | bool flag_main_service = false; |
| 69 | bool flag_dump_uid = false; |
| 70 | bool flag_dump_task = false; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 71 | bool flag_dump_perf = false; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 72 | int opt; |
| 73 | |
Tom Cherry | 3f3b170 | 2020-03-17 09:31:01 -0700 | [diff] [blame] | 74 | signal(SIGPIPE, SIG_IGN); |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 75 | android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM)); |
| 76 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 77 | for (;;) { |
| 78 | int opt_idx = 0; |
| 79 | static struct option long_options[] = { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 80 | {"perf", no_argument, nullptr, 'p'}, |
| 81 | {"start", no_argument, nullptr, 's'}, |
| 82 | {"task", no_argument, nullptr, 't'}, |
| 83 | {"uid", no_argument, nullptr, 'u'}, |
| 84 | {nullptr, 0, nullptr, 0} |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 85 | }; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 86 | opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 87 | if (opt == -1) { |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | switch (opt) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 92 | case 'p': |
| 93 | flag_dump_perf = true; |
| 94 | break; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 95 | case 's': |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 96 | flag_main_service = true; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 97 | break; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 98 | case 't': |
| 99 | flag_dump_task = true; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 100 | break; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 101 | case 'u': |
| 102 | flag_dump_uid = true; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 103 | break; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 104 | default: |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 105 | help_message(); |
| 106 | return 0; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | if (argc == 1) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 111 | flag_main_service = true; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 114 | if (flag_main_service && (flag_dump_uid || flag_dump_task)) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 115 | fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n"); |
| 116 | help_message(); |
| 117 | return -1; |
| 118 | } |
| 119 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 120 | if (flag_main_service) { // start main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 121 | // Start the main thread of storaged |
shaozhongqi | cda9b2a | 2020-02-11 15:27:10 +0800 | [diff] [blame] | 122 | storaged_sp = new storaged_t(); |
| 123 | storaged_sp->init(); |
| 124 | storaged_sp->report_storage_info(); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 125 | pthread_t storaged_main_thread; |
Jin Qian | 4882eab | 2017-04-03 18:05:01 -0700 | [diff] [blame] | 126 | errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL); |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 127 | if (errno != 0) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 128 | PLOG(ERROR) << "Failed to create main thread"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 132 | if (StoragedService::start() != android::OK || |
| 133 | StoragedPrivateService::start() != android::OK) { |
Tom Cherry | b8c1147 | 2020-01-10 17:08:15 -0800 | [diff] [blame] | 134 | PLOG(ERROR) << "Failed to start storaged service"; |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 138 | android::ProcessState::self()->startThreadPool(); |
| 139 | IPCThreadState::self()->joinThreadPool(); |
| 140 | pthread_join(storaged_main_thread, NULL); |
| 141 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 145 | sp<IStoragedPrivate> storaged_service = get_storaged_pri_service(); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 146 | if (storaged_service == NULL) { |
| 147 | fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n"); |
| 148 | return -1; |
| 149 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 150 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 151 | if (flag_dump_uid || flag_dump_task) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 152 | vector<UidInfo> uid_io; |
| 153 | binder::Status status = storaged_service->dumpUids(&uid_io); |
| 154 | if (!status.isOk() || uid_io.size() == 0) { |
| 155 | fprintf(stderr, "UID I/O info is not available.\n"); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 159 | sort_running_uids_info(uid_io); |
| 160 | log_console_running_uids_info(uid_io, flag_dump_task); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 161 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 162 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 163 | if (flag_dump_perf) { |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 164 | vector<int> perf_history; |
| 165 | binder::Status status = storaged_service->dumpPerfHistory(&perf_history); |
| 166 | if (!status.isOk() || perf_history.size() == 0) { |
| 167 | fprintf(stderr, "I/O perf history is not available.\n"); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame] | 171 | log_console_perf_history(perf_history); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 172 | } |
| 173 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 174 | return 0; |
ynwang | af49d97 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 175 | } |