blob: 4a9c32e2cc10c60d3217dc0cfd2fd3bfb224a4f2 [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
Elliott Hughes841b2632015-02-12 14:28:54 -080019#include "property_service.h"
Elliott Hughes24627902015-02-04 10:25:09 -080020
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <dirent.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <errno.h>
Elliott Hughes24627902015-02-04 10:25:09 -080023#include <fcntl.h>
24#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdlib.h>
Elliott Hughesf3cf4382015-02-03 17:12:07 -080026#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <sys/stat.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080028#include <sys/utsname.h>
Elliott Hughes24627902015-02-04 10:25:09 -080029#include <time.h>
30#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031
Elliott Hughesc2497942016-12-16 14:45:17 -080032#include <chrono>
33#include <condition_variable>
Elliott Hughes81399e12015-03-10 08:39:45 -070034#include <memory>
Elliott Hughesc2497942016-12-16 14:45:17 -080035#include <mutex>
Elliott Hughes841b2632015-02-12 14:28:54 -080036#include <string>
Elliott Hughesc2497942016-12-16 14:45:17 -080037#include <thread>
Tom Cherryb7349902015-08-26 11:43:36 -070038#include <vector>
Yongqin Liua197ff12014-12-05 13:45:02 +080039
Elliott Hughes4f713192015-12-04 22:00:26 -080040#include <android-base/file.h>
Elliott Hughesc2497942016-12-16 14:45:17 -080041#include <android-base/logging.h>
Elliott Hughesa3641af2016-11-10 17:43:47 -080042#include <android-base/stringprintf.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080043
Elliott Hughesa3641af2016-11-10 17:43:47 -080044using android::base::StringPrintf;
Elliott Hughesc2497942016-12-16 14:45:17 -080045using namespace std::chrono_literals;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Elliott Hughesc2497942016-12-16 14:45:17 -080047static std::thread* g_bootcharting_thread;
Yongqin Liua197ff12014-12-05 13:45:02 +080048
Elliott Hughesc2497942016-12-16 14:45:17 -080049static std::mutex g_bootcharting_finished_mutex;
50static std::condition_variable g_bootcharting_finished_cv;
51static bool g_bootcharting_finished;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Elliott Hughes841b2632015-02-12 14:28:54 -080053static long long get_uptime_jiffies() {
Elliott Hughesc2497942016-12-16 14:45:17 -080054 std::string uptime;
55 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) return 0;
56 return 100LL * strtod(uptime.c_str(), NULL);
57}
58
59static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename,
60 const char* mode) {
61 std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose);
62 if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename;
63 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064}
65
Elliott Hughes841b2632015-02-12 14:28:54 -080066static void log_header() {
Elliott Hughesc2497942016-12-16 14:45:17 -080067 char date[32];
68 time_t now_t = time(NULL);
69 struct tm now = *localtime(&now_t);
70 strftime(date, sizeof(date), "%F %T", &now);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Elliott Hughesc2497942016-12-16 14:45:17 -080072 utsname uts;
73 if (uname(&uts) == -1) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughesc2497942016-12-16 14:45:17 -080075 std::string fingerprint = property_get("ro.build.fingerprint");
76 if (fingerprint.empty()) return;
Elliott Hughes841b2632015-02-12 14:28:54 -080077
Elliott Hughesc2497942016-12-16 14:45:17 -080078 std::string kernel_cmdline;
79 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080080
Elliott Hughesc2497942016-12-16 14:45:17 -080081 auto fp = fopen_unique("/data/bootchart/header", "we");
82 if (!fp) return;
83 fprintf(&*fp, "version = Android init 0.8\n");
84 fprintf(&*fp, "title = Boot chart for Android (%s)\n", date);
85 fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
86 fprintf(&*fp, "system.release = %s\n", fingerprint.c_str());
87 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
88 fprintf(&*fp, "system.cpu = %s\n", uts.machine);
89 fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
Elliott Hughesa3641af2016-11-10 17:43:47 -080092static void log_uptime(FILE* log) {
Elliott Hughesc2497942016-12-16 14:45:17 -080093 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
Elliott Hughesa3641af2016-11-10 17:43:47 -080096static void log_file(FILE* log, const char* procfile) {
Elliott Hughesc2497942016-12-16 14:45:17 -080097 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098
Elliott Hughesc2497942016-12-16 14:45:17 -080099 std::string content;
100 if (android::base::ReadFileToString(procfile, &content)) {
101 fprintf(log, "%s\n", content.c_str());
102 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103}
104
Elliott Hughesc2497942016-12-16 14:45:17 -0800105static void log_processes(FILE* log) {
106 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107
Elliott Hughesc2497942016-12-16 14:45:17 -0800108 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
109 struct dirent* entry;
110 while ((entry = readdir(dir.get())) != NULL) {
111 // Only match numeric values.
112 int pid = atoi(entry->d_name);
113 if (pid == 0) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114
Elliott Hughesc2497942016-12-16 14:45:17 -0800115 // /proc/<pid>/stat only has truncated task names, so get the full
116 // name from /proc/<pid>/cmdline.
117 std::string cmdline;
118 android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
119 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
Elliott Hughesa3641af2016-11-10 17:43:47 -0800120
Elliott Hughesc2497942016-12-16 14:45:17 -0800121 // Read process stat line.
122 std::string stat;
123 if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
124 if (!cmdline.empty()) {
125 // Substitute the process name with its real name.
126 size_t open = stat.find('(');
127 size_t close = stat.find_last_of(')');
128 if (open != std::string::npos && close != std::string::npos) {
129 stat.replace(open + 1, close - open - 1, full_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
Elliott Hughesc2497942016-12-16 14:45:17 -0800131 }
132 fputs(stat.c_str(), log);
133 }
134 }
135
136 fputc('\n', log);
137}
138
139static void bootchart_thread_main() {
140 LOG(INFO) << "Bootcharting started";
141
142 // Open log files.
143 auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we");
144 if (!stat_log) return;
145 auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we");
146 if (!proc_log) return;
147 auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we");
148 if (!disk_log) return;
149
150 log_header();
151
152 while (true) {
153 {
154 std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex);
155 g_bootcharting_finished_cv.wait_for(lock, 200ms);
156 if (g_bootcharting_finished) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800158
Elliott Hughesc2497942016-12-16 14:45:17 -0800159 log_file(&*stat_log, "/proc/stat");
160 log_file(&*disk_log, "/proc/diskstats");
161 log_processes(&*proc_log);
162 }
163
164 LOG(INFO) << "Bootcharting finished";
Elliott Hughesa3641af2016-11-10 17:43:47 -0800165}
166
167static int do_bootchart_start() {
Elliott Hughesc2497942016-12-16 14:45:17 -0800168 // We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
169 std::string start;
170 if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
171 LOG(VERBOSE) << "Not bootcharting";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 return 0;
Elliott Hughesc2497942016-12-16 14:45:17 -0800173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughesc2497942016-12-16 14:45:17 -0800175 g_bootcharting_thread = new std::thread(bootchart_thread_main);
176 return 0;
Yongqin Liua197ff12014-12-05 13:45:02 +0800177}
178
Elliott Hughesa3641af2016-11-10 17:43:47 -0800179static int do_bootchart_stop() {
Elliott Hughesc2497942016-12-16 14:45:17 -0800180 if (!g_bootcharting_thread) return 0;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800181
Elliott Hughesc2497942016-12-16 14:45:17 -0800182 // Tell the worker thread it's time to quit.
183 {
184 std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex);
185 g_bootcharting_finished = true;
186 g_bootcharting_finished_cv.notify_one();
187 }
188
189 g_bootcharting_thread->join();
190 delete g_bootcharting_thread;
191 g_bootcharting_thread = nullptr;
192 return 0;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800193}
194
195int do_bootchart(const std::vector<std::string>& args) {
Elliott Hughesc2497942016-12-16 14:45:17 -0800196 if (args[1] == "start") return do_bootchart_start();
197 return do_bootchart_stop();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198}