主要针对Linux系统的Bootchart文件进行分析,关于怎么采集Bootchart可以查看官方文档。
文件总览
.
├── enabled
├── header
├── proc_diskstats.log
├── proc_ps.log
└── proc_stat.log
Android手机中生成的Bootchart文件如上。
enabled文件
采集Bootchart的标志文件,不关注内容。参照Android12的源码部分。
static Result<void> do_bootchart_start() {
// 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";
return {};
}
g_bootcharting_thread = new std::thread(bootchart_thread_main);
return {};
}
只要/data/bootchart/enabled文件存在,就开启bootchart_thread_main线程。
head文件
head文件存储了版本、时间、system.uname、system.release、CPU类型、kernel启动参数等。
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());
proc_diskstats.log
该文件是周期性的将/proc/diskstats内容进行存储,并加上启动时间。
disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
log_file(&*disk_log, "/proc/diskstats");
// 写入时间
static void log_uptime(FILE* log) {
fprintf(log, "%lld\n", get_uptime_jiffies());
}
static void log_file(FILE* log, const char* procfile) {
log_uptime(log);
std::string content;
if (android::base::ReadFileToString(procfile, &content)) {
fprintf(log, "%s\n", content.c_str());
}
}
proc_ps.log
该文件是周期性的将/proc目录下进程进行识别存储,并加上启动时间。/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件。详见/proc详解、/proc文件浅析。
- 根据PID获取进程名
# 目前13641对应的企业微信
1|umi:/proc/13641 $ cat cmdline
com.tencent.wework
- 获取stat信息,具体内容含义见Linux中 /proc/<pid>/stat文件解释
127|umi:/proc/13641 $ cat stat
13641 (.tencent.wework) S 864 864 0 0 -1 1077952832 1723239 37306 346570 8 21236 12605 39 63 20 0 189 0 71638050 76137267200 7274 18446744073709551615 1 1 0 0 0 0 4608 4097 1073775868 0 0 0 17 3 0 0 1775 0 0 0 0 0 0 0 0 0 0
proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
log_processes(&*proc_log);
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);
}
}
fputs(stat.c_str(), log);
}
}
fputc('\n', log);
}
proc_stat.log
该文件是周期性的将/proc/stat内容进行存储,并加上启动时间。
stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
log_file(&*stat_log, "/proc/stat");
Bootchart解析
具体解析参考后续系列文章。

786

被折叠的 条评论
为什么被折叠?



