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" |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 23 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 29 | #include <chrono> |
| 30 | #include <condition_variable> |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 31 | #include <memory> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 32 | #include <mutex> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 33 | #include <thread> |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 34 | |
Luis Hector Chavez | 2980f57 | 2018-04-11 15:35:44 -0700 | [diff] [blame] | 35 | #include <android-base/chrono_utils.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include <android-base/file.h> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 38 | #include <android-base/properties.h> |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 41 | using android::base::StringPrintf; |
Luis Hector Chavez | 2980f57 | 2018-04-11 15:35:44 -0700 | [diff] [blame] | 42 | using android::base::boot_clock; |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 43 | using namespace std::chrono_literals; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace init { |
| 47 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 48 | static std::thread* g_bootcharting_thread; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 49 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 50 | static std::mutex g_bootcharting_finished_mutex; |
| 51 | static std::condition_variable g_bootcharting_finished_cv; |
| 52 | static bool g_bootcharting_finished; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 54 | static long long get_uptime_jiffies() { |
Luis Hector Chavez | 2980f57 | 2018-04-11 15:35:44 -0700 | [diff] [blame] | 55 | constexpr int64_t kNanosecondsPerJiffy = 10000000; |
| 56 | boot_clock::time_point uptime = boot_clock::now(); |
| 57 | return uptime.time_since_epoch().count() / kNanosecondsPerJiffy; |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename, |
| 61 | const char* mode) { |
| 62 | std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose); |
| 63 | if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename; |
| 64 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 67 | static void log_header() { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 68 | char date[32]; |
| 69 | time_t now_t = time(NULL); |
| 70 | struct tm now = *localtime(&now_t); |
| 71 | strftime(date, sizeof(date), "%F %T", &now); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 73 | utsname uts; |
| 74 | if (uname(&uts) == -1) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 76 | std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", ""); |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 77 | if (fingerprint.empty()) return; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 79 | std::string kernel_cmdline; |
| 80 | android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 81 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 82 | auto fp = fopen_unique("/data/bootchart/header", "we"); |
| 83 | if (!fp) return; |
| 84 | fprintf(&*fp, "version = Android init 0.8\n"); |
| 85 | fprintf(&*fp, "title = Boot chart for Android (%s)\n", date); |
| 86 | fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine); |
| 87 | fprintf(&*fp, "system.release = %s\n", fingerprint.c_str()); |
| 88 | // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm. |
| 89 | fprintf(&*fp, "system.cpu = %s\n", uts.machine); |
| 90 | fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 93 | static void log_uptime(FILE* log) { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 94 | fprintf(log, "%lld\n", get_uptime_jiffies()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 97 | static void log_file(FILE* log, const char* procfile) { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 98 | log_uptime(log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 100 | std::string content; |
| 101 | if (android::base::ReadFileToString(procfile, &content)) { |
| 102 | fprintf(log, "%s\n", content.c_str()); |
| 103 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 106 | static void log_processes(FILE* log) { |
| 107 | log_uptime(log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 108 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 109 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir); |
| 110 | struct dirent* entry; |
| 111 | while ((entry = readdir(dir.get())) != NULL) { |
| 112 | // Only match numeric values. |
| 113 | int pid = atoi(entry->d_name); |
| 114 | if (pid == 0) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 115 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 116 | // /proc/<pid>/stat only has truncated task names, so get the full |
| 117 | // name from /proc/<pid>/cmdline. |
| 118 | std::string cmdline; |
| 119 | android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline); |
| 120 | const char* full_name = cmdline.c_str(); // So we stop at the first NUL. |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 121 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 122 | // Read process stat line. |
| 123 | std::string stat; |
| 124 | if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) { |
| 125 | if (!cmdline.empty()) { |
| 126 | // Substitute the process name with its real name. |
| 127 | size_t open = stat.find('('); |
| 128 | size_t close = stat.find_last_of(')'); |
| 129 | if (open != std::string::npos && close != std::string::npos) { |
| 130 | stat.replace(open + 1, close - open - 1, full_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | } |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 132 | } |
| 133 | fputs(stat.c_str(), log); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fputc('\n', log); |
| 138 | } |
| 139 | |
| 140 | static void bootchart_thread_main() { |
| 141 | LOG(INFO) << "Bootcharting started"; |
| 142 | |
Jooyung Han | 7f8721b | 2022-04-22 12:05:09 +0900 | [diff] [blame] | 143 | // Unshare the mount namespace of this thread so that the init process itself can switch |
| 144 | // the mount namespace later while this thread is still running. |
| 145 | // Otherwise, setns() call invoked as part of `enter_default_mount_ns` fails with EINVAL. |
| 146 | // |
| 147 | // Note that after unshare()'ing the mount namespace from the main thread, this thread won't |
| 148 | // receive mount/unmount events from the other mount namespace unless the events are happening |
| 149 | // from under a sharable mount. |
| 150 | // |
| 151 | // The bootchart thread is safe to unshare the mount namespace because it only reads from /proc |
| 152 | // and write to /data which are not private mounts. |
| 153 | if (unshare(CLONE_NEWNS) == -1) { |
| 154 | PLOG(ERROR) << "Cannot create mount namespace"; |
| 155 | return; |
| 156 | } |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 157 | // Open log files. |
| 158 | auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we"); |
| 159 | if (!stat_log) return; |
| 160 | auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we"); |
| 161 | if (!proc_log) return; |
| 162 | auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we"); |
| 163 | if (!disk_log) return; |
| 164 | |
| 165 | log_header(); |
| 166 | |
| 167 | while (true) { |
| 168 | { |
| 169 | std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex); |
| 170 | g_bootcharting_finished_cv.wait_for(lock, 200ms); |
| 171 | if (g_bootcharting_finished) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 173 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 174 | log_file(&*stat_log, "/proc/stat"); |
| 175 | log_file(&*disk_log, "/proc/diskstats"); |
| 176 | log_processes(&*proc_log); |
| 177 | } |
| 178 | |
| 179 | LOG(INFO) << "Bootcharting finished"; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 182 | static Result<void> do_bootchart_start() { |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 183 | // We don't care about the content, but we do care that /data/bootchart/enabled actually exists. |
| 184 | std::string start; |
| 185 | if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) { |
| 186 | LOG(VERBOSE) << "Not bootcharting"; |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 187 | return {}; |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 188 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 190 | g_bootcharting_thread = new std::thread(bootchart_thread_main); |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 191 | return {}; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 194 | static Result<void> do_bootchart_stop() { |
| 195 | if (!g_bootcharting_thread) return {}; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 196 | |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 197 | // Tell the worker thread it's time to quit. |
| 198 | { |
| 199 | std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex); |
| 200 | g_bootcharting_finished = true; |
| 201 | g_bootcharting_finished_cv.notify_one(); |
| 202 | } |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 203 | |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 204 | g_bootcharting_thread->join(); |
| 205 | delete g_bootcharting_thread; |
| 206 | g_bootcharting_thread = nullptr; |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 207 | return {}; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 210 | Result<void> do_bootchart(const BuiltinArguments& args) { |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 211 | if (args[1] == "start") return do_bootchart_start(); |
| 212 | return do_bootchart_stop(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 214 | |
| 215 | } // namespace init |
| 216 | } // namespace android |