blob: a7bda149281c84dbd7df3fc74c652a8ec3d56112 [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 Cherryb8c11472020-01-10 17:08:15 -080074 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
75
ynwang62cb3722016-06-17 14:30:48 -070076 for (;;) {
77 int opt_idx = 0;
78 static struct option long_options[] = {
Jin Qianb049d182017-10-12 17:02:17 -070079 {"perf", no_argument, nullptr, 'p'},
80 {"start", no_argument, nullptr, 's'},
81 {"task", no_argument, nullptr, 't'},
82 {"uid", no_argument, nullptr, 'u'},
83 {nullptr, 0, nullptr, 0}
ynwang62cb3722016-06-17 14:30:48 -070084 };
Jin Qianb049d182017-10-12 17:02:17 -070085 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070086 if (opt == -1) {
87 break;
88 }
89
90 switch (opt) {
Jin Qianb049d182017-10-12 17:02:17 -070091 case 'p':
92 flag_dump_perf = true;
93 break;
ynwang62cb3722016-06-17 14:30:48 -070094 case 's':
Yang Jin3906c892017-06-22 15:18:21 -070095 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -070096 break;
Yang Jin3906c892017-06-22 15:18:21 -070097 case 't':
98 flag_dump_task = true;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080099 break;
Jin Qianb049d182017-10-12 17:02:17 -0700100 case 'u':
101 flag_dump_uid = true;
Jin Qiand691d6e2017-09-28 16:02:22 -0700102 break;
Jin Qianb049d182017-10-12 17:02:17 -0700103 default:
ynwang62cb3722016-06-17 14:30:48 -0700104 help_message();
105 return 0;
ynwang62cb3722016-06-17 14:30:48 -0700106 }
107 }
108
109 if (argc == 1) {
Yang Jin3906c892017-06-22 15:18:21 -0700110 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700111 }
112
Yang Jin3906c892017-06-22 15:18:21 -0700113 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
ynwang62cb3722016-06-17 14:30:48 -0700114 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
115 help_message();
116 return -1;
117 }
118
ynwang62cb3722016-06-17 14:30:48 -0700119 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700120 // Start the main thread of storaged
shaozhongqicda9b2a2020-02-11 15:27:10 +0800121 storaged_sp = new storaged_t();
122 storaged_sp->init();
123 storaged_sp->report_storage_info();
ynwang62cb3722016-06-17 14:30:48 -0700124 pthread_t storaged_main_thread;
Jin Qian4882eab2017-04-03 18:05:01 -0700125 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
Jin Qian535ddbe2017-01-11 17:19:21 -0800126 if (errno != 0) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800127 PLOG(ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700128 return -1;
129 }
130
Jin Qianb049d182017-10-12 17:02:17 -0700131 if (StoragedService::start() != android::OK ||
132 StoragedPrivateService::start() != android::OK) {
Tom Cherryb8c11472020-01-10 17:08:15 -0800133 PLOG(ERROR) << "Failed to start storaged service";
Jin Qianb049d182017-10-12 17:02:17 -0700134 return -1;
135 }
136
ynwang62cb3722016-06-17 14:30:48 -0700137 android::ProcessState::self()->startThreadPool();
138 IPCThreadState::self()->joinThreadPool();
139 pthread_join(storaged_main_thread, NULL);
140
ynwang62cb3722016-06-17 14:30:48 -0700141 return 0;
142 }
143
Jin Qianb049d182017-10-12 17:02:17 -0700144 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
Jin Qiand691d6e2017-09-28 16:02:22 -0700145 if (storaged_service == NULL) {
146 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
147 return -1;
148 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800149
Jin Qiand691d6e2017-09-28 16:02:22 -0700150 if (flag_dump_uid || flag_dump_task) {
Jin Qianb049d182017-10-12 17:02:17 -0700151 vector<UidInfo> uid_io;
152 binder::Status status = storaged_service->dumpUids(&uid_io);
153 if (!status.isOk() || uid_io.size() == 0) {
154 fprintf(stderr, "UID I/O info is not available.\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800155 return 0;
156 }
157
Jin Qianb049d182017-10-12 17:02:17 -0700158 sort_running_uids_info(uid_io);
159 log_console_running_uids_info(uid_io, flag_dump_task);
Jin Qiand691d6e2017-09-28 16:02:22 -0700160 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800161
Jin Qiand691d6e2017-09-28 16:02:22 -0700162 if (flag_dump_perf) {
Jin Qianb049d182017-10-12 17:02:17 -0700163 vector<int> perf_history;
164 binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
165 if (!status.isOk() || perf_history.size() == 0) {
166 fprintf(stderr, "I/O perf history is not available.\n");
Jin Qiand691d6e2017-09-28 16:02:22 -0700167 return 0;
168 }
169
Jin Qianb049d182017-10-12 17:02:17 -0700170 log_console_perf_history(perf_history);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800171 }
172
ynwang62cb3722016-06-17 14:30:48 -0700173 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700174}