blob: 0bc36e56bc2262f05845334d19014962f7b4556d [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
Jin Qianb049d182017-10-12 17:02:17 -070054 storaged_sp->load_proto();
55 storaged_sp->init_battery_service();
56 storaged_sp->report_storage_info();
Jin Qian5b962c62017-01-30 14:48:38 -080057
Jin Qian535ddbe2017-01-11 17:19:21 -080058 LOG_TO(SYSTEM, INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070059
60 for (;;) {
Jin Qianb049d182017-10-12 17:02:17 -070061 storaged_sp->event_checked();
62 storaged_sp->pause();
ynwang62cb3722016-06-17 14:30:48 -070063 }
64 return NULL;
65}
66
Jin Qian65dea712017-08-29 16:48:20 -070067void help_message(void) {
ynwang62cb3722016-06-17 14:30:48 -070068 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080069 printf(" -u --uid Dump uid I/O usage to stdout\n");
Yang Jin3906c892017-06-22 15:18:21 -070070 printf(" -t --task Dump task I/O usage to stdout\n");
Jin Qiand691d6e2017-09-28 16:02:22 -070071 printf(" -p --perf Dump I/O perf history to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -070072 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -070073 fflush(stdout);
74}
75
ynwang62cb3722016-06-17 14:30:48 -070076int main(int argc, char** argv) {
Yang Jin3906c892017-06-22 15:18:21 -070077 bool flag_main_service = false;
78 bool flag_dump_uid = false;
79 bool flag_dump_task = false;
Jin Qiand691d6e2017-09-28 16:02:22 -070080 bool flag_dump_perf = false;
ynwang62cb3722016-06-17 14:30:48 -070081 int opt;
82
83 for (;;) {
84 int opt_idx = 0;
85 static struct option long_options[] = {
Jin Qianb049d182017-10-12 17:02:17 -070086 {"perf", no_argument, nullptr, 'p'},
87 {"start", no_argument, nullptr, 's'},
88 {"task", no_argument, nullptr, 't'},
89 {"uid", no_argument, nullptr, 'u'},
90 {nullptr, 0, nullptr, 0}
ynwang62cb3722016-06-17 14:30:48 -070091 };
Jin Qianb049d182017-10-12 17:02:17 -070092 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070093 if (opt == -1) {
94 break;
95 }
96
97 switch (opt) {
Jin Qianb049d182017-10-12 17:02:17 -070098 case 'p':
99 flag_dump_perf = true;
100 break;
ynwang62cb3722016-06-17 14:30:48 -0700101 case 's':
Yang Jin3906c892017-06-22 15:18:21 -0700102 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700103 break;
Yang Jin3906c892017-06-22 15:18:21 -0700104 case 't':
105 flag_dump_task = true;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800106 break;
Jin Qianb049d182017-10-12 17:02:17 -0700107 case 'u':
108 flag_dump_uid = true;
Jin Qiand691d6e2017-09-28 16:02:22 -0700109 break;
Jin Qianb049d182017-10-12 17:02:17 -0700110 default:
ynwang62cb3722016-06-17 14:30:48 -0700111 help_message();
112 return 0;
ynwang62cb3722016-06-17 14:30:48 -0700113 }
114 }
115
116 if (argc == 1) {
Yang Jin3906c892017-06-22 15:18:21 -0700117 flag_main_service = true;
ynwang62cb3722016-06-17 14:30:48 -0700118 }
119
Yang Jin3906c892017-06-22 15:18:21 -0700120 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
ynwang62cb3722016-06-17 14:30:48 -0700121 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
122 help_message();
123 return -1;
124 }
125
ynwang62cb3722016-06-17 14:30:48 -0700126 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700127 // Start the main thread of storaged
128 pthread_t storaged_main_thread;
Jin Qian4882eab2017-04-03 18:05:01 -0700129 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
Jin Qian535ddbe2017-01-11 17:19:21 -0800130 if (errno != 0) {
131 PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700132 return -1;
133 }
134
Jin Qianb049d182017-10-12 17:02:17 -0700135 if (StoragedService::start() != android::OK ||
136 StoragedPrivateService::start() != android::OK) {
137 PLOG_TO(SYSTEM, ERROR) << "Failed to start storaged service";
138 return -1;
139 }
140
ynwang62cb3722016-06-17 14:30:48 -0700141 android::ProcessState::self()->startThreadPool();
142 IPCThreadState::self()->joinThreadPool();
143 pthread_join(storaged_main_thread, NULL);
144
ynwang62cb3722016-06-17 14:30:48 -0700145 return 0;
146 }
147
Jin Qianb049d182017-10-12 17:02:17 -0700148 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
Jin Qiand691d6e2017-09-28 16:02:22 -0700149 if (storaged_service == NULL) {
150 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
151 return -1;
152 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800153
Jin Qiand691d6e2017-09-28 16:02:22 -0700154 if (flag_dump_uid || flag_dump_task) {
Jin Qianb049d182017-10-12 17:02:17 -0700155 vector<UidInfo> uid_io;
156 binder::Status status = storaged_service->dumpUids(&uid_io);
157 if (!status.isOk() || uid_io.size() == 0) {
158 fprintf(stderr, "UID I/O info is not available.\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800159 return 0;
160 }
161
Jin Qianb049d182017-10-12 17:02:17 -0700162 sort_running_uids_info(uid_io);
163 log_console_running_uids_info(uid_io, flag_dump_task);
Jin Qiand691d6e2017-09-28 16:02:22 -0700164 }
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800165
Jin Qiand691d6e2017-09-28 16:02:22 -0700166 if (flag_dump_perf) {
Jin Qianb049d182017-10-12 17:02:17 -0700167 vector<int> perf_history;
168 binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
169 if (!status.isOk() || perf_history.size() == 0) {
170 fprintf(stderr, "I/O perf history is not available.\n");
Jin Qiand691d6e2017-09-28 16:02:22 -0700171 return 0;
172 }
173
Jin Qianb049d182017-10-12 17:02:17 -0700174 log_console_perf_history(perf_history);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800175 }
176
ynwang62cb3722016-06-17 14:30:48 -0700177 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700178}