The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 17 | #include "bootchart.h" |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 18 | #include "log.h" |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 19 | #include "property_service.h" |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
| 24 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 26 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 28 | #include <sys/utsname.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 29 | #include <time.h> |
| 30 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 32 | #include <memory> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 33 | #include <string> |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 34 | #include <vector> |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 35 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 37 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | #define LOG_ROOT "/data/bootchart" |
| 39 | #define LOG_STAT LOG_ROOT"/proc_stat.log" |
| 40 | #define LOG_PROCS LOG_ROOT"/proc_ps.log" |
| 41 | #define LOG_DISK LOG_ROOT"/proc_diskstats.log" |
| 42 | #define LOG_HEADER LOG_ROOT"/header" |
| 43 | #define LOG_ACCT LOG_ROOT"/kernel_pacct" |
| 44 | |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 45 | #define LOG_STARTFILE LOG_ROOT"/start" |
| 46 | #define LOG_STOPFILE LOG_ROOT"/stop" |
| 47 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 48 | // Polling period in ms. |
| 49 | static const int BOOTCHART_POLLING_MS = 200; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 50 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 51 | // Max polling time in seconds. |
| 52 | static const int BOOTCHART_MAX_TIME_SEC = 10*60; |
| 53 | |
| 54 | static long long g_last_bootchart_time; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 55 | static int g_remaining_samples; |
| 56 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 57 | static FILE* log_stat; |
| 58 | static FILE* log_procs; |
| 59 | static FILE* log_disks; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 61 | static long long get_uptime_jiffies() { |
| 62 | std::string uptime; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 63 | if (!android::base::ReadFileToString("/proc/uptime", &uptime)) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 64 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | } |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 66 | return 100LL * strtod(uptime.c_str(), NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 69 | static void log_header() { |
| 70 | char date[32]; |
| 71 | time_t now_t = time(NULL); |
| 72 | struct tm now = *localtime(&now_t); |
| 73 | strftime(date, sizeof(date), "%F %T", &now); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 75 | utsname uts; |
| 76 | if (uname(&uts) == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 80 | std::string fingerprint = property_get("ro.build.fingerprint"); |
| 81 | if (fingerprint.empty()) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | std::string kernel_cmdline; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 86 | android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 87 | |
| 88 | FILE* out = fopen(LOG_HEADER, "we"); |
| 89 | if (out == NULL) { |
| 90 | return; |
| 91 | } |
Dan Willemsen | 30622bb | 2015-10-22 13:04:22 -0700 | [diff] [blame] | 92 | fprintf(out, "version = Android init 0.8\n"); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 93 | fprintf(out, "title = Boot chart for Android (%s)\n", date); |
| 94 | fprintf(out, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine); |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 95 | fprintf(out, "system.release = %s\n", fingerprint.c_str()); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 96 | // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm. |
| 97 | fprintf(out, "system.cpu = %s\n", uts.machine); |
| 98 | fprintf(out, "system.kernel.options = %s\n", kernel_cmdline.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | fclose(out); |
| 100 | } |
| 101 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 102 | static void do_log_uptime(FILE* log) { |
| 103 | fprintf(log, "%lld\n", get_uptime_jiffies()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 106 | static void do_log_file(FILE* log, const char* procfile) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | do_log_uptime(log); |
| 108 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 109 | std::string content; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 110 | if (android::base::ReadFileToString(procfile, &content)) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 111 | fprintf(log, "%s\n", content.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 115 | static void do_log_procs(FILE* log) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | do_log_uptime(log); |
| 117 | |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 118 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 119 | struct dirent* entry; |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 120 | while ((entry = readdir(dir.get())) != NULL) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 121 | // Only match numeric values. |
| 122 | char* end; |
| 123 | int pid = strtol(entry->d_name, &end, 10); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 124 | if (end != NULL && end > entry->d_name && *end == 0) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 125 | char filename[32]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 127 | // /proc/<pid>/stat only has truncated task names, so get the full |
| 128 | // name from /proc/<pid>/cmdline. |
| 129 | snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); |
| 130 | std::string cmdline; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 131 | android::base::ReadFileToString(filename, &cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 132 | const char* full_name = cmdline.c_str(); // So we stop at the first NUL. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 134 | // Read process stat line. |
| 135 | snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); |
| 136 | std::string stat; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 137 | if (android::base::ReadFileToString(filename, &stat)) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 138 | if (!cmdline.empty()) { |
| 139 | // Substitute the process name with its real name. |
| 140 | size_t open = stat.find('('); |
| 141 | size_t close = stat.find_last_of(')'); |
| 142 | if (open != std::string::npos && close != std::string::npos) { |
| 143 | stat.replace(open + 1, close - open - 1, full_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | } |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 145 | } |
| 146 | fputs(stat.c_str(), log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 150 | |
| 151 | fputc('\n', log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 154 | static int bootchart_init() { |
| 155 | int timeout = 0; |
| 156 | |
| 157 | std::string start; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 158 | android::base::ReadFileToString(LOG_STARTFILE, &start); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 159 | if (!start.empty()) { |
| 160 | timeout = atoi(start.c_str()); |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 161 | } else { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 162 | // 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; |
Yu Ning | 9136f38 | 2015-07-15 16:41:51 +0800 | [diff] [blame] | 167 | const char* s; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 168 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 169 | #define KERNEL_OPTION "androidboot.bootchart=" |
Yu Ning | 9136f38 | 2015-07-15 16:41:51 +0800 | [diff] [blame] | 170 | if ((s = strstr(cmdline.c_str(), KERNEL_OPTION)) != NULL) { |
| 171 | timeout = atoi(s + sizeof(KERNEL_OPTION) - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | if (timeout == 0) |
| 175 | return 0; |
| 176 | |
| 177 | if (timeout > BOOTCHART_MAX_TIME_SEC) |
| 178 | timeout = BOOTCHART_MAX_TIME_SEC; |
| 179 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 180 | int count = (timeout*1000 + BOOTCHART_POLLING_MS-1)/BOOTCHART_POLLING_MS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 181 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 182 | log_stat = fopen(LOG_STAT, "we"); |
| 183 | if (log_stat == NULL) { |
| 184 | return -1; |
| 185 | } |
| 186 | log_procs = fopen(LOG_PROCS, "we"); |
| 187 | if (log_procs == NULL) { |
| 188 | fclose(log_stat); |
| 189 | return -1; |
| 190 | } |
| 191 | log_disks = fopen(LOG_DISK, "we"); |
| 192 | if (log_disks == NULL) { |
| 193 | fclose(log_stat); |
| 194 | fclose(log_procs); |
| 195 | return -1; |
| 196 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 198 | // Create kernel process accounting file. |
Elliott Hughes | 59abac2 | 2015-03-28 12:12:51 -0700 | [diff] [blame] | 199 | close(open(LOG_ACCT, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644)); |
| 200 | acct(LOG_ACCT); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | |
| 202 | log_header(); |
| 203 | return count; |
| 204 | } |
| 205 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 206 | int do_bootchart_init(const std::vector<std::string>& args) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 207 | g_remaining_samples = bootchart_init(); |
| 208 | if (g_remaining_samples < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 209 | PLOG(ERROR) << "Bootcharting initialization failed"; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 210 | } else if (g_remaining_samples > 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 211 | LOG(INFO) << "Bootcharting started (will run for " |
| 212 | << ((g_remaining_samples * BOOTCHART_POLLING_MS) / 1000) << " s)."; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 213 | } else { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 214 | LOG(VERBOSE) << "Not bootcharting."; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int bootchart_step() { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | do_log_file(log_stat, "/proc/stat"); |
| 221 | do_log_file(log_disks, "/proc/diskstats"); |
| 222 | do_log_procs(log_procs); |
| 223 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 224 | // Stop if /data/bootchart/stop contains 1. |
| 225 | std::string stop; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 226 | if (android::base::ReadFileToString(LOG_STOPFILE, &stop) && stop == "1") { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 227 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 233 | /* called to get time (in ms) used by bootchart */ |
| 234 | static long long bootchart_gettime() { |
| 235 | return 10LL*get_uptime_jiffies(); |
| 236 | } |
| 237 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 238 | static void bootchart_finish() { |
| 239 | unlink(LOG_STOPFILE); |
| 240 | fclose(log_stat); |
| 241 | fclose(log_disks); |
| 242 | fclose(log_procs); |
| 243 | acct(NULL); |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 244 | } |
| 245 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 246 | void bootchart_sample(int* timeout) { |
| 247 | // Do we have any more bootcharting to do? |
| 248 | if (g_remaining_samples <= 0) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | long long current_time = bootchart_gettime(); |
| 253 | int elapsed_time = current_time - g_last_bootchart_time; |
| 254 | |
| 255 | if (elapsed_time >= BOOTCHART_POLLING_MS) { |
| 256 | /* count missed samples */ |
| 257 | while (elapsed_time >= BOOTCHART_POLLING_MS) { |
| 258 | elapsed_time -= BOOTCHART_POLLING_MS; |
| 259 | g_remaining_samples--; |
| 260 | } |
| 261 | /* count may be negative, take a sample anyway */ |
| 262 | g_last_bootchart_time = current_time; |
| 263 | if (bootchart_step() < 0 || g_remaining_samples <= 0) { |
| 264 | bootchart_finish(); |
| 265 | g_remaining_samples = 0; |
| 266 | } |
| 267 | } |
| 268 | if (g_remaining_samples > 0) { |
| 269 | int remaining_time = BOOTCHART_POLLING_MS - elapsed_time; |
| 270 | if (*timeout < 0 || *timeout > remaining_time) { |
| 271 | *timeout = remaining_time; |
| 272 | } |
| 273 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | } |