blob: b7db9b6d617c6d6755cd6569a7346efde72b44a2 [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
Luis Hector Chavez2980f572018-04-11 15:35:44 -070035#include <android-base/chrono_utils.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/file.h>
Elliott Hughesc2497942016-12-16 14:45:17 -080037#include <android-base/logging.h>
Tom Cherryccf23532017-03-28 16:40:41 -070038#include <android-base/properties.h>
Elliott Hughesa3641af2016-11-10 17:43:47 -080039#include <android-base/stringprintf.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080040
Elliott Hughesa3641af2016-11-10 17:43:47 -080041using android::base::StringPrintf;
Luis Hector Chavez2980f572018-04-11 15:35:44 -070042using android::base::boot_clock;
Elliott Hughesc2497942016-12-16 14:45:17 -080043using namespace std::chrono_literals;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044
Tom Cherry81f5d3e2017-06-22 12:53:17 -070045namespace android {
46namespace init {
47
Elliott Hughesc2497942016-12-16 14:45:17 -080048static std::thread* g_bootcharting_thread;
Yongqin Liua197ff12014-12-05 13:45:02 +080049
Elliott Hughesc2497942016-12-16 14:45:17 -080050static std::mutex g_bootcharting_finished_mutex;
51static std::condition_variable g_bootcharting_finished_cv;
52static bool g_bootcharting_finished;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Elliott Hughes841b2632015-02-12 14:28:54 -080054static long long get_uptime_jiffies() {
Luis Hector Chavez2980f572018-04-11 15:35:44 -070055 constexpr int64_t kNanosecondsPerJiffy = 10000000;
56 boot_clock::time_point uptime = boot_clock::now();
57 return uptime.time_since_epoch().count() / kNanosecondsPerJiffy;
Elliott Hughesc2497942016-12-16 14:45:17 -080058}
59
60static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
61 const char* mode) {
62 std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose);
63 if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename;
64 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065}
66
Elliott Hughes841b2632015-02-12 14:28:54 -080067static void log_header() {
Elliott Hughesc2497942016-12-16 14:45:17 -080068 char date[32];
69 time_t now_t = time(NULL);
70 struct tm now = *localtime(&now_t);
71 strftime(date, sizeof(date), "%F %T", &now);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Elliott Hughesc2497942016-12-16 14:45:17 -080073 utsname uts;
74 if (uname(&uts) == -1) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Tom Cherryccf23532017-03-28 16:40:41 -070076 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "");
Elliott Hughesc2497942016-12-16 14:45:17 -080077 if (fingerprint.empty()) return;
Elliott Hughes841b2632015-02-12 14:28:54 -080078
Elliott Hughesc2497942016-12-16 14:45:17 -080079 std::string kernel_cmdline;
80 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080081
Elliott Hughesc2497942016-12-16 14:45:17 -080082 auto fp = fopen_unique("/data/bootchart/header", "we");
83 if (!fp) return;
84 fprintf(&*fp, "version = Android init 0.8\n");
85 fprintf(&*fp, "title = Boot chart for Android (%s)\n", date);
86 fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
87 fprintf(&*fp, "system.release = %s\n", fingerprint.c_str());
88 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
89 fprintf(&*fp, "system.cpu = %s\n", uts.machine);
90 fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091}
92
Elliott Hughesa3641af2016-11-10 17:43:47 -080093static void log_uptime(FILE* log) {
Elliott Hughesc2497942016-12-16 14:45:17 -080094 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095}
96
Elliott Hughesa3641af2016-11-10 17:43:47 -080097static void log_file(FILE* log, const char* procfile) {
Elliott Hughesc2497942016-12-16 14:45:17 -080098 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Elliott Hughesc2497942016-12-16 14:45:17 -0800100 std::string content;
101 if (android::base::ReadFileToString(procfile, &content)) {
102 fprintf(log, "%s\n", content.c_str());
103 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104}
105
Elliott Hughesc2497942016-12-16 14:45:17 -0800106static void log_processes(FILE* log) {
107 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108
Elliott Hughesc2497942016-12-16 14:45:17 -0800109 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
110 struct dirent* entry;
111 while ((entry = readdir(dir.get())) != NULL) {
112 // Only match numeric values.
113 int pid = atoi(entry->d_name);
114 if (pid == 0) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115
Elliott Hughesc2497942016-12-16 14:45:17 -0800116 // /proc/<pid>/stat only has truncated task names, so get the full
117 // name from /proc/<pid>/cmdline.
118 std::string cmdline;
119 android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
120 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
Elliott Hughesa3641af2016-11-10 17:43:47 -0800121
Elliott Hughesc2497942016-12-16 14:45:17 -0800122 // Read process stat line.
123 std::string stat;
124 if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
125 if (!cmdline.empty()) {
126 // Substitute the process name with its real name.
127 size_t open = stat.find('(');
128 size_t close = stat.find_last_of(')');
129 if (open != std::string::npos && close != std::string::npos) {
130 stat.replace(open + 1, close - open - 1, full_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800132 }
133 fputs(stat.c_str(), log);
134 }
135 }
136
137 fputc('\n', log);
138}
139
140static void bootchart_thread_main() {
141 LOG(INFO) << "Bootcharting started";
142
143 // Open log files.
144 auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
145 if (!stat_log) return;
146 auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
147 if (!proc_log) return;
148 auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
149 if (!disk_log) return;
150
151 log_header();
152
153 while (true) {
154 {
155 std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex);
156 g_bootcharting_finished_cv.wait_for(lock, 200ms);
157 if (g_bootcharting_finished) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800159
Elliott Hughesc2497942016-12-16 14:45:17 -0800160 log_file(&*stat_log, "/proc/stat");
161 log_file(&*disk_log, "/proc/diskstats");
162 log_processes(&*proc_log);
163 }
164
165 LOG(INFO) << "Bootcharting finished";
Elliott Hughesa3641af2016-11-10 17:43:47 -0800166}
167
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700168static Result<void> do_bootchart_start() {
Tom Cherry557946e2017-08-01 13:50:23 -0700169 // We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
170 std::string start;
171 if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
172 LOG(VERBOSE) << "Not bootcharting";
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700173 return {};
Tom Cherry557946e2017-08-01 13:50:23 -0700174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Tom Cherry557946e2017-08-01 13:50:23 -0700176 g_bootcharting_thread = new std::thread(bootchart_thread_main);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700177 return {};
Yongqin Liua197ff12014-12-05 13:45:02 +0800178}
179
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700180static Result<void> do_bootchart_stop() {
181 if (!g_bootcharting_thread) return {};
Elliott Hughesa3641af2016-11-10 17:43:47 -0800182
Tom Cherry557946e2017-08-01 13:50:23 -0700183 // Tell the worker thread it's time to quit.
184 {
185 std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex);
186 g_bootcharting_finished = true;
187 g_bootcharting_finished_cv.notify_one();
188 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800189
Tom Cherry557946e2017-08-01 13:50:23 -0700190 g_bootcharting_thread->join();
191 delete g_bootcharting_thread;
192 g_bootcharting_thread = nullptr;
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700193 return {};
Elliott Hughesa3641af2016-11-10 17:43:47 -0800194}
195
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700196Result<void> do_bootchart(const BuiltinArguments& args) {
Tom Cherry557946e2017-08-01 13:50:23 -0700197 if (args[1] == "start") return do_bootchart_start();
198 return do_bootchart_stop();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700200
201} // namespace init
202} // namespace android