2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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.
|
|
|
|
*/
|
|
|
|
|
2015-02-04 19:25:09 +01:00
|
|
|
#include "bootchart.h"
|
2016-12-16 23:45:17 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <dirent.h>
|
2015-02-04 19:25:09 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdlib.h>
|
2015-02-04 02:12:07 +01:00
|
|
|
#include <string.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <sys/stat.h>
|
2015-02-12 23:28:54 +01:00
|
|
|
#include <sys/utsname.h>
|
2015-02-04 19:25:09 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2015-03-10 16:39:45 +01:00
|
|
|
#include <memory>
|
2016-12-16 23:45:17 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
2015-02-12 23:28:54 +01:00
|
|
|
|
2018-04-12 00:35:44 +02:00
|
|
|
#include <android-base/chrono_utils.h>
|
2015-12-05 07:00:26 +01:00
|
|
|
#include <android-base/file.h>
|
2016-12-16 23:45:17 +01:00
|
|
|
#include <android-base/logging.h>
|
2017-03-29 01:40:41 +02:00
|
|
|
#include <android-base/properties.h>
|
2016-11-11 02:43:47 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2014-12-05 06:45:02 +01:00
|
|
|
|
2016-11-11 02:43:47 +01:00
|
|
|
using android::base::StringPrintf;
|
2018-04-12 00:35:44 +02:00
|
|
|
using android::base::boot_clock;
|
2016-12-16 23:45:17 +01:00
|
|
|
using namespace std::chrono_literals;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static std::thread* g_bootcharting_thread;
|
2014-12-05 06:45:02 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static std::mutex g_bootcharting_finished_mutex;
|
|
|
|
static std::condition_variable g_bootcharting_finished_cv;
|
|
|
|
static bool g_bootcharting_finished;
|
2015-02-12 23:28:54 +01:00
|
|
|
|
|
|
|
static long long get_uptime_jiffies() {
|
2018-04-12 00:35:44 +02:00
|
|
|
constexpr int64_t kNanosecondsPerJiffy = 10000000;
|
|
|
|
boot_clock::time_point uptime = boot_clock::now();
|
|
|
|
return uptime.time_since_epoch().count() / kNanosecondsPerJiffy;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
|
|
|
|
const char* mode) {
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose);
|
|
|
|
if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename;
|
|
|
|
return result;
|
|
|
|
}
|
2014-07-24 19:11:35 +02:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static void log_header() {
|
|
|
|
char date[32];
|
|
|
|
time_t now_t = time(NULL);
|
|
|
|
struct tm now = *localtime(&now_t);
|
|
|
|
strftime(date, sizeof(date), "%F %T", &now);
|
|
|
|
|
|
|
|
utsname uts;
|
|
|
|
if (uname(&uts) == -1) return;
|
|
|
|
|
2017-03-29 01:40:41 +02:00
|
|
|
std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "");
|
2016-12-16 23:45:17 +01:00
|
|
|
if (fingerprint.empty()) return;
|
|
|
|
|
|
|
|
std::string kernel_cmdline;
|
|
|
|
android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
|
|
|
|
|
|
|
auto fp = fopen_unique("/data/bootchart/header", "we");
|
|
|
|
if (!fp) return;
|
|
|
|
fprintf(&*fp, "version = Android init 0.8\n");
|
|
|
|
fprintf(&*fp, "title = Boot chart for Android (%s)\n", date);
|
|
|
|
fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
|
|
|
|
fprintf(&*fp, "system.release = %s\n", fingerprint.c_str());
|
|
|
|
// TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
|
|
|
|
fprintf(&*fp, "system.cpu = %s\n", uts.machine);
|
|
|
|
fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 02:43:47 +01:00
|
|
|
static void log_uptime(FILE* log) {
|
2016-12-16 23:45:17 +01:00
|
|
|
fprintf(log, "%lld\n", get_uptime_jiffies());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 02:43:47 +01:00
|
|
|
static void log_file(FILE* log, const char* procfile) {
|
2016-12-16 23:45:17 +01:00
|
|
|
log_uptime(log);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
std::string content;
|
|
|
|
if (android::base::ReadFileToString(procfile, &content)) {
|
|
|
|
fprintf(log, "%s\n", content.c_str());
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static void log_processes(FILE* log) {
|
|
|
|
log_uptime(log);
|
|
|
|
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
|
|
|
|
struct dirent* entry;
|
|
|
|
while ((entry = readdir(dir.get())) != NULL) {
|
|
|
|
// Only match numeric values.
|
|
|
|
int pid = atoi(entry->d_name);
|
|
|
|
if (pid == 0) continue;
|
|
|
|
|
|
|
|
// /proc/<pid>/stat only has truncated task names, so get the full
|
|
|
|
// name from /proc/<pid>/cmdline.
|
|
|
|
std::string cmdline;
|
|
|
|
android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
|
|
|
|
const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
|
|
|
|
|
|
|
|
// Read process stat line.
|
|
|
|
std::string stat;
|
|
|
|
if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
|
|
|
|
if (!cmdline.empty()) {
|
|
|
|
// Substitute the process name with its real name.
|
|
|
|
size_t open = stat.find('(');
|
|
|
|
size_t close = stat.find_last_of(')');
|
|
|
|
if (open != std::string::npos && close != std::string::npos) {
|
|
|
|
stat.replace(open + 1, close - open - 1, full_name);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-12-16 23:45:17 +01:00
|
|
|
}
|
|
|
|
fputs(stat.c_str(), log);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2016-12-16 23:45:17 +01:00
|
|
|
}
|
2014-12-05 06:45:02 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
fputc('\n', log);
|
2014-12-05 06:45:02 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
static void bootchart_thread_main() {
|
|
|
|
LOG(INFO) << "Bootcharting started";
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
// Open log files.
|
|
|
|
auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
|
|
|
|
if (!stat_log) return;
|
|
|
|
auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
|
|
|
|
if (!proc_log) return;
|
|
|
|
auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
|
|
|
|
if (!disk_log) return;
|
|
|
|
|
|
|
|
log_header();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex);
|
|
|
|
g_bootcharting_finished_cv.wait_for(lock, 200ms);
|
|
|
|
if (g_bootcharting_finished) break;
|
2015-02-12 23:28:54 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
log_file(&*stat_log, "/proc/stat");
|
|
|
|
log_file(&*disk_log, "/proc/diskstats");
|
|
|
|
log_processes(&*proc_log);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-12-16 23:45:17 +01:00
|
|
|
LOG(INFO) << "Bootcharting finished";
|
2015-02-12 23:28:54 +01:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
static Result<void> do_bootchart_start() {
|
2017-08-01 22:50:23 +02:00
|
|
|
// We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
|
|
|
|
std::string start;
|
|
|
|
if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
|
|
|
|
LOG(VERBOSE) << "Not bootcharting";
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2017-08-01 22:50:23 +02:00
|
|
|
}
|
2016-12-16 23:45:17 +01:00
|
|
|
|
2017-08-01 22:50:23 +02:00
|
|
|
g_bootcharting_thread = new std::thread(bootchart_thread_main);
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2016-11-11 02:43:47 +01:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
static Result<void> do_bootchart_stop() {
|
|
|
|
if (!g_bootcharting_thread) return {};
|
2016-12-16 23:45:17 +01:00
|
|
|
|
2017-08-01 22:50:23 +02:00
|
|
|
// Tell the worker thread it's time to quit.
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex);
|
|
|
|
g_bootcharting_finished = true;
|
|
|
|
g_bootcharting_finished_cv.notify_one();
|
|
|
|
}
|
2016-12-16 23:45:17 +01:00
|
|
|
|
2017-08-01 22:50:23 +02:00
|
|
|
g_bootcharting_thread->join();
|
|
|
|
delete g_bootcharting_thread;
|
|
|
|
g_bootcharting_thread = nullptr;
|
2019-06-10 20:08:01 +02:00
|
|
|
return {};
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-06-10 20:08:01 +02:00
|
|
|
Result<void> do_bootchart(const BuiltinArguments& args) {
|
2017-08-01 22:50:23 +02:00
|
|
|
if (args[1] == "start") return do_bootchart_start();
|
|
|
|
return do_bootchart_stop();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|