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