2009-03-04 11:32:55 +08: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-05 02:25:09 +08:00
|
|
|
#include "bootchart.h"
|
2014-12-05 13:45:02 +08:00
|
|
|
#include "keywords.h"
|
|
|
|
#include "log.h"
|
2015-02-13 06:28:54 +08:00
|
|
|
#include "property_service.h"
|
2015-02-05 02:25:09 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2015-02-05 02:25:09 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <stdlib.h>
|
2015-02-04 09:12:07 +08:00
|
|
|
#include <string.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
#include <sys/stat.h>
|
2015-02-13 06:28:54 +08:00
|
|
|
#include <sys/utsname.h>
|
2015-02-05 02:25:09 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
#include <memory>
|
2015-02-13 06:28:54 +08:00
|
|
|
#include <string>
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
#include <base/file.h>
|
2014-12-05 13:45:02 +08:00
|
|
|
|
2009-03-04 11:32:55 +08:00
|
|
|
#define LOG_ROOT "/data/bootchart"
|
|
|
|
#define LOG_STAT LOG_ROOT"/proc_stat.log"
|
|
|
|
#define LOG_PROCS LOG_ROOT"/proc_ps.log"
|
|
|
|
#define LOG_DISK LOG_ROOT"/proc_diskstats.log"
|
|
|
|
#define LOG_HEADER LOG_ROOT"/header"
|
|
|
|
#define LOG_ACCT LOG_ROOT"/kernel_pacct"
|
|
|
|
|
2014-12-05 13:45:02 +08:00
|
|
|
#define LOG_STARTFILE LOG_ROOT"/start"
|
|
|
|
#define LOG_STOPFILE LOG_ROOT"/stop"
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
// Polling period in ms.
|
|
|
|
static const int BOOTCHART_POLLING_MS = 200;
|
2014-12-05 13:45:02 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
// Max polling time in seconds.
|
|
|
|
static const int BOOTCHART_MAX_TIME_SEC = 10*60;
|
|
|
|
|
|
|
|
static long long g_last_bootchart_time;
|
2014-12-05 13:45:02 +08:00
|
|
|
static int g_remaining_samples;
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static FILE* log_stat;
|
|
|
|
static FILE* log_procs;
|
|
|
|
static FILE* log_disks;
|
|
|
|
|
|
|
|
static long long get_uptime_jiffies() {
|
|
|
|
std::string uptime;
|
2015-03-17 01:08:46 +08:00
|
|
|
if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
|
2015-02-13 06:28:54 +08:00
|
|
|
return 0;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
2015-02-13 06:28:54 +08:00
|
|
|
return 100LL * strtod(uptime.c_str(), NULL);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08: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);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
utsname uts;
|
|
|
|
if (uname(&uts) == -1) {
|
|
|
|
return;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
char fingerprint[PROP_VALUE_MAX];
|
|
|
|
if (property_get("ro.build.fingerprint", fingerprint) == -1) {
|
|
|
|
return;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
std::string kernel_cmdline;
|
2015-03-17 01:08:46 +08:00
|
|
|
android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
|
2014-07-25 01:11:35 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
FILE* out = fopen(LOG_HEADER, "we");
|
|
|
|
if (out == NULL) {
|
2009-03-04 11:32:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-02-13 06:28:54 +08:00
|
|
|
fprintf(out, "version = Android init 0.8 " __TIME__ "\n");
|
|
|
|
fprintf(out, "title = Boot chart for Android (%s)\n", date);
|
|
|
|
fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
|
|
|
|
fprintf(out, "system.release = %s\n", fingerprint);
|
|
|
|
// TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
|
|
|
|
fprintf(out, "system.cpu = %s\n", uts.machine);
|
|
|
|
fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static void do_log_uptime(FILE* log) {
|
|
|
|
fprintf(log, "%lld\n", get_uptime_jiffies());
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static void do_log_file(FILE* log, const char* procfile) {
|
2009-03-04 11:32:55 +08:00
|
|
|
do_log_uptime(log);
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
std::string content;
|
2015-03-17 01:08:46 +08:00
|
|
|
if (android::base::ReadFileToString(procfile, &content)) {
|
2015-02-13 06:28:54 +08:00
|
|
|
fprintf(log, "%s\n", content.c_str());
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static void do_log_procs(FILE* log) {
|
2009-03-04 11:32:55 +08:00
|
|
|
do_log_uptime(log);
|
|
|
|
|
2015-03-10 23:39:45 +08:00
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
|
2015-02-13 06:28:54 +08:00
|
|
|
struct dirent* entry;
|
2015-03-10 23:39:45 +08:00
|
|
|
while ((entry = readdir(dir.get())) != NULL) {
|
2015-02-13 06:28:54 +08:00
|
|
|
// Only match numeric values.
|
|
|
|
char* end;
|
|
|
|
int pid = strtol(entry->d_name, &end, 10);
|
2009-03-04 11:32:55 +08:00
|
|
|
if (end != NULL && end > entry->d_name && *end == 0) {
|
2015-02-13 06:28:54 +08:00
|
|
|
char filename[32];
|
|
|
|
|
|
|
|
// /proc/<pid>/stat only has truncated task names, so get the full
|
|
|
|
// name from /proc/<pid>/cmdline.
|
|
|
|
snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
|
|
|
|
std::string cmdline;
|
2015-03-17 01:08:46 +08:00
|
|
|
android::base::ReadFileToString(filename, &cmdline);
|
2015-02-13 06:28:54 +08:00
|
|
|
const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
|
|
|
|
|
|
|
|
// Read process stat line.
|
|
|
|
snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
|
|
|
|
std::string stat;
|
2015-03-17 01:08:46 +08:00
|
|
|
if (android::base::ReadFileToString(filename, &stat)) {
|
2015-02-13 06:28:54 +08:00
|
|
|
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 11:32:55 +08:00
|
|
|
}
|
2015-02-13 06:28:54 +08:00
|
|
|
}
|
|
|
|
fputs(stat.c_str(), log);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 13:45:02 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
fputc('\n', log);
|
2014-12-05 13:45:02 +08:00
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static int bootchart_init() {
|
|
|
|
int timeout = 0;
|
|
|
|
|
|
|
|
std::string start;
|
2015-03-17 01:08:46 +08:00
|
|
|
android::base::ReadFileToString(LOG_STARTFILE, &start);
|
2015-02-13 06:28:54 +08:00
|
|
|
if (!start.empty()) {
|
|
|
|
timeout = atoi(start.c_str());
|
|
|
|
} else {
|
|
|
|
// When running with emulator, androidboot.bootchart=<timeout>
|
|
|
|
// might be passed by as kernel parameters to specify the bootchart
|
|
|
|
// timeout. this is useful when using -wipe-data since the /data
|
|
|
|
// partition is fresh.
|
|
|
|
std::string cmdline;
|
2015-03-17 01:08:46 +08:00
|
|
|
android::base::ReadFileToString("/proc/cmdline", &cmdline);
|
2015-02-13 06:28:54 +08:00
|
|
|
#define KERNEL_OPTION "androidboot.bootchart="
|
|
|
|
if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
|
|
|
|
timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timeout == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (timeout > BOOTCHART_MAX_TIME_SEC)
|
|
|
|
timeout = BOOTCHART_MAX_TIME_SEC;
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
int count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
log_stat = fopen(LOG_STAT, "we");
|
|
|
|
if (log_stat == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
log_procs = fopen(LOG_PROCS, "we");
|
|
|
|
if (log_procs == NULL) {
|
|
|
|
fclose(log_stat);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
log_disks = fopen(LOG_DISK, "we");
|
|
|
|
if (log_disks == NULL) {
|
|
|
|
fclose(log_stat);
|
|
|
|
fclose(log_procs);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
// Create kernel process accounting file.
|
2015-03-29 03:12:51 +08:00
|
|
|
close(open(LOG_ACCT, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
|
|
|
|
acct(LOG_ACCT);
|
2009-03-04 11:32:55 +08:00
|
|
|
|
|
|
|
log_header();
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
int do_bootchart_init(int nargs, char** args) {
|
|
|
|
g_remaining_samples = bootchart_init();
|
|
|
|
if (g_remaining_samples < 0) {
|
2015-03-29 03:12:51 +08:00
|
|
|
ERROR("Bootcharting init failure: %s\n", strerror(errno));
|
2015-02-13 06:28:54 +08:00
|
|
|
} else if (g_remaining_samples > 0) {
|
2015-03-29 03:12:51 +08:00
|
|
|
NOTICE("Bootcharting started (will run for %d s).\n",
|
|
|
|
(g_remaining_samples * BOOTCHART_POLLING_MS) / 1000);
|
2015-02-13 06:28:54 +08:00
|
|
|
} else {
|
2015-03-29 03:12:51 +08:00
|
|
|
NOTICE("Not bootcharting.\n");
|
2015-02-13 06:28:54 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bootchart_step() {
|
2009-03-04 11:32:55 +08:00
|
|
|
do_log_file(log_stat, "/proc/stat");
|
|
|
|
do_log_file(log_disks, "/proc/diskstats");
|
|
|
|
do_log_procs(log_procs);
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
// Stop if /data/bootchart/stop contains 1.
|
|
|
|
std::string stop;
|
2015-03-17 01:08:46 +08:00
|
|
|
if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
|
2015-02-13 06:28:54 +08:00
|
|
|
return -1;
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:45:02 +08:00
|
|
|
/* called to get time (in ms) used by bootchart */
|
|
|
|
static long long bootchart_gettime() {
|
|
|
|
return 10LL*get_uptime_jiffies();
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
static void bootchart_finish() {
|
|
|
|
unlink(LOG_STOPFILE);
|
|
|
|
fclose(log_stat);
|
|
|
|
fclose(log_disks);
|
|
|
|
fclose(log_procs);
|
|
|
|
acct(NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:45:02 +08:00
|
|
|
void bootchart_sample(int* timeout) {
|
2015-02-13 06:28:54 +08:00
|
|
|
// Do we have any more bootcharting to do?
|
|
|
|
if (g_remaining_samples <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-05 13:45:02 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
long long current_time = bootchart_gettime();
|
|
|
|
int elapsed_time = current_time - g_last_bootchart_time;
|
2014-12-05 13:45:02 +08:00
|
|
|
|
2015-02-13 06:28:54 +08:00
|
|
|
if (elapsed_time >= BOOTCHART_POLLING_MS) {
|
|
|
|
/* count missed samples */
|
|
|
|
while (elapsed_time >= BOOTCHART_POLLING_MS) {
|
|
|
|
elapsed_time -= BOOTCHART_POLLING_MS;
|
|
|
|
g_remaining_samples--;
|
2014-12-05 13:45:02 +08:00
|
|
|
}
|
2015-02-13 06:28:54 +08:00
|
|
|
/* count may be negative, take a sample anyway */
|
|
|
|
g_last_bootchart_time = current_time;
|
|
|
|
if (bootchart_step() < 0 || g_remaining_samples <= 0) {
|
|
|
|
bootchart_finish();
|
|
|
|
g_remaining_samples = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (g_remaining_samples > 0) {
|
|
|
|
int remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
|
|
|
|
if (*timeout < 0 || *timeout > remaining_time) {
|
|
|
|
*timeout = remaining_time;
|
2014-12-05 13:45:02 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 11:32:55 +08:00
|
|
|
}
|