blob: 825603a9a2c6358b59aaf665c35f49786166374d [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 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
Elliott Hughes24627902015-02-04 10:25:09 -080017#include "bootchart.h"
Elliott Hughesc2497942016-12-16 14:45:17 -080018
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <dirent.h>
Elliott Hughes24627902015-02-04 10:25:09 -080020#include <fcntl.h>
21#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdlib.h>
Elliott Hughesf3cf4382015-02-03 17:12:07 -080023#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <sys/stat.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080025#include <sys/utsname.h>
Elliott Hughes24627902015-02-04 10:25:09 -080026#include <time.h>
27#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
Elliott Hughesc2497942016-12-16 14:45:17 -080029#include <chrono>
30#include <condition_variable>
Elliott Hughes81399e12015-03-10 08:39:45 -070031#include <memory>
Elliott Hughesc2497942016-12-16 14:45:17 -080032#include <mutex>
Elliott Hughesc2497942016-12-16 14:45:17 -080033#include <thread>
Yongqin Liua197ff12014-12-05 13:45:02 +080034
Elliott Hughes4f713192015-12-04 22:00:26 -080035#include <android-base/file.h>
Elliott Hughesc2497942016-12-16 14:45:17 -080036#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070037#include <android-base/properties.h>
Elliott Hughesa3641af2016-11-10 17:43:47 -080038#include <android-base/stringprintf.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080039
Elliott Hughesa3641af2016-11-10 17:43:47 -080040using android::base::StringPrintf;
Elliott Hughesc2497942016-12-16 14:45:17 -080041using namespace std::chrono_literals;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Elliott Hughesc2497942016-12-16 14:45:17 -080043static std::thread* g_bootcharting_thread;
Yongqin Liua197ff12014-12-05 13:45:02 +080044
Elliott Hughesc2497942016-12-16 14:45:17 -080045static std::mutex g_bootcharting_finished_mutex;
46static std::condition_variable g_bootcharting_finished_cv;
47static bool g_bootcharting_finished;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes841b2632015-02-12 14:28:54 -080049static long long get_uptime_jiffies() {
Elliott Hughesc2497942016-12-16 14:45:17 -080050 std::string uptime;
51 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) return 0;
52 return 100LL * strtod(uptime.c_str(), NULL);
53}
54
55static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
56 const char* mode) {
57 std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose);
58 if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename;
59 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060}
61
Elliott Hughes841b2632015-02-12 14:28:54 -080062static void log_header() {
Elliott Hughesc2497942016-12-16 14:45:17 -080063 char date[32];
64 time_t now_t = time(NULL);
65 struct tm now = *localtime(&now_t);
66 strftime(date, sizeof(date), "%F %T", &now);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Elliott Hughesc2497942016-12-16 14:45:17 -080068 utsname uts;
69 if (uname(&uts) == -1) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Tom Cherryccf23532017-03-28 16:40:41 -070071 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "");
Elliott Hughesc2497942016-12-16 14:45:17 -080072 if (fingerprint.empty()) return;
Elliott Hughes841b2632015-02-12 14:28:54 -080073
Elliott Hughesc2497942016-12-16 14:45:17 -080074 std::string kernel_cmdline;
75 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080076
Elliott Hughesc2497942016-12-16 14:45:17 -080077 auto fp = fopen_unique("/data/bootchart/header", "we");
78 if (!fp) return;
79 fprintf(&*fp, "version = Android init 0.8\n");
80 fprintf(&*fp, "title = Boot chart for Android (%s)\n", date);
81 fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
82 fprintf(&*fp, "system.release = %s\n", fingerprint.c_str());
83 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
84 fprintf(&*fp, "system.cpu = %s\n", uts.machine);
85 fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086}
87
Elliott Hughesa3641af2016-11-10 17:43:47 -080088static void log_uptime(FILE* log) {
Elliott Hughesc2497942016-12-16 14:45:17 -080089 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
Elliott Hughesa3641af2016-11-10 17:43:47 -080092static void log_file(FILE* log, const char* procfile) {
Elliott Hughesc2497942016-12-16 14:45:17 -080093 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
Elliott Hughesc2497942016-12-16 14:45:17 -080095 std::string content;
96 if (android::base::ReadFileToString(procfile, &content)) {
97 fprintf(log, "%s\n", content.c_str());
98 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099}
100
Elliott Hughesc2497942016-12-16 14:45:17 -0800101static void log_processes(FILE* log) {
102 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103
Elliott Hughesc2497942016-12-16 14:45:17 -0800104 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
105 struct dirent* entry;
106 while ((entry = readdir(dir.get())) != NULL) {
107 // Only match numeric values.
108 int pid = atoi(entry->d_name);
109 if (pid == 0) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110
Elliott Hughesc2497942016-12-16 14:45:17 -0800111 // /proc/<pid>/stat only has truncated task names, so get the full
112 // name from /proc/<pid>/cmdline.
113 std::string cmdline;
114 android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
115 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
Elliott Hughesa3641af2016-11-10 17:43:47 -0800116
Elliott Hughesc2497942016-12-16 14:45:17 -0800117 // Read process stat line.
118 std::string stat;
119 if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
120 if (!cmdline.empty()) {
121 // Substitute the process name with its real name.
122 size_t open = stat.find('(');
123 size_t close = stat.find_last_of(')');
124 if (open != std::string::npos && close != std::string::npos) {
125 stat.replace(open + 1, close - open - 1, full_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800127 }
128 fputs(stat.c_str(), log);
129 }
130 }
131
132 fputc('\n', log);
133}
134
135static void bootchart_thread_main() {
136 LOG(INFO) << "Bootcharting started";
137
138 // Open log files.
139 auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
140 if (!stat_log) return;
141 auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
142 if (!proc_log) return;
143 auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
144 if (!disk_log) return;
145
146 log_header();
147
148 while (true) {
149 {
150 std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex);
151 g_bootcharting_finished_cv.wait_for(lock, 200ms);
152 if (g_bootcharting_finished) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800154
Elliott Hughesc2497942016-12-16 14:45:17 -0800155 log_file(&*stat_log, "/proc/stat");
156 log_file(&*disk_log, "/proc/diskstats");
157 log_processes(&*proc_log);
158 }
159
160 LOG(INFO) << "Bootcharting finished";
Elliott Hughesa3641af2016-11-10 17:43:47 -0800161}
162
163static int do_bootchart_start() {
Elliott Hughesc2497942016-12-16 14:45:17 -0800164 // We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
165 std::string start;
166 if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
167 LOG(VERBOSE) << "Not bootcharting";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return 0;
Elliott Hughesc2497942016-12-16 14:45:17 -0800169 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesc2497942016-12-16 14:45:17 -0800171 g_bootcharting_thread = new std::thread(bootchart_thread_main);
172 return 0;
Yongqin Liua197ff12014-12-05 13:45:02 +0800173}
174
Elliott Hughesa3641af2016-11-10 17:43:47 -0800175static int do_bootchart_stop() {
Elliott Hughesc2497942016-12-16 14:45:17 -0800176 if (!g_bootcharting_thread) return 0;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800177
Elliott Hughesc2497942016-12-16 14:45:17 -0800178 // Tell the worker thread it's time to quit.
179 {
180 std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex);
181 g_bootcharting_finished = true;
182 g_bootcharting_finished_cv.notify_one();
183 }
184
185 g_bootcharting_thread->join();
186 delete g_bootcharting_thread;
187 g_bootcharting_thread = nullptr;
188 return 0;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800189}
190
191int do_bootchart(const std::vector<std::string>& args) {
Elliott Hughesc2497942016-12-16 14:45:17 -0800192 if (args[1] == "start") return do_bootchart_start();
193 return do_bootchart_stop();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194}