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 | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 37 | #include <android-base/stringprintf.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 38 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 39 | using android::base::StringPrintf; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 41 | static constexpr const char* LOG_STAT = "/data/bootchart/proc_stat.log"; |
| 42 | static constexpr const char* LOG_PROC = "/data/bootchart/proc_ps.log"; |
| 43 | static constexpr const char* LOG_DISK = "/data/bootchart/proc_diskstats.log"; |
| 44 | static constexpr const char* LOG_HEADER = "/data/bootchart/header"; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 45 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 46 | // Polling period in ms. |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 47 | static constexpr int BOOTCHART_POLLING_MS = 200; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 48 | |
| 49 | static long long g_last_bootchart_time; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 50 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 51 | static bool g_bootcharting = false; |
| 52 | |
| 53 | static FILE* g_stat_log; |
| 54 | static FILE* g_proc_log; |
| 55 | static FILE* g_disk_log; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 57 | static long long get_uptime_jiffies() { |
| 58 | std::string uptime; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 59 | if (!android::base::ReadFileToString("/proc/uptime", &uptime)) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 60 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | } |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 62 | return 100LL * strtod(uptime.c_str(), NULL); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 65 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 71 | utsname uts; |
| 72 | if (uname(&uts) == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Yabin Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 76 | std::string fingerprint = property_get("ro.build.fingerprint"); |
| 77 | if (fingerprint.empty()) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | std::string kernel_cmdline; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 82 | android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 83 | |
| 84 | FILE* out = fopen(LOG_HEADER, "we"); |
| 85 | if (out == NULL) { |
| 86 | return; |
| 87 | } |
Dan Willemsen | 30622bb | 2015-10-22 13:04:22 -0700 | [diff] [blame] | 88 | fprintf(out, "version = Android init 0.8\n"); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 89 | 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 Cui | 74edcea | 2015-07-24 10:11:05 -0700 | [diff] [blame] | 91 | fprintf(out, "system.release = %s\n", fingerprint.c_str()); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 92 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | fclose(out); |
| 96 | } |
| 97 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 98 | static void log_uptime(FILE* log) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 99 | fprintf(log, "%lld\n", get_uptime_jiffies()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 102 | static void log_file(FILE* log, const char* procfile) { |
| 103 | log_uptime(log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 105 | std::string content; |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 106 | if (android::base::ReadFileToString(procfile, &content)) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 107 | fprintf(log, "%s\n", content.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 111 | static void log_processes() { |
| 112 | log_uptime(g_proc_log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 114 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 115 | struct dirent* entry; |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 116 | while ((entry = readdir(dir.get())) != NULL) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 117 | // Only match numeric values. |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 118 | int pid = atoi(entry->d_name); |
| 119 | if (pid == 0) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 121 | // /proc/<pid>/stat only has truncated task names, so get the full |
| 122 | // name from /proc/<pid>/cmdline. |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 123 | std::string cmdline; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 124 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 141 | |
| 142 | fputc('\n', g_proc_log); |
| 143 | } |
| 144 | |
| 145 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | return 0; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 151 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 153 | // 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 Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 157 | return -1; |
| 158 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 159 | 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 Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 164 | 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 Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 170 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | log_header(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 180 | static void do_bootchart_step() { |
| 181 | log_file(g_stat_log, "/proc/stat"); |
| 182 | log_file(g_disk_log, "/proc/diskstats"); |
| 183 | log_processes(); |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 186 | static int do_bootchart_stop() { |
| 187 | if (!g_bootcharting) return 0; |
| 188 | |
Elliott Hughes | 5ee97e8 | 2016-10-28 12:30:32 -0700 | [diff] [blame] | 189 | LOG(INFO) << "Bootcharting finished"; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 190 | g_bootcharting = false; |
| 191 | fclose(g_stat_log); |
| 192 | fclose(g_disk_log); |
| 193 | fclose(g_proc_log); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int do_bootchart(const std::vector<std::string>& args) { |
| 198 | if (args[1] == "start") return do_bootchart_start(); |
| 199 | return do_bootchart_stop(); |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 202 | void bootchart_sample(int* timeout) { |
| 203 | // Do we have any more bootcharting to do? |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 204 | if (!g_bootcharting) return; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 205 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 206 | long long current_time = 10LL * get_uptime_jiffies(); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 207 | int elapsed_time = current_time - g_last_bootchart_time; |
| 208 | |
| 209 | if (elapsed_time >= BOOTCHART_POLLING_MS) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 210 | while (elapsed_time >= BOOTCHART_POLLING_MS) { |
| 211 | elapsed_time -= BOOTCHART_POLLING_MS; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 212 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 213 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 214 | g_last_bootchart_time = current_time; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 215 | do_bootchart_step(); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 216 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame^] | 217 | |
| 218 | // Schedule another? |
| 219 | if (g_bootcharting) { |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 220 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | } |