blob: 379b4fa8423d6ba0dc5e8029377c59cbe7c56ac3 [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
Tom Cherry81f5d3e2017-06-22 12:53:17 -070043namespace android {
44namespace init {
45
Elliott Hughesc2497942016-12-16 14:45:17 -080046static std::thread* g_bootcharting_thread;
Yongqin Liua197ff12014-12-05 13:45:02 +080047
Elliott Hughesc2497942016-12-16 14:45:17 -080048static std::mutex g_bootcharting_finished_mutex;
49static std::condition_variable g_bootcharting_finished_cv;
50static bool g_bootcharting_finished;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Elliott Hughes841b2632015-02-12 14:28:54 -080052static long long get_uptime_jiffies() {
Elliott Hughesc2497942016-12-16 14:45:17 -080053 std::string uptime;
54 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) return 0;
55 return 100LL * strtod(uptime.c_str(), NULL);
56}
57
58static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
59 const char* mode) {
60 std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose);
61 if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename;
62 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063}
64
Elliott Hughes841b2632015-02-12 14:28:54 -080065static void log_header() {
Elliott Hughesc2497942016-12-16 14:45:17 -080066 char date[32];
67 time_t now_t = time(NULL);
68 struct tm now = *localtime(&now_t);
69 strftime(date, sizeof(date), "%F %T", &now);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Elliott Hughesc2497942016-12-16 14:45:17 -080071 utsname uts;
72 if (uname(&uts) == -1) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Tom Cherryccf23532017-03-28 16:40:41 -070074 std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", "");
Elliott Hughesc2497942016-12-16 14:45:17 -080075 if (fingerprint.empty()) return;
Elliott Hughes841b2632015-02-12 14:28:54 -080076
Elliott Hughesc2497942016-12-16 14:45:17 -080077 std::string kernel_cmdline;
78 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080079
Elliott Hughesc2497942016-12-16 14:45:17 -080080 auto fp = fopen_unique("/data/bootchart/header", "we");
81 if (!fp) return;
82 fprintf(&*fp, "version = Android init 0.8\n");
83 fprintf(&*fp, "title = Boot chart for Android (%s)\n", date);
84 fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
85 fprintf(&*fp, "system.release = %s\n", fingerprint.c_str());
86 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
87 fprintf(&*fp, "system.cpu = %s\n", uts.machine);
88 fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089}
90
Elliott Hughesa3641af2016-11-10 17:43:47 -080091static void log_uptime(FILE* log) {
Elliott Hughesc2497942016-12-16 14:45:17 -080092 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093}
94
Elliott Hughesa3641af2016-11-10 17:43:47 -080095static void log_file(FILE* log, const char* procfile) {
Elliott Hughesc2497942016-12-16 14:45:17 -080096 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097
Elliott Hughesc2497942016-12-16 14:45:17 -080098 std::string content;
99 if (android::base::ReadFileToString(procfile, &content)) {
100 fprintf(log, "%s\n", content.c_str());
101 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102}
103
Elliott Hughesc2497942016-12-16 14:45:17 -0800104static void log_processes(FILE* log) {
105 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Elliott Hughesc2497942016-12-16 14:45:17 -0800107 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
108 struct dirent* entry;
109 while ((entry = readdir(dir.get())) != NULL) {
110 // Only match numeric values.
111 int pid = atoi(entry->d_name);
112 if (pid == 0) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Elliott Hughesc2497942016-12-16 14:45:17 -0800114 // /proc/<pid>/stat only has truncated task names, so get the full
115 // name from /proc/<pid>/cmdline.
116 std::string cmdline;
117 android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
118 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
Elliott Hughesa3641af2016-11-10 17:43:47 -0800119
Elliott Hughesc2497942016-12-16 14:45:17 -0800120 // Read process stat line.
121 std::string stat;
122 if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
123 if (!cmdline.empty()) {
124 // Substitute the process name with its real name.
125 size_t open = stat.find('(');
126 size_t close = stat.find_last_of(')');
127 if (open != std::string::npos && close != std::string::npos) {
128 stat.replace(open + 1, close - open - 1, full_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800130 }
131 fputs(stat.c_str(), log);
132 }
133 }
134
135 fputc('\n', log);
136}
137
138static void bootchart_thread_main() {
139 LOG(INFO) << "Bootcharting started";
140
141 // Open log files.
142 auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
143 if (!stat_log) return;
144 auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
145 if (!proc_log) return;
146 auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
147 if (!disk_log) return;
148
149 log_header();
150
151 while (true) {
152 {
153 std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex);
154 g_bootcharting_finished_cv.wait_for(lock, 200ms);
155 if (g_bootcharting_finished) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800157
Elliott Hughesc2497942016-12-16 14:45:17 -0800158 log_file(&*stat_log, "/proc/stat");
159 log_file(&*disk_log, "/proc/diskstats");
160 log_processes(&*proc_log);
161 }
162
163 LOG(INFO) << "Bootcharting finished";
Elliott Hughesa3641af2016-11-10 17:43:47 -0800164}
165
Tom Cherry557946e2017-08-01 13:50:23 -0700166static Result<Success> do_bootchart_start() {
167 // We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
168 std::string start;
169 if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
170 LOG(VERBOSE) << "Not bootcharting";
171 return Success();
172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Tom Cherry557946e2017-08-01 13:50:23 -0700174 g_bootcharting_thread = new std::thread(bootchart_thread_main);
175 return Success();
Yongqin Liua197ff12014-12-05 13:45:02 +0800176}
177
Tom Cherry557946e2017-08-01 13:50:23 -0700178static Result<Success> do_bootchart_stop() {
179 if (!g_bootcharting_thread) return Success();
Elliott Hughesa3641af2016-11-10 17:43:47 -0800180
Tom Cherry557946e2017-08-01 13:50:23 -0700181 // Tell the worker thread it's time to quit.
182 {
183 std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex);
184 g_bootcharting_finished = true;
185 g_bootcharting_finished_cv.notify_one();
186 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800187
Tom Cherry557946e2017-08-01 13:50:23 -0700188 g_bootcharting_thread->join();
189 delete g_bootcharting_thread;
190 g_bootcharting_thread = nullptr;
191 return Success();
Elliott Hughesa3641af2016-11-10 17:43:47 -0800192}
193
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700194Result<Success> do_bootchart(const BuiltinArguments& args) {
Tom Cherry557946e2017-08-01 13:50:23 -0700195 if (args[1] == "start") return do_bootchart_start();
196 return do_bootchart_stop();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700198
199} // namespace init
200} // namespace android