blob: b3f1281fc7654361bf0741729d0be284e1c9cdcf [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>
24#include <sys/capability.h>
25#include <sys/prctl.h>
26#include <sys/resource.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <vector>
30
31#include <android-base/macros.h>
Jin Qian535ddbe2017-01-11 17:19:21 -080032#include <android-base/logging.h>
ynwang62cb3722016-06-17 14:30:48 -070033#include <android-base/stringprintf.h>
34#include <binder/ProcessState.h>
35#include <binder/IServiceManager.h>
36#include <binder/IPCThreadState.h>
37#include <cutils/android_get_control_file.h>
ynwang62cb3722016-06-17 14:30:48 -070038#include <cutils/sched_policy.h>
39#include <private/android_filesystem_config.h>
40
41#include <storaged.h>
42#include <storaged_service.h>
43#include <storaged_utils.h>
44
Jin Qiand691d6e2017-09-28 16:02:22 -070045using namespace std;
Jin Qianb049d182017-10-12 17:02:17 -070046using namespace android;
Jin Qiand691d6e2017-09-28 16:02:22 -070047
Jin Qianb049d182017-10-12 17:02:17 -070048sp<storaged_t> storaged_sp;
ynwang62cb3722016-06-17 14:30:48 -070049
ynwang62cb3722016-06-17 14:30:48 -070050// Function of storaged's main thread
Jin Qian4882eab2017-04-03 18:05:01 -070051void* storaged_main(void* /* unused */) {
Jin Qianb049d182017-10-12 17:02:17 -070052 storaged_sp = new storaged_t();
ynwang62cb3722016-06-17 14:30:48 -070053
Yifan Hong4a43bdc2018-01-16 17:20:32 -080054 storaged_sp->init();
Jin Qianb049d182017-10-12 17:02:17 -070055 storaged_sp->report_storage_info();
Jin Qian5b962c62017-01-30 14:48:38 -080056
Jin Qian535ddbe2017-01-11 17:19:21 -080057 LOG_TO(SYSTEM, INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070058
59 for (;;) {
Jin Qianb049d182017-10-12 17:02:17 -070060 storaged_sp->event_checked();
61 storaged_sp->pause();
ynwang62cb3722016-06-17 14:30:48 -070062 }
63 return NULL;
64}
65
Jin Qian65dea712017-08-29 16:48:20 -070066void help_message(void) {
ynwang62cb3722016-06-17 14:30:48 -070067 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080068 printf(" -u --uid Dump uid I/O usage to stdout\n");
Yang Jin3906c892017-06-22 15:18:21 -070069 printf(" -t --task Dump task I/O usage to stdout\n");
Jin Qiand691d6e2017-09-28 16:02:22 -070070 printf(" -p --perf Dump I/O perf history to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -070071 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -070072 fflush(stdout);
73}
74
ynwang62cb3722016-06-17 14:30:48 -070075int main(int argc, char** argv) {
Yang Jin3906c892017-06-22 15:18:21 -070076 bool flag_main_service = false;
77 bool flag_dump_uid = false;
78 bool flag_dump_task = false;
Jin Qiand691d6e2017-09-28 16:02:22 -070079 bool flag_dump_perf = false;
ynwang62cb3722016-06-17 14:30:48 -070080 int opt;
81
82 for (;;) {
83 int opt_idx = 0;
84 static struct option long_options[] = {
Jin Qianb049d182017-10-12 17:02:17 -070085 {"perf", no_argument, nullptr, 'p'},
86 {"start", no_argument, nullptr, 's'},
87 {"task", no_argument, nullptr, 't'},
88 {"uid", no_argument, nullptr, 'u'},
89 {nullptr, 0, nullptr, 0}
ynwang62cb3722016-06-17 14:30:48 -070090 };
Jin Qianb049d182017-10-12 17:02:17 -070091 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070092 if (opt == -1) {
93 break;
94 }
95
96 switch (opt) {
Jin Qianb049d182017-10-12 17:02:17 -070097 case 'p':
98 flag_dump_perf = true;
99 break;
ynwang62cb3722016-06-17 14:30:48 -0700100 case 's':
Yang Jin3906c892017-06-22 15:18:21 -0700101 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700102 break;
Yang Jin3906c892017-06-22 15:18:21 -0700103 case 't':
104 flag_dump_task = true;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800105 break;
Jin Qianb049d182017-10-12 17:02:17 -0700106 case 'u':
107 flag_dump_uid = true;
Jin Qiand691d6e2017-09-28 16:02:22 -0700108 break;
Jin Qianb049d182017-10-12 17:02:17 -0700109 default:
ynwang62cb3722016-06-17 14:30:48 -0700110 help_message();
111 return 0;
ynwang62cb3722016-06-17 14:30:48 -0700112 }
113 }
114
115 if (argc == 1) {
Yang Jin3906c892017-06-22 15:18:21 -0700116 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700117 }
118
Yang Jin3906c892017-06-22 15:18:21 -0700119 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
ynwang62cb3722016-06-17 14:30:48 -0700120 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
121 help_message();
122 return -1;
123 }
124
ynwang62cb3722016-06-17 14:30:48 -0700125 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700126 // Start the main thread of storaged
127 pthread_t storaged_main_thread;
Jin Qian4882eab2017-04-03 18:05:01 -0700128 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
Jin Qian535ddbe2017-01-11 17:19:21 -0800129 if (errno != 0) {
130 PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700131 return -1;
132 }
133
Jin Qianb049d182017-10-12 17:02:17 -0700134 if (StoragedService::start() != android::OK ||
135 StoragedPrivateService::start() != android::OK) {
136 PLOG_TO(SYSTEM, ERROR) << "Failed to start storaged service";
137 return -1;
138 }
139
ynwang62cb3722016-06-17 14:30:48 -0700140 android::ProcessState::self()->startThreadPool();
141 IPCThreadState::self()->joinThreadPool();
142 pthread_join(storaged_main_thread, NULL);
143
ynwang62cb3722016-06-17 14:30:48 -0700144 return 0;
145 }
146
Jin Qianb049d182017-10-12 17:02:17 -0700147 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
Jin Qiand691d6e2017-09-28 16:02:22 -0700148 if (storaged_service == NULL) {
149 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
150 return -1;
151 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800152
Jin Qiand691d6e2017-09-28 16:02:22 -0700153 if (flag_dump_uid || flag_dump_task) {
Jin Qianb049d182017-10-12 17:02:17 -0700154 vector<UidInfo> uid_io;
155 binder::Status status = storaged_service->dumpUids(&uid_io);
156 if (!status.isOk() || uid_io.size() == 0) {
157 fprintf(stderr, "UID I/O info is not available.\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800158 return 0;
159 }
160
Jin Qianb049d182017-10-12 17:02:17 -0700161 sort_running_uids_info(uid_io);
162 log_console_running_uids_info(uid_io, flag_dump_task);
Jin Qiand691d6e2017-09-28 16:02:22 -0700163 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800164
Jin Qiand691d6e2017-09-28 16:02:22 -0700165 if (flag_dump_perf) {
Jin Qianb049d182017-10-12 17:02:17 -0700166 vector<int> perf_history;
167 binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
168 if (!status.isOk() || perf_history.size() == 0) {
169 fprintf(stderr, "I/O perf history is not available.\n");
Jin Qiand691d6e2017-09-28 16:02:22 -0700170 return 0;
171 }
172
Jin Qianb049d182017-10-12 17:02:17 -0700173 log_console_perf_history(perf_history);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800174 }
175
ynwang62cb3722016-06-17 14:30:48 -0700176 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700177}