1 /*
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/stat.h>
25 #include <sys/types.h>
26 #include <vector>
27
28 #include <android-base/logging.h>
29 #include <android-base/macros.h>
30 #include <android-base/stringprintf.h>
31 #include <binder/IPCThreadState.h>
32 #include <binder/IServiceManager.h>
33 #include <binder/ProcessState.h>
34 #include <cutils/sched_policy.h>
35 #include <private/android_filesystem_config.h>
36
37 #include <storaged.h>
38 #include <storaged_service.h>
39 #include <storaged_utils.h>
40
41 using namespace std;
42 using namespace android;
43
44 sp<storaged_t> storaged_sp;
45
46 // Function of storaged's main thread
storaged_main(void *)47 void* storaged_main(void* /* unused */) {
48 LOG(INFO) << "storaged: Start";
49
50 for (;;) {
51 storaged_sp->event_checked();
52 storaged_sp->pause();
53 }
54 return NULL;
55 }
56
help_message(void)57 void help_message(void) {
58 printf("usage: storaged [OPTION]\n");
59 printf(" -u --uid Dump uid I/O usage to stdout\n");
60 printf(" -t --task Dump task I/O usage to stdout\n");
61 printf(" -p --perf Dump I/O perf history to stdout\n");
62 printf(" -s --start Start storaged (default)\n");
63 fflush(stdout);
64 }
65
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67 bool flag_main_service = false;
68 bool flag_dump_uid = false;
69 bool flag_dump_task = false;
70 bool flag_dump_perf = false;
71 int opt;
72
73 signal(SIGPIPE, SIG_IGN);
74 android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
75
76 for (;;) {
77 int opt_idx = 0;
78 static struct option long_options[] = {
79 {"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}
84 };
85 opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
86 if (opt == -1) {
87 break;
88 }
89
90 switch (opt) {
91 case 'p':
92 flag_dump_perf = true;
93 break;
94 case 's':
95 flag_main_service = true;
96 break;
97 case 't':
98 flag_dump_task = true;
99 break;
100 case 'u':
101 flag_dump_uid = true;
102 break;
103 default:
104 help_message();
105 return 0;
106 }
107 }
108
109 if (argc == 1) {
110 flag_main_service = true;
111 }
112
113 if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
114 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
115 help_message();
116 return -1;
117 }
118
119 if (flag_main_service) { // start main thread
120 // Start the main thread of storaged
121 storaged_sp = new storaged_t();
122 storaged_sp->init();
123 storaged_sp->report_storage_info();
124 pthread_t storaged_main_thread;
125 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
126 if (errno != 0) {
127 PLOG(ERROR) << "Failed to create main thread";
128 return -1;
129 }
130
131 if (StoragedService::start() != android::OK ||
132 StoragedPrivateService::start() != android::OK) {
133 PLOG(ERROR) << "Failed to start storaged service";
134 return -1;
135 }
136
137 android::ProcessState::self()->startThreadPool();
138 IPCThreadState::self()->joinThreadPool();
139 pthread_join(storaged_main_thread, NULL);
140
141 return 0;
142 }
143
144 sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
145 if (storaged_service == NULL) {
146 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
147 return -1;
148 }
149
150 if (flag_dump_uid || flag_dump_task) {
151 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");
155 return 0;
156 }
157
158 sort_running_uids_info(uid_io);
159 log_console_running_uids_info(uid_io, flag_dump_task);
160 }
161
162 if (flag_dump_perf) {
163 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");
167 return 0;
168 }
169
170 log_console_perf_history(perf_history);
171 }
172
173 return 0;
174 }
175