blob: 530eba806cb103e6d94ea559b614aa1ab0781801 [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 "keywords.h"
19#include "log.h"
Elliott Hughes841b2632015-02-12 14:28:54 -080020#include "property_service.h"
Elliott Hughes24627902015-02-04 10:25:09 -080021
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <dirent.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <errno.h>
Elliott Hughes24627902015-02-04 10:25:09 -080024#include <fcntl.h>
25#include <stdio.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <stdlib.h>
Elliott Hughesf3cf4382015-02-03 17:12:07 -080027#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028#include <sys/stat.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080029#include <sys/utsname.h>
Elliott Hughes24627902015-02-04 10:25:09 -080030#include <time.h>
31#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Elliott Hughes841b2632015-02-12 14:28:54 -080033#include <string>
Yongqin Liua197ff12014-12-05 13:45:02 +080034
Dan Albert98ff7722015-03-13 22:39:54 -070035#include <base/file.h>
Elliott Hughes841b2632015-02-12 14:28:54 -080036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#define LOG_ROOT "/data/bootchart"
38#define LOG_STAT LOG_ROOT"/proc_stat.log"
39#define LOG_PROCS LOG_ROOT"/proc_ps.log"
40#define LOG_DISK LOG_ROOT"/proc_diskstats.log"
41#define LOG_HEADER LOG_ROOT"/header"
42#define LOG_ACCT LOG_ROOT"/kernel_pacct"
43
Yongqin Liua197ff12014-12-05 13:45:02 +080044#define LOG_STARTFILE LOG_ROOT"/start"
45#define LOG_STOPFILE LOG_ROOT"/stop"
46
Elliott Hughes841b2632015-02-12 14:28:54 -080047// Polling period in ms.
48static const int BOOTCHART_POLLING_MS = 200;
Yongqin Liua197ff12014-12-05 13:45:02 +080049
Elliott Hughes841b2632015-02-12 14:28:54 -080050// Max polling time in seconds.
51static const int BOOTCHART_MAX_TIME_SEC = 10*60;
52
53static long long g_last_bootchart_time;
Yongqin Liua197ff12014-12-05 13:45:02 +080054static int g_remaining_samples;
55
Elliott Hughes841b2632015-02-12 14:28:54 -080056static FILE* log_stat;
57static FILE* log_procs;
58static FILE* log_disks;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Elliott Hughes841b2632015-02-12 14:28:54 -080060static long long get_uptime_jiffies() {
61 std::string uptime;
Dan Albert98ff7722015-03-13 22:39:54 -070062 if (!android::base::ReadFileToString("/proc/uptime", &uptime)) {
Elliott Hughes841b2632015-02-12 14:28:54 -080063 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064 }
Elliott Hughes841b2632015-02-12 14:28:54 -080065 return 100LL * strtod(uptime.c_str(), NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066}
67
Elliott Hughes841b2632015-02-12 14:28:54 -080068static void log_header() {
69 char date[32];
70 time_t now_t = time(NULL);
71 struct tm now = *localtime(&now_t);
72 strftime(date, sizeof(date), "%F %T", &now);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Elliott Hughes841b2632015-02-12 14:28:54 -080074 utsname uts;
75 if (uname(&uts) == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 }
78
Elliott Hughes841b2632015-02-12 14:28:54 -080079 char fingerprint[PROP_VALUE_MAX];
80 if (property_get("ro.build.fingerprint", fingerprint) == -1) {
81 return;
82 }
83
84 std::string kernel_cmdline;
Dan Albert98ff7722015-03-13 22:39:54 -070085 android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -080086
87 FILE* out = fopen(LOG_HEADER, "we");
88 if (out == NULL) {
89 return;
90 }
91 fprintf(out, "version = Android init 0.8 " __TIME__ "\n");
92 fprintf(out, "title = Boot chart for Android (%s)\n", date);
93 fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine);
94 fprintf(out, "system.release = %s\n", fingerprint);
95 // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm.
96 fprintf(out, "system.cpu = %s\n", uts.machine);
97 fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 fclose(out);
99}
100
Elliott Hughes841b2632015-02-12 14:28:54 -0800101static void do_log_uptime(FILE* log) {
102 fprintf(log, "%lld\n", get_uptime_jiffies());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103}
104
Elliott Hughes841b2632015-02-12 14:28:54 -0800105static void do_log_file(FILE* log, const char* procfile) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 do_log_uptime(log);
107
Elliott Hughes841b2632015-02-12 14:28:54 -0800108 std::string content;
Dan Albert98ff7722015-03-13 22:39:54 -0700109 if (android::base::ReadFileToString(procfile, &content)) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800110 fprintf(log, "%s\n", content.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112}
113
Elliott Hughes841b2632015-02-12 14:28:54 -0800114static void do_log_procs(FILE* log) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 do_log_uptime(log);
116
Elliott Hughes841b2632015-02-12 14:28:54 -0800117 DIR* dir = opendir("/proc");
118 struct dirent* entry;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 while ((entry = readdir(dir)) != NULL) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800120 // Only match numeric values.
121 char* end;
122 int pid = strtol(entry->d_name, &end, 10);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 if (end != NULL && end > entry->d_name && *end == 0) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800124 char filename[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125
Elliott Hughes841b2632015-02-12 14:28:54 -0800126 // /proc/<pid>/stat only has truncated task names, so get the full
127 // name from /proc/<pid>/cmdline.
128 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
129 std::string cmdline;
Dan Albert98ff7722015-03-13 22:39:54 -0700130 android::base::ReadFileToString(filename, &cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -0800131 const char* full_name = cmdline.c_str(); // So we stop at the first NUL.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Elliott Hughes841b2632015-02-12 14:28:54 -0800133 // Read process stat line.
134 snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
135 std::string stat;
Dan Albert98ff7722015-03-13 22:39:54 -0700136 if (android::base::ReadFileToString(filename, &stat)) {
Elliott Hughes841b2632015-02-12 14:28:54 -0800137 if (!cmdline.empty()) {
138 // Substitute the process name with its real name.
139 size_t open = stat.find('(');
140 size_t close = stat.find_last_of(')');
141 if (open != std::string::npos && close != std::string::npos) {
142 stat.replace(open + 1, close - open - 1, full_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
Elliott Hughes841b2632015-02-12 14:28:54 -0800144 }
145 fputs(stat.c_str(), log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 }
147 }
148 }
149 closedir(dir);
Elliott Hughes841b2632015-02-12 14:28:54 -0800150
151 fputc('\n', log);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152}
153
Elliott Hughes841b2632015-02-12 14:28:54 -0800154static int bootchart_init() {
155 int timeout = 0;
156
157 std::string start;
Dan Albert98ff7722015-03-13 22:39:54 -0700158 android::base::ReadFileToString(LOG_STARTFILE, &start);
Elliott Hughes841b2632015-02-12 14:28:54 -0800159 if (!start.empty()) {
160 timeout = atoi(start.c_str());
Yongqin Liua197ff12014-12-05 13:45:02 +0800161 } else {
Elliott Hughes841b2632015-02-12 14:28:54 -0800162 // When running with emulator, androidboot.bootchart=<timeout>
163 // might be passed by as kernel parameters to specify the bootchart
164 // timeout. this is useful when using -wipe-data since the /data
165 // partition is fresh.
166 std::string cmdline;
Dan Albert98ff7722015-03-13 22:39:54 -0700167 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Elliott Hughes841b2632015-02-12 14:28:54 -0800168#define KERNEL_OPTION "androidboot.bootchart="
169 if (strstr(cmdline.c_str(), KERNEL_OPTION) != NULL) {
170 timeout = atoi(cmdline.c_str() + sizeof(KERNEL_OPTION) - 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 }
172 }
173 if (timeout == 0)
174 return 0;
175
176 if (timeout > BOOTCHART_MAX_TIME_SEC)
177 timeout = BOOTCHART_MAX_TIME_SEC;
178
Elliott Hughes841b2632015-02-12 14:28:54 -0800179 int count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughes841b2632015-02-12 14:28:54 -0800181 log_stat = fopen(LOG_STAT, "we");
182 if (log_stat == NULL) {
183 return -1;
184 }
185 log_procs = fopen(LOG_PROCS, "we");
186 if (log_procs == NULL) {
187 fclose(log_stat);
188 return -1;
189 }
190 log_disks = fopen(LOG_DISK, "we");
191 if (log_disks == NULL) {
192 fclose(log_stat);
193 fclose(log_procs);
194 return -1;
195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Elliott Hughes841b2632015-02-12 14:28:54 -0800197 // Create kernel process accounting file.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 {
Nick Kralevich45a884f2015-02-02 14:37:22 -0800199 int fd = open( LOG_ACCT, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC,0644);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 if (fd >= 0) {
201 close(fd);
202 acct( LOG_ACCT );
203 }
204 }
205
206 log_header();
207 return count;
208}
209
Elliott Hughes841b2632015-02-12 14:28:54 -0800210int do_bootchart_init(int nargs, char** args) {
211 g_remaining_samples = bootchart_init();
212 if (g_remaining_samples < 0) {
213 ERROR("bootcharting init failure: %s\n", strerror(errno));
214 } else if (g_remaining_samples > 0) {
215 NOTICE("bootcharting started (will run for %d ms)\n", g_remaining_samples*BOOTCHART_POLLING_MS);
216 } else {
217 NOTICE("bootcharting ignored\n");
218 }
219 return 0;
220}
221
222static int bootchart_step() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 do_log_file(log_stat, "/proc/stat");
224 do_log_file(log_disks, "/proc/diskstats");
225 do_log_procs(log_procs);
226
Elliott Hughes841b2632015-02-12 14:28:54 -0800227 // Stop if /data/bootchart/stop contains 1.
228 std::string stop;
Dan Albert98ff7722015-03-13 22:39:54 -0700229 if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") {
Elliott Hughes841b2632015-02-12 14:28:54 -0800230 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
233 return 0;
234}
235
Yongqin Liua197ff12014-12-05 13:45:02 +0800236/* called to get time (in ms) used by bootchart */
237static long long bootchart_gettime() {
238 return 10LL*get_uptime_jiffies();
239}
240
Elliott Hughes841b2632015-02-12 14:28:54 -0800241static void bootchart_finish() {
242 unlink(LOG_STOPFILE);
243 fclose(log_stat);
244 fclose(log_disks);
245 fclose(log_procs);
246 acct(NULL);
Yongqin Liua197ff12014-12-05 13:45:02 +0800247}
248
Elliott Hughes841b2632015-02-12 14:28:54 -0800249void bootchart_sample(int* timeout) {
250 // Do we have any more bootcharting to do?
251 if (g_remaining_samples <= 0) {
252 return;
253 }
254
255 long long current_time = bootchart_gettime();
256 int elapsed_time = current_time - g_last_bootchart_time;
257
258 if (elapsed_time >= BOOTCHART_POLLING_MS) {
259 /* count missed samples */
260 while (elapsed_time >= BOOTCHART_POLLING_MS) {
261 elapsed_time -= BOOTCHART_POLLING_MS;
262 g_remaining_samples--;
263 }
264 /* count may be negative, take a sample anyway */
265 g_last_bootchart_time = current_time;
266 if (bootchart_step() < 0 || g_remaining_samples <= 0) {
267 bootchart_finish();
268 g_remaining_samples = 0;
269 }
270 }
271 if (g_remaining_samples > 0) {
272 int remaining_time = BOOTCHART_POLLING_MS - elapsed_time;
273 if (*timeout < 0 || *timeout > remaining_time) {
274 *timeout = remaining_time;
275 }
276 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277}