blob: bbed210de280fdde724ce7ec0728fdd1ea344194 [file] [log] [blame]
ynwang62cb3722016-06-17 14:30:48 -07001/*
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>
ynwang62cb3722016-06-17 14:30:48 -070024#include <sys/stat.h>
25#include <sys/types.h>
26#include <vector>
27
28#include <android-base/macros.h>
Jin Qian535ddbe2017-01-11 17:19:21 -080029#include <android-base/logging.h>
ynwang62cb3722016-06-17 14:30:48 -070030#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>
ynwang62cb3722016-06-17 14:30:48 -070035#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 Qiand691d6e2017-09-28 16:02:22 -070042using namespace std;
Jin Qianb049d182017-10-12 17:02:17 -070043using namespace android;
Jin Qiand691d6e2017-09-28 16:02:22 -070044
Jin Qianb049d182017-10-12 17:02:17 -070045sp<storaged_t> storaged_sp;
ynwang62cb3722016-06-17 14:30:48 -070046
ynwang62cb3722016-06-17 14:30:48 -070047// Function of storaged's main thread
Jin Qian4882eab2017-04-03 18:05:01 -070048void* storaged_main(void* /* unused */) {
Tom Cherryb8c11472020-01-10 17:08:15 -080049 LOG(INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070050
51 for (;;) {
Jin Qianb049d182017-10-12 17:02:17 -070052 storaged_sp->event_checked();
53 storaged_sp->pause();
ynwang62cb3722016-06-17 14:30:48 -070054 }
55 return NULL;
56}
57
Jin Qian65dea712017-08-29 16:48:20 -070058void help_message(void) {
ynwang62cb3722016-06-17 14:30:48 -070059 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080060 printf(" -u --uid Dump uid I/O usage to stdout\n");
Yang Jin3906c892017-06-22 15:18:21 -070061 printf(" -t --task Dump task I/O usage to stdout\n");
Jin Qiand691d6e2017-09-28 16:02:22 -070062 printf(" -p --perf Dump I/O perf history to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -070063 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -070064 fflush(stdout);
65}
66
ynwang62cb3722016-06-17 14:30:48 -070067int main(int argc, char** argv) {
Yang Jin3906c892017-06-22 15:18:21 -070068 bool flag_main_service = false;
69 bool flag_dump_uid = false;
70 bool flag_dump_task = false;
Jin Qiand691d6e2017-09-28 16:02:22 -070071 bool flag_dump_perf = false;
ynwang62cb3722016-06-17 14:30:48 -070072 int opt;
73
Tom Cherry3f3b1702020-03-17 09:31:01 -070074 signal(SIGPIPE, SIG_IGN);
Tom Cherryb8c11472020-01-10 17:08:15 -080075 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
76
ynwang62cb3722016-06-17 14:30:48 -070077 for (;;) {
78 int opt_idx = 0;
79 static struct option long_options[] = {
Jin Qianb049d182017-10-12 17:02:17 -070080 {"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}
ynwang62cb3722016-06-17 14:30:48 -070085 };
Jin Qianb049d182017-10-12 17:02:17 -070086 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070087 if (opt == -1) {
88 break;
89 }
90
91 switch (opt) {
Jin Qianb049d182017-10-12 17:02:17 -070092 case 'p':
93 flag_dump_perf = true;
94 break;
ynwang62cb3722016-06-17 14:30:48 -070095 case 's':
Yang Jin3906c892017-06-22 15:18:21 -070096 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -070097 break;
Yang Jin3906c892017-06-22 15:18:21 -070098 case 't':
99 flag_dump_task = true;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800100 break;
Jin Qianb049d182017-10-12 17:02:17 -0700101 case 'u':
102 flag_dump_uid = true;
Jin Qiand691d6e2017-09-28 16:02:22 -0700103 break;
Jin Qianb049d182017-10-12 17:02:17 -0700104 default:
ynwang62cb3722016-06-17 14:30:48 -0700105 help_message();
106 return 0;
ynwang62cb3722016-06-17 14:30:48 -0700107 }
108 }
109
110 if (argc == 1) {
Yang Jin3906c892017-06-22 15:18:21 -0700111 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700112 }
113
Yang Jin3906c892017-06-22 15:18:21 -0700114 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
ynwang62cb3722016-06-17 14:30:48 -0700115 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
116 help_message();
117 return -1;
118 }
119
ynwang62cb3722016-06-17 14:30:48 -0700120 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700121 // Start the main thread of storaged
shaozhongqicda9b2a2020-02-11 15:27:10 +0800122 storaged_sp = new storaged_t();
123 storaged_sp->init();
124 storaged_sp->report_storage_info();
ynwang62cb3722016-06-17 14:30:48 -0700125 pthread_t storaged_main_thread;
Jin Qian4882eab2017-04-03 18:05:01 -0700126 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
Jin Qian535ddbe2017-01-11 17:19:21 -0800127 if (errno != 0) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800128 PLOG(ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700129 return -1;
130 }
131
Jin Qianb049d182017-10-12 17:02:17 -0700132 if (StoragedService::start() != android::OK ||
133 StoragedPrivateService::start() != android::OK) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800134 PLOG(ERROR) << "Failed to start storaged service";
Jin Qianb049d182017-10-12 17:02:17 -0700135 return -1;
136 }
137
ynwang62cb3722016-06-17 14:30:48 -0700138 android::ProcessState::self()->startThreadPool();
139 IPCThreadState::self()->joinThreadPool();
140 pthread_join(storaged_main_thread, NULL);
141
ynwang62cb3722016-06-17 14:30:48 -0700142 return 0;
143 }
144
Jin Qianb049d182017-10-12 17:02:17 -0700145 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
Jin Qiand691d6e2017-09-28 16:02:22 -0700146 if (storaged_service == NULL) {
147 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
148 return -1;
149 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800150
Jin Qiand691d6e2017-09-28 16:02:22 -0700151 if (flag_dump_uid || flag_dump_task) {
Jin Qianb049d182017-10-12 17:02:17 -0700152 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 Qianbcd6e3b2016-12-28 15:43:51 -0800156 return 0;
157 }
158
Jin Qianb049d182017-10-12 17:02:17 -0700159 sort_running_uids_info(uid_io);
160 log_console_running_uids_info(uid_io, flag_dump_task);
Jin Qiand691d6e2017-09-28 16:02:22 -0700161 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800162
Jin Qiand691d6e2017-09-28 16:02:22 -0700163 if (flag_dump_perf) {
Jin Qianb049d182017-10-12 17:02:17 -0700164 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 Qiand691d6e2017-09-28 16:02:22 -0700168 return 0;
169 }
170
Jin Qianb049d182017-10-12 17:02:17 -0700171 log_console_perf_history(perf_history);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800172 }
173
ynwang62cb3722016-06-17 14:30:48 -0700174 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700175}