blob: e35bd6fe7fed95f2076d83cc4f752a9f73fc9ae0 [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 */) {
Jin Qianb049d182017-10-12 17:02:17 -070049 storaged_sp = new storaged_t();
ynwang62cb3722016-06-17 14:30:48 -070050
Yifan Hong4a43bdc2018-01-16 17:20:32 -080051 storaged_sp->init();
Jin Qianb049d182017-10-12 17:02:17 -070052 storaged_sp->report_storage_info();
Jin Qian5b962c62017-01-30 14:48:38 -080053
Tom Cherryb8c11472020-01-10 17:08:15 -080054 LOG(INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070055
56 for (;;) {
Jin Qianb049d182017-10-12 17:02:17 -070057 storaged_sp->event_checked();
58 storaged_sp->pause();
ynwang62cb3722016-06-17 14:30:48 -070059 }
60 return NULL;
61}
62
Jin Qian65dea712017-08-29 16:48:20 -070063void help_message(void) {
ynwang62cb3722016-06-17 14:30:48 -070064 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080065 printf(" -u --uid Dump uid I/O usage to stdout\n");
Yang Jin3906c892017-06-22 15:18:21 -070066 printf(" -t --task Dump task I/O usage to stdout\n");
Jin Qiand691d6e2017-09-28 16:02:22 -070067 printf(" -p --perf Dump I/O perf history to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -070068 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -070069 fflush(stdout);
70}
71
ynwang62cb3722016-06-17 14:30:48 -070072int main(int argc, char** argv) {
Yang Jin3906c892017-06-22 15:18:21 -070073 bool flag_main_service = false;
74 bool flag_dump_uid = false;
75 bool flag_dump_task = false;
Jin Qiand691d6e2017-09-28 16:02:22 -070076 bool flag_dump_perf = false;
ynwang62cb3722016-06-17 14:30:48 -070077 int opt;
78
Tom Cherryb8c11472020-01-10 17:08:15 -080079 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
80
ynwang62cb3722016-06-17 14:30:48 -070081 for (;;) {
82 int opt_idx = 0;
83 static struct option long_options[] = {
Jin Qianb049d182017-10-12 17:02:17 -070084 {"perf", no_argument, nullptr, 'p'},
85 {"start", no_argument, nullptr, 's'},
86 {"task", no_argument, nullptr, 't'},
87 {"uid", no_argument, nullptr, 'u'},
88 {nullptr, 0, nullptr, 0}
ynwang62cb3722016-06-17 14:30:48 -070089 };
Jin Qianb049d182017-10-12 17:02:17 -070090 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070091 if (opt == -1) {
92 break;
93 }
94
95 switch (opt) {
Jin Qianb049d182017-10-12 17:02:17 -070096 case 'p':
97 flag_dump_perf = true;
98 break;
ynwang62cb3722016-06-17 14:30:48 -070099 case 's':
Yang Jin3906c892017-06-22 15:18:21 -0700100 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700101 break;
Yang Jin3906c892017-06-22 15:18:21 -0700102 case 't':
103 flag_dump_task = true;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800104 break;
Jin Qianb049d182017-10-12 17:02:17 -0700105 case 'u':
106 flag_dump_uid = true;
Jin Qiand691d6e2017-09-28 16:02:22 -0700107 break;
Jin Qianb049d182017-10-12 17:02:17 -0700108 default:
ynwang62cb3722016-06-17 14:30:48 -0700109 help_message();
110 return 0;
ynwang62cb3722016-06-17 14:30:48 -0700111 }
112 }
113
114 if (argc == 1) {
Yang Jin3906c892017-06-22 15:18:21 -0700115 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700116 }
117
Yang Jin3906c892017-06-22 15:18:21 -0700118 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
ynwang62cb3722016-06-17 14:30:48 -0700119 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
120 help_message();
121 return -1;
122 }
123
ynwang62cb3722016-06-17 14:30:48 -0700124 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700125 // Start the main thread of storaged
126 pthread_t storaged_main_thread;
Jin Qian4882eab2017-04-03 18:05:01 -0700127 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
Jin Qian535ddbe2017-01-11 17:19:21 -0800128 if (errno != 0) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800129 PLOG(ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700130 return -1;
131 }
132
Jin Qianb049d182017-10-12 17:02:17 -0700133 if (StoragedService::start() != android::OK ||
134 StoragedPrivateService::start() != android::OK) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800135 PLOG(ERROR) << "Failed to start storaged service";
Jin Qianb049d182017-10-12 17:02:17 -0700136 return -1;
137 }
138
ynwang62cb3722016-06-17 14:30:48 -0700139 android::ProcessState::self()->startThreadPool();
140 IPCThreadState::self()->joinThreadPool();
141 pthread_join(storaged_main_thread, NULL);
142
ynwang62cb3722016-06-17 14:30:48 -0700143 return 0;
144 }
145
Jin Qianb049d182017-10-12 17:02:17 -0700146 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
Jin Qiand691d6e2017-09-28 16:02:22 -0700147 if (storaged_service == NULL) {
148 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
149 return -1;
150 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800151
Jin Qiand691d6e2017-09-28 16:02:22 -0700152 if (flag_dump_uid || flag_dump_task) {
Jin Qianb049d182017-10-12 17:02:17 -0700153 vector<UidInfo> uid_io;
154 binder::Status status = storaged_service->dumpUids(&uid_io);
155 if (!status.isOk() || uid_io.size() == 0) {
156 fprintf(stderr, "UID I/O info is not available.\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800157 return 0;
158 }
159
Jin Qianb049d182017-10-12 17:02:17 -0700160 sort_running_uids_info(uid_io);
161 log_console_running_uids_info(uid_io, flag_dump_task);
Jin Qiand691d6e2017-09-28 16:02:22 -0700162 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800163
Jin Qiand691d6e2017-09-28 16:02:22 -0700164 if (flag_dump_perf) {
Jin Qianb049d182017-10-12 17:02:17 -0700165 vector<int> perf_history;
166 binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
167 if (!status.isOk() || perf_history.size() == 0) {
168 fprintf(stderr, "I/O perf history is not available.\n");
Jin Qiand691d6e2017-09-28 16:02:22 -0700169 return 0;
170 }
171
Jin Qianb049d182017-10-12 17:02:17 -0700172 log_console_perf_history(perf_history);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800173 }
174
ynwang62cb3722016-06-17 14:30:48 -0700175 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700176}