blob: 7a8a3d53f18a5fc9e53d46d9eaa4bbc3641dfb0f [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"
Yongqin Liua197ff12014-12-05 13:45:02 +080018#include "log.h"
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 Hughes81399e12015-03-10 08:39:45 -070032#include <memory>
Elliott Hughes841b2632015-02-12 14:28:54 -080033#include <string>
Tom Cherryb7349902015-08-26 11:43:36 -070034#include <vector>
Yongqin Liua197ff12014-12-05 13:45:02 +080035
Elliott Hughes4f713192015-12-04 22:00:26 -080036#include <android-base/file.h>
Elliott Hughesa3641af2016-11-10 17:43:47 -080037#include <android-base/stringprintf.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080038
Elliott Hughesa3641af2016-11-10 17:43:47 -080039using android::base::StringPrintf;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
Elliott Hughesa3641af2016-11-10 17:43:47 -080041static constexpr const char* LOG_STAT = "/data/bootchart/proc_stat.log";
42static constexpr const char* LOG_PROC = "/data/bootchart/proc_ps.log";
43static constexpr const char* LOG_DISK = "/data/bootchart/proc_diskstats.log";
44static constexpr const char* LOG_HEADER = "/data/bootchart/header";
Yongqin Liua197ff12014-12-05 13:45:02 +080045
Elliott Hughes841b2632015-02-12 14:28:54 -080046// Polling period in ms.
Elliott Hughesa3641af2016-11-10 17:43:47 -080047static constexpr int BOOTCHART_POLLING_MS = 200;
Elliott Hughes841b2632015-02-12 14:28:54 -080048
49static long long g_last_bootchart_time;
Yongqin Liua197ff12014-12-05 13:45:02 +080050
Elliott Hughesa3641af2016-11-10 17:43:47 -080051static bool g_bootcharting = false;
52
53static FILE* g_stat_log;
54static FILE* g_proc_log;
55static FILE* g_disk_log;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Elliott Hughes841b2632015-02-12 14:28:54 -080057static long long get_uptime_jiffies() {
58 std::string uptime;
Dan Albertc007bc32015-03-16 10:08:46 -070059 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
Elliott Hughes841b2632015-02-12 14:28:54 -080060 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 }
Elliott Hughes841b2632015-02-12 14:28:54 -080062 return 100LL * strtod(uptime.c_str(), NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063}
64
Elliott Hughes841b2632015-02-12 14:28:54 -080065static void log_header() {
66 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 Hughes841b2632015-02-12 14:28:54 -080071 utsname uts;
72 if (uname(&uts) == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 }
75
Yabin Cui74edcea2015-07-24 10:11:05 -070076 std::string fingerprint = property_get("ro.build.fingerprint");
77 if (fingerprint.empty()) {
Elliott Hughes841b2632015-02-12 14:28:54 -080078 return;
79 }
80
81 std::string kernel_cmdline;
Dan Albertc007bc32015-03-16 10:08:46 -070082 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080083
84 FILE* out = fopen(LOG_HEADER, "we");
85 if (out == NULL) {
86 return;
87 }
Dan Willemsen30622bb2015-10-22 13:04:22 -070088 fprintf(out, "version = Android init 0.8\n");
Elliott Hughes841b2632015-02-12 14:28:54 -080089 fprintf(out, "title = Boot chart for Android (%s)\n", date);
90 fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
Yabin Cui74edcea2015-07-24 10:11:05 -070091 fprintf(out, "system.release = %s\n", fingerprint.c_str());
Elliott Hughes841b2632015-02-12 14:28:54 -080092 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
93 fprintf(out, "system.cpu = %s\n", uts.machine);
94 fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095 fclose(out);
96}
97
Elliott Hughesa3641af2016-11-10 17:43:47 -080098static void log_uptime(FILE* log) {
Elliott Hughes841b2632015-02-12 14:28:54 -080099 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100}
101
Elliott Hughesa3641af2016-11-10 17:43:47 -0800102static void log_file(FILE* log, const char* procfile) {
103 log_uptime(log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104
Elliott Hughes841b2632015-02-12 14:28:54 -0800105 std::string content;
Dan Albertc007bc32015-03-16 10:08:46 -0700106 if (android::base::ReadFileToString(procfile, &content)) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800107 fprintf(log, "%s\n", content.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109}
110
Elliott Hughesa3641af2016-11-10 17:43:47 -0800111static void log_processes() {
112 log_uptime(g_proc_log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Elliott Hughes81399e12015-03-10 08:39:45 -0700114 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir);
Elliott Hughes841b2632015-02-12 14:28:54 -0800115 struct dirent* entry;
Elliott Hughes81399e12015-03-10 08:39:45 -0700116 while ((entry = readdir(dir.get())) != NULL) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800117 // Only match numeric values.
Elliott Hughesa3641af2016-11-10 17:43:47 -0800118 int pid = atoi(entry->d_name);
119 if (pid == 0) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120
Elliott Hughesa3641af2016-11-10 17:43:47 -0800121 // /proc/<pid>/stat only has truncated task names, so get the full
122 // name from /proc/<pid>/cmdline.
Elliott Hughes841b2632015-02-12 14:28:54 -0800123 std::string cmdline;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800124 android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline);
125 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
126
127 // Read process stat line.
128 std::string stat;
129 if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) {
130 if (!cmdline.empty()) {
131 // Substitute the process name with its real name.
132 size_t open = stat.find('(');
133 size_t close = stat.find_last_of(')');
134 if (open != std::string::npos && close != std::string::npos) {
135 stat.replace(open + 1, close - open - 1, full_name);
136 }
137 }
138 fputs(stat.c_str(), g_proc_log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 }
140 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800141
142 fputc('\n', g_proc_log);
143}
144
145static int do_bootchart_start() {
146 // We don't care about the content, but we do care that /data/bootchart/enabled actually exists.
147 std::string start;
148 if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) {
149 LOG(VERBOSE) << "Not bootcharting";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 return 0;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152
Elliott Hughesa3641af2016-11-10 17:43:47 -0800153 // Open log files.
154 std::unique_ptr<FILE, decltype(&fclose)> stat_log(fopen(LOG_STAT, "we"), fclose);
155 if (!stat_log) {
156 PLOG(ERROR) << "Bootcharting couldn't open " << LOG_STAT;
Elliott Hughes841b2632015-02-12 14:28:54 -0800157 return -1;
158 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800159 std::unique_ptr<FILE, decltype(&fclose)> proc_log(fopen(LOG_PROC, "we"), fclose);
160 if (!proc_log) {
161 PLOG(ERROR) << "Bootcharting couldn't open " << LOG_PROC;
Elliott Hughes841b2632015-02-12 14:28:54 -0800162 return -1;
163 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800164 std::unique_ptr<FILE, decltype(&fclose)> disk_log(fopen(LOG_DISK, "we"), fclose);
165 if (!disk_log) {
166 PLOG(ERROR) << "Bootcharting couldn't open " << LOG_DISK;
Elliott Hughes841b2632015-02-12 14:28:54 -0800167 return -1;
168 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesa3641af2016-11-10 17:43:47 -0800170 LOG(INFO) << "Bootcharting started";
171 g_stat_log = stat_log.release();
172 g_proc_log = proc_log.release();
173 g_disk_log = disk_log.release();
174 g_bootcharting = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 log_header();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
177 return 0;
178}
179
Elliott Hughesa3641af2016-11-10 17:43:47 -0800180static void do_bootchart_step() {
181 log_file(g_stat_log, "/proc/stat");
182 log_file(g_disk_log, "/proc/diskstats");
183 log_processes();
Yongqin Liua197ff12014-12-05 13:45:02 +0800184}
185
Elliott Hughesa3641af2016-11-10 17:43:47 -0800186static int do_bootchart_stop() {
187 if (!g_bootcharting) return 0;
188
Elliott Hughes5ee97e82016-10-28 12:30:32 -0700189 LOG(INFO) << "Bootcharting finished";
Elliott Hughesa3641af2016-11-10 17:43:47 -0800190 g_bootcharting = false;
191 fclose(g_stat_log);
192 fclose(g_disk_log);
193 fclose(g_proc_log);
194 return 0;
195}
196
197int do_bootchart(const std::vector<std::string>& args) {
198 if (args[1] == "start") return do_bootchart_start();
199 return do_bootchart_stop();
Yongqin Liua197ff12014-12-05 13:45:02 +0800200}
201
Elliott Hughes841b2632015-02-12 14:28:54 -0800202void bootchart_sample(int* timeout) {
203 // Do we have any more bootcharting to do?
Elliott Hughesa3641af2016-11-10 17:43:47 -0800204 if (!g_bootcharting) return;
Elliott Hughes841b2632015-02-12 14:28:54 -0800205
Elliott Hughesa3641af2016-11-10 17:43:47 -0800206 long long current_time = 10LL * get_uptime_jiffies();
Elliott Hughes841b2632015-02-12 14:28:54 -0800207 int elapsed_time = current_time - g_last_bootchart_time;
208
209 if (elapsed_time >= BOOTCHART_POLLING_MS) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800210 while (elapsed_time >= BOOTCHART_POLLING_MS) {
211 elapsed_time -= BOOTCHART_POLLING_MS;
Elliott Hughes841b2632015-02-12 14:28:54 -0800212 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800213
Elliott Hughes841b2632015-02-12 14:28:54 -0800214 g_last_bootchart_time = current_time;
Elliott Hughesa3641af2016-11-10 17:43:47 -0800215 do_bootchart_step();
Elliott Hughes841b2632015-02-12 14:28:54 -0800216 }
Elliott Hughesa3641af2016-11-10 17:43:47 -0800217
218 // Schedule another?
219 if (g_bootcharting) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800220 int remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
221 if (*timeout < 0 || *timeout > remaining_time) {
222 *timeout = remaining_time;
223 }
224 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225}