2016-06-17 23:30:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "storaged"
|
|
|
|
#define KLOG_LEVEL 6
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/macros.h>
|
2017-01-12 02:19:21 +01:00
|
|
|
#include <android-base/logging.h>
|
2016-06-17 23:30:48 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <binder/ProcessState.h>
|
|
|
|
#include <binder/IServiceManager.h>
|
|
|
|
#include <binder/IPCThreadState.h>
|
|
|
|
#include <cutils/android_get_control_file.h>
|
|
|
|
#include <cutils/sched_policy.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
|
|
|
#include <storaged.h>
|
|
|
|
#include <storaged_service.h>
|
|
|
|
#include <storaged_utils.h>
|
|
|
|
|
2017-09-29 01:02:22 +02:00
|
|
|
using namespace std;
|
2017-10-13 02:02:17 +02:00
|
|
|
using namespace android;
|
2017-09-29 01:02:22 +02:00
|
|
|
|
2017-10-13 02:02:17 +02:00
|
|
|
sp<storaged_t> storaged_sp;
|
2016-06-17 23:30:48 +02:00
|
|
|
|
|
|
|
// Function of storaged's main thread
|
2017-04-04 03:05:01 +02:00
|
|
|
void* storaged_main(void* /* unused */) {
|
2017-10-13 02:02:17 +02:00
|
|
|
storaged_sp = new storaged_t();
|
2016-06-17 23:30:48 +02:00
|
|
|
|
2018-01-17 02:20:32 +01:00
|
|
|
storaged_sp->init();
|
2017-10-13 02:02:17 +02:00
|
|
|
storaged_sp->report_storage_info();
|
2017-01-30 23:48:38 +01:00
|
|
|
|
2020-01-11 02:08:15 +01:00
|
|
|
LOG(INFO) << "storaged: Start";
|
2016-06-17 23:30:48 +02:00
|
|
|
|
|
|
|
for (;;) {
|
2017-10-13 02:02:17 +02:00
|
|
|
storaged_sp->event_checked();
|
|
|
|
storaged_sp->pause();
|
2016-06-17 23:30:48 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-30 01:48:20 +02:00
|
|
|
void help_message(void) {
|
2016-06-17 23:30:48 +02:00
|
|
|
printf("usage: storaged [OPTION]\n");
|
2016-12-29 00:43:51 +01:00
|
|
|
printf(" -u --uid Dump uid I/O usage to stdout\n");
|
2017-06-23 00:18:21 +02:00
|
|
|
printf(" -t --task Dump task I/O usage to stdout\n");
|
2017-09-29 01:02:22 +02:00
|
|
|
printf(" -p --perf Dump I/O perf history to stdout\n");
|
2016-06-17 23:30:48 +02:00
|
|
|
printf(" -s --start Start storaged (default)\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2017-06-23 00:18:21 +02:00
|
|
|
bool flag_main_service = false;
|
|
|
|
bool flag_dump_uid = false;
|
|
|
|
bool flag_dump_task = false;
|
2017-09-29 01:02:22 +02:00
|
|
|
bool flag_dump_perf = false;
|
2016-06-17 23:30:48 +02:00
|
|
|
int opt;
|
|
|
|
|
2020-01-11 02:08:15 +01:00
|
|
|
android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
|
|
|
|
|
2016-06-17 23:30:48 +02:00
|
|
|
for (;;) {
|
|
|
|
int opt_idx = 0;
|
|
|
|
static struct option long_options[] = {
|
2017-10-13 02:02:17 +02:00
|
|
|
{"perf", no_argument, nullptr, 'p'},
|
|
|
|
{"start", no_argument, nullptr, 's'},
|
|
|
|
{"task", no_argument, nullptr, 't'},
|
|
|
|
{"uid", no_argument, nullptr, 'u'},
|
|
|
|
{nullptr, 0, nullptr, 0}
|
2016-06-17 23:30:48 +02:00
|
|
|
};
|
2017-10-13 02:02:17 +02:00
|
|
|
opt = getopt_long(argc, argv, ":pstu", long_options, &opt_idx);
|
2016-06-17 23:30:48 +02:00
|
|
|
if (opt == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (opt) {
|
2017-10-13 02:02:17 +02:00
|
|
|
case 'p':
|
|
|
|
flag_dump_perf = true;
|
|
|
|
break;
|
2016-06-17 23:30:48 +02:00
|
|
|
case 's':
|
2017-06-23 00:18:21 +02:00
|
|
|
flag_main_service = true;
|
2016-06-17 23:30:48 +02:00
|
|
|
break;
|
2017-06-23 00:18:21 +02:00
|
|
|
case 't':
|
|
|
|
flag_dump_task = true;
|
2016-12-29 00:43:51 +01:00
|
|
|
break;
|
2017-10-13 02:02:17 +02:00
|
|
|
case 'u':
|
|
|
|
flag_dump_uid = true;
|
2017-09-29 01:02:22 +02:00
|
|
|
break;
|
2016-06-17 23:30:48 +02:00
|
|
|
default:
|
|
|
|
help_message();
|
2017-10-13 02:02:17 +02:00
|
|
|
return 0;
|
2016-06-17 23:30:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2017-06-23 00:18:21 +02:00
|
|
|
flag_main_service = true;
|
2016-06-17 23:30:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 00:18:21 +02:00
|
|
|
if (flag_main_service && (flag_dump_uid || flag_dump_task)) {
|
2016-06-17 23:30:48 +02:00
|
|
|
fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
|
|
|
|
help_message();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_main_service) { // start main thread
|
|
|
|
// Start the main thread of storaged
|
|
|
|
pthread_t storaged_main_thread;
|
2017-04-04 03:05:01 +02:00
|
|
|
errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
|
2017-01-12 02:19:21 +01:00
|
|
|
if (errno != 0) {
|
2020-01-11 02:08:15 +01:00
|
|
|
PLOG(ERROR) << "Failed to create main thread";
|
2016-06-17 23:30:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:17 +02:00
|
|
|
if (StoragedService::start() != android::OK ||
|
|
|
|
StoragedPrivateService::start() != android::OK) {
|
2020-01-11 02:08:15 +01:00
|
|
|
PLOG(ERROR) << "Failed to start storaged service";
|
2017-10-13 02:02:17 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-06-17 23:30:48 +02:00
|
|
|
android::ProcessState::self()->startThreadPool();
|
|
|
|
IPCThreadState::self()->joinThreadPool();
|
|
|
|
pthread_join(storaged_main_thread, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:17 +02:00
|
|
|
sp<IStoragedPrivate> storaged_service = get_storaged_pri_service();
|
2017-09-29 01:02:22 +02:00
|
|
|
if (storaged_service == NULL) {
|
|
|
|
fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-29 00:43:51 +01:00
|
|
|
|
2017-09-29 01:02:22 +02:00
|
|
|
if (flag_dump_uid || flag_dump_task) {
|
2017-10-13 02:02:17 +02:00
|
|
|
vector<UidInfo> uid_io;
|
|
|
|
binder::Status status = storaged_service->dumpUids(&uid_io);
|
|
|
|
if (!status.isOk() || uid_io.size() == 0) {
|
|
|
|
fprintf(stderr, "UID I/O info is not available.\n");
|
2016-12-29 00:43:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:17 +02:00
|
|
|
sort_running_uids_info(uid_io);
|
|
|
|
log_console_running_uids_info(uid_io, flag_dump_task);
|
2017-09-29 01:02:22 +02:00
|
|
|
}
|
2016-12-29 00:43:51 +01:00
|
|
|
|
2017-09-29 01:02:22 +02:00
|
|
|
if (flag_dump_perf) {
|
2017-10-13 02:02:17 +02:00
|
|
|
vector<int> perf_history;
|
|
|
|
binder::Status status = storaged_service->dumpPerfHistory(&perf_history);
|
|
|
|
if (!status.isOk() || perf_history.size() == 0) {
|
|
|
|
fprintf(stderr, "I/O perf history is not available.\n");
|
2017-09-29 01:02:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:02:17 +02:00
|
|
|
log_console_perf_history(perf_history);
|
2016-12-29 00:43:51 +01:00
|
|
|
}
|
|
|
|
|
2016-06-17 23:30:48 +02:00
|
|
|
return 0;
|
2016-06-17 23:30:48 +02:00
|
|
|
}
|